OSDN Git Service

0e47ed8fe915dc48eaff5e88be16c2796f50c2ef
[android-x86/packages-apps-Settings.git] / src / com / android / settings / notification / ConfigureNotificationSettings.java
1 /*
2  * Copyright (C) 2015 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;
18
19 import android.app.Activity;
20 import android.app.Application;
21 import android.app.Fragment;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.os.Bundle;
25 import android.os.UserHandle;
26 import android.provider.SearchIndexableResource;
27 import android.support.annotation.VisibleForTesting;
28 import android.support.v7.preference.Preference;
29
30 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
31 import com.android.settings.R;
32 import com.android.settings.RingtonePreference;
33 import com.android.settings.dashboard.DashboardFragment;
34 import com.android.settings.gestures.SwipeToNotificationPreferenceController;
35 import com.android.settings.search.BaseSearchIndexProvider;
36 import com.android.settings.search.Indexable;
37 import com.android.settingslib.core.AbstractPreferenceController;
38 import com.android.settingslib.core.lifecycle.Lifecycle;
39
40 import java.util.ArrayList;
41 import java.util.Arrays;
42 import java.util.List;
43
44 public class ConfigureNotificationSettings extends DashboardFragment {
45     private static final String TAG = "ConfigNotiSettings";
46
47     @VisibleForTesting
48     static final String KEY_LOCKSCREEN = "lock_screen_notifications";
49     @VisibleForTesting
50     static final String KEY_LOCKSCREEN_WORK_PROFILE_HEADER =
51             "lock_screen_notifications_profile_header";
52     @VisibleForTesting
53     static final String KEY_LOCKSCREEN_WORK_PROFILE = "lock_screen_notifications_profile";
54     @VisibleForTesting
55     static final String KEY_SWIPE_DOWN = "gesture_swipe_down_fingerprint_notifications";
56
57     private static final String KEY_NOTI_DEFAULT_RINGTONE = "notification_default_ringtone";
58
59     private RingtonePreference mRequestPreference;
60     private static final int REQUEST_CODE = 200;
61     private static final String SELECTED_PREFERENCE_KEY = "selected_preference";
62
63     @Override
64     public int getMetricsCategory() {
65         return MetricsEvent.CONFIGURE_NOTIFICATION;
66     }
67
68     @Override
69     protected String getLogTag() {
70         return TAG;
71     }
72
73     @Override
74     protected int getPreferenceScreenResId() {
75         return R.xml.configure_notification_settings;
76     }
77
78     @Override
79     protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
80         final Activity activity = getActivity();
81         final Application app;
82         if (activity != null) {
83             app = activity.getApplication();
84         } else {
85             app = null;
86         }
87         return buildPreferenceControllers(context, getLifecycle(), app, this);
88     }
89
90     private static List<AbstractPreferenceController> buildPreferenceControllers(Context context,
91             Lifecycle lifecycle, Application app, Fragment host) {
92         final List<AbstractPreferenceController> controllers = new ArrayList<>();
93         final BadgingNotificationPreferenceController badgeController =
94                 new BadgingNotificationPreferenceController(context);
95         final PulseNotificationPreferenceController pulseController =
96                 new PulseNotificationPreferenceController(context);
97         final LockScreenNotificationPreferenceController lockScreenNotificationController =
98                 new LockScreenNotificationPreferenceController(context,
99                         KEY_LOCKSCREEN,
100                         KEY_LOCKSCREEN_WORK_PROFILE_HEADER,
101                         KEY_LOCKSCREEN_WORK_PROFILE);
102         if (lifecycle != null) {
103             lifecycle.addObserver(pulseController);
104             lifecycle.addObserver(lockScreenNotificationController);
105         }
106         controllers.add(new RecentNotifyingAppsPreferenceController(
107                 context, new NotificationBackend(), app, host));
108         controllers.add(new SwipeToNotificationPreferenceController(context, lifecycle,
109                 KEY_SWIPE_DOWN));
110         controllers.add(badgeController);
111         controllers.add(pulseController);
112         controllers.add(lockScreenNotificationController);
113         controllers.add(new NotificationRingtonePreferenceController(context) {
114             @Override
115             public String getPreferenceKey() {
116                 return KEY_NOTI_DEFAULT_RINGTONE;
117             }
118
119         });
120         return controllers;
121     }
122
123     @Override
124     public boolean onPreferenceTreeClick(Preference preference) {
125         if (preference instanceof RingtonePreference) {
126             mRequestPreference = (RingtonePreference) preference;
127             mRequestPreference.onPrepareRingtonePickerIntent(mRequestPreference.getIntent());
128             startActivityForResultAsUser(
129                     mRequestPreference.getIntent(),
130                     REQUEST_CODE,
131                     null,
132                     UserHandle.of(mRequestPreference.getUserId()));
133             return true;
134         }
135         return super.onPreferenceTreeClick(preference);
136     }
137
138     @Override
139     public void onActivityResult(int requestCode, int resultCode, Intent data) {
140         if (mRequestPreference != null) {
141             mRequestPreference.onActivityResult(requestCode, resultCode, data);
142             mRequestPreference = null;
143         }
144     }
145
146     @Override
147     public void onSaveInstanceState(Bundle outState) {
148         super.onSaveInstanceState(outState);
149         if (mRequestPreference != null) {
150             outState.putString(SELECTED_PREFERENCE_KEY, mRequestPreference.getKey());
151         }
152     }
153
154     /**
155      * For Search.
156      */
157     public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
158             new BaseSearchIndexProvider() {
159                 @Override
160                 public List<SearchIndexableResource> getXmlResourcesToIndex(
161                         Context context, boolean enabled) {
162                     final SearchIndexableResource sir = new SearchIndexableResource(context);
163                     sir.xmlResId = R.xml.configure_notification_settings;
164                     return Arrays.asList(sir);
165                 }
166
167                 @Override
168                 public List<AbstractPreferenceController> createPreferenceControllers(
169                         Context context) {
170                     return buildPreferenceControllers(context, null, null, null);
171                 }
172
173                 @Override
174                 public List<String> getNonIndexableKeys(Context context) {
175                     final List<String> keys = super.getNonIndexableKeys(context);
176                     keys.add(KEY_SWIPE_DOWN);
177                     keys.add(KEY_LOCKSCREEN);
178                     keys.add(KEY_LOCKSCREEN_WORK_PROFILE);
179                     keys.add(KEY_LOCKSCREEN_WORK_PROFILE_HEADER);
180                     return keys;
181                 }
182             };
183 }