blob: 663e0255feb96fceaeaa6517bdaa5b699e32a9d1 [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,
41 ChooserActivity.ChooserGridAdapter adapter) {
42 super(context, /* currentPage */ 0);
43 mItems = new ChooserProfileDescriptor[] {
44 createProfileDescriptor(adapter)
45 };
46 }
47
48 ChooserMultiProfilePagerAdapter(Context context,
49 ChooserActivity.ChooserGridAdapter personalAdapter,
50 ChooserActivity.ChooserGridAdapter workAdapter,
51 @Profile int defaultProfile) {
52 super(context, /* currentPage */ defaultProfile);
53 mItems = new ChooserProfileDescriptor[] {
54 createProfileDescriptor(personalAdapter),
55 createProfileDescriptor(workAdapter)
56 };
57 }
58
59 private ChooserProfileDescriptor createProfileDescriptor(
60 ChooserActivity.ChooserGridAdapter adapter) {
61 final LayoutInflater inflater = LayoutInflater.from(getContext());
62 final ViewGroup rootView =
63 (ViewGroup) inflater.inflate(R.layout.chooser_list_per_profile, null, false);
64 return new ChooserProfileDescriptor(rootView, adapter);
65 }
66
67 RecyclerView getListViewForIndex(int index) {
68 return getItem(index).recyclerView;
69 }
70
71 @Override
72 ChooserProfileDescriptor getItem(int pageIndex) {
73 return mItems[pageIndex];
74 }
75
76 @Override
77 int getItemCount() {
78 return mItems.length;
79 }
80
81 @Override
arangelovcf268642020-01-15 15:09:51 +000082 @VisibleForTesting
83 public ChooserActivity.ChooserGridAdapter getAdapterForIndex(int pageIndex) {
arangelov38a6fce2019-12-02 18:21:22 +000084 return mItems[pageIndex].chooserGridAdapter;
85 }
86
87 @Override
arangelov5fc9e7d2020-01-07 17:59:14 +000088 @Nullable
89 ChooserListAdapter getListAdapterForUserHandle(UserHandle userHandle) {
90 if (getActiveListAdapter().getUserHandle() == userHandle) {
91 return getActiveListAdapter();
92 } else if (getInactiveListAdapter() != null
93 && getInactiveListAdapter().getUserHandle() == userHandle) {
94 return getInactiveListAdapter();
95 }
96 return null;
97 }
98
99 @Override
arangelov38a6fce2019-12-02 18:21:22 +0000100 void setupListAdapter(int pageIndex) {
101 final RecyclerView recyclerView = getItem(pageIndex).recyclerView;
102 ChooserActivity.ChooserGridAdapter chooserGridAdapter =
103 getItem(pageIndex).chooserGridAdapter;
104 recyclerView.setAdapter(chooserGridAdapter);
105 GridLayoutManager glm = (GridLayoutManager) recyclerView.getLayoutManager();
106 glm.setSpanCount(chooserGridAdapter.getMaxTargetsPerRow());
107 glm.setSpanSizeLookup(
108 new GridLayoutManager.SpanSizeLookup() {
109 @Override
110 public int getSpanSize(int position) {
111 return chooserGridAdapter.shouldCellSpan(position)
112 ? SINGLE_CELL_SPAN_SIZE
113 : glm.getSpanCount();
114 }
115 });
116 }
117
118 @Override
119 @VisibleForTesting
arangelova3912cf2019-12-13 14:34:45 +0000120 public ChooserListAdapter getActiveListAdapter() {
arangelov38a6fce2019-12-02 18:21:22 +0000121 return getAdapterForIndex(getCurrentPage()).getListAdapter();
122 }
123
124 @Override
arangelova3912cf2019-12-13 14:34:45 +0000125 @VisibleForTesting
126 public ChooserListAdapter getInactiveListAdapter() {
127 if (getCount() == 1) {
128 return null;
129 }
130 return getAdapterForIndex(1 - getCurrentPage()).getListAdapter();
131 }
132
133 @Override
arangelov38a6fce2019-12-02 18:21:22 +0000134 ChooserActivity.ChooserGridAdapter getCurrentRootAdapter() {
135 return getAdapterForIndex(getCurrentPage());
136 }
137
138 @Override
arangelovcf268642020-01-15 15:09:51 +0000139 RecyclerView getActiveAdapterView() {
arangelov38a6fce2019-12-02 18:21:22 +0000140 return getListViewForIndex(getCurrentPage());
141 }
142
arangelovcf268642020-01-15 15:09:51 +0000143 @Override
144 @Nullable
145 RecyclerView getInactiveAdapterView() {
146 if (getCount() == 1) {
147 return null;
148 }
149 return getListViewForIndex(1 - getCurrentPage());
150 }
151
arangelov38a6fce2019-12-02 18:21:22 +0000152 class ChooserProfileDescriptor extends ProfileDescriptor {
153 private ChooserActivity.ChooserGridAdapter chooserGridAdapter;
154 private RecyclerView recyclerView;
155 ChooserProfileDescriptor(ViewGroup rootView, ChooserActivity.ChooserGridAdapter adapter) {
156 super(rootView);
157 chooserGridAdapter = adapter;
158 recyclerView = rootView.findViewById(R.id.resolver_list);
159 }
160 }
161}