1 /*
2  * Copyright (C) 2016 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 com.android.settings.R;
20 import com.android.settings.RestrictedListPreference;
21 import com.android.settings.Utils;
22 import com.android.settingslib.RestrictedLockUtils;
23 
24 import android.app.AlertDialog;
25 import android.app.Dialog;
26 import android.content.Context;
27 import android.content.DialogInterface;
28 import android.os.Bundle;
29 import android.os.UserHandle;
30 import android.os.UserManager;
31 import android.provider.Settings;
32 import android.util.AttributeSet;
33 import android.view.View;
34 import android.widget.CheckBox;
35 import android.widget.CompoundButton;
36 import android.widget.ImageView;
37 import android.widget.ListAdapter;
38 import android.widget.ListView;
39 
40 public class NotificationLockscreenPreference extends RestrictedListPreference {
41 
42     private boolean mAllowRemoteInput;
43     private Listener mListener;
44     private boolean mShowRemoteInput;
45     private boolean mRemoteInputCheckBoxEnabled = true;
46     private int mUserId = UserHandle.myUserId();
47     private RestrictedLockUtils.EnforcedAdmin mAdminRestrictingRemoteInput;
48 
NotificationLockscreenPreference(Context context, AttributeSet attrs)49     public NotificationLockscreenPreference(Context context, AttributeSet attrs) {
50         super(context, attrs);
51     }
52 
setRemoteInputCheckBoxEnabled(boolean enabled)53     public void setRemoteInputCheckBoxEnabled(boolean enabled) {
54         mRemoteInputCheckBoxEnabled = enabled;
55     }
56 
setRemoteInputRestricted(RestrictedLockUtils.EnforcedAdmin admin)57     public void setRemoteInputRestricted(RestrictedLockUtils.EnforcedAdmin admin) {
58         mAdminRestrictingRemoteInput = admin;
59     }
60 
61     @Override
onClick()62     protected void onClick() {
63         final Context context = getContext();
64         if (!Utils.startQuietModeDialogIfNecessary(context, UserManager.get(context), mUserId)) {
65             // Call super to create preference dialog only when work mode is on
66             // startQuietModeDialogIfNecessary will return false if mUserId is not a managed user
67             super.onClick();
68         }
69     }
70 
setUserId(int userId)71     public void setUserId(int userId) {
72         mUserId = userId;
73     }
74 
75     @Override
onPrepareDialogBuilder(AlertDialog.Builder builder, DialogInterface.OnClickListener innerListener)76     protected void onPrepareDialogBuilder(AlertDialog.Builder builder,
77             DialogInterface.OnClickListener innerListener) {
78 
79         mListener = new Listener(innerListener);
80         builder.setSingleChoiceItems(createListAdapter(), getSelectedValuePos(), mListener);
81         mShowRemoteInput = getEntryValues().length == 3;
82         mAllowRemoteInput = Settings.Secure.getInt(getContext().getContentResolver(),
83                 Settings.Secure.LOCK_SCREEN_ALLOW_REMOTE_INPUT, 0) != 0;
84         builder.setView(R.layout.lockscreen_remote_input);
85     }
86 
87     @Override
onDialogCreated(Dialog dialog)88     protected void onDialogCreated(Dialog dialog) {
89         super.onDialogCreated(dialog);
90         dialog.create();
91         CheckBox checkbox = (CheckBox) dialog.findViewById(R.id.lockscreen_remote_input);
92         checkbox.setChecked(!mAllowRemoteInput);
93         checkbox.setOnCheckedChangeListener(mListener);
94         checkbox.setEnabled(mAdminRestrictingRemoteInput == null);
95 
96         View restricted = dialog.findViewById(R.id.restricted_lock_icon_remote_input);
97         restricted.setVisibility(mAdminRestrictingRemoteInput == null ? View.GONE : View.VISIBLE);
98 
99         if (mAdminRestrictingRemoteInput != null) {
100             checkbox.setClickable(false);
101             dialog.findViewById(com.android.internal.R.id.customPanel)
102                     .setOnClickListener(mListener);
103         }
104     }
105 
106     @Override
onDialogStateRestored(Dialog dialog, Bundle savedInstanceState)107     protected void onDialogStateRestored(Dialog dialog, Bundle savedInstanceState) {
108         super.onDialogStateRestored(dialog, savedInstanceState);
109         ListView listView = ((AlertDialog) dialog).getListView();
110         int selectedPosition = listView.getCheckedItemPosition();
111 
112         View panel = dialog.findViewById(com.android.internal.R.id.customPanel);
113         panel.setVisibility(checkboxVisibilityForSelectedIndex(selectedPosition,
114                 mShowRemoteInput));
115         mListener.setView(panel);
116     }
117 
118     @Override
createListAdapter()119     protected ListAdapter createListAdapter() {
120         return new RestrictedArrayAdapter(getContext(), getEntries(), -1);
121     }
122 
123     @Override
onDialogClosed(boolean positiveResult)124     protected void onDialogClosed(boolean positiveResult) {
125         super.onDialogClosed(positiveResult);
126         Settings.Secure.putInt(getContext().getContentResolver(),
127                 Settings.Secure.LOCK_SCREEN_ALLOW_REMOTE_INPUT, mAllowRemoteInput ? 1 : 0);
128     }
129 
130     @Override
isAutoClosePreference()131     protected boolean isAutoClosePreference() {
132         return false;
133     }
134 
checkboxVisibilityForSelectedIndex(int selected, boolean showRemoteAtAll)135     private int checkboxVisibilityForSelectedIndex(int selected,
136             boolean showRemoteAtAll) {
137         return selected == 1 && showRemoteAtAll && mRemoteInputCheckBoxEnabled ? View.VISIBLE
138                 : View.GONE;
139     }
140 
141     private class Listener implements DialogInterface.OnClickListener,
142             CompoundButton.OnCheckedChangeListener, View.OnClickListener {
143 
144         private final DialogInterface.OnClickListener mInner;
145         private View mView;
146 
Listener(DialogInterface.OnClickListener inner)147         public Listener(DialogInterface.OnClickListener inner) {
148             mInner = inner;
149         }
150 
151         @Override
onClick(DialogInterface dialog, int which)152         public void onClick(DialogInterface dialog, int which) {
153             mInner.onClick(dialog, which);
154             ListView listView = ((AlertDialog) dialog).getListView();
155             int selectedPosition = listView.getCheckedItemPosition();
156             if (mView != null) {
157                 mView.setVisibility(
158                         checkboxVisibilityForSelectedIndex(selectedPosition, mShowRemoteInput));
159             }
160         }
161 
162         @Override
onCheckedChanged(CompoundButton buttonView, boolean isChecked)163         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
164             mAllowRemoteInput = !isChecked;
165         }
166 
setView(View view)167         public void setView(View view) {
168             mView = view;
169         }
170 
171         @Override
onClick(View v)172         public void onClick(View v) {
173             if (v.getId() == com.android.internal.R.id.customPanel) {
174                 RestrictedLockUtils.sendShowAdminSupportDetailsIntent(getContext(),
175                         mAdminRestrictingRemoteInput);
176             }
177         }
178     }
179 }
180