|
1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
|
2 * This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 package org.mozilla.gecko.home; |
|
7 |
|
8 import android.app.Activity; |
|
9 import android.content.Context; |
|
10 import android.os.Bundle; |
|
11 import android.support.v4.app.FragmentActivity; |
|
12 import android.support.v4.app.LoaderManager; |
|
13 import android.support.v4.app.LoaderManager.LoaderCallbacks; |
|
14 import android.support.v4.content.Loader; |
|
15 import android.view.LayoutInflater; |
|
16 import android.view.View; |
|
17 import android.view.View.OnClickListener; |
|
18 import android.view.ViewGroup; |
|
19 import android.widget.BaseAdapter; |
|
20 import android.widget.ListView; |
|
21 import android.widget.TextView; |
|
22 import android.widget.Toast; |
|
23 |
|
24 import java.util.ArrayList; |
|
25 import java.util.Arrays; |
|
26 import java.util.List; |
|
27 |
|
28 import org.mozilla.gecko.R; |
|
29 import org.mozilla.gecko.home.HomeConfig; |
|
30 import org.mozilla.gecko.home.HomeConfig.PanelConfig; |
|
31 import org.mozilla.gecko.home.PanelInfoManager.RequestCallback; |
|
32 import org.mozilla.gecko.home.PanelInfoManager.PanelInfo; |
|
33 |
|
34 /** |
|
35 * Dialog for selecting new home panels to add. |
|
36 */ |
|
37 public class HomePanelPicker extends FragmentActivity { |
|
38 private static final String LOGTAG = "HomePanelPicker"; |
|
39 |
|
40 /** |
|
41 * Intent extra key for caller to pass in a list of existing panel ids. |
|
42 */ |
|
43 public static final String CURRENT_PANELS_IDS = "currentPanelsIds"; |
|
44 |
|
45 /** |
|
46 * Request code when starting the activity with startActivityForResult. |
|
47 */ |
|
48 public static final int REQUEST_CODE_ADD_PANEL = 1; |
|
49 |
|
50 private static final int LOADER_ID_CONFIG = 0; |
|
51 |
|
52 private ListView mListView; |
|
53 private List<String> mCurrentPanelsIds; |
|
54 private List<PanelInfo> mPanelInfos; |
|
55 |
|
56 @Override |
|
57 public void onCreate(Bundle savedInstance) { |
|
58 super.onCreate(savedInstance); |
|
59 setContentView(R.layout.home_panel_picker); |
|
60 |
|
61 // Fetch current panel ids from extras bundle, if provided by caller. |
|
62 Bundle intentExtras = getIntent().getExtras(); |
|
63 if (intentExtras != null) { |
|
64 String[] panelIdsArray = intentExtras.getStringArray(CURRENT_PANELS_IDS); |
|
65 if (panelIdsArray != null) { |
|
66 mCurrentPanelsIds = Arrays.asList(panelIdsArray); |
|
67 } |
|
68 } |
|
69 |
|
70 mListView = (ListView) findViewById(R.id.list); |
|
71 |
|
72 // Set an empty adapter. |
|
73 final PickerAdapter adapter = new PickerAdapter(this); |
|
74 mListView.setAdapter(adapter); |
|
75 |
|
76 requestAvailablePanels(); |
|
77 } |
|
78 |
|
79 /** |
|
80 * Get panels and update the adapter to display the ones |
|
81 * available for install. |
|
82 */ |
|
83 private void requestAvailablePanels() { |
|
84 final PanelInfoManager pm = new PanelInfoManager(); |
|
85 pm.requestAvailablePanels(new RequestCallback() { |
|
86 @Override |
|
87 public void onComplete(final List<PanelInfo> panelInfos) { |
|
88 mPanelInfos = panelInfos; |
|
89 |
|
90 // Fetch current home panels if necessary. |
|
91 if (mCurrentPanelsIds == null) { |
|
92 loadConfig(); |
|
93 } else { |
|
94 updatePanelsAdapter(mPanelInfos); |
|
95 } |
|
96 } |
|
97 }); |
|
98 } |
|
99 |
|
100 /** |
|
101 * Fetch panels from HomeConfig. |
|
102 * |
|
103 * This is an async call using Loaders. |
|
104 */ |
|
105 private void loadConfig() { |
|
106 final ConfigLoaderCallbacks loaderCallbacks = new ConfigLoaderCallbacks(); |
|
107 final LoaderManager lm = HomePanelPicker.this.getSupportLoaderManager(); |
|
108 lm.initLoader(LOADER_ID_CONFIG, null, loaderCallbacks); |
|
109 } |
|
110 |
|
111 /** |
|
112 * Update the list adapter to contain only available panels (with the |
|
113 * installed panels filtered out). |
|
114 * |
|
115 * @param panelInfos List of all available panels. |
|
116 */ |
|
117 private void updatePanelsAdapter(List<PanelInfo> panelInfos) { |
|
118 final List<PanelInfo> availablePanels = new ArrayList<PanelInfo>(); |
|
119 |
|
120 // Filter out panels that are already displayed. |
|
121 for (PanelInfo panelInfo : panelInfos) { |
|
122 if (!mCurrentPanelsIds.contains(panelInfo.getId())) { |
|
123 availablePanels.add(panelInfo); |
|
124 } |
|
125 } |
|
126 |
|
127 if (availablePanels.isEmpty()) { |
|
128 setContentView(R.layout.home_panel_picker_empty); |
|
129 return; |
|
130 } |
|
131 |
|
132 final PickerAdapter adapter = (PickerAdapter) mListView.getAdapter(); |
|
133 adapter.updateFromPanelInfos(availablePanels); |
|
134 } |
|
135 |
|
136 private void installNewPanelAndQuit(PanelInfo panelInfo) { |
|
137 final PanelConfig newPanelConfig = panelInfo.toPanelConfig(); |
|
138 HomePanelsManager.getInstance().installPanel(newPanelConfig); |
|
139 showToastForNewPanel(newPanelConfig); |
|
140 |
|
141 setResult(Activity.RESULT_OK); |
|
142 finish(); |
|
143 } |
|
144 |
|
145 private void showToastForNewPanel(PanelConfig panelConfig) { |
|
146 String panelName = panelConfig.getTitle(); |
|
147 |
|
148 // Display toast. |
|
149 final String successMsg = getResources().getString(R.string.home_add_panel_installed, panelName); |
|
150 Toast.makeText(this, successMsg, Toast.LENGTH_SHORT).show(); |
|
151 } |
|
152 |
|
153 // ViewHolder for rows of the panel picker. |
|
154 private static class PanelRow { |
|
155 final TextView title; |
|
156 int position; |
|
157 |
|
158 public PanelRow(View view) { |
|
159 title = (TextView) view.findViewById(R.id.title); |
|
160 } |
|
161 } |
|
162 |
|
163 private class PickerAdapter extends BaseAdapter { |
|
164 private final LayoutInflater mInflater; |
|
165 private List<PanelInfo> mPanelInfos; |
|
166 private final OnClickListener mOnClickListener; |
|
167 |
|
168 public PickerAdapter(Context context) { |
|
169 this(context, null); |
|
170 } |
|
171 |
|
172 public PickerAdapter(Context context, List<PanelInfo> panelInfos) { |
|
173 mInflater = LayoutInflater.from(context); |
|
174 mPanelInfos = panelInfos; |
|
175 |
|
176 mOnClickListener = new OnClickListener() { |
|
177 @Override |
|
178 public void onClick(View view) { |
|
179 final PanelRow panelsRow = (PanelRow) view.getTag(); |
|
180 installNewPanelAndQuit(mPanelInfos.get(panelsRow.position)); |
|
181 } |
|
182 }; |
|
183 } |
|
184 |
|
185 @Override |
|
186 public int getCount() { |
|
187 if (mPanelInfos == null) { |
|
188 return 0; |
|
189 } |
|
190 |
|
191 return mPanelInfos.size(); |
|
192 } |
|
193 |
|
194 @Override |
|
195 public PanelInfo getItem(int position) { |
|
196 if (mPanelInfos == null) { |
|
197 return null; |
|
198 } |
|
199 |
|
200 return mPanelInfos.get(position); |
|
201 } |
|
202 |
|
203 @Override |
|
204 public long getItemId(int position) { |
|
205 return position; |
|
206 } |
|
207 |
|
208 @Override |
|
209 public View getView(int position, View convertView, ViewGroup parent) { |
|
210 PanelRow row; |
|
211 |
|
212 if (convertView == null) { |
|
213 convertView = mInflater.inflate(R.layout.home_panel_picker_row, null); |
|
214 convertView.setOnClickListener(mOnClickListener); |
|
215 |
|
216 row = new PanelRow(convertView); |
|
217 convertView.setTag(row); |
|
218 } else { |
|
219 row = (PanelRow) convertView.getTag(); |
|
220 } |
|
221 |
|
222 row.title.setText(mPanelInfos.get(position).getTitle()); |
|
223 row.position = position; |
|
224 |
|
225 return convertView; |
|
226 } |
|
227 |
|
228 public void updateFromPanelInfos(List<PanelInfo> panelInfos) { |
|
229 mPanelInfos = panelInfos; |
|
230 notifyDataSetChanged(); |
|
231 } |
|
232 } |
|
233 |
|
234 /** |
|
235 * Fetch installed Home panels and update the adapter for this activity. |
|
236 */ |
|
237 private class ConfigLoaderCallbacks implements LoaderCallbacks<HomeConfig.State> { |
|
238 @Override |
|
239 public Loader<HomeConfig.State> onCreateLoader(int id, Bundle args) { |
|
240 final HomeConfig homeConfig = HomeConfig.getDefault(HomePanelPicker.this); |
|
241 return new HomeConfigLoader(HomePanelPicker.this, homeConfig); |
|
242 } |
|
243 |
|
244 @Override |
|
245 public void onLoadFinished(Loader<HomeConfig.State> loader, HomeConfig.State configState) { |
|
246 mCurrentPanelsIds = new ArrayList<String>(); |
|
247 for (PanelConfig panelConfig : configState) { |
|
248 mCurrentPanelsIds.add(panelConfig.getId()); |
|
249 } |
|
250 |
|
251 updatePanelsAdapter(mPanelInfos); |
|
252 } |
|
253 |
|
254 @Override |
|
255 public void onLoaderReset(Loader<HomeConfig.State> loader) {} |
|
256 } |
|
257 } |