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 19 import android.annotation.Nullable; 20 import android.content.Context; 21 import android.os.UserHandle; 22 import android.view.LayoutInflater; 23 import android.view.View; 24 import android.view.ViewGroup; 25 import android.widget.ListView; 26 27 import com.android.internal.R; 28 import com.android.internal.annotations.VisibleForTesting; 29 import com.android.internal.widget.PagerAdapter; 30 31 /** 32 * A {@link PagerAdapter} which describes the work and personal profile intent resolver screens. 33 */ 34 @VisibleForTesting 35 public class ResolverMultiProfilePagerAdapter extends AbstractMultiProfilePagerAdapter { 36 37 private final ResolverProfileDescriptor[] mItems; 38 private boolean mUseLayoutWithDefault; 39 ResolverMultiProfilePagerAdapter(Context context, ResolverListAdapter adapter, EmptyStateProvider emptyStateProvider, QuietModeManager quietModeManager, UserHandle workProfileUserHandle, UserHandle cloneUserHandle)40 ResolverMultiProfilePagerAdapter(Context context, 41 ResolverListAdapter adapter, 42 EmptyStateProvider emptyStateProvider, 43 QuietModeManager quietModeManager, 44 UserHandle workProfileUserHandle, 45 UserHandle cloneUserHandle) { 46 super(context, /* currentPage */ 0, emptyStateProvider, quietModeManager, 47 workProfileUserHandle, cloneUserHandle); 48 mItems = new ResolverProfileDescriptor[] { 49 createProfileDescriptor(adapter) 50 }; 51 } 52 ResolverMultiProfilePagerAdapter(Context context, ResolverListAdapter personalAdapter, ResolverListAdapter workAdapter, EmptyStateProvider emptyStateProvider, QuietModeManager quietModeManager, @Profile int defaultProfile, UserHandle workProfileUserHandle, UserHandle cloneUserHandle)53 ResolverMultiProfilePagerAdapter(Context context, 54 ResolverListAdapter personalAdapter, 55 ResolverListAdapter workAdapter, 56 EmptyStateProvider emptyStateProvider, 57 QuietModeManager quietModeManager, 58 @Profile int defaultProfile, 59 UserHandle workProfileUserHandle, 60 UserHandle cloneUserHandle) { 61 super(context, /* currentPage */ defaultProfile, emptyStateProvider, quietModeManager, 62 workProfileUserHandle, cloneUserHandle); 63 mItems = new ResolverProfileDescriptor[] { 64 createProfileDescriptor(personalAdapter), 65 createProfileDescriptor(workAdapter) 66 }; 67 } 68 createProfileDescriptor( ResolverListAdapter adapter)69 private ResolverProfileDescriptor createProfileDescriptor( 70 ResolverListAdapter adapter) { 71 final LayoutInflater inflater = LayoutInflater.from(getContext()); 72 final ViewGroup rootView = 73 (ViewGroup) inflater.inflate(R.layout.resolver_list_per_profile, null, false); 74 return new ResolverProfileDescriptor(rootView, adapter); 75 } 76 getListViewForIndex(int index)77 ListView getListViewForIndex(int index) { 78 return getItem(index).listView; 79 } 80 81 @Override getItem(int pageIndex)82 public ResolverProfileDescriptor getItem(int pageIndex) { 83 return mItems[pageIndex]; 84 } 85 86 @Override getItemCount()87 int getItemCount() { 88 return mItems.length; 89 } 90 91 @Override setupListAdapter(int pageIndex)92 void setupListAdapter(int pageIndex) { 93 final ListView listView = getItem(pageIndex).listView; 94 listView.setAdapter(getItem(pageIndex).resolverListAdapter); 95 } 96 97 @Override 98 @VisibleForTesting getAdapterForIndex(int pageIndex)99 public ResolverListAdapter getAdapterForIndex(int pageIndex) { 100 return mItems[pageIndex].resolverListAdapter; 101 } 102 103 @Override instantiateItem(ViewGroup container, int position)104 public ViewGroup instantiateItem(ViewGroup container, int position) { 105 setupListAdapter(position); 106 return super.instantiateItem(container, position); 107 } 108 109 @Override 110 @Nullable getListAdapterForUserHandle(UserHandle userHandle)111 ResolverListAdapter getListAdapterForUserHandle(UserHandle userHandle) { 112 if (getPersonalListAdapter().getUserHandle().equals(userHandle) 113 || userHandle.equals(getCloneUserHandle())) { 114 return getPersonalListAdapter(); 115 } else if (getWorkListAdapter() != null 116 && getWorkListAdapter().getUserHandle().equals(userHandle)) { 117 return getWorkListAdapter(); 118 } 119 return null; 120 } 121 122 @Override 123 @VisibleForTesting getActiveListAdapter()124 public ResolverListAdapter getActiveListAdapter() { 125 return getAdapterForIndex(getCurrentPage()); 126 } 127 128 @Override 129 @VisibleForTesting getInactiveListAdapter()130 public ResolverListAdapter getInactiveListAdapter() { 131 if (getCount() == 1) { 132 return null; 133 } 134 return getAdapterForIndex(1 - getCurrentPage()); 135 } 136 137 @Override getPersonalListAdapter()138 public ResolverListAdapter getPersonalListAdapter() { 139 return getAdapterForIndex(PROFILE_PERSONAL); 140 } 141 142 @Override 143 @Nullable getWorkListAdapter()144 public ResolverListAdapter getWorkListAdapter() { 145 if (getCount() == 1) { 146 return null; 147 } 148 return getAdapterForIndex(PROFILE_WORK); 149 } 150 151 @Override getCurrentRootAdapter()152 ResolverListAdapter getCurrentRootAdapter() { 153 return getActiveListAdapter(); 154 } 155 156 @Override getActiveAdapterView()157 ListView getActiveAdapterView() { 158 return getListViewForIndex(getCurrentPage()); 159 } 160 161 @Override 162 @Nullable getInactiveAdapterView()163 ViewGroup getInactiveAdapterView() { 164 if (getCount() == 1) { 165 return null; 166 } 167 return getListViewForIndex(1 - getCurrentPage()); 168 } 169 setUseLayoutWithDefault(boolean useLayoutWithDefault)170 void setUseLayoutWithDefault(boolean useLayoutWithDefault) { 171 mUseLayoutWithDefault = useLayoutWithDefault; 172 } 173 174 @Override setupContainerPadding(View container)175 protected void setupContainerPadding(View container) { 176 int bottom = mUseLayoutWithDefault ? container.getPaddingBottom() : 0; 177 container.setPadding(container.getPaddingLeft(), container.getPaddingTop(), 178 container.getPaddingRight(), bottom); 179 } 180 181 class ResolverProfileDescriptor extends ProfileDescriptor { 182 private ResolverListAdapter resolverListAdapter; 183 final ListView listView; ResolverProfileDescriptor(ViewGroup rootView, ResolverListAdapter adapter)184 ResolverProfileDescriptor(ViewGroup rootView, ResolverListAdapter adapter) { 185 super(rootView); 186 resolverListAdapter = adapter; 187 listView = rootView.findViewById(R.id.resolver_list); 188 } 189 } 190 } 191