michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.home; michael@0: michael@0: import android.app.Activity; michael@0: import android.content.Context; michael@0: import android.os.Bundle; michael@0: import android.support.v4.app.FragmentActivity; michael@0: import android.support.v4.app.LoaderManager; michael@0: import android.support.v4.app.LoaderManager.LoaderCallbacks; michael@0: import android.support.v4.content.Loader; michael@0: import android.view.LayoutInflater; michael@0: import android.view.View; michael@0: import android.view.View.OnClickListener; michael@0: import android.view.ViewGroup; michael@0: import android.widget.BaseAdapter; michael@0: import android.widget.ListView; michael@0: import android.widget.TextView; michael@0: import android.widget.Toast; michael@0: michael@0: import java.util.ArrayList; michael@0: import java.util.Arrays; michael@0: import java.util.List; michael@0: michael@0: import org.mozilla.gecko.R; michael@0: import org.mozilla.gecko.home.HomeConfig; michael@0: import org.mozilla.gecko.home.HomeConfig.PanelConfig; michael@0: import org.mozilla.gecko.home.PanelInfoManager.RequestCallback; michael@0: import org.mozilla.gecko.home.PanelInfoManager.PanelInfo; michael@0: michael@0: /** michael@0: * Dialog for selecting new home panels to add. michael@0: */ michael@0: public class HomePanelPicker extends FragmentActivity { michael@0: private static final String LOGTAG = "HomePanelPicker"; michael@0: michael@0: /** michael@0: * Intent extra key for caller to pass in a list of existing panel ids. michael@0: */ michael@0: public static final String CURRENT_PANELS_IDS = "currentPanelsIds"; michael@0: michael@0: /** michael@0: * Request code when starting the activity with startActivityForResult. michael@0: */ michael@0: public static final int REQUEST_CODE_ADD_PANEL = 1; michael@0: michael@0: private static final int LOADER_ID_CONFIG = 0; michael@0: michael@0: private ListView mListView; michael@0: private List mCurrentPanelsIds; michael@0: private List mPanelInfos; michael@0: michael@0: @Override michael@0: public void onCreate(Bundle savedInstance) { michael@0: super.onCreate(savedInstance); michael@0: setContentView(R.layout.home_panel_picker); michael@0: michael@0: // Fetch current panel ids from extras bundle, if provided by caller. michael@0: Bundle intentExtras = getIntent().getExtras(); michael@0: if (intentExtras != null) { michael@0: String[] panelIdsArray = intentExtras.getStringArray(CURRENT_PANELS_IDS); michael@0: if (panelIdsArray != null) { michael@0: mCurrentPanelsIds = Arrays.asList(panelIdsArray); michael@0: } michael@0: } michael@0: michael@0: mListView = (ListView) findViewById(R.id.list); michael@0: michael@0: // Set an empty adapter. michael@0: final PickerAdapter adapter = new PickerAdapter(this); michael@0: mListView.setAdapter(adapter); michael@0: michael@0: requestAvailablePanels(); michael@0: } michael@0: michael@0: /** michael@0: * Get panels and update the adapter to display the ones michael@0: * available for install. michael@0: */ michael@0: private void requestAvailablePanels() { michael@0: final PanelInfoManager pm = new PanelInfoManager(); michael@0: pm.requestAvailablePanels(new RequestCallback() { michael@0: @Override michael@0: public void onComplete(final List panelInfos) { michael@0: mPanelInfos = panelInfos; michael@0: michael@0: // Fetch current home panels if necessary. michael@0: if (mCurrentPanelsIds == null) { michael@0: loadConfig(); michael@0: } else { michael@0: updatePanelsAdapter(mPanelInfos); michael@0: } michael@0: } michael@0: }); michael@0: } michael@0: michael@0: /** michael@0: * Fetch panels from HomeConfig. michael@0: * michael@0: * This is an async call using Loaders. michael@0: */ michael@0: private void loadConfig() { michael@0: final ConfigLoaderCallbacks loaderCallbacks = new ConfigLoaderCallbacks(); michael@0: final LoaderManager lm = HomePanelPicker.this.getSupportLoaderManager(); michael@0: lm.initLoader(LOADER_ID_CONFIG, null, loaderCallbacks); michael@0: } michael@0: michael@0: /** michael@0: * Update the list adapter to contain only available panels (with the michael@0: * installed panels filtered out). michael@0: * michael@0: * @param panelInfos List of all available panels. michael@0: */ michael@0: private void updatePanelsAdapter(List panelInfos) { michael@0: final List availablePanels = new ArrayList(); michael@0: michael@0: // Filter out panels that are already displayed. michael@0: for (PanelInfo panelInfo : panelInfos) { michael@0: if (!mCurrentPanelsIds.contains(panelInfo.getId())) { michael@0: availablePanels.add(panelInfo); michael@0: } michael@0: } michael@0: michael@0: if (availablePanels.isEmpty()) { michael@0: setContentView(R.layout.home_panel_picker_empty); michael@0: return; michael@0: } michael@0: michael@0: final PickerAdapter adapter = (PickerAdapter) mListView.getAdapter(); michael@0: adapter.updateFromPanelInfos(availablePanels); michael@0: } michael@0: michael@0: private void installNewPanelAndQuit(PanelInfo panelInfo) { michael@0: final PanelConfig newPanelConfig = panelInfo.toPanelConfig(); michael@0: HomePanelsManager.getInstance().installPanel(newPanelConfig); michael@0: showToastForNewPanel(newPanelConfig); michael@0: michael@0: setResult(Activity.RESULT_OK); michael@0: finish(); michael@0: } michael@0: michael@0: private void showToastForNewPanel(PanelConfig panelConfig) { michael@0: String panelName = panelConfig.getTitle(); michael@0: michael@0: // Display toast. michael@0: final String successMsg = getResources().getString(R.string.home_add_panel_installed, panelName); michael@0: Toast.makeText(this, successMsg, Toast.LENGTH_SHORT).show(); michael@0: } michael@0: michael@0: // ViewHolder for rows of the panel picker. michael@0: private static class PanelRow { michael@0: final TextView title; michael@0: int position; michael@0: michael@0: public PanelRow(View view) { michael@0: title = (TextView) view.findViewById(R.id.title); michael@0: } michael@0: } michael@0: michael@0: private class PickerAdapter extends BaseAdapter { michael@0: private final LayoutInflater mInflater; michael@0: private List mPanelInfos; michael@0: private final OnClickListener mOnClickListener; michael@0: michael@0: public PickerAdapter(Context context) { michael@0: this(context, null); michael@0: } michael@0: michael@0: public PickerAdapter(Context context, List panelInfos) { michael@0: mInflater = LayoutInflater.from(context); michael@0: mPanelInfos = panelInfos; michael@0: michael@0: mOnClickListener = new OnClickListener() { michael@0: @Override michael@0: public void onClick(View view) { michael@0: final PanelRow panelsRow = (PanelRow) view.getTag(); michael@0: installNewPanelAndQuit(mPanelInfos.get(panelsRow.position)); michael@0: } michael@0: }; michael@0: } michael@0: michael@0: @Override michael@0: public int getCount() { michael@0: if (mPanelInfos == null) { michael@0: return 0; michael@0: } michael@0: michael@0: return mPanelInfos.size(); michael@0: } michael@0: michael@0: @Override michael@0: public PanelInfo getItem(int position) { michael@0: if (mPanelInfos == null) { michael@0: return null; michael@0: } michael@0: michael@0: return mPanelInfos.get(position); michael@0: } michael@0: michael@0: @Override michael@0: public long getItemId(int position) { michael@0: return position; michael@0: } michael@0: michael@0: @Override michael@0: public View getView(int position, View convertView, ViewGroup parent) { michael@0: PanelRow row; michael@0: michael@0: if (convertView == null) { michael@0: convertView = mInflater.inflate(R.layout.home_panel_picker_row, null); michael@0: convertView.setOnClickListener(mOnClickListener); michael@0: michael@0: row = new PanelRow(convertView); michael@0: convertView.setTag(row); michael@0: } else { michael@0: row = (PanelRow) convertView.getTag(); michael@0: } michael@0: michael@0: row.title.setText(mPanelInfos.get(position).getTitle()); michael@0: row.position = position; michael@0: michael@0: return convertView; michael@0: } michael@0: michael@0: public void updateFromPanelInfos(List panelInfos) { michael@0: mPanelInfos = panelInfos; michael@0: notifyDataSetChanged(); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Fetch installed Home panels and update the adapter for this activity. michael@0: */ michael@0: private class ConfigLoaderCallbacks implements LoaderCallbacks { michael@0: @Override michael@0: public Loader onCreateLoader(int id, Bundle args) { michael@0: final HomeConfig homeConfig = HomeConfig.getDefault(HomePanelPicker.this); michael@0: return new HomeConfigLoader(HomePanelPicker.this, homeConfig); michael@0: } michael@0: michael@0: @Override michael@0: public void onLoadFinished(Loader loader, HomeConfig.State configState) { michael@0: mCurrentPanelsIds = new ArrayList(); michael@0: for (PanelConfig panelConfig : configState) { michael@0: mCurrentPanelsIds.add(panelConfig.getId()); michael@0: } michael@0: michael@0: updatePanelsAdapter(mPanelInfos); michael@0: } michael@0: michael@0: @Override michael@0: public void onLoaderReset(Loader loader) {} michael@0: } michael@0: }