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, |
| 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 |
arangelov | cf26864 | 2020-01-15 15:09:51 +0000 | [diff] [blame] | 82 | @VisibleForTesting |
| 83 | public ChooserActivity.ChooserGridAdapter getAdapterForIndex(int pageIndex) { |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 84 | return mItems[pageIndex].chooserGridAdapter; |
| 85 | } |
| 86 | |
| 87 | @Override |
arangelov | 5fc9e7d | 2020-01-07 17:59:14 +0000 | [diff] [blame] | 88 | @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 |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 100 | 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 |
arangelov | a3912cf | 2019-12-13 14:34:45 +0000 | [diff] [blame] | 120 | public ChooserListAdapter getActiveListAdapter() { |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 121 | return getAdapterForIndex(getCurrentPage()).getListAdapter(); |
| 122 | } |
| 123 | |
| 124 | @Override |
arangelov | a3912cf | 2019-12-13 14:34:45 +0000 | [diff] [blame] | 125 | @VisibleForTesting |
| 126 | public ChooserListAdapter getInactiveListAdapter() { |
| 127 | if (getCount() == 1) { |
| 128 | return null; |
| 129 | } |
| 130 | return getAdapterForIndex(1 - getCurrentPage()).getListAdapter(); |
| 131 | } |
| 132 | |
| 133 | @Override |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 134 | ChooserActivity.ChooserGridAdapter getCurrentRootAdapter() { |
| 135 | return getAdapterForIndex(getCurrentPage()); |
| 136 | } |
| 137 | |
| 138 | @Override |
arangelov | cf26864 | 2020-01-15 15:09:51 +0000 | [diff] [blame] | 139 | RecyclerView getActiveAdapterView() { |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 140 | return getListViewForIndex(getCurrentPage()); |
| 141 | } |
| 142 | |
arangelov | cf26864 | 2020-01-15 15:09:51 +0000 | [diff] [blame] | 143 | @Override |
| 144 | @Nullable |
| 145 | RecyclerView getInactiveAdapterView() { |
| 146 | if (getCount() == 1) { |
| 147 | return null; |
| 148 | } |
| 149 | return getListViewForIndex(1 - getCurrentPage()); |
| 150 | } |
| 151 | |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 152 | 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 | } |