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