1 /* 2 * Copyright (C) 2018 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 package com.android.launcher3.allapps; 17 18 import android.content.Context; 19 import android.os.AsyncTask; 20 import android.os.Process; 21 import android.os.UserHandle; 22 import android.util.AttributeSet; 23 import android.view.MotionEvent; 24 import android.widget.Switch; 25 26 import com.android.launcher3.compat.UserManagerCompat; 27 28 import java.lang.ref.WeakReference; 29 import java.util.List; 30 31 public class WorkModeSwitch extends Switch { 32 WorkModeSwitch(Context context)33 public WorkModeSwitch(Context context) { 34 super(context); 35 } 36 WorkModeSwitch(Context context, AttributeSet attrs)37 public WorkModeSwitch(Context context, AttributeSet attrs) { 38 super(context, attrs); 39 } 40 WorkModeSwitch(Context context, AttributeSet attrs, int defStyleAttr)41 public WorkModeSwitch(Context context, AttributeSet attrs, int defStyleAttr) { 42 super(context, attrs, defStyleAttr); 43 } 44 45 @Override setChecked(boolean checked)46 public void setChecked(boolean checked) { 47 // No-op, do not change the checked state until broadcast is received. 48 } 49 50 @Override toggle()51 public void toggle() { 52 trySetQuietModeEnabledToAllProfilesAsync(isChecked()); 53 } 54 setCheckedInternal(boolean checked)55 private void setCheckedInternal(boolean checked) { 56 super.setChecked(checked); 57 } 58 refresh()59 public void refresh() { 60 UserManagerCompat userManager = UserManagerCompat.getInstance(getContext()); 61 setCheckedInternal(!userManager.isAnyProfileQuietModeEnabled()); 62 setEnabled(true); 63 } 64 65 @Override onTouchEvent(MotionEvent ev)66 public boolean onTouchEvent(MotionEvent ev) { 67 return ev.getActionMasked() == MotionEvent.ACTION_MOVE || super.onTouchEvent(ev); 68 } 69 trySetQuietModeEnabledToAllProfilesAsync(boolean enabled)70 private void trySetQuietModeEnabledToAllProfilesAsync(boolean enabled) { 71 new SetQuietModeEnabledAsyncTask(enabled, new WeakReference<>(this)).execute(); 72 } 73 74 private static final class SetQuietModeEnabledAsyncTask 75 extends AsyncTask<Void, Void, Boolean> { 76 77 private final boolean enabled; 78 private final WeakReference<WorkModeSwitch> switchWeakReference; 79 SetQuietModeEnabledAsyncTask(boolean enabled, WeakReference<WorkModeSwitch> switchWeakReference)80 SetQuietModeEnabledAsyncTask(boolean enabled, 81 WeakReference<WorkModeSwitch> switchWeakReference) { 82 this.enabled = enabled; 83 this.switchWeakReference = switchWeakReference; 84 } 85 86 @Override onPreExecute()87 protected void onPreExecute() { 88 super.onPreExecute(); 89 WorkModeSwitch workModeSwitch = switchWeakReference.get(); 90 if (workModeSwitch != null) { 91 workModeSwitch.setEnabled(false); 92 } 93 } 94 95 @Override doInBackground(Void... voids)96 protected Boolean doInBackground(Void... voids) { 97 WorkModeSwitch workModeSwitch = switchWeakReference.get(); 98 if (workModeSwitch == null) { 99 return false; 100 } 101 UserManagerCompat userManager = 102 UserManagerCompat.getInstance(workModeSwitch.getContext()); 103 List<UserHandle> userProfiles = userManager.getUserProfiles(); 104 boolean showConfirm = false; 105 for (UserHandle userProfile : userProfiles) { 106 if (Process.myUserHandle().equals(userProfile)) { 107 continue; 108 } 109 showConfirm |= !userManager.requestQuietModeEnabled(enabled, userProfile); 110 } 111 return showConfirm; 112 } 113 114 @Override onPostExecute(Boolean showConfirm)115 protected void onPostExecute(Boolean showConfirm) { 116 if (showConfirm) { 117 WorkModeSwitch workModeSwitch = switchWeakReference.get(); 118 if (workModeSwitch != null) { 119 workModeSwitch.setEnabled(true); 120 } 121 } 122 } 123 } 124 } 125