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.car.settings.applications;
18 
19 import android.car.drivingstate.CarUxRestrictions;
20 import android.content.Context;
21 import android.content.pm.ApplicationInfo;
22 import android.content.pm.PackageInfo;
23 
24 import androidx.preference.TwoStatePreference;
25 
26 import com.android.car.settings.common.FragmentController;
27 import com.android.car.settings.common.Logger;
28 import com.android.car.settings.notifications.BaseNotificationsPreferenceController;
29 
30 /**
31  * Controller for preference which enables / disables showing notifications for an application.
32  */
33 public class NotificationsPreferenceController extends
34         BaseNotificationsPreferenceController<TwoStatePreference> {
35 
36     private static final Logger LOG = new Logger(NotificationsPreferenceController.class);
37 
38     private String mPackageName;
39     private int mUid;
40     private ApplicationInfo mAppInfo;
41 
NotificationsPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)42     public NotificationsPreferenceController(Context context, String preferenceKey,
43             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
44         super(context, preferenceKey, fragmentController, uxRestrictions);
45     }
46 
47     /**
48      * Set the package info of the application.
49      */
setPackageInfo(PackageInfo packageInfo)50     public void setPackageInfo(PackageInfo packageInfo) {
51         mPackageName = packageInfo.packageName;
52         mUid = packageInfo.applicationInfo.uid;
53         mAppInfo = packageInfo.applicationInfo;
54     }
55 
56     @Override
getPreferenceType()57     protected Class<TwoStatePreference> getPreferenceType() {
58         return TwoStatePreference.class;
59     }
60 
61     @Override
updateState(TwoStatePreference preference)62     protected void updateState(TwoStatePreference preference) {
63         preference.setChecked(areNotificationsEnabled(mPackageName, mUid));
64         preference.setEnabled(areNotificationsChangeable(mAppInfo));
65     }
66 
67     @Override
handlePreferenceChanged(TwoStatePreference preference, Object newValue)68     protected boolean handlePreferenceChanged(TwoStatePreference preference, Object newValue) {
69         boolean enabled = (boolean) newValue;
70         return toggleNotificationsSetting(mPackageName, mUid, enabled);
71     }
72 }
73