Bug 783312 - Show the awesomescreen as a way to select sites for pinning. r=mfinkle
authorWes Johnston <wjohnston@mozilla.com>
Fri, 28 Dec 2012 13:46:08 -0800
changeset 126292 c2c914a1870b088c1a956cfabde9755e3e2de59d
parent 126291 e74b5611f5d11148128fae4e7df9efe2114d35c9
child 126293 a9c2f983a4ead793572db5d359199bb5d184a4e4
push id2151
push userlsblakk@mozilla.com
push dateTue, 19 Feb 2013 18:06:57 +0000
treeherdermozilla-beta@4952e88741ec [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmfinkle
bugs783312
milestone20.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 783312 - Show the awesomescreen as a way to select sites for pinning. r=mfinkle
mobile/android/base/AboutHomeContent.java
mobile/android/base/ActivityHandlerHelper.java
mobile/android/base/AwesomeBar.java
mobile/android/base/AwesomeBarTabs.java
mobile/android/base/awesomebar/AllPagesTab.java
mobile/android/base/awesomebar/BookmarksTab.java
mobile/android/base/awesomebar/HistoryTab.java
mobile/android/base/db/BrowserDB.java
--- a/mobile/android/base/AboutHomeContent.java
+++ b/mobile/android/base/AboutHomeContent.java
@@ -2,20 +2,23 @@
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko;
 
 import org.mozilla.gecko.db.BrowserContract.Thumbnails;
 import org.mozilla.gecko.db.BrowserContract.Bookmarks;
+import org.mozilla.gecko.db.BrowserContract.Combined;
 import org.mozilla.gecko.db.BrowserDB;
 import org.mozilla.gecko.db.BrowserDB.URLColumns;
+import org.mozilla.gecko.db.BrowserDB.PinnedSite;
 import org.mozilla.gecko.sync.setup.SyncAccounts;
 import org.mozilla.gecko.sync.setup.activities.SetupSyncActivity;
+import org.mozilla.gecko.util.ActivityResultHandler;
 import org.mozilla.gecko.util.GeckoAsyncTask;
 
 import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
 
 import android.accounts.Account;
 import android.accounts.AccountManager;
@@ -157,16 +160,22 @@ public class AboutHomeContent extends Sc
         mInflater.inflate(R.layout.abouthome_content, this);
 
         mTopSitesGrid = (TopSitesGridView)findViewById(R.id.top_sites_grid);
         mTopSitesGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
             public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                 TopSitesViewHolder holder = (TopSitesViewHolder) v.getTag();
                 String spec = holder.url;
 
+                // If we don't have a url, this must be an empty row. Show the edit dialog box
+                if (TextUtils.isEmpty(spec)) {
+                    editSite(spec, position);
+                    return;
+                }
+
                 if (mUriLoadCallback != null)
                     mUriLoadCallback.callback(spec);
             }
         });
 
         mPromoBox = (AboutHomePromoBox) findViewById(R.id.promo_box);
         mAddons = (AboutHomeSection) findViewById(R.id.recommended_addons);
         mLastTabs = (AboutHomeSection) findViewById(R.id.last_tabs);
@@ -834,9 +843,43 @@ public class AboutHomeContent extends Sc
             c.moveToPosition(position);
             if (!c.isAfterLast()) {
                 url = c.getString(c.getColumnIndex(URLColumns.URL));
                 title = c.getString(c.getColumnIndex(URLColumns.TITLE));
             }
             return buildView(url, title, convertView);
         }
     }
