mobile/android/base/preferences/PanelsPreferenceCategory.java

branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
equal deleted inserted replaced
-1:000000000000 0:4ea3c8dfc50f
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 package org.mozilla.gecko.preferences;
6
7 import org.mozilla.gecko.Telemetry;
8 import org.mozilla.gecko.TelemetryContract;
9 import org.mozilla.gecko.home.HomeConfig;
10 import org.mozilla.gecko.home.HomeConfig.PanelConfig;
11 import org.mozilla.gecko.home.HomeConfig.State;
12 import org.mozilla.gecko.util.ThreadUtils;
13 import org.mozilla.gecko.util.UiAsyncTask;
14
15 import android.content.Context;
16 import android.text.TextUtils;
17 import android.util.AttributeSet;
18
19 public class PanelsPreferenceCategory extends CustomListCategory {
20 public static final String LOGTAG = "PanelsPrefCategory";
21
22 protected HomeConfig mHomeConfig;
23 protected HomeConfig.Editor mConfigEditor;
24
25 protected UiAsyncTask<Void, Void, HomeConfig.State> mLoadTask;
26
27 public PanelsPreferenceCategory(Context context) {
28 super(context);
29 initConfig(context);
30 }
31
32 public PanelsPreferenceCategory(Context context, AttributeSet attrs) {
33 super(context, attrs);
34 initConfig(context);
35 }
36
37 public PanelsPreferenceCategory(Context context, AttributeSet attrs, int defStyle) {
38 super(context, attrs, defStyle);
39 initConfig(context);
40 }
41
42 protected void initConfig(Context context) {
43 mHomeConfig = HomeConfig.getDefault(context);
44 }
45
46 @Override
47 public void onAttachedToActivity() {
48 super.onAttachedToActivity();
49
50 loadHomeConfig(null);
51 }
52
53 /**
54 * Load the Home Panels config and populate the preferences screen and maintain local state.
55 */
56 private void loadHomeConfig(final String animatePanelId) {
57 mLoadTask = new UiAsyncTask<Void, Void, HomeConfig.State>(ThreadUtils.getBackgroundHandler()) {
58 @Override
59 public HomeConfig.State doInBackground(Void... params) {
60 return mHomeConfig.load();
61 }
62
63 @Override
64 public void onPostExecute(HomeConfig.State configState) {
65 mConfigEditor = configState.edit();
66 displayHomeConfig(configState, animatePanelId);
67 }
68 };
69 mLoadTask.execute();
70 }
71
72 /**
73 * Simplified refresh of Home Panels when there is no state to be persisted.
74 */
75 public void refresh() {
76 refresh(null, null);
77 }
78
79 /**
80 * Refresh the Home Panels list and animate a panel, if specified.
81 * If null, load from HomeConfig.
82 *
83 * @param State HomeConfig.State to rebuild Home Panels list from.
84 * @param String panelId of panel to be animated.
85 */
86 public void refresh(State state, String animatePanelId) {
87 // Clear all the existing home panels.
88 removeAll();
89
90 if (state == null) {
91 loadHomeConfig(animatePanelId);
92 } else {
93 displayHomeConfig(state, animatePanelId);
94 }
95 }
96
97 private void displayHomeConfig(HomeConfig.State configState, String animatePanelId) {
98 int index = 0;
99 for (PanelConfig panelConfig : configState) {
100 final boolean isRemovable = panelConfig.isDynamic();
101
102 // Create and add the pref.
103 final String panelId = panelConfig.getId();
104 final boolean animate = TextUtils.equals(animatePanelId, panelId);
105
106 final PanelsPreference pref = new PanelsPreference(getContext(), PanelsPreferenceCategory.this, isRemovable, index, animate);
107 pref.setTitle(panelConfig.getTitle());
108 pref.setKey(panelConfig.getId());
109 // XXX: Pull icon from PanelInfo.
110 addPreference(pref);
111
112 if (panelConfig.isDisabled()) {
113 pref.setHidden(true);
114 }
115
116 index++;
117 }
118
119 setPositionState();
120 setDefaultFromConfig();
121 }
122
123 private void setPositionState() {
124 final int prefCount = getPreferenceCount();
125
126 // Pass in position state to first and last preference.
127 final PanelsPreference firstPref = (PanelsPreference) getPreference(0);
128 firstPref.setIsFirst();
129
130 final PanelsPreference lastPref = (PanelsPreference) getPreference(prefCount - 1);
131 lastPref.setIsLast();
132 }
133
134 private void setDefaultFromConfig() {
135 final String defaultPanelId = mConfigEditor.getDefaultPanelId();
136 if (defaultPanelId == null) {
137 mDefaultReference = null;
138 return;
139 }
140
141 final int prefCount = getPreferenceCount();
142
143 for (int i = 0; i < prefCount; i++) {
144 final PanelsPreference pref = (PanelsPreference) getPreference(i);
145
146 if (defaultPanelId.equals(pref.getKey())) {
147 super.setDefault(pref);
148 break;
149 }
150 }
151 }
152
153 @Override
154 public void setDefault(CustomListPreference pref) {
155 super.setDefault(pref);
156
157 final String id = pref.getKey();
158
159 final String defaultPanelId = mConfigEditor.getDefaultPanelId();
160 if (defaultPanelId != null && defaultPanelId.equals(id)) {
161 return;
162 }
163
164 mConfigEditor.setDefault(id);
165 mConfigEditor.apply();
166
167 Telemetry.sendUIEvent(TelemetryContract.Event.PANEL_SET_DEFAULT, null, id);
168 }
169
170 @Override
171 protected void onPrepareForRemoval() {
172 if (mLoadTask != null) {
173 mLoadTask.cancel(true);
174 }
175 }
176
177 @Override
178 public void uninstall(CustomListPreference pref) {
179 mConfigEditor.uninstall(pref.getKey());
180 mConfigEditor.apply();
181
182 super.uninstall(pref);
183 }
184
185 public void moveUp(PanelsPreference pref) {
186 final int panelIndex = pref.getIndex();
187 if (panelIndex > 0) {
188 final String panelKey = pref.getKey();
189 mConfigEditor.moveTo(panelKey, panelIndex - 1);
190 final State state = mConfigEditor.apply();
191 refresh(state, panelKey);
192 }
193 }
194
195 public void moveDown(PanelsPreference pref) {
196 final int panelIndex = pref.getIndex();
197 if (panelIndex < getPreferenceCount() - 1) {
198 final String panelKey = pref.getKey();
199 mConfigEditor.moveTo(panelKey, panelIndex + 1);
200 final State state = mConfigEditor.apply();
201 refresh(state, panelKey);
202 }
203 }
204
205 /**
206 * Update the hide/show state of the preference and save the HomeConfig
207 * changes.
208 *
209 * @param pref Preference to update
210 * @param toHide New hidden state of the preference
211 */
212 protected void setHidden(PanelsPreference pref, boolean toHide) {
213 mConfigEditor.setDisabled(pref.getKey(), toHide);
214 mConfigEditor.apply();
215
216 pref.setHidden(toHide);
217 setDefaultFromConfig();
218 }
219
220 /**
221 * When the default panel is removed or disabled, find an enabled panel
222 * if possible and set it as mDefaultReference.
223 */
224 @Override
225 protected void setFallbackDefault() {
226 setDefaultFromConfig();
227 }
228 }

mercurial