• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.homepage.contextualcards.conditional;
18 
19 import android.Manifest;
20 import android.app.settings.SettingsEnums;
21 import android.content.BroadcastReceiver;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.IntentFilter;
25 import android.hardware.display.ColorDisplayManager;
26 import android.os.UserHandle;
27 import android.util.Log;
28 
29 import com.android.settings.R;
30 import com.android.settings.homepage.contextualcards.ContextualCard;
31 
32 import java.net.URISyntaxException;
33 import java.util.Objects;
34 
35 public class GrayscaleConditionController implements ConditionalCardController {
36     static final int ID = Objects.hash("GrayscaleConditionController");
37 
38     private static final String TAG = "GrayscaleCondition";
39     private static final String ACTION_GRAYSCALE_CHANGED =
40             "android.settings.action.GRAYSCALE_CHANGED";
41     private static final IntentFilter GRAYSCALE_CHANGED_FILTER = new IntentFilter(
42             ACTION_GRAYSCALE_CHANGED);
43 
44     private final Context mAppContext;
45     private final ConditionManager mConditionManager;
46     private final ColorDisplayManager mColorDisplayManager;
47     private final Receiver mReceiver;
48 
49     private Intent mIntent;
50 
GrayscaleConditionController(Context appContext, ConditionManager conditionManager)51     public GrayscaleConditionController(Context appContext, ConditionManager conditionManager) {
52         mAppContext = appContext;
53         mConditionManager = conditionManager;
54         mColorDisplayManager = mAppContext.getSystemService(ColorDisplayManager.class);
55         mReceiver = new Receiver();
56     }
57 
58     @Override
getId()59     public long getId() {
60         return ID;
61     }
62 
63     @Override
isDisplayable()64     public boolean isDisplayable() {
65         try {
66             mIntent = Intent.parseUri(
67                     mAppContext.getString(R.string.config_grayscale_settings_intent),
68                     Intent.URI_INTENT_SCHEME);
69         } catch (URISyntaxException e) {
70             Log.w(TAG, "Failure parsing grayscale settings intent, skipping", e);
71             return false;
72         }
73         return mColorDisplayManager.isSaturationActivated();
74     }
75 
76     @Override
onPrimaryClick(Context context)77     public void onPrimaryClick(Context context) {
78         mAppContext.startActivity(mIntent);
79     }
80 
81     @Override
onActionClick()82     public void onActionClick() {
83         // Turn off grayscale
84         mColorDisplayManager.setSaturationLevel(100 /* staturationLevel */);
85         sendBroadcast();
86         mConditionManager.onConditionChanged();
87     }
88 
89     @Override
buildContextualCard()90     public ContextualCard buildContextualCard() {
91         return new ConditionalContextualCard.Builder()
92                 .setConditionId(ID)
93                 .setMetricsConstant(SettingsEnums.SETTINGS_CONDITION_GRAYSCALE_MODE)
94                 .setActionText(mAppContext.getText(R.string.condition_turn_off))
95                 .setName(mAppContext.getPackageName() + "/" + mAppContext.getText(
96                         R.string.condition_grayscale_title))
97                 .setTitleText(mAppContext.getText(R.string.condition_grayscale_title).toString())
98                 .setSummaryText(
99                         mAppContext.getText(R.string.condition_grayscale_summary).toString())
100                 .setIconDrawable(mAppContext.getDrawable(R.drawable.ic_gray_scale_24dp))
101                 .setViewType(ConditionContextualCardRenderer.VIEW_TYPE_HALF_WIDTH)
102                 .build();
103     }
104 
105     @Override
startMonitoringStateChange()106     public void startMonitoringStateChange() {
107         mAppContext.registerReceiver(mReceiver, GRAYSCALE_CHANGED_FILTER,
108                 Manifest.permission.CONTROL_DISPLAY_COLOR_TRANSFORMS, null /* scheduler */);
109     }
110 
111     @Override
stopMonitoringStateChange()112     public void stopMonitoringStateChange() {
113         mAppContext.unregisterReceiver(mReceiver);
114     }
115 
sendBroadcast()116     private void sendBroadcast() {
117         final Intent intent = new Intent(ACTION_GRAYSCALE_CHANGED);
118         intent.addFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
119         mAppContext.sendBroadcastAsUser(intent, UserHandle.CURRENT,
120                 Manifest.permission.CONTROL_DISPLAY_COLOR_TRANSFORMS);
121     }
122 
123     public class Receiver extends BroadcastReceiver {
124         @Override
onReceive(Context context, Intent intent)125         public void onReceive(Context context, Intent intent) {
126             if (ACTION_GRAYSCALE_CHANGED.equals(intent.getAction())) {
127                 mConditionManager.onConditionChanged();
128             }
129         }
130     }
131 }
132