Bug 695199: Add/Remove bookmarks through menus [r=mfinkle]

This commit is contained in:
Sriram Ramasubramanian 2011-10-26 14:33:16 -07:00
Родитель 76d9ad660f
Коммит eeceee0060
12 изменённых файлов: 166 добавлений и 171 удалений

Просмотреть файл

@ -438,6 +438,28 @@ abstract public class GeckoApp
mi.setOnMenuItemClickListener(item);
}
}
Tab tab = Tabs.getInstance().getSelectedTab();
MenuItem bookmark = aMenu.findItem(R.id.bookmark);
if (tab == null) {
bookmark.setVisible(false);
return true;
}
bookmark.setVisible(true);
bookmark.setCheckable(true);
if (tab.isBookmark()) {
bookmark.setChecked(true);
bookmark.setIcon(R.drawable.bookmark_remove);
bookmark.setTitle(R.string.bookmark_remove);
} else {
bookmark.setChecked(false);
bookmark.setIcon(R.drawable.bookmark_add);
bookmark.setTitle(R.string.bookmark_add);
}
return true;
}
@ -445,23 +467,25 @@ abstract public class GeckoApp
public boolean onOptionsItemSelected(MenuItem item) {
Tab tab = null;
Tab.HistoryEntry he = null;
Intent intent = null;
switch (item.getItemId()) {
case R.id.quit:
quit();
return true;
case R.id.bookmarks:
Intent intent = new Intent(this, GeckoBookmarks.class);
case R.id.bookmark:
tab = Tabs.getInstance().getSelectedTab();
if (tab == null) {
startActivity(intent);
return true;
}
he = tab.getLastHistoryEntry();
if (he != null) {
intent.setData(android.net.Uri.parse(he.mUri));
intent.putExtra("title", he.mTitle);
startActivity(intent);
if (tab != null) {
if (item.isChecked()) {
tab.removeBookmark();
Toast.makeText(this, R.string.bookmark_removed, Toast.LENGTH_SHORT).show();
item.setIcon(R.drawable.bookmark_add);
item.setTitle(R.string.bookmark_add);
} else {
tab.addBookmark();
Toast.makeText(this, R.string.bookmark_added, Toast.LENGTH_SHORT).show();
item.setIcon(R.drawable.bookmark_remove);
item.setTitle(R.string.bookmark_remove);
}
}
return true;
case R.id.share:
@ -1074,12 +1098,15 @@ abstract public class GeckoApp
mDoorHanger = new DoorHanger(this);
Tab tab = Tabs.getInstance().getSelectedTab();
Tabs tabs = Tabs.getInstance();
Tab tab = tabs.getSelectedTab();
if (tab != null) {
mBrowserToolbar.setTitle(tab.getTitle());
mBrowserToolbar.setFavicon(tab.getFavicon());
mBrowserToolbar.updateTabs(Tabs.getInstance().getCount());
}
}
tabs.setContentResolver(getContentResolver());
if (surfaceView == null) {
surfaceView = new GeckoSurfaceView(this);

Просмотреть файл

@ -1,100 +0,0 @@
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (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.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Android code.
*
* The Initial Developer of the Original Code is Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2009-2011
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Brad Lassey <blassey@mozilla.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
package org.mozilla.gecko;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Browser;
import android.util.Log;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class GeckoBookmarks extends ListActivity {
private static final String LOG_NAME = "GeckoBookmarks";
private static final String TITLE_KEY = "title";
private static final String kBookmarksWhereClause = Browser.BookmarkColumns.BOOKMARK + " = 1";
private Cursor mCursor;
private Uri mUri;
private String mTitle;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bookmarks);
mCursor = managedQuery(Browser.BOOKMARKS_URI,
null, kBookmarksWhereClause, null, null);
startManagingCursor(mCursor);
ListAdapter adapter =
new SimpleCursorAdapter(this, R.layout.bookmark_list_row, mCursor,
new String[] {Browser.BookmarkColumns.TITLE,
Browser.BookmarkColumns.URL},
new int[] {R.id.bookmark_title, R.id.bookmark_url});
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
mCursor.moveToPosition(position);
String spec = mCursor.getString(mCursor.getColumnIndex(Browser.BookmarkColumns.URL));
Log.i(LOG_NAME, "clicked: " + spec);
Intent intent = new Intent(this, GeckoApp.mAppContext.getClass());
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse(spec));
startActivity(intent);
}
public void addBookmark(View v) {
if (mUri != null)
Browser.saveBookmark(this, mTitle, mUri.toString());
finish();
}
@Override
protected void onNewIntent(Intent intent) {
// just save the uri from the intent
mUri = intent.getData();
mTitle = intent.getStringExtra(TITLE_KEY);
}
}

Просмотреть файл

@ -56,7 +56,6 @@ JAVAFILES = \
AlertNotification.java \
AwesomeBar.java \
AwesomeBarTabs.java \
GeckoBookmarks.java \
Tab.java \
Tabs.java \
GeckoEventListener.java \
@ -145,8 +144,6 @@ RES_LAYOUT = \
res/layout/awesomebar_row.xml \
res/layout/awesomebar_tabs.xml \
res/layout/browser_toolbar.xml \
res/layout/bookmarks.xml \
res/layout/bookmark_list_row.xml \
res/layout/tabs_tray.xml \
res/layout/tabs_row.xml \
res/layout/doorhangerpopup.xml \
@ -185,6 +182,9 @@ MOZ_ANDROID_DRAWABLES += embedding/android/resources/drawable/desktop_notificati
embedding/android/resources/drawable/address_bar_button_right.9.png \
embedding/android/resources/drawable/address_bar_button_middle.9.png \
embedding/android/resources/drawable/address_bar_bg.9.png \
embedding/android/resources/drawable/bookmark_add.png \
embedding/android/resources/drawable/bookmark_remove.png \
embedding/android/resources/drawable/addons.png \
embedding/android/resources/drawable/share.png \
$(NULL)

Просмотреть файл

@ -40,10 +40,12 @@ package org.mozilla.gecko;
import java.util.*;
import android.content.*;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.graphics.drawable.*;
import android.util.Log;
import android.provider.Browser;
public class Tab {
@ -53,6 +55,7 @@ public class Tab {
private Drawable favicon, thumbnail;
private Stack<HistoryEntry> history;
private boolean loading;
private boolean bookmark;
static class HistoryEntry {
public final String mUri;
@ -71,6 +74,7 @@ public class Tab {
this.favicon = null;
this.thumbnail = null;
this.history = new Stack<HistoryEntry>();
this.bookmark = false;
}
public Tab(int id, String url) {
@ -80,6 +84,7 @@ public class Tab {
this.favicon = null;
this.thumbnail = null;
this.history = new Stack<HistoryEntry>();
this.bookmark = false;
}
public int getId() {
@ -102,6 +107,10 @@ public class Tab {
return loading;
}
public boolean isBookmark() {
return bookmark;
}
public Stack<HistoryEntry> getHistory() {
return history;
}
@ -111,6 +120,7 @@ public class Tab {
if(url != null && url.length() > 0) {
this.url = new String(url);
Log.i(LOG_FILE_NAME, "Updated url: " + url + " for tab with id: " + this.id);
updateBookmark();
}
}
@ -125,6 +135,10 @@ public class Tab {
this.loading = loading;
}
private void setBookmark(boolean bookmark) {
this.bookmark = bookmark;
}
public void addHistory(HistoryEntry entry) {
if (history.empty() || !history.peek().mUri.equals(entry.mUri)) {
history.push(entry);
@ -142,6 +156,18 @@ public class Tab {
this.favicon = favicon;
Log.i(LOG_FILE_NAME, "Updated favicon for tab with id: " + this.id);
}
private void updateBookmark() {
new CheckBookmarkTask().execute();
}
public void addBookmark() {
new AddBookmarkTask().execute();
}
public void removeBookmark() {
new RemoveBookmarkTask().execute();
}
public boolean doReload() {
if (history.empty())
@ -168,4 +194,80 @@ public class Tab {
return null;
}
}
private class CheckBookmarkTask extends AsyncTask<Void, Void, Boolean> {
@Override
protected Boolean doInBackground(Void... unused) {
ContentResolver resolver = Tabs.getInstance().getContentResolver();
Cursor cursor = resolver.query(Browser.BOOKMARKS_URI,
null,
Browser.BookmarkColumns.URL + " = ? and " + Browser.BookmarkColumns.BOOKMARK + " = ?",
new String[] { getURL(), "1" },
Browser.BookmarkColumns.URL);
if (cursor.getCount() == 1)
return true;
else
return false;
}
@Override
protected void onPostExecute(Boolean isBookmark) {
setBookmark(isBookmark.booleanValue());
}
}
private class AddBookmarkTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... unused) {
ContentResolver resolver = Tabs.getInstance().getContentResolver();
Cursor cursor = resolver.query(Browser.BOOKMARKS_URI,
null,
Browser.BookmarkColumns.URL + " = ?",
new String[] { getURL() },
Browser.BookmarkColumns.URL);
ContentValues values = new ContentValues();
values.put(Browser.BookmarkColumns.BOOKMARK, "1");
values.put(Browser.BookmarkColumns.TITLE, getTitle());
if (cursor.getCount() == 1) {
//entry exists, update the bookmark flag
resolver.update(Browser.BOOKMARKS_URI,
values,
Browser.BookmarkColumns.URL + " = ?",
new String[] { getURL() });
} else {
//add a new entry
values.put(Browser.BookmarkColumns.URL, url);
resolver.insert(Browser.BOOKMARKS_URI,
values);
}
return null;
}
@Override
protected void onPostExecute(Void unused) {
setBookmark(true);
}
}
private class RemoveBookmarkTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... unused) {
ContentResolver resolver = Tabs.getInstance().getContentResolver();
ContentValues values = new ContentValues();
values.put(Browser.BookmarkColumns.BOOKMARK, "0");
resolver.update(Browser.BOOKMARKS_URI,
values,
Browser.BookmarkColumns.URL + " = ?",
new String[] { getURL() });
return null;
}
@Override
protected void onPostExecute(Void unused) {
setBookmark(false);
}
}
}

Просмотреть файл

@ -39,6 +39,7 @@ package org.mozilla.gecko;
import java.util.*;
import android.content.ContentResolver;
import android.graphics.drawable.*;
import android.util.Log;
@ -48,6 +49,7 @@ public class Tabs {
private static int selectedTab = -1;
private HashMap<Integer, Tab> tabs;
private ArrayList<Tab> order;
private ContentResolver resolver;
private Tabs() {
tabs = new HashMap<Integer, Tab>();
@ -126,6 +128,14 @@ public class Tabs {
return tabs;
}
public void setContentResolver(ContentResolver resolver) {
this.resolver = resolver;
}
public ContentResolver getContentResolver() {
return resolver;
}
//Making Tabs a singleton class
private static class TabsInstanceHolder {
private static final Tabs INSTANCE = new Tabs();

Просмотреть файл

@ -24,8 +24,10 @@
<!ENTITY choose_file "Choose File">
<!ENTITY bookmarks_title "Bookmarks">
<!ENTITY bookmarks "Bookmarks">
<!ENTITY bookmark_add "Add Bookmark">
<!ENTITY bookmark_add "Bookmark">
<!ENTITY bookmark_remove "Remove">
<!ENTITY bookmark_added "Bookmark added successfully!">
<!ENTITY bookmark_removed "Bookmark removed!">
<!ENTITY history_today_section "Today">
<!ENTITY history_yesterday_section "Yesterday">

Двоичные данные
embedding/android/resources/drawable/bookmark_add.png Normal file

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 1.8 KiB

Двоичные данные
embedding/android/resources/drawable/bookmark_remove.png Normal file

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 2.6 KiB

Просмотреть файл

@ -1,32 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<ImageView
android:id="@+id/bookmark_icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="6dip"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:id="@+id/bookmark_title"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center_vertical"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:id="@+id/bookmark_url"
android:singleLine="true"
android:ellipsize="marquee"
/>
</LinearLayout>
</LinearLayout>

Просмотреть файл

@ -1,17 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/bookmark_add"
android:onClick="addBookmark"/>
<ListView
android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

Просмотреть файл

@ -4,8 +4,9 @@
<item android:id="@+id/reload"
android:title="@string/reload"/>
<item android:id="@+id/bookmarks"
android:title="@string/bookmarks"
<item android:id="@+id/bookmark"
android:title="@string/bookmark_add"
android:icon="@drawable/bookmark_add"
android:showAsAction="ifRoom"/>
<item android:id="@+id/preferences"

Просмотреть файл

@ -31,8 +31,10 @@
<string name="quit">&quit;</string>
<string name="bookmarks_title">&bookmarks_title;</string>
<string name="bookmarks">&bookmarks;</string>
<string name="bookmark_add">&bookmark_add;</string>
<string name="bookmark_remove">&bookmark_remove;</string>
<string name="bookmark_added">&bookmark_added;</string>
<string name="bookmark_removed">&bookmark_removed;</string>
<string name="history_today_section">&history_today_section;</string>
<string name="history_yesterday_section">&history_yesterday_section;</string>