Bug 1266477 - Migrate screenshot observer code to class extending BrowserAppDelegate. r=mcomella,ahunt
authorSebastian Kaspari <s.kaspari@gmail.com>
Thu, 21 Apr 2016 16:20:32 +0200
changeset 294743 8a3cf7fdd36f09af2a7f9c2442b1ef4ec6ba975b
parent 294742 f00621d9b0bcb469842505542292afbcc969e53f
child 294744 be988dc7bd2063d7ad0c75e9244fe6aa1a998b84
push id30210
push userkwierso@gmail.com
push dateMon, 25 Apr 2016 22:25:12 +0000
treeherdermozilla-central@79de998e7307 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmcomella, ahunt
bugs1266477
milestone48.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 1266477 - Migrate screenshot observer code to class extending BrowserAppDelegate. r=mcomella,ahunt MozReview-Commit-ID: FoIfEhmd4mN
mobile/android/base/java/org/mozilla/gecko/BrowserApp.java
mobile/android/base/java/org/mozilla/gecko/ScreenshotDelegate.java
mobile/android/base/moz.build
--- a/mobile/android/base/java/org/mozilla/gecko/BrowserApp.java
+++ b/mobile/android/base/java/org/mozilla/gecko/BrowserApp.java
@@ -298,20 +298,20 @@ public class BrowserApp extends GeckoApp
 
     // The animator used to toggle HomePager visibility has a race where if the HomePager is shown
     // (starting the animation), the HomePager is hidden, and the HomePager animation completes,
     // both the web content and the HomePager will be hidden. This flag is used to prevent the
     // race by determining if the web content should be hidden at the animation's end.
     private boolean mHideWebContentOnAnimationEnd;
 
     private final DynamicToolbar mDynamicToolbar = new DynamicToolbar();
