mobile/android/base/home/LastTabsPanel.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

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 org.mozilla.gecko.AboutPages;
michael@0 9 import org.mozilla.gecko.GeckoProfile;
michael@0 10 import org.mozilla.gecko.R;
michael@0 11 import org.mozilla.gecko.SessionParser;
michael@0 12 import org.mozilla.gecko.Telemetry;
michael@0 13 import org.mozilla.gecko.TelemetryContract;
michael@0 14 import org.mozilla.gecko.db.BrowserContract.Combined;
michael@0 15 import org.mozilla.gecko.home.HomePager.OnNewTabsListener;
michael@0 16
michael@0 17 import android.app.Activity;
michael@0 18 import android.content.Context;
michael@0 19 import android.database.Cursor;
michael@0 20 import android.database.MatrixCursor;
michael@0 21 import android.database.MatrixCursor.RowBuilder;
michael@0 22 import android.os.Bundle;
michael@0 23 import android.support.v4.app.LoaderManager.LoaderCallbacks;
michael@0 24 import android.support.v4.content.Loader;
michael@0 25 import android.support.v4.widget.CursorAdapter;
michael@0 26 import android.view.LayoutInflater;
michael@0 27 import android.view.View;
michael@0 28 import android.view.ViewGroup;
michael@0 29 import android.view.ViewStub;
michael@0 30 import android.widget.AdapterView;
michael@0 31 import android.widget.ImageView;
michael@0 32 import android.widget.TextView;
michael@0 33
michael@0 34 /**
michael@0 35 * Fragment that displays tabs from last session in a ListView.
michael@0 36 */
michael@0 37 public class LastTabsPanel extends HomeFragment {
michael@0 38 // Logging tag name
michael@0 39 private static final String LOGTAG = "GeckoLastTabsPanel";
michael@0 40
michael@0 41 // Cursor loader ID for the session parser
michael@0 42 private static final int LOADER_ID_LAST_TABS = 0;
michael@0 43
michael@0 44 // Adapter for the list of search results
michael@0 45 private LastTabsAdapter mAdapter;
michael@0 46
michael@0 47 // The view shown by the fragment.
michael@0 48 private HomeListView mList;
michael@0 49
michael@0 50 // The title for this HomeFragment panel.
michael@0 51 private TextView mTitle;
michael@0 52
michael@0 53 // The button view for restoring tabs from last session.
michael@0 54 private View mRestoreButton;
michael@0 55
michael@0 56 // Reference to the View to display when there are no results.
michael@0 57 private View mEmptyView;
michael@0 58
michael@0 59 // Callbacks used for the search and favicon cursor loaders
michael@0 60 private CursorLoaderCallbacks mCursorLoaderCallbacks;
michael@0 61
michael@0 62 // On new tabs listener
michael@0 63 private OnNewTabsListener mNewTabsListener;
michael@0 64
michael@0 65 public static LastTabsPanel newInstance() {
michael@0 66 return new LastTabsPanel();
michael@0 67 }
michael@0 68
michael@0 69 public LastTabsPanel() {
michael@0 70 mNewTabsListener = null;
michael@0 71 }
michael@0 72
michael@0 73 @Override
michael@0 74 public void onAttach(Activity activity) {
michael@0 75 super.onAttach(activity);
michael@0 76
michael@0 77 try {
michael@0 78 mNewTabsListener = (OnNewTabsListener) activity;
michael@0 79 } catch (ClassCastException e) {
michael@0 80 throw new ClassCastException(activity.toString()
michael@0 81 + " must implement HomePager.OnNewTabsListener");
michael@0 82 }
michael@0 83 }
michael@0 84
michael@0 85 @Override
michael@0 86 public void onDetach() {
michael@0 87 super.onDetach();
michael@0 88
michael@0 89 mNewTabsListener = null;
michael@0 90 }
michael@0 91
michael@0 92 @Override
michael@0 93 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
michael@0 94 return inflater.inflate(R.layout.home_last_tabs_panel, container, false);
michael@0 95 }
michael@0 96
michael@0 97 @Override
michael@0 98 public void onViewCreated(View view, Bundle savedInstanceState) {
michael@0 99 mTitle = (TextView) view.findViewById(R.id.title);
michael@0 100 if (mTitle != null) {
michael@0 101 mTitle.setText(R.string.home_last_tabs_title);
michael@0 102 }
michael@0 103
michael@0 104 mList = (HomeListView) view.findViewById(R.id.list);
michael@0 105 mList.setTag(HomePager.LIST_TAG_LAST_TABS);
michael@0 106
michael@0 107 mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
michael@0 108 @Override
michael@0 109 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
michael@0 110 final Cursor c = mAdapter.getCursor();
michael@0 111 if (c == null || !c.moveToPosition(position)) {
michael@0 112 return;
michael@0 113 }
michael@0 114
michael@0 115 Telemetry.sendUIEvent(TelemetryContract.Event.LOAD_URL);
michael@0 116
michael@0 117 final String url = c.getString(c.getColumnIndexOrThrow(Combined.URL));
michael@0 118 mNewTabsListener.onNewTabs(new String[] { url });
michael@0 119 }
michael@0 120 });
michael@0 121
michael@0 122 mList.setContextMenuInfoFactory(new HomeContextMenuInfo.Factory() {
michael@0 123 @Override
michael@0 124 public HomeContextMenuInfo makeInfoForCursor(View view, int position, long id, Cursor cursor) {
michael@0 125 final HomeContextMenuInfo info = new HomeContextMenuInfo(view, position, id);
michael@0 126 info.url = cursor.getString(cursor.getColumnIndexOrThrow(Combined.URL));
michael@0 127 info.title = cursor.getString(cursor.getColumnIndexOrThrow(Combined.TITLE));
michael@0 128 return info;
michael@0 129 }
michael@0 130 });
michael@0 131
michael@0 132 registerForContextMenu(mList);
michael@0 133
michael@0 134 mRestoreButton = view.findViewById(R.id.open_all_tabs_button);
michael@0 135 mRestoreButton.setOnClickListener(new View.OnClickListener() {
michael@0 136 @Override
michael@0 137 public void onClick(View v) {
michael@0 138 openAllTabs();
michael@0 139 }
michael@0 140 });
michael@0 141 }
michael@0 142
michael@0 143 @Override
michael@0 144 public void onDestroyView() {
michael@0 145 super.onDestroyView();
michael@0 146 mList = null;
michael@0 147 mTitle = null;
michael@0 148 mEmptyView = null;
michael@0 149 mRestoreButton = null;
michael@0 150 }
michael@0 151
michael@0 152 @Override
michael@0 153 public void onActivityCreated(Bundle savedInstanceState) {
michael@0 154 super.onActivityCreated(savedInstanceState);
michael@0 155
michael@0 156 // Intialize adapter
michael@0 157 mAdapter = new LastTabsAdapter(getActivity());
michael@0 158 mList.setAdapter(mAdapter);
michael@0 159
michael@0 160 // Create callbacks before the initial loader is started
michael@0 161 mCursorLoaderCallbacks = new CursorLoaderCallbacks();
michael@0 162 loadIfVisible();
michael@0 163 }
michael@0 164
michael@0 165 private void updateUiFromCursor(Cursor c) {
michael@0 166 if (c != null && c.getCount() > 0) {
michael@0 167 if (mTitle != null) {
michael@0 168 mTitle.setVisibility(View.VISIBLE);
michael@0 169 }
michael@0 170 mRestoreButton.setVisibility(View.VISIBLE);
michael@0 171 return;
michael@0 172 }
michael@0 173
michael@0 174 // Cursor is empty, so hide the title and set the
michael@0 175 // empty view if it hasn't been set already.
michael@0 176 if (mTitle != null) {
michael@0 177 mTitle.setVisibility(View.GONE);
michael@0 178 }
michael@0 179 mRestoreButton.setVisibility(View.GONE);
michael@0 180
michael@0 181 if (mEmptyView == null) {
michael@0 182 // Set empty panel view. We delay this so that the empty view won't flash.
michael@0 183 final ViewStub emptyViewStub = (ViewStub) getView().findViewById(R.id.home_empty_view_stub);
michael@0 184 mEmptyView = emptyViewStub.inflate();
michael@0 185
michael@0 186 final ImageView emptyIcon = (ImageView) mEmptyView.findViewById(R.id.home_empty_image);
michael@0 187 emptyIcon.setImageResource(R.drawable.icon_last_tabs_empty);
michael@0 188
michael@0 189 final TextView emptyText = (TextView) mEmptyView.findViewById(R.id.home_empty_text);
michael@0 190 emptyText.setText(R.string.home_last_tabs_empty);
michael@0 191
michael@0 192 mList.setEmptyView(mEmptyView);
michael@0 193 }
michael@0 194 }
michael@0 195
michael@0 196 @Override
michael@0 197 protected void load() {
michael@0 198 getLoaderManager().initLoader(LOADER_ID_LAST_TABS, null, mCursorLoaderCallbacks);
michael@0 199 }
michael@0 200
michael@0 201 private void openAllTabs() {
michael@0 202 final Cursor c = mAdapter.getCursor();
michael@0 203 if (c == null || !c.moveToFirst()) {
michael@0 204 return;
michael@0 205 }
michael@0 206
michael@0 207 final String[] urls = new String[c.getCount()];
michael@0 208
michael@0 209 do {
michael@0 210 urls[c.getPosition()] = c.getString(c.getColumnIndexOrThrow(Combined.URL));
michael@0 211 } while (c.moveToNext());
michael@0 212
michael@0 213 Telemetry.sendUIEvent(TelemetryContract.Event.LOAD_URL, TelemetryContract.Method.BUTTON);
michael@0 214
michael@0 215 mNewTabsListener.onNewTabs(urls);
michael@0 216 }
michael@0 217
michael@0 218 private static class LastTabsCursorLoader extends SimpleCursorLoader {
michael@0 219 public LastTabsCursorLoader(Context context) {
michael@0 220 super(context);
michael@0 221 }
michael@0 222
michael@0 223 @Override
michael@0 224 public Cursor loadCursor() {
michael@0 225 final Context context = getContext();
michael@0 226
michael@0 227 final String jsonString = GeckoProfile.get(context).readSessionFile(true);
michael@0 228 if (jsonString == null) {
michael@0 229 // No previous session data
michael@0 230 return null;
michael@0 231 }
michael@0 232
michael@0 233 final MatrixCursor c = new MatrixCursor(new String[] { Combined._ID,
michael@0 234 Combined.URL,
michael@0 235 Combined.TITLE });
michael@0 236
michael@0 237 new SessionParser() {
michael@0 238 @Override
michael@0 239 public void onTabRead(SessionTab tab) {
michael@0 240 final String url = tab.getUrl();
michael@0 241
michael@0 242 // Don't show last tabs for about:home
michael@0 243 if (AboutPages.isAboutHome(url)) {
michael@0 244 return;
michael@0 245 }
michael@0 246
michael@0 247 final RowBuilder row = c.newRow();
michael@0 248 row.add(-1);
michael@0 249 row.add(url);
michael@0 250
michael@0 251 final String title = tab.getTitle();
michael@0 252 row.add(title);
michael@0 253 }
michael@0 254 }.parse(jsonString);
michael@0 255
michael@0 256 return c;
michael@0 257 }
michael@0 258 }
michael@0 259
michael@0 260 private static class LastTabsAdapter extends CursorAdapter {
michael@0 261 public LastTabsAdapter(Context context) {
michael@0 262 super(context, null, 0);
michael@0 263 }
michael@0 264
michael@0 265 @Override
michael@0 266 public void bindView(View view, Context context, Cursor cursor) {
michael@0 267 ((TwoLinePageRow) view).updateFromCursor(cursor);
michael@0 268 }
michael@0 269
michael@0 270 @Override
michael@0 271 public View newView(Context context, Cursor cursor, ViewGroup parent) {
michael@0 272 return LayoutInflater.from(context).inflate(R.layout.home_item_row, parent, false);
michael@0 273 }
michael@0 274 }
michael@0 275
michael@0 276 private class CursorLoaderCallbacks implements LoaderCallbacks<Cursor> {
michael@0 277 @Override
michael@0 278 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
michael@0 279 return new LastTabsCursorLoader(getActivity());
michael@0 280 }
michael@0 281
michael@0 282 @Override
michael@0 283 public void onLoadFinished(Loader<Cursor> loader, Cursor c) {
michael@0 284 mAdapter.swapCursor(c);
michael@0 285 updateUiFromCursor(c);
michael@0 286 }
michael@0 287
michael@0 288 @Override
michael@0 289 public void onLoaderReset(Loader<Cursor> loader) {
michael@0 290 mAdapter.swapCursor(null);
michael@0 291 }
michael@0 292 }
michael@0 293 }

mercurial