diff options
author | Christian Schneppe <christian@pix-art.de> | 2018-03-12 21:26:56 +0100 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2018-03-12 21:27:40 +0100 |
commit | 0e61c65a31eeab6a12b3e79fe6b714bad0ef4aa9 (patch) | |
tree | 5a7ffcde1868f24a6634c9918501487ee425e1e9 /src/main | |
parent | f5854d238e4c8aecb5370e56301bd43015ad8ba9 (diff) |
rewrite how preference screens are invokved
Diffstat (limited to '')
-rw-r--r-- | src/main/AndroidManifest.xml | 4 | ||||
-rw-r--r-- | src/main/java/de/pixart/messenger/Config.java | 2 | ||||
-rw-r--r-- | src/main/java/de/pixart/messenger/ui/SettingsActivity.java | 185 | ||||
-rw-r--r-- | src/main/java/de/pixart/messenger/ui/SettingsFragment.java | 76 | ||||
-rw-r--r-- | src/main/res/xml/preferences.xml | 16 |
5 files changed, 181 insertions, 102 deletions
diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index 5f59388b8..961c4df2a 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -244,6 +244,10 @@ <meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".ui.SettingsActivity" /> + <intent-filter> + <action android:name="android.intent.action.VIEW" /> + <category android:name="android.intent.category.PREFERENCE" /> + </intent-filter> </activity> <activity android:name=".ui.UpdaterActivity" diff --git a/src/main/java/de/pixart/messenger/Config.java b/src/main/java/de/pixart/messenger/Config.java index 8783e1c64..60f2410aa 100644 --- a/src/main/java/de/pixart/messenger/Config.java +++ b/src/main/java/de/pixart/messenger/Config.java @@ -114,6 +114,8 @@ public final class Config { public static final boolean X509_VERIFICATION = false; //use x509 certificates to verify OMEMO keys + public static final boolean ONLY_INTERNAL_STORAGE = false; //use internal storage instead of sdcard to save attachments + public static final boolean IGNORE_ID_REWRITE_IN_MUC = true; public static final long MAM_MAX_CATCHUP = MILLISECONDS_IN_DAY * 5; diff --git a/src/main/java/de/pixart/messenger/ui/SettingsActivity.java b/src/main/java/de/pixart/messenger/ui/SettingsActivity.java index 1b744314e..cf1ccd658 100644 --- a/src/main/java/de/pixart/messenger/ui/SettingsActivity.java +++ b/src/main/java/de/pixart/messenger/ui/SettingsActivity.java @@ -1,6 +1,5 @@ package de.pixart.messenger.ui; -import android.support.v7.app.AlertDialog; import android.app.FragmentManager; import android.content.Context; import android.content.DialogInterface; @@ -8,6 +7,7 @@ import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.OnSharedPreferenceChangeListener; import android.content.pm.PackageManager; +import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.preference.CheckBoxPreference; @@ -16,12 +16,14 @@ import android.preference.Preference; import android.preference.PreferenceCategory; import android.preference.PreferenceManager; import android.preference.PreferenceScreen; +import android.support.v7.app.AlertDialog; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.widget.EditText; import android.widget.Toast; +import java.io.File; import java.security.KeyStoreException; import java.util.ArrayList; import java.util.Arrays; @@ -29,11 +31,11 @@ import java.util.Collections; import java.util.List; import java.util.Locale; -import de.pixart.messenger.services.MemorizingTrustManager; import de.pixart.messenger.Config; import de.pixart.messenger.R; import de.pixart.messenger.entities.Account; import de.pixart.messenger.services.ExportLogsService; +import de.pixart.messenger.services.MemorizingTrustManager; import de.pixart.messenger.xmpp.XmppConnection; import de.pixart.messenger.xmpp.jid.InvalidJidException; import de.pixart.messenger.xmpp.jid.Jid; @@ -70,6 +72,10 @@ public class SettingsActivity extends XmppActivity implements mSettingsFragment = new SettingsFragment(); fm.beginTransaction().replace(android.R.id.content, mSettingsFragment).commit(); } + mSettingsFragment.setActivityIntent(getIntent()); + + this.mTheme = findTheme(); + setTheme(this.mTheme); } @Override @@ -83,7 +89,9 @@ public class SettingsActivity extends XmppActivity implements PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this); multiAccountPreference = mSettingsFragment.findPreference("enable_multi_accounts"); - isMultiAccountChecked = ((CheckBoxPreference) multiAccountPreference).isChecked(); + if (multiAccountPreference != null) { + isMultiAccountChecked = ((CheckBoxPreference) multiAccountPreference).isChecked(); + } ListPreference resources = (ListPreference) mSettingsFragment.findPreference("resource"); if (resources != null) { @@ -121,9 +129,8 @@ public class SettingsActivity extends XmppActivity implements } final Preference removeCertsPreference = mSettingsFragment.findPreference("remove_trusted_certificates"); - removeCertsPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { - @Override - public boolean onPreferenceClick(Preference preference) { + if (removeCertsPreference != null) { + removeCertsPreference.setOnPreferenceClickListener(preference -> { final MemorizingTrustManager mtm = xmppConnectionService.getMemorizingTrustManager(); final ArrayList<String> aliases = Collections.list(mtm.getCertificates()); if (aliases.size() == 0) { @@ -134,44 +141,37 @@ public class SettingsActivity extends XmppActivity implements final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(SettingsActivity.this); dialogBuilder.setTitle(getResources().getString(R.string.dialog_manage_certs_title)); dialogBuilder.setMultiChoiceItems(aliases.toArray(new CharSequence[aliases.size()]), null, - new DialogInterface.OnMultiChoiceClickListener() { - @Override - public void onClick(DialogInterface dialog, int indexSelected, - boolean isChecked) { - if (isChecked) { - selectedItems.add(indexSelected); - } else if (selectedItems.contains(indexSelected)) { - selectedItems.remove(Integer.valueOf(indexSelected)); - } - if (selectedItems.size() > 0) - ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(true); - else { - ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(false); - } + (dialog, indexSelected, isChecked) -> { + if (isChecked) { + selectedItems.add(indexSelected); + } else if (selectedItems.contains(indexSelected)) { + selectedItems.remove(Integer.valueOf(indexSelected)); + } + if (selectedItems.size() > 0) + ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(true); + else { + ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(false); } }); dialogBuilder.setPositiveButton( - getResources().getString(R.string.dialog_manage_certs_positivebutton), new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - int count = selectedItems.size(); - if (count > 0) { - for (int i = 0; i < count; i++) { - try { - Integer item = Integer.valueOf(selectedItems.get(i).toString()); - String alias = aliases.get(item); - mtm.deleteCertificate(alias); - } catch (KeyStoreException e) { - e.printStackTrace(); - displayToast("Error: " + e.getLocalizedMessage()); - } - } - if (xmppConnectionServiceBound) { - reconnectAccounts(); + getResources().getString(R.string.dialog_manage_certs_positivebutton), (dialog, which) -> { + int count = selectedItems.size(); + if (count > 0) { + for (int i = 0; i < count; i++) { + try { + Integer item = Integer.valueOf(selectedItems.get(i).toString()); + String alias = aliases.get(item); + mtm.deleteCertificate(alias); + } catch (KeyStoreException e) { + e.printStackTrace(); + displayToast("Error: " + e.getLocalizedMessage()); } - displayToast(getResources().getQuantityString(R.plurals.toast_delete_certificates, count, count)); } + if (xmppConnectionServiceBound) { + reconnectAccounts(); + } + displayToast(getResources().getQuantityString(R.plurals.toast_delete_certificates, count, count)); } }); dialogBuilder.setNegativeButton(getResources().getString(R.string.dialog_manage_certs_negativebutton), null); @@ -179,33 +179,41 @@ public class SettingsActivity extends XmppActivity implements removeCertsDialog.show(); removeCertsDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false); return true; - } - }); + }); + } final Preference exportLogsPreference = mSettingsFragment.findPreference("export_logs"); - exportLogsPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { - @Override - public boolean onPreferenceClick(Preference preference) { + if (exportLogsPreference != null) { + exportLogsPreference.setOnPreferenceClickListener(preference -> { if (hasStoragePermission(REQUEST_WRITE_LOGS)) { startExport(); } return true; + }); + } + + if (Config.ONLY_INTERNAL_STORAGE) { + final Preference cleanCachePreference = mSettingsFragment.findPreference("clean_cache"); + if (cleanCachePreference != null) { + cleanCachePreference.setOnPreferenceClickListener(preference -> cleanCache()); } - }); - final Preference deleteOmemoPreference = mSettingsFragment.findPreference("delete_omemo_identities"); - deleteOmemoPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { - @Override - public boolean onPreferenceClick(Preference preference) { - deleteOmemoIdentities(); - return true; + final Preference cleanPrivateStoragePreference = mSettingsFragment.findPreference("clean_private_storage"); + if (cleanPrivateStoragePreference != null) { + cleanPrivateStoragePreference.setOnPreferenceClickListener(preference -> cleanPrivateStorage()); } - }); + } + + final Preference deleteOmemoPreference = mSettingsFragment.findPreference("delete_omemo_identities"); + if (deleteOmemoPreference != null) { + deleteOmemoPreference.setOnPreferenceClickListener(preference -> deleteOmemoIdentities()); + } final Preference enableMultiAccountsPreference = mSettingsFragment.findPreference("enable_multi_accounts"); - Log.d(Config.LOGTAG, "Multi account checkbox checked: " + isMultiAccountChecked); - if (isMultiAccountChecked) { - enableMultiAccountsPreference.setEnabled(false); + if (enableMultiAccountsPreference != null) { + Log.d(Config.LOGTAG, "Multi account checkbox checked: " + isMultiAccountChecked); + if (isMultiAccountChecked) { + enableMultiAccountsPreference.setEnabled(false); /* if (xmppConnectionServiceBound) { // todo doesn't work --> it seems the service is never bound final List<Account> accounts = xmppConnectionService.getAccounts(); @@ -221,12 +229,13 @@ public class SettingsActivity extends XmppActivity implements enableMultiAccountsPreference.setEnabled(false); } */ - } else { - enableMultiAccountsPreference.setEnabled(true); - enableMultiAccountsPreference.setOnPreferenceClickListener(preference -> { - enableMultiAccounts(); - return true; - }); + } else { + enableMultiAccountsPreference.setEnabled(true); + enableMultiAccountsPreference.setOnPreferenceClickListener(preference -> { + enableMultiAccounts(); + return true; + }); + } } } @@ -234,7 +243,60 @@ public class SettingsActivity extends XmppActivity implements return i != null && getPackageManager().queryIntentActivities(i, PackageManager.MATCH_DEFAULT_ONLY).size() > 0; } - private void deleteOmemoIdentities() { + private boolean cleanCache() { + Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS); + intent.setData(Uri.parse("package:" + getPackageName())); + startActivity(intent); + return true; + } + + private boolean cleanPrivateStorage() { + cleanPrivatePictures(); + cleanPrivateFiles(); + return true; + } + + private void cleanPrivatePictures() { + try { + File dir = new File(getFilesDir().getAbsolutePath(), "/Pictures/"); + File[] array = dir.listFiles(); + if (array != null) { + for (int b = 0; b < array.length; b++) { + String name = array[b].getName().toLowerCase(); + if (name.equals(".nomedia")) { + continue; + } + if (array[b].isFile()) { + array[b].delete(); + } + } + } + } catch (Throwable e) { + Log.e("CleanCache", e.toString()); + } + } + + private void cleanPrivateFiles() { + try { + File dir = new File(getFilesDir().getAbsolutePath(), "/Files/"); + File[] array = dir.listFiles(); + if (array != null) { + for (int b = 0; b < array.length; b++) { + String name = array[b].getName().toLowerCase(); + if (name.equals(".nomedia")) { + continue; + } + if (array[b].isFile()) { + array[b].delete(); + } + } + } + } catch (Throwable e) { + Log.e("CleanCache", e.toString()); + } + } + + private boolean deleteOmemoIdentities() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(R.string.pref_delete_omemo_identities); final List<CharSequence> accounts = new ArrayList<>(); @@ -281,6 +343,7 @@ public class SettingsActivity extends XmppActivity implements AlertDialog dialog = builder.create(); dialog.show(); dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false); + return true; } private void enableMultiAccounts() { diff --git a/src/main/java/de/pixart/messenger/ui/SettingsFragment.java b/src/main/java/de/pixart/messenger/ui/SettingsFragment.java index 8cea5eb61..939ef6145 100644 --- a/src/main/java/de/pixart/messenger/ui/SettingsFragment.java +++ b/src/main/java/de/pixart/messenger/ui/SettingsFragment.java @@ -1,64 +1,58 @@ package de.pixart.messenger.ui; -import android.app.Dialog; +import android.content.Intent; import android.os.Bundle; import android.preference.Preference; +import android.preference.PreferenceCategory; import android.preference.PreferenceFragment; import android.preference.PreferenceScreen; -import android.view.View; -import android.view.ViewGroup; -import android.view.ViewParent; -import android.widget.FrameLayout; -import android.widget.LinearLayout; +import android.text.TextUtils; +import de.pixart.messenger.Config; import de.pixart.messenger.R; public class SettingsFragment extends PreferenceFragment { - //http://stackoverflow.com/questions/16374820/action-bar-home-button-not-functional-with-nested-preferencescreen/16800527#16800527 - private void initializeActionBar(PreferenceScreen preferenceScreen) { - final Dialog dialog = preferenceScreen.getDialog(); + private String page = null; - if (dialog != null) { - View homeBtn = dialog.findViewById(android.R.id.home); + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + addPreferencesFromResource(R.xml.preferences); - if (homeBtn != null) { - View.OnClickListener dismissDialogClickListener = new View.OnClickListener() { - @Override - public void onClick(View v) { - dialog.dismiss(); - } - }; + // Remove from standard preferences if the flag ONLY_INTERNAL_STORAGE is false + if (!Config.ONLY_INTERNAL_STORAGE) { + PreferenceCategory mCategory = (PreferenceCategory) findPreference("security_options"); + if (mCategory != null) { + Preference cleanCache = findPreference("clean_cache"); + Preference cleanPrivateStorage = findPreference("clean_private_storage"); + mCategory.removePreference(cleanCache); + mCategory.removePreference(cleanPrivateStorage); + } + } - ViewParent homeBtnContainer = homeBtn.getParent(); + if (!TextUtils.isEmpty(page)) { + openPreferenceScreen(page); + } + } - if (homeBtnContainer instanceof FrameLayout) { - ViewGroup containerParent = (ViewGroup) homeBtnContainer.getParent(); - if (containerParent instanceof LinearLayout) { - containerParent.setOnClickListener(dismissDialogClickListener); - } else { - ((FrameLayout) homeBtnContainer).setOnClickListener(dismissDialogClickListener); - } - } else { - homeBtn.setOnClickListener(dismissDialogClickListener); + public void setActivityIntent(final Intent intent) { + if (intent != null) { + if (Intent.ACTION_VIEW.equals(intent.getAction())) { + if (intent.getExtras() != null) { + this.page = intent.getExtras().getString("page"); } } } } - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - // Load the preferences from an XML resource - addPreferencesFromResource(R.xml.preferences); - } - - @Override - public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { - super.onPreferenceTreeClick(preferenceScreen, preference); - if (preference instanceof PreferenceScreen) { - initializeActionBar((PreferenceScreen) preference); + private void openPreferenceScreen(final String screenName) { + final Preference pref = findPreference(screenName); + if (pref instanceof PreferenceScreen) { + final PreferenceScreen preferenceScreen = (PreferenceScreen) pref; + getActivity().setTitle(preferenceScreen.getTitle()); + preferenceScreen.setDependency(""); + setPreferenceScreen((PreferenceScreen) pref); } - return false; } } diff --git a/src/main/res/xml/preferences.xml b/src/main/res/xml/preferences.xml index 0343d1171..bfcffe012 100644 --- a/src/main/res/xml/preferences.xml +++ b/src/main/res/xml/preferences.xml @@ -65,6 +65,14 @@ android:key="quiet_hours" android:summary="@string/pref_quiet_hours_summary" android:title="@string/title_pref_quiet_hours"> + <intent + android:action="android.intent.action.VIEW" + android:targetClass="de.pixart.messenger.ui.SettingsActivity" + android:targetPackage="de.pixart.messenger"> + <extra + android:name="page" + android:value="quiet_hours" /> + </intent> <CheckBoxPreference android:defaultValue="@bool/enable_quiet_hours" android:key="enable_quiet_hours" @@ -247,6 +255,14 @@ android:icon="@drawable/ic_settings_black_24dp" android:key="expert" android:title="@string/pref_expert_options"> + <intent + android:action="android.intent.action.VIEW" + android:targetClass="de.pixart.messenger.ui.SettingsActivity" + android:targetPackage="de.pixart.messenger"> + <extra + android:name="page" + android:value="expert" /> + </intent> <PreferenceCategory android:title="@string/pref_general" android:key="general"> <ListPreference |