2018-01-03 14:41:31 +00:00
|
|
|
/*
|
2020-03-19 16:34:24 -07:00
|
|
|
* Copyright (C) 2020 The Android Open Source Project
|
2018-01-03 14:41:31 +00:00
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
package com.android.launcher3.allapps;
|
|
|
|
|
|
2020-03-19 16:34:24 -07:00
|
|
|
import static com.android.launcher3.util.PackageManagerHelper.hasShortcutsPermission;
|
|
|
|
|
|
|
|
|
|
import android.animation.ObjectAnimator;
|
|
|
|
|
import android.animation.PropertyValuesHolder;
|
2018-01-03 14:41:31 +00:00
|
|
|
import android.content.Context;
|
2020-03-19 16:34:24 -07:00
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
|
import android.graphics.Rect;
|
2018-01-03 14:41:31 +00:00
|
|
|
import android.os.AsyncTask;
|
|
|
|
|
import android.os.Process;
|
|
|
|
|
import android.os.UserHandle;
|
2019-12-10 12:19:13 -08:00
|
|
|
import android.os.UserManager;
|
2018-01-03 14:41:31 +00:00
|
|
|
import android.util.AttributeSet;
|
2018-07-16 17:47:39 +01:00
|
|
|
import android.view.MotionEvent;
|
2018-01-03 14:41:31 +00:00
|
|
|
import android.widget.Switch;
|
|
|
|
|
|
2020-03-19 16:34:24 -07:00
|
|
|
import com.android.launcher3.Insettable;
|
|
|
|
|
import com.android.launcher3.Launcher;
|
|
|
|
|
import com.android.launcher3.R;
|
2019-12-10 12:19:13 -08:00
|
|
|
import com.android.launcher3.Utilities;
|
|
|
|
|
import com.android.launcher3.pm.UserCache;
|
2018-01-03 14:41:31 +00:00
|
|
|
|
2018-08-02 16:30:51 -07:00
|
|
|
import java.lang.ref.WeakReference;
|
2018-01-03 14:41:31 +00:00
|
|
|
|
2020-03-19 16:34:24 -07:00
|
|
|
/**
|
|
|
|
|
* Work profile toggle switch shown at the bottom of AllApps work tab
|
|
|
|
|
*/
|
|
|
|
|
public class WorkModeSwitch extends Switch implements Insettable {
|
|
|
|
|
|
|
|
|
|
private Rect mInsets = new Rect();
|
|
|
|
|
protected ObjectAnimator mOpenCloseAnimator;
|
|
|
|
|
|
2018-01-03 14:41:31 +00:00
|
|
|
|
|
|
|
|
public WorkModeSwitch(Context context) {
|
|
|
|
|
super(context);
|
2020-03-19 16:34:24 -07:00
|
|
|
init();
|
2018-01-03 14:41:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public WorkModeSwitch(Context context, AttributeSet attrs) {
|
|
|
|
|
super(context, attrs);
|
2020-03-19 16:34:24 -07:00
|
|
|
init();
|
2018-01-03 14:41:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public WorkModeSwitch(Context context, AttributeSet attrs, int defStyleAttr) {
|
|
|
|
|
super(context, attrs, defStyleAttr);
|
2020-03-19 16:34:24 -07:00
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void init() {
|
|
|
|
|
mOpenCloseAnimator = ObjectAnimator.ofPropertyValuesHolder(this);
|
2018-01-03 14:41:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void setChecked(boolean checked) {
|
2020-03-19 16:34:24 -07:00
|
|
|
|
2018-01-03 14:41:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void toggle() {
|
|
|
|
|
trySetQuietModeEnabledToAllProfilesAsync(isChecked());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void setCheckedInternal(boolean checked) {
|
|
|
|
|
super.setChecked(checked);
|
2020-03-19 16:34:24 -07:00
|
|
|
setCompoundDrawablesWithIntrinsicBounds(
|
|
|
|
|
checked ? R.drawable.ic_corp : R.drawable.ic_corp_off, 0, 0, 0);
|
2018-01-03 14:41:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void refresh() {
|
2020-03-19 16:34:24 -07:00
|
|
|
if (!shouldShowWorkSwitch()) return;
|
2019-12-10 12:19:13 -08:00
|
|
|
UserCache userManager = UserCache.INSTANCE.get(getContext());
|
2018-01-15 14:52:47 +00:00
|
|
|
setCheckedInternal(!userManager.isAnyProfileQuietModeEnabled());
|
|
|
|
|
setEnabled(true);
|
2018-01-03 14:41:31 +00:00
|
|
|
}
|
|
|
|
|
|
2020-03-19 16:34:24 -07:00
|
|
|
@Override
|
|
|
|
|
protected void onLayout(boolean changed, int l, int t, int r, int b) {
|
|
|
|
|
super.onLayout(changed, l, t, r, b);
|
|
|
|
|
this.setVisibility(shouldShowWorkSwitch() ? VISIBLE : GONE);
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-16 17:47:39 +01:00
|
|
|
@Override
|
|
|
|
|
public boolean onTouchEvent(MotionEvent ev) {
|
|
|
|
|
return ev.getActionMasked() == MotionEvent.ACTION_MOVE || super.onTouchEvent(ev);
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-03 14:41:31 +00:00
|
|
|
private void trySetQuietModeEnabledToAllProfilesAsync(boolean enabled) {
|
2018-08-02 16:30:51 -07:00
|
|
|
new SetQuietModeEnabledAsyncTask(enabled, new WeakReference<>(this)).execute();
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-19 16:34:24 -07:00
|
|
|
@Override
|
|
|
|
|
public void setInsets(Rect insets) {
|
|
|
|
|
int bottomInset = insets.bottom - mInsets.bottom;
|
|
|
|
|
mInsets.set(insets);
|
|
|
|
|
setPadding(getPaddingLeft(), getPaddingTop(), getPaddingRight(),
|
|
|
|
|
getPaddingBottom() + bottomInset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Animates in/out work profile toggle panel based on the tab user is on
|
|
|
|
|
*/
|
|
|
|
|
public void setWorkTabVisible(boolean workTabVisible) {
|
|
|
|
|
if (!shouldShowWorkSwitch()) return;
|
|
|
|
|
|
|
|
|
|
mOpenCloseAnimator.setValues(PropertyValuesHolder.ofFloat(ALPHA, workTabVisible ? 1 : 0));
|
|
|
|
|
mOpenCloseAnimator.start();
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-02 16:30:51 -07:00
|
|
|
private static final class SetQuietModeEnabledAsyncTask
|
|
|
|
|
extends AsyncTask<Void, Void, Boolean> {
|
|
|
|
|
|
|
|
|
|
private final boolean enabled;
|
|
|
|
|
private final WeakReference<WorkModeSwitch> switchWeakReference;
|
2018-01-03 14:41:31 +00:00
|
|
|
|
2018-08-02 16:30:51 -07:00
|
|
|
SetQuietModeEnabledAsyncTask(boolean enabled,
|
|
|
|
|
WeakReference<WorkModeSwitch> switchWeakReference) {
|
|
|
|
|
this.enabled = enabled;
|
|
|
|
|
this.switchWeakReference = switchWeakReference;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onPreExecute() {
|
|
|
|
|
super.onPreExecute();
|
|
|
|
|
WorkModeSwitch workModeSwitch = switchWeakReference.get();
|
|
|
|
|
if (workModeSwitch != null) {
|
|
|
|
|
workModeSwitch.setEnabled(false);
|
2018-01-03 14:41:31 +00:00
|
|
|
}
|
2018-08-02 16:30:51 -07:00
|
|
|
}
|
2018-01-03 14:41:31 +00:00
|
|
|
|
2018-08-02 16:30:51 -07:00
|
|
|
@Override
|
|
|
|
|
protected Boolean doInBackground(Void... voids) {
|
|
|
|
|
WorkModeSwitch workModeSwitch = switchWeakReference.get();
|
2019-12-10 12:19:13 -08:00
|
|
|
if (workModeSwitch == null || !Utilities.ATLEAST_P) {
|
2018-08-02 16:30:51 -07:00
|
|
|
return false;
|
|
|
|
|
}
|
2019-12-10 12:19:13 -08:00
|
|
|
|
|
|
|
|
Context context = workModeSwitch.getContext();
|
|
|
|
|
UserManager userManager = context.getSystemService(UserManager.class);
|
2018-08-02 16:30:51 -07:00
|
|
|
boolean showConfirm = false;
|
2019-12-10 12:19:13 -08:00
|
|
|
for (UserHandle userProfile : UserCache.INSTANCE.get(context).getUserProfiles()) {
|
2018-08-02 16:30:51 -07:00
|
|
|
if (Process.myUserHandle().equals(userProfile)) {
|
|
|
|
|
continue;
|
2018-01-03 14:41:31 +00:00
|
|
|
}
|
2018-08-02 16:30:51 -07:00
|
|
|
showConfirm |= !userManager.requestQuietModeEnabled(enabled, userProfile);
|
2018-01-03 14:41:31 +00:00
|
|
|
}
|
2018-08-02 16:30:51 -07:00
|
|
|
return showConfirm;
|
|
|
|
|
}
|
2018-01-03 14:41:31 +00:00
|
|
|
|
2018-08-02 16:30:51 -07:00
|
|
|
@Override
|
|
|
|
|
protected void onPostExecute(Boolean showConfirm) {
|
|
|
|
|
if (showConfirm) {
|
|
|
|
|
WorkModeSwitch workModeSwitch = switchWeakReference.get();
|
|
|
|
|
if (workModeSwitch != null) {
|
|
|
|
|
workModeSwitch.setEnabled(true);
|
2018-01-03 14:41:31 +00:00
|
|
|
}
|
|
|
|
|
}
|
2018-08-02 16:30:51 -07:00
|
|
|
}
|
2018-01-03 14:41:31 +00:00
|
|
|
}
|
2020-03-19 16:34:24 -07:00
|
|
|
|
|
|
|
|
private boolean shouldShowWorkSwitch() {
|
|
|
|
|
Launcher launcher = Launcher.getLauncher(getContext());
|
|
|
|
|
return Utilities.ATLEAST_P && (hasShortcutsPermission(launcher)
|
|
|
|
|
|| launcher.checkSelfPermission("android.permission.MODIFY_QUIET_MODE")
|
|
|
|
|
== PackageManager.PERMISSION_GRANTED);
|
|
|
|
|
}
|
2018-01-03 14:41:31 +00:00
|
|
|
}
|