1 /* 2 * Copyright (C) 2021 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.notifications; 18 19 import android.car.drivingstate.CarUxRestrictions; 20 import android.content.Context; 21 import android.content.pm.ApplicationInfo; 22 import android.graphics.drawable.Drawable; 23 24 import androidx.annotation.VisibleForTesting; 25 import androidx.preference.Preference; 26 import androidx.preference.PreferenceCategory; 27 28 import com.android.car.settings.applications.ApplicationDetailsFragment; 29 import com.android.car.settings.applications.ApplicationListItemManager; 30 import com.android.car.settings.common.FragmentController; 31 import com.android.car.ui.preference.CarUiTwoActionSwitchPreference; 32 import com.android.settingslib.applications.ApplicationsState; 33 34 import java.util.ArrayList; 35 36 /** 37 * Controller for of list of preferences that enable / disable showing notifications for an 38 * application. 39 */ 40 public class NotificationsAppListPreferenceController extends 41 BaseNotificationsPreferenceController<PreferenceCategory> implements 42 ApplicationListItemManager.AppListItemListener { 43 44 private NotificationsFragment.NotificationSwitchListener mNotificationSwitchListener; 45 NotificationsAppListPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)46 public NotificationsAppListPreferenceController(Context context, String preferenceKey, 47 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 48 super(context, preferenceKey, fragmentController, uxRestrictions); 49 } 50 setNotificationSwitchListener( NotificationsFragment.NotificationSwitchListener listener)51 public void setNotificationSwitchListener( 52 NotificationsFragment.NotificationSwitchListener listener) { 53 mNotificationSwitchListener = listener; 54 } 55 56 @Override getPreferenceType()57 protected Class<PreferenceCategory> getPreferenceType() { 58 return PreferenceCategory.class; 59 } 60 61 @Override onDataLoaded(ArrayList<ApplicationsState.AppEntry> apps)62 public void onDataLoaded(ArrayList<ApplicationsState.AppEntry> apps) { 63 getPreference().removeAll(); 64 for (ApplicationsState.AppEntry appEntry : apps) { 65 getPreference().addPreference( 66 createPreference(appEntry.label, appEntry.icon, appEntry.info)); 67 } 68 } 69 createPreference(String title, Drawable icon, ApplicationInfo appInfo)70 private Preference createPreference(String title, Drawable icon, ApplicationInfo appInfo) { 71 String packageName = appInfo.packageName; 72 int uid = appInfo.uid; 73 74 CarUiTwoActionSwitchPreference preference = createPreference(); 75 preference.setTitle(title); 76 preference.setIcon(icon); 77 preference.setKey(packageName); 78 preference.setOnPreferenceClickListener(p -> onPrimaryActionClick(packageName)); 79 80 preference.setOnSecondaryActionClickListener((newValue) -> { 81 onSecondaryActionClick(packageName, uid, newValue); 82 }); 83 preference.setSecondaryActionChecked(areNotificationsEnabled(packageName, uid)); 84 preference.setSecondaryActionEnabled(areNotificationsChangeable(appInfo)); 85 86 return preference; 87 } 88 89 @VisibleForTesting createPreference()90 CarUiTwoActionSwitchPreference createPreference() { 91 return new CarUiTwoActionSwitchPreference(getContext()); 92 } 93 94 @VisibleForTesting onPrimaryActionClick(String packageName)95 boolean onPrimaryActionClick(String packageName) { 96 getFragmentController().launchFragment( 97 ApplicationDetailsFragment.getInstance(packageName)); 98 return true; 99 } 100 @VisibleForTesting onSecondaryActionClick(String packageName, int uid, boolean newValue)101 void onSecondaryActionClick(String packageName, int uid, boolean newValue) { 102 toggleNotificationsSetting(packageName, uid, newValue); 103 if (mNotificationSwitchListener != null) { 104 mNotificationSwitchListener.onSwitchChanged(); 105 } 106 } 107 } 108