rewrite how preference screens are invokved

This commit is contained in:
Daniel Gultsch 2018-02-16 19:49:14 +01:00
parent de0272fd1a
commit a25c5ed401
4 changed files with 104 additions and 122 deletions

View file

@ -186,6 +186,10 @@
<meta-data <meta-data
android:name="android.support.PARENT_ACTIVITY" android:name="android.support.PARENT_ACTIVITY"
android:value="eu.siacs.conversations.ui.SettingsActivity" /> android:value="eu.siacs.conversations.ui.SettingsActivity" />
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.PREFERENCE" />
</intent-filter>
</activity> </activity>
<activity android:name="com.soundcloud.android.crop.CropImageActivity" /> <activity android:name="com.soundcloud.android.crop.CropImageActivity" />
<activity android:name=".ui.MemorizingActivity"/> <activity android:name=".ui.MemorizingActivity"/>

View file

@ -7,7 +7,7 @@ import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener; import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
@ -63,6 +63,7 @@ public class SettingsActivity extends XmppActivity implements
mSettingsFragment = new SettingsFragment(); mSettingsFragment = new SettingsFragment();
fm.beginTransaction().replace(android.R.id.content, mSettingsFragment).commit(); fm.beginTransaction().replace(android.R.id.content, mSettingsFragment).commit();
} }
mSettingsFragment.setActivityIntent(getIntent());
this.mTheme = findTheme(); this.mTheme = findTheme();
setTheme(this.mTheme); setTheme(this.mTheme);
@ -132,9 +133,8 @@ public class SettingsActivity extends XmppActivity implements
} }
final Preference removeCertsPreference = mSettingsFragment.findPreference("remove_trusted_certificates"); final Preference removeCertsPreference = mSettingsFragment.findPreference("remove_trusted_certificates");
removeCertsPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { if (removeCertsPreference != null) {
@Override removeCertsPreference.setOnPreferenceClickListener(preference -> {
public boolean onPreferenceClick(Preference preference) {
final MemorizingTrustManager mtm = xmppConnectionService.getMemorizingTrustManager(); final MemorizingTrustManager mtm = xmppConnectionService.getMemorizingTrustManager();
final ArrayList<String> aliases = Collections.list(mtm.getCertificates()); final ArrayList<String> aliases = Collections.list(mtm.getCertificates());
if (aliases.size() == 0) { if (aliases.size() == 0) {
@ -145,10 +145,7 @@ public class SettingsActivity extends XmppActivity implements
final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(SettingsActivity.this); final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(SettingsActivity.this);
dialogBuilder.setTitle(getResources().getString(R.string.dialog_manage_certs_title)); dialogBuilder.setTitle(getResources().getString(R.string.dialog_manage_certs_title));
dialogBuilder.setMultiChoiceItems(aliases.toArray(new CharSequence[aliases.size()]), null, dialogBuilder.setMultiChoiceItems(aliases.toArray(new CharSequence[aliases.size()]), null,
new DialogInterface.OnMultiChoiceClickListener() { (dialog, indexSelected, isChecked) -> {
@Override
public void onClick(DialogInterface dialog, int indexSelected,
boolean isChecked) {
if (isChecked) { if (isChecked) {
selectedItems.add(indexSelected); selectedItems.add(indexSelected);
} else if (selectedItems.contains(indexSelected)) { } else if (selectedItems.contains(indexSelected)) {
@ -159,13 +156,10 @@ public class SettingsActivity extends XmppActivity implements
else { else {
((AlertDialog) dialog).getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(false); ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(false);
} }
}
}); });
dialogBuilder.setPositiveButton( dialogBuilder.setPositiveButton(
getResources().getString(R.string.dialog_manage_certs_positivebutton), new DialogInterface.OnClickListener() { getResources().getString(R.string.dialog_manage_certs_positivebutton), (dialog, which) -> {
@Override
public void onClick(DialogInterface dialog, int which) {
int count = selectedItems.size(); int count = selectedItems.size();
if (count > 0) { if (count > 0) {
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
@ -183,55 +177,41 @@ public class SettingsActivity extends XmppActivity implements
} }
displayToast(getResources().getQuantityString(R.plurals.toast_delete_certificates, count, count)); displayToast(getResources().getQuantityString(R.plurals.toast_delete_certificates, count, count));
} }
}
}); });
dialogBuilder.setNegativeButton(getResources().getString(R.string.dialog_manage_certs_negativebutton), null); dialogBuilder.setNegativeButton(getResources().getString(R.string.dialog_manage_certs_negativebutton), null);
AlertDialog removeCertsDialog = dialogBuilder.create(); AlertDialog removeCertsDialog = dialogBuilder.create();
removeCertsDialog.show(); removeCertsDialog.show();
removeCertsDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false); removeCertsDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
return true; return true;
}
}); });
}
final Preference exportLogsPreference = mSettingsFragment.findPreference("export_logs"); final Preference exportLogsPreference = mSettingsFragment.findPreference("export_logs");
exportLogsPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { if (exportLogsPreference != null) {
@Override exportLogsPreference.setOnPreferenceClickListener(preference -> {
public boolean onPreferenceClick(Preference preference) {
if (hasStoragePermission(REQUEST_WRITE_LOGS)) { if (hasStoragePermission(REQUEST_WRITE_LOGS)) {
startExport(); startExport();
} }
return true; return true;
}
}); });
}
if (Config.ONLY_INTERNAL_STORAGE) { if (Config.ONLY_INTERNAL_STORAGE) {
final Preference cleanCachePreference = mSettingsFragment.findPreference("clean_cache"); final Preference cleanCachePreference = mSettingsFragment.findPreference("clean_cache");
cleanCachePreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { if (cleanCachePreference != null) {
@Override cleanCachePreference.setOnPreferenceClickListener(preference -> cleanCache());
public boolean onPreferenceClick(Preference preference) {
cleanCache();
return true;
} }
});
final Preference cleanPrivateStoragePreference = mSettingsFragment.findPreference("clean_private_storage"); final Preference cleanPrivateStoragePreference = mSettingsFragment.findPreference("clean_private_storage");
cleanPrivateStoragePreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { if (cleanPrivateStoragePreference != null) {
@Override cleanPrivateStoragePreference.setOnPreferenceClickListener(preference -> cleanPrivateStorage());
public boolean onPreferenceClick(Preference preference) {
cleanPrivateStorage();
return true;
} }
});
} }
final Preference deleteOmemoPreference = mSettingsFragment.findPreference("delete_omemo_identities"); final Preference deleteOmemoPreference = mSettingsFragment.findPreference("delete_omemo_identities");
deleteOmemoPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { if (deleteOmemoPreference != null) {
@Override deleteOmemoPreference.setOnPreferenceClickListener(preference -> deleteOmemoIdentities());
public boolean onPreferenceClick(Preference preference) {
deleteOmemoIdentities();
return true;
} }
});
} }
private boolean isCallable(final Intent i) { private boolean isCallable(final Intent i) {
@ -239,15 +219,17 @@ public class SettingsActivity extends XmppActivity implements
} }
private void cleanCache() { private boolean cleanCache() {
Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS); Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:" + getPackageName())); intent.setData(Uri.parse("package:" + getPackageName()));
startActivity(intent); startActivity(intent);
return true;
} }
private void cleanPrivateStorage() { private boolean cleanPrivateStorage() {
cleanPrivatePictures(); cleanPrivatePictures();
cleanPrivateFiles(); cleanPrivateFiles();
return true;
} }
private void cleanPrivatePictures() { private void cleanPrivatePictures() {
@ -290,7 +272,7 @@ public class SettingsActivity extends XmppActivity implements
} }
} }
private void deleteOmemoIdentities() { private boolean deleteOmemoIdentities() {
AlertDialog.Builder builder = new AlertDialog.Builder(this); AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.pref_delete_omemo_identities); builder.setTitle(R.string.pref_delete_omemo_identities);
final List<CharSequence> accounts = new ArrayList<>(); final List<CharSequence> accounts = new ArrayList<>();
@ -337,6 +319,7 @@ public class SettingsActivity extends XmppActivity implements
AlertDialog dialog = builder.create(); AlertDialog dialog = builder.create();
dialog.show(); dialog.show();
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false); dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
return true;
} }
@Override @Override

