Removing unnecessary work adapter wrapper

Simplifying view type constants to make it easier to add new view types

Bug: 234008165
Test: Verified on device
Change-Id: I96589ceba362ea7e75afdfc3fe8da3be214aa682
diff --git a/src/com/android/launcher3/allapps/AlphabeticalAppsList.java b/src/com/android/launcher3/allapps/AlphabeticalAppsList.java
index 45a567d..8bbe216 100644
--- a/src/com/android/launcher3/allapps/AlphabeticalAppsList.java
+++ b/src/com/android/launcher3/allapps/AlphabeticalAppsList.java
@@ -50,7 +50,7 @@
 
     public static final String TAG = "AlphabeticalAppsList";
 
-    private final WorkAdapterProvider mWorkAdapterProvider;
+    private final WorkProfileManager mWorkProviderManager;
 
     /**
      * Info about a fast scroller section, depending if sections are merged, the fast scroller
@@ -92,11 +92,11 @@
     private Predicate<ItemInfo> mItemFilter;
 
     public AlphabeticalAppsList(Context context, @Nullable AllAppsStore appsStore,
-            WorkAdapterProvider adapterProvider) {
+            WorkProfileManager workProfileManager) {
         mAllAppsStore = appsStore;
         mActivityContext = ActivityContext.lookupContext(context);
         mAppNameComparator = new AppInfoComparator(context);
-        mWorkAdapterProvider = adapterProvider;
+        mWorkProviderManager = workProfileManager;
         mNumAppsPerRowAllApps = mActivityContext.getDeviceProfile().inv.numAllAppsColumns;
         if (mAllAppsStore != null) {
             mAllAppsStore.addUpdateListener(this);
@@ -260,9 +260,9 @@
             }
         } else {
             int position = 0;
-            if (mWorkAdapterProvider != null) {
-                position += mWorkAdapterProvider.addWorkItems(mAdapterItems);
-                if (!mWorkAdapterProvider.shouldShowWorkApps()) {
+            if (mWorkProviderManager != null) {
+                position += mWorkProviderManager.addWorkItems(mAdapterItems);
+                if (!mWorkProviderManager.shouldShowWorkApps()) {
                     return;
                 }
             }
diff --git a/src/com/android/launcher3/allapps/BaseAllAppsAdapter.java b/src/com/android/launcher3/allapps/BaseAllAppsAdapter.java
index fcba246..459fa88 100644
--- a/src/com/android/launcher3/allapps/BaseAllAppsAdapter.java
+++ b/src/com/android/launcher3/allapps/BaseAllAppsAdapter.java
@@ -63,6 +63,11 @@
     // A divider that separates the apps list and the search market button
     public static final int VIEW_TYPE_ALL_APPS_DIVIDER = 1 << 4;
 
+    public static final int VIEW_TYPE_WORK_EDU_CARD = 1 << 5;
+    public static final int VIEW_TYPE_WORK_DISABLED_CARD = 1 << 6;
+
+    public static final int NEXT_ID = 7;
+
     // Common view type masks
     public static final int VIEW_TYPE_MASK_DIVIDER = VIEW_TYPE_ALL_APPS_DIVIDER;
     public static final int VIEW_TYPE_MASK_ICON = VIEW_TYPE_ICON;
@@ -225,6 +230,12 @@
             case VIEW_TYPE_ALL_APPS_DIVIDER:
                 return new ViewHolder(mLayoutInflater.inflate(
                         R.layout.all_apps_divider, parent, false));
+            case VIEW_TYPE_WORK_EDU_CARD:
+                return new ViewHolder(mLayoutInflater.inflate(
+                        R.layout.work_apps_edu, parent, false));
+            case VIEW_TYPE_WORK_DISABLED_CARD:
+                return new ViewHolder(mLayoutInflater.inflate(
+                        R.layout.work_apps_paused, parent, false));
             default:
                 BaseAdapterProvider adapterProvider = getAdapterProvider(viewType);
                 if (adapterProvider != null) {
@@ -258,8 +269,12 @@
                 }
                 break;
             case VIEW_TYPE_ALL_APPS_DIVIDER:
+            case VIEW_TYPE_WORK_DISABLED_CARD:
                 // nothing to do
                 break;
+            case VIEW_TYPE_WORK_EDU_CARD:
+                ((WorkEduCard) holder.itemView).setPosition(position);
+                break;
             default:
                 BaseAdapterProvider adapterProvider = getAdapterProvider(holder.getItemViewType());
                 if (adapterProvider != null) {
diff --git a/src/com/android/launcher3/allapps/BaseAllAppsContainerView.java b/src/com/android/launcher3/allapps/BaseAllAppsContainerView.java
index 72a9b14..6faedc0 100644
--- a/src/com/android/launcher3/allapps/BaseAllAppsContainerView.java
+++ b/src/com/android/launcher3/allapps/BaseAllAppsContainerView.java
@@ -147,8 +147,7 @@
 
         mWorkManager = new WorkProfileManager(
                 mActivityContext.getSystemService(UserManager.class),
-                this,
-                Utilities.getPrefs(mActivityContext), mActivityContext.getDeviceProfile());
+                this, Utilities.getPrefs(mActivityContext));
         mAH = Arrays.asList(null, null, null);
         mAH.set(AdapterHolder.MAIN, new AdapterHolder(AdapterHolder.MAIN));
         mAH.set(AdapterHolder.WORK, new AdapterHolder(AdapterHolder.WORK));
@@ -800,12 +799,9 @@
             mType = type;
             mAppsList = new AlphabeticalAppsList<>(mActivityContext,
                     isSearch() ? null : mAllAppsStore,
-                    isWork() ? mWorkManager.getAdapterProvider() : null);
-
+                    isWork() ? mWorkManager : null);
             BaseAdapterProvider[] adapterProviders =
-                    isWork() ? new BaseAdapterProvider[]{mMainAdapterProvider,
-                            mWorkManager.getAdapterProvider()}
-                            : new BaseAdapterProvider[]{mMainAdapterProvider};
+                    new BaseAdapterProvider[]{mMainAdapterProvider};
 
             mAdapter = createAdapter(mAppsList, adapterProviders);
             mAppsList.setAdapter(mAdapter);
diff --git a/src/com/android/launcher3/allapps/WorkAdapterProvider.java b/src/com/android/launcher3/allapps/WorkAdapterProvider.java
deleted file mode 100644
index 76d08c8..0000000
--- a/src/com/android/launcher3/allapps/WorkAdapterProvider.java
+++ /dev/null
@@ -1,138 +0,0 @@
-/*
- * Copyright (C) 2021 The Android Open Source Project
- *
- * 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;
-
-import android.content.SharedPreferences;
-import android.view.LayoutInflater;
-import android.view.View;
-import android.view.ViewGroup;
-import android.widget.TextView;
-
-import com.android.launcher3.R;
-import com.android.launcher3.allapps.BaseAllAppsAdapter.AdapterItem;
-import com.android.launcher3.model.StringCache;
-import com.android.launcher3.views.ActivityContext;
-
-import java.util.ArrayList;
-
-/**
- * A UI expansion wrapper providing for providing work profile specific views
- */
-public class WorkAdapterProvider extends BaseAdapterProvider {
-
-    public static final String KEY_WORK_EDU_STEP = "showed_work_profile_edu";
-
-    private static final int VIEW_TYPE_WORK_EDU_CARD = 1 << 20;
-    private static final int VIEW_TYPE_WORK_DISABLED_CARD = 1 << 21;
-
-    @WorkProfileManager.WorkProfileState
-    private int mState;
-    private ActivityContext mActivityContext;
-    private SharedPreferences mPreferences;
-
-    WorkAdapterProvider(ActivityContext activityContext, SharedPreferences prefs) {
-        mActivityContext = activityContext;
-        mPreferences = prefs;
-    }
-
-    @Override
-    public void onBindView(AllAppsGridAdapter.ViewHolder holder, int position) {
-        if (holder.itemView instanceof WorkEduCard) {
-            ((WorkEduCard) holder.itemView).setPosition(position);
-        }
-    }
-
-    @Override
-    public AllAppsGridAdapter.ViewHolder onCreateViewHolder(LayoutInflater layoutInflater,
-            ViewGroup parent, int viewType) {
-        int viewId = viewType == VIEW_TYPE_WORK_DISABLED_CARD ? R.layout.work_apps_paused
-                : R.layout.work_apps_edu;
-        View view = layoutInflater.inflate(viewId, parent, false);
-        setDeviceManagementResources(view, viewType);
-        return new AllAppsGridAdapter.ViewHolder(view);
-    }
-
-    private void setDeviceManagementResources(View view, int viewType) {
-        StringCache cache = mActivityContext.getStringCache();
-        if (cache == null) {
-            return;
-        }
-        if (viewType == VIEW_TYPE_WORK_DISABLED_CARD) {
-            setWorkProfilePausedResources(view, cache);
-        } else {
-            setWorkProfileEduResources(view, cache);
-        }
-    }
-
-    private void setWorkProfilePausedResources(View view, StringCache cache) {
-        TextView title = view.findViewById(R.id.work_apps_paused_title);
-        title.setText(cache.workProfilePausedTitle);
-
-        TextView body = view.findViewById(R.id.work_apps_paused_content);
-        body.setText(cache.workProfilePausedDescription);
-
-        TextView button = view.findViewById(R.id.enable_work_apps);
-        button.setText(cache.workProfileEnableButton);
-    }
-
-    private void setWorkProfileEduResources(View view, StringCache cache) {
-        TextView title = view.findViewById(R.id.work_apps_paused_title);
-        title.setText(cache.workProfileEdu);
-
-    }
-
-    /**
-     * returns whether or not work apps should be visible in work tab.
-     */
-    public boolean shouldShowWorkApps() {
-        return mState != WorkProfileManager.STATE_DISABLED;
-    }
-
-    /**
-     * Adds work profile specific adapter items to adapterItems and returns number of items added
-     */
-    public int addWorkItems(ArrayList<AllAppsGridAdapter.AdapterItem> adapterItems) {
-        if (mState == WorkProfileManager.STATE_DISABLED) {
-            //add disabled card here.
-            adapterItems.add(new AdapterItem(VIEW_TYPE_WORK_DISABLED_CARD));
-        } else if (mState == WorkProfileManager.STATE_ENABLED && !isEduSeen()) {
-            adapterItems.add(new AdapterItem(VIEW_TYPE_WORK_EDU_CARD));
-        }
-
-        return adapterItems.size();
-    }
-
-    /**
-     * Sets the current state of work profile
-     */
-    public void updateCurrentState(@WorkProfileManager.WorkProfileState int state) {
-        mState = state;
-    }
-
-    @Override
-    public boolean isViewSupported(int viewType) {
-        return viewType == VIEW_TYPE_WORK_DISABLED_CARD || viewType == VIEW_TYPE_WORK_EDU_CARD;
-    }
-
-    @Override
-    public int getItemsPerRow(int viewType, int appsPerRow) {
-        return 1;
-    }
-
-    private boolean isEduSeen() {
-        return mPreferences.getInt(KEY_WORK_EDU_STEP, 0) != 0;
-    }
-}
diff --git a/src/com/android/launcher3/allapps/WorkEduCard.java b/src/com/android/launcher3/allapps/WorkEduCard.java
index 539cff1..968a556 100644
--- a/src/com/android/launcher3/allapps/WorkEduCard.java
+++ b/src/com/android/launcher3/allapps/WorkEduCard.java
@@ -24,9 +24,11 @@
 import android.view.animation.Animation;
 import android.view.animation.AnimationUtils;
 import android.widget.FrameLayout;
