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 android.app.Dialog; 20 import android.content.Context; 21 import android.content.DialogInterface; 22 import android.os.Bundle; 23 import android.os.UserHandle; 24 import android.os.UserManager; 25 import android.provider.Settings; 26 import android.util.AttributeSet; 27 import android.view.View; 28 import android.widget.CheckBox; 29 import android.widget.CompoundButton; 30 import android.widget.ListView; 31 32 import androidx.appcompat.app.AlertDialog; 33 import androidx.appcompat.app.AlertDialog.Builder; 34 35 import com.android.settings.R; 36 import com.android.settings.RestrictedListPreference; 37 import com.android.settings.Utils; 38 import com.android.settingslib.RestrictedLockUtils; 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(Builder builder, DialogInterface.OnClickListener innerListener)76 protected void onPrepareDialogBuilder(Builder builder, 77 DialogInterface.OnClickListener innerListener) { 78 79 mListener = new Listener(innerListener); 80 builder.setSingleChoiceItems(createListAdapter(builder.getContext()), getSelectedValuePos(), 81 mListener); 82 mShowRemoteInput = getEntryValues().length == 3; 83 mAllowRemoteInput = Settings.Secure.getInt(getContext().getContentResolver(), 84 Settings.Secure.LOCK_SCREEN_ALLOW_REMOTE_INPUT, 0) != 0; 85 builder.setView(R.layout.lockscreen_remote_input); 86 } 87 88 @Override onDialogCreated(Dialog dialog)89 protected void onDialogCreated(Dialog dialog) { 90 super.onDialogCreated(dialog); 91 dialog.create(); 92 CheckBox checkbox = (CheckBox) dialog.findViewById(R.id.lockscreen_remote_input); 93 checkbox.setChecked(!mAllowRemoteInput); 94 checkbox.setOnCheckedChangeListener(mListener); 95 checkbox.setEnabled(mAdminRestrictingRemoteInput == null); 96 97 View restricted = dialog.findViewById(R.id.restricted_lock_icon_remote_input); 98 restricted.setVisibility(mAdminRestrictingRemoteInput == null ? View.GONE : View.VISIBLE); 99 100 if (mAdminRestrictingRemoteInput != null) { 101 checkbox.setClickable(false); 102 dialog.findViewById(com.android.internal.R.id.customPanel) 103 .setOnClickListener(mListener); 104 } 105 } 106 107 @Override onDialogStateRestored(Dialog dialog, Bundle savedInstanceState)108 protected void onDialogStateRestored(Dialog dialog, Bundle savedInstanceState) { 109 super.onDialogStateRestored(dialog, savedInstanceState); 110 ListView listView = ((AlertDialog) dialog).getListView(); 111 int selectedPosition = listView.getCheckedItemPosition(); 112 113 View panel = dialog.findViewById(com.android.internal.R.id.customPanel); 114 panel.setVisibility(checkboxVisibilityForSelectedIndex(selectedPosition, 115 mShowRemoteInput)); 116 mListener.setView(panel); 117 } 118 119 @Override onDialogClosed(boolean positiveResult)120 protected void onDialogClosed(boolean positiveResult) { 121 super.onDialogClosed(positiveResult); 122 Settings.Secure.putInt(getContext().getContentResolver(), 123 Settings.Secure.LOCK_SCREEN_ALLOW_REMOTE_INPUT, mAllowRemoteInput ? 1 : 0); 124 } 125 126 @Override isAutoClosePreference()127 protected boolean isAutoClosePreference() { 128 return false; 129 } 130 checkboxVisibilityForSelectedIndex(int selected, boolean showRemoteAtAll)131 private int checkboxVisibilityForSelectedIndex(int selected, 132 boolean showRemoteAtAll) { 133 return selected == 1 && showRemoteAtAll && mRemoteInputCheckBoxEnabled ? View.VISIBLE 134 : View.GONE; 135 } 136 137 private class Listener implements DialogInterface.OnClickListener, 138 CompoundButton.OnCheckedChangeListener, View.OnClickListener { 139 140 private final DialogInterface.OnClickListener mInner; 141 private View mView; 142 Listener(DialogInterface.OnClickListener inner)143 public Listener(DialogInterface.OnClickListener inner) { 144 mInner = inner; 145 } 146 147 @Override onClick(DialogInterface dialog, int which)148 public void onClick(DialogInterface dialog, int which) { 149 mInner.onClick(dialog, which); 150 ListView listView = ((AlertDialog) dialog).getListView(); 151 int selectedPosition = listView.getCheckedItemPosition(); 152 if (mView != null) { 153 mView.setVisibility( 154 checkboxVisibilityForSelectedIndex(selectedPosition, mShowRemoteInput)); 155 } 156 } 157 158 @Override onCheckedChanged(CompoundButton buttonView, boolean isChecked)159 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 160 mAllowRemoteInput = !isChecked; 161 } 162 setView(View view)163 public void setView(View view) { 164 mView = view; 165 } 166 167 @Override onClick(View v)168 public void onClick(View v) { 169 if (v.getId() == com.android.internal.R.id.customPanel) { 170 RestrictedLockUtils.sendShowAdminSupportDetailsIntent(getContext(), 171 mAdminRestrictingRemoteInput); 172 } 173 } 174 } 175 } 176