1 /* 2 * Copyright (C) 2019 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 android.content.ComponentName; 20 import android.content.Context; 21 import android.os.UserHandle; 22 import android.provider.Settings; 23 24 import androidx.fragment.app.Fragment; 25 import androidx.preference.Preference; 26 27 import com.android.settings.R; 28 import com.android.settings.core.TogglePreferenceController; 29 30 import com.google.common.annotations.VisibleForTesting; 31 32 public class NotificationAssistantPreferenceController extends TogglePreferenceController { 33 private static final String TAG = "NASPreferenceController"; 34 static final String KEY_NAS = "notification_assistant"; 35 36 private Fragment mFragment; 37 private int mUserId = UserHandle.myUserId(); 38 39 @VisibleForTesting 40 protected NotificationBackend mNotificationBackend; 41 private ComponentName mDefaultNASComponent; 42 NotificationAssistantPreferenceController(Context context)43 public NotificationAssistantPreferenceController(Context context) { 44 super(context, KEY_NAS); 45 mNotificationBackend = new NotificationBackend(); 46 getDefaultNASIntent(); 47 } 48 49 50 @Override getAvailabilityStatus()51 public int getAvailabilityStatus() { 52 return AVAILABLE; 53 } 54 55 @Override isChecked()56 public boolean isChecked() { 57 ComponentName acn = mNotificationBackend.getAllowedNotificationAssistant(); 58 return (acn != null && acn.equals(mDefaultNASComponent)); 59 } 60 61 @Override setChecked(boolean isChecked)62 public boolean setChecked(boolean isChecked) { 63 ComponentName cn = isChecked 64 ? mDefaultNASComponent : null; 65 if (isChecked) { 66 if (mFragment == null) { 67 throw new IllegalStateException("No fragment to start activity"); 68 } 69 showDialog(cn); 70 return false; 71 } else { 72 setNotificationAssistantGranted(null); 73 return true; 74 } 75 } 76 77 @Override getSliceHighlightMenuRes()78 public int getSliceHighlightMenuRes() { 79 return R.string.menu_key_notifications; 80 } 81 setNotificationAssistantGranted(ComponentName cn)82 protected void setNotificationAssistantGranted(ComponentName cn) { 83 if (Settings.Secure.getIntForUser(mContext.getContentResolver(), 84 Settings.Secure.NAS_SETTINGS_UPDATED, 0, mUserId) == 0) { 85 mNotificationBackend.setNASMigrationDoneAndResetDefault(mUserId, cn != null); 86 } 87 mNotificationBackend.setNotificationAssistantGranted(cn); 88 } 89 showDialog(ComponentName cn)90 protected void showDialog(ComponentName cn) { 91 NotificationAssistantDialogFragment dialogFragment = 92 NotificationAssistantDialogFragment.newInstance(mFragment, cn); 93 dialogFragment.show(mFragment.getFragmentManager(), TAG); 94 } 95 setFragment(Fragment fragment)96 public void setFragment(Fragment fragment) { 97 mFragment = fragment; 98 } 99 100 @VisibleForTesting setBackend(NotificationBackend backend)101 void setBackend(NotificationBackend backend) { 102 mNotificationBackend = backend; 103 } 104 105 @VisibleForTesting getDefaultNASIntent()106 void getDefaultNASIntent() { 107 mDefaultNASComponent = mNotificationBackend.getDefaultNotificationAssistant(); 108 } 109 110 @Override isSliceable()111 public boolean isSliceable() { 112 return (mFragment != null && mFragment instanceof ConfigureNotificationSettings); 113 } 114 115 @Override updateState(Preference preference)116 public void updateState(Preference preference) { 117 super.updateState(preference); 118 if (mDefaultNASComponent == null) { 119 preference.setEnabled(false); 120 } 121 } 122 } 123