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 com.android.internal.logging.MetricsLogger;
20import com.android.settings.R;
21import com.android.settings.SettingsActivity;
22import com.android.settings.SettingsPreferenceFragment;
23
24import android.app.admin.DevicePolicyManager;
25import android.content.Context;
26import android.content.Intent;
27import android.os.Bundle;
28import android.provider.Settings;
29import android.view.LayoutInflater;
30import android.view.View;
31import android.view.ViewGroup;
32import android.widget.RadioButton;
33import android.widget.RadioGroup;
34
35public class RedactionInterstitial extends SettingsActivity {
36
37    @Override
38    public Intent getIntent() {
39        Intent modIntent = new Intent(super.getIntent());
40        modIntent.putExtra(EXTRA_SHOW_FRAGMENT, RedactionInterstitialFragment.class.getName());
41        return modIntent;
42    }
43
44    @Override
45    protected boolean isValidFragment(String fragmentName) {
46        return RedactionInterstitialFragment.class.getName().equals(fragmentName);
47    }
48
49    /**
50     * Create an intent for launching RedactionInterstitial.
51     * @return An intent to launch the activity is if is available, @null if the activity is not
52     * available to be launched.
53     */
54    public static Intent createStartIntent(Context ctx) {
55        if (isSecureNotificationsDisabled(ctx)) {
56            // If there is no choices for the user, we should not start the activity.
57            return null;
58        } else {
59            return new Intent(ctx, RedactionInterstitial.class)
60                    .putExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, true)
61                    .putExtra(EXTRA_PREFS_SET_BACK_TEXT, (String) null)
62                    .putExtra(EXTRA_PREFS_SET_NEXT_TEXT, ctx.getString(
63                            R.string.app_notifications_dialog_done))
64                    .putExtra(EXTRA_SHOW_FRAGMENT_TITLE_RESID,
65                            R.string.lock_screen_notifications_interstitial_title);
66        }
67    }
68
69    private static boolean isSecureNotificationsDisabled(Context context) {
70        final DevicePolicyManager dpm =
71                (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
72        return dpm != null && (dpm.getKeyguardDisabledFeatures(null)
73                & DevicePolicyManager.KEYGUARD_DISABLE_SECURE_NOTIFICATIONS) != 0;
74    }
75
76    private static boolean isUnredactedNotificationsDisabled(Context context) {
77        final DevicePolicyManager dpm =
78                (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
79        return dpm != null && (dpm.getKeyguardDisabledFeatures(null)
80                & DevicePolicyManager.KEYGUARD_DISABLE_UNREDACTED_NOTIFICATIONS) != 0;
81    }
82
83    public static class RedactionInterstitialFragment extends SettingsPreferenceFragment
84            implements RadioGroup.OnCheckedChangeListener {
85
86        private RadioGroup mRadioGroup;
87        private RadioButton mShowAllButton;
88        private RadioButton mRedactSensitiveButton;
89
90        @Override
91        protected int getMetricsCategory() {
92            return MetricsLogger.NOTIFICATION_REDACTION;
93        }
94
95        @Override
96        public View onCreateView(LayoutInflater inflater, ViewGroup container,
97                Bundle savedInstanceState) {
98            return inflater.inflate(R.layout.redaction_interstitial, container, false);
99        }
100
101        @Override
102        public void onViewCreated(View view, Bundle savedInstanceState) {
103            super.onViewCreated(view, savedInstanceState);
104            mRadioGroup = (RadioGroup) view.findViewById(R.id.radio_group);
105            mShowAllButton = (RadioButton) view.findViewById(R.id.show_all);
106            mRedactSensitiveButton = (RadioButton) view.findViewById(R.id.redact_sensitive);
107
108            mRadioGroup.setOnCheckedChangeListener(this);
109
110            // Disable buttons according to policy.
111            if (isSecureNotificationsDisabled(getActivity())) {
112                mShowAllButton.setEnabled(false);
113                mRedactSensitiveButton.setEnabled(false);
114            } else if (isUnredactedNotificationsDisabled(getActivity())) {
115                mShowAllButton.setEnabled(false);
116            }
117        }
118
119        @Override
120        public void onResume() {
121            super.onResume();
122            loadFromSettings();
123        }
124
125        private void loadFromSettings() {
126            final boolean enabled = Settings.Secure.getInt(getContentResolver(),
127                        Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 0) != 0;
128            final boolean show = Settings.Secure.getInt(getContentResolver(),
129                        Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, 1) != 0;
130
131            int checkedButtonId = R.id.hide_all;
132            if (enabled) {
133                if (show && mShowAllButton.isEnabled()) {
134                    checkedButtonId = R.id.show_all;
135                } else if (mRedactSensitiveButton.isEnabled()) {
136                    checkedButtonId = R.id.redact_sensitive;
137                }
138            }
139
140            mRadioGroup.check(checkedButtonId);
141        }
142
143        @Override
144        public void onCheckedChanged(RadioGroup group, int checkedId) {
145            final boolean show = (checkedId == R.id.show_all);
146            final boolean enabled = (checkedId != R.id.hide_all);
147
148            Settings.Secure.putInt(getContentResolver(),
149                    Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS, show ? 1 : 0);
150            Settings.Secure.putInt(getContentResolver(),
151                    Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, enabled ? 1 : 0);
152        }
153    }
154}
155