Bug 1395841 - Part 2: Support creating a shortcut from page option in menu. r=nechen
authorJing-wei Wu <topwu.tw@gmail.com>
Thu, 28 Sep 2017 10:57:48 +0800
changeset 383451 38571a57eb1161597abd001704684b79e7715479
parent 383450 13e001aa0253aa789eac679b4dc3137d4c871b83
child 383452 4785e88cd39ac18713f112d00ec7d43246934aa8
push id32594
push userkwierso@gmail.com
push dateThu, 28 Sep 2017 22:49:33 +0000
treeherdermozilla-central@6dea0ee45b66 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnechen
bugs1395841
milestone58.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 1395841 - Part 2: Support creating a shortcut from page option in menu. r=nechen When page option 'add page shortcut' is clicked, creating a shortcut(not PWA) on launcher. Also make sure that heavy tasks are executed in background thread. MozReview-Commit-ID: 8KtwdXENtEd
mobile/android/base/java/org/mozilla/gecko/BrowserApp.java
mobile/android/base/java/org/mozilla/gecko/GeckoApplication.java
mobile/android/base/java/org/mozilla/gecko/activitystream/homepanel/menu/ActivityStreamContextMenu.java
mobile/android/base/java/org/mozilla/gecko/delegates/BookmarkStateChangeDelegate.java
mobile/android/base/java/org/mozilla/gecko/home/HomeFragment.java
mobile/android/base/java/org/mozilla/gecko/promotion/HomeScreenPrompt.java
--- a/mobile/android/base/java/org/mozilla/gecko/BrowserApp.java
+++ b/mobile/android/base/java/org/mozilla/gecko/BrowserApp.java
@@ -1501,17 +1501,17 @@ public class BrowserApp extends GeckoApp
             final String title = tab.getDisplayTitle();
             if (url == null || title == null) {
                 return true;
             }
 
             ThreadUtils.postToBackgroundThread(new Runnable() {
                 @Override
                 public void run() {
-                    GeckoApplication.createShortcut(title, url);
+                    GeckoApplication.createBrowserShortcut(title, url);
                 }
             });
 
             Telemetry.sendUIEvent(TelemetryContract.Event.ACTION, TelemetryContract.Method.CONTEXT_MENU,
                 getResources().getResourceEntryName(itemId));
             return true;
         }
 
--- a/mobile/android/base/java/org/mozilla/gecko/GeckoApplication.java
+++ b/mobile/android/base/java/org/mozilla/gecko/GeckoApplication.java
@@ -47,16 +47,17 @@ import org.mozilla.gecko.preferences.Dis
 import org.mozilla.gecko.util.ActivityUtils;
 import org.mozilla.gecko.telemetry.TelemetryBackgroundReceiver;
 import org.mozilla.gecko.util.BundleEventListener;
 import org.mozilla.gecko.util.EventCallback;
 import org.mozilla.gecko.util.GeckoBundle;
 import org.mozilla.gecko.util.HardwareUtils;
 import org.mozilla.gecko.util.PRNGFixes;
 import org.mozilla.gecko.util.ThreadUtils;
+import org.mozilla.gecko.util.UIAsyncTask;
 
 import java.io.File;
 import java.lang.reflect.Method;
 import java.util.UUID;
 
 public class GeckoApplication extends Application
                               implements HapticFeedbackDelegate {
     private static final String LOG_TAG = "GeckoApplication";
@@ -565,22 +566,31 @@ public class GeckoApplication extends Ap
 
         final Context context = GeckoAppShell.getApplicationContext();
         // Do not allow duplicate items.
         intent.putExtra("duplicate", false);
 
         intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
         context.sendBroadcast(intent);
 
-        // Remember interaction
-        final UrlAnnotations urlAnnotations = BrowserDB.from(context).getUrlAnnotations();
-        urlAnnotations.insertHomeScreenShortcut(context.getContentResolver(), aURI, true);
+        (new UIAsyncTask.WithoutParams<Void>(ThreadUtils.getBackgroundHandler()) {
+            @Override
+            public Void doInBackground() {
+                // Remember interaction, post to background thread to prevent violating StrictMode policy.
+                final UrlAnnotations urlAnnotations = BrowserDB.from(context).getUrlAnnotations();
+                urlAnnotations.insertHomeScreenShortcut(context.getContentResolver(), aURI, true);
+                return null;
+            }
 
-        // After shortcut is created, show the mobile desktop.
-        ActivityUtils.goToHomeScreen(context);
+            @Override
+            public void onPostExecute(Void aVoid) {
+                // After shortcut is created and db entry is inserted, show the mobile desktop.
+                ActivityUtils.goToHomeScreen(context);
+            }
+        }).execute();
     }
 
     private static Bitmap getLauncherIcon(Bitmap aSource, int size) {
         final float[] DEFAULT_LAUNCHER_ICON_HSV = { 32.0f, 1.0f, 1.0f };
         final int kOffset = 6;
         final int kRadius = 5;
 
         final int insetSize = aSource != null ? size * 2 / 3 : size;
--- a/mobile/android/base/java/org/mozilla/gecko/activitystream/homepanel/menu/ActivityStreamContextMenu.java
+++ b/mobile/android/base/java/org/mozilla/gecko/activitystream/homepanel/menu/ActivityStreamContextMenu.java
@@ -279,17 +279,22 @@ public abstract class ActivityStreamCont
                 });
                 break;
 
             case R.id.copy_url:
                 Clipboard.setText(item.getUrl());
                 break;
 
             case R.id.add_homescreen:
