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 import android.text.TextUtils; 32 33 import androidx.slice.Slice; 34 import androidx.slice.builders.ListBuilder; 35 import androidx.slice.builders.SliceAction; 36 37 import com.android.settings.R; 38 import com.android.settings.Utils; 39 import com.android.settings.aware.AwareFeatureProvider; 40 import com.android.settings.overlay.FeatureFactory; 41 import com.android.settings.slices.CustomSliceRegistry; 42 import com.android.settings.slices.CustomSliceable; 43 44 import java.util.Arrays; 45 import java.util.Set; 46 import java.util.stream.Collectors; 47 48 /** 49 * Custom {@link Slice} for Always on Display. 50 * <p> 51 * We make a custom slice instead of using {@link AmbientDisplayAlwaysOnPreferenceController} 52 * because the controller will be unavailable if devices support aware sensor, and thus 53 * can not convert to slice. 54 * </p> 55 * 56 */ 57 public class AlwaysOnDisplaySlice implements CustomSliceable { 58 private static final int MY_USER = UserHandle.myUserId(); 59 60 private final Context mContext; 61 private final AmbientDisplayConfiguration mConfig; 62 private final AwareFeatureProvider mFeatureProvider; 63 AlwaysOnDisplaySlice(Context context)64 public AlwaysOnDisplaySlice(Context context) { 65 mContext = context; 66 mConfig = new AmbientDisplayConfiguration(mContext); 67 mFeatureProvider = FeatureFactory.getFactory(context).getAwareFeatureProvider(); 68 } 69 70 @Override getSlice()71 public Slice getSlice() { 72 if (!mConfig.alwaysOnAvailableForUser(MY_USER)) { 73 return null; 74 } 75 76 final PendingIntent toggleAction = getBroadcastIntent(mContext); 77 @ColorInt final int color = Utils.getColorAccentDefaultColor(mContext); 78 final boolean isChecked = mConfig.alwaysOnEnabled(MY_USER); 79 80 return new ListBuilder(mContext, CustomSliceRegistry.ALWAYS_ON_SLICE_URI, 81 ListBuilder.INFINITY) 82 .setAccentColor(color) 83 .setKeywords(getKeywords()) 84 .addRow(new ListBuilder.RowBuilder() 85 .setTitle(mContext.getText(R.string.doze_always_on_title)) 86 .setSubtitle(mContext.getText(R.string.doze_always_on_summary)) 87 .setPrimaryAction( 88 SliceAction.createToggle(toggleAction, null /* actionTitle */, 89 isChecked))) 90 .build(); 91 } 92 getKeywords()93 private Set<String> getKeywords() { 94 final String keywords = mContext.getString(R.string.keywords_always_show_time_info); 95 return Arrays.stream(TextUtils.split(keywords, ",")) 96 .map(String::trim) 97 .collect(Collectors.toSet()); 98 } 99 100 @Override getUri()101 public Uri getUri() { 102 return CustomSliceRegistry.ALWAYS_ON_SLICE_URI; 103 } 104 105 @Override onNotifyChange(Intent intent)106 public void onNotifyChange(Intent intent) { 107 final boolean isChecked = intent.getBooleanExtra(android.app.slice.Slice.EXTRA_TOGGLE_STATE, 108 false); 109 final ContentResolver resolver = mContext.getContentResolver(); 110 final boolean isAwareSupported = mFeatureProvider.isSupported(mContext); 111 final boolean isAwareEnabled = mFeatureProvider.isEnabled(mContext); 112 113 Settings.Secure.putInt(resolver, DOZE_ALWAYS_ON, isChecked ? 1 : 0); 114 Settings.Secure.putInt(resolver, DOZE_WAKE_DISPLAY_GESTURE, 115 (isAwareEnabled && isAwareSupported && isChecked) ? 1 : 0); 116 } 117 118 @Override getIntent()119 public Intent getIntent() { 120 return null; 121 } 122 123 @Override getSliceHighlightMenuRes()124 public int getSliceHighlightMenuRes() { 125 return R.string.menu_key_display; 126 } 127 } 128