-    private final ScreenshotObserver mScreenshotObserver = new ScreenshotObserver();
 
     private final List<BrowserAppDelegate> delegates = Collections.unmodifiableList(Arrays.asList(
-            (BrowserAppDelegate) new AddToHomeScreenPromotion()
+            (BrowserAppDelegate) new AddToHomeScreenPromotion(),
+            (BrowserAppDelegate) new ScreenshotDelegate()
     ));
 
     @NonNull
     private SearchEngineManager searchEngineManager; // Contains reference to Context - DO NOT LEAK!
 
     @Override
     public View onCreateView(final String name, final Context context, final AttributeSet attrs) {
         final View view;
@@ -802,40 +802,16 @@ public class BrowserApp extends GeckoApp
 
         mDynamicToolbar.setEnabledChangedListener(new DynamicToolbar.OnEnabledChangedListener() {
             @Override
             public void onEnabledChanged(boolean enabled) {
                 setDynamicToolbarEnabled(enabled);
             }
         });
 
-        // Watch for screenshots while browser is in foreground.
-        mScreenshotObserver.setListener(getContext(), new ScreenshotObserver.OnScreenshotListener() {
-            @Override
-            public void onScreenshotTaken(final String screenshotPath, final String title) {
-                // Treat screenshots as a sharing method.
-                Telemetry.sendUIEvent(TelemetryContract.Event.SHARE, TelemetryContract.Method.BUTTON, "screenshot");
-
-                if (!AppConstants.SCREENSHOTS_IN_BOOKMARKS_ENABLED) {
-                    return;
-                }
-
-                final Tab selectedTab = Tabs.getInstance().getSelectedTab();
-                if (selectedTab == null) {
-                    Log.w(LOGTAG, "Selected tab is null: could not page info to store screenshot.");
-                    return;
-                }
-
-                getProfile().getDB().getUrlAnnotations().insertScreenshot(
-                        getContentResolver(), selectedTab.getURL(), screenshotPath);
-                SnackbarHelper.showSnackbar(BrowserApp.this,
-                        getResources().getString(R.string.screenshot_added_to_bookmarks), Snackbar.LENGTH_SHORT);
-            }
-        });
-
         // Set the maximum bits-per-pixel the favicon system cares about.
         IconDirectoryEntry.setMaxBPP(GeckoAppShell.getScreenDepth());
 
         // The update service is enabled for RELEASE_BUILD, which includes the release and beta channels.
         // However, no updates are served.  Therefore, we don't trust the update service directly, and
         // try to avoid prompting unnecessarily. See Bug 1232798.
         if (!AppConstants.RELEASE_BUILD && UpdateServiceHelper.isUpdaterEnabled()) {
             Permissions.from(this)
@@ -1103,36 +1079,32 @@ public class BrowserApp extends GeckoApp
             return;
         }
 
         EventDispatcher.getInstance().unregisterGeckoThreadListener((GeckoEventListener) this,
             "Prompt:ShowTop");
 
         processTabQueue();
 
-        mScreenshotObserver.start();
-
         for (BrowserAppDelegate delegate : delegates) {
             delegate.onResume(this);
         }
     }
 
     @Override
     public void onPause() {
         super.onPause();
 
         // Needed for Adjust to get accurate session measurements
         AdjustConstants.getAdjustHelper().onPause();
 
         // Register for Prompt:ShowTop so we can foreground this activity even if it's hidden.
         EventDispatcher.getInstance().registerGeckoThreadListener((GeckoEventListener) this,
             "Prompt:ShowTop");
 
-        mScreenshotObserver.stop();
-
         for (BrowserAppDelegate delegate : delegates) {
             delegate.onPause(this);
         }
     }
 
     @Override
     public void onRestart() {
         super.onRestart();
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/java/org/mozilla/gecko/ScreenshotDelegate.java
@@ -0,0 +1,67 @@
+/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
+ * 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 android.app.Activity;
+import android.os.Bundle;
+import android.support.design.widget.Snackbar;
+import android.util.Log;
+
+import java.lang.ref.WeakReference;
+
+/**
+ * Delegate for observing screenshots being taken.
+ */
+public class ScreenshotDelegate extends BrowserAppDelegate implements ScreenshotObserver.OnScreenshotListener {
+    private static final String LOGTAG = "GeckoScreenshotDelegate";
+
+    private WeakReference<Activity> activityReference;
+    private final ScreenshotObserver mScreenshotObserver = new ScreenshotObserver();
+
+    @Override
+    public void onCreate(BrowserApp browserApp, Bundle savedInstanceState) {
+        activityReference = new WeakReference<Activity>(browserApp);
+
+        mScreenshotObserver.setListener(browserApp, this);
+    }
+
+    @Override
+    public void onScreenshotTaken(String screenshotPath, String title) {
+        // Treat screenshots as a sharing method.
+        Telemetry.sendUIEvent(TelemetryContract.Event.SHARE, TelemetryContract.Method.BUTTON, "screenshot");
+
+        if (!AppConstants.SCREENSHOTS_IN_BOOKMARKS_ENABLED) {
+            return;
+        }
+
+        final Tab selectedTab = Tabs.getInstance().getSelectedTab();
+        if (selectedTab == null) {
+            Log.w(LOGTAG, "Selected tab is null: could not page info to store screenshot.");
+            return;
+        }
+
+        final Activity activity = activityReference.get();
+        if (activity == null) {
+            return;
+        }
+
+        GeckoProfile.get(activity).getDB().getUrlAnnotations().insertScreenshot(
+                activity.getContentResolver(), selectedTab.getURL(), screenshotPath);
+
+        SnackbarHelper.showSnackbar(activity,
+                activity.getResources().getString(R.string.screenshot_added_to_bookmarks), Snackbar.LENGTH_SHORT);
+    }
+
+    @Override
+    public void onResume(BrowserApp browserApp) {
+        mScreenshotObserver.start();
+    }
+
+    @Override
+    public void onPause(BrowserApp browserApp) {
+        mScreenshotObserver.stop();
+    }
+}
--- a/mobile/android/base/moz.build
+++ b/mobile/android/base/moz.build
@@ -528,16 +528,17 @@ gbjar.sources += ['java/org/mozilla/geck
     'restrictions/DefaultConfiguration.java',
     'restrictions/GuestProfileConfiguration.java',
     'restrictions/Restrictable.java',
     'restrictions/RestrictedProfileConfiguration.java',
     'restrictions/RestrictionCache.java',
     'restrictions/RestrictionConfiguration.java',
     'restrictions/RestrictionProvider.java',
     'restrictions/Restrictions.java',
+    'ScreenshotDelegate.java',
     'ScreenshotObserver.java',
     'search/SearchEngine.java',
     'search/SearchEngineManager.java',
     'ServiceNotificationClient.java',
     'SessionParser.java',
     'SharedPreferencesHelper.java',
     'SiteIdentity.java',
     'SmsManager.java',