blob: 2fd5bfd716567536e6c709fc9310d16c487502a4 [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 */
16package com.android.internal.app;
17import android.annotation.IntDef;
18import android.content.Context;
19import android.os.UserHandle;
20import android.view.View;
21import android.view.ViewGroup;
22
23import com.android.internal.annotations.VisibleForTesting;
24import com.android.internal.widget.PagerAdapter;
25
26import com.android.internal.util.Preconditions;
27import com.android.internal.widget.ViewPager;
28
29/**
30 * Skeletal {@link PagerAdapter} implementation of a work or personal profile page for
31 * intent resolution (including share sheet).
32 */
33public abstract class AbstractMultiProfilePagerAdapter extends PagerAdapter {
34
35 static final int PROFILE_PERSONAL = 0;
36 static final int PROFILE_WORK = 1;
37 @IntDef({PROFILE_PERSONAL, PROFILE_WORK})
38 @interface Profile {}
39
40 private final Context mContext;
41 private int mCurrentPage;
42
43 AbstractMultiProfilePagerAdapter(Context context, int currentPage) {
44 mContext = Preconditions.checkNotNull(context);
45 mCurrentPage = currentPage;
46 }
47
48 Context getContext() {
49 return mContext;
50 }
51
52 /**
53 * Sets this instance of this class as {@link ViewPager}'s {@link PagerAdapter} and sets
54 * an {@link ViewPager.OnPageChangeListener} where it keeps track of the currently displayed
55 * page and rebuilds the list.
56 */
57 void setupViewPager(ViewPager viewPager) {
58 viewPager.setCurrentItem(mCurrentPage);
59 viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
60 @Override
61 public void onPageSelected(int position) {
62 mCurrentPage = position;
63 getCurrentListAdapter().rebuildList();
64 }
65 });
66 viewPager.setAdapter(this);
67 }
68
69 @Override
70 public ViewGroup instantiateItem(ViewGroup container, int position) {
71 final ProfileDescriptor profileDescriptor = getItem(position);
72 setupListAdapter(position);
73 container.addView(profileDescriptor.rootView);
74 return profileDescriptor.rootView;
75 }
76
77 @Override
78 public void destroyItem(ViewGroup container, int position, Object view) {
79 container.removeView((View) view);
80 }
81
82 @Override
83 public int getCount() {
84 return getItemCount();
85 }
86
87 protected int getCurrentPage() {
88 return mCurrentPage;
89 }
90
91 UserHandle getCurrentUserHandle() {
92 return getCurrentListAdapter().mResolverListController.getUserHandle();
93 }
94
95 @Override
96 public boolean isViewFromObject(View view, Object object) {
97 return view == object;
98 }
99
100 @Override
101 public CharSequence getPageTitle(int position) {
102 return null;
103 }
104
105 /**
106 * Returns the {@link ProfileDescriptor} relevant to the given <code>pageIndex</code>.
107 * <ul>
108 * <li>For a device with only one user, <code>pageIndex</code> value of
109 * <code>0</code> would return the personal profile {@link ProfileDescriptor}.</li>
110 * <li>For a device with a work profile, <code>pageIndex</code> value of <code>0</code> would
111 * return the personal profile {@link ProfileDescriptor}, and <code>pageIndex</code> value of
112 * <code>1</code> would return the work profile {@link ProfileDescriptor}.</li>
113 * </ul>
114 */
115 abstract ProfileDescriptor getItem(int pageIndex);
116
117 /**
118 * Returns the number of {@link ProfileDescriptor} objects.
119 * <p>For a normal consumer device with only one user returns <code>1</code>.
120 * <p>For a device with a work profile returns <code>2</code>.
121 */
122 abstract int getItemCount();
123
124 /**
125 * Responsible for assigning an adapter to the list view for the relevant page, specified by
126 * <code>pageIndex</code>, and other list view-related initialization procedures.
127 */
128 abstract void setupListAdapter(int pageIndex);
129
130 /**
131 * Returns the adapter of the list view for the relevant page specified by
132 * <code>pageIndex</code>.
133 * <p>This method is meant to be implemented with an implementation-specific return type
134 * depending on the adapter type.
135 */
136 abstract Object getAdapterForIndex(int pageIndex);
137
138 @VisibleForTesting
139 public abstract ResolverListAdapter getCurrentListAdapter();
140
141 abstract Object getCurrentRootAdapter();
142
143 abstract ViewGroup getCurrentAdapterView();
144
145 protected class ProfileDescriptor {
146 final ViewGroup rootView;
147 ProfileDescriptor(ViewGroup rootView) {
148 this.rootView = rootView;
149 }
150 }
151}