• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.database.Cursor;
27 import android.icu.text.ListFormatter;
28 import android.provider.Contacts;
29 import android.provider.ContactsContract;
30 import android.support.v7.preference.Preference;
31 import android.support.v7.preference.PreferenceScreen;
32 
33 import com.android.internal.annotations.VisibleForTesting;
34 import com.android.settings.R;
35 import com.android.settingslib.core.lifecycle.Lifecycle;
36 
37 import java.util.ArrayList;
38 import java.util.List;
39 
40 public class ZenModeStarredContactsPreferenceController extends
41         AbstractZenModePreferenceController implements Preference.OnPreferenceClickListener {
42 
43     protected static final String KEY = "zen_mode_starred_contacts";
44     private Preference mPreference;
45     private final int mPriorityCategory;
46     private final PackageManager mPackageManager;
47 
48     @VisibleForTesting
49     Intent mStarredContactsIntent;
50     @VisibleForTesting
51     Intent mFallbackIntent;
52 
ZenModeStarredContactsPreferenceController(Context context, Lifecycle lifecycle, int priorityCategory)53     public ZenModeStarredContactsPreferenceController(Context context, Lifecycle lifecycle, int
54             priorityCategory) {
55         super(context, KEY, lifecycle);
56         mPriorityCategory = priorityCategory;
57         mPackageManager = mContext.getPackageManager();
58 
59         mStarredContactsIntent = new Intent(Contacts.Intents.UI.LIST_STARRED_ACTION);
60 
61         mFallbackIntent =  new Intent(Intent.ACTION_MAIN);
62         mFallbackIntent.addCategory(Intent.CATEGORY_APP_CONTACTS);
63     }
64 
65     @Override
displayPreference(PreferenceScreen screen)66     public void displayPreference(PreferenceScreen screen) {
67         super.displayPreference(screen);
68         mPreference = screen.findPreference(KEY);
69         mPreference.setOnPreferenceClickListener(this);
70     }
71 
72     @Override
getPreferenceKey()73     public String getPreferenceKey() {
74         return KEY;
75     }
76 
77     @Override
isAvailable()78     public boolean isAvailable() {
79         if (mPriorityCategory == PRIORITY_CATEGORY_CALLS) {
80             return mBackend.isPriorityCategoryEnabled(PRIORITY_CATEGORY_CALLS)
81                     && mBackend.getPriorityCallSenders() == PRIORITY_SENDERS_STARRED
82                     && isIntentValid();
83         } else if (mPriorityCategory == PRIORITY_CATEGORY_MESSAGES) {
84             return mBackend.isPriorityCategoryEnabled(PRIORITY_CATEGORY_MESSAGES)
85                     && mBackend.getPriorityMessageSenders() == PRIORITY_SENDERS_STARRED
86                     && isIntentValid();
87         } else {
88             // invalid category
89             return false;
90         }
91     }
92 
93     @Override
updateState(Preference preference)94     public void updateState(Preference preference) {
95         super.updateState(preference);
96 
97         List<String> starredContacts = getStarredContacts();
98         int numStarredContacts = starredContacts.size();
99 
100         List<String> displayContacts = new ArrayList<>();
101 
102         if (numStarredContacts == 0) {
103             displayContacts.add(mContext.getString(R.string.zen_mode_from_none));
104         } else {
105             for (int i = 0; i < 2 && i < numStarredContacts; i++) {
106                 displayContacts.add(starredContacts.get(i));
107             }
108 
109             if (numStarredContacts == 3) {
110                 displayContacts.add(starredContacts.get(2));
111             } else if (numStarredContacts > 2) {
112                 displayContacts.add(mContext.getResources().getQuantityString(
113                         R.plurals.zen_mode_starred_contacts_summary_additional_contacts,
114                         numStarredContacts - 2, numStarredContacts - 2));
115             }
116         }
117 
118         mPreference.setSummary(ListFormatter.getInstance().format(displayContacts));
119     }
120 
121     @Override
onPreferenceClick(Preference preference)122     public boolean onPreferenceClick(Preference preference) {
123         if (mStarredContactsIntent.resolveActivity(mPackageManager) != null) {
124             mContext.startActivity(mStarredContactsIntent);
125         } else {
126             mContext.startActivity(mFallbackIntent);
127         }
128         return true;
129     }
130 
getStarredContacts()131     private List<String> getStarredContacts() {
132         List<String> starredContacts = new ArrayList<>();
133 
134         Cursor cursor = mContext.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
135                 new String[]{ContactsContract.Contacts.DISPLAY_NAME_PRIMARY},
136                 ContactsContract.Data.STARRED + "=1", null,
137                 ContactsContract.Data.TIMES_CONTACTED);
138 
139         if (cursor.moveToFirst()) {
140             do {
141                 starredContacts.add(cursor.getString(0));
142             } while (cursor.moveToNext());
143         }
144         return starredContacts;
145     }
146 
isIntentValid()147     private boolean isIntentValid() {
148         return mStarredContactsIntent.resolveActivity(mPackageManager) != null
149                 || mFallbackIntent.resolveActivity(mPackageManager) != null;
150     }
151 }
152