+
+    // Edit the site at position. Provide a url to start editing with
+    public void editSite(String url, final int position) {
+        Intent intent = new Intent(mContext, AwesomeBar.class);
+        intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
+        intent.putExtra(AwesomeBar.TARGET_KEY, AwesomeBar.Target.PICK_SITE.toString());
+        if (url != null && !TextUtils.isEmpty(url)) {
+            intent.putExtra(AwesomeBar.CURRENT_URL_KEY, url);
+        }
+
+        int requestCode = GeckoAppShell.sActivityHelper.makeRequestCode(new ActivityResultHandler() {
+            public void onActivityResult(int resultCode, Intent data) {
+                final String title = data.getStringExtra(AwesomeBar.TITLE_KEY);
+                final String url = data.getStringExtra(AwesomeBar.URL_KEY);
+
+                // update the database on a background thread
+                (new GeckoAsyncTask<Void, Void, Void>(GeckoApp.mAppContext, GeckoAppShell.getHandler()) {
+                    @Override
+                    public Void doInBackground(Void... params) {
+                        final ContentResolver resolver = mActivity.getContentResolver();
+                        BrowserDB.pinSite(resolver, url, (title == null ? url : title), position);
+                        return null;
+                    }
+        
+                    @Override
+                    public void onPostExecute(Void v) {
+                        update(EnumSet.of(UpdateFlags.TOP_SITES));
+                    }
+                }).execute();
+            }
+        });
+
+        mActivity.startActivityForResult(intent, requestCode);
+    }
 }
--- a/mobile/android/base/ActivityHandlerHelper.java
+++ b/mobile/android/base/ActivityHandlerHelper.java
@@ -46,16 +46,20 @@ class ActivityHandlerHelper {
         mCameraImageResultHandler = new CameraImageResultHandler(mFilePickerResult);
         mCameraVideoResultHandler = new CameraVideoResultHandler(mFilePickerResult);
     }
 
     int makeRequestCodeForAwesomebar() {
         return mActivityResultHandlerMap.put(mAwesomebarResultHandler);
     }
 
+    int makeRequestCode(ActivityResultHandler aHandler) {
+        return mActivityResultHandlerMap.put(aHandler);
+    }
+
     private int addIntentActivitiesToList(Context context, Intent intent, ArrayList<PromptService.PromptListItem> items, ArrayList<Intent> aIntents) {
         PackageManager pm = context.getPackageManager();
         List<ResolveInfo> lri = pm.queryIntentActivityOptions(GeckoApp.mAppContext.getComponentName(), null, intent, 0);
 
         if (lri == null) {
             return 0;
         }
 
--- a/mobile/android/base/AwesomeBar.java
+++ b/mobile/android/base/AwesomeBar.java
@@ -55,19 +55,20 @@ public class AwesomeBar extends GeckoAct
                                                                  InputMethods.METHOD_SWYPE,
                                                                  InputMethods.METHOD_SWYPE_BETA,
                                                                  });
 
     static final String URL_KEY = "url";
     static final String CURRENT_URL_KEY = "currenturl";
     static final String TARGET_KEY = "target";
     static final String SEARCH_KEY = "search";
+    static final String TITLE_KEY = "title";
     static final String USER_ENTERED_KEY = "user_entered";
     static final String READING_LIST_KEY = "reading_list";
-    public static enum Target { NEW_TAB, CURRENT_TAB };
+    public static enum Target { NEW_TAB, CURRENT_TAB, PICK_SITE };
 
     private String mTarget;
     private AwesomeBarTabs mAwesomeTabs;
     private CustomEditText mText;
     private ImageButton mGoButton;
     private ContentResolver mResolver;
     private ContextMenuSubject mContextMenuSubject;
     private boolean mIsUsingSwype;
@@ -86,18 +87,18 @@ public class AwesomeBar extends GeckoAct
         mGoButton = (ImageButton) findViewById(R.id.awesomebar_button);
         mText = (CustomEditText) findViewById(R.id.awesomebar_text);
 
         TabWidget tabWidget = (TabWidget) findViewById(android.R.id.tabs);
         tabWidget.setDividerDrawable(null);
 
         mAwesomeTabs = (AwesomeBarTabs) findViewById(R.id.awesomebar_tabs);
         mAwesomeTabs.setOnUrlOpenListener(new AwesomeBarTabs.OnUrlOpenListener() {
-            public void onUrlOpen(String url) {
-                openUrlAndFinish(url);
+            public void onUrlOpen(String url, String title) {
+                openUrlAndFinish(url, title, false);
             }
 
             public void onSearch(String engine, String text) {
                 openSearchAndFinish(text, engine);
             }
 
             public void onEditSuggestion(final String text) {
                 GeckoApp.mAppContext.mMainHandler.post(new Runnable() {
@@ -342,18 +343,26 @@ public class AwesomeBar extends GeckoAct
 
     private void finishWithResult(Intent intent) {
         setResult(Activity.RESULT_OK, intent);
         finish();
         overridePendingTransition(R.anim.awesomebar_hold_still, R.anim.awesomebar_fade_out);
     }
 
     private void openUrlAndFinish(String url) {
+        openUrlAndFinish(url, null, false);
+    }
+
+    private void openUrlAndFinish(String url, String title, boolean userEntered) {
         Intent resultIntent = new Intent();
         resultIntent.putExtra(URL_KEY, url);
+        if (title != null && !TextUtils.isEmpty(title))
+            resultIntent.putExtra(TITLE_KEY, title);
+        if (userEntered)
+            resultIntent.putExtra(USER_ENTERED_KEY, userEntered);
         resultIntent.putExtra(TARGET_KEY, mTarget);
         finishWithResult(resultIntent);
     }
 
     private void openUserEnteredAndFinish(String url) {
         int index = url.indexOf(' ');
         String keywordUrl = null;
         String keywordSearch = null;
@@ -366,21 +375,17 @@ public class AwesomeBar extends GeckoAct
             keywordSearch = url.substring(index + 1);
         }
 
         if (keywordUrl != null) {
             String search = URLEncoder.encode(keywordSearch);
             url = keywordUrl.replace("%s", search);
         }
 
-        Intent resultIntent = new Intent();
-        resultIntent.putExtra(URL_KEY, url);
-        resultIntent.putExtra(TARGET_KEY, mTarget);
-        resultIntent.putExtra(USER_ENTERED_KEY, true);
-        finishWithResult(resultIntent);
+        openUrlAndFinish(url, "", true);
     }
 
     private void openSearchAndFinish(String url, String engine) {
         Intent resultIntent = new Intent();
         resultIntent.putExtra(URL_KEY, url);
         resultIntent.putExtra(TARGET_KEY, mTarget);
         resultIntent.putExtra(SEARCH_KEY, engine);
         finishWithResult(resultIntent);
--- a/mobile/android/base/AwesomeBarTabs.java
+++ b/mobile/android/base/AwesomeBarTabs.java
@@ -39,17 +39,17 @@ public class AwesomeBarTabs extends TabH
     
     private AwesomeBarTab mTabs[];
 
     // FIXME: This value should probably come from a
     // prefs key (just like XUL-based fennec)
     private static final int MAX_RESULTS = 100;
 
     public interface OnUrlOpenListener {
-        public void onUrlOpen(String url);
+        public void onUrlOpen(String url, String title);
         public void onSearch(String engine, String text);
         public void onEditSuggestion(String suggestion);
     }
 
     private class AwesomePagerAdapter extends PagerAdapter {
         public AwesomePagerAdapter() {
             super();
         }
--- a/mobile/android/base/awesomebar/AllPagesTab.java
+++ b/mobile/android/base/awesomebar/AllPagesTab.java
@@ -244,17 +244,18 @@ public class AllPagesTab extends Awesome
         }
 
         public void onClick() {
             AwesomeBarTabs.OnUrlOpenListener listener = getUrlListener();
             if (listener == null)
                 return;
 
             String url = mCursor.getString(mCursor.getColumnIndexOrThrow(URLColumns.URL));
-            listener.onUrlOpen(url);
+            String title = mCursor.getString(mCursor.getColumnIndexOrThrow(URLColumns.TITLE));
+            listener.onUrlOpen(url, title);
         }
 
         public ContextMenuSubject getSubject() {
             // Use the history id in order to allow removing history entries
             int id = mCursor.getInt(mCursor.getColumnIndexOrThrow(Combined.HISTORY_ID));
 
             String keyword = null;
             int keywordCol = mCursor.getColumnIndex(URLColumns.KEYWORD);
@@ -455,17 +456,17 @@ public class AllPagesTab extends Awesome
                     if (listener != null) {
                         String suggestion = ((TextView) v.findViewById(R.id.suggestion_text)).getText().toString();
 
                         // If we're not clicking the user-entered view (the
                         // first suggestion item) and the search matches a URL
                         // pattern, go to that URL. Otherwise, do a search for
                         // the term.
                         if (v != viewHolder.userEnteredView && !StringUtils.isSearchQuery(suggestion)) {
-                            listener.onUrlOpen(suggestion);
+                            listener.onUrlOpen(suggestion, null);
                         } else {
                             listener.onSearch(engine.name, suggestion);
                         }
                     }
                 }
             };
 
             // when a suggestion is long-clicked, copy the suggestion into the URL EditText
--- a/mobile/android/base/awesomebar/BookmarksTab.java
+++ b/mobile/android/base/awesomebar/BookmarksTab.java
@@ -187,21 +187,22 @@ public class BookmarksTab extends Awesom
 
         // Otherwise, just open the URL
         AwesomeBarTabs.OnUrlOpenListener listener = getUrlListener();
         if (listener == null) {
             return;
         }
 
         String url = cursor.getString(cursor.getColumnIndexOrThrow(URLColumns.URL));
+        String title = cursor.getString(cursor.getColumnIndexOrThrow(URLColumns.TITLE));
         long parentId = cursor.getLong(cursor.getColumnIndexOrThrow(Bookmarks.PARENT));
         if (parentId == Bookmarks.FIXED_READING_LIST_ID) {
             url = ReaderModeUtils.getAboutReaderForUrl(url, true);
         }
-        listener.onUrlOpen(url);
+        listener.onUrlOpen(url, title);
     }
 
     private class BookmarksListAdapter extends SimpleCursorAdapter {
         private static final int VIEW_TYPE_ITEM = 0;
         private static final int VIEW_TYPE_FOLDER = 1;
         private static final int VIEW_TYPE_COUNT = 2;
 
         private LinkedList<Pair<Integer, String>> mParentStack;
--- a/mobile/android/base/awesomebar/HistoryTab.java
+++ b/mobile/android/base/awesomebar/HistoryTab.java
@@ -372,19 +372,20 @@ public class HistoryTab extends AwesomeB
         HistoryListAdapter adapter = getCursorAdapter();
         if (adapter == null)
             return false;
 
         @SuppressWarnings("unchecked")
         Map<String,Object> historyItem = (Map<String,Object>) adapter.getChild(groupPosition, childPosition);
 
         String url = (String) historyItem.get(URLColumns.URL);
+        String title = (String) historyItem.get(URLColumns.TITLE);
         AwesomeBarTabs.OnUrlOpenListener listener = getUrlListener();
         if (!TextUtils.isEmpty(url) && listener != null)
-            listener.onUrlOpen(url);
+            listener.onUrlOpen(url, title);
 
         return true;
     }
 
     public ContextMenuSubject getSubject(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
         ContextMenuSubject subject = null;
 
         if (!(menuInfo instanceof ExpandableListView.ExpandableListContextMenuInfo)) {
--- a/mobile/android/base/db/BrowserDB.java
+++ b/mobile/android/base/db/BrowserDB.java
@@ -272,17 +272,17 @@ public class BrowserDB {
     public static void unpinAllSites(ContentResolver cr) {
         sDb.unpinAllSites(cr);
     }
 
     public static Cursor getPinnedSites(ContentResolver cr, int limit) {
         return sDb.getPinnedSites(cr, limit);
     }
 
-    private static class PinnedSite {
+    public static class PinnedSite {
         public String title = "";
         public String url = "";
 
         public PinnedSite(String aTitle, String aUrl) {
             title = aTitle;
             url = aUrl;
         }
     }