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