1 /* 2 * Copyright (C) 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.launcher3.allapps; 17 18 import android.view.LayoutInflater; 19 import android.view.ViewGroup; 20 21 import com.android.launcher3.BaseDraggingActivity; 22 import com.android.launcher3.R; 23 import com.android.launcher3.Utilities; 24 25 import java.util.ArrayList; 26 27 /** 28 * A UI expansion wrapper providing for providing work profile specific views 29 */ 30 public class WorkAdapterProvider extends BaseAdapterProvider { 31 32 public static final String KEY_WORK_EDU_STEP = "showed_work_profile_edu"; 33 34 private static final int VIEW_TYPE_WORK_EDU_CARD = 1 << 20; 35 private static final int VIEW_TYPE_WORK_DISABLED_CARD = 1 << 21; 36 private final Runnable mRefreshCB; 37 private final BaseDraggingActivity mLauncher; 38 private boolean mEnabled; 39 WorkAdapterProvider(BaseDraggingActivity launcher, Runnable refreshCallback)40 WorkAdapterProvider(BaseDraggingActivity launcher, Runnable refreshCallback) { 41 mLauncher = launcher; 42 mRefreshCB = refreshCallback; 43 } 44 45 @Override onBindView(AllAppsGridAdapter.ViewHolder holder, int position)46 public void onBindView(AllAppsGridAdapter.ViewHolder holder, int position) { 47 if (holder.itemView instanceof WorkEduCard) { 48 ((WorkEduCard) holder.itemView).setPosition(position); 49 } 50 } 51 52 @Override onCreateViewHolder(LayoutInflater layoutInflater, ViewGroup parent, int viewType)53 public AllAppsGridAdapter.ViewHolder onCreateViewHolder(LayoutInflater layoutInflater, 54 ViewGroup parent, int viewType) { 55 int viewId = viewType == VIEW_TYPE_WORK_DISABLED_CARD ? R.layout.work_apps_paused 56 : R.layout.work_apps_edu; 57 return new AllAppsGridAdapter.ViewHolder(layoutInflater.inflate(viewId, parent, false)); 58 } 59 60 /** 61 * returns whether or not work apps should be visible in work tab. 62 */ shouldShowWorkApps()63 public boolean shouldShowWorkApps() { 64 return mEnabled; 65 } 66 67 /** 68 * Adds work profile specific adapter items to adapterItems and returns number of items added 69 */ addWorkItems(ArrayList<AllAppsGridAdapter.AdapterItem> adapterItems)70 public int addWorkItems(ArrayList<AllAppsGridAdapter.AdapterItem> adapterItems) { 71 if (!mEnabled) { 72 //add disabled card here. 73 AllAppsGridAdapter.AdapterItem disabledCard = new AllAppsGridAdapter.AdapterItem(); 74 disabledCard.viewType = VIEW_TYPE_WORK_DISABLED_CARD; 75 adapterItems.add(disabledCard); 76 } else if (!isEduSeen()) { 77 AllAppsGridAdapter.AdapterItem eduCard = new AllAppsGridAdapter.AdapterItem(); 78 eduCard.viewType = VIEW_TYPE_WORK_EDU_CARD; 79 adapterItems.add(eduCard); 80 } 81 82 return adapterItems.size(); 83 } 84 85 /** 86 * Sets the current state of work profile 87 */ updateCurrentState(boolean isEnabled)88 public void updateCurrentState(boolean isEnabled) { 89 mEnabled = isEnabled; 90 mRefreshCB.run(); 91 } 92 93 @Override isViewSupported(int viewType)94 public boolean isViewSupported(int viewType) { 95 return viewType == VIEW_TYPE_WORK_DISABLED_CARD || viewType == VIEW_TYPE_WORK_EDU_CARD; 96 } 97 98 @Override getItemsPerRow(int viewType, int appsPerRow)99 public int getItemsPerRow(int viewType, int appsPerRow) { 100 return 1; 101 } 102 isEduSeen()103 private boolean isEduSeen() { 104 return Utilities.getPrefs(mLauncher).getInt(KEY_WORK_EDU_STEP, 0) != 0; 105 } 106 } 107