Enable work profile tab in all apps.

This CL will bring two tabs to all apps: Personal and Work,
currently only if the user has a workfile set up and
behind a feature flag defaulting to disabled.

Bug: 68713881

Change-Id: Ib5a558281ef3593359db3ad593ee1d0cf279f547
This commit is contained in:
Mario Bertschler committed 2017-11-14 10:28:54 -08:00
1 parent f405f507b7
commit ac9408a5cd
19 files changed
+1009 -189

No files matched your search

@@ -18,11 +18,18 @@ package com.android.launcher3.allapps;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.os.Process;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Selection;
import android.text.SpannableStringBuilder;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
@@ -47,9 +54,14 @@ import com.android.launcher3.dragndrop.DragController;
import com.android.launcher3.dragndrop.DragOptions;
import com.android.launcher3.keyboard.FocusedItemDecorator;
import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
import com.android.launcher3.util.ComponentKey;
import com.android.launcher3.util.ComponentKeyMapper;
import com.android.launcher3.util.ItemInfoMatcher;
import com.android.launcher3.util.PackageUserKey;
import com.android.launcher3.util.Themes;
import com.android.launcher3.util.TransformingTouchDelegate;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
@@ -63,23 +75,26 @@ public class AllAppsContainerView extends RelativeLayout implements DragSource,
protected final Rect mBasePadding = new Rect();
private final Launcher mLauncher;
private final AlphabeticalAppsList mApps;
private final AllAppsGridAdapter mAdapter;
private final LinearLayoutManager mLayoutManager;
private final AdapterHolder[] mAH;
private final ClickShadowView mTouchFeedbackView;
private final ItemInfoMatcher mPersonalMatcher = ItemInfoMatcher.ofUser(Process.myUserHandle());
private final ItemInfoMatcher mWorkMatcher = ItemInfoMatcher.not(mPersonalMatcher);
private AllAppsRecyclerView mAppsRecyclerView;
private SearchUiManager mSearchUiManager;
private View mSearchContainer;
private InterceptingViewPager mViewPager;
private ViewGroup mHeader;
private FloatingHeaderHandler mFloatingHeaderHandler;
private SpannableStringBuilder mSearchQueryBuilder = null;
private int mNumAppsPerRow;
private int mNumPredictedAppsPerRow;
private SpringAnimationHandler mSpringAnimationHandler;
private TransformingTouchDelegate mTouchDelegate;
private boolean mUsingTabs;
private final HashMap<ComponentKey, AppInfo> mComponentToAppMap = new HashMap<>();
public AllAppsContainerView(Context context) {
this(context, null);
@@ -93,11 +108,7 @@ public class AllAppsContainerView extends RelativeLayout implements DragSource,
super(context, attrs, defStyleAttr);
mLauncher = Launcher.getLauncher(context);
mApps = new AlphabeticalAppsList(context);
mAdapter = new AllAppsGridAdapter(mLauncher, mApps, mLauncher, this);
mSpringAnimationHandler = mAdapter.getSpringAnimationHandler();
mApps.setAdapter(mAdapter);
mLayoutManager = mAdapter.getLayoutManager();
mSearchQueryBuilder = new SpannableStringBuilder();
Selection.setSelection(mSearchQueryBuilder, 0);
@@ -107,6 +118,10 @@ public class AllAppsContainerView extends RelativeLayout implements DragSource,
int size = mLauncher.getDeviceProfile().allAppsIconSizePx
+ mTouchFeedbackView.getExtraSize();
addView(mTouchFeedbackView, size, size);
mAH = new AdapterHolder[2];
mAH[AdapterHolder.MAIN] = new AdapterHolder();
mAH[AdapterHolder.WORK] = new AdapterHolder();
}
@Override
@@ -116,7 +131,17 @@ public class AllAppsContainerView extends RelativeLayout implements DragSource,
DeviceProfile grid = Launcher.getLauncher(getContext()).getDeviceProfile();
grid.addLauncherLayoutChangedListener(this);
mTouchDelegate = new TransformingTouchDelegate(mAppsRecyclerView);
applyTouchDelegate();
}
private void applyTouchDelegate() {
RecyclerView rv = getActiveRecyclerView();
mTouchDelegate = new TransformingTouchDelegate(rv);
mTouchDelegate.setBounds(
rv.getLeft() - mBasePadding.left,
rv.getTop() - mBasePadding.top,
rv.getRight() + mBasePadding.right,
rv.getBottom() + mBasePadding.bottom);
setTouchDelegate(mTouchDelegate);
}
@@ -148,11 +173,7 @@ public class AllAppsContainerView extends RelativeLayout implements DragSource,
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
mTouchDelegate.setBounds(
mAppsRecyclerView.getLeft() - mBasePadding.left,
mAppsRecyclerView.getTop() - mBasePadding.top,
mAppsRecyclerView.getRight() + mBasePadding.right,
mAppsRecyclerView.getBottom() + mBasePadding.bottom);
applyTouchDelegate();
}
@Override
@@ -164,36 +185,69 @@ public class AllAppsContainerView extends RelativeLayout implements DragSource,
* Sets the current set of apps.
*/
public void setApps(List<AppInfo> apps) {
mApps.setApps(apps);
boolean hasWorkProfileApp = hasWorkProfileApp(apps);
if (mUsingTabs != hasWorkProfileApp) {
rebindAdapters(hasWorkProfileApp);
}
mComponentToAppMap.clear();
addOrUpdateApps(apps);
}
/**
* Adds or updates existing apps in the list
*/
public void addOrUpdateApps(List<AppInfo> apps) {
mApps.addOrUpdateApps(apps);
mSearchUiManager.refreshSearchResult();
}
public void updatePromiseAppProgress(PromiseAppInfo app) {
int childCount = mAppsRecyclerView.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = mAppsRecyclerView.getChildAt(i);
if (child instanceof BubbleTextView && child.getTag() == app) {
BubbleTextView bubbleTextView = (BubbleTextView) child;
bubbleTextView.applyProgressLevel(app.level);
}
for (AppInfo app : apps) {
mComponentToAppMap.put(app.toComponentKey(), app);
}
onAppsUpdated();
mSearchUiManager.refreshSearchResult();
}
/**
* Removes some apps from the list.
*/
public void removeApps(List<AppInfo> apps) {
mApps.removeApps(apps);
for (AppInfo app : apps) {
mComponentToAppMap.remove(app.toComponentKey());
}
onAppsUpdated();
mSearchUiManager.refreshSearchResult();
}
private void onAppsUpdated() {
for (int i = 0; i < getNumOfAdapters(); i++) {
mAH[i].appsList.onAppsUpdated();
}
}
private int getNumOfAdapters() {
return mUsingTabs ? mAH.length : 1;
}
public void updatePromiseAppProgress(PromiseAppInfo app) {
for (int i = 0; i < mAH.length; i++) {
updatePromiseAppProgress(app, mAH[i].recyclerView);
}
if (mFloatingHeaderHandler != null) {
updatePromiseAppProgress(app, mFloatingHeaderHandler.getContentView());
}
}
private void updatePromiseAppProgress(PromiseAppInfo app, ViewGroup parent) {
if (parent == null) {
return;
}
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
if (child instanceof BubbleTextView && child.getTag() == app) {
BubbleTextView bubbleTextView = (BubbleTextView) child;
bubbleTextView.applyProgressLevel(app.level);
}
}
}
/**
* Returns whether the view itself will handle the touch event or not.
*/
@@ -203,15 +257,31 @@ public class AllAppsContainerView extends RelativeLayout implements DragSource,
if (mLauncher.getDragLayer().isEventOverView(mSearchContainer, ev)) {
return true;
}
return mAppsRecyclerView.shouldContainerScroll(ev, mLauncher.getDragLayer());
if (mUsingTabs && mLauncher.getDragLayer().isEventOverView(mHeader, ev)) {
return true;
}
AllAppsRecyclerView rv = getActiveRecyclerView();
return rv == null || rv.shouldContainerScroll(ev, mLauncher.getDragLayer());
}
public AllAppsRecyclerView getActiveRecyclerView() {
if (!mUsingTabs || mViewPager.getCurrentItem() == 0) {
return mAH[AdapterHolder.MAIN].recyclerView;
} else {
return mAH[AdapterHolder.WORK].recyclerView;
}
}
/**
* Resets the state of AllApps.
*/
public void reset() {
for (int i = 0; i < mAH.length; i++) {
if (mAH[i].recyclerView != null) {
mAH[i].recyclerView.scrollToTop();
}
}
// Reset the search bar and base recycler view after transitioning home
mAppsRecyclerView.scrollToTop();
mSearchUiManager.reset();
}
@@ -224,33 +294,18 @@ public class AllAppsContainerView extends RelativeLayout implements DragSource,
setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
mAppsRecyclerView.requestFocus();
if (hasFocus && getActiveRecyclerView() != null) {
getActiveRecyclerView().requestFocus();
}
}
});
// Load the all apps recycler view
mAppsRecyclerView = findViewById(R.id.apps_list_view);
mAppsRecyclerView.setApps(mApps);
mAppsRecyclerView.setLayoutManager(mLayoutManager);
mAppsRecyclerView.setAdapter(mAdapter);
mAppsRecyclerView.setHasFixedSize(true);
// No animations will occur when changes occur to the items in this RecyclerView.
mAppsRecyclerView.setItemAnimator(null);
if (FeatureFlags.LAUNCHER3_PHYSICS) {
mAppsRecyclerView.setSpringAnimationHandler(mSpringAnimationHandler);
}
mHeader = findViewById(R.id.all_apps_header);
rebindAdapters(mUsingTabs);
mSearchContainer = findViewById(R.id.search_container_all_apps);
mSearchUiManager = (SearchUiManager) mSearchContainer;
mSearchUiManager.initialize(mApps, mAppsRecyclerView);
FocusedItemDecorator focusedItemDecorator = new FocusedItemDecorator(mAppsRecyclerView);
mAppsRecyclerView.addItemDecoration(focusedItemDecorator);
mAppsRecyclerView.preMeasureViews(mAdapter);
mAdapter.setIconFocusListener(focusedItemDecorator.getFocusListener());
mSearchUiManager.initialize(this);
onLauncherLayoutChanged();
}
@@ -269,10 +324,9 @@ public class AllAppsContainerView extends RelativeLayout implements DragSource,
mNumPredictedAppsPerRow != grid.inv.numColumns) {
mNumAppsPerRow = grid.inv.numColumns;
mNumPredictedAppsPerRow = grid.inv.numColumns;
mAppsRecyclerView.setNumAppsPerRow(grid, mNumAppsPerRow);
mAdapter.setNumAppsPerRow(mNumAppsPerRow);
mApps.setNumAppsPerRow(mNumAppsPerRow, mNumPredictedAppsPerRow);
for (int i = 0; i < mAH.length; i++) {
mAH[i].applyNumsPerRow();
}
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@@ -325,10 +379,10 @@ public class AllAppsContainerView extends RelativeLayout implements DragSource,
@Override
public void setInsets(Rect insets) {
DeviceProfile grid = mLauncher.getDeviceProfile();
mAppsRecyclerView.setPadding(
mAppsRecyclerView.getPaddingLeft(), mAppsRecyclerView.getPaddingTop(),
mAppsRecyclerView.getPaddingRight(), insets.bottom);
for (int i = 0; i < mAH.length; i++) {
mAH[i].padding.bottom = insets.bottom;
mAH[i].applyPadding();
}
if (grid.isVerticalBarLayout()) {
ViewGroup.MarginLayoutParams mlp = (MarginLayoutParams) getLayoutParams();
mlp.leftMargin = insets.left;
@@ -345,20 +399,300 @@ public class AllAppsContainerView extends RelativeLayout implements DragSource,
public void updateIconBadges(Set<PackageUserKey> updatedBadges) {
final PackageUserKey packageUserKey = new PackageUserKey(null, null);
final int n = mAppsRecyclerView.getChildCount();
for (int i = 0; i < n; i++) {
View child = mAppsRecyclerView.getChildAt(i);
if (!(child instanceof BubbleTextView) || !(child.getTag() instanceof ItemInfo)) {
continue;
}
ItemInfo info = (ItemInfo) child.getTag();
if (packageUserKey.updateFromItemInfo(info) && updatedBadges.contains(packageUserKey)) {
((BubbleTextView) child).applyBadgeState(info, true /* animate */);
for (int j = 0; j < mAH.length; j++) {
if (mAH[j].recyclerView != null) {
final int n = mAH[j].recyclerView.getChildCount();
for (int i = 0; i < n; i++) {
View child = mAH[j].recyclerView.getChildAt(i);
if (!(child instanceof BubbleTextView) || !(child.getTag() instanceof ItemInfo)) {
continue;
}
ItemInfo info = (ItemInfo) child.getTag();
if (packageUserKey.updateFromItemInfo(info) && updatedBadges.contains(packageUserKey)) {
((BubbleTextView) child).applyBadgeState(info, true /* animate */);
}
}
}
}
}
public SpringAnimationHandler getSpringAnimationHandler() {
return mSpringAnimationHandler;
return mUsingTabs ? null : mAH[AdapterHolder.MAIN].animationHandler;
}
private void rebindAdapters(boolean showTabs) {
if (showTabs != mUsingTabs) {
replaceRVContainer(showTabs);
}
mUsingTabs = showTabs;
if (mUsingTabs) {
mAH[AdapterHolder.MAIN].setup(mViewPager.getChildAt(0), mPersonalMatcher);
mAH[AdapterHolder.WORK].setup(mViewPager.getChildAt(1), mWorkMatcher);
setupWorkProfileTabs();
setupHeader();
mHeader.setVisibility(View.VISIBLE);
} else {
mHeader.setVisibility(View.GONE);
mAH[AdapterHolder.MAIN].setup(findViewById(R.id.apps_list_view), null);
}
applyTouchDelegate();
}
private boolean hasWorkProfileApp(List<AppInfo> apps) {
if (FeatureFlags.ALL_APPS_TABS_ENABLED) {
for (AppInfo app : apps) {
if (mWorkMatcher.matches(app, null)) {
return true;
}
}
}
return false;
}
private void replaceRVContainer(boolean showTabs) {
for (int i = 0; i < mAH.length; i++) {
if (mAH[i].recyclerView != null) {
mAH[i].recyclerView.setLayoutManager(null);
}
}
View oldView = getRecyclerViewContainer();
int index = indexOfChild(oldView);
removeView(oldView);
int layout = showTabs ? R.layout.all_apps_tabs : R.layout.all_apps_rv_layout;
View newView = LayoutInflater.from(getContext()).inflate(layout, this, false);
addView(newView, index);
mViewPager = showTabs ? (InterceptingViewPager) newView : null;
}
public View getRecyclerViewContainer() {
return mViewPager != null ? mViewPager : findViewById(R.id.apps_list_view);
}
private void setupWorkProfileTabs() {
mViewPager.setAdapter(new TabsPagerAdapter());
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
boolean mVisible = true;
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if (positionOffset == 0 && !mVisible || positionOffset > 0 && mVisible) {
mVisible = positionOffset == 0;
for (int i = 0; i < mAH.length; i++) {
if (mAH[i].recyclerView != null) {
mAH[i].recyclerView.getScrollbar().setAlpha(mVisible ? 1 : 0);
}
}
}
}
@Override
public void onPageSelected(int pos) {
mFloatingHeaderHandler.setMainActive(pos == 0);
applyTouchDelegate();
if (mAH[pos].recyclerView != null) {
mAH[pos].recyclerView.bindFastScrollbar();
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
findViewById(R.id.tab_personal)
.setOnClickListener((View view) -> mViewPager.setCurrentItem(0));
findViewById(R.id.tab_work)
.setOnClickListener((View view) -> mViewPager.setCurrentItem(1));
}
public void setPredictedApps(List<ComponentKeyMapper<AppInfo>> apps) {
if (mUsingTabs) {
mFloatingHeaderHandler.getContentView().setPredictedApps(apps);
}
mAH[AdapterHolder.MAIN].appsList.setPredictedApps(apps);
}
public AppInfo findApp(ComponentKeyMapper<AppInfo> mapper) {
return mapper.getItem(mComponentToAppMap);
}
public AlphabeticalAppsList getApps() {
return mAH[AdapterHolder.MAIN].appsList;
}
public boolean isUsingTabs() {
return mUsingTabs;
}
public FloatingHeaderHandler getFloatingHeaderHandler() {
return mFloatingHeaderHandler;
}
private void setupHeader() {
int contentHeight = mLauncher.getDeviceProfile().allAppsCellHeightPx;
RecyclerView mainRV = mAH[AdapterHolder.MAIN].recyclerView;
RecyclerView workRV = mAH[AdapterHolder.WORK] != null
? mAH[AdapterHolder.WORK].recyclerView : null;
mFloatingHeaderHandler = new FloatingHeaderHandler(mHeader, mainRV, workRV, contentHeight);
mFloatingHeaderHandler.getContentView().setNumAppsPerRow(mNumPredictedAppsPerRow);
mFloatingHeaderHandler.getContentView().setComponentToAppMap(mComponentToAppMap);
for (int i = 0; i < mAH.length; i++) {
mAH[i].paddingTopForTabs = contentHeight;
mAH[i].applyPadding();
}
}
public void setLastSearchQuery(String query) {
for (int i = 0; i < mAH.length; i++) {
mAH[i].adapter.setLastSearchQuery(query);
}
}
public void onSearchResultsChanged() {
for (int i = 0; i < mAH.length; i++) {
mAH[i].recyclerView.onSearchResultsChanged();
}
}
public void setRecyclerViewPaddingTop(int top) {
for (int i = 0; i < mAH.length; i++) {
mAH[i].padding.top = top;
mAH[i].applyPadding();
}
}
public void setRecyclerViewSidePadding(int left, int right) {
for (int i = 0; i < mAH.length; i++) {
mAH[i].padding.left = left;
mAH[i].padding.right = right;
mAH[i].applyPadding();
}
}
public void setRecyclerViewVerticalFadingEdgeEnabled(boolean enabled) {
for (int i = 0; i < mAH.length; i++) {
mAH[i].applyVerticalFadingEdgeEnabled(enabled);
}
}
public void addElevationController(RecyclerView.OnScrollListener scrollListener) {
if (!mUsingTabs) {
mAH[AdapterHolder.MAIN].recyclerView.addOnScrollListener(scrollListener);
}
}
public List<AppInfo> getPredictedApps() {
if (mUsingTabs) {
return mFloatingHeaderHandler.getContentView().getPredictedApps();
} else {
return mAH[AdapterHolder.MAIN].appsList.getPredictedApps();
}
}
public class AdapterHolder {
public static final int MAIN = 0;
public static final int WORK = 1;
final AllAppsGridAdapter adapter;
final LinearLayoutManager layoutManager;
final SpringAnimationHandler animationHandler;
final AlphabeticalAppsList appsList;
final Rect padding = new Rect();
int paddingTopForTabs;
AllAppsRecyclerView recyclerView;
boolean verticalFadingEdge;
AdapterHolder() {
appsList = new AlphabeticalAppsList(mLauncher, mComponentToAppMap);
adapter = new AllAppsGridAdapter(mLauncher, appsList, mLauncher,
AllAppsContainerView.this, true);
appsList.setAdapter(adapter);
animationHandler = adapter.getSpringAnimationHandler();
layoutManager = adapter.getLayoutManager();
}
void setup(@NonNull View rv, @Nullable ItemInfoMatcher matcher) {
appsList.updateItemFilter(matcher);
recyclerView = (AllAppsRecyclerView) rv;
recyclerView.setApps(appsList, mUsingTabs);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
recyclerView.setHasFixedSize(true);
// No animations will occur when changes occur to the items in this RecyclerView.
recyclerView.setItemAnimator(null);
if (FeatureFlags.LAUNCHER3_PHYSICS && animationHandler != null) {
recyclerView.setSpringAnimationHandler(animationHandler);
}
FocusedItemDecorator focusedItemDecorator = new FocusedItemDecorator(recyclerView);
recyclerView.addItemDecoration(focusedItemDecorator);
recyclerView.preMeasureViews(adapter);
adapter.setIconFocusListener(focusedItemDecorator.getFocusListener());
applyVerticalFadingEdgeEnabled(verticalFadingEdge);
applyPadding();
applyNumsPerRow();
}
void applyPadding() {
if (recyclerView != null) {
int paddingTop = mUsingTabs ? paddingTopForTabs : padding.top;
recyclerView.setPadding(padding.left, paddingTop, padding.right, padding.bottom);
}
}
void applyNumsPerRow() {
if (mNumAppsPerRow > 0) {
if (recyclerView != null) {
recyclerView.setNumAppsPerRow(mLauncher.getDeviceProfile(), mNumAppsPerRow);
}
adapter.setNumAppsPerRow(mNumAppsPerRow);
appsList.setNumAppsPerRow(mNumAppsPerRow, mNumPredictedAppsPerRow);
if (mUsingTabs && mFloatingHeaderHandler != null) {
mFloatingHeaderHandler.getContentView()
.setNumAppsPerRow(mNumPredictedAppsPerRow);
}
}
}
public void applyVerticalFadingEdgeEnabled(boolean enabled) {
verticalFadingEdge = enabled;
mAH[AdapterHolder.MAIN].recyclerView.setVerticalFadingEdgeEnabled(!mUsingTabs
&& verticalFadingEdge);
}
}
private class TabsPagerAdapter extends PagerAdapter {
@Override
public int getCount() {
return 2;
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
if (position == 0) {
return mAH[AdapterHolder.MAIN].recyclerView;
} else {
return mAH[AdapterHolder.WORK].recyclerView;
}
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
if (position == 0) {
return getResources().getString(R.string.all_apps_personal_tab);
} else {
return getResources().getString(R.string.all_apps_work_tab);
}
}
}
}