Commit a08775af authored by Richard Knoll's avatar Richard Knoll Committed by Commit Bot

Add ScreenOnAndUnlocked info to DeviceConditions.

Some features need to know if the screen is on and unlocked (e.g. the
ClickToCall feature will use this to open the dialer immediately instead
of displaying a notification). This adds this information to
DeviceConditions and updates all dependencies.

Bug: None
Change-Id: If67dc2d736b20284c0e79a0244753f594bfdba74
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1751730Reviewed-by: default avatarTommy Nyquist <nyquist@chromium.org>
Commit-Queue: Richard Knoll <knollr@chromium.org>
Cr-Commit-Position: refs/heads/master@{#686482}
parent 291b96e7
......@@ -5,6 +5,7 @@
package org.chromium.chrome.browser;
import android.annotation.TargetApi;
import android.app.KeyguardManager;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
......@@ -14,6 +15,7 @@ import android.os.BatteryManager;
import android.os.Build;
import android.os.PowerManager;
import org.chromium.base.ApiCompatibilityUtils;
import org.chromium.base.VisibleForTesting;
import org.chromium.net.ConnectionType;
import org.chromium.net.NetworkChangeNotifier;
......@@ -27,6 +29,7 @@ public class DeviceConditions {
private boolean mPowerConnected;
private int mBatteryPercentage;
private boolean mPowerSaveOn;
private boolean mScreenOnAndUnlocked;
// Network related variables.
private @ConnectionType int mNetConnectionType = ConnectionType.CONNECTION_UNKNOWN;
......@@ -42,12 +45,13 @@ public class DeviceConditions {
*/
@VisibleForTesting
public DeviceConditions(boolean powerConnected, int batteryPercentage, int netConnectionType,
boolean powerSaveOn, boolean activeNetworkMetered) {
boolean powerSaveOn, boolean activeNetworkMetered, boolean screenOnAndUnlocked) {
mPowerConnected = powerConnected;
mBatteryPercentage = batteryPercentage;
mPowerSaveOn = powerSaveOn;
mNetConnectionType = netConnectionType;
mActiveNetworkMetered = activeNetworkMetered;
mScreenOnAndUnlocked = screenOnAndUnlocked;
}
@VisibleForTesting
......@@ -64,7 +68,7 @@ public class DeviceConditions {
return new DeviceConditions(isCurrentlyPowerConnected(context, batteryStatus),
getCurrentBatteryPercentage(context, batteryStatus),
getCurrentNetConnectionType(context), isCurrentlyInPowerSaveMode(context),
isCurrentActiveNetworkMetered(context));
isCurrentActiveNetworkMetered(context), isCurrentlyScreenOnAndUnlocked(context));
}
/** @return Whether the device is connected to a power source. */
......@@ -173,6 +177,16 @@ public class DeviceConditions {
return cm.isActiveNetworkMetered();
}
/**
* @return Whether the screen is currently on and unlocked.
*/
public static boolean isCurrentlyScreenOnAndUnlocked(Context context) {
KeyguardManager keyguardManager =
(KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
return keyguardManager != null && !keyguardManager.isKeyguardLocked()
&& ApiCompatibilityUtils.isInteractive(context);
}
private static Intent getBatteryStatus(Context context) {
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
// Note this is a sticky intent, so we aren't really registering a receiver, just getting
......@@ -232,4 +246,9 @@ public class DeviceConditions {
public boolean isActiveNetworkMetered() {
return mActiveNetworkMetered;
}
/** Returns whether the screen is on and unlocked. */
public boolean isScreenOnAndUnlocked() {
return mScreenOnAndUnlocked;
}
}
......@@ -59,4 +59,9 @@ public class ShadowDeviceConditions {
public static boolean isCurrentActiveNetworkMetered(Context context) {
return sDeviceConditions.isActiveNetworkMetered();
}
@Implementation
public static boolean isCurrentlyScreenOnAndUnlocked(Context context) {
return sDeviceConditions.isScreenOnAndUnlocked();
}
}
......@@ -114,9 +114,10 @@ public class ExploreSitesBackgroundTaskUnitTest {
boolean powerSaveModeOn = true;
int highBatteryLevel = 75;
boolean metered = true;
boolean screenOnAndUnlocked = true;
DeviceConditions deviceConditions = new DeviceConditions(
!powerConnected, highBatteryLevel, connectionType, !powerSaveModeOn, !metered);
DeviceConditions deviceConditions = new DeviceConditions(!powerConnected, highBatteryLevel,
connectionType, !powerSaveModeOn, !metered, screenOnAndUnlocked);
ShadowDeviceConditions.setCurrentConditions(deviceConditions);
}
......
......@@ -62,6 +62,7 @@ public class OfflineBackgroundTaskTest {
private static final boolean POWER_CONNECTED = true;
private static final boolean POWER_SAVE_MODE_ON = true;
private static final boolean METERED = true;
private static final boolean SCREEN_ON_AND_UNLOCKED = true;
private static final int MINIMUM_BATTERY_LEVEL = 33;
private static final String IS_LOW_END_DEVICE_SWITCH =
"--" + BaseSwitches.ENABLE_LOW_END_DEVICE_MODE;
......@@ -74,7 +75,8 @@ public class OfflineBackgroundTaskTest {
private TriggerConditions mTriggerConditions =
new TriggerConditions(!REQUIRE_POWER, MINIMUM_BATTERY_LEVEL, REQUIRE_UNMETERED);
private DeviceConditions mDeviceConditions = new DeviceConditions(!POWER_CONNECTED,
MINIMUM_BATTERY_LEVEL + 5, ConnectionType.CONNECTION_3G, !POWER_SAVE_MODE_ON, !METERED);
MINIMUM_BATTERY_LEVEL + 5, ConnectionType.CONNECTION_3G, !POWER_SAVE_MODE_ON, !METERED,
SCREEN_ON_AND_UNLOCKED);
private Activity mTestActivity;
@Mock
......@@ -134,9 +136,9 @@ public class OfflineBackgroundTaskTest {
@Feature({"OfflinePages"})
public void testCheckConditions_BatteryConditions_LowBattery_NoPower() {
// Setup low battery conditions with no power connected.
DeviceConditions deviceConditionsLowBattery =
new DeviceConditions(!POWER_CONNECTED, MINIMUM_BATTERY_LEVEL - 1,
ConnectionType.CONNECTION_WIFI, !POWER_SAVE_MODE_ON, !METERED);
DeviceConditions deviceConditionsLowBattery = new DeviceConditions(!POWER_CONNECTED,
MINIMUM_BATTERY_LEVEL - 1, ConnectionType.CONNECTION_WIFI, !POWER_SAVE_MODE_ON,
!METERED, SCREEN_ON_AND_UNLOCKED);
ShadowDeviceConditions.setCurrentConditions(deviceConditionsLowBattery);
// Verify that conditions for processing are not met.
......@@ -159,9 +161,9 @@ public class OfflineBackgroundTaskTest {
@Feature({"OfflinePages"})
public void testCheckConditions_BatteryConditions_LowBattery_WithPower() {
// Set battery percentage below minimum level, but connect power.
DeviceConditions deviceConditionsPowerConnected =
new DeviceConditions(POWER_CONNECTED, MINIMUM_BATTERY_LEVEL - 1,
ConnectionType.CONNECTION_WIFI, !POWER_SAVE_MODE_ON, !METERED);
DeviceConditions deviceConditionsPowerConnected = new DeviceConditions(POWER_CONNECTED,
MINIMUM_BATTERY_LEVEL - 1, ConnectionType.CONNECTION_WIFI, !POWER_SAVE_MODE_ON,
!METERED, SCREEN_ON_AND_UNLOCKED);
ShadowDeviceConditions.setCurrentConditions(deviceConditionsPowerConnected);
// Now verify that same battery level, with power connected, will pass the conditions.
......
......@@ -195,10 +195,10 @@ public class OfflineNotificationBackgroundTaskUnitTest {
}
private void setupDeviceOnlineStatus(boolean online) {
DeviceConditions deviceConditions =
new DeviceConditions(false /* POWER_CONNECTED */, 75 /* BATTERY_LEVEL */,
online ? ConnectionType.CONNECTION_WIFI : ConnectionType.CONNECTION_NONE,
false /* POWER_SAVE */, false /* metered */);
DeviceConditions deviceConditions = new DeviceConditions(false /* POWER_CONNECTED */,
75 /* BATTERY_LEVEL */,
online ? ConnectionType.CONNECTION_WIFI : ConnectionType.CONNECTION_NONE,
false /* POWER_SAVE */, false /* metered */, true /* screenOnAndUnlocked */);
ShadowDeviceConditions.setCurrentConditions(deviceConditions);
}
......
......@@ -90,6 +90,7 @@ public class PrefetchBackgroundTaskUnitTest {
public static final int HIGH_BATTERY_LEVEL = 75;
public static final int LOW_BATTERY_LEVEL = 25;
public static final boolean METERED = true;
public static final boolean SCREEN_ON_AND_UNLOCKED = true;
@Spy
private PrefetchBackgroundTask mPrefetchBackgroundTask = new PrefetchBackgroundTask();
......@@ -193,9 +194,9 @@ public class PrefetchBackgroundTaskUnitTest {
TaskParameters.create(TaskIds.OFFLINE_PAGES_PREFETCH_JOB_ID).build();
// Setup battery conditions with no power connected.
DeviceConditions deviceConditions =
new DeviceConditions(!POWER_CONNECTED, HIGH_BATTERY_LEVEL - 1,
ConnectionType.CONNECTION_WIFI, !POWER_SAVE_MODE_ON, !METERED);
DeviceConditions deviceConditions = new DeviceConditions(!POWER_CONNECTED,
HIGH_BATTERY_LEVEL - 1, ConnectionType.CONNECTION_WIFI, !POWER_SAVE_MODE_ON,
!METERED, SCREEN_ON_AND_UNLOCKED);
ShadowDeviceConditions.setCurrentConditions(deviceConditions);
mPrefetchBackgroundTask.onStartTask(null, params, new TaskFinishedCallback() {
......@@ -225,7 +226,8 @@ public class PrefetchBackgroundTaskUnitTest {
// Setup battery conditions with no power connected.
DeviceConditions deviceConditions = new DeviceConditions(!POWER_CONNECTED,
0 /* battery level */, ConnectionType.CONNECTION_2G, !POWER_SAVE_MODE_ON, METERED);
0 /* battery level */, ConnectionType.CONNECTION_2G, !POWER_SAVE_MODE_ON, METERED,
SCREEN_ON_AND_UNLOCKED);
ShadowDeviceConditions.setCurrentConditions(deviceConditions);
mPrefetchBackgroundTask.onStartTask(null, params, new TaskFinishedCallback() {
......@@ -244,7 +246,8 @@ public class PrefetchBackgroundTaskUnitTest {
public void testBatteryLow() throws Exception {
// Setup low battery conditions with no power connected.
DeviceConditions deviceConditionsLowBattery = new DeviceConditions(!POWER_CONNECTED,
LOW_BATTERY_LEVEL, ConnectionType.CONNECTION_WIFI, !POWER_SAVE_MODE_ON, METERED);
LOW_BATTERY_LEVEL, ConnectionType.CONNECTION_WIFI, !POWER_SAVE_MODE_ON, METERED,
SCREEN_ON_AND_UNLOCKED);
ShadowDeviceConditions.setCurrentConditions(deviceConditionsLowBattery);
// Check impact on starting before native loaded.
......@@ -265,7 +268,8 @@ public class PrefetchBackgroundTaskUnitTest {
public void testBatteryHigh() throws Exception {
// Setup high battery conditions with no power connected.
DeviceConditions deviceConditionsHighBattery = new DeviceConditions(!POWER_CONNECTED,
HIGH_BATTERY_LEVEL, ConnectionType.CONNECTION_WIFI, !POWER_SAVE_MODE_ON, !METERED);
HIGH_BATTERY_LEVEL, ConnectionType.CONNECTION_WIFI, !POWER_SAVE_MODE_ON, !METERED,
SCREEN_ON_AND_UNLOCKED);
ShadowDeviceConditions.setCurrentConditions(deviceConditionsHighBattery);
// Check impact on starting before native loaded.
......@@ -286,7 +290,8 @@ public class PrefetchBackgroundTaskUnitTest {
public void testNoNetwork() throws Exception {
// Setup no network conditions.
DeviceConditions deviceConditionsNoNetwork = new DeviceConditions(!POWER_CONNECTED,
HIGH_BATTERY_LEVEL, ConnectionType.CONNECTION_NONE, !POWER_SAVE_MODE_ON, !METERED);
HIGH_BATTERY_LEVEL, ConnectionType.CONNECTION_NONE, !POWER_SAVE_MODE_ON, !METERED,
SCREEN_ON_AND_UNLOCKED);
ShadowDeviceConditions.setCurrentConditions(deviceConditionsNoNetwork);
// Check impact on starting before native loaded.
......@@ -310,9 +315,9 @@ public class PrefetchBackgroundTaskUnitTest {
@Test
public void testNoNetworkLimitless() throws Exception {
// Setup no network conditions.
DeviceConditions deviceConditionsNoNetwork =
new DeviceConditions(!POWER_CONNECTED, 0 /* battery level */,
ConnectionType.CONNECTION_NONE, !POWER_SAVE_MODE_ON, !METERED);
DeviceConditions deviceConditionsNoNetwork = new DeviceConditions(!POWER_CONNECTED,
0 /* battery level */, ConnectionType.CONNECTION_NONE, !POWER_SAVE_MODE_ON,
!METERED, SCREEN_ON_AND_UNLOCKED);
ShadowDeviceConditions.setCurrentConditions(deviceConditionsNoNetwork);
// Check impact on starting before native loaded.
......@@ -336,7 +341,8 @@ public class PrefetchBackgroundTaskUnitTest {
public void testUnmeteredWifiNetwork() throws Exception {
// Setup unmetered wifi conditions.
DeviceConditions deviceConditionsUnmeteredWifi = new DeviceConditions(!POWER_CONNECTED,
HIGH_BATTERY_LEVEL, ConnectionType.CONNECTION_WIFI, !POWER_SAVE_MODE_ON, !METERED);
HIGH_BATTERY_LEVEL, ConnectionType.CONNECTION_WIFI, !POWER_SAVE_MODE_ON, !METERED,
SCREEN_ON_AND_UNLOCKED);
ShadowDeviceConditions.setCurrentConditions(deviceConditionsUnmeteredWifi);
// Check impact on starting before native loaded.
......@@ -357,7 +363,8 @@ public class PrefetchBackgroundTaskUnitTest {
public void testMeteredWifiNetwork() throws Exception {
// Setup metered wifi conditions.
DeviceConditions deviceConditionsMeteredWifi = new DeviceConditions(!POWER_CONNECTED,
HIGH_BATTERY_LEVEL, ConnectionType.CONNECTION_WIFI, !POWER_SAVE_MODE_ON, METERED);
HIGH_BATTERY_LEVEL, ConnectionType.CONNECTION_WIFI, !POWER_SAVE_MODE_ON, METERED,
SCREEN_ON_AND_UNLOCKED);
ShadowDeviceConditions.setCurrentConditions(deviceConditionsMeteredWifi);
// Check impact on starting before native loaded.
......@@ -378,7 +385,8 @@ public class PrefetchBackgroundTaskUnitTest {
public void test2GNetwork() throws Exception {
// Setup metered 2g connection conditions.
DeviceConditions deviceConditions2G = new DeviceConditions(!POWER_CONNECTED,
HIGH_BATTERY_LEVEL, ConnectionType.CONNECTION_2G, !POWER_SAVE_MODE_ON, METERED);
HIGH_BATTERY_LEVEL, ConnectionType.CONNECTION_2G, !POWER_SAVE_MODE_ON, METERED,
SCREEN_ON_AND_UNLOCKED);
// TODO(petewil): this test name and the condition below do not match.
ShadowDeviceConditions.setCurrentConditions(deviceConditions2G);
......@@ -399,9 +407,9 @@ public class PrefetchBackgroundTaskUnitTest {
@Test
public void testBluetoothNetwork() throws Exception {
// Setup bluetooth connection conditions.
DeviceConditions deviceConditionsBluetooth =
new DeviceConditions(!POWER_CONNECTED, HIGH_BATTERY_LEVEL,
ConnectionType.CONNECTION_BLUETOOTH, !POWER_SAVE_MODE_ON, !METERED);
DeviceConditions deviceConditionsBluetooth = new DeviceConditions(!POWER_CONNECTED,
HIGH_BATTERY_LEVEL, ConnectionType.CONNECTION_BLUETOOTH, !POWER_SAVE_MODE_ON,
!METERED, SCREEN_ON_AND_UNLOCKED);
ShadowDeviceConditions.setCurrentConditions(deviceConditionsBluetooth);
// Check impact on starting before native loaded.
......@@ -425,9 +433,9 @@ public class PrefetchBackgroundTaskUnitTest {
TaskParameters.create(TaskIds.OFFLINE_PAGES_PREFETCH_JOB_ID).build();
// Conditions should be appropriate for running the task.
DeviceConditions deviceConditions =
new DeviceConditions(POWER_CONNECTED, HIGH_BATTERY_LEVEL - 1,
ConnectionType.CONNECTION_WIFI, !POWER_SAVE_MODE_ON, !METERED);
DeviceConditions deviceConditions = new DeviceConditions(POWER_CONNECTED,
HIGH_BATTERY_LEVEL - 1, ConnectionType.CONNECTION_WIFI, !POWER_SAVE_MODE_ON,
!METERED, SCREEN_ON_AND_UNLOCKED);
ShadowDeviceConditions.setCurrentConditions(deviceConditions);
mPrefetchBackgroundTask.onStartTask(null, params, new TaskFinishedCallback() {
......@@ -447,7 +455,8 @@ public class PrefetchBackgroundTaskUnitTest {
public void testPowerSaverOn() throws Exception {
// Setup power save mode, battery is high, wifi, not plugged in.
DeviceConditions deviceConditionsPowerSave = new DeviceConditions(!POWER_CONNECTED,
HIGH_BATTERY_LEVEL, ConnectionType.CONNECTION_WIFI, POWER_SAVE_MODE_ON, !METERED);
HIGH_BATTERY_LEVEL, ConnectionType.CONNECTION_WIFI, POWER_SAVE_MODE_ON, !METERED,
SCREEN_ON_AND_UNLOCKED);
ShadowDeviceConditions.setCurrentConditions(deviceConditionsPowerSave);
// Check impact on starting before native loaded.
......
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