[Refactor] Extract work profile related logic from AllAppsContainerView to WorkProfileManager

Bug: 195623679
Test: presubmit
Change-Id: I9954fb40034d1804aaf19f0778f95477e48ccc8f
This commit is contained in:
y
2021-08-31 20:24:33 -07:00
committed by sfufa@google.com
parent 7f693253c8
commit eb52419282
7 changed files with 313 additions and 147 deletions

View File

@@ -16,43 +16,41 @@
package com.android.launcher3.allapps;
import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_TURN_OFF_WORK_APPS_TAP;
import static com.android.launcher3.util.Executors.UI_HELPER_EXECUTOR;
import android.content.Context;
import android.graphics.Insets;
import android.graphics.Rect;
import android.os.Build;
import android.os.Process;
import android.os.UserHandle;
import android.os.UserManager;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowInsets;
import android.widget.Button;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import com.android.launcher3.BaseDraggingActivity;
import com.android.launcher3.DeviceProfile;
import com.android.launcher3.Insettable;
import com.android.launcher3.Launcher;
import com.android.launcher3.Utilities;
import com.android.launcher3.anim.KeyboardInsetAnimationCallback;
import com.android.launcher3.pm.UserCache;
import com.android.launcher3.workprofile.PersonalWorkSlidingTabStrip;
/**
* Work profile toggle switch shown at the bottom of AllApps work tab
*/
public class WorkModeSwitch extends Button implements Insettable, View.OnClickListener {
public class WorkModeSwitch extends Button implements Insettable, View.OnClickListener,
KeyboardInsetAnimationCallback.KeyboardInsetListener,
PersonalWorkSlidingTabStrip.OnActivePageChangedListener {
private Rect mInsets = new Rect();
private static final int FLAG_FADE_ONGOING = 1 << 1;
private static final int FLAG_TRANSLATION_ONGOING = 1 << 2;
private static final int FLAG_PROFILE_TOGGLE_ONGOING = 1 << 3;
private final Rect mInsets = new Rect();
private int mFlags;
private boolean mWorkEnabled;
private boolean mOnWorkTab;
@Nullable
private KeyboardInsetAnimationCallback mKeyboardInsetAnimationCallback;
private boolean mWorkTabVisible;
public WorkModeSwitch(Context context) {
this(context, null, 0);
}
@@ -71,9 +69,12 @@ public class WorkModeSwitch extends Button implements Insettable, View.OnClickLi
setSelected(true);
setOnClickListener(this);
if (Utilities.ATLEAST_R) {
mKeyboardInsetAnimationCallback = new KeyboardInsetAnimationCallback(this);
setWindowInsetsAnimationCallback(mKeyboardInsetAnimationCallback);
KeyboardInsetAnimationCallback keyboardInsetAnimationCallback =
new KeyboardInsetAnimationCallback(this);
setWindowInsetsAnimationCallback(keyboardInsetAnimationCallback);
}
DeviceProfile grid = BaseDraggingActivity.fromContext(getContext()).getDeviceProfile();
setInsets(grid.getInsets());
}
@Override
@@ -87,57 +88,57 @@ public class WorkModeSwitch extends Button implements Insettable, View.OnClickLi
}
}
/**
* Animates in/out work profile toggle panel based on the tab user is on
*/
public void setWorkTabVisible(boolean workTabVisible) {
clearAnimation();
mWorkTabVisible = workTabVisible;
if (workTabVisible && mWorkEnabled) {
setEnabled(true);
setVisibility(VISIBLE);
setAlpha(0);
animate().alpha(1).start();
} else {
animate().alpha(0).withEndAction(() -> this.setVisibility(GONE)).start();
}
@Override
public void onActivePageChanged(int page) {
mOnWorkTab = page == AllAppsContainerView.AdapterHolder.WORK;
updateVisibility();
}
@Override
public void onClick(View view) {
if (Utilities.ATLEAST_P && mWorkTabVisible) {
setEnabled(false);
Launcher.fromContext(getContext()).getStatsLogManager().logger().log(
LAUNCHER_TURN_OFF_WORK_APPS_TAP);
UI_HELPER_EXECUTOR.post(() -> setWorkProfileEnabled(getContext(), false));
if (Utilities.ATLEAST_P && isEnabled()) {
setFlag(FLAG_PROFILE_TOGGLE_ONGOING);
Launcher launcher = Launcher.getLauncher(getContext());
launcher.getStatsLogManager().logger().log(LAUNCHER_TURN_OFF_WORK_APPS_TAP);
launcher.getAppsView().getWorkManager().setWorkProfileEnabled(false);
}
}
@Override
public boolean isEnabled() {
return super.isEnabled() && getVisibility() == VISIBLE && mFlags == 0;
}
/**
* Sets the enabled or disabled state of the button
*/
public void updateCurrentState(boolean active) {
removeFlag(FLAG_PROFILE_TOGGLE_ONGOING);
mWorkEnabled = active;
setEnabled(true);
setVisibility(active ? VISIBLE : GONE);
updateVisibility();
}
@RequiresApi(Build.VERSION_CODES.P)
public static Boolean setWorkProfileEnabled(Context context, boolean enabled) {
UserManager userManager = context.getSystemService(UserManager.class);
boolean showConfirm = false;
for (UserHandle userProfile : UserCache.INSTANCE.get(context).getUserProfiles()) {
if (Process.myUserHandle().equals(userProfile)) {
continue;
}
showConfirm |= !userManager.requestQuietModeEnabled(!enabled, userProfile);
private void updateVisibility() {
clearAnimation();
if (mWorkEnabled && mOnWorkTab) {
setFlag(FLAG_FADE_ONGOING);
setVisibility(VISIBLE);
setAlpha(0);
animate().alpha(1).withEndAction(() -> removeFlag(FLAG_FADE_ONGOING)).start();
} else if (getVisibility() != GONE) {
setFlag(FLAG_FADE_ONGOING);
animate().alpha(0).withEndAction(() -> {
removeFlag(FLAG_FADE_ONGOING);
this.setVisibility(GONE);
}).start();
}
return showConfirm;
}
@Override
public WindowInsets onApplyWindowInsets(WindowInsets insets) {
if (Utilities.ATLEAST_R && mWorkTabVisible) {
if (Utilities.ATLEAST_R && isEnabled()) {
setTranslationY(0);
if (insets.isVisible(WindowInsets.Type.ime())) {
Insets keyboardInsets = insets.getInsets(WindowInsets.Type.ime());
@@ -146,4 +147,22 @@ public class WorkModeSwitch extends Button implements Insettable, View.OnClickLi
}
return insets;
}
@Override
public void onTranslationStart() {
setFlag(FLAG_TRANSLATION_ONGOING);
}
@Override
public void onTranslationEnd() {
removeFlag(FLAG_TRANSLATION_ONGOING);
}
private void setFlag(int flag) {
mFlags |= flag;
}
private void removeFlag(int flag) {
mFlags &= ~flag;
}
}