blob: e3501422f915c2ee91d7ad5dfeeeb6f66fc589fd [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;
23import android.view.ViewGroup;
24
25import com.android.internal.R;
26import com.android.internal.annotations.VisibleForTesting;
27import com.android.internal.widget.GridLayoutManager;
28import com.android.internal.widget.PagerAdapter;
29import com.android.internal.widget.RecyclerView;
30
31/**
32 * A {@link PagerAdapter} which describes the work and personal profile share sheet screens.
33 */
34@VisibleForTesting
35public class ChooserMultiProfilePagerAdapter extends AbstractMultiProfilePagerAdapter {
36 private static final int SINGLE_CELL_SPAN_SIZE = 1;
37
38 private final ChooserProfileDescriptor[] mItems;
39
40 ChooserMultiProfilePagerAdapter(Context context,
arangelov7981b122020-01-16 10:58:27 +000041 ChooserActivity.ChooserGridAdapter adapter,
42 UserHandle personalProfileUserHandle,
43 UserHandle workProfileUserHandle) {
44 super(context, /* currentPage */ 0, personalProfileUserHandle, workProfileUserHandle);
arangelov38a6fce2019-12-02 18:21:22 +000045 mItems = new ChooserProfileDescriptor[] {
46 createProfileDescriptor(adapter)
47 };
48 }
49
50 ChooserMultiProfilePagerAdapter(Context context,
51 ChooserActivity.ChooserGridAdapter personalAdapter,
52 ChooserActivity.ChooserGridAdapter workAdapter,
arangelov7981b122020-01-16 10:58:27 +000053 @Profile int defaultProfile,
54 UserHandle personalProfileUserHandle,
55 UserHandle workProfileUserHandle) {
56 super(context, /* currentPage */ defaultProfile, personalProfileUserHandle,
57 workProfileUserHandle);
arangelov38a6fce2019-12-02 18:21:22 +000058 mItems = new ChooserProfileDescriptor[] {
59 createProfileDescriptor(personalAdapter),
60 createProfileDescriptor(workAdapter)
61 };
62 }
63
64 private ChooserProfileDescriptor createProfileDescriptor(
65 ChooserActivity.ChooserGridAdapter adapter) {
66 final LayoutInflater inflater = LayoutInflater.from(getContext());
67 final ViewGroup rootView =
68 (ViewGroup) inflater.inflate(R.layout.chooser_list_per_profile, null, false);
69 return new ChooserProfileDescriptor(rootView, adapter);
70 }
71
72 RecyclerView getListViewForIndex(int index) {
73 return getItem(index).recyclerView;
74 }
75
76 @Override
77 ChooserProfileDescriptor getItem(int pageIndex) {
78 return mItems[pageIndex];
79 }
80
81 @Override
82 int getItemCount() {
83 return mItems.length;
84 }
85
86 @Override
arangelovcf268642020-01-15 15:09:51 +000087 @VisibleForTesting
88 public ChooserActivity.ChooserGridAdapter getAdapterForIndex(int pageIndex) {
arangelov38a6fce2019-12-02 18:21:22 +000089 return mItems[pageIndex].chooserGridAdapter;
90 }
91
92 @Override
arangelov5fc9e7d2020-01-07 17:59:14 +000093 @Nullable
94 ChooserListAdapter getListAdapterForUserHandle(UserHandle userHandle) {
95 if (getActiveListAdapter().getUserHandle() == userHandle) {
96 return getActiveListAdapter();
97 } else if (getInactiveListAdapter() != null
98 && getInactiveListAdapter().getUserHandle() == userHandle) {
99 return getInactiveListAdapter();
100 }
101 return null;
102 }
103
104 @Override
arangelov38a6fce2019-12-02 18:21:22 +0000105 void setupListAdapter(int pageIndex) {
106 final RecyclerView recyclerView = getItem(pageIndex).recyclerView;
107 ChooserActivity.ChooserGridAdapter chooserGridAdapter =
108 getItem(pageIndex).chooserGridAdapter;
109 recyclerView.setAdapter(chooserGridAdapter);
110 GridLayoutManager glm = (GridLayoutManager) recyclerView.getLayoutManager();
111 glm.setSpanCount(chooserGridAdapter.getMaxTargetsPerRow());
112 glm.setSpanSizeLookup(
113 new GridLayoutManager.SpanSizeLookup() {
114 @Override
115 public int getSpanSize(int position) {
116 return chooserGridAdapter.shouldCellSpan(position)
117 ? SINGLE_CELL_SPAN_SIZE
118 : glm.getSpanCount();
119 }
120 });
121 }
122
123 @Override
124 @VisibleForTesting
arangelova3912cf2019-12-13 14:34:45 +0000125 public ChooserListAdapter getActiveListAdapter() {
arangelov38a6fce2019-12-02 18:21:22 +0000126 return getAdapterForIndex(getCurrentPage()).getListAdapter();
127 }
128
129 @Override
arangelova3912cf2019-12-13 14:34:45 +0000130 @VisibleForTesting
131 public ChooserListAdapter getInactiveListAdapter() {
132 if (getCount() == 1) {
133 return null;
134 }
135 return getAdapterForIndex(1 - getCurrentPage()).getListAdapter();
136 }
137
138 @Override
arangelov7981b122020-01-16 10:58:27 +0000139 public ResolverListAdapter getPersonalListAdapter() {
140 return getAdapterForIndex(PROFILE_PERSONAL).getListAdapter();
141 }
142
143 @Override
144 @Nullable
145 public ResolverListAdapter getWorkListAdapter() {
146 return getAdapterForIndex(PROFILE_WORK).getListAdapter();
147 }
148
149 @Override
arangelov38a6fce2019-12-02 18:21:22 +0000150 ChooserActivity.ChooserGridAdapter getCurrentRootAdapter() {
151 return getAdapterForIndex(getCurrentPage());
152 }
153
154 @Override
arangelovcf268642020-01-15 15:09:51 +0000155 RecyclerView getActiveAdapterView() {
arangelov38a6fce2019-12-02 18:21:22 +0000156 return getListViewForIndex(getCurrentPage());
157 }
158
arangelovcf268642020-01-15 15:09:51 +0000159 @Override
160 @Nullable
161 RecyclerView getInactiveAdapterView() {
162 if (getCount() == 1) {
163 return null;
164 }
165 return getListViewForIndex(1 - getCurrentPage());
166 }
167
arangelov38a6fce2019-12-02 18:21:22 +0000168 class ChooserProfileDescriptor extends ProfileDescriptor {
169 private ChooserActivity.ChooserGridAdapter chooserGridAdapter;
170 private RecyclerView recyclerView;
171 ChooserProfileDescriptor(ViewGroup rootView, ChooserActivity.ChooserGridAdapter adapter) {
172 super(rootView);
173 chooserGridAdapter = adapter;
174 recyclerView = rootView.findViewById(R.id.resolver_list);
175 }
176 }
177}