View file

@ -1,77 +1,60 @@
package eu.siacs.conversations.ui; package eu.siacs.conversations.ui;
import android.app.Dialog; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.preference.Preference; import android.preference.Preference;
import android.preference.PreferenceCategory; import android.preference.PreferenceCategory;
import android.preference.PreferenceFragment; import android.preference.PreferenceFragment;
import android.preference.PreferenceScreen; import android.preference.PreferenceScreen;
import android.view.View; import android.text.TextUtils;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import eu.siacs.conversations.Config; import eu.siacs.conversations.Config;
import eu.siacs.conversations.R; import eu.siacs.conversations.R;
public class SettingsFragment extends PreferenceFragment { public class SettingsFragment extends PreferenceFragment {
//http://stackoverflow.com/questions/16374820/action-bar-home-button-not-functional-with-nested-preferencescreen/16800527#16800527 private String page = null;
private void initializeActionBar(PreferenceScreen preferenceScreen) {
final Dialog dialog = preferenceScreen.getDialog();
if (dialog != null) {
View homeBtn = dialog.findViewById(android.R.id.home);
if (homeBtn != null) {
View.OnClickListener dismissDialogClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
};
ViewParent homeBtnContainer = homeBtn.getParent();
if (homeBtnContainer instanceof FrameLayout) {
ViewGroup containerParent = (ViewGroup) homeBtnContainer.getParent();
if (containerParent instanceof LinearLayout) {
((LinearLayout) containerParent).setOnClickListener(dismissDialogClickListener);
} else {
((FrameLayout) homeBtnContainer).setOnClickListener(dismissDialogClickListener);
}
} else {
homeBtn.setOnClickListener(dismissDialogClickListener);
}
}
}
}
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences); addPreferencesFromResource(R.xml.preferences);
// Remove from standard preferences if the flag ONLY_INTERNAL_STORAGE is not true // Remove from standard preferences if the flag ONLY_INTERNAL_STORAGE is not true
if (!Config.ONLY_INTERNAL_STORAGE) { if (!Config.ONLY_INTERNAL_STORAGE) {
PreferenceCategory mCategory = (PreferenceCategory) findPreference("security_options"); PreferenceCategory mCategory = (PreferenceCategory) findPreference("security_options");
Preference mPref1 = findPreference("clean_cache"); if (mCategory != null) {
Preference mPref2 = findPreference("clean_private_storage"); Preference cleanCache = findPreference("clean_cache");
mCategory.removePreference(mPref1); Preference cleanPrivateStorage = findPreference("clean_private_storage");
mCategory.removePreference(mPref2); mCategory.removePreference(cleanCache);
mCategory.removePreference(cleanPrivateStorage);
}
}
if (!TextUtils.isEmpty(page)) {
openPreferenceScreen(page);
} }
} }
@Override public void setActivityIntent(final Intent intent) {
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { if (intent != null) {
super.onPreferenceTreeClick(preferenceScreen, preference); if (Intent.ACTION_VIEW.equals(intent.getAction())) {
if (preference instanceof PreferenceScreen) { if (intent.getExtras() != null) {
initializeActionBar((PreferenceScreen) preference); this.page = intent.getExtras().getString("page");
} }
return false; }
}
}
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);
}
} }
} }

