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