blob: ad31d8b2e49a690e57719736a4a1ed67d1b121a1 [file] [log] [blame]
arangelov38a6fce2019-12-02 18:21:22 +00001/*
2 * Copyright (C) 2019 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
17package com.android.internal.app;
18
arangelovcf268642020-01-15 15:09:51 +000019import android.annotation.Nullable;
arangelov38a6fce2019-12-02 18:21:22 +000020import android.content.Context;
arangelov5fc9e7d2020-01-07 17:59:14 +000021import android.os.UserHandle;
arangelov38a6fce2019-12-02 18:21:22 +000022import android.view.LayoutInflater;
arangelov78fd3602020-02-28 15:21:20 +000023import android.view.View;
arangelov38a6fce2019-12-02 18:21:22 +000024import android.view.ViewGroup;
25import android.widget.ListView;
26
27import com.android.internal.R;
28import com.android.internal.annotations.VisibleForTesting;
29import com.android.internal.widget.PagerAdapter;
30
31/**
32 * A {@link PagerAdapter} which describes the work and personal profile intent resolver screens.
33 */
34@VisibleForTesting
35public class ResolverMultiProfilePagerAdapter extends AbstractMultiProfilePagerAdapter {
36
37 private final ResolverProfileDescriptor[] mItems;
arangelov2c6115a2020-04-08 13:26:13 +010038 private final boolean mShouldShowNoCrossProfileIntentsEmptyState;
arangelov38a6fce2019-12-02 18:21:22 +000039
40 ResolverMultiProfilePagerAdapter(Context context,
arangelov7981b122020-01-16 10:58:27 +000041 ResolverListAdapter adapter,
42 UserHandle personalProfileUserHandle,
43 UserHandle workProfileUserHandle) {
44 super(context, /* currentPage */ 0, personalProfileUserHandle, workProfileUserHandle);
arangelov38a6fce2019-12-02 18:21:22 +000045 mItems = new ResolverProfileDescriptor[] {
46 createProfileDescriptor(adapter)
47 };
arangelov2c6115a2020-04-08 13:26:13 +010048 mShouldShowNoCrossProfileIntentsEmptyState = true;
arangelov38a6fce2019-12-02 18:21:22 +000049 }
50
51 ResolverMultiProfilePagerAdapter(Context context,
52 ResolverListAdapter personalAdapter,
53 ResolverListAdapter workAdapter,
arangelov7981b122020-01-16 10:58:27 +000054 @Profile int defaultProfile,
55 UserHandle personalProfileUserHandle,
arangelov2c6115a2020-04-08 13:26:13 +010056 UserHandle workProfileUserHandle,
57 boolean shouldShowNoCrossProfileIntentsEmptyState) {
arangelov7981b122020-01-16 10:58:27 +000058 super(context, /* currentPage */ defaultProfile, personalProfileUserHandle,
59 workProfileUserHandle);
arangelov38a6fce2019-12-02 18:21:22 +000060 mItems = new ResolverProfileDescriptor[] {
61 createProfileDescriptor(personalAdapter),
62 createProfileDescriptor(workAdapter)
63 };
arangelov2c6115a2020-04-08 13:26:13 +010064 mShouldShowNoCrossProfileIntentsEmptyState = shouldShowNoCrossProfileIntentsEmptyState;
arangelov38a6fce2019-12-02 18:21:22 +000065 }
66
67 private ResolverProfileDescriptor createProfileDescriptor(
68 ResolverListAdapter adapter) {
69 final LayoutInflater inflater = LayoutInflater.from(getContext());
70 final ViewGroup rootView =
71 (ViewGroup) inflater.inflate(R.layout.resolver_list_per_profile, null, false);
72 return new ResolverProfileDescriptor(rootView, adapter);
73 }
74
75 ListView getListViewForIndex(int index) {
76 return getItem(index).listView;
77 }
78
79 @Override
80 ResolverProfileDescriptor getItem(int pageIndex) {
81 return mItems[pageIndex];
82 }
83
84 @Override
85 int getItemCount() {
86 return mItems.length;
87 }
88
89 @Override
90 void setupListAdapter(int pageIndex) {
91 final ListView listView = getItem(pageIndex).listView;
92 listView.setAdapter(getItem(pageIndex).resolverListAdapter);
93 }
94
95 @Override
arangelovcf268642020-01-15 15:09:51 +000096 @VisibleForTesting
97 public ResolverListAdapter getAdapterForIndex(int pageIndex) {
arangelov38a6fce2019-12-02 18:21:22 +000098 return mItems[pageIndex].resolverListAdapter;
99 }
100
101 @Override
arangelovc4dbdbd2020-02-18 20:54:16 +0000102 public ViewGroup instantiateItem(ViewGroup container, int position) {
103 setupListAdapter(position);
104 return super.instantiateItem(container, position);
105 }
106
107 @Override
arangelov5fc9e7d2020-01-07 17:59:14 +0000108 @Nullable
109 ResolverListAdapter getListAdapterForUserHandle(UserHandle userHandle) {
arangelove5b369c2020-03-12 17:36:05 +0000110 if (getActiveListAdapter().getUserHandle().equals(userHandle)) {
arangelov5fc9e7d2020-01-07 17:59:14 +0000111 return getActiveListAdapter();
112 } else if (getInactiveListAdapter() != null
arangelove5b369c2020-03-12 17:36:05 +0000113 && getInactiveListAdapter().getUserHandle().equals(userHandle)) {
arangelov5fc9e7d2020-01-07 17:59:14 +0000114 return getInactiveListAdapter();
115 }
116 return null;
117 }
118
119 @Override
arangelov38a6fce2019-12-02 18:21:22 +0000120 @VisibleForTesting
arangelova3912cf2019-12-13 14:34:45 +0000121 public ResolverListAdapter getActiveListAdapter() {
arangelov38a6fce2019-12-02 18:21:22 +0000122 return getAdapterForIndex(getCurrentPage());
123 }
124
125 @Override
arangelova3912cf2019-12-13 14:34:45 +0000126 @VisibleForTesting
127 public ResolverListAdapter getInactiveListAdapter() {
128 if (getCount() == 1) {
129 return null;
130 }
131 return getAdapterForIndex(1 - getCurrentPage());
132 }
133
134 @Override
arangelov7981b122020-01-16 10:58:27 +0000135 public ResolverListAdapter getPersonalListAdapter() {
136 return getAdapterForIndex(PROFILE_PERSONAL);
137 }
138
139 @Override
140 @Nullable
141 public ResolverListAdapter getWorkListAdapter() {
142 return getAdapterForIndex(PROFILE_WORK);
143 }
144
145 @Override
arangelov38a6fce2019-12-02 18:21:22 +0000146 ResolverListAdapter getCurrentRootAdapter() {
arangelova3912cf2019-12-13 14:34:45 +0000147 return getActiveListAdapter();
arangelov38a6fce2019-12-02 18:21:22 +0000148 }
149
150 @Override
arangelovcf268642020-01-15 15:09:51 +0000151 ListView getActiveAdapterView() {
arangelov38a6fce2019-12-02 18:21:22 +0000152 return getListViewForIndex(getCurrentPage());
153 }
154
arangelovcf268642020-01-15 15:09:51 +0000155 @Override
156 @Nullable
157 ViewGroup getInactiveAdapterView() {
158 if (getCount() == 1) {
159 return null;
160 }
161 return getListViewForIndex(1 - getCurrentPage());
162 }
163
arangelov4bf17472020-02-17 20:21:46 +0000164 @Override
165 String getMetricsCategory() {
166 return ResolverActivity.METRICS_CATEGORY_RESOLVER;
167 }
168
arangelov78fd3602020-02-28 15:21:20 +0000169 @Override
arangelov2c6115a2020-04-08 13:26:13 +0100170 boolean allowShowNoCrossProfileIntentsEmptyState() {
171 return mShouldShowNoCrossProfileIntentsEmptyState;
172 }
173
174 @Override
arangelov78fd3602020-02-28 15:21:20 +0000175 protected void showWorkProfileOffEmptyState(ResolverListAdapter activeListAdapter,
176 View.OnClickListener listener) {
177 showEmptyState(activeListAdapter,
178 R.drawable.ic_work_apps_off,
arangelovafec9d02020-03-17 12:41:01 +0000179 R.string.resolver_turn_on_work_apps,
arangelov78fd3602020-02-28 15:21:20 +0000180 /* subtitleRes */ 0,
181 listener);
182 }
183
184 @Override
185 protected void showNoPersonalToWorkIntentsEmptyState(ResolverListAdapter activeListAdapter) {
186 showEmptyState(activeListAdapter,
187 R.drawable.ic_sharing_disabled,
188 R.string.resolver_cant_access_work_apps,
189 R.string.resolver_cant_access_work_apps_explanation);
190 }
191
192 @Override
193 protected void showNoWorkToPersonalIntentsEmptyState(ResolverListAdapter activeListAdapter) {
194 showEmptyState(activeListAdapter,
195 R.drawable.ic_sharing_disabled,
196 R.string.resolver_cant_access_personal_apps,
197 R.string.resolver_cant_access_personal_apps_explanation);
198 }
199
arangelovafec9d02020-03-17 12:41:01 +0000200 @Override
201 protected void showNoPersonalAppsAvailableEmptyState(ResolverListAdapter listAdapter) {
202 showEmptyState(listAdapter,
203 R.drawable.ic_no_apps,
204 R.string.resolver_no_personal_apps_available_resolve,
205 /* subtitleRes */ 0);
206 }
207
208 @Override
209 protected void showNoWorkAppsAvailableEmptyState(ResolverListAdapter listAdapter) {
210 showEmptyState(listAdapter,
211 R.drawable.ic_no_apps,
212 R.string.resolver_no_work_apps_available_resolve,
213 /* subtitleRes */ 0);
214 }
215
arangelov38a6fce2019-12-02 18:21:22 +0000216 class ResolverProfileDescriptor extends ProfileDescriptor {
217 private ResolverListAdapter resolverListAdapter;
218 final ListView listView;
219 ResolverProfileDescriptor(ViewGroup rootView, ResolverListAdapter adapter) {
220 super(rootView);
221 resolverListAdapter = adapter;
222 listView = rootView.findViewById(R.id.resolver_list);
223 }
224 }
225}