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.os.UserManager; 23 import android.provider.Settings; 24 25 import androidx.fragment.app.Fragment; 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 private static final String KEY_NAS = "notification_assistant"; 35 36 private static final int AVAILABLE = 1; 37 private final UserManager mUserManager; 38 private Fragment mFragment; 39 private int mUserId = UserHandle.myUserId(); 40 41 @VisibleForTesting 42 protected NotificationBackend mNotificationBackend; 43 NotificationAssistantPreferenceController(Context context)44 public NotificationAssistantPreferenceController(Context context) { 45 super(context, KEY_NAS); 46 mUserManager = UserManager.get(context); 47 } 48 49 @Override getAvailabilityStatus()50 public int getAvailabilityStatus() { 51 return AVAILABLE; 52 } 53 54 @Override isChecked()55 public boolean isChecked() { 56 ComponentName acn = mNotificationBackend.getAllowedNotificationAssistant(); 57 ComponentName dcn = mNotificationBackend.getDefaultNotificationAssistant(); 58 return (acn != null && acn.equals(dcn)); 59 } 60 61 @Override setChecked(boolean isChecked)62 public boolean setChecked(boolean isChecked) { 63 ComponentName cn = isChecked 64 ? mNotificationBackend.getDefaultNotificationAssistant() : 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 }