Merge "Add warning message when power save mode is enabled for smart auto rotate settings fragment" into sc-dev

This commit is contained in:
Abel Tesfaye
2021-05-12 19:27:51 +00:00
committed by Android (Google) Code Review
7 changed files with 232 additions and 7 deletions

View File

@@ -11364,6 +11364,9 @@
<!-- [CHAR LIMIT=60] Summary string on dark theme explaining why the toggle is disabled while the setting is still on-->
<string name="dark_ui_mode_disabled_summary_dark_theme_on">Temporarily disabled due to Battery Saver</string>
<!-- [CHAR LIMIT=60] Summary string for screen attention explaining why the toggle is disabled by battery saver-->
<string name="ambient_camera_summary_battery_saver_on">Temporarily disabled due to Battery Saver</string>
<!-- [CHAR LIMIT=60] Summary string on dark theme explaining why the toggle is disabled while the setting is off-->
<string name="dark_ui_mode_disabled_summary_dark_theme_off">Temporarily turned on due to Battery Saver</string>

View File

@@ -32,6 +32,11 @@
android:summary="@string/auto_rotate_camera_lock_summary"
settings:controller="com.android.settings.display.SmartAutoRotateCameraStateController" />
<com.android.settingslib.widget.BannerMessagePreference
android:key="camera_battery_saver_state"
android:title="@string/ambient_camera_summary_battery_saver_on"
settings:controller="com.android.settings.display.SmartAutoRotateBatterySaverController" />
<SwitchPreference
android:key="face_based_rotate"
android:title="@string/auto_rotate_switch_face_based"

View File

@@ -0,0 +1,96 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.display;
import static com.android.settings.display.SmartAutoRotateController.isRotationResolverServiceAvailable;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.PowerManager;
import androidx.annotation.VisibleForTesting;
import androidx.lifecycle.LifecycleObserver;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.core.lifecycle.events.OnStart;
import com.android.settingslib.core.lifecycle.events.OnStop;
import com.android.settingslib.widget.BannerMessagePreference;
/**
* The controller of camera based rotate battery saver warning preference. The preference appears
* when battery saver mode is enabled.
*/
public class SmartAutoRotateBatterySaverController extends BasePreferenceController implements
LifecycleObserver, OnStart, OnStop {
private Preference mPreference;
private final PowerManager mPowerManager;
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (mPreference == null) {
return;
}
mPreference.setVisible(isPowerSaveMode());
updateState(mPreference);
}
};
public SmartAutoRotateBatterySaverController(Context context, String key) {
super(context, key);
mPowerManager = context.getSystemService(PowerManager.class);
}
@VisibleForTesting
boolean isPowerSaveMode() {
return mPowerManager.isPowerSaveMode();
}
@Override
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreference = screen.findPreference(getPreferenceKey());
((BannerMessagePreference) mPreference)
.setPositiveButtonText(R.string.disable_text)
.setPositiveButtonOnClickListener(v -> {
mPowerManager.setPowerSaveModeEnabled(false);
});
}
@Override
public void onStart() {
mContext.registerReceiver(mReceiver,
new IntentFilter(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED));
}
@Override
public void onStop() {
mContext.unregisterReceiver(mReceiver);
}
@Override
@AvailabilityStatus
public int getAvailabilityStatus() {
return isRotationResolverServiceAvailable(mContext)
&& isPowerSaveMode() ? AVAILABLE_UNSEARCHABLE : UNSUPPORTED_ON_DEVICE;
}
}

View File

@@ -20,15 +20,19 @@ import static android.provider.Settings.Secure.CAMERA_AUTOROTATE;
import android.Manifest;
import android.app.settings.SettingsEnums;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.hardware.SensorPrivacyManager;
import android.os.PowerManager;
import android.provider.Settings;
import android.service.rotationresolver.RotationResolverService;
import android.text.TextUtils;
import androidx.lifecycle.LifecycleObserver;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
@@ -37,15 +41,24 @@ import com.android.internal.view.RotationPolicy;
import com.android.settings.core.TogglePreferenceController;
import com.android.settings.overlay.FeatureFactory;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
import com.android.settingslib.core.lifecycle.events.OnStart;
import com.android.settingslib.core.lifecycle.events.OnStop;
/**
* SmartAutoRotateController controls whether auto rotation is enabled
*/
public class SmartAutoRotateController extends TogglePreferenceController implements
Preference.OnPreferenceChangeListener {
Preference.OnPreferenceChangeListener, LifecycleObserver, OnStart, OnStop {
private final MetricsFeatureProvider mMetricsFeatureProvider;
private final SensorPrivacyManager mPrivacyManager;
private final PowerManager mPowerManager;
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
updateState(mPreference);
}
};
private Preference mPreference;
public SmartAutoRotateController(Context context, String preferenceKey) {
@@ -54,6 +67,7 @@ public class SmartAutoRotateController extends TogglePreferenceController implem
mPrivacyManager = SensorPrivacyManager.getInstance(context);
mPrivacyManager
.addSensorPrivacyListener(CAMERA, (sensor, enabled) -> updateState(mPreference));
mPowerManager = context.getSystemService(PowerManager.class);
}
@Override
@@ -62,7 +76,7 @@ public class SmartAutoRotateController extends TogglePreferenceController implem
return UNSUPPORTED_ON_DEVICE;
}
return !RotationPolicy.isRotationLocked(mContext) && hasSufficientPermission(mContext)
&& !isCameraLocked() ? AVAILABLE : DISABLED_DEPENDENT_SETTING;
&& !isCameraLocked() && !isPowerSaveMode() ? AVAILABLE : DISABLED_DEPENDENT_SETTING;
}
@Override
@@ -82,10 +96,26 @@ public class SmartAutoRotateController extends TogglePreferenceController implem
return mPrivacyManager.isSensorPrivacyEnabled(SensorPrivacyManager.Sensors.CAMERA);
}
@VisibleForTesting
boolean isPowerSaveMode() {
return mPowerManager.isPowerSaveMode();
}
@Override
public void onStart() {
mContext.registerReceiver(mReceiver,
new IntentFilter(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED));
}
@Override
public void onStop() {
mContext.unregisterReceiver(mReceiver);
}
@Override
public boolean isChecked() {
return hasSufficientPermission(mContext) && !isCameraLocked() && Settings.Secure.getInt(
mContext.getContentResolver(),
return hasSufficientPermission(mContext) && !isCameraLocked() && !isPowerSaveMode()
&& Settings.Secure.getInt(mContext.getContentResolver(),
CAMERA_AUTOROTATE, 0) == 1;
}

View File

@@ -21,6 +21,7 @@ import static com.android.settings.display.SmartAutoRotateController.isRotationR
import android.app.settings.SettingsEnums;
import android.hardware.SensorPrivacyManager;
import android.os.Bundle;
import android.os.PowerManager;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
@@ -50,6 +51,7 @@ public class SmartAutoRotatePreferenceFragment extends DashboardFragment {
private RotationPolicy.RotationPolicyListener mRotationPolicyListener;
private SensorPrivacyManager mPrivacyManager;
private AutoRotateSwitchBarController mSwitchBarController;
private PowerManager mPowerManager;
private static final String FACE_SWITCH_PREFERENCE_ID = "face_based_rotate";
@Override
@@ -69,6 +71,7 @@ public class SmartAutoRotatePreferenceFragment extends DashboardFragment {
mSwitchBarController = new AutoRotateSwitchBarController(activity, switchBar,
getSettingsLifecycle());
mPrivacyManager = SensorPrivacyManager.getInstance(activity);
mPowerManager = getSystemService(PowerManager.class);
final Preference footerPreference = findPreference(FooterPreference.KEY_FOOTER);
if (footerPreference != null) {
footerPreference.setTitle(Html.fromHtml(getString(R.string.smart_rotate_text_headline),
@@ -89,9 +92,10 @@ public class SmartAutoRotatePreferenceFragment extends DashboardFragment {
final boolean isLocked = RotationPolicy.isRotationLocked(getContext());
final boolean isCameraLocked = mPrivacyManager.isSensorPrivacyEnabled(
SensorPrivacyManager.Sensors.CAMERA);
final boolean isBatterySaver = mPowerManager.isPowerSaveMode();
final Preference preference = findPreference(FACE_SWITCH_PREFERENCE_ID);
if (preference != null && hasSufficientPermission(getContext())) {
preference.setEnabled(!isLocked && !isCameraLocked);
preference.setEnabled(!isLocked && !isCameraLocked && !isBatterySaver);
}
}
};

View File

@@ -0,0 +1,80 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.display;
import static com.android.settings.core.BasePreferenceController.AVAILABLE_UNSEARCHABLE;
import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.when;
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo;
import com.android.settings.testutils.ResolveInfoBuilder;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
public class SmartAutoRotateBatterySaverControllerTest {
private static final String PACKAGE_NAME = "package_name";
private SmartAutoRotateBatterySaverController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
final Context context = Mockito.spy(RuntimeEnvironment.application);
final ContentResolver contentResolver = RuntimeEnvironment.application.getContentResolver();
when(context.getContentResolver()).thenReturn(contentResolver);
final PackageManager packageManager = Mockito.mock(PackageManager.class);
when(context.getPackageManager()).thenReturn(packageManager);
doReturn(PACKAGE_NAME).when(packageManager).getRotationResolverPackageName();
mController = Mockito.spy(
new SmartAutoRotateBatterySaverController(context, "smart_auto_rotate"));
when(mController.isPowerSaveMode()).thenReturn(false);
final ResolveInfo resolveInfo = new ResolveInfoBuilder(PACKAGE_NAME).build();
resolveInfo.serviceInfo = new ServiceInfo();
when(packageManager.resolveService(any(), anyInt())).thenReturn(resolveInfo);
}
@Test
public void getAvailabilityStatus_returnUnsupportedOnDevice() {
assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
}
@Test
public void getAvailabilityStatus_powerSaveModeEnabled_returnAvailableUnSearchAble() {
when(mController.isPowerSaveMode()).thenReturn(true);
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE_UNSEARCHABLE);
}
}

View File

@@ -74,8 +74,9 @@ public class SmartAutoRotateControllerTest {
doReturn(PACKAGE_NAME).when(mPackageManager).getRotationResolverPackageName();
doReturn(PackageManager.PERMISSION_GRANTED).when(mPackageManager).checkPermission(
Manifest.permission.CAMERA, PACKAGE_NAME);
mController = new SmartAutoRotateController(context, "test_key");
mController = Mockito.spy(new SmartAutoRotateController(context, "test_key"));
when(mController.isCameraLocked()).thenReturn(false);
when(mController.isPowerSaveMode()).thenReturn(false);
doReturn(mController.getPreferenceKey()).when(mPreference).getKey();
final ResolveInfo resolveInfo = new ResolveInfoBuilder(PACKAGE_NAME).build();
@@ -115,6 +116,12 @@ public class SmartAutoRotateControllerTest {
assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_DEPENDENT_SETTING);
}
@Test
public void getAvailabilityStatus_powerSaveEnabled_returnDisableDependentSetting() {
when(mController.isPowerSaveMode()).thenReturn(true);
assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_DEPENDENT_SETTING);
}
private void enableAutoRotation() {
Settings.System.putIntForUser(mContentResolver,
Settings.System.ACCELEROMETER_ROTATION, 1, UserHandle.USER_CURRENT);