Skip to content

Commit

Permalink
WIP enhance the Manage Url page
Browse files Browse the repository at this point in the history
  • Loading branch information
eldy committed Mar 15, 2023
1 parent 6314f70 commit 32154ac
Show file tree
Hide file tree
Showing 9 changed files with 282 additions and 174 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package com.nltechno.dolidroidpro;

import java.util.ArrayList;
import java.util.List;

import com.nltechno.utils.Utils;
Expand Down Expand Up @@ -186,10 +187,11 @@ public void onStart() {
try
{
Log.d(LOG_TAG, "Loop on listOfRootUrl "+MainActivity.listOfRootUrl.size());

// Now loop of each entry and rewrite or exclude it
for (int i = 0; i < MainActivity.listOfRootUrl.size(); i++)
{
String s=MainActivity.listOfRootUrl.get(i);
String s = MainActivity.listOfRootUrl.get(i).url;
Log.d(LOG_TAG, "Check for s="+s+" equal to savedDolRootUrl="+savedDolRootUrl);
if (s.equals(savedDolRootUrl)) // Add new value into saved list
{
Expand Down
69 changes: 52 additions & 17 deletions app/src/main/java/com/nltechno/dolidroidpro/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public class MainActivity extends Activity implements OnItemSelectedListener {
private static final String LOG_TAG = "DoliDroidMainActivity";
public final static String FILENAME = "dolidroid_prefs"; // File will be into
private final static String HOME_URL = "";
public static List<String> listOfRootUrl = null;
public static List<PredefinedUrl> listOfRootUrl = null;
public static int nbOfEntries = 0;
private boolean firstCallToOnStart = Boolean.TRUE;

Expand Down Expand Up @@ -183,12 +183,15 @@ public void onStart() {

SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

this.listOfRootUrl = new ArrayList<String>();
// The array to contains the list of all predefined URLs
// This list is saved into a file named FILENAME
this.listOfRootUrl = new ArrayList<PredefinedUrl>();

//ArrayAdapter <CharSequence> adapter = new ArrayAdapter <CharSequence> (this, android.R.layout.simple_spinner_item);
//adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter <CharSequence> adapter = new ArrayAdapter <CharSequence> (this, R.layout.main_spinner_item); // Set style for selected visible value
adapter.setDropDownViewResource(R.layout.main_spinner_item); // Set style for dropdown box

this.nbOfEntries=0;
try {
FileInputStream fis = openFileInput(FILENAME);
Expand All @@ -201,19 +204,36 @@ public void onStart() {
while ((strLine = br.readLine()) != null) {
// Print the content on the console
Log.d(LOG_TAG, "Found entry " + this.nbOfEntries + " : " + strLine);
if (! this.listOfRootUrl.contains(strLine))
{
// Check if entry already present
int count = 0;
boolean entryfound = false;
while (count < this.listOfRootUrl.size()) {
if (this.listOfRootUrl.get(count).url == strLine) {
entryfound = true;
break;
}
count++;
}

if (! entryfound) {
this.nbOfEntries++;
if (this.nbOfEntries == 1)
{
homeUrlFirstFound = strLine;
}
this.listOfRootUrl.add(strLine);

// Add new entry into the array this.listOfRootUrl
PredefinedUrl tmppredefinedurl = new PredefinedUrl();
tmppredefinedurl.url = strLine;
this.listOfRootUrl.add(tmppredefinedurl);
} else {
Log.d(LOG_TAG, "Duplicate");
}
}
Collections.sort(this.listOfRootUrl);

// TODO Sort the array
//Collections.sort(this.listOfRootUrl);

// Close the input stream
in.close();
} catch (Exception e) {// Catch exception if any
Expand All @@ -231,27 +251,27 @@ public void onStart() {
// Set entries to adapter
for (int i = 0; i < this.listOfRootUrl.size(); i++)
{
adapter.add(this.listOfRootUrl.get(i));
adapter.add(this.listOfRootUrl.get(i).url);
}

// Show combo list if there is at least 1 choice
Spinner spinner1 = findViewById(R.id.combo_list_of_urls);
Spinner spinner_for_list_of_predefined_entries = findViewById(R.id.combo_list_of_urls);
TextView texViewLink = findViewById(R.id.textViewLink);

if (this.nbOfEntries > 0) {
spinner1.setAdapter(adapter);
spinner1.setVisibility(View.VISIBLE);
spinner_for_list_of_predefined_entries.setAdapter(adapter);
spinner_for_list_of_predefined_entries.setVisibility(View.VISIBLE);
texViewLink.setVisibility(View.INVISIBLE);

if (this.nbOfEntries == 1)
{
Log.d(LOG_TAG, "Set selection to = "+homeUrlFirstFound);
// Only one URL known, we autoselect it
//spinner1.setSelection(1, false);
//spinner_for_list_of_predefined_entries.setSelection(1, false);
homeUrlToSuggest=homeUrlFirstFound;
}
} else {
spinner1.setVisibility(View.INVISIBLE);
spinner_for_list_of_predefined_entries.setVisibility(View.INVISIBLE);
texViewLink.setVisibility(View.VISIBLE);
}

Expand All @@ -262,7 +282,7 @@ public void onStart() {
// If listener was not already added, we add one
if (firstCallToOnStart) {
Log.d(LOG_TAG, "First call to onStart");
spinner1.setOnItemSelectedListener(this); // Enable handler with onItemSelected and onNothingSelected
spinner_for_list_of_predefined_entries.setOnItemSelectedListener(this); // Enable handler with onItemSelected and onNothingSelected
firstCallToOnStart = false;
editText1.addTextChangedListener(fieldValidatorTextWatcher);
editText1.setText(homeUrlToSuggest);
Expand Down Expand Up @@ -639,17 +659,32 @@ public void openDolUrl(View button) throws IOException
fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
for (int i = 0; i < this.listOfRootUrl.size(); i++)
{
String s=this.listOfRootUrl.get(i)+"\n";
String s = this.listOfRootUrl.get(i).url+"\n";
Log.d(LOG_TAG, "write " + s);
fos.write(s.getBytes());
}
if (! this.listOfRootUrl.contains(dolRootUrl)) // Add new value into saved list

// Check if entry already present
int count = 0;
boolean entryfound = false;
while (count < this.listOfRootUrl.size()) {
if (this.listOfRootUrl.get(count).url == dolRootUrl) {
entryfound = true;
break;
}
count++;
}

if (! entryfound) // Add new value into saved list
{
// Add entry into file on disk
Log.d(LOG_TAG, "write new value " + Utils.bytesToString(dolRootUrl.getBytes()));
fos.write(dolRootUrl.getBytes());
// Add entry also into this.listOfRootUrl
this.listOfRootUrl.add(dolRootUrl);

// Add new entry into the array this.listOfRootUrl
PredefinedUrl tmppredefinedurl = new PredefinedUrl();
tmppredefinedurl.url = dolRootUrl;
this.listOfRootUrl.add(tmppredefinedurl);
}
fos.close();
}
Expand Down
83 changes: 59 additions & 24 deletions app/src/main/java/com/nltechno/dolidroidpro/ManageURLActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,26 @@ public void onCreate(Bundle savedInstanceState) {

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

// Fill the list of Urls
// Define an array with string to show into the ArrayAdapter list
ArrayList<String> listofRootUrlString = new ArrayList<String>();
ArrayList<String> listofRootUrlStringEmpty = new ArrayList<String>();
int count = 0;
while (MainActivity.listOfRootUrl.size() > count) {
listofRootUrlString.add(MainActivity.listOfRootUrl.get(count).url);
count++;
}
// Fill the list of Urls into the ArrayAdapter
ListView listViewOfUrls = (ListView) findViewById(R.id.listViewConnections);
ArrayAdapter<String> arr = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_activated_1,
MainActivity.listOfRootUrl);
listofRootUrlString);
ArrayAdapter<String> arrempty = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_activated_1,
listofRootUrlStringEmpty);
listViewOfUrls.setAdapter(arr);

//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.
Button btn = findViewById(R.id.buttonDeletePredefinedUrl);
Expand All @@ -125,18 +135,19 @@ public void onClick(View v) {
Intent intent = getIntent();
String savedDolRootUrl = intent.getStringExtra("savedDolRootUrl");

// Now loop of each entry and rewrite or exclude it
// Now loop of each entry of file MainActivity.FILENAME and rewrite or exclude the entry
fos = openFileOutput(MainActivity.FILENAME, Context.MODE_PRIVATE);
int itoremove = -1;
int sizeofarray = MainActivity.listOfRootUrl.size();
for (int i = 0; i < sizeofarray; i++)
{
String s=MainActivity.listOfRootUrl.get(i);
if (! s.equals(savedDolRootUrl)) // Add new value into saved list
{
String s = MainActivity.listOfRootUrl.get(i).url;
if (! s.equals(savedDolRootUrl)) {
// Keep this value s
Log.d(LOG_TAG, "write " + s);
fos.write((s+"\n").getBytes());
} else {
// We fount the entry to remove
Log.d(LOG_TAG, "exclude entry " + s);
btn.setEnabled(false);
btn.setTextColor(Color.LTGRAY);
Expand All @@ -157,6 +168,7 @@ 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) {
Expand All @@ -172,23 +184,27 @@ public void onClick(View v) {
public void onClick(View v) {
Log.d(LOG_TAG, "We click on Remove all predefined URLs");

FileOutputStream fos;
try {
// Delete the file of predefined URLs MainActivity.FILENAME
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());
Boolean result = file.delete();
Log.d(LOG_TAG, result.toString());
boolean result = file.delete();
Log.d(LOG_TAG, result ? "true" : "false");

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

// Now update button label entry
if (MainActivity.listOfRootUrl != null) {
buttonClearAllUrl.setText(getString(R.string.DeleteAllPredefinedUrl) + " (" + MainActivity.listOfRootUrl.size() + ")");
} else {
buttonClearAllUrl.setText(getString(R.string.DeleteAllPredefinedUrl) + " (0)");
}
buttonClearAllUrl.setText(getString(R.string.DeleteAllPredefinedUrl));
buttonClearAllUrl.setEnabled(true);

listViewOfUrls.setAdapter(arrempty);
TextView textViewListOfUrl = findViewById(R.id.textListOfUrlsTitle);
TextView textViewListOfUrl2 = findViewById(R.id.textListOfUrlsTitle2);
textViewListOfUrl.setText(getString(R.string.menu_manage_all_urls));
textViewListOfUrl.setVisibility(View.VISIBLE);
textViewListOfUrl2.setVisibility(View.VISIBLE);

// Clear saved login / pass too
// Clear also the list of saved login / pass too
try {
//SharedPreferences sharedPrefsEncrypted = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC);
Expand All @@ -200,7 +216,7 @@ public void onClick(View v) {
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
);
SharedPreferences.Editor editorEncrypted = sharedPrefsEncrypted.edit();
editorEncrypted.clear();
editorEncrypted.clear(); // delete the file
editorEncrypted.commit();

Log.d(LOG_TAG, "The encrypted shared preferences file has been cleared");
Expand Down Expand Up @@ -281,7 +297,8 @@ public void onStart() {
// Now loop of each entry and rewrite or exclude it
for (int i = 0; i < MainActivity.listOfRootUrl.size(); i++)
{
String s=MainActivity.listOfRootUrl.get(i);
PredefinedUrl tmppredefinedurl = MainActivity.listOfRootUrl.get(i);
String s = tmppredefinedurl.url;
Log.d(LOG_TAG, "Check for s="+s+" equal to savedDolRootUrl="+savedDolRootUrl);
if (s.equals(savedDolRootUrl)) // Add new value into saved list
{
Expand Down Expand Up @@ -353,23 +370,41 @@ public void onStart() {
}


// Update menu label to add the number of predefined URL into label
// Update the menu label to add the number of predefined URL into label
TextView textViewListOfUrl = findViewById(R.id.textListOfUrlsTitle);
if (MainActivity.listOfRootUrl.size() > 1) {
TextView textViewListOfUrl2 = findViewById(R.id.textListOfUrlsTitle2);
if (MainActivity.listOfRootUrl.size() >= 1) {
// Set position of textViewListOfUrl
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() + ")");
textViewListOfUrl.setVisibility(View.VISIBLE);
textViewListOfUrl2.setVisibility(View.INVISIBLE);
} else {
textViewListOfUrl.setText(getString(R.string.menu_manage_all_urls) + " (0)");
textViewListOfUrl.setText(getString(R.string.menu_manage_all_urls));
textViewListOfUrl.setVisibility(View.VISIBLE);
textViewListOfUrl2.setVisibility(View.VISIBLE);
}
} else {
textViewListOfUrl.setVisibility(View.INVISIBLE);
// Set position of textViewListOfUrl
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);

textViewListOfUrl.setVisibility(View.VISIBLE);
textViewListOfUrl2.setVisibility(View.VISIBLE);

Button btnClearAll = findViewById(R.id.buttonClearAllUrl);
btnClearAll.setVisibility(View.INVISIBLE);
ListView listView = findViewById(R.id.listViewConnections);
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/java/com/nltechno/dolidroidpro/PredefinedUrl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.nltechno.dolidroidpro;

/**
* Class of a given predefined URL entry
*/
public class PredefinedUrl {
public String url;
public String logo;
public String basicauthlogin;
public String basicauthpass;
public int position = 100;
}
Loading

0 comments on commit 32154ac

Please sign in to comment.