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 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 48 49 mFallbackIntent = new Intent(Intent.ACTION_MAIN); 50 mFallbackIntent.addCategory(Intent.CATEGORY_APP_CONTACTS); 51 mFallbackIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 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 (!super.isAvailable() || mRule.getZenPolicy() == null || !isIntentValid()) { 72 return false; 73 } 74 75 if (mPriorityCategory == ZenPolicy.PRIORITY_CATEGORY_CALLS) { 76 return mRule.getZenPolicy().getPriorityCallSenders() == ZenPolicy.PEOPLE_TYPE_STARRED; 77 } else if (mPriorityCategory == ZenPolicy.PRIORITY_CATEGORY_MESSAGES) { 78 return mRule.getZenPolicy().getPriorityMessageSenders() 79 == ZenPolicy.PEOPLE_TYPE_STARRED; 80 } else { 81 // invalid category 82 return false; 83 } 84 } 85 86 @Override getSummary()87 public CharSequence getSummary() { 88 return mBackend.getStarredContactsSummary(mContext); 89 } 90 91 @Override onPreferenceClick(Preference preference)92 public boolean onPreferenceClick(Preference preference) { 93 if (mStarredContactsIntent.resolveActivity(mPackageManager) != null) { 94 mContext.startActivity(mStarredContactsIntent); 95 } else { 96 mContext.startActivity(mFallbackIntent); 97 } 98 return true; 99 } 100 isIntentValid()101 private boolean isIntentValid() { 102 return mStarredContactsIntent.resolveActivity(mPackageManager) != null 103 || mFallbackIntent.resolveActivity(mPackageManager) != null; 104 } 105 } 106