+import android.widget.TextView;
 
 import com.android.launcher3.R;
 import com.android.launcher3.Utilities;
+import com.android.launcher3.model.StringCache;
 import com.android.launcher3.views.ActivityContext;
 
 /**
@@ -72,12 +74,18 @@
     protected void onFinishInflate() {
         super.onFinishInflate();
         findViewById(R.id.action_btn).setOnClickListener(this);
+
+        StringCache cache = mActivityContext.getStringCache();
+        if (cache != null) {
+            TextView title = findViewById(R.id.work_apps_paused_title);
+            title.setText(cache.workProfileEdu);
+        }
     }
 
     @Override
     public void onClick(View view) {
         startAnimation(mDismissAnim);
-        Utilities.getPrefs(getContext()).edit().putInt(WorkAdapterProvider.KEY_WORK_EDU_STEP,
+        Utilities.getPrefs(getContext()).edit().putInt(WorkProfileManager.KEY_WORK_EDU_STEP,
                 1).apply();
     }
 
diff --git a/src/com/android/launcher3/allapps/WorkPausedCard.java b/src/com/android/launcher3/allapps/WorkPausedCard.java
index 729622f..26a7803 100644
--- a/src/com/android/launcher3/allapps/WorkPausedCard.java
+++ b/src/com/android/launcher3/allapps/WorkPausedCard.java
@@ -23,9 +23,11 @@
 import android.view.View;
 import android.widget.Button;
 import android.widget.LinearLayout;
+import android.widget.TextView;
 
 import com.android.launcher3.R;
 import com.android.launcher3.Utilities;
+import com.android.launcher3.model.StringCache;
 import com.android.launcher3.views.ActivityContext;
 
 /**
@@ -49,12 +51,27 @@
         mActivityContext = ActivityContext.lookupContext(getContext());
     }
 
-
     @Override
     protected void onFinishInflate() {
         super.onFinishInflate();
         mBtn = findViewById(R.id.enable_work_apps);
         mBtn.setOnClickListener(this);
+
+        StringCache cache = mActivityContext.getStringCache();
+        if (cache != null) {
+            setWorkProfilePausedResources(cache);
+        }
+    }
+
+    private void setWorkProfilePausedResources(StringCache cache) {
+        TextView title = findViewById(R.id.work_apps_paused_title);
+        title.setText(cache.workProfilePausedTitle);
+
+        TextView body = findViewById(R.id.work_apps_paused_content);
+        body.setText(cache.workProfilePausedDescription);
+
+        TextView button = findViewById(R.id.enable_work_apps);
+        button.setText(cache.workProfileEnableButton);
     }
 
     @Override
diff --git a/src/com/android/launcher3/allapps/WorkProfileManager.java b/src/com/android/launcher3/allapps/WorkProfileManager.java
index 2f5b7a2..cfac985 100644
--- a/src/com/android/launcher3/allapps/WorkProfileManager.java
+++ b/src/com/android/launcher3/allapps/WorkProfileManager.java
@@ -15,6 +15,8 @@
  */
 package com.android.launcher3.allapps;
 
