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