1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
|
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.browser;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.Fragment;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.ClipboardManager;
import android.content.ContentResolver;
import android.content.Context;
import android.content.CursorLoader;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.Loader;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Browser;
import android.provider.BrowserContract;
import android.provider.BrowserContract.History;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebIconDatabase.IconListener;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.ExpandableListContextMenuInfo;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.TextView;
import android.widget.Toast;
/**
* Activity for displaying the browser's history, divided into
* days of viewing.
*/
public class BrowserHistoryPage extends Fragment
implements LoaderCallbacks<Cursor>, OnChildClickListener {
static final int LOADER_HISTORY = 1;
static final int LOADER_MOST_VISITED = 2;
BookmarksHistoryCallbacks mCallbacks;
ExpandableListView mList;
View mEmptyView;
HistoryAdapter mAdapter;
boolean mDisableNewWindow;
HistoryItem mContextHeader;
String mMostVisitsLimit;
// Implementation of WebIconDatabase.IconListener
class IconReceiver implements IconListener {
@Override
public void onReceivedIcon(String url, Bitmap icon) {
mAdapter.notifyDataSetChanged();
}
}
// Instance of IconReceiver
final IconReceiver mIconReceiver = new IconReceiver();
static interface HistoryQuery {
static final String[] PROJECTION = new String[] {
History._ID, // 0
History.DATE_LAST_VISITED, // 1
History.TITLE, // 2
History.URL, // 3
History.FAVICON, // 4
History.VISITS // 5
};
static final int INDEX_ID = 0;
static final int INDEX_DATE_LAST_VISITED = 1;
static final int INDEX_TITE = 2;
static final int INDEX_URL = 3;
static final int INDEX_FAVICON = 4;
static final int INDEX_VISITS = 5;
}
private void copy(CharSequence text) {
ClipboardManager cm = (ClipboardManager) getActivity().getSystemService(
Context.CLIPBOARD_SERVICE);
cm.setText(text);
}
static BrowserHistoryPage newInstance(BookmarksHistoryCallbacks cb, Bundle args) {
BrowserHistoryPage bhp = new BrowserHistoryPage();
bhp.mCallbacks = cb;
bhp.setArguments(args);
return bhp;
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
switch (id) {
case LOADER_HISTORY: {
CursorLoader loader = new CursorLoader(getActivity(), History.CONTENT_URI,
HistoryQuery.PROJECTION, null, null, null);
return loader;
}
case LOADER_MOST_VISITED: {
Uri uri = History.CONTENT_URI
.buildUpon()
.appendQueryParameter(BrowserContract.PARAM_LIMIT, mMostVisitsLimit)
.build();
CursorLoader loader = new CursorLoader(getActivity(), uri,
HistoryQuery.PROJECTION, null, null, History.VISITS + " DESC");
return loader;
}
default: {
throw new IllegalArgumentException();
}
}
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
switch (loader.getId()) {
case LOADER_HISTORY: {
mAdapter.changeCursor(data);
// Add an empty view late, so it does not claim an empty
// history before the adapter is present
mList.setEmptyView(mEmptyView);
break;
}
case LOADER_MOST_VISITED: {
mAdapter.changeMostVisitedCursor(data);
// Add an empty view late, so it does not claim an empty
// history before the adapter is present
mList.setEmptyView(mEmptyView);
break;
}
default: {
throw new IllegalArgumentException();
}
}
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setHasOptionsMenu(true);
Bundle args = getArguments();
mDisableNewWindow = args.getBoolean(BrowserBookmarksPage.EXTRA_DISABLE_WINDOW, false);
int mvlimit = getResources().getInteger(R.integer.most_visits_limit);
mMostVisitsLimit = Integer.toString(mvlimit);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.history, container, false);
mList = (ExpandableListView) root.findViewById(android.R.id.list);
mList.setCacheColorHint(0);
mList.setOnCreateContextMenuListener(this);
mList.setOnChildClickListener(this);
mAdapter = new HistoryAdapter(getActivity());
mList.setAdapter(mAdapter);
mEmptyView = root.findViewById(android.R.id.empty);
// Start the loader
getLoaderManager().initLoader(LOADER_HISTORY, null, this);
getLoaderManager().initLoader(LOADER_MOST_VISITED, null, this);
// Register to receive icons in case they haven't all been loaded.
CombinedBookmarkHistoryView.getIconListenerSet().addListener(mIconReceiver);
return root;
}
@Override
public void onDestroy() {
super.onDestroy();
CombinedBookmarkHistoryView.getIconListenerSet().removeListener(mIconReceiver);
getLoaderManager().stopLoader(LOADER_HISTORY);
getLoaderManager().stopLoader(LOADER_MOST_VISITED);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.history, menu);
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
menu.findItem(R.id.clear_history_menu_id).setVisible(
mAdapter != null && !mAdapter.isEmpty());
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.clear_history_menu_id:
final ContentResolver resolver = getActivity().getContentResolver();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
.setTitle(R.string.clear)
.setMessage(R.string.pref_privacy_clear_history_dlg)
.setIcon(android.R.drawable.ic_dialog_alert)
.setNegativeButton(R.string.cancel, null)
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
Browser.clearHistory(resolver);
mCallbacks.onRemoveParentChildRelationships();
}
}
});
final Dialog dialog = builder.create();
dialog.show();
return true;
default:
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
ExpandableListContextMenuInfo i = (ExpandableListContextMenuInfo) menuInfo;
// Do not allow a context menu to come up from the group views.
if (!(i.targetView instanceof HistoryItem)) {
return;
}
// Inflate the menu
Activity parent = getActivity();
MenuInflater inflater = parent.getMenuInflater();
inflater.inflate(R.menu.historycontext, menu);
HistoryItem historyItem = (HistoryItem) i.targetView;
// Setup the header
if (mContextHeader == null) {
mContextHeader = new HistoryItem(parent);
} else if (mContextHeader.getParent() != null) {
((ViewGroup) mContextHeader.getParent()).removeView(mContextHeader);
}
historyItem.copyTo(mContextHeader);
menu.setHeaderView(mContextHeader);
// Only show open in new tab if it was not explicitly disabled
if (mDisableNewWindow) {
menu.findItem(R.id.new_window_context_menu_id).setVisible(false);
}
// For a bookmark, provide the option to remove it from bookmarks
if (historyItem.isBookmark()) {
MenuItem item = menu.findItem(R.id.save_to_bookmarks_menu_id);
item.setTitle(R.string.remove_from_bookmarks);
}
// decide whether to show the share link option
PackageManager pm = parent.getPackageManager();
Intent send = new Intent(Intent.ACTION_SEND);
send.setType("text/plain");
ResolveInfo ri = pm.resolveActivity(send, PackageManager.MATCH_DEFAULT_ONLY);
menu.findItem(R.id.share_link_context_menu_id).setVisible(ri != null);
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
ExpandableListContextMenuInfo i =
(ExpandableListContextMenuInfo) item.getMenuInfo();
if (i == null) {
return false;
}
HistoryItem historyItem = (HistoryItem) i.targetView;
String url = historyItem.getUrl();
String title = historyItem.getName();
Activity activity = getActivity();
switch (item.getItemId()) {
case R.id.open_context_menu_id:
mCallbacks.onUrlSelected(url, false);
return true;
case R.id.new_window_context_menu_id:
mCallbacks.onUrlSelected(url, true);
return true;
case R.id.save_to_bookmarks_menu_id:
if (historyItem.isBookmark()) {
Bookmarks.removeFromBookmarks(activity, activity.getContentResolver(),
url, title);
} else {
Browser.saveBookmark(activity, title, url);
}
return true;
case R.id.share_link_context_menu_id:
Browser.sendString(activity, url,
activity.getText(R.string.choosertitle_sharevia).toString());
return true;
case R.id.copy_url_context_menu_id:
copy(url);
return true;
case R.id.delete_context_menu_id:
Browser.deleteFromHistory(activity.getContentResolver(), url);
return true;
case R.id.homepage_context_menu_id:
BrowserSettings.getInstance().setHomePage(activity, url);
Toast.makeText(activity, R.string.homepage_set, Toast.LENGTH_LONG).show();
return true;
default:
break;
}
return super.onContextItemSelected(item);
}
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
if (v instanceof HistoryItem) {
mCallbacks.onUrlSelected(((HistoryItem) v).getUrl(), false);
return true;
}
return false;
}
private class HistoryAdapter extends DateSortedExpandableListAdapter {
private Cursor mMostVisited, mHistoryCursor;
HistoryAdapter(Context context) {
super(context, HistoryQuery.INDEX_DATE_LAST_VISITED);
}
@Override
public void changeCursor(Cursor cursor) {
mHistoryCursor = cursor;
super.changeCursor(cursor);
}
void changeMostVisitedCursor(Cursor cursor) {
if (mMostVisited == cursor) {
return;
}
if (mMostVisited != null) {
mMostVisited.unregisterDataSetObserver(mDataSetObserver);
mMostVisited.close();
}
mMostVisited = cursor;
if (mMostVisited != null) {
mMostVisited.registerDataSetObserver(mDataSetObserver);
}
notifyDataSetChanged();
}
@Override
public long getChildId(int groupPosition, int childPosition) {
if (!mDataValid) return 0;
if (moveCursorToChildPosition(groupPosition, childPosition)) {
Cursor cursor = getCursor(groupPosition);
return cursor.getLong(HistoryQuery.INDEX_ID);
}
return 0;
}
@Override
public int getGroupCount() {
return super.getGroupCount() + (mMostVisited != null ? 1 : 0);
}
@Override
public int getChildrenCount(int groupPosition) {
if (groupPosition >= super.getGroupCount()) {
return mMostVisited.getCount();
}
return super.getChildrenCount(groupPosition);
}
@Override
public boolean isEmpty() {
if (!super.isEmpty()) {
return false;
}
return mMostVisited == null
|| mMostVisited.isClosed()
|| mMostVisited.getCount() == 0;
}
Cursor getCursor(int groupPosition) {
if (groupPosition >= super.getGroupCount()) {
return mMostVisited;
}
return mHistoryCursor;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (groupPosition >= super.getGroupCount()) {
if (!mDataValid) throw new IllegalStateException("Data is not valid");
TextView item;
if (null == convertView || !(convertView instanceof TextView)) {
LayoutInflater factory = LayoutInflater.from(getContext());
item = (TextView) factory.inflate(R.layout.history_header, null);
} else {
item = (TextView) convertView;
}
item.setText(R.string.tab_most_visited);
return item;
}
return super.getGroupView(groupPosition, isExpanded, convertView, parent);
}
@Override
boolean moveCursorToChildPosition(
int groupPosition, int childPosition) {
if (groupPosition >= super.getGroupCount()) {
if (mDataValid && !mMostVisited.isClosed()) {
mMostVisited.moveToPosition(childPosition);
return true;
}
return false;
}
return super.moveCursorToChildPosition(groupPosition, childPosition);
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
HistoryItem item;
if (null == convertView || !(convertView instanceof HistoryItem)) {
item = new HistoryItem(getContext());
// Add padding on the left so it will be indented from the
// arrows on the group views.
item.setPadding(item.getPaddingLeft() + 10,
item.getPaddingTop(),
item.getPaddingRight(),
item.getPaddingBottom());
} else {
item = (HistoryItem) convertView;
}
// Bail early if the Cursor is closed.
if (!moveCursorToChildPosition(groupPosition, childPosition)) {
return item;
}
Cursor cursor = getCursor(groupPosition);
item.setName(cursor.getString(HistoryQuery.INDEX_TITE));
String url = cursor.getString(HistoryQuery.INDEX_URL);
item.setUrl(url);
byte[] data = cursor.getBlob(HistoryQuery.INDEX_FAVICON);
if (data != null) {
item.setFavicon(BitmapFactory.decodeByteArray(data, 0,
data.length));
} else {
item.setFavicon(CombinedBookmarkHistoryView
.getIconListenerSet().getFavicon(url));
}
return item;
}
}
}
|