+import static com.android.launcher3.allapps.BaseAllAppsAdapter.VIEW_TYPE_WORK_DISABLED_CARD;
+import static com.android.launcher3.allapps.BaseAllAppsAdapter.VIEW_TYPE_WORK_EDU_CARD;
 import static com.android.launcher3.model.BgDataModel.Callbacks.FLAG_HAS_SHORTCUT_PERMISSION;
 import static com.android.launcher3.model.BgDataModel.Callbacks.FLAG_QUIET_MODE_CHANGE_PERMISSION;
 import static com.android.launcher3.model.BgDataModel.Callbacks.FLAG_QUIET_MODE_ENABLED;
@@ -31,13 +33,14 @@
 import androidx.annotation.Nullable;
 import androidx.annotation.RequiresApi;
 
-import com.android.launcher3.DeviceProfile;
 import com.android.launcher3.R;
+import com.android.launcher3.allapps.BaseAllAppsAdapter.AdapterItem;
 import com.android.launcher3.model.data.ItemInfo;
 import com.android.launcher3.workprofile.PersonalWorkSlidingTabStrip;
 
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
+import java.util.ArrayList;
 import java.util.function.Predicate;
 
 /**
@@ -48,13 +51,12 @@
 public class WorkProfileManager implements PersonalWorkSlidingTabStrip.OnActivePageChangedListener {
     private static final String TAG = "WorkProfileManager";
 
+    public static final String KEY_WORK_EDU_STEP = "showed_work_profile_edu";
 
     public static final int STATE_ENABLED = 1;
     public static final int STATE_DISABLED = 2;
     public static final int STATE_TRANSITION = 3;
 
-    private final UserManager mUserManager;
-
     /**
      * Work profile manager states
      */
