Fabian Kozynski | 01b2a63 | 2019-02-20 12:55:10 -0500 | [diff] [blame] | 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 | |
Fabian Kozynski | d565d7a | 2019-02-25 15:52:40 -0500 | [diff] [blame] | 19 | import android.content.ComponentName; |
Fabian Kozynski | 01b2a63 | 2019-02-20 12:55:10 -0500 | [diff] [blame] | 20 | import android.content.Context; |
Fabian Kozynski | d565d7a | 2019-02-25 15:52:40 -0500 | [diff] [blame] | 21 | import android.os.UserHandle; |
Chloris Kuo | d37f0fa | 2021-04-16 15:20:30 -0700 | [diff] [blame] | 22 | import android.os.UserManager; |
Chloris Kuo | 2c955bb | 2021-03-19 07:00:45 -0700 | [diff] [blame] | 23 | import android.provider.Settings; |
Fabian Kozynski | 01b2a63 | 2019-02-20 12:55:10 -0500 | [diff] [blame] | 24 | |
Chloris Kuo | 2c955bb | 2021-03-19 07:00:45 -0700 | [diff] [blame] | 25 | import androidx.fragment.app.Fragment; |
| 26 | |
| 27 | import com.android.settings.core.TogglePreferenceController; |
Fabian Kozynski | d565d7a | 2019-02-25 15:52:40 -0500 | [diff] [blame] | 28 | |
| 29 | import com.google.common.annotations.VisibleForTesting; |
Fabian Kozynski | 01b2a63 | 2019-02-20 12:55:10 -0500 | [diff] [blame] | 30 | |
Chloris Kuo | 2c955bb | 2021-03-19 07:00:45 -0700 | [diff] [blame] | 31 | public class NotificationAssistantPreferenceController extends TogglePreferenceController { |
| 32 | private static final String TAG = "NASPreferenceController"; |
| 33 | private static final int AVAILABLE = 1; |
Chloris Kuo | d37f0fa | 2021-04-16 15:20:30 -0700 | [diff] [blame] | 34 | private final UserManager mUserManager; |
Chloris Kuo | 2c955bb | 2021-03-19 07:00:45 -0700 | [diff] [blame] | 35 | private Fragment mFragment; |
| 36 | private int mUserId = UserHandle.myUserId(); |
Fabian Kozynski | 01b2a63 | 2019-02-20 12:55:10 -0500 | [diff] [blame] | 37 | |
Fabian Kozynski | d565d7a | 2019-02-25 15:52:40 -0500 | [diff] [blame] | 38 | @VisibleForTesting |
| 39 | protected NotificationBackend mNotificationBackend; |
Fabian Kozynski | d565d7a | 2019-02-25 15:52:40 -0500 | [diff] [blame] | 40 | |
Chloris Kuo | 2c955bb | 2021-03-19 07:00:45 -0700 | [diff] [blame] | 41 | public NotificationAssistantPreferenceController(Context context, NotificationBackend backend, |
| 42 | Fragment fragment, String preferenceKey) { |
Fabian Kozynski | 01b2a63 | 2019-02-20 12:55:10 -0500 | [diff] [blame] | 43 | super(context, preferenceKey); |
Chloris Kuo | 2c955bb | 2021-03-19 07:00:45 -0700 | [diff] [blame] | 44 | mNotificationBackend = backend; |
| 45 | mFragment = fragment; |
Chloris Kuo | d37f0fa | 2021-04-16 15:20:30 -0700 | [diff] [blame] | 46 | mUserManager = UserManager.get(context); |
Fabian Kozynski | 01b2a63 | 2019-02-20 12:55:10 -0500 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public int getAvailabilityStatus() { |
Chloris Kuo | 2c955bb | 2021-03-19 07:00:45 -0700 | [diff] [blame] | 51 | return AVAILABLE; |
Fabian Kozynski | 01b2a63 | 2019-02-20 12:55:10 -0500 | [diff] [blame] | 52 | } |
Fabian Kozynski | d565d7a | 2019-02-25 15:52:40 -0500 | [diff] [blame] | 53 | |
| 54 | @Override |
Chloris Kuo | 2c955bb | 2021-03-19 07:00:45 -0700 | [diff] [blame] | 55 | public boolean isChecked() { |
| 56 | ComponentName acn = mNotificationBackend.getAllowedNotificationAssistant(); |
| 57 | ComponentName dcn = mNotificationBackend.getDefaultNotificationAssistant(); |
| 58 | return (acn != null && acn.equals(dcn)); |
Fabian Kozynski | d565d7a | 2019-02-25 15:52:40 -0500 | [diff] [blame] | 59 | } |
| 60 | |
Chloris Kuo | 2c955bb | 2021-03-19 07:00:45 -0700 | [diff] [blame] | 61 | @Override |
| 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 | } |
Fabian Kozynski | d565d7a | 2019-02-25 15:52:40 -0500 | [diff] [blame] | 75 | } |
Chloris Kuo | 2c955bb | 2021-03-19 07:00:45 -0700 | [diff] [blame] | 76 | |
| 77 | protected void setNotificationAssistantGranted(ComponentName cn) { |
| 78 | if (Settings.Secure.getIntForUser(mContext.getContentResolver(), |
| 79 | Settings.Secure.NAS_SETTINGS_UPDATED, 0, mUserId) == 0) { |
Chloris Kuo | f06f289 | 2021-04-20 19:03:56 -0700 | [diff] [blame] | 80 | mNotificationBackend.setNASMigrationDoneAndResetDefault(mUserId, cn != null); |
Chloris Kuo | 2c955bb | 2021-03-19 07:00:45 -0700 | [diff] [blame] | 81 | } |
| 82 | mNotificationBackend.setNotificationAssistantGranted(cn); |
| 83 | } |
| 84 | |
| 85 | protected void showDialog(ComponentName cn) { |
| 86 | NotificationAssistantDialogFragment dialogFragment = |
| 87 | NotificationAssistantDialogFragment.newInstance(mFragment, cn); |
| 88 | dialogFragment.show(mFragment.getFragmentManager(), TAG); |
| 89 | } |
| 90 | } |