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