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.car.settings.qc; 18 19 import static com.android.car.qc.QCItem.QC_ACTION_TOGGLE_STATE; 20 import static com.android.car.qc.QCItem.QC_TYPE_ACTION_SWITCH; 21 import static com.android.car.settings.qc.QCUtils.getActionDisabledDialogIntent; 22 import static com.android.car.settings.qc.QCUtils.getAvailabilityStatusForZoneFromXml; 23 import static com.android.car.settings.qc.SettingsQCRegistry.ADAPTIVE_BRIGHTNESS_SWITCH_URI; 24 25 import android.app.PendingIntent; 26 import android.content.Context; 27 import android.content.Intent; 28 import android.net.Uri; 29 import android.os.UserManager; 30 import android.provider.Settings; 31 32 import androidx.annotation.VisibleForTesting; 33 34 import com.android.car.qc.QCActionItem; 35 import com.android.car.qc.QCItem; 36 import com.android.car.qc.QCList; 37 import com.android.car.qc.QCRow; 38 import com.android.car.settings.R; 39 import com.android.car.settings.enterprise.EnterpriseUtils; 40 41 /** 42 * QCItem for toggling adaptive brightness. 43 */ 44 public class AdaptiveBrightnessSwitch extends SettingsQCItem { AdaptiveBrightnessSwitch(Context context)45 public AdaptiveBrightnessSwitch(Context context) { 46 super(context); 47 setAvailabilityStatusForZone(getAvailabilityStatusForZoneFromXml(context, 48 R.xml.display_settings_fragment, R.string.pk_adaptive_brightness_switch)); 49 } 50 51 @Override getQCItem()52 QCItem getQCItem() { 53 if (!supportsAdaptiveBrightness() || isHiddenForZone()) { 54 return null; 55 } 56 57 String userRestriction = UserManager.DISALLOW_CONFIG_BRIGHTNESS; 58 boolean hasDpmRestrictions = EnterpriseUtils.hasUserRestrictionByDpm(getContext(), 59 userRestriction); 60 boolean hasUmRestrictions = EnterpriseUtils.hasUserRestrictionByUm(getContext(), 61 userRestriction); 62 63 boolean isReadOnlyForZone = isReadOnlyForZone(); 64 PendingIntent disabledPendingIntent = isReadOnlyForZone 65 ? QCUtils.getDisabledToastBroadcastIntent(getContext()) 66 : getActionDisabledDialogIntent(getContext(), userRestriction); 67 68 QCActionItem actionItem = new QCActionItem.Builder(QC_TYPE_ACTION_SWITCH) 69 .setChecked(isAdaptiveBrightnessEnabled()) 70 .setAction(getBroadcastIntent()) 71 .setEnabled(!hasUmRestrictions && !hasDpmRestrictions && isWritableForZone()) 72 .setClickableWhileDisabled(hasDpmRestrictions || isReadOnlyForZone) 73 .setDisabledClickAction(disabledPendingIntent) 74 .setContentDescription(getContext(), R.string.qc_display_brightness) 75 .build(); 76 77 QCList.Builder listBuilder = new QCList.Builder() 78 .addRow(new QCRow.Builder() 79 .setTitle(getContext().getString(R.string.auto_brightness_title)) 80 .addEndItem(actionItem) 81 .build() 82 ); 83 return listBuilder.build(); 84 } 85 86 @Override getUri()87 Uri getUri() { 88 return ADAPTIVE_BRIGHTNESS_SWITCH_URI; 89 } 90 91 @Override onNotifyChange(Intent intent)92 void onNotifyChange(Intent intent) { 93 boolean newState = intent.getBooleanExtra(QC_ACTION_TOGGLE_STATE, 94 !isAdaptiveBrightnessEnabled()); 95 Settings.System.putInt(getContext().getContentResolver(), 96 Settings.System.SCREEN_BRIGHTNESS_MODE, 97 newState ? Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC 98 : Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); 99 } 100 isAdaptiveBrightnessEnabled()101 private boolean isAdaptiveBrightnessEnabled() { 102 int brightnessMode = Settings.System.getInt(getContext().getContentResolver(), 103 Settings.System.SCREEN_BRIGHTNESS_MODE, 104 Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); 105 return brightnessMode != Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL; 106 } 107 108 @VisibleForTesting supportsAdaptiveBrightness()109 boolean supportsAdaptiveBrightness() { 110 return getContext().getResources().getBoolean(R.bool.config_automatic_brightness_available); 111 } 112 } 113