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.content.SharedPreferences;
19 import android.view.LayoutInflater;
20 import android.view.ViewGroup;
21 
22 import com.android.launcher3.R;
23 
24 import java.util.ArrayList;
25 
26 /**
27  * A UI expansion wrapper providing for providing work profile specific views
28  */
29 public class WorkAdapterProvider extends BaseAdapterProvider {
30 
31     public static final String KEY_WORK_EDU_STEP = "showed_work_profile_edu";
32 
33     private static final int VIEW_TYPE_WORK_EDU_CARD = 1 << 20;
34     private static final int VIEW_TYPE_WORK_DISABLED_CARD = 1 << 21;
35 
36     @WorkProfileManager.WorkProfileState
37     private int mState;
38     private SharedPreferences mPreferences;
39 
WorkAdapterProvider(SharedPreferences prefs)40     WorkAdapterProvider(SharedPreferences prefs) {
41         mPreferences = prefs;
42     }
43 
44     @Override
onBindView(AllAppsGridAdapter.ViewHolder holder, int position)45     public void onBindView(AllAppsGridAdapter.ViewHolder holder, int position) {
46         if (holder.itemView instanceof WorkEduCard) {
47             ((WorkEduCard) holder.itemView).setPosition(position);
48         }
49     }
50 
51     @Override
onCreateViewHolder(LayoutInflater layoutInflater, ViewGroup parent, int viewType)52     public AllAppsGridAdapter.ViewHolder onCreateViewHolder(LayoutInflater layoutInflater,
53             ViewGroup parent, int viewType) {
54         int viewId = viewType == VIEW_TYPE_WORK_DISABLED_CARD ? R.layout.work_apps_paused
55                 : R.layout.work_apps_edu;
56         return new AllAppsGridAdapter.ViewHolder(layoutInflater.inflate(viewId, parent, false));
57     }
58 
59     /**
60      * returns whether or not work apps should be visible in work tab.
61      */
shouldShowWorkApps()62     public boolean shouldShowWorkApps() {
63         return mState != WorkProfileManager.STATE_DISABLED;
64     }
65 
66     /**
67      * Adds work profile specific adapter items to adapterItems and returns number of items added
68      */
addWorkItems(ArrayList<AllAppsGridAdapter.AdapterItem> adapterItems)69     public int addWorkItems(ArrayList<AllAppsGridAdapter.AdapterItem> adapterItems) {
70         if (mState == WorkProfileManager.STATE_DISABLED) {
71             //add disabled card here.
72             AllAppsGridAdapter.AdapterItem disabledCard = new AllAppsGridAdapter.AdapterItem();
73             disabledCard.viewType = VIEW_TYPE_WORK_DISABLED_CARD;
74             adapterItems.add(disabledCard);
75         } else if (mState == WorkProfileManager.STATE_ENABLED && !isEduSeen()) {
76             AllAppsGridAdapter.AdapterItem eduCard = new AllAppsGridAdapter.AdapterItem();
77             eduCard.viewType = VIEW_TYPE_WORK_EDU_CARD;
78             adapterItems.add(eduCard);
79         }
80 
81         return adapterItems.size();
82     }
83 
84     /**
85      * Sets the current state of work profile
86      */
updateCurrentState(@orkProfileManager.WorkProfileState int state)87     public void updateCurrentState(@WorkProfileManager.WorkProfileState int state) {
88         mState = state;
89     }
90 
91     @Override
isViewSupported(int viewType)92     public boolean isViewSupported(int viewType) {
93         return viewType == VIEW_TYPE_WORK_DISABLED_CARD || viewType == VIEW_TYPE_WORK_EDU_CARD;
94     }
95 
96     @Override
getItemsPerRow(int viewType, int appsPerRow)97     public int getItemsPerRow(int viewType, int appsPerRow) {
98         return 1;
99     }
100 
isEduSeen()101     private boolean isEduSeen() {
102         return mPreferences.getInt(KEY_WORK_EDU_STEP, 0) != 0;
103     }
104 }
105