• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static com.android.car.settings.storage.StorageUtils.maybeInitializeVolume;
20 
21 import android.app.Application;
22 import android.content.Context;
23 import android.os.Bundle;
24 import android.os.UserManager;
25 import android.os.storage.StorageManager;
26 import android.os.storage.VolumeInfo;
27 import android.provider.Settings;
28 
29 import androidx.annotation.XmlRes;
30 
31 import com.android.car.settings.R;
32 import com.android.car.settings.applications.ApplicationListItemManager;
33 import com.android.car.settings.common.SettingsFragment;
34 import com.android.car.settings.search.CarBaseSearchIndexProvider;
35 import com.android.settingslib.applications.ApplicationsState;
36 import com.android.settingslib.search.SearchIndexable;
37 
38 /** Shows subsettings related to notifications. */
39 @SearchIndexable
40 public class NotificationsFragment extends SettingsFragment {
41 
42     private ApplicationListItemManager mAppListItemManager;
43     @Override
44     @XmlRes
getPreferenceScreenResId()45     protected int getPreferenceScreenResId() {
46         return R.xml.notifications_fragment;
47     }
48 
49     @Override
onAttach(Context context)50     public void onAttach(Context context) {
51         super.onAttach(context);
52 
53         Application application = requireActivity().getApplication();
54         ApplicationsState applicationsState = ApplicationsState.getInstance(application);
55         StorageManager sm = context.getSystemService(StorageManager.class);
56         UserManager um = context.getSystemService(UserManager.class);
57         VolumeInfo volume = maybeInitializeVolume(sm, getArguments());
58 
59         NotificationsAppListPreferenceController notificationsAppListController =
60                 use(NotificationsAppListPreferenceController.class,
61                         R.string.pk_notifications_settings_all_apps);
62         RecentNotificationsAppsPreferenceController recentNotificationsController =
63                 use(RecentNotificationsAppsPreferenceController.class,
64                         R.string.pk_notifications_settings_recently_sent);
65 
66         mAppListItemManager = new ApplicationListItemManager(volume, getLifecycle(),
67                 applicationsState,
68                 getContext().getResources().getInteger(
69                         R.integer.millisecond_app_data_update_interval),
70                 getContext().getResources().getInteger(
71                         R.integer.millisecond_max_app_load_wait_interval),
72                 getContext().getPackageManager(), um);
73         mAppListItemManager.registerListener(notificationsAppListController);
74         mAppListItemManager.registerListener(recentNotificationsController);
75         recentNotificationsController.setApplicationsState(applicationsState);
76 
77         // Since an app can show in both sections, use a listener to notify the other section to
78         // update in order to maintain consistency
79         notificationsAppListController.setNotificationSwitchListener(() ->
80                 mAppListItemManager.onPackageListChanged());
81         recentNotificationsController.setNotificationSwitchListener(() ->
82                 mAppListItemManager.onPackageListChanged());
83     }
84 
85     @Override
onCreate(Bundle savedInstanceState)86     public void onCreate(Bundle savedInstanceState) {
87         super.onCreate(savedInstanceState);
88         mAppListItemManager.startLoading(getAppFilter(), ApplicationsState.ALPHA_COMPARATOR);
89     }
90 
91     @Override
onStart()92     public void onStart() {
93         super.onStart();
94         mAppListItemManager.onFragmentStart();
95     }
96 
97     @Override
onStop()98     public void onStop() {
99         super.onStop();
100         mAppListItemManager.onFragmentStop();
101     }
102 
getAppFilter()103     private ApplicationsState.AppFilter getAppFilter() {
104         // Display only non-system apps
105         return ApplicationsState.FILTER_DOWNLOADED_AND_LAUNCHER_AND_INSTANT;
106     }
107 
108     /**
109      * Callback that is called when a app's notification setting is toggled
110      */
111     public interface NotificationSwitchListener {
112         /**
113          * An app's notification setting has been changed
114          */
onSwitchChanged()115         void onSwitchChanged();
116     }
117 
118     /**
119      * Data provider for Settings Search.
120      */
121     public static final CarBaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
122             new CarBaseSearchIndexProvider(R.xml.notifications_fragment,
123                     Settings.ACTION_NOTIFICATION_SETTINGS);
124 }
125