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