Home | History | Annotate | Download | only in allapps
      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.widget.Switch;
     24 
     25 import com.android.launcher3.compat.UserManagerCompat;
     26 
     27 import java.util.List;
     28 
     29 public class WorkModeSwitch extends Switch {
     30 
     31     public WorkModeSwitch(Context context) {
     32         super(context);
     33     }
     34 
     35     public WorkModeSwitch(Context context, AttributeSet attrs) {
     36         super(context, attrs);
     37     }
     38 
     39     public WorkModeSwitch(Context context, AttributeSet attrs, int defStyleAttr) {
     40         super(context, attrs, defStyleAttr);
     41     }
     42 
     43     @Override
     44     public void setChecked(boolean checked) {
     45         // No-op, do not change the checked state until broadcast is received.
     46     }
     47 
     48     @Override
     49     public void toggle() {
     50         trySetQuietModeEnabledToAllProfilesAsync(isChecked());
     51     }
     52 
     53     private void setCheckedInternal(boolean checked) {
     54         super.setChecked(checked);
     55     }
     56 
     57     public void refresh() {
     58         UserManagerCompat userManager = UserManagerCompat.getInstance(getContext());
     59         setCheckedInternal(!userManager.isAnyProfileQuietModeEnabled());
     60         setEnabled(true);
     61     }
     62 
     63     private void trySetQuietModeEnabledToAllProfilesAsync(boolean enabled) {
     64         new AsyncTask<Void, Void, Boolean>() {
     65 
     66             @Override
     67             protected void onPreExecute() {
     68                 super.onPreExecute();
     69                 setEnabled(false);
     70             }
     71 
     72             @Override
     73             protected Boolean doInBackground(Void... voids) {
     74                 UserManagerCompat userManager = UserManagerCompat.getInstance(getContext());
     75                 List<UserHandle> userProfiles = userManager.getUserProfiles();
     76                 boolean showConfirm = false;
     77                 for (UserHandle userProfile : userProfiles) {
     78                     if (Process.myUserHandle().equals(userProfile)) {
     79                         continue;
     80                     }
     81                     showConfirm |= !userManager.requestQuietModeEnabled(enabled, userProfile);
     82                 }
     83                 return showConfirm;
     84             }
     85 
     86             @Override
     87             protected void onPostExecute(Boolean showConfirm) {
     88                 if (showConfirm) {
     89                     setEnabled(true);
     90                 }
     91             }
     92         }.execute();
     93     }
     94 }
     95