arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.internal.app; |
| 18 | |
arangelov | cf26864 | 2020-01-15 15:09:51 +0000 | [diff] [blame] | 19 | import android.annotation.Nullable; |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 20 | import android.content.Context; |
arangelov | 5fc9e7d | 2020-01-07 17:59:14 +0000 | [diff] [blame] | 21 | import android.os.UserHandle; |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 22 | import android.view.LayoutInflater; |
| 23 | import android.view.ViewGroup; |
| 24 | |
| 25 | import com.android.internal.R; |
| 26 | import com.android.internal.annotations.VisibleForTesting; |
| 27 | import com.android.internal.widget.GridLayoutManager; |
| 28 | import com.android.internal.widget.PagerAdapter; |
| 29 | import com.android.internal.widget.RecyclerView; |
| 30 | |
| 31 | /** |
| 32 | * A {@link PagerAdapter} which describes the work and personal profile share sheet screens. |
| 33 | */ |
| 34 | @VisibleForTesting |
| 35 | public 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, |
arangelov | 7981b12 | 2020-01-16 10:58:27 +0000 | [diff] [blame] | 41 | ChooserActivity.ChooserGridAdapter adapter, |
| 42 | UserHandle personalProfileUserHandle, |
| 43 | UserHandle workProfileUserHandle) { |
| 44 | super(context, /* currentPage */ 0, personalProfileUserHandle, workProfileUserHandle); |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 45 | mItems = new ChooserProfileDescriptor[] { |
| 46 | createProfileDescriptor(adapter) |
| 47 | }; |
| 48 | } |
| 49 | |
| 50 | ChooserMultiProfilePagerAdapter(Context context, |
| 51 | ChooserActivity.ChooserGridAdapter personalAdapter, |
| 52 | ChooserActivity.ChooserGridAdapter workAdapter, |
arangelov | 7981b12 | 2020-01-16 10:58:27 +0000 | [diff] [blame] | 53 | @Profile int defaultProfile, |
| 54 | UserHandle personalProfileUserHandle, |
| 55 | UserHandle workProfileUserHandle) { |
| 56 | super(context, /* currentPage */ defaultProfile, personalProfileUserHandle, |
| 57 | workProfileUserHandle); |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 58 | 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 |
arangelov | cf26864 | 2020-01-15 15:09:51 +0000 | [diff] [blame] | 87 | @VisibleForTesting |
| 88 | public ChooserActivity.ChooserGridAdapter getAdapterForIndex(int pageIndex) { |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 89 | return mItems[pageIndex].chooserGridAdapter; |
| 90 | } |
| 91 | |
| 92 | @Override |
arangelov | 5fc9e7d | 2020-01-07 17:59:14 +0000 | [diff] [blame] | 93 | @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 |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 105 | 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 |
arangelov | a3912cf | 2019-12-13 14:34:45 +0000 | [diff] [blame] | 125 | public ChooserListAdapter getActiveListAdapter() { |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 126 | return getAdapterForIndex(getCurrentPage()).getListAdapter(); |
| 127 | } |
| 128 | |
| 129 | @Override |
arangelov | a3912cf | 2019-12-13 14:34:45 +0000 | [diff] [blame] | 130 | @VisibleForTesting |
| 131 | public ChooserListAdapter getInactiveListAdapter() { |
| 132 | if (getCount() == 1) { |
| 133 | return null; |
| 134 | } |
| 135 | return getAdapterForIndex(1 - getCurrentPage()).getListAdapter(); |
| 136 | } |
| 137 | |
| 138 | @Override |
arangelov | 7981b12 | 2020-01-16 10:58:27 +0000 | [diff] [blame] | 139 | 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 |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 150 | ChooserActivity.ChooserGridAdapter getCurrentRootAdapter() { |
| 151 | return getAdapterForIndex(getCurrentPage()); |
| 152 | } |
| 153 | |
| 154 | @Override |
arangelov | cf26864 | 2020-01-15 15:09:51 +0000 | [diff] [blame] | 155 | RecyclerView getActiveAdapterView() { |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 156 | return getListViewForIndex(getCurrentPage()); |
| 157 | } |
| 158 | |
arangelov | cf26864 | 2020-01-15 15:09:51 +0000 | [diff] [blame] | 159 | @Override |
| 160 | @Nullable |
| 161 | RecyclerView getInactiveAdapterView() { |
| 162 | if (getCount() == 1) { |
| 163 | return null; |
| 164 | } |
| 165 | return getListViewForIndex(1 - getCurrentPage()); |
| 166 | } |
| 167 | |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 168 | 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 | } |