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.settings.notification.app;
18 
19 import android.app.settings.SettingsEnums;
20 import android.content.Context;
21 import android.text.TextUtils;
22 import android.util.Log;
23 
24 import com.android.settings.R;
25 import com.android.settings.notification.AppBubbleListPreferenceController;
26 import com.android.settings.notification.NotificationBackend;
27 import com.android.settings.search.BaseSearchIndexProvider;
28 import com.android.settingslib.core.AbstractPreferenceController;
29 import com.android.settingslib.search.SearchIndexable;
30 
31 import java.util.ArrayList;
32 import java.util.List;
33 
34 /**
35  * App level settings for bubbles.
36  */
37 @SearchIndexable
38 public class AppBubbleNotificationSettings extends NotificationSettings implements
39         GlobalBubblePermissionObserverMixin.Listener {
40     private static final String TAG = "AppBubNotiSettings";
41     private GlobalBubblePermissionObserverMixin mObserverMixin;
42 
43     @Override
getMetricsCategory()44     public int getMetricsCategory() {
45         return SettingsEnums.APP_BUBBLE_SETTINGS;
46     }
47 
48     @Override
getLogTag()49     protected String getLogTag() {
50         return TAG;
51     }
52 
53     @Override
getPreferenceScreenResId()54     protected int getPreferenceScreenResId() {
55         return R.xml.app_bubble_notification_settings;
56     }
57 
58     @Override
createPreferenceControllers(Context context)59     protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
60         mControllers = getPreferenceControllers(context, this, mDependentFieldListener);
61         return new ArrayList<>(mControllers);
62     }
63 
getPreferenceControllers( Context context, AppBubbleNotificationSettings fragment, DependentFieldListener listener)64     protected static List<NotificationPreferenceController> getPreferenceControllers(
65             Context context, AppBubbleNotificationSettings fragment,
66             DependentFieldListener listener) {
67         List<NotificationPreferenceController> controllers = new ArrayList<>();
68         controllers.add(new HeaderPreferenceController(context, fragment));
69         controllers.add(new BubblePreferenceController(context, fragment != null
70                 ? fragment.getChildFragmentManager()
71                 : null,
72                 new NotificationBackend(), true /* isAppPage */, listener));
73         controllers.add(new AppBubbleListPreferenceController(context, new NotificationBackend()));
74         return controllers;
75     }
76 
77     @Override
onGlobalBubblePermissionChanged()78     public void onGlobalBubblePermissionChanged() {
79         updatePreferenceStates();
80     }
81 
82     @Override
onResume()83     public void onResume() {
84         super.onResume();
85 
86         if (mUid < 0 || TextUtils.isEmpty(mPkg) || mPkgInfo == null) {
87             Log.w(TAG, "Missing package or uid or packageinfo");
88             finish();
89             return;
90         }
91 
92         for (NotificationPreferenceController controller : mControllers) {
93             controller.onResume(mAppRow, null, null, null, null, mSuspendedAppsAdmin);
94             controller.displayPreference(getPreferenceScreen());
95         }
96         updatePreferenceStates();
97 
98         mObserverMixin = new GlobalBubblePermissionObserverMixin(getContext(), this);
99         mObserverMixin.onStart();
100     }
101 
102     @Override
onPause()103     public void onPause() {
104         mObserverMixin.onStop();
105         super.onPause();
106     }
107 
108     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
109             new BaseSearchIndexProvider() {
110 
111                 @Override
112                 protected boolean isPageSearchEnabled(Context context) {
113                     return false;
114                 }
115 
116                 @Override
117                 public List<AbstractPreferenceController> createPreferenceControllers(Context
118                         context) {
119                     return new ArrayList<>(AppBubbleNotificationSettings.getPreferenceControllers(
120                             context, null, null));
121                 }
122             };
123 }
124