@@ -64,26 +66,23 @@
             STATE_TRANSITION
     })
     @Retention(RetentionPolicy.SOURCE)
-    public @interface WorkProfileState {
-    }
+    public @interface WorkProfileState { }
 
+    private final UserManager mUserManager;
     private final BaseAllAppsContainerView<?> mAllApps;
-    private final WorkAdapterProvider mAdapterProvider;
     private final Predicate<ItemInfo> mMatcher;
 
     private WorkModeSwitch mWorkModeSwitch;
-    private final DeviceProfile mDeviceProfile;
 
     @WorkProfileState
     private int mCurrentState;
+    private SharedPreferences mPreferences;
 
-
-    public WorkProfileManager(UserManager userManager, BaseAllAppsContainerView<?> allApps,
-            SharedPreferences preferences, DeviceProfile deviceProfile) {
+    public WorkProfileManager(
+            UserManager userManager, BaseAllAppsContainerView<?> allApps, SharedPreferences prefs) {
         mUserManager = userManager;
         mAllApps = allApps;
-        mDeviceProfile = deviceProfile;
-        mAdapterProvider = new WorkAdapterProvider(allApps.mActivityContext, preferences);
+        mPreferences = prefs;
         mMatcher = mAllApps.mPersonalMatcher.negate();
     }
 
@@ -120,7 +119,6 @@
 
     private void updateCurrentState(@WorkProfileState int currentState) {
         mCurrentState = currentState;
-        mAdapterProvider.updateCurrentState(currentState);
         if (getAH() != null) {
             getAH().mAppsList.updateAdapterItems();
         }
@@ -161,10 +159,6 @@
         mWorkModeSwitch = null;
     }
 
-    public WorkAdapterProvider getAdapterProvider() {
-        return mAdapterProvider;
-    }
-
     public Predicate<ItemInfo> getMatcher() {
         return mMatcher;
     }
@@ -181,4 +175,28 @@
     public int getCurrentState() {
         return mCurrentState;
     }
+
+    /**
+     * returns whether or not work apps should be visible in work tab.
+     */
+    public boolean shouldShowWorkApps() {
+        return mCurrentState != WorkProfileManager.STATE_DISABLED;
+    }
+
+    /**
+     * Adds work profile specific adapter items to adapterItems and returns number of items added
+     */
+    public int addWorkItems(ArrayList<AdapterItem> adapterItems) {
+        if (mCurrentState == WorkProfileManager.STATE_DISABLED) {
+            //add disabled card here.
+            adapterItems.add(new AdapterItem(VIEW_TYPE_WORK_DISABLED_CARD));
+        } else if (mCurrentState == WorkProfileManager.STATE_ENABLED && !isEduSeen()) {
+            adapterItems.add(new AdapterItem(VIEW_TYPE_WORK_EDU_CARD));
+        }
+        return adapterItems.size();
+    }
+
+    private boolean isEduSeen() {
+        return mPreferences.getInt(KEY_WORK_EDU_STEP, 0) != 0;
+    }
 }