View file

@ -87,6 +87,12 @@
android:key="quiet_hours" android:key="quiet_hours"
android:summary="@string/pref_quiet_hours_summary" android:summary="@string/pref_quiet_hours_summary"
android:title="@string/title_pref_quiet_hours"> android:title="@string/title_pref_quiet_hours">
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="eu.siacs.conversations"
android:targetClass="eu.siacs.conversations.ui.SettingsActivity" >
<extra android:name="page" android:value="quiet_hours" />
</intent>
<CheckBoxPreference <CheckBoxPreference
android:defaultValue="@bool/enable_quiet_hours" android:defaultValue="@bool/enable_quiet_hours"
android:key="enable_quiet_hours" android:key="enable_quiet_hours"
@ -185,6 +191,12 @@
android:key="expert" android:key="expert"
android:summary="@string/pref_expert_options_summary" android:summary="@string/pref_expert_options_summary"
android:title="@string/pref_expert_options"> android:title="@string/pref_expert_options">
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="eu.siacs.conversations"
android:targetClass="eu.siacs.conversations.ui.SettingsActivity" >
<extra android:name="page" android:value="expert" />
</intent>
<PreferenceCategory android:title="@string/pref_security_settings" <PreferenceCategory android:title="@string/pref_security_settings"
android:key="security_options"> android:key="security_options">
<CheckBoxPreference <CheckBoxPreference