1 /* 2 * Copyright (C) 2020 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 android.provider.Settings.Secure.DOZE_ALWAYS_ON; 20 import static android.provider.Settings.Secure.DOZE_WAKE_DISPLAY_GESTURE; 21 22 import android.annotation.ColorInt; 23 import android.app.PendingIntent; 24 import android.content.ContentResolver; 25 import android.content.Context; 26 import android.content.Intent; 27 import android.hardware.display.AmbientDisplayConfiguration; 28 import android.net.Uri; 29 import android.os.UserHandle; 30 import android.provider.Settings; 31 32 import androidx.slice.Slice; 33 import androidx.slice.builders.ListBuilder; 34 import androidx.slice.builders.SliceAction; 35 36 import com.android.settings.R; 37 import com.android.settings.Utils; 38 import com.android.settings.aware.AwareFeatureProvider; 39 import com.android.settings.overlay.FeatureFactory; 40 import com.android.settings.slices.CustomSliceRegistry; 41 import com.android.settings.slices.CustomSliceable; 42 43 /** 44 * Custom {@link Slice} for Always on Display. 45 * <p> 46 * We make a custom slice instead of using {@link AmbientDisplayAlwaysOnPreferenceController} 47 * because the controller will be unavailable if devices support aware sensor, and thus 48 * can not convert to slice. 49 * </p> 50 * 51 */ 52 public class AlwaysOnDisplaySlice implements CustomSliceable { 53 private static final int MY_USER = UserHandle.myUserId(); 54 55 private final Context mContext; 56 private final AmbientDisplayConfiguration mConfig; 57 private final AwareFeatureProvider mFeatureProvider; 58 AlwaysOnDisplaySlice(Context context)59 public AlwaysOnDisplaySlice(Context context) { 60 mContext = context; 61 mConfig = new AmbientDisplayConfiguration(mContext); 62 mFeatureProvider = FeatureFactory.getFactory(context).getAwareFeatureProvider(); 63 } 64 65 @Override getSlice()66 public Slice getSlice() { 67 if (!mConfig.alwaysOnAvailableForUser(MY_USER)) { 68 return null; 69 } 70 71 final PendingIntent toggleAction = getBroadcastIntent(mContext); 72 @ColorInt final int color = Utils.getColorAccentDefaultColor(mContext); 73 final boolean isChecked = mConfig.alwaysOnEnabled(MY_USER); 74 75 return new ListBuilder(mContext, CustomSliceRegistry.ALWAYS_ON_SLICE_URI, 76 ListBuilder.INFINITY) 77 .setAccentColor(color) 78 .addRow(new ListBuilder.RowBuilder() 79 .setTitle(mContext.getText(R.string.doze_always_on_title)) 80 .setSubtitle(mContext.getText(R.string.doze_always_on_summary)) 81 .setPrimaryAction( 82 SliceAction.createToggle(toggleAction, null /* actionTitle */, 83 isChecked))) 84 .build(); 85 } 86 87 @Override getUri()88 public Uri getUri() { 89 return CustomSliceRegistry.ALWAYS_ON_SLICE_URI; 90 } 91 92 @Override onNotifyChange(Intent intent)93 public void onNotifyChange(Intent intent) { 94 final boolean isChecked = intent.getBooleanExtra(android.app.slice.Slice.EXTRA_TOGGLE_STATE, 95 false); 96 final ContentResolver resolver = mContext.getContentResolver(); 97 final boolean isAwareSupported = mFeatureProvider.isSupported(mContext); 98 final boolean isAwareEnabled = mFeatureProvider.isEnabled(mContext); 99 100 Settings.Secure.putInt(resolver, DOZE_ALWAYS_ON, isChecked ? 1 : 0); 101 Settings.Secure.putInt(resolver, DOZE_WAKE_DISPLAY_GESTURE, 102 (isAwareEnabled && isAwareSupported && isChecked) ? 1 : 0); 103 } 104 105 @Override getIntent()106 public Intent getIntent() { 107 return null; 108 } 109 } 110