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;
|
|
|
|
|
|
2021-06-15 18:46:30 -07:00
|
|
|
import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_TURN_OFF_WORK_APPS_TAP;
|
2021-05-25 19:21:37 -05:00
|
|
|
import static com.android.launcher3.util.Executors.UI_HELPER_EXECUTOR;
|
|
|
|
|
|
2018-01-03 14:41:31 +00:00
|
|
|
import android.content.Context;
|
2021-10-17 22:24:17 +07:00
|
|
|
import android.content.res.ColorStateList;
|
2021-06-16 09:46:25 -05:00
|
|
|
import android.graphics.Insets;
|
2020-03-19 16:34:24 -07:00
|
|
|
import android.graphics.Rect;
|
2021-05-25 19:21:37 -05:00
|
|
|
import android.os.Build;
|
2018-01-03 14:41:31 +00:00
|
|
|
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;
|
2021-05-25 19:21:37 -05:00
|
|
|
import android.view.View;
|
|
|
|
|
import android.view.ViewGroup;
|
2021-06-16 09:46:25 -05:00
|
|
|
import android.view.WindowInsets;
|
2021-05-25 19:21:37 -05:00
|
|
|
import android.widget.Button;
|
|
|
|
|
|
2021-06-16 09:46:25 -05:00
|
|
|
import androidx.annotation.Nullable;
|
2021-05-25 19:21:37 -05:00
|
|
|
import androidx.annotation.RequiresApi;
|
2018-01-03 14:41:31 +00:00
|
|
|
|
2020-03-19 16:34:24 -07:00
|
|
|
import com.android.launcher3.Insettable;
|
2021-06-15 18:46:30 -07:00
|
|
|
import com.android.launcher3.Launcher;
|
2019-12-10 12:19:13 -08:00
|
|
|
import com.android.launcher3.Utilities;
|
2021-06-16 09:46:25 -05:00
|
|
|
import com.android.launcher3.anim.KeyboardInsetAnimationCallback;
|
2019-12-10 12:19:13 -08:00
|
|
|
import com.android.launcher3.pm.UserCache;
|
2018-01-03 14:41:31 +00:00
|
|
|
|
2021-10-17 22:24:17 +07:00
|
|
|
import app.lawnchair.font.FontManager;
|
|
|
|
|
import app.lawnchair.theme.color.ColorStateListTokens;
|
|
|
|
|
import app.lawnchair.theme.drawable.DrawableTokens;
|
|
|
|
|
|
2020-03-19 16:34:24 -07:00
|
|
|
/**
|
|
|
|
|
* Work profile toggle switch shown at the bottom of AllApps work tab
|
|
|
|
|
*/
|
2021-05-25 19:21:37 -05:00
|
|
|
public class WorkModeSwitch extends Button implements Insettable, View.OnClickListener {
|
2020-03-19 16:34:24 -07:00
|
|
|
|
2020-03-31 03:54:50 -07:00
|
|
|
private Rect mInsets = new Rect();
|
2021-05-25 19:21:37 -05:00
|
|
|
private boolean mWorkEnabled;
|
2020-04-13 16:47:47 -07:00
|
|
|
|
2021-06-16 09:46:25 -05:00
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
|
private KeyboardInsetAnimationCallback mKeyboardInsetAnimationCallback;
|
|
|
|
|
|
2018-01-03 14:41:31 +00:00
|
|
|
public WorkModeSwitch(Context context) {
|
2021-05-25 19:21:37 -05:00
|
|
|
this(context, null, 0);
|
2018-01-03 14:41:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public WorkModeSwitch(Context context, AttributeSet attrs) {
|
2021-05-25 19:21:37 -05:00
|
|
|
this(context, attrs, 0);
|
2018-01-03 14:41:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public WorkModeSwitch(Context context, AttributeSet attrs, int defStyleAttr) {
|
|
|
|
|
super(context, attrs, defStyleAttr);
|
2021-10-17 22:24:17 +07:00
|
|
|
|
|
|
|
|
FontManager.INSTANCE.get(context).overrideFont(this, attrs);
|
2018-01-03 14:41:31 +00:00
|
|
|
}
|
|
|
|
|
|
2020-04-13 16:47:47 -07:00
|
|
|
@Override
|
2021-05-25 19:21:37 -05:00
|
|
|
protected void onFinishInflate() {
|
|
|
|
|
super.onFinishInflate();
|
2021-06-17 11:34:20 -05:00
|
|
|
setSelected(true);
|
2021-05-25 19:21:37 -05:00
|
|
|
setOnClickListener(this);
|
2021-06-16 09:46:25 -05:00
|
|
|
if (Utilities.ATLEAST_R) {
|
|
|
|
|
mKeyboardInsetAnimationCallback = new KeyboardInsetAnimationCallback(this);
|
|
|
|
|
setWindowInsetsAnimationCallback(mKeyboardInsetAnimationCallback);
|
|
|
|
|
}
|
2021-10-17 22:24:17 +07:00
|
|
|
|
|
|
|
|
setBackground(DrawableTokens.WorkAppsToggleBackground.resolve(getContext()));
|
|
|
|
|
ColorStateList textColor = ColorStateListTokens.AllAppsTabText.resolve(getContext());
|
|
|
|
|
setTextColor(textColor);
|
|
|
|
|
setCompoundDrawableTintList(textColor);
|
2018-08-02 16:30:51 -07:00
|
|
|
}
|
|
|
|
|
|
2020-03-19 16:34:24 -07:00
|
|
|
@Override
|
|
|
|
|
public void setInsets(Rect insets) {
|
|
|
|
|
int bottomInset = insets.bottom - mInsets.bottom;
|
|
|
|
|
mInsets.set(insets);
|
2021-05-25 19:21:37 -05:00
|
|
|
ViewGroup.MarginLayoutParams marginLayoutParams =
|
|
|
|
|
(ViewGroup.MarginLayoutParams) getLayoutParams();
|
|
|
|
|
if (marginLayoutParams != null) {
|
|
|
|
|
marginLayoutParams.bottomMargin = bottomInset + marginLayoutParams.bottomMargin;
|
|
|
|
|
}
|
2020-03-19 16:34:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Animates in/out work profile toggle panel based on the tab user is on
|
|
|
|
|
*/
|
|
|
|
|
public void setWorkTabVisible(boolean workTabVisible) {
|
2020-03-31 03:54:50 -07:00
|
|
|
clearAnimation();
|
|
|
|
|
if (workTabVisible) {
|
2021-05-25 19:21:37 -05:00
|
|
|
setEnabled(true);
|
2021-06-14 18:27:44 -05:00
|
|
|
if (mWorkEnabled) {
|
|
|
|
|
setVisibility(VISIBLE);
|
|
|
|
|
}
|
2020-03-31 03:54:50 -07:00
|
|
|
setAlpha(0);
|
|
|
|
|
animate().alpha(1).start();
|
|
|
|
|
} else {
|
|
|
|
|
animate().alpha(0).withEndAction(() -> this.setVisibility(GONE)).start();
|
|
|
|
|
}
|
2020-03-19 16:34:24 -07:00
|
|
|
}
|
|
|
|
|
|
2021-05-25 19:21:37 -05:00
|
|
|
@Override
|
|
|
|
|
public void onClick(View view) {
|
|
|
|
|
if (Utilities.ATLEAST_P) {
|
|
|
|
|
setEnabled(false);
|
2021-06-15 18:46:30 -07:00
|
|
|
Launcher.fromContext(getContext()).getStatsLogManager().logger().log(
|
|
|
|
|
LAUNCHER_TURN_OFF_WORK_APPS_TAP);
|
2021-06-14 18:27:44 -05:00
|
|
|
UI_HELPER_EXECUTOR.post(() -> setWorkProfileEnabled(getContext(), false));
|
2018-08-02 16:30:51 -07:00
|
|
|
}
|
2018-01-03 14:41:31 +00:00
|
|
|
}
|
2020-03-19 16:34:24 -07:00
|
|
|
|
2020-03-31 03:54:50 -07:00
|
|
|
/**
|
2021-05-25 19:21:37 -05:00
|
|
|
* Sets the enabled or disabled state of the button
|
2020-03-31 03:54:50 -07:00
|
|
|
*/
|
2021-05-25 19:21:37 -05:00
|
|
|
public void updateCurrentState(boolean active) {
|
|
|
|
|
mWorkEnabled = active;
|
|
|
|
|
setEnabled(true);
|
2021-06-14 18:27:44 -05:00
|
|
|
setVisibility(active ? VISIBLE : GONE);
|
2021-05-25 19:21:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@RequiresApi(Build.VERSION_CODES.P)
|
2021-06-14 18:27:44 -05:00
|
|
|
public static Boolean setWorkProfileEnabled(Context context, boolean enabled) {
|
|
|
|
|
UserManager userManager = context.getSystemService(UserManager.class);
|
2021-05-25 19:21:37 -05:00
|
|
|
boolean showConfirm = false;
|
2021-06-14 18:27:44 -05:00
|
|
|
for (UserHandle userProfile : UserCache.INSTANCE.get(context).getUserProfiles()) {
|
2021-05-25 19:21:37 -05:00
|
|
|
if (Process.myUserHandle().equals(userProfile)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2021-06-14 18:27:44 -05:00
|
|
|
showConfirm |= !userManager.requestQuietModeEnabled(!enabled, userProfile);
|
2020-03-31 03:54:50 -07:00
|
|
|
}
|
2021-05-25 19:21:37 -05:00
|
|
|
return showConfirm;
|
2020-03-31 03:54:50 -07:00
|
|
|
}
|
2021-06-16 09:46:25 -05:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public WindowInsets onApplyWindowInsets(WindowInsets insets) {
|
|
|
|
|
if (Utilities.ATLEAST_R) {
|
|
|
|
|
setTranslationY(0);
|
|
|
|
|
if (insets.isVisible(WindowInsets.Type.ime())) {
|
|
|
|
|
Insets keyboardInsets = insets.getInsets(WindowInsets.Type.ime());
|
|
|
|
|
setTranslationY(mInsets.bottom - keyboardInsets.bottom);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return insets;
|
|
|
|
|
}
|
2018-01-03 14:41:31 +00:00
|
|
|
}
|