|
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.animation.PropertyAnimator; |
|
8 import org.mozilla.gecko.animation.PropertyAnimator.Property; |
|
9 import org.mozilla.gecko.animation.ViewHelper; |
|
10 import org.mozilla.gecko.R; |
|
11 |
|
12 import android.app.AlertDialog; |
|
13 import android.app.Dialog; |
|
14 import android.content.Context; |
|
15 import android.content.DialogInterface; |
|
16 import android.content.DialogInterface.OnClickListener; |
|
17 import android.content.DialogInterface.OnShowListener; |
|
18 import android.content.res.Resources; |
|
19 import android.util.Log; |
|
20 import android.view.View; |
|
21 import android.view.ViewGroup; |
|
22 import android.widget.TextView; |
|
23 |
|
24 public class PanelsPreference extends CustomListPreference { |
|
25 protected String LOGTAG = "PanelsPreference"; |
|
26 |
|
27 // Position state of this Preference in enclosing category. |
|
28 private static final int STATE_IS_FIRST = 0; |
|
29 private static final int STATE_IS_LAST = 1; |
|
30 |
|
31 /** |
|
32 * Index of the context menu button for controlling display options. |
|
33 * For (removable) Dynamic panels, this button removes the panel. |
|
34 * For built-in panels, this button toggles showing or hiding the panel. |
|
35 */ |
|
36 private static final int INDEX_DISPLAY_BUTTON = 1; |
|
37 private static final int INDEX_REORDER_BUTTON = 2; |
|
38 |
|
39 // Indices of buttons in context menu for reordering. |
|
40 private static final int INDEX_MOVE_UP_BUTTON = 0; |
|
41 private static final int INDEX_MOVE_DOWN_BUTTON = 1; |
|
42 |
|
43 private String LABEL_HIDE; |
|
44 private String LABEL_SHOW; |
|
45 |
|
46 private View preferenceView; |
|
47 protected boolean mIsHidden = false; |
|
48 private boolean mIsRemovable; |
|
49 |
|
50 private boolean mAnimate; |
|
51 private static final int ANIMATION_DURATION_MS = 400; |
|
52 |
|
53 // State for reordering. |
|
54 private int mPositionState = -1; |
|
55 private final int mIndex; |
|
56 |
|
57 public PanelsPreference(Context context, CustomListCategory parentCategory, boolean isRemovable, int index, boolean animate) { |
|
58 super(context, parentCategory); |
|
59 mIsRemovable = isRemovable; |
|
60 mIndex = index; |
|
61 mAnimate = animate; |
|
62 } |
|
63 |
|
64 @Override |
|
65 protected int getPreferenceLayoutResource() { |
|
66 return R.layout.preference_panels; |
|
67 } |
|
68 |
|
69 @Override |
|
70 protected void onBindView(View view) { |
|
71 super.onBindView(view); |
|
72 |
|
73 // Override view handling so we can grey out "hidden" PanelPreferences. |
|
74 view.setEnabled(!mIsHidden); |
|
75 |
|
76 if (view instanceof ViewGroup) { |
|
77 final ViewGroup group = (ViewGroup) view; |
|
78 for (int i = 0; i < group.getChildCount(); i++) { |
|
79 group.getChildAt(i).setEnabled(!mIsHidden); |
|
80 } |
|
81 preferenceView = group; |
|
82 } |
|
83 |
|
84 if (mAnimate) { |
|
85 ViewHelper.setAlpha(preferenceView, 0); |
|
86 |
|
87 final PropertyAnimator animator = new PropertyAnimator(ANIMATION_DURATION_MS); |
|
88 animator.attach(preferenceView, Property.ALPHA, 1); |
|
89 animator.start(); |
|
90 |
|
91 // Clear animate flag. |
|
92 mAnimate = false; |
|
93 } |
|
94 } |
|
95 |
|
96 @Override |
|
97 protected String[] createDialogItems() { |
|
98 final Resources res = getContext().getResources(); |
|
99 final String labelReorder = res.getString(R.string.pref_panels_reorder); |
|
100 |
|
101 if (mIsRemovable) { |
|
102 return new String[] { LABEL_SET_AS_DEFAULT, LABEL_REMOVE, labelReorder }; |
|
103 } |
|
104 |
|
105 // Built-in panels can't be removed, so use show/hide options. |
|
106 LABEL_HIDE = res.getString(R.string.pref_panels_hide); |
|
107 LABEL_SHOW = res.getString(R.string.pref_panels_show); |
|
108 |
|
109 return new String[] { LABEL_SET_AS_DEFAULT, LABEL_HIDE, labelReorder }; |
|
110 } |
|
111 |
|
112 @Override |
|
113 public void setIsDefault(boolean isDefault) { |
|
114 mIsDefault = isDefault; |
|
115 if (isDefault) { |
|
116 setSummary(LABEL_IS_DEFAULT); |
|
117 if (mIsHidden) { |
|
118 // Unhide the panel if it's being set as the default. |
|
119 setHidden(false); |
|
120 } |
|
121 } else { |
|
122 setSummary(""); |
|
123 } |
|
124 } |
|
125 |
|
126 @Override |
|
127 protected void onDialogIndexClicked(int index) { |
|
128 switch(index) { |
|
129 case INDEX_SET_DEFAULT_BUTTON: |
|
130 mParentCategory.setDefault(this); |
|
131 break; |
|
132 |
|
133 case INDEX_DISPLAY_BUTTON: |
|
134 // Handle display options for the panel. |
|
135 if (mIsRemovable) { |
|
136 // For removable panels, the button displays text for removing the panel. |
|
137 mParentCategory.uninstall(this); |
|
138 } else { |
|
139 // Otherwise, the button toggles between text for showing or hiding the panel. |
|
140 ((PanelsPreferenceCategory) mParentCategory).setHidden(this, !mIsHidden); |
|
141 } |
|
142 break; |
|
143 |
|
144 case INDEX_REORDER_BUTTON: |
|
145 // Display dialog for changing preference order. |
|
146 final Dialog orderDialog = makeReorderDialog(); |
|
147 orderDialog.show(); |
|
148 break; |
|
149 |
|
150 default: |
|
151 Log.w(LOGTAG, "Selected index out of range: " + index); |
|
152 } |
|
153 } |
|
154 |
|
155 @Override |
|
156 protected void configureShownDialog() { |
|
157 super.configureShownDialog(); |
|
158 |
|
159 // Handle Show/Hide buttons. |
|
160 if (!mIsRemovable) { |
|
161 final TextView hideButton = (TextView) mDialog.getListView().getChildAt(INDEX_DISPLAY_BUTTON); |
|
162 hideButton.setText(mIsHidden ? LABEL_SHOW : LABEL_HIDE); |
|
163 } |
|
164 } |
|
165 |
|
166 |
|
167 private Dialog makeReorderDialog() { |
|
168 final AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); |
|
169 |
|
170 final Resources res = getContext().getResources(); |
|
171 final String labelUp = res.getString(R.string.pref_panels_move_up); |
|
172 final String labelDown = res.getString(R.string.pref_panels_move_down); |
|
173 |
|
174 builder.setTitle(getTitle()); |
|
175 builder.setItems(new String[] { labelUp, labelDown }, new OnClickListener() { |
|
176 @Override |
|
177 public void onClick(DialogInterface dialog, int index) { |
|
178 dialog.dismiss(); |
|
179 switch (index) { |
|
180 case INDEX_MOVE_UP_BUTTON: |
|
181 ((PanelsPreferenceCategory) mParentCategory).moveUp(PanelsPreference.this); |
|
182 break; |
|
183 |
|
184 case INDEX_MOVE_DOWN_BUTTON: |
|
185 ((PanelsPreferenceCategory) mParentCategory).moveDown(PanelsPreference.this); |
|
186 break; |
|
187 } |
|
188 } |
|
189 }); |
|
190 |
|
191 final Dialog dialog = builder.create(); |
|
192 dialog.setOnShowListener(new OnShowListener() { |
|
193 @Override |
|
194 public void onShow(DialogInterface dialog) { |
|
195 setReorderItemsEnabled(dialog); |
|
196 } |
|
197 }); |
|
198 |
|
199 return dialog; |
|
200 } |
|
201 |
|
202 public void setIsFirst() { |
|
203 mPositionState = STATE_IS_FIRST; |
|
204 } |
|
205 |
|
206 public void setIsLast() { |
|
207 mPositionState = STATE_IS_LAST; |
|
208 } |
|
209 |
|
210 /** |
|
211 * Configure enabled state of the reorder dialog, which must be done after the dialog is shown. |
|
212 * @param dialog Dialog to configure |
|
213 */ |
|
214 private void setReorderItemsEnabled(DialogInterface dialog) { |
|
215 // Update button enabled-ness for reordering. |
|
216 switch (mPositionState) { |
|
217 case STATE_IS_FIRST: |
|
218 final TextView itemUp = (TextView) ((AlertDialog) dialog).getListView().getChildAt(INDEX_MOVE_UP_BUTTON); |
|
219 itemUp.setEnabled(false); |
|
220 // Disable clicks to this view. |
|
221 itemUp.setOnClickListener(null); |
|
222 break; |
|
223 |
|
224 case STATE_IS_LAST: |
|
225 final TextView itemDown = (TextView) ((AlertDialog) dialog).getListView().getChildAt(INDEX_MOVE_DOWN_BUTTON); |
|
226 itemDown.setEnabled(false); |
|
227 // Disable clicks to this view. |
|
228 itemDown.setOnClickListener(null); |
|
229 break; |
|
230 |
|
231 default: |
|
232 // Do nothing. |
|
233 break; |
|
234 } |
|
235 } |
|
236 |
|
237 public void setHidden(boolean toHide) { |
|
238 if (toHide) { |
|
239 setIsDefault(false); |
|
240 } |
|
241 |
|
242 if (mIsHidden != toHide) { |
|
243 mIsHidden = toHide; |
|
244 notifyChanged(); |
|
245 } |
|
246 } |
|
247 |
|
248 public boolean isHidden() { |
|
249 return mIsHidden; |
|
250 } |
|
251 |
|
252 public int getIndex() { |
|
253 return mIndex; |
|
254 } |
|
255 } |