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 android.app.admin.DevicePolicyManager;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.os.Bundle;
23 import android.os.UserManager;
24 import android.provider.Settings;
25 import android.view.LayoutInflater;
26 import android.view.View;
27 import android.view.ViewGroup;
28 import android.widget.CheckBox;
29 import android.widget.CompoundButton;
30 import android.widget.RadioButton;
31 import android.widget.RadioGroup;
32 import android.widget.TextView;
33 
34 import com.android.internal.logging.MetricsProto.MetricsEvent;
35 import com.android.settings.R;
36 import com.android.settings.RestrictedCheckBox;
37 import com.android.settings.RestrictedRadioButton;
38 import com.android.settings.SettingsActivity;
39 import com.android.settings.SettingsPreferenceFragment;
40 import com.android.settings.Utils;
41 import com.android.settingslib.RestrictedLockUtils;
42 
43 import static android.app.admin.DevicePolicyManager.KEYGUARD_DISABLE_SECURE_NOTIFICATIONS;
44 import static android.app.admin.DevicePolicyManager.KEYGUARD_DISABLE_UNREDACTED_NOTIFICATIONS;
45 
46 import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
47 
48 public class RedactionInterstitial extends SettingsActivity {
49 
50     @Override
getIntent()51     public Intent getIntent() {
52         Intent modIntent = new Intent(super.getIntent());
53         modIntent.putExtra(EXTRA_SHOW_FRAGMENT, RedactionInterstitialFragment.class.getName());
54         return modIntent;
55     }
56 
57     @Override
isValidFragment(String fragmentName)58     protected boolean isValidFragment(String fragmentName) {
59         return RedactionInterstitialFragment.class.getName().equals(fragmentName);
60     }
61 
62     /**
63      * Create an intent for launching RedactionInterstitial.
64      * @return An intent to launch the activity is if is available, @null if the activity is not
65      * available to be launched.
66      */
createStartIntent(Context ctx, int userId)67     public static Intent createStartIntent(Context ctx, int userId) {
68         return new Intent(ctx, RedactionInterstitial.class)
69                 .putExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, true)
70                 .putExtra(EXTRA_PREFS_SET_BACK_TEXT, (String) null)
71                 .putExtra(EXTRA_PREFS_SET_NEXT_TEXT, ctx.getString(
72                         R.string.app_notifications_dialog_done))
73                 .putExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID,
74                         Utils.isManagedProfile(UserManager.get(ctx), userId)
75                             ? R.string.lock_screen_notifications_interstitial_title_profile
76                             : R.string.lock_screen_notifications_interstitial_title)
77                 .putExtra(Intent.EXTRA_USER_ID, userId);
78     }
79 
80     public static class RedactionInterstitialFragment extends SettingsPreferenceFragment
81             implements RadioGroup.OnCheckedChangeListener {
82 
83         private RadioGroup mRadioGroup;
84         private RestrictedRadioButton mShowAllButton;
85         private RestrictedRadioButton mRedactSensitiveButton;
86         private int mUserId;
87 
88         @Override
getMetricsCategory()89         protected int getMetricsCategory() {
90             return MetricsEvent.NOTIFICATION_REDACTION;
91         }
92 
93         @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)94         public View onCreateView(LayoutInflater inflater, ViewGroup container,
95                 Bundle savedInstanceState) {
96             return inflater.inflate(R.layout.redaction_interstitial, container, false);
97         }
98 
99         @Override
onViewCreated(View view, Bundle savedInstanceState)100         public void onViewCreated(View view, Bundle savedInstanceState) {
101             super.onViewCreated(view, savedInstanceState);
102             mRadioGroup = (RadioGroup) view.findViewById(R.id.radio_group);
103             mShowAllButton = (RestrictedRadioButton) view.findViewById(R.id.show_all);
104             mRedactSensitiveButton =
105                     (RestrictedRadioButton) view.findViewById(R.id.redact_sensitive);
106 
107             mRadioGroup.setOnCheckedChangeListener(this);
108             mUserId = Utils.getUserIdFromBundle(
109                     getContext(), getActivity().getIntent().getExtras());
110             if (Utils.isManagedProfile(UserManager.get(getContext()), mUserId)) {
111                 ((TextView) view.findViewById(R.id.message))
112                     .setText(R.string.lock_screen_notifications_interstitial_message_profile);
113                 mShowAllButton.setText(R.string.lock_screen_notifications_summary_show_profile);
114                 mRedactSensitiveButton
115                     .setText(R.string.lock_screen_notifications_summary_hide_profile);
116                 ((RadioButton) view.findViewById(R.id.hide_all))
117                     .setText(R.string.lock_screen_notifications_summary_disable_profile);
118             }
119         }
120 
121         @Override
onResume()122         public void onResume() {
123             super.onResume();
124             // Disable buttons according to policy.
125 
126             checkNotificationFeaturesAndSetDisabled(mShowAllButton,
127                     KEYGUARD_DISABLE_SECURE_NOTIFICATIONS |
128                     KEYGUARD_DISABLE_UNREDACTED_NOTIFICATIONS);
129             checkNotificationFeaturesAndSetDisabled(mRedactSensitiveButton,
130                     KEYGUARD_DISABLE_SECURE_NOTIFICATIONS);
131             loadFromSettings();
132         }
133 
checkNotificationFeaturesAndSetDisabled(RestrictedRadioButton button, int keyguardNotifications)134         private void checkNotificationFeaturesAndSetDisabled(RestrictedRadioButton button,
135                 int keyguardNotifications) {
136             EnforcedAdmin admin = RestrictedLockUtils.checkIfKeyguardFeaturesDisabled(
137                     getActivity(), keyguardNotifications, mUserId);
138             button.setDisabledByAdmin(admin);
139         }
140 
loadFromSettings()141         private void loadFromSettings() {
142             final boolean enabled = Settings.Secure.getIntForUser(getContentResolver(),
143                         Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 0, mUserId) != 0;
144             final boolean show = Settings.Secure.getIntForUser(getContentResolver(),
145                         Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 1, mUserId) != 0;
146 
147             int checkedButtonId = R.id.hide_all;
148             if (enabled) {
149                 if (show && !mShowAllButton.isDisabledByAdmin()) {
150                     checkedButtonId = R.id.show_all;
151                 } else if (!mRedactSensitiveButton.isDisabledByAdmin()) {
152                     checkedButtonId = R.id.redact_sensitive;
153                 }
154             }
155 
156             mRadioGroup.check(checkedButtonId);
157         }
158 
159         @Override
onCheckedChanged(RadioGroup group, int checkedId)160         public void onCheckedChanged(RadioGroup group, int checkedId) {
161             final boolean show = (checkedId == R.id.show_all);
162             final boolean enabled = (checkedId != R.id.hide_all);
163 
164             Settings.Secure.putIntForUser(getContentResolver(),
165                     Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, show ? 1 : 0, mUserId);
166             Settings.Secure.putIntForUser(getContentResolver(),
167                     Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, enabled ? 1 : 0, mUserId);
168 
169         }
170     }
171 }
172