diff --git a/tests/src/com/android/launcher3/ui/WorkProfileTest.java b/tests/src/com/android/launcher3/ui/WorkProfileTest.java
index 35b4ca6..a7a17b1 100644
--- a/tests/src/com/android/launcher3/ui/WorkProfileTest.java
+++ b/tests/src/com/android/launcher3/ui/WorkProfileTest.java
@@ -30,7 +30,6 @@
 import com.android.launcher3.R;
 import com.android.launcher3.allapps.ActivityAllAppsContainerView;
 import com.android.launcher3.allapps.AllAppsPagedView;
-import com.android.launcher3.allapps.WorkAdapterProvider;
 import com.android.launcher3.allapps.WorkEduCard;
 import com.android.launcher3.allapps.WorkPausedCard;
 import com.android.launcher3.allapps.WorkProfileManager;
@@ -155,7 +154,7 @@
     public void testEdu() {
         waitForWorkTabSetup();
         executeOnLauncher(l -> {
-            l.getSharedPrefs().edit().putInt(WorkAdapterProvider.KEY_WORK_EDU_STEP, 0).commit();
+            l.getSharedPrefs().edit().putInt(WorkProfileManager.KEY_WORK_EDU_STEP, 0).commit();
             ((AllAppsPagedView) l.getAppsView().getContentView()).setCurrentPage(WORK_PAGE);
             l.getAppsView().getWorkManager().reset();
         });