1 /* 2 * Copyright (C) 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.display; 18 19 import static androidx.lifecycle.Lifecycle.Event.ON_START; 20 import static androidx.lifecycle.Lifecycle.Event.ON_STOP; 21 22 import static com.android.settings.display.SmartAutoRotateController.isRotationResolverServiceAvailable; 23 24 import android.content.BroadcastReceiver; 25 import android.content.Context; 26 import android.content.Intent; 27 import android.content.IntentFilter; 28 import android.os.PowerManager; 29 30 import androidx.annotation.VisibleForTesting; 31 import androidx.lifecycle.LifecycleObserver; 32 import androidx.lifecycle.OnLifecycleEvent; 33 import androidx.preference.Preference; 34 import androidx.preference.PreferenceScreen; 35 36 import com.android.settings.R; 37 import com.android.settings.core.BasePreferenceController; 38 import com.android.settingslib.widget.BannerMessagePreference; 39 40 /** 41 * The controller of camera based rotate battery saver warning preference. The preference appears 42 * when battery saver mode is enabled. 43 */ 44 public class SmartAutoRotateBatterySaverController extends BasePreferenceController implements 45 LifecycleObserver { 46 47 private Preference mPreference; 48 private final PowerManager mPowerManager; 49 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 50 @Override 51 public void onReceive(Context context, Intent intent) { 52 if (mPreference == null) { 53 return; 54 } 55 mPreference.setVisible(isAvailable()); 56 updateState(mPreference); 57 } 58 }; 59 SmartAutoRotateBatterySaverController(Context context, String key)60 public SmartAutoRotateBatterySaverController(Context context, String key) { 61 super(context, key); 62 mPowerManager = context.getSystemService(PowerManager.class); 63 } 64 65 @VisibleForTesting isPowerSaveMode()66 boolean isPowerSaveMode() { 67 return mPowerManager.isPowerSaveMode(); 68 } 69 70 @Override displayPreference(PreferenceScreen screen)71 public void displayPreference(PreferenceScreen screen) { 72 super.displayPreference(screen); 73 mPreference = screen.findPreference(getPreferenceKey()); 74 ((BannerMessagePreference) mPreference) 75 .setPositiveButtonText(R.string.ambient_camera_battery_saver_off) 76 .setPositiveButtonOnClickListener(v -> { 77 mPowerManager.setPowerSaveModeEnabled(false); 78 }); 79 } 80 81 @OnLifecycleEvent(ON_START) onStart()82 public void onStart() { 83 mContext.registerReceiver(mReceiver, 84 new IntentFilter(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED)); 85 } 86 87 @OnLifecycleEvent(ON_STOP) onStop()88 public void onStop() { 89 mContext.unregisterReceiver(mReceiver); 90 } 91 92 @Override 93 @AvailabilityStatus getAvailabilityStatus()94 public int getAvailabilityStatus() { 95 return isRotationResolverServiceAvailable(mContext) 96 && isPowerSaveMode() ? AVAILABLE_UNSEARCHABLE : UNSUPPORTED_ON_DEVICE; 97 } 98 } 99