Bug 965453 - Add submenu for bookmarks. r=wesj

This commit is contained in:
Brian Nicholson 2014-02-20 21:27:04 -08:00
parent e15c9d8385
commit 9376f5dfbb
10 changed files with 191 additions and 112 deletions

View File

@ -643,42 +643,6 @@ abstract public class BrowserApp extends GeckoApp
registerEventListener("Prompt:ShowTop");
}
private void showBookmarkDialog() {
final Tab tab = Tabs.getInstance().getSelectedTab();
final Prompt ps = new Prompt(this, new Prompt.PromptCallback() {
@Override
public void onPromptFinished(String result) {
int itemId = -1;
try {
itemId = new JSONObject(result).getInt("button");
} catch(JSONException ex) {
Log.e(LOGTAG, "Exception reading bookmark prompt result", ex);
}
if (tab == null)
return;
if (itemId == 0) {
new EditBookmarkDialog(BrowserApp.this).show(tab.getURL());
} else if (itemId == 1) {
String url = tab.getURL();
String title = tab.getDisplayTitle();
Bitmap favicon = tab.getFavicon();
if (url != null && title != null) {
GeckoAppShell.createShortcut(title, url, url, favicon, "");
}
}
}
});
final Prompt.PromptListItem[] items = new Prompt.PromptListItem[2];
Resources res = getResources();
items[0] = new Prompt.PromptListItem(res.getString(R.string.contextmenu_edit_bookmark));
items[1] = new Prompt.PromptListItem(res.getString(R.string.contextmenu_add_to_launcher));
ps.show("", "", items, false);
}
private void setDynamicToolbarEnabled(boolean enabled) {
if (enabled) {
if (mLayerView != null) {
@ -745,16 +709,7 @@ abstract public class BrowserApp extends GeckoApp
}
if (itemId == R.id.subscribe) {
Tab tab = Tabs.getInstance().getSelectedTab();
if (tab != null && tab.hasFeeds()) {
JSONObject args = new JSONObject();
try {
args.put("tabId", tab.getId());
} catch (JSONException e) {
Log.e(LOGTAG, "error building json arguments");
}
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Feeds:Subscribe", args.toString()));
}
subscribeToFeeds(Tabs.getInstance().getSelectedTab());
return true;
}
@ -784,27 +739,6 @@ abstract public class BrowserApp extends GeckoApp
return true;
}
if (itemId == R.id.add_to_launcher) {
Tab tab = Tabs.getInstance().getSelectedTab();
if (tab == null) {
return true;
}
final String url = tab.getURL();
final String title = tab.getDisplayTitle();
if (url == null || title == null) {
return true;
}
final OnFaviconLoadedListener listener = new GeckoAppShell.CreateShortcutFaviconLoadedListener(url, title);
Favicons.getSizedFavicon(url,
tab.getFaviconURL(),
Integer.MAX_VALUE,
LoadFaviconTask.FLAG_PERSIST,
listener);
return true;
}
return false;
}
@ -2155,6 +2089,9 @@ abstract public class BrowserApp extends GeckoApp
MenuItem desktopMode = aMenu.findItem(R.id.desktop_mode);
MenuItem enterGuestMode = aMenu.findItem(R.id.new_guest_session);
MenuItem exitGuestMode = aMenu.findItem(R.id.exit_guest_session);
MenuItem subscribe = aMenu.findItem(R.id.subscribe);
MenuItem addToReadingList = aMenu.findItem(R.id.reading_list_add);
MenuItem save = aMenu.findItem(R.id.save);
// Only show the "Quit" menu item on pre-ICS or television devices.
// In ICS+, it's easy to kill an app through the task switcher.
@ -2179,11 +2116,10 @@ abstract public class BrowserApp extends GeckoApp
return true;
}
save.setVisible(!GeckoProfile.get(this).inGuestMode());
bookmark.setEnabled(!AboutPages.isAboutReader(tab.getURL()));
bookmark.setVisible(!GeckoProfile.get(this).inGuestMode());
bookmark.setCheckable(true);
bookmark.setChecked(tab.isBookmark());
bookmark.setIcon(tab.isBookmark() ? R.drawable.ic_menu_bookmark_remove : R.drawable.ic_menu_bookmark_add);
back.setEnabled(tab.canDoBack());
forward.setEnabled(tab.canDoForward());
@ -2273,18 +2209,22 @@ abstract public class BrowserApp extends GeckoApp
else
enterGuestMode.setVisible(true);
addToReadingList.setChecked(tab.isReadingListItem());
addToReadingList.setEnabled(tab.getReaderEnabled());
subscribe.setEnabled(tab.hasFeeds());
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Tab tab = null;
final Tab tab = Tabs.getInstance().getSelectedTab();
Intent intent = null;
final int itemId = item.getItemId();
if (itemId == R.id.bookmark) {
tab = Tabs.getInstance().getSelectedTab();
if (tab != null) {
if (item.isChecked()) {
tab.removeBookmark();
@ -2294,12 +2234,12 @@ abstract public class BrowserApp extends GeckoApp
tab.addBookmark();
getButtonToast().show(false,
getResources().getString(R.string.bookmark_added),
getResources().getString(R.string.bookmark_options),
getResources().getString(R.string.contextmenu_edit_bookmark),
null,
new ButtonToast.ToastListener() {
@Override
public void onButtonClicked() {
showBookmarkDialog();
new EditBookmarkDialog(BrowserApp.this).show(tab.getURL());
}
@Override
@ -2317,21 +2257,18 @@ abstract public class BrowserApp extends GeckoApp
}
if (itemId == R.id.reload) {
tab = Tabs.getInstance().getSelectedTab();
if (tab != null)
tab.doReload();
return true;
}
if (itemId == R.id.back) {
tab = Tabs.getInstance().getSelectedTab();
if (tab != null)
tab.doBack();
return true;
}
if (itemId == R.id.forward) {
tab = Tabs.getInstance().getSelectedTab();
if (tab != null)
tab.doForward();
return true;
@ -2374,13 +2311,12 @@ abstract public class BrowserApp extends GeckoApp
}
if (itemId == R.id.desktop_mode) {
Tab selectedTab = Tabs.getInstance().getSelectedTab();
if (selectedTab == null)
if (tab == null)
return true;
JSONObject args = new JSONObject();
try {
args.put("desktopMode", !item.isChecked());
args.put("tabId", selectedTab.getId());
args.put("tabId", tab.getId());
} catch (JSONException e) {
Log.e(LOGTAG, "error building json arguments");
}
@ -2415,6 +2351,25 @@ abstract public class BrowserApp extends GeckoApp
return true;
}
if (itemId == R.id.launcher_add) {
addToLauncher(tab.getURL(), tab.getTitle(), tab.getFaviconURL());
return true;
}
if (itemId == R.id.reading_list_add) {
if (item.isChecked()) {
ReaderModeUtils.removeFromReadingList(tab.getURL());
} else {
ReaderModeUtils.addToReadingList(tab);
}
return true;
}
if (itemId == R.id.subscribe) {
subscribeToFeeds(tab);
return true;
}
return super.onOptionsItemSelected(item);
}
@ -2459,6 +2414,33 @@ abstract public class BrowserApp extends GeckoApp
ps.show(res.getString(titleString), res.getString(msgString), null, false);
}
public void subscribeToFeeds(Tab tab) {
if (!tab.hasFeeds()) {
return;
}
JSONObject args = new JSONObject();
try {
args.put("tabId", tab.getId());
} catch (JSONException e) {
Log.e(LOGTAG, "JSON error", e);
}
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Feeds:Subscribe", args.toString()));
}
private void addToLauncher(String url, String title, String faviconUrl) {
if (url == null || title == null) {
return;
}
final OnFaviconLoadedListener listener = new GeckoAppShell.CreateShortcutFaviconLoadedListener(url, title);
Favicons.getSizedFavicon(url,
faviconUrl,
Integer.MAX_VALUE,
LoadFaviconTask.FLAG_PERSIST,
listener);
}
/**
* This will detect if the key pressed is back. If so, will show the history.
*/

View File

@ -4,9 +4,12 @@
package org.mozilla.gecko;
import org.json.JSONException;
import org.json.JSONObject;
import org.mozilla.gecko.util.StringUtils;
import android.net.Uri;
import android.util.Log;
public class ReaderModeUtils {
private static final String LOGTAG = "ReaderModeUtils";
@ -45,4 +48,30 @@ public class ReaderModeUtils {
return aboutReaderUrl;
}
public static void addToReadingList(Tab tab) {
if (!tab.getReaderEnabled()) {
return;
}
JSONObject json = new JSONObject();
try {
json.put("tabID", String.valueOf(tab.getId()));
} catch (JSONException e) {
Log.e(LOGTAG, "JSON error - failing to add to reading list", e);
return;
}
GeckoEvent e = GeckoEvent.createBroadcastEvent("Reader:Add", json.toString());
GeckoAppShell.sendEventToGecko(e);
}
public static void removeFromReadingList(String url) {
if (url == null) {
return;
}
GeckoEvent e = GeckoEvent.createBroadcastEvent("Reader:Remove", url);
GeckoAppShell.sendEventToGecko(e);
}
}

View File

@ -8,6 +8,9 @@ package org.mozilla.gecko;
import org.mozilla.gecko.SiteIdentity.SecurityMode;
import org.mozilla.gecko.db.BrowserContract.Bookmarks;
import org.mozilla.gecko.db.BrowserDB;
import org.mozilla.gecko.favicons.Favicons;
import org.mozilla.gecko.favicons.LoadFaviconTask;
import org.mozilla.gecko.favicons.OnFaviconLoadedListener;
import org.mozilla.gecko.gfx.Layer;
import org.mozilla.gecko.util.ThreadUtils;
@ -453,22 +456,6 @@ public class Tab {
});
}
public void addToReadingList() {
if (!mReaderEnabled)
return;
JSONObject json = new JSONObject();
try {
json.put("tabID", String.valueOf(getId()));
} catch (JSONException e) {
Log.e(LOGTAG, "JSON error - failing to add to reading list", e);
return;
}
GeckoEvent e = GeckoEvent.createBroadcastEvent("Reader:Add", json.toString());
GeckoAppShell.sendEventToGecko(e);
}
public void toggleReaderMode() {
if (AboutPages.isAboutReader(mUrl)) {
Tabs.getInstance().loadUrl(ReaderModeUtils.getUrlFromAboutReader(mUrl));

View File

@ -54,6 +54,7 @@
<!ENTITY back "Back">
<!ENTITY stop "Stop">
<!ENTITY site_security "Site Security">
<!ENTITY save "Save">
<!ENTITY close_tab "Close Tab">
<!ENTITY one_tab "1 tab">
@ -276,6 +277,7 @@ size. -->
<!ENTITY site_settings_clear "Clear">
<!ENTITY site_settings_no_settings "There are no settings to clear.">
<!ENTITY reading_list_add "Add to Reading List">
<!ENTITY reading_list_added "Page added to your Reading List">
<!ENTITY reading_list_removed "Page removed from your Reading List">
<!ENTITY reading_list_failed "Failed to add page to your Reading List">

View File

@ -20,10 +20,30 @@
android:title="@string/forward"
android:visible="false"/>
<item android:id="@+id/bookmark"
<item android:id="@+id/save"
android:icon="@drawable/ic_menu_bookmark_add"
android:title="@string/save"
android:showAsAction="ifRoom">
<menu>
<item android:id="@+id/bookmark"
android:title="@string/bookmark"
android:showAsAction="ifRoom"/>
android:checkable="true" />
<item android:id="@+id/reading_list_add"
android:title="@string/reading_list_add"
android:checkable="true" />
<item android:id="@+id/launcher_add"
android:title="@string/contextmenu_add_to_launcher" />
<item android:id="@+id/subscribe"
android:title="@string/contextmenu_subscribe" />
</menu>
</item>
<item android:id="@+id/share"
android:icon="@drawable/ic_menu_share"

View File

@ -20,10 +20,30 @@
android:title="@string/reload"
android:showAsAction="always"/>
<item android:id="@+id/bookmark"
<item android:id="@+id/save"
android:icon="@drawable/ic_menu_bookmark_add"
android:title="@string/save"
android:showAsAction="ifRoom">
<menu>
<item android:id="@+id/bookmark"
android:title="@string/bookmark"
android:showAsAction="ifRoom"/>
android:checkable="true" />
<item android:id="@+id/reading_list_add"
android:title="@string/reading_list_add"
android:checkable="true" />
<item android:id="@+id/launcher_add"
android:title="@string/contextmenu_add_to_launcher" />
<item android:id="@+id/subscribe"
android:title="@string/contextmenu_subscribe" />
</menu>
</item>
<item android:id="@+id/share"
android:icon="@drawable/ic_menu_share"

View File

@ -20,10 +20,30 @@
android:title="@string/forward"
android:visible="false"/>
<item android:id="@+id/bookmark"
<item android:id="@+id/save"
android:icon="@drawable/ic_menu_bookmark_add"
android:title="@string/save"
android:showAsAction="always">
<menu>
<item android:id="@+id/bookmark"
android:title="@string/bookmark"
android:showAsAction="always"/>
android:checkable="true" />
<item android:id="@+id/reading_list_add"
android:title="@string/reading_list_add"
android:checkable="true" />
<item android:id="@+id/launcher_add"
android:title="@string/contextmenu_add_to_launcher" />
<item android:id="@+id/subscribe"
android:title="@string/contextmenu_subscribe" />
</menu>
</item>
<item android:id="@+id/share"
android:icon="@drawable/ic_menu_share"

View File

@ -18,9 +18,29 @@
android:icon="@drawable/ic_menu_forward"
android:title="@string/forward"/>
<item android:id="@+id/bookmark"
<item android:id="@+id/save"
android:icon="@drawable/ic_menu_bookmark_add"
android:title="@string/bookmark"/>
android:title="@string/save">
<menu>
<item android:id="@+id/bookmark"
android:title="@string/bookmark"
android:checkable="true" />
<item android:id="@+id/reading_list_add"
android:title="@string/reading_list_add"
android:checkable="true" />
<item android:id="@+id/launcher_add"
android:title="@string/contextmenu_add_to_launcher" />
<item android:id="@+id/subscribe"
android:title="@string/contextmenu_subscribe" />
</menu>
</item>
<item android:id="@+id/new_tab"
android:icon="@drawable/ic_menu_new_tab"

View File

@ -223,12 +223,14 @@
<string name="tabs_normal">&tabs_normal;</string>
<string name="tabs_private">&tabs_private;</string>
<string name="tabs_synced">&tabs_synced;</string>
<string name="save">&save;</string>
<string name="site_settings_title">&site_settings_title3;</string>
<string name="site_settings_cancel">&site_settings_cancel;</string>
<string name="site_settings_clear">&site_settings_clear;</string>
<string name="site_settings_no_settings">&site_settings_no_settings;</string>
<string name="reading_list_add">&reading_list_add;</string>
<string name="reading_list_added">&reading_list_added;</string>
<string name="reading_list_removed">&reading_list_removed;</string>
<string name="reading_list_failed">&reading_list_failed;</string>

View File

@ -11,6 +11,7 @@ import org.mozilla.gecko.GeckoAppShell;
import org.mozilla.gecko.GeckoProfile;
import org.mozilla.gecko.LightweightTheme;
import org.mozilla.gecko.R;
import org.mozilla.gecko.ReaderModeUtils;
import org.mozilla.gecko.SiteIdentity.SecurityMode;
import org.mozilla.gecko.Tab;
import org.mozilla.gecko.Tabs;
@ -30,7 +31,6 @@ import org.mozilla.gecko.util.GeckoEventListener;
import org.mozilla.gecko.widget.GeckoImageButton;
import org.mozilla.gecko.widget.GeckoImageView;
import org.mozilla.gecko.widget.GeckoRelativeLayout;
import org.json.JSONObject;
import android.content.Context;
@ -1345,10 +1345,7 @@ public class BrowserToolbar extends GeckoRelativeLayout
tab.toggleReaderMode();
}
} else if (event.equals("Reader:LongClick")) {
Tab tab = Tabs.getInstance().getSelectedTab();
if (tab != null) {
tab.addToReadingList();
}
ReaderModeUtils.addToReadingList(Tabs.getInstance().getSelectedTab());
}
}