Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit cba5f9a6 authored by Sunny Goyal's avatar Sunny Goyal
Browse files

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
parent 6c057718
Loading
Loading
Loading
Loading
+6 −6
Original line number Diff line number Diff line
@@ -50,7 +50,7 @@ public class AlphabeticalAppsList<T extends Context & ActivityContext> implement

    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 @@ public class AlphabeticalAppsList<T extends Context & ActivityContext> implement
    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 @@ public class AlphabeticalAppsList<T extends Context & ActivityContext> implement
            }
        } 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;
                }
            }
+15 −0
Original line number Diff line number Diff line
@@ -63,6 +63,11 @@ public abstract class BaseAllAppsAdapter<T extends Context & ActivityContext> ex
    // 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 @@ public abstract class BaseAllAppsAdapter<T extends Context & ActivityContext> ex
            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 @@ public abstract class BaseAllAppsAdapter<T extends Context & ActivityContext> ex
                }
                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) {
+3 −7
Original line number Diff line number Diff line
@@ -147,8 +147,7 @@ public abstract class BaseAllAppsContainerView<T extends Context & ActivityConte

        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 @@ public abstract class BaseAllAppsContainerView<T extends Context & ActivityConte
            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);
+0 −138
Original line number Diff line number Diff line
/*
 * 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;
    }
}
+9 −1
Original line number Diff line number Diff line
@@ -24,9 +24,11 @@ import android.view.ViewGroup;
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 @@ public class WorkEduCard extends FrameLayout implements
    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();
    }

Loading