Skip to content

Commit

Permalink
Clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
eldy committed Feb 1, 2023
1 parent 1743a59 commit 1cde9fb
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
*
* TargetApi indicates that Lint should treat this type as targeting a given API level, no matter what the project target is.
*/
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public class MainActivity extends Activity implements OnItemSelectedListener {

private static final String LOG_TAG = "DoliDroidMainActivity";
Expand Down Expand Up @@ -98,6 +97,7 @@ public class MainActivity extends Activity implements OnItemSelectedListener {
public void onCreate(Bundle savedInstanceState) {
Log.i(LOG_TAG, "onCreate Running with SDK="+Build.VERSION.SDK_INT+" hardware menu="+Utils.hasMenuHardware(this));
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
boolean prefAlwaysShowBar = sharedPrefs.getBoolean("prefAlwaysShowBar", true);
Expand Down Expand Up @@ -131,9 +131,6 @@ public void onCreate(Bundle savedInstanceState) {
//this.savWindow.requestFeature(Window.FEATURE_PROGRESS);
//this.savWindow.setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);


setContentView(R.layout.activity_main);

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package com.nltechno.dolidroidpro;

import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
Expand All @@ -35,13 +34,10 @@
*
* TargetApi indicates that Lint should treat this type as targeting a given API level, no matter what the project target is.
*/
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public class MainActivityTest extends Activity implements OnItemSelectedListener {

private static final String LOG_TAG = "DoliDroidMainActivityTest";

final Activity activity = this;


/**
* Called when activity is created
Expand All @@ -50,7 +46,6 @@ public class MainActivityTest extends Activity implements OnItemSelectedListener
public void onCreate(Bundle savedInstanceState) {
Log.i(LOG_TAG, "onCreate Running with SDK=" + Build.VERSION.SDK_INT + " hardware menu=" + Utils.hasMenuHardware(this));
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_maintest);

}
Expand Down
152 changes: 51 additions & 101 deletions app/src/main/java/com/nltechno/dolidroidpro/ManageURLActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package com.nltechno.dolidroidpro;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
Expand All @@ -41,8 +40,11 @@
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import androidx.security.crypto.EncryptedSharedPreferences;
Expand All @@ -60,23 +62,27 @@
*
* @author [email protected]
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
public class ManageURLActivity extends Activity {

private static final String LOG_TAG = "DoliDroidManageURLActivity";
private String menuAre="hardwareonly";

static final int RESULT_ABOUT = RESULT_FIRST_USER;

ListView listView;


/**
* Called when activity is created
*/
@Override
public void onCreate(Bundle savedInstanceState) {
Log.i(LOG_TAG, "onCreate savedInstanceState="+savedInstanceState);
super.onCreate(savedInstanceState);
// Set the XML view to use
setContentView(R.layout.activity_manageurl);

SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
boolean prefAlwaysShowBar = sharedPrefs.getBoolean("prefAlwaysShowBar", true);
boolean prefAlwaysAutoFill = sharedPrefs.getBoolean("prefAlwaysAutoFill", true);
Log.d(LOG_TAG, "prefAlwaysShowBar="+prefAlwaysShowBar+" prefAlwaysAutoFill="+prefAlwaysAutoFill);
Expand All @@ -96,8 +102,6 @@ public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE); // Hide title with menus
}

setContentView(R.layout.activity_manageurl);

TextView t2 = findViewById(R.id.TextInstanceURLTitle);
t2.setMovementMethod(LinkMovementMethod.getInstance());

Expand All @@ -106,15 +110,15 @@ public void onCreate(Bundle savedInstanceState) {

Log.d(LOG_TAG, "Open file " + MainActivity.FILENAME+ " in directory "+getApplicationContext().getFilesDir().toString());

// Fill the list of Urls
ListView listViewOfUrls = (ListView) findViewById(R.id.list_view);
ArrayAdapter<String> arr = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
MainActivity.listOfRootUrl);
listViewOfUrls.setAdapter(arr);

// Update menu label to add the number of predefined URL into label
Button buttonClearAllUrl = findViewById(R.id.buttonClearAllUrl);
if (MainActivity.listOfRootUrl != null) {
buttonClearAllUrl.setText(getString(R.string.DeleteAllPredefinedUrl) + " (" + MainActivity.listOfRootUrl.size() + ")");
} else {
buttonClearAllUrl.setText(getString(R.string.DeleteAllPredefinedUrl) + " (0)");
}

//ScrollView scrollView_main = (ScrollView) findViewById(R.id.scrollView_main);

// Create listener to respond to click on button Delete current URL
// Not using the android:onClick tag is bugged. Declaring listener is also faster.
Expand Down Expand Up @@ -163,27 +167,31 @@ public void onClick(View v) {
}
});

// Update menu label to add the number of predefined URL into label
Button buttonClearAllUrl = findViewById(R.id.buttonClearAllUrl);
if (MainActivity.listOfRootUrl != null) {
buttonClearAllUrl.setText(getString(R.string.DeleteAllPredefinedUrl) + " (" + MainActivity.listOfRootUrl.size() + ")");
} else {
buttonClearAllUrl.setText(getString(R.string.DeleteAllPredefinedUrl) + " (0)");
}

// Create listener to respond to click on button Remove all predefined URL
// Not using the android:onClick tag is bugged. Declaring listener is also faster.
Button btn2 = findViewById(R.id.buttonClearAllUrl);
btn2.setOnClickListener(new View.OnClickListener()
{
buttonClearAllUrl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(LOG_TAG, "We click on Remove all predefined URLs");

FileOutputStream fos;
try
{
try {
File file = new File(getApplicationContext().getFilesDir().toString() + "/" + MainActivity.FILENAME);
Log.d(LOG_TAG, "Clear predefined URL list "+MainActivity.FILENAME+" (from ManageURLActivity) by deleting file with full path="+file.getAbsolutePath());
Log.d(LOG_TAG, "Clear predefined URL list " + MainActivity.FILENAME + " (from ManageURLActivity) by deleting file with full path=" + file.getAbsolutePath());
Boolean result = file.delete();
Log.d(LOG_TAG, result.toString());

MainActivity.listOfRootUrl = new ArrayList<String>(); // Clear array of menu entry
MainActivity.listOfRootUrl = new ArrayList<String>(); // Clear array of menu entry

// Now update button label entry
Button buttonClearAllUrl = findViewById(R.id.buttonClearAllUrl);
if (MainActivity.listOfRootUrl != null) {
buttonClearAllUrl.setText(getString(R.string.DeleteAllPredefinedUrl) + " (" + MainActivity.listOfRootUrl.size() + ")");
} else {
Expand All @@ -206,13 +214,10 @@ public void onClick(View v) {
editorEncrypted.commit();

Log.d(LOG_TAG, "The encrypted shared preferences file has been cleared");
}
catch(Exception e) {
} catch (Exception e) {
Log.w(LOG_TAG, "Failed to clear encrypted shared preferences file");
}
}
catch(Exception ioe)
{
} catch (Exception ioe) {
Log.e(LOG_TAG, "Error");
}
}
Expand All @@ -237,73 +242,9 @@ public void onStart() {
Button btn = findViewById(R.id.buttonDeletePredefinedUrl);


// Show text section 1
TextView textViewAbout1 = findViewById(R.id.TextAbout01);
String s1="";

PackageManager manager = this.getPackageManager();
try
{
PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);

s1+=getString(R.string.Version)+": <b>"+info.versionName+" (build "+info.versionCode+")</b><br />\n";
s1+=getString(R.string.VersionStaticResources)+": <b>"+SecondActivity.VERSION_RESOURCES+"</b><br />\n";

//s+= "PackageName = " + info.packageName + "\n";
s1+=getString(R.string.Author)+": <b>Laurent Destailleur</b><br />\n";
s1+=getString(R.string.Web)+": <span style=\"color:#008888\"><a href=\"https://www.dolicloud.com?origin=dolidroid&amp;utm_source=dolidroid&amp;utm_campaign=none&amp;utm_medium=mobile\">https://www.dolicloud.com</a></span><br />\n";
s1+=getString(R.string.Compatibility)+": <b>Dolibarr 8+</b><br />\n";
s1+=getString(R.string.License)+": <b>GPL v3+</b><br />\n";
//s1+=getString(R.string.Sources)+": https://www.nltechno.com/services/<br />\n";
s1+=getString(R.string.Sources)+": <span style=\"color:#008888\"><a href=\"https://github.com/DoliCloud/DoliDroid.git\">https://github.com/DoliCloud/DoliDroid.git</a></span><br />\n";
// This download key allow to download file with name src_dolidroid-info.versionName-downloadkey
//String downloadkey=Utils.MD5Hex("dolidroid"+info.versionName.replaceAll("[^0-9.]", "")+"saltnltechno").substring(0, 8);
//s1+=getString(R.string.Sources)+" Download Key: dolidroid-"+info.versionName.replaceAll("[^0-9.]", "")+"-"+downloadkey+"<br />\n";

s1+="<br />\n";

s1+=getString(R.string.DeviceAPILevel)+": <b>"+Build.VERSION.SDK_INT+"</b><br />\n";
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
s1+=getString(R.string.DeviceSize)+": <b>"+width+"x"+height+"</b><br />";
//s1+=getString(R.string.DeviceHasMenuHardware)+": <b>"+(Utils.hasMenuHardware(this)?getString(R.string.Yes):getString(R.string.No))+"</b><br />\n";
s1+=getString(R.string.DeviceHasDownloadManager)+": <b>"+(Utils.isDownloadManagerAvailable(this)?getString(R.string.Yes):getString(R.string.No))+"</b><br />\n";

// This return /storage/sdcard0/Download for example (we use this for downloading files)
String downloaddirpublic=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
// This return /storage/sdcard0 for example (we do not use this)
//String downloaddir="";
//if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) downloaddir = Environment.getExternalStorageDirectory().getAbsolutePath();
s1+=getString(R.string.DownloadDirectory)+": <b>"+downloaddirpublic+"</b><br />\n";

String photosdirpublic=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath();
//if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) photosdirpublic = Environment.getExternalStorageDirectory().getAbsolutePath();
s1+=getString(R.string.PhotosDirectory)+": <b>"+photosdirpublic+"</b><br />\n";

Intent testIntent = new Intent(Intent.ACTION_VIEW);
testIntent.setType("application/pdf");
List<ResolveInfo> list = manager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY);
s1+=getString(R.string.DeviceHasPDFViewer)+": <b>"+(list.size() > 0?getString(R.string.Yes)+" ("+list.size()+")":getString(R.string.No))+"</b><br />\n";

Intent testIntent2 = new Intent(Intent.ACTION_VIEW);
testIntent2.setType("application/vnd.oasis.opendocument.text");
List<ResolveInfo> list2 = manager.queryIntentActivities(testIntent2, PackageManager.MATCH_DEFAULT_ONLY);
s1+=getString(R.string.DeviceHasODXViewer)+": <b>"+(list2.size() > 0?getString(R.string.Yes)+" ("+list2.size()+")":getString(R.string.No))+"</b>\n";

//s+="Permissions = " + info.permissions;
}
catch(Exception e)
{
Log.e(LOG_TAG, e.getMessage());
}

// For api level 24: textViewAbout1.setText(Html.fromHtml(s1, Html.FROM_HTML_MODE_LEGACY));


// Show text section 2
// Show text title
TextView textViewAbout2 = findViewById(R.id.TextInstanceURLTitle);
String s2="";

Expand Down Expand Up @@ -419,18 +360,27 @@ public void onStart() {

// Update menu label to add the number of predefined URL into label
TextView textViewListOfUrl = findViewById(R.id.textListOfUrlsTitle);
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) textViewListOfUrl.getLayoutParams();
if (s2b != null && ! "".equals(s2b)) {
layoutParams.addRule(RelativeLayout.BELOW, R.id.buttonDeletePredefinedUrl);
} else {
layoutParams.addRule(RelativeLayout.BELOW, R.id.imageTop);
}
textViewListOfUrl.setLayoutParams(layoutParams);
if (MainActivity.listOfRootUrl != null) {
textViewListOfUrl.setText(getString(R.string.menu_manage_all_urls) + " (" + MainActivity.listOfRootUrl.size() + ")");
if (MainActivity.listOfRootUrl.size() > 1) {
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) textViewListOfUrl.getLayoutParams();
if (s2b != null && !"".equals(s2b)) {
layoutParams.addRule(RelativeLayout.BELOW, R.id.buttonDeletePredefinedUrl);
} else {
layoutParams.addRule(RelativeLayout.BELOW, R.id.imageTop);
}
textViewListOfUrl.setLayoutParams(layoutParams);
if (MainActivity.listOfRootUrl != null) {
textViewListOfUrl.setText(getString(R.string.menu_manage_all_urls) + " (" + MainActivity.listOfRootUrl.size() + ")");
} else {
textViewListOfUrl.setText(getString(R.string.menu_manage_all_urls) + " (0)");
}
} else {
textViewListOfUrl.setText(getString(R.string.menu_manage_all_urls) + " (0)");
textViewListOfUrl.setVisibility(View.INVISIBLE);
Button btnClearAll = findViewById(R.id.buttonClearAllUrl);
btnClearAll.setVisibility(View.INVISIBLE);
ListView listView = findViewById(R.id.list_view);
listView.setVisibility(View.INVISIBLE);
}

}

/**
Expand Down
12 changes: 4 additions & 8 deletions app/src/main/java/com/nltechno/dolidroidpro/SecondActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
import android.os.Parcelable;
import android.preference.PreferenceManager;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.app.DownloadManager;
Expand Down Expand Up @@ -112,7 +111,6 @@
/**
* Second activity class
*/
@TargetApi(Build.VERSION_CODES.M)
@SuppressLint("SetJavaScriptEnabled")
public class SecondActivity extends Activity {

Expand Down Expand Up @@ -221,9 +219,10 @@ public class SecondActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

Log.i(LOG_TAG, "onCreate savedInstanceState="+savedInstanceState);
super.onCreate(savedInstanceState);
// To have the view SecondActivity with WebView included:
setContentView(R.layout.activity_second);

// Read the non encrypted share preferences files
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Expand Down Expand Up @@ -258,7 +257,7 @@ public void onCreate(Bundle savedInstanceState)
//this.savWindow.setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);

Intent intent = getIntent();
String dolRootUrl = intent.getStringExtra("dolRootUrl");
String dolRootUrl = intent.getStringExtra("dolRootUrl");
String dolRequestUrl = intent.getStringExtra("dolRequestUrl");

this.savedDolRootUrl = dolRootUrl; // this include user:pass of http basic urls. Always end with /. Example: hTtP:https://user:[email protected]:xxx/
Expand Down Expand Up @@ -324,9 +323,6 @@ public void onCreate(Bundle savedInstanceState)
Log.d(LOG_TAG, "onCreate isDownloadManagerAvailable="+Utils.isDownloadManagerAvailable(this));
Log.d(LOG_TAG, "onCreate We are in onCreate and will load URL urlToGo=" + urlToGo);

// To have the view SecondActivity with WebView included:
setContentView(R.layout.activity_second);

progress = findViewById(R.id.progressBar1);
progress.setMax(100);
/*Drawable d=getResources().getDrawable(R.drawable.progressbar_style);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package com.nltechno.dolidroidpro;

import android.app.Activity;
import android.app.AlertDialog;
import android.webkit.SslErrorHandler;
import android.content.DialogInterface;
Expand Down
Loading

0 comments on commit 1cde9fb

Please sign in to comment.