mobile/android/base/home/PinSiteDialog.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 org.mozilla.gecko.R;
michael@0 9 import org.mozilla.gecko.db.BrowserDB.URLColumns;
michael@0 10
michael@0 11 import android.content.Context;
michael@0 12 import android.database.Cursor;
michael@0 13 import android.os.Bundle;
michael@0 14 import android.support.v4.app.DialogFragment;
michael@0 15 import android.support.v4.app.LoaderManager;
michael@0 16 import android.support.v4.app.LoaderManager.LoaderCallbacks;
michael@0 17 import android.support.v4.content.Loader;
michael@0 18 import android.support.v4.widget.CursorAdapter;
michael@0 19 import android.text.Editable;
michael@0 20 import android.text.TextUtils;
michael@0 21 import android.text.TextWatcher;
michael@0 22 import android.view.KeyEvent;
michael@0 23 import android.view.LayoutInflater;
michael@0 24 import android.view.View;
michael@0 25 import android.view.ViewGroup;
michael@0 26 import android.view.WindowManager;
michael@0 27 import android.widget.AdapterView;
michael@0 28 import android.widget.EditText;
michael@0 29 import android.widget.ListView;
michael@0 30
michael@0 31 /**
michael@0 32 * Dialog fragment that displays frecency search results, for pinning a site, in a GridView.
michael@0 33 */
michael@0 34 class PinSiteDialog extends DialogFragment {
michael@0 35 // Listener for url selection
michael@0 36 public static interface OnSiteSelectedListener {
michael@0 37 public void onSiteSelected(String url, String title);
michael@0 38 }
michael@0 39
michael@0 40 // Cursor loader ID for search query
michael@0 41 private static final int LOADER_ID_SEARCH = 0;
michael@0 42
michael@0 43 // Holds the current search term to use in the query
michael@0 44 private String mSearchTerm;
michael@0 45
michael@0 46 // Adapter for the list of search results
michael@0 47 private SearchAdapter mAdapter;
michael@0 48
michael@0 49 // Search entry
michael@0 50 private EditText mSearch;
michael@0 51
michael@0 52 // Search results
michael@0 53 private ListView mList;
michael@0 54
michael@0 55 // Callbacks used for the search loader
michael@0 56 private CursorLoaderCallbacks mLoaderCallbacks;
michael@0 57
michael@0 58 // Bookmark selected listener
michael@0 59 private OnSiteSelectedListener mOnSiteSelectedListener;
michael@0 60
michael@0 61 public static PinSiteDialog newInstance() {
michael@0 62 return new PinSiteDialog();
michael@0 63 }
michael@0 64
michael@0 65 private PinSiteDialog() {
michael@0 66 }
michael@0 67
michael@0 68 @Override
michael@0 69 public void onCreate(Bundle savedInstanceState) {
michael@0 70 super.onCreate(savedInstanceState);
michael@0 71
michael@0 72 setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Holo_Light_Dialog);
michael@0 73 }
michael@0 74
michael@0 75 @Override
michael@0 76 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
michael@0 77 // All list views are styled to look the same with a global activity theme.
michael@0 78 // If the style of the list changes, inflate it from an XML.
michael@0 79 return inflater.inflate(R.layout.pin_site_dialog, container, false);
michael@0 80 }
michael@0 81
michael@0 82 @Override
michael@0 83 public void onViewCreated(View view, Bundle savedInstanceState) {
michael@0 84 super.onViewCreated(view, savedInstanceState);
michael@0 85
michael@0 86 mSearch = (EditText) view.findViewById(R.id.search);
michael@0 87 mSearch.addTextChangedListener(new TextWatcher() {
michael@0 88 @Override
michael@0 89 public void afterTextChanged(Editable s) {
michael@0 90 }
michael@0 91
michael@0 92 @Override
michael@0 93 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
michael@0 94 }
michael@0 95
michael@0 96 @Override
michael@0 97 public void onTextChanged(CharSequence s, int start, int before, int count) {
michael@0 98 setSearchTerm(mSearch.getText().toString());
michael@0 99 filter(mSearchTerm);
michael@0 100 }
michael@0 101 });
michael@0 102
michael@0 103 mSearch.setOnKeyListener(new View.OnKeyListener() {
michael@0 104 @Override
michael@0 105 public boolean onKey(View v, int keyCode, KeyEvent event) {
michael@0 106 if (keyCode != KeyEvent.KEYCODE_ENTER || mOnSiteSelectedListener == null) {
michael@0 107 return false;
michael@0 108 }
michael@0 109
michael@0 110 // If the user manually entered a search term or URL, wrap the value in
michael@0 111 // a special URI until we can get a valid URL for this bookmark.
michael@0 112 final String text = mSearch.getText().toString().trim();
michael@0 113 if (!TextUtils.isEmpty(text)) {
michael@0 114 final String url = TopSitesPanel.encodeUserEnteredUrl(text);
michael@0 115 mOnSiteSelectedListener.onSiteSelected(url, text);
michael@0 116 dismiss();
michael@0 117 }
michael@0 118
michael@0 119 return true;
michael@0 120 }
michael@0 121 });
michael@0 122
michael@0 123 mSearch.setOnFocusChangeListener(new View.OnFocusChangeListener() {
michael@0 124 @Override
michael@0 125 public void onFocusChange(View v, boolean hasFocus) {
michael@0 126 if (hasFocus) {
michael@0 127 getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
michael@0 128 }
michael@0 129 }
michael@0 130 });
michael@0 131
michael@0 132 mList = (HomeListView) view.findViewById(R.id.list);
michael@0 133 mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
michael@0 134 @Override
michael@0 135 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
michael@0 136 if (mOnSiteSelectedListener != null) {
michael@0 137 final Cursor c = mAdapter.getCursor();
michael@0 138 if (c == null || !c.moveToPosition(position)) {
michael@0 139 return;
michael@0 140 }
michael@0 141
michael@0 142 final String url = c.getString(c.getColumnIndexOrThrow(URLColumns.URL));
michael@0 143 final String title = c.getString(c.getColumnIndexOrThrow(URLColumns.TITLE));
michael@0 144 mOnSiteSelectedListener.onSiteSelected(url, title);
michael@0 145 }
michael@0 146
michael@0 147 // Dismiss the fragment and the dialog.
michael@0 148 dismiss();
michael@0 149 }
michael@0 150 });
michael@0 151 }
michael@0 152
michael@0 153 @Override
michael@0 154 public void onActivityCreated(Bundle savedInstanceState) {
michael@0 155 super.onActivityCreated(savedInstanceState);
michael@0 156
michael@0 157 final LoaderManager manager = getLoaderManager();
michael@0 158
michael@0 159 // Initialize the search adapter
michael@0 160 mAdapter = new SearchAdapter(getActivity());
michael@0 161 mList.setAdapter(mAdapter);
michael@0 162
michael@0 163 // Create callbacks before the initial loader is started
michael@0 164 mLoaderCallbacks = new CursorLoaderCallbacks();
michael@0 165
michael@0 166 // Reconnect to the loader only if present
michael@0 167 manager.initLoader(LOADER_ID_SEARCH, null, mLoaderCallbacks);
michael@0 168
michael@0 169 // If there is a search term, put it in the text field
michael@0 170 if (!TextUtils.isEmpty(mSearchTerm)) {
michael@0 171 mSearch.setText(mSearchTerm);
michael@0 172 mSearch.selectAll();
michael@0 173 }
michael@0 174
michael@0 175 // Always start with an empty filter
michael@0 176 filter("");
michael@0 177 }
michael@0 178
michael@0 179 @Override
michael@0 180 public void onDestroyView() {
michael@0 181 super.onDestroyView();
michael@0 182
michael@0 183 // Discard any additional site selection as the dialog
michael@0 184 // is getting destroyed (see bug 935542).
michael@0 185 setOnSiteSelectedListener(null);
michael@0 186 }
michael@0 187
michael@0 188 public void setSearchTerm(String searchTerm) {
michael@0 189 mSearchTerm = searchTerm;
michael@0 190 }
michael@0 191
michael@0 192 private void filter(String searchTerm) {
michael@0 193 // Restart loaders with the new search term
michael@0 194 SearchLoader.restart(getLoaderManager(), LOADER_ID_SEARCH, mLoaderCallbacks, searchTerm);
michael@0 195 }
michael@0 196
michael@0 197 public void setOnSiteSelectedListener(OnSiteSelectedListener listener) {
michael@0 198 mOnSiteSelectedListener = listener;
michael@0 199 }
michael@0 200
michael@0 201 private static class SearchAdapter extends CursorAdapter {
michael@0 202 private LayoutInflater mInflater;
michael@0 203
michael@0 204 public SearchAdapter(Context context) {
michael@0 205 super(context, null, 0);
michael@0 206 mInflater = LayoutInflater.from(context);
michael@0 207 }
michael@0 208
michael@0 209 @Override
michael@0 210 public void bindView(View view, Context context, Cursor cursor) {
michael@0 211 TwoLinePageRow row = (TwoLinePageRow) view;
michael@0 212 row.setShowIcons(false);
michael@0 213 row.updateFromCursor(cursor);
michael@0 214 }
michael@0 215
michael@0 216 @Override
michael@0 217 public View newView(Context context, Cursor cursor, ViewGroup parent) {
michael@0 218 return (TwoLinePageRow) mInflater.inflate(R.layout.home_item_row, parent, false);
michael@0 219 }
michael@0 220 }
michael@0 221
michael@0 222 private class CursorLoaderCallbacks implements LoaderCallbacks<Cursor> {
michael@0 223 @Override
michael@0 224 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
michael@0 225 return SearchLoader.createInstance(getActivity(), args);
michael@0 226 }
michael@0 227
michael@0 228 @Override
michael@0 229 public void onLoadFinished(Loader<Cursor> loader, Cursor c) {
michael@0 230 mAdapter.swapCursor(c);
michael@0 231 }
michael@0 232
michael@0 233 @Override
michael@0 234 public void onLoaderReset(Loader<Cursor> loader) {
michael@0 235 mAdapter.swapCursor(null);
michael@0 236 }
michael@0 237 }
michael@0 238 }

mercurial