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