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