Bug 761706 - Move application-level receiver objects from GeckoApp to GeckoApplication. r=mfinkle
authorKartikaya Gupta <kgupta@mozilla.com>
Fri, 14 Sep 2012 11:19:40 -0400
changeset 107206 967eec8011604cb2bb02a651771770bbfd8b3d25
parent 107205 0a5e2949c8f382f209d67aba4550fcbe2b249a72
child 107207 ec6eab545501a8810b3736eeab4215429c2b57a9
push id74
push usershu@rfrn.org
push dateTue, 18 Sep 2012 19:23:47 +0000
reviewersmfinkle
bugs761706
milestone18.0a1
Bug 761706 - Move application-level receiver objects from GeckoApp to GeckoApplication. r=mfinkle
mobile/android/base/GeckoActivity.java.in
mobile/android/base/GeckoApp.java
mobile/android/base/GeckoApplication.java
--- a/mobile/android/base/GeckoActivity.java.in
+++ b/mobile/android/base/GeckoActivity.java.in
@@ -1,70 +1,31 @@
 /* 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/. */
 
-#filter substitution
 package org.mozilla.gecko;
 
 import android.app.Activity;
-import android.content.ComponentName;
-import android.content.Intent;
 
 public class GeckoActivity extends Activity {
-    private boolean hasStarted = false;
-    private boolean mGeckoActivityOpened = false;
-
     @Override
     public void onPause() {
         super.onPause();
 
-        // Avoid pause notifications in destroy path.
-        if (!isFinishing() && (getApplication() instanceof GeckoApplication))
+        if (getApplication() instanceof GeckoApplication) {
             ((GeckoApplication) getApplication()).onActivityPause(this);
+        }
     }
 
     @Override
     public void onResume() {
         super.onResume();
 
-        // Avoid resume notifications in startup path.
-        if (hasStarted && (getApplication() instanceof GeckoApplication)) {
+        if (getApplication() instanceof GeckoApplication) {
             ((GeckoApplication) getApplication()).onActivityResume(this);
-            mGeckoActivityOpened = false;
-        } else {
-            hasStarted = true;
         }
     }
 
-    @Override
-    public void startActivity(Intent intent) {
-        checkIfGeckoActivity(intent);
-        super.startActivity(intent);
-    }
-
-    @Override
-    public void startActivityForResult(Intent intent, int request) {
-        checkIfGeckoActivity(intent);
-        super.startActivityForResult(intent, request);
-    }
-
-    private void checkIfGeckoActivity(Intent intent) {
-        // Whenever we call our own activity, the component and it's package name is set.
-        // If we call an activity from another package, or an open intent (leaving android to resolve)
-        // component has a different package name or it is null.
-        ComponentName component = intent.getComponent();
-        mGeckoActivityOpened = false;
-        if (component != null &&
-            component.getPackageName() != null &&
-            component.getPackageName().equals("@ANDROID_PACKAGE_NAME@")) {
-            mGeckoActivityOpened = true;
-        }
-    }
-
-    public boolean isGeckoActivityOpened() {
-        return mGeckoActivityOpened;
-    }
-
     public boolean isApplicationInBackground() {
         return ((GeckoApplication) getApplication()).isApplicationInBackground();
     } 
 }
--- a/mobile/android/base/GeckoApp.java
+++ b/mobile/android/base/GeckoApp.java
@@ -106,17 +106,16 @@ import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.CopyOnWriteArrayList;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 abstract public class GeckoApp
                 extends GeckoActivity 
                 implements GeckoEventListener, SensorEventListener, LocationListener,
-                           GeckoApplication.ApplicationLifecycleCallbacks,
                            Tabs.OnTabsChangedListener, GeckoEventResponder
 {
     private static final String LOGTAG = "GeckoApp";
 
     public static enum StartupMode {
         NORMAL,
         NEW_VERSION,
         NEW_PROFILE
@@ -1436,18 +1435,16 @@ abstract public class GeckoApp
                 editor.putBoolean(GeckoApp.PREFS_OOM_EXCEPTION, false);
 
                 // put a flag to check if we got a normal onSaveInstaceState
                 // on exit, or if we were suddenly killed (crash or native OOM)
                 editor.putBoolean(GeckoApp.PREFS_WAS_STOPPED, false);
                 editor.commit();
             }
         });
-
-        ((GeckoApplication)getApplication()).addApplicationLifecycleCallbacks(this);
     }
 
     void initializeChrome(String uri, Boolean isExternalURL) {
         mDoorHangerPopup = new DoorHangerPopup(this, null);
     }
 
     private void initialize() {
         mInitialized = true;
@@ -1597,35 +1594,24 @@ abstract public class GeckoApp
         registerEventListener("Share:Image");
         registerEventListener("Sanitize:ClearHistory");
         registerEventListener("Update:Check");
 
         if (SmsManager.getInstance() != null) {
           SmsManager.getInstance().start();
         }
 
-        GeckoBatteryManager.getInstance().init(this);
-        GeckoBatteryManager.getInstance().start();
-
-        GeckoConnectivityReceiver.getInstance().init(this);
-        GeckoConnectivityReceiver.getInstance().start();
-
         mPromptService = new PromptService();
 
         mTextSelection = new TextSelection((TextSelectionHandle) findViewById(R.id.start_handle),
                                            (TextSelectionHandle) findViewById(R.id.end_handle),
                                            GeckoAppShell.getEventDispatcher());
 
-        GeckoNetworkManager.getInstance().init(this);
-        GeckoNetworkManager.getInstance().start();
-
         UpdateServiceHelper.registerForUpdates(this);
 
-        GeckoScreenOrientationListener.getInstance().start();
-
         final GeckoApp self = this;
 
         GeckoAppShell.getHandler().postDelayed(new Runnable() {
             public void run() {
                 Log.w(LOGTAG, "zerdatime " + SystemClock.uptimeMillis() + " - pre checkLaunchState");
                 // Sync settings need Gecko to be loaded, so
                 // no hurry in starting this.
                 checkMigrateSync();
@@ -1894,16 +1880,18 @@ abstract public class GeckoApp
 
         int newOrientation = getResources().getConfiguration().orientation;
 
         if (mOrientation != newOrientation) {
             mOrientation = newOrientation;
             refreshChrome();
         }
 
+        GeckoScreenOrientationListener.getInstance().start();
+
         // User may have enabled/disabled accessibility.
         GeckoAccessibility.updateAccessibilitySettings();
 
         GeckoBackgroundThread.getHandler().post(new Runnable() {
             public void run() {
                 SharedPreferences prefs =
                     GeckoApp.mAppContext.getSharedPreferences(GeckoApp.PREFS_NAME, 0);
                 SharedPreferences.Editor editor = prefs.edit();
@@ -1953,16 +1941,18 @@ abstract public class GeckoApp
                 SharedPreferences prefs =
                     GeckoApp.mAppContext.getSharedPreferences(GeckoApp.PREFS_NAME, 0);
                 SharedPreferences.Editor editor = prefs.edit();
                 editor.putBoolean(GeckoApp.PREFS_WAS_STOPPED, true);
                 editor.commit();
             }
         });
 
+        GeckoScreenOrientationListener.getInstance().stop();
+
         super.onPause();
     }
 
     @Override
     public void onRestart()
     {
         Log.i(LOGTAG, "restart");
 
@@ -2061,21 +2051,17 @@ abstract public class GeckoApp
         if (SmsManager.getInstance() != null) {
             SmsManager.getInstance().stop();
             if (isFinishing())
                 SmsManager.getInstance().shutdown();
         }
 
         super.onDestroy();
 
-        GeckoBatteryManager.getInstance().stop();
-
         Tabs.unregisterOnTabsChangedListener(this);
-
-        ((GeckoApplication) getApplication()).removeApplicationLifecycleCallbacks(this);
     }
 
     protected void registerEventListener(String event) {
         GeckoAppShell.getEventDispatcher().registerEventListener(event, this);
     }
 
     protected void unregisterEventListener(String event) {
         GeckoAppShell.getEventDispatcher().unregisterEventListener(event, this);
@@ -2128,37 +2114,16 @@ abstract public class GeckoApp
         Log.e(LOGTAG, "low memory");
         if (checkLaunchState(LaunchState.GeckoRunning))
             GeckoAppShell.onLowMemory();
         super.onLowMemory();
         GeckoAppShell.geckoEventSync();
     }
 
     @Override
-    public void onApplicationPause() {
-        Log.i(LOGTAG, "application paused");
-        GeckoAppShell.sendEventToGecko(GeckoEvent.createPauseEvent(true));
-
-        GeckoConnectivityReceiver.getInstance().stop();
-        GeckoNetworkManager.getInstance().stop();
-        GeckoScreenOrientationListener.getInstance().stop();
-    }
-
-    @Override
-    public void onApplicationResume() {
-        Log.i(LOGTAG, "application resumed");
-        if (checkLaunchState(LaunchState.GeckoRunning))
-            GeckoAppShell.sendEventToGecko(GeckoEvent.createResumeEvent(true));
-
-        GeckoConnectivityReceiver.getInstance().start();
-        GeckoNetworkManager.getInstance().start();
-        GeckoScreenOrientationListener.getInstance().start();
-    }
-
-    @Override
     public Object onRetainNonConfigurationInstance() {
         // Send a non-null value so that we can restart the application, 
         // when activity restarts due to configuration change.
         return new Boolean(true);
     } 
 
     abstract public String getPackageName();
     abstract public String getContentProcessName();
--- a/mobile/android/base/GeckoApplication.java
+++ b/mobile/android/base/GeckoApplication.java
@@ -5,71 +5,46 @@
 package org.mozilla.gecko;
 
 import android.app.Application;
 
 import java.util.ArrayList;
 
 public class GeckoApplication extends Application {
 
-    private boolean mInBackground = false;
-    private ArrayList<ApplicationLifecycleCallbacks> mListeners;
+    private boolean mInBackground;
 
     @Override
     public void onCreate() {
         // workaround for http://code.google.com/p/android/issues/detail?id=20915
         try {
             Class.forName("android.os.AsyncTask");
         } catch (ClassNotFoundException e) {}
 
         super.onCreate();
-    }
 
-    public interface ApplicationLifecycleCallbacks {
-        public void onApplicationPause();
-        public void onApplicationResume();
-    }
-
-    public void addApplicationLifecycleCallbacks(ApplicationLifecycleCallbacks callback) {
-        if (mListeners == null)
-            mListeners = new ArrayList<ApplicationLifecycleCallbacks>();
-
-        mListeners.add(callback);
-    }
-
-    public void removeApplicationLifecycleCallbacks(ApplicationLifecycleCallbacks callback) {
-        if (mListeners == null)
-            return;
-
-        mListeners.remove(callback);
+        GeckoConnectivityReceiver.getInstance().init(getApplicationContext());
+        GeckoBatteryManager.getInstance().init(getApplicationContext());
+        GeckoBatteryManager.getInstance().start();
+        GeckoNetworkManager.getInstance().init(getApplicationContext());
     }
 
-    public void onActivityPause(GeckoActivity activity) {
-        if (activity.isGeckoActivityOpened())
-            return;
-
-        if (mListeners == null)
-            return;
-
+    protected void onActivityPause(GeckoActivity activity) {
         mInBackground = true;
 
-        for (ApplicationLifecycleCallbacks listener: mListeners)
-            listener.onApplicationPause();
+        GeckoAppShell.sendEventToGecko(GeckoEvent.createPauseEvent(true));
+        GeckoConnectivityReceiver.getInstance().stop();
+        GeckoNetworkManager.getInstance().stop();
     }
 
-    public void onActivityResume(GeckoActivity activity) {
-        // This is a misnomer. Should have been "wasGeckoActivityOpened".
-        if (activity.isGeckoActivityOpened())
-            return;
-
-        if (mListeners == null)
-            return;
-
-        for (ApplicationLifecycleCallbacks listener: mListeners)
-            listener.onApplicationResume();
+    protected void onActivityResume(GeckoActivity activity) {
+        if (GeckoApp.checkLaunchState(GeckoApp.LaunchState.GeckoRunning))
+            GeckoAppShell.sendEventToGecko(GeckoEvent.createResumeEvent(true));
+        GeckoConnectivityReceiver.getInstance().start();
+        GeckoNetworkManager.getInstance().start();
 
         mInBackground = false;
     }
 
     public boolean isApplicationInBackground() {
         return mInBackground;
     }
 }