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 | import android.widget.ListView; |
| 25 | |
| 26 | import com.android.internal.R; |
| 27 | import com.android.internal.annotations.VisibleForTesting; |
| 28 | import com.android.internal.widget.PagerAdapter; |
| 29 | |
| 30 | /** |
| 31 | * A {@link PagerAdapter} which describes the work and personal profile intent resolver screens. |
| 32 | */ |
| 33 | @VisibleForTesting |
| 34 | public class ResolverMultiProfilePagerAdapter extends AbstractMultiProfilePagerAdapter { |
| 35 | |
| 36 | private final ResolverProfileDescriptor[] mItems; |
| 37 | |
| 38 | ResolverMultiProfilePagerAdapter(Context context, |
| 39 | ResolverListAdapter adapter) { |
| 40 | super(context, /* currentPage */ 0); |
| 41 | mItems = new ResolverProfileDescriptor[] { |
| 42 | createProfileDescriptor(adapter) |
| 43 | }; |
| 44 | } |
| 45 | |
| 46 | ResolverMultiProfilePagerAdapter(Context context, |
| 47 | ResolverListAdapter personalAdapter, |
| 48 | ResolverListAdapter workAdapter, |
| 49 | @Profile int defaultProfile) { |
| 50 | super(context, /* currentPage */ defaultProfile); |
| 51 | mItems = new ResolverProfileDescriptor[] { |
| 52 | createProfileDescriptor(personalAdapter), |
| 53 | createProfileDescriptor(workAdapter) |
| 54 | }; |
| 55 | } |
| 56 | |
| 57 | private ResolverProfileDescriptor createProfileDescriptor( |
| 58 | ResolverListAdapter adapter) { |
| 59 | final LayoutInflater inflater = LayoutInflater.from(getContext()); |
| 60 | final ViewGroup rootView = |
| 61 | (ViewGroup) inflater.inflate(R.layout.resolver_list_per_profile, null, false); |
| 62 | return new ResolverProfileDescriptor(rootView, adapter); |
| 63 | } |
| 64 | |
| 65 | ListView getListViewForIndex(int index) { |
| 66 | return getItem(index).listView; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | ResolverProfileDescriptor getItem(int pageIndex) { |
| 71 | return mItems[pageIndex]; |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | int getItemCount() { |
| 76 | return mItems.length; |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | void setupListAdapter(int pageIndex) { |
| 81 | final ListView listView = getItem(pageIndex).listView; |
| 82 | listView.setAdapter(getItem(pageIndex).resolverListAdapter); |
| 83 | } |
| 84 | |
| 85 | @Override |
arangelov | cf26864 | 2020-01-15 15:09:51 +0000 | [diff] [blame] | 86 | @VisibleForTesting |
| 87 | public ResolverListAdapter getAdapterForIndex(int pageIndex) { |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 88 | return mItems[pageIndex].resolverListAdapter; |
| 89 | } |
| 90 | |
| 91 | @Override |
arangelov | 5fc9e7d | 2020-01-07 17:59:14 +0000 | [diff] [blame] | 92 | @Nullable |
| 93 | ResolverListAdapter getListAdapterForUserHandle(UserHandle userHandle) { |
| 94 | if (getActiveListAdapter().getUserHandle() == userHandle) { |
| 95 | return getActiveListAdapter(); |
| 96 | } else if (getInactiveListAdapter() != null |
| 97 | && getInactiveListAdapter().getUserHandle() == userHandle) { |
| 98 | return getInactiveListAdapter(); |
| 99 | } |
| 100 | return null; |
| 101 | } |
| 102 | |
| 103 | @Override |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 104 | @VisibleForTesting |
arangelov | a3912cf | 2019-12-13 14:34:45 +0000 | [diff] [blame] | 105 | public ResolverListAdapter getActiveListAdapter() { |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 106 | return getAdapterForIndex(getCurrentPage()); |
| 107 | } |
| 108 | |
| 109 | @Override |
arangelov | a3912cf | 2019-12-13 14:34:45 +0000 | [diff] [blame] | 110 | @VisibleForTesting |
| 111 | public ResolverListAdapter getInactiveListAdapter() { |
| 112 | if (getCount() == 1) { |
| 113 | return null; |
| 114 | } |
| 115 | return getAdapterForIndex(1 - getCurrentPage()); |
| 116 | } |
| 117 | |
| 118 | @Override |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 119 | ResolverListAdapter getCurrentRootAdapter() { |
arangelov | a3912cf | 2019-12-13 14:34:45 +0000 | [diff] [blame] | 120 | return getActiveListAdapter(); |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | @Override |
arangelov | cf26864 | 2020-01-15 15:09:51 +0000 | [diff] [blame] | 124 | ListView getActiveAdapterView() { |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 125 | return getListViewForIndex(getCurrentPage()); |
| 126 | } |
| 127 | |
arangelov | cf26864 | 2020-01-15 15:09:51 +0000 | [diff] [blame] | 128 | @Override |
| 129 | @Nullable |
| 130 | ViewGroup getInactiveAdapterView() { |
| 131 | if (getCount() == 1) { |
| 132 | return null; |
| 133 | } |
| 134 | return getListViewForIndex(1 - getCurrentPage()); |
| 135 | } |
| 136 | |
arangelov | 38a6fce | 2019-12-02 18:21:22 +0000 | [diff] [blame] | 137 | class ResolverProfileDescriptor extends ProfileDescriptor { |
| 138 | private ResolverListAdapter resolverListAdapter; |
| 139 | final ListView listView; |
| 140 | ResolverProfileDescriptor(ViewGroup rootView, ResolverListAdapter adapter) { |
| 141 | super(rootView); |
| 142 | resolverListAdapter = adapter; |
| 143 | listView = rootView.findViewById(R.id.resolver_list); |
| 144 | } |
| 145 | } |
| 146 | } |