Bug 761706 - Move application-level receiver objects from GeckoApp to GeckoApplication. r=mfinkle

This commit is contained in:
Kartikaya Gupta 2012-09-14 11:19:40 -04:00
parent aedcf8eb47
commit 4060fb3e3e
3 changed files with 22 additions and 121 deletions

View File

@ -2,68 +2,29 @@
* 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();
}

View File

@ -111,7 +111,6 @@ 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";
@ -1441,8 +1440,6 @@ abstract public class GeckoApp
editor.commit();
}
});
((GeckoApplication)getApplication()).addApplicationLifecycleCallbacks(this);
}
void initializeChrome(String uri, Boolean isExternalURL) {
@ -1602,25 +1599,14 @@ abstract public class GeckoApp
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() {
@ -1899,6 +1885,8 @@ abstract public class GeckoApp
refreshChrome();
}
GeckoScreenOrientationListener.getInstance().start();
// User may have enabled/disabled accessibility.
GeckoAccessibility.updateAccessibilitySettings();
@ -1958,6 +1946,8 @@ abstract public class GeckoApp
}
});
GeckoScreenOrientationListener.getInstance().stop();
super.onPause();
}
@ -2066,11 +2056,7 @@ abstract public class GeckoApp
super.onDestroy();
GeckoBatteryManager.getInstance().stop();
Tabs.unregisterOnTabsChangedListener(this);
((GeckoApplication) getApplication()).removeApplicationLifecycleCallbacks(this);
}
protected void registerEventListener(String event) {
@ -2132,27 +2118,6 @@ abstract public class GeckoApp
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,

View File

@ -10,8 +10,7 @@ import java.util.ArrayList;
public class GeckoApplication extends Application {
private boolean mInBackground = false;
private ArrayList<ApplicationLifecycleCallbacks> mListeners;
private boolean mInBackground;
@Override
public void onCreate() {
@ -21,50 +20,26 @@ public class GeckoApplication extends Application {
} catch (ClassNotFoundException e) {}
super.onCreate();
GeckoConnectivityReceiver.getInstance().init(getApplicationContext());
GeckoBatteryManager.getInstance().init(getApplicationContext());
GeckoBatteryManager.getInstance().start();
GeckoNetworkManager.getInstance().init(getApplicationContext());
}
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);
}
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;
}