Bug 739747: Add application level onPause and onResume. [r=mfinkle, r=blassey]

This commit is contained in:
Sriram Ramasubramanian 2012-04-03 11:58:01 -07:00
Родитель bb96b17e38
Коммит 9beeb5e1fa
9 изменённых файлов: 163 добавлений и 35 удалений

Просмотреть файл

@ -49,6 +49,7 @@
<application android:label="@MOZ_APP_DISPLAYNAME@"
android:icon="@drawable/icon"
android:name="org.mozilla.gecko.GeckoApplication"
#if MOZILLA_OFFICIAL
android:debuggable="false">
#else

Просмотреть файл

@ -86,7 +86,7 @@ import org.mozilla.gecko.db.BrowserDB;
import org.json.JSONObject;
public class AwesomeBar extends Activity implements GeckoEventListener {
public class AwesomeBar extends GeckoActivity implements GeckoEventListener {
private static final String LOGTAG = "GeckoAwesomeBar";
static final String URL_KEY = "url";

Просмотреть файл

@ -0,0 +1,69 @@
/* 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.app.Application;
import android.os.Bundle;
import android.content.Intent;
import android.content.ComponentName;
public class GeckoActivity extends Activity {
private boolean hasStarted = false;
private boolean isGeckoActivityOpened = false;
@Override
public void onPause() {
super.onPause();
// Avoid pause notifications in destroy path.
if (!isFinishing() && (getApplication() instanceof GeckoApplication))
((GeckoApplication) getApplication()).onActivityPause(this);
}
@Override
public void onResume() {
super.onResume();
// Avoid resume notifications in startup path.
if (hasStarted && (getApplication() instanceof GeckoApplication)) {
((GeckoApplication) getApplication()).onActivityResume(this);
isGeckoActivityOpened = 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();
if (component == null)
isGeckoActivityOpened = false;
if (component.getPackageName().equals("@ANDROID_PACKAGE_NAME@"))
isGeckoActivityOpened = true;
else
isGeckoActivityOpened = false;
}
public boolean isApplicationInBackground() {
return !isGeckoActivityOpened;
}
}

Просмотреть файл

@ -93,8 +93,9 @@ import android.content.pm.PackageManager.*;
import dalvik.system.*;
abstract public class GeckoApp
extends Activity implements GeckoEventListener, SensorEventListener, LocationListener
{
extends GeckoActivity
implements GeckoEventListener, SensorEventListener, LocationListener,
GeckoApplication.ApplicationLifecycleCallbacks {
private static final String LOGTAG = "GeckoApp";
public static enum StartupMode {
@ -141,7 +142,6 @@ abstract public class GeckoApp
private static AbsoluteLayout mPluginContainer;
public String mLastTitle;
private int mOwnActivityDepth = 0;
private boolean mRestoreSession = false;
private boolean mInitialized = false;
@ -530,7 +530,7 @@ abstract public class GeckoApp
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (mOwnActivityDepth > 0)
if (!isApplicationInBackground())
return; // we're showing one of our own activities and likely won't get paged out
if (outState == null)
@ -1584,6 +1584,8 @@ abstract public class GeckoApp
mMainLayout = (LinearLayout) findViewById(R.id.main_layout);
mConnectivityReceiver = new GeckoConnectivityReceiver();
((GeckoApplication) getApplication()).addApplicationLifecycleCallbacks(this);
}
private void initialize() {
@ -1971,7 +1973,6 @@ abstract public class GeckoApp
{
Log.i(LOGTAG, "pause");
GeckoAppShell.sendEventToGecko(GeckoEvent.createPauseEvent(mOwnActivityDepth));
// The user is navigating away from this activity, but nothing
// has come to the foreground yet; for Gecko, we may want to
// stop repainting, for example.
@ -1991,8 +1992,6 @@ abstract public class GeckoApp
public void onResume()
{
Log.i(LOGTAG, "resume");
if (checkLaunchState(LaunchState.GeckoRunning))
GeckoAppShell.sendEventToGecko(GeckoEvent.createResumeEvent(mOwnActivityDepth));
// After an onPause, the activity is back in the foreground.
// Undo whatever we did in onPause.
@ -2023,9 +2022,6 @@ abstract public class GeckoApp
GeckoScreenOrientationListener.getInstance().start();
}
});
if (mOwnActivityDepth > 0)
mOwnActivityDepth--;
}
@Override
@ -2043,7 +2039,7 @@ abstract public class GeckoApp
// etc., and generally mark the profile as 'clean', and then
// dirty it again if we get an onResume.
GeckoAppShell.sendEventToGecko(GeckoEvent.createStoppingEvent(mOwnActivityDepth));
GeckoAppShell.sendEventToGecko(GeckoEvent.createStoppingEvent(isApplicationInBackground()));
super.onStop();
}
@ -2060,7 +2056,7 @@ abstract public class GeckoApp
Log.w(LOGTAG, "zerdatime " + SystemClock.uptimeMillis() + " - onStart");
Log.i(LOGTAG, "start");
GeckoAppShell.sendEventToGecko(GeckoEvent.createStartEvent(mOwnActivityDepth));
GeckoAppShell.sendEventToGecko(GeckoEvent.createStartEvent(isApplicationInBackground()));
super.onStart();
}
@ -2115,6 +2111,8 @@ abstract public class GeckoApp
if (mAboutHomeContent != null) {
mAboutHomeContent.onDestroy();
}
((GeckoApplication) getApplication()).removeApplicationLifecycleCallbacks(this);
}
@Override
@ -2148,6 +2146,19 @@ abstract public class GeckoApp
super.onLowMemory();
}
@Override
public void onApplicationPause() {
Log.i(LOGTAG, "application paused");
GeckoAppShell.sendEventToGecko(GeckoEvent.createPauseEvent(true));
}
@Override
public void onApplicationResume() {
Log.i(LOGTAG, "application resumed");
if (checkLaunchState(LaunchState.GeckoRunning))
GeckoAppShell.sendEventToGecko(GeckoEvent.createResumeEvent(true));
}
abstract public String getPackageName();
abstract public String getContentProcessName();
@ -2771,18 +2782,6 @@ abstract public class GeckoApp
}
}
}
@Override
public void startActivity(Intent intent) {
mOwnActivityDepth++;
super.startActivity(intent);
}
@Override
public void startActivityForResult(Intent intent, int request) {
mOwnActivityDepth++;
super.startActivityForResult(intent, request);
}
}
class PluginLayoutParams extends AbsoluteLayout.LayoutParams

Просмотреть файл

@ -0,0 +1,57 @@
/* 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 java.util.ArrayList;
import android.app.Activity;
import android.app.Application;
public class GeckoApplication extends Application {
private ArrayList<ApplicationLifecycleCallbacks> mListeners;
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.isApplicationInBackground())
return;
if (mListeners == null)
return;
for (ApplicationLifecycleCallbacks listener: mListeners)
listener.onApplicationPause();
}
public void onActivityResume(GeckoActivity activity) {
// This is a misnomer. Should have been "wasApplicationInBackground".
if (!activity.isApplicationInBackground())
return;
if (mListeners == null)
return;
for (ApplicationLifecycleCallbacks listener: mListeners)
listener.onApplicationResume();
}
}

Просмотреть файл

@ -150,27 +150,27 @@ public class GeckoEvent {
mType = evType;
}
public static GeckoEvent createPauseEvent(int activityDepth) {
public static GeckoEvent createPauseEvent(boolean isApplicationInBackground) {
GeckoEvent event = new GeckoEvent(ACTIVITY_PAUSING);
event.mFlags = activityDepth > 0 ? 1 : 0;
event.mFlags = isApplicationInBackground ? 0 : 1;
return event;
}
public static GeckoEvent createResumeEvent(int activityDepth) {
public static GeckoEvent createResumeEvent(boolean isApplicationInBackground) {
GeckoEvent event = new GeckoEvent(ACTIVITY_RESUMING);
event.mFlags = activityDepth > 0 ? 1 : 0;
event.mFlags = isApplicationInBackground ? 0 : 1;
return event;
}
public static GeckoEvent createStoppingEvent(int activityDepth) {
public static GeckoEvent createStoppingEvent(boolean isApplicationInBackground) {
GeckoEvent event = new GeckoEvent(ACTIVITY_STOPPING);
event.mFlags = activityDepth > 0 ? 1 : 0;
event.mFlags = isApplicationInBackground ? 0 : 1;
return event;
}
public static GeckoEvent createStartEvent(int activityDepth) {
public static GeckoEvent createStartEvent(boolean isApplicationInBackground) {
GeckoEvent event = new GeckoEvent(ACTIVITY_START);
event.mFlags = activityDepth > 0 ? 1 : 0;
event.mFlags = isApplicationInBackground ? 0 : 1;
return event;
}

Просмотреть файл

@ -79,6 +79,7 @@ FENNEC_JAVA_FILES = \
FloatUtils.java \
FormAssistPopup.java \
GeckoActionBar.java \
GeckoApplication.java \
GeckoApp.java \
GeckoAppShell.java \
GeckoAsyncTask.java \
@ -161,6 +162,7 @@ endif
FENNEC_PP_JAVA_FILES = \
App.java \
GeckoActivity.java \
LauncherShortcuts.java \
NotificationHandler.java \
Restarter.java \

Просмотреть файл

@ -27,7 +27,7 @@ import android.widget.SimpleExpandableListAdapter;
import android.text.TextUtils;
import android.util.Log;
public class RemoteTabs extends Activity
public class RemoteTabs extends GeckoActivity
implements ExpandableListView.OnGroupClickListener, ExpandableListView.OnChildClickListener,
TabsAccessor.OnQueryTabsCompleteListener {
private static final String LOGTAG = "GeckoRemoteTabs";

Просмотреть файл

@ -31,7 +31,7 @@ import android.widget.TextView;
import org.mozilla.gecko.sync.setup.SyncAccounts;
public class TabsTray extends Activity implements Tabs.OnTabsChangedListener {
public class TabsTray extends GeckoActivity implements Tabs.OnTabsChangedListener {
private static int sPreferredHeight;
private static int sMaxHeight;