-                GeckoApplication.createShortcut(item.getTitle(), item.getUrl());
+                ThreadUtils.postToBackgroundThread(new Runnable() {
+                    @Override
+                    public void run() {
+                        GeckoApplication.createBrowserShortcut(item.getTitle(), item.getUrl());
+                    }
+                });
                 break;
 
             case R.id.open_new_tab:
                 onUrlOpenInBackgroundListener.onUrlOpenInBackgroundWithReferrer(item.getUrl(), referrerUri,
                         EnumSet.noneOf(HomePager.OnUrlOpenInBackgroundListener.Flags.class));
                 break;
 
             case R.id.open_new_private_tab:
--- a/mobile/android/base/java/org/mozilla/gecko/delegates/BookmarkStateChangeDelegate.java
+++ b/mobile/android/base/java/org/mozilla/gecko/delegates/BookmarkStateChangeDelegate.java
@@ -180,17 +180,17 @@ public class BookmarkStateChangeDelegate
 
                     final String url = tab.getURL();
                     final String title = tab.getDisplayTitle();
 
                     if (url != null && title != null) {
                         ThreadUtils.postToBackgroundThread(new Runnable() {
                             @Override
                             public void run() {
-                                GeckoApplication.createShortcut(title, url);
+                                GeckoApplication.createBrowserShortcut(title, url);
                             }
                         });
                     }
                 }
             }
         });
 
         if (AppConstants.Versions.feature26Plus) {
--- a/mobile/android/base/java/org/mozilla/gecko/home/HomeFragment.java
+++ b/mobile/android/base/java/org/mozilla/gecko/home/HomeFragment.java
@@ -307,18 +307,17 @@ public abstract class HomeFragment exten
                 return false;
             }
 
             // Fetch an icon big enough for use as a home screen icon.
             final String displayTitle = info.getDisplayTitle();
             ThreadUtils.postToBackgroundThread(new Runnable() {
                 @Override
                 public void run() {
-                    GeckoApplication.createShortcut(displayTitle, info.url);
-
+                    GeckoApplication.createBrowserShortcut(displayTitle, info.url);
                 }
             });
 
             Telemetry.sendUIEvent(TelemetryContract.Event.ACTION, TelemetryContract.Method.CONTEXT_MENU, "home_add_to_launcher");
             return true;
         }
 
         if (itemId == R.id.home_open_private_tab || itemId == R.id.home_open_new_tab) {
--- a/mobile/android/base/java/org/mozilla/gecko/promotion/HomeScreenPrompt.java
+++ b/mobile/android/base/java/org/mozilla/gecko/promotion/HomeScreenPrompt.java
@@ -115,29 +115,27 @@ public class HomeScreenPrompt extends Lo
             }
         });
     }
 
     private void addToHomeScreen() {
         ThreadUtils.postToBackgroundThread(new Runnable() {
             @Override
             public void run() {
-                GeckoApplication.createShortcut(title, url);
+                GeckoApplication.createBrowserShortcut(title, url);
 
                 Telemetry.sendUIEvent(TelemetryContract.Event.ACTION, TelemetryContract.Method.BUTTON, TELEMETRY_EXTRA);
 
                 ActivityUtils.goToHomeScreen(HomeScreenPrompt.this);
 
                 finish();
             }
         });
     }
 
-
-
     private void loadShortcutIcon() {
         Icons.with(this)
                 .pageUrl(url)
                 .skipNetwork()
                 .skipMemory()
                 .forLauncherIcon()
                 .build()
                 .execute(this);