1 /* 2 * Copyright (C) 2018 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.car.settings.display; 18 19 import static android.os.UserManager.DISALLOW_CONFIG_BRIGHTNESS; 20 21 import static com.android.car.settings.enterprise.ActionDisabledByAdminDialogFragment.DISABLED_BY_ADMIN_CONFIRM_DIALOG_TAG; 22 import static com.android.car.settings.enterprise.EnterpriseUtils.hasUserRestrictionByDpm; 23 import static com.android.car.settings.enterprise.EnterpriseUtils.hasUserRestrictionByUm; 24 25 import android.car.drivingstate.CarUxRestrictions; 26 import android.content.Context; 27 import android.database.ContentObserver; 28 import android.net.Uri; 29 import android.os.Handler; 30 import android.os.Looper; 31 import android.provider.Settings; 32 import android.widget.Toast; 33 34 import androidx.preference.TwoStatePreference; 35 36 import com.android.car.settings.R; 37 import com.android.car.settings.common.FragmentController; 38 import com.android.car.settings.common.Logger; 39 import com.android.car.settings.common.PreferenceController; 40 import com.android.car.settings.enterprise.EnterpriseUtils; 41 42 /** Business logic for controlling the adaptive brightness setting. */ 43 public class AdaptiveBrightnessTogglePreferenceController extends 44 PreferenceController<TwoStatePreference> { 45 46 private static final Logger LOG = 47 new Logger(AdaptiveBrightnessTogglePreferenceController.class); 48 private static final Uri ADAPTIVE_BRIGHTNESS_URI = Settings.System.getUriFor( 49 Settings.System.SCREEN_BRIGHTNESS_MODE); 50 private final Handler mHandler = new Handler(Looper.getMainLooper()); 51 private final ContentObserver mAdaptiveBrightnessObserver = new ContentObserver(mHandler) { 52 @Override 53 public void onChange(boolean selfChange) { 54 refreshUi(); 55 } 56 }; 57 AdaptiveBrightnessTogglePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)58 public AdaptiveBrightnessTogglePreferenceController(Context context, String preferenceKey, 59 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 60 super(context, preferenceKey, fragmentController, uxRestrictions); 61 } 62 63 @Override getPreferenceType()64 protected Class<TwoStatePreference> getPreferenceType() { 65 return TwoStatePreference.class; 66 } 67 68 @Override onCreateInternal()69 protected void onCreateInternal() { 70 super.onCreateInternal(); 71 setClickableWhileDisabled(getPreference(), /* clickable= */ true, p -> { 72 if (hasUserRestrictionByDpm(getContext(), DISALLOW_CONFIG_BRIGHTNESS)) { 73 showActionDisabledByAdminDialog(); 74 } else { 75 Toast.makeText(getContext(), 76 getContext().getString(R.string.action_unavailable), 77 Toast.LENGTH_LONG).show(); 78 LOG.d(getContext().getString(R.string.action_unavailable)); 79 } 80 }); 81 } 82 83 @Override onStartInternal()84 protected void onStartInternal() { 85 super.onStartInternal(); 86 getContext().getContentResolver().registerContentObserver(ADAPTIVE_BRIGHTNESS_URI, 87 /* notifyForDescendants= */ false, mAdaptiveBrightnessObserver); 88 } 89 90 @Override onStopInternal()91 protected void onStopInternal() { 92 super.onStopInternal(); 93 getContext().getContentResolver().unregisterContentObserver(mAdaptiveBrightnessObserver); 94 } 95 96 @Override updateState(TwoStatePreference preference)97 protected void updateState(TwoStatePreference preference) { 98 preference.setChecked(isAdaptiveBrightnessChecked()); 99 } 100 101 @Override getDefaultAvailabilityStatus()102 public int getDefaultAvailabilityStatus() { 103 if (!supportsAdaptiveBrightness()) { 104 return UNSUPPORTED_ON_DEVICE; 105 } 106 if (hasUserRestrictionByUm(getContext(), DISALLOW_CONFIG_BRIGHTNESS) 107 || hasUserRestrictionByDpm(getContext(), DISALLOW_CONFIG_BRIGHTNESS)) { 108 return AVAILABLE_FOR_VIEWING; 109 } 110 return AVAILABLE; 111 } 112 113 @Override handlePreferenceChanged(TwoStatePreference preference, Object newValue)114 protected boolean handlePreferenceChanged(TwoStatePreference preference, Object newValue) { 115 boolean enableAdaptiveBrightness = (boolean) newValue; 116 Settings.System.putInt(getContext().getContentResolver(), 117 Settings.System.SCREEN_BRIGHTNESS_MODE, 118 enableAdaptiveBrightness ? Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC 119 : Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); 120 return true; 121 } 122 isAdaptiveBrightnessChecked()123 private boolean isAdaptiveBrightnessChecked() { 124 int brightnessMode = Settings.System.getInt(getContext().getContentResolver(), 125 Settings.System.SCREEN_BRIGHTNESS_MODE, 126 Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); 127 return brightnessMode != Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL; 128 } 129 supportsAdaptiveBrightness()130 private boolean supportsAdaptiveBrightness() { 131 return getContext().getResources().getBoolean(R.bool.config_automatic_brightness_available); 132 } 133 showActionDisabledByAdminDialog()134 private void showActionDisabledByAdminDialog() { 135 getFragmentController().showDialog( 136 EnterpriseUtils.getActionDisabledByAdminDialog(getContext(), 137 DISALLOW_CONFIG_BRIGHTNESS), 138 DISABLED_BY_ADMIN_CONFIRM_DIALOG_TAG); 139 } 140 } 141