Bug 1351739 - Part 1 - Track the currently active activity. r=sebastian,walkingice

Required because later on, we'll need to know if we're in the correct activity for a tab or need to switch activities.

As a follow-up, we can later also hook up our current manual activity tracking from GeckoApplication to this (we most probably won't be able to get rid of the GeckoActivityStatus shenanigans, though).

MozReview-Commit-ID: 5lZrAMsB9Gy

--HG--
extra : rebase_source : 5a8fec92cad15db05063dbb940874615c317c768
This commit is contained in:
Jan Henning 2017-04-02 11:22:12 +02:00
Родитель 1c3e4ca69b
Коммит 9fcd07adad
5 изменённых файлов: 99 добавлений и 0 удалений

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

@ -41,6 +41,11 @@ public abstract class GeckoActivity extends AppCompatActivity implements GeckoAc
}
}
@Override
protected void onNewIntent(Intent externalIntent) {
GeckoActivityMonitor.getInstance().onActivityNewIntent(this);
}
@Override
public void onCreate(android.os.Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

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

@ -0,0 +1,89 @@
/* -*- 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.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
public class GeckoActivityMonitor implements Application.ActivityLifecycleCallbacks {
private static final String LOGTAG = "GeckoActivityMonitor";
// We only hold a reference to the currently running activity - when this activity pauses,
// the reference is released or else overwritten by the next activity.
@SuppressLint("StaticFieldLeak")
private static final GeckoActivityMonitor instance = new GeckoActivityMonitor();
private Activity currentActivity;
public static GeckoActivityMonitor getInstance() {
return instance;
}
private GeckoActivityMonitor() { }
public Activity getCurrentActivity() {
return currentActivity;
}
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
currentActivity = activity;
}
// onNewIntent happens in-between a pause/resume cycle, which means that we wouldn't have
// a current activity to report if we were using only the official ActivityLifecycleCallbacks.
// For code that wants to know the current activity even at this point we therefore have to
// handle this ourselves.
public void onActivityNewIntent(Activity activity) {
currentActivity = activity;
}
@Override
public void onActivityStarted(Activity activity) {
currentActivity = activity;
}
@Override
public void onActivityResumed(Activity activity) {
currentActivity = activity;
}
/**
* Intended to be used if the current activity is required to be up-to-date for code that
* executes in onCreate/onStart/... before calling the corresponding superclass method.
*/
public void setCurrentActivity(Activity activity) {
currentActivity = activity;
}
@Override
public void onActivityPaused(Activity activity) {
releaseIfCurrentActivity(activity);
}
@Override
public void onActivityStopped(Activity activity) {
releaseIfCurrentActivity(activity);
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) { }
@Override
public void onActivityDestroyed(Activity activity) {
releaseIfCurrentActivity(activity);
}
private void releaseIfCurrentActivity(Activity activity) {
// If the next activity has already started by the time the previous activity is being
// stopped/destroyed, we no longer need to clear the previous activity.
if (currentActivity == activity) {
currentActivity = null;
}
}
}

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

@ -2180,6 +2180,8 @@ public abstract class GeckoApp
@Override
protected void onNewIntent(Intent externalIntent) {
super.onNewIntent(externalIntent);
final SafeIntent intent = new SafeIntent(externalIntent);
final boolean isFirstTab = !mWasFirstTabShownAfterActivityUnhidden;

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

@ -166,6 +166,8 @@ public class GeckoApplication extends Application
mRefWatcher = LeakCanary.install(this);
registerActivityLifecycleCallbacks(GeckoActivityMonitor.getInstance());
final Context context = getApplicationContext();
GeckoAppShell.setApplicationContext(context);
HardwareUtils.init(context);

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

@ -586,6 +586,7 @@ gbjar.sources += ['java/org/mozilla/gecko/' + x for x in [
'firstrun/TabQueuePanel.java',
'FormAssistPopup.java',
'GeckoActivity.java',
'GeckoActivityMonitor.java',
'GeckoActivityStatus.java',
'GeckoApp.java',
'GeckoApplication.java',