Commit e0c63157 authored by gogerald's avatar gogerald Committed by Commit Bot

Remove obsolete NTPLaunchAfterInactivity

Bug: 1048225
Change-Id: I39b1e3d99fd092d95ad9dc9361e4912f92d6461a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2036375
Auto-Submit: Ganggui Tang <gogerald@chromium.org>
Reviewed-by: default avatarTed Choc <tedchoc@chromium.org>
Reviewed-by: default avatarJesse Doherty <jwd@chromium.org>
Commit-Queue: Jesse Doherty <jwd@chromium.org>
Cr-Commit-Position: refs/heads/master@{#739101}
parent dddbf4b3
......@@ -4,18 +4,13 @@
package org.chromium.chrome.browser;
import android.text.format.DateUtils;
import androidx.annotation.VisibleForTesting;
import org.chromium.base.Log;
import org.chromium.chrome.browser.flags.ChromeFeatureList;
import org.chromium.chrome.browser.lifecycle.ActivityLifecycleDispatcher;
import org.chromium.chrome.browser.lifecycle.Destroyable;
import org.chromium.chrome.browser.lifecycle.PauseResumeWithNativeObserver;
import org.chromium.chrome.browser.lifecycle.StartStopWithNativeObserver;
import org.chromium.chrome.browser.preferences.SharedPreferencesManager;
import org.chromium.content_public.browser.UiThreadTaskTraits;
/**
* Manages pref that can track the delay since the last stop of the tracked activity.
......@@ -25,42 +20,9 @@ public class ChromeInactivityTracker
private static final String TAG = "InactivityTracker";
private static final long UNKNOWN_LAST_BACKGROUNDED_TIME = -1;
private static final int UNKNOWN_LAUNCH_DELAY_MINS = -1;
private static final int DEFAULT_LAUNCH_DELAY_IN_MINS = 5;
@VisibleForTesting
public static final String FEATURE_NAME = ChromeFeatureList.NTP_LAUNCH_AFTER_INACTIVITY;
@VisibleForTesting
public static final String NTP_LAUNCH_DELAY_IN_MINS_PARAM = "delay_in_mins";
// Only true if the feature is enabled.
private final boolean mIsEnabled;
private final String mPrefName;
private int mNtpLaunchDelayInMins = 1;
private final ActivityLifecycleDispatcher mLifecycleDispatcher;
private final Runnable mInactiveCallback;
private CancelableRunnableTask mCurrentlyPostedInactiveCallback;
private static class CancelableRunnableTask implements Runnable {
private boolean mIsRunnable = true;
private final Runnable mTask;
private CancelableRunnableTask(Runnable task) {
mTask = task;
}
@Override
public void run() {
if (mIsRunnable) {
mTask.run();
}
}
public void cancel() {
mIsRunnable = false;
}
}
/**
* Creates an inactivity tracker without a timeout callback. This is useful if clients only
......@@ -71,29 +33,7 @@ public class ChromeInactivityTracker
*/
public ChromeInactivityTracker(
String prefName, ActivityLifecycleDispatcher lifecycleDispatcher) {
this(prefName, lifecycleDispatcher, () -> {});
}
/**
* Creates an inactivity tracker that stores a timestamp in prefs, and sets a timeout. If the
* timeout expires while the activity that is tracked is still stopped, then the callback is
* executed. If the activity otherwise starts up, it can check whether the timeout has expired
* using #inactivityThresholdPassed and perform the appropriate behavior.
* @param prefName the location in shared preferences that the timestamp is stored.
* @param lifecycleDispatcher tracks the lifecycle of the Activity of interest, and calls
* observer methods on ChromeInactivityTracker.
* @param inactiveCallback called if the activity is stopped for longer than the configured
* inactivity timeout.
*/
public ChromeInactivityTracker(String prefName, ActivityLifecycleDispatcher lifecycleDispatcher,
Runnable inactiveCallback) {
mPrefName = prefName;
mInactiveCallback = inactiveCallback;
mNtpLaunchDelayInMins = ChromeFeatureList.getFieldTrialParamByFeatureAsInt(
FEATURE_NAME, NTP_LAUNCH_DELAY_IN_MINS_PARAM, DEFAULT_LAUNCH_DELAY_IN_MINS);
mIsEnabled = ChromeFeatureList.isEnabled(FEATURE_NAME);
mLifecycleDispatcher = lifecycleDispatcher;
mLifecycleDispatcher.register(this);
}
......@@ -108,14 +48,9 @@ public class ChromeInactivityTracker
}
/**
* Updates shared preferences such that the last backgrounded time is no
* longer valid. This will prevent multiple new intents from firing.
* @return The last backgrounded time in millis.
*/
private void clearLastBackgroundedTimeInPrefs() {
setLastBackgroundedTimeInPrefs(UNKNOWN_LAST_BACKGROUNDED_TIME);
}
long getLastBackgroundedTimeMs() {
public long getLastBackgroundedTimeMs() {
return SharedPreferencesManager.getInstance().readLong(
mPrefName, UNKNOWN_LAST_BACKGROUNDED_TIME);
}
......@@ -131,35 +66,8 @@ public class ChromeInactivityTracker
return System.currentTimeMillis() - lastBackgroundedTimeMs;
}
/**
* @return true if the timestamp in prefs is older than the configured timeout.
*/
public boolean inactivityThresholdPassed() {
if (!mIsEnabled) {
return false;
}
long lastBackgroundedTimeMs = getLastBackgroundedTimeMs();
if (lastBackgroundedTimeMs == UNKNOWN_LAST_BACKGROUNDED_TIME) return false;
long backgroundDurationMinutes =
getTimeSinceLastBackgroundedMs() / DateUtils.MINUTE_IN_MILLIS;
if (backgroundDurationMinutes < mNtpLaunchDelayInMins) {
Log.i(TAG, "Not launching NTP due to inactivity, background time: %d, launch delay: %d",
backgroundDurationMinutes, mNtpLaunchDelayInMins);
return false;
}
Log.i(TAG, "Forcing NTP due to inactivity.");
return true;
}
@Override
public void onStartWithNative() {
cancelCurrentTask();
}
public void onStartWithNative() {}
@Override
public void onResumeWithNative() {
......@@ -167,7 +75,7 @@ public class ChromeInactivityTracker
// handlers the chance to respond to inactivity during any onStartWithNative handler
// regardless of ordering. onResume is always called after onStart, and it should be fine to
// consider Chrome active if it reaches onResume.
clearLastBackgroundedTimeInPrefs();
setLastBackgroundedTimeInPrefs(UNKNOWN_LAST_BACKGROUNDED_TIME);
}
@Override
......@@ -176,34 +84,11 @@ public class ChromeInactivityTracker
@Override
public void onStopWithNative() {
// Always track the last backgrounded time in case others are using the pref.
long timeInMillis = System.currentTimeMillis();
setLastBackgroundedTimeInPrefs(timeInMillis);
if (!mIsEnabled) return;
Log.i(TAG, "onStop, scheduling for " + mNtpLaunchDelayInMins + " minutes");
cancelCurrentTask();
if (mNtpLaunchDelayInMins == UNKNOWN_LAUNCH_DELAY_MINS) {
Log.w(TAG, "Configured with unknown launch delay, disabling.");
return;
}
mCurrentlyPostedInactiveCallback = new CancelableRunnableTask(mInactiveCallback);
org.chromium.base.task.PostTask.postDelayedTask(UiThreadTaskTraits.DEFAULT,
mCurrentlyPostedInactiveCallback,
mNtpLaunchDelayInMins * DateUtils.MINUTE_IN_MILLIS);
setLastBackgroundedTimeInPrefs(System.currentTimeMillis());
}
@Override
public void destroy() {
mLifecycleDispatcher.unregister(this);
cancelCurrentTask();
}
private void cancelCurrentTask() {
if (mCurrentlyPostedInactiveCallback != null) {
mCurrentlyPostedInactiveCallback.cancel();
mCurrentlyPostedInactiveCallback = null;
}
}
}
......@@ -901,9 +901,6 @@ public class ChromeTabbedActivity extends ChromeActivity {
super.onNewIntentWithNative(intent);
if (isMainIntentFromLauncher(intent)) {
if (IntentHandler.getUrlFromIntent(intent) == null) {
maybeLaunchNtpOrResetBottomSheetFromMainIntent(intent);
}
logMainIntentBehavior(intent);
}
......@@ -1024,62 +1021,6 @@ public class ChromeTabbedActivity extends ChromeActivity {
return mInactivityTracker;
}
/**
* Determines if the intent should trigger an NTP and launches it if applicable. If Chrome Home
* is enabled, we reset the bottom sheet state to half after some time being backgrounded.
*
* @param intent The intent to check whether an NTP should be triggered.
* @return Whether an NTP was triggered as a result of this intent.
*/
private boolean maybeLaunchNtpOrResetBottomSheetFromMainIntent(Intent intent) {
assert isMainIntentFromLauncher(intent);
if (!IntentHandler.isIntentUserVisible()) return false;
if (!mInactivityTracker.inactivityThresholdPassed()) return false;
if (isInOverviewMode() && !isTablet()) {
mOverviewModeController.hideOverview(false);
}
if (!reuseOrCreateNewNtp()) return false;
RecordUserAction.record("MobileStartup.MainIntent.NTPCreatedDueToInactivity");
return true;
}
/**
* Creates or reuses an existing NTP and displays it to the user.
*
* @return Whether an NTP was reused/created. This returns false if the currently selected
* tab is an NTP, and no action is taken.
*/
private boolean reuseOrCreateNewNtp() {
// In cases where the tab model is initialized, attempt to reuse an existing NTP if
// available before attempting to create a new one.
TabModel normalTabModel = getTabModelSelector().getModel(false);
Tab ntpToRefocus = null;
for (int i = 0; i < normalTabModel.getCount(); i++) {
Tab tab = normalTabModel.getTabAt(i);
if (NewTabPage.isNTPUrl(tab.getUrl()) && !tab.canGoBack() && !tab.canGoForward()) {
// If the currently selected tab is an NTP, then take no action.
if (getActivityTab().equals(tab)) return false;
ntpToRefocus = tab;
break;
}
}
if (ntpToRefocus != null) {
normalTabModel.moveTab(ntpToRefocus.getId(), normalTabModel.getCount());
normalTabModel.setIndex(
TabModelUtils.getTabIndexById(normalTabModel, ntpToRefocus.getId()),
TabSelectionType.FROM_USER);
} else {
getTabCreator(false).launchUrl(UrlConstants.NTP_URL, TabLaunchType.FROM_EXTERNAL_APP);
}
return true;
}
@Override
public void initializeState() {
// This method goes through 3 steps:
......@@ -1128,11 +1069,6 @@ public class ChromeTabbedActivity extends ChromeActivity {
}
if (isMainIntentFromLauncher(intent)) {
if (IntentHandler.getUrlFromIntent(intent) == null) {
assert !mIntentWithEffect
: "ACTION_MAIN should not have triggered any prior action";
mIntentWithEffect = maybeLaunchNtpOrResetBottomSheetFromMainIntent(intent);
}
logMainIntentBehavior(intent);
}
}
......
......@@ -166,7 +166,6 @@ const base::Feature* kFeaturesExposedToJava[] = {
&kKitKatSupported,
&kNewPhotoPicker,
&kNotificationSuspender,
&kNTPLaunchAfterInactivity,
&kOfflineIndicatorV2,
&kOmniboxSpareRenderer,
&kOverlayNewLayout,
......@@ -501,9 +500,6 @@ const base::Feature kNewPhotoPicker{"NewPhotoPicker",
const base::Feature kNotificationSuspender{"NotificationSuspender",
base::FEATURE_ENABLED_BY_DEFAULT};
const base::Feature kNTPLaunchAfterInactivity{
"NTPLaunchAfterInactivity", base::FEATURE_DISABLED_BY_DEFAULT};
const base::Feature kOfflineIndicatorV2{"OfflineIndicatorV2",
base::FEATURE_DISABLED_BY_DEFAULT};
......
......@@ -91,7 +91,6 @@ extern const base::Feature kKitKatSupported;
extern const base::Feature kLanguagesPreference;
extern const base::Feature kNewPhotoPicker;
extern const base::Feature kNotificationSuspender;
extern const base::Feature kNTPLaunchAfterInactivity;
extern const base::Feature kOfflineIndicatorV2;
extern const base::Feature kOmniboxSpareRenderer;
extern const base::Feature kOverlayNewLayout;
......
......@@ -282,7 +282,6 @@ public abstract class ChromeFeatureList {
public static final String NEW_PHOTO_PICKER = "NewPhotoPicker";
public static final String NOTIFICATION_SUSPENDER = "NotificationSuspender";
public static final String NTP_ARTICLE_SUGGESTIONS = "NTPArticleSuggestions";
public static final String NTP_LAUNCH_AFTER_INACTIVITY = "NTPLaunchAfterInactivity";
public static final String OFFLINE_INDICATOR = "OfflineIndicator";
public static final String OFFLINE_INDICATOR_ALWAYS_HTTP_PROBE =
"OfflineIndicatorAlwaysHttpProbe";
......
......@@ -3740,24 +3740,6 @@
]
}
],
"NTPLaunchAfterInactivity": [
{
"platforms": [
"android"
],
"experiments": [
{
"name": "OneHourDelay",
"params": {
"delay_in_mins": "60"
},
"enable_features": [
"NTPLaunchAfterInactivity"
]
}
]
}
],
"NTPPopularSites": [
{
"platforms": [
......
......@@ -13583,6 +13583,7 @@ should be able to be added at any place in this file.
</action>
<action name="MobileStartup.MainIntent.NTPCreatedDueToInactivity">
<obsolete>Deprecated as of 02/2019</obsolete>
<owner>tedchoc@chromium.org</owner>
<description>
An ACTION_MAIN intent was received by Chrome and an NTP was created due to
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment