mobile/android/base/home/HomePanelPicker.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial