1package eu.siacs.conversations.ui;
2
3import android.content.Intent;
4import android.os.Bundle;
5import android.preference.Preference;
6import android.preference.PreferenceCategory;
7import android.preference.PreferenceFragment;
8import android.preference.PreferenceScreen;
9import android.text.TextUtils;
10import android.widget.ListView;
11
12import java.lang.reflect.Method;
13
14import eu.siacs.conversations.Config;
15import eu.siacs.conversations.R;
16import eu.siacs.conversations.utils.Compatibility;
17
18public class SettingsFragment extends PreferenceFragment {
19
20 private String page = null;
21 private String suffix = null;
22
23 @Override
24 public void onCreate(Bundle savedInstanceState) {
25 super.onCreate(savedInstanceState);
26
27 addPreferencesFromResource(R.xml.preferences);
28
29 // Remove from standard preferences if the flag ONLY_INTERNAL_STORAGE is false
30 if (!Config.ONLY_INTERNAL_STORAGE) {
31 PreferenceCategory mCategory = (PreferenceCategory) findPreference("security_options");
32 if (mCategory != null) {
33 Preference cleanCache = findPreference("clean_cache");
34 Preference cleanPrivateStorage = findPreference("clean_private_storage");
35 mCategory.removePreference(cleanCache);
36 mCategory.removePreference(cleanPrivateStorage);
37 }
38 }
39 Compatibility.removeUnusedPreferences(this);
40
41 if (!TextUtils.isEmpty(page)) {
42 openPreferenceScreen(page);
43 }
44
45 }
46
47 @Override
48 public void onActivityCreated(Bundle bundle) {
49 super.onActivityCreated(bundle);
50
51 final ListView listView = getActivity().findViewById(android.R.id.list);
52 if (listView != null) {
53 listView.setDivider(null);
54 }
55 }
56
57 public void setActivityIntent(final Intent intent) {
58 boolean wasEmpty = TextUtils.isEmpty(page);
59 if (intent != null) {
60 if (Intent.ACTION_VIEW.equals(intent.getAction())) {
61 if (intent.getExtras() != null) {
62 this.page = intent.getExtras().getString("page");
63 this.suffix = intent.getExtras().getString("suffix");
64 if (wasEmpty) {
65 openPreferenceScreen(page);
66 }
67 }
68 }
69 }
70 }
71
72 private void openPreferenceScreen(final String screenName) {
73 final Preference pref = findPreference(screenName);
74 if (pref instanceof PreferenceScreen) {
75 final PreferenceScreen preferenceScreen = (PreferenceScreen) pref;
76 getActivity().setTitle(preferenceScreen.getTitle());
77 preferenceScreen.setDependency("");
78 if (this.suffix != null) {
79 for (int i = 0; i < preferenceScreen.getPreferenceCount(); i++) {
80 final Preference p = preferenceScreen.getPreference(i);
81 if (!p.hasKey()) continue;
82 p.setKey(p.getKey() + this.suffix);
83 if (p.getDependency() != null && !"".equals(p.getDependency())) {
84 p.setDependency(p.getDependency() + this.suffix);
85 }
86 reloadPref(p);
87 }
88 }
89 setPreferenceScreen((PreferenceScreen) pref);
90 }
91 }
92
93 static void reloadPref(final Preference pref) {
94 Class iterClass = pref.getClass();
95 while(iterClass != Object.class) {
96 try {
97 Method m = iterClass.getDeclaredMethod("onSetInitialValue", boolean.class, Object.class);
98 m.setAccessible(true);
99 m.invoke(pref, true, null);
100 } catch (Exception e) { }
101 iterClass = iterClass.getSuperclass();
102 }
103 }
104}