1 /*
2  * Copyright (C) 2018 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.notification;
18 
19 import static android.app.NotificationManager.Policy.PRIORITY_CATEGORY_CALLS;
20 import static android.app.NotificationManager.Policy.PRIORITY_CATEGORY_MESSAGES;
21 import static android.app.NotificationManager.Policy.PRIORITY_SENDERS_STARRED;
22 
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.pm.PackageManager;
26 import android.provider.Contacts;
27 
28 import androidx.preference.Preference;
29 import androidx.preference.PreferenceScreen;
30 
31 import com.android.settingslib.core.lifecycle.Lifecycle;
32 
33 public class ZenModeStarredContactsPreferenceController extends
34         AbstractZenModePreferenceController implements Preference.OnPreferenceClickListener {
35     private Preference mPreference;
36     private final int mPriorityCategory;
37     private final PackageManager mPackageManager;
38 
39     private Intent mStarredContactsIntent;
40     private Intent mFallbackIntent;
41 
ZenModeStarredContactsPreferenceController(Context context, Lifecycle lifecycle, int priorityCategory, String key)42     public ZenModeStarredContactsPreferenceController(Context context, Lifecycle lifecycle, int
43             priorityCategory, String key) {
44         super(context, key, lifecycle);
45         mPriorityCategory = priorityCategory;
46         mPackageManager = mContext.getPackageManager();
47 
48         mStarredContactsIntent = new Intent(Contacts.Intents.UI.LIST_STARRED_ACTION);
49 
50         mFallbackIntent =  new Intent(Intent.ACTION_MAIN);
51         mFallbackIntent.addCategory(Intent.CATEGORY_APP_CONTACTS);
52     }
53 
54     @Override
displayPreference(PreferenceScreen screen)55     public void displayPreference(PreferenceScreen screen) {
56         super.displayPreference(screen);
57         mPreference = screen.findPreference(KEY);
58 
59         if (mPreference != null) {
60             mPreference.setOnPreferenceClickListener(this);
61         }
62     }
63 
64     @Override
getPreferenceKey()65     public String getPreferenceKey() {
66         return KEY;
67     }
68 
69     @Override
isAvailable()70     public boolean isAvailable() {
71         if (mPriorityCategory == PRIORITY_CATEGORY_CALLS) {
72             return mBackend.isPriorityCategoryEnabled(PRIORITY_CATEGORY_CALLS)
73                     && mBackend.getPriorityCallSenders() == PRIORITY_SENDERS_STARRED
74                     && isIntentValid();
75         } else if (mPriorityCategory == PRIORITY_CATEGORY_MESSAGES) {
76             return mBackend.isPriorityCategoryEnabled(PRIORITY_CATEGORY_MESSAGES)
77                     && mBackend.getPriorityMessageSenders() == PRIORITY_SENDERS_STARRED
78                     && isIntentValid();
79         } else {
80             // invalid category
81             return false;
82         }
83     }
84 
85     @Override
getSummary()86     public CharSequence getSummary() {
87         return mBackend.getStarredContactsSummary();
88     }
89 
90     @Override
onPreferenceClick(Preference preference)91     public boolean onPreferenceClick(Preference preference) {
92         if (mStarredContactsIntent.resolveActivity(mPackageManager) != null) {
93             mContext.startActivity(mStarredContactsIntent);
94         } else {
95             mContext.startActivity(mFallbackIntent);
96         }
97         return true;
98     }
99 
isIntentValid()100     private boolean isIntentValid() {
101         return mStarredContactsIntent.resolveActivity(mPackageManager) != null
102                 || mFallbackIntent.resolveActivity(mPackageManager) != null;
103     }
104 }
105