• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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 static android.app.admin.DevicePolicyManager.KEYGUARD_DISABLE_SECURE_NOTIFICATIONS;
20 import static android.app.admin.DevicePolicyManager.KEYGUARD_DISABLE_UNREDACTED_NOTIFICATIONS;
21 import static android.provider.Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS;
22 import static android.provider.Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS;
23 
24 import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
25 
26 import android.app.settings.SettingsEnums;
27 import android.content.Context;
28 import android.content.Intent;
29 import android.content.res.Resources;
30 import android.os.Bundle;
31 import android.os.UserManager;
32 import android.provider.Settings;
33 import android.view.LayoutInflater;
34 import android.view.View;
35 import android.view.ViewGroup;
36 import android.widget.RadioButton;
37 import android.widget.RadioGroup;
38 import android.widget.TextView;
39 
40 import com.android.settings.R;
41 import com.android.settings.RestrictedRadioButton;
42 import com.android.settings.SettingsActivity;
43 import com.android.settings.SettingsPreferenceFragment;
44 import com.android.settings.SetupRedactionInterstitial;
45 import com.android.settings.SetupWizardUtils;
46 import com.android.settings.Utils;
47 import com.android.settingslib.RestrictedLockUtilsInternal;
48 
49 import com.google.android.setupcompat.template.FooterBarMixin;
50 import com.google.android.setupcompat.template.FooterButton;
51 import com.google.android.setupdesign.GlifLayout;
52 
53 public class RedactionInterstitial extends SettingsActivity {
54 
55     @Override
getIntent()56     public Intent getIntent() {
57         Intent modIntent = new Intent(super.getIntent());
58         modIntent.putExtra(EXTRA_SHOW_FRAGMENT, RedactionInterstitialFragment.class.getName());
59         return modIntent;
60     }
61 
62     @Override
onApplyThemeResource(Resources.Theme theme, int resid, boolean first)63     protected void onApplyThemeResource(Resources.Theme theme, int resid, boolean first) {
64         resid = SetupWizardUtils.getTheme(getIntent());
65         super.onApplyThemeResource(theme, resid, first);
66     }
67 
68     @Override
isValidFragment(String fragmentName)69     protected boolean isValidFragment(String fragmentName) {
70         return RedactionInterstitialFragment.class.getName().equals(fragmentName);
71     }
72 
73     @Override
onCreate(Bundle savedInstance)74     protected void onCreate(Bundle savedInstance) {
75         super.onCreate(savedInstance);
76         findViewById(R.id.content_parent).setFitsSystemWindows(false);
77     }
78 
79     /**
80      * Create an intent for launching RedactionInterstitial.
81      *
82      * @return An intent to launch the activity is if is available, @null if the activity is not
83      * available to be launched.
84      */
createStartIntent(Context ctx, int userId)85     public static Intent createStartIntent(Context ctx, int userId) {
86         return new Intent(ctx, RedactionInterstitial.class)
87                 .putExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID,
88                         UserManager.get(ctx).isManagedProfile(userId)
89                                 ? R.string.lock_screen_notifications_interstitial_title_profile
90                                 : R.string.lock_screen_notifications_interstitial_title)
91                 .putExtra(Intent.EXTRA_USER_ID, userId);
92     }
93 
94     public static class RedactionInterstitialFragment extends SettingsPreferenceFragment
95             implements RadioGroup.OnCheckedChangeListener {
96 
97         private RadioGroup mRadioGroup;
98         private RestrictedRadioButton mShowAllButton;
99         private RestrictedRadioButton mRedactSensitiveButton;
100         private int mUserId;
101 
102         @Override
getMetricsCategory()103         public int getMetricsCategory() {
104             return SettingsEnums.NOTIFICATION_REDACTION;
105         }
106 
107         @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)108         public View onCreateView(LayoutInflater inflater, ViewGroup container,
109                 Bundle savedInstanceState) {
110             return inflater.inflate(R.layout.redaction_interstitial, container, false);
111         }
112 
113         @Override
onViewCreated(View view, Bundle savedInstanceState)114         public void onViewCreated(View view, Bundle savedInstanceState) {
115             super.onViewCreated(view, savedInstanceState);
116             mRadioGroup = (RadioGroup) view.findViewById(R.id.radio_group);
117             mShowAllButton = (RestrictedRadioButton) view.findViewById(R.id.show_all);
118             mRedactSensitiveButton =
119                     (RestrictedRadioButton) view.findViewById(R.id.redact_sensitive);
120 
121             mRadioGroup.setOnCheckedChangeListener(this);
122             mUserId = Utils.getUserIdFromBundle(
123                     getContext(), getActivity().getIntent().getExtras());
124             if (UserManager.get(getContext()).isManagedProfile(mUserId)) {
125                 ((TextView) view.findViewById(R.id.sud_layout_description))
126                         .setText(R.string.lock_screen_notifications_interstitial_message_profile);
127                 mShowAllButton.setText(R.string.lock_screen_notifications_summary_show_profile);
128                 mRedactSensitiveButton
129                         .setText(R.string.lock_screen_notifications_summary_hide_profile);
130 
131                 ((RadioButton) view.findViewById(R.id.hide_all)).setVisibility(View.GONE);
132             }
133 
134             final GlifLayout layout = view.findViewById(R.id.setup_wizard_layout);
135             final FooterBarMixin mixin = layout.getMixin(FooterBarMixin.class);
136             mixin.setPrimaryButton(
137                     new FooterButton.Builder(getContext())
138                             .setText(R.string.app_notifications_dialog_done)
139                             .setListener(this::onDoneButtonClicked)
140                             .setButtonType(FooterButton.ButtonType.NEXT)
141                             .setTheme(R.style.SudGlifButton_Primary)
142                             .build()
143             );
144         }
145 
onDoneButtonClicked(View view)146         private void onDoneButtonClicked(View view) {
147             SetupRedactionInterstitial.setEnabled(getContext(), false);
148             final RedactionInterstitial activity = (RedactionInterstitial) getActivity();
149             if (activity != null) {
150                 activity.setResult(RESULT_OK, null);
151                 finish();
152             }
153         }
154 
155         @Override
onResume()156         public void onResume() {
157             super.onResume();
158             // Disable buttons according to policy.
159 
160             checkNotificationFeaturesAndSetDisabled(mShowAllButton,
161                     KEYGUARD_DISABLE_SECURE_NOTIFICATIONS |
162                             KEYGUARD_DISABLE_UNREDACTED_NOTIFICATIONS);
163             checkNotificationFeaturesAndSetDisabled(mRedactSensitiveButton,
164                     KEYGUARD_DISABLE_SECURE_NOTIFICATIONS);
165             loadFromSettings();
166         }
167 
checkNotificationFeaturesAndSetDisabled(RestrictedRadioButton button, int keyguardNotifications)168         private void checkNotificationFeaturesAndSetDisabled(RestrictedRadioButton button,
169                 int keyguardNotifications) {
170             EnforcedAdmin admin = RestrictedLockUtilsInternal.checkIfKeyguardFeaturesDisabled(
171                     getActivity(), keyguardNotifications, mUserId);
172             button.setDisabledByAdmin(admin);
173         }
174 
loadFromSettings()175         private void loadFromSettings() {
176             final boolean managedProfile = UserManager.get(getContext()).isManagedProfile(mUserId);
177             // Hiding all notifications is device-wide setting, managed profiles can only set
178             // whether their notifications are show in full or redacted.
179             final boolean showNotifications = managedProfile || Settings.Secure.getIntForUser(
180                     getContentResolver(), LOCK_SCREEN_SHOW_NOTIFICATIONS, 0, mUserId) != 0;
181             final boolean showUnredacted = Settings.Secure.getIntForUser(
182                     getContentResolver(), LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 1, mUserId) != 0;
183 
184             int checkedButtonId = R.id.hide_all;
185             if (showNotifications) {
186                 if (showUnredacted && !mShowAllButton.isDisabledByAdmin()) {
187                     checkedButtonId = R.id.show_all;
188                 } else if (!mRedactSensitiveButton.isDisabledByAdmin()) {
189                     checkedButtonId = R.id.redact_sensitive;
190                 }
191             }
192 
193             mRadioGroup.check(checkedButtonId);
194         }
195 
196         @Override
onCheckedChanged(RadioGroup group, int checkedId)197         public void onCheckedChanged(RadioGroup group, int checkedId) {
198             final boolean show = (checkedId == R.id.show_all);
199             final boolean enabled = (checkedId != R.id.hide_all);
200 
201             Settings.Secure.putIntForUser(getContentResolver(),
202                     LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, show ? 1 : 0, mUserId);
203             Settings.Secure.putIntForUser(getContentResolver(),
204                     LOCK_SCREEN_SHOW_NOTIFICATIONS, enabled ? 1 : 0, mUserId);
205 
206         }
207     }
208 }
209