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