diff --git a/packages/SettingsLib/Android.bp b/packages/SettingsLib/Android.bp
index 9a80b02679763..4b60341dd686f 100644
--- a/packages/SettingsLib/Android.bp
+++ b/packages/SettingsLib/Android.bp
@@ -50,6 +50,7 @@ android_library {
"SettingsLibTwoTargetPreference",
"SettingsLibSettingsTransition",
"SettingsLibButtonPreference",
+ "SettingsLibProfileSelector",
"setupdesign",
"zxing-core-1.7",
],
diff --git a/packages/SettingsLib/ProfileSelector/Android.bp b/packages/SettingsLib/ProfileSelector/Android.bp
new file mode 100644
index 0000000000000..7846e67a80d1f
--- /dev/null
+++ b/packages/SettingsLib/ProfileSelector/Android.bp
@@ -0,0 +1,26 @@
+package {
+ // See: http://go/android-license-faq
+ // A large-scale-change added 'default_applicable_licenses' to import
+ // all of the 'license_kinds' from "frameworks_base_license"
+ // to get the below license kinds:
+ // SPDX-license-identifier-Apache-2.0
+ default_applicable_licenses: ["frameworks_base_license"],
+}
+
+android_library {
+ name: "SettingsLibProfileSelector",
+
+ srcs: ["src/**/*.java"],
+ resource_dirs: ["res"],
+
+ static_libs: [
+ "com.google.android.material_material",
+ "SettingsLibSettingsTheme",
+ ],
+
+ sdk_version: "system_current",
+ min_sdk_version: "23",
+ apex_available: [
+ "//apex_available:platform",
+ ],
+}
diff --git a/packages/SettingsLib/ProfileSelector/AndroidManifest.xml b/packages/SettingsLib/ProfileSelector/AndroidManifest.xml
new file mode 100644
index 0000000000000..a57469e39eb6f
--- /dev/null
+++ b/packages/SettingsLib/ProfileSelector/AndroidManifest.xml
@@ -0,0 +1,22 @@
+
+
+
+
0: Personal tab. + *
1: Work profile tab. + */ + public static final String EXTRA_SHOW_FRAGMENT_TAB = + ":settings:show_fragment_tab"; + + /** + * Used in fragment argument with Extra key EXTRA_SHOW_FRAGMENT_TAB + */ + public static final int PERSONAL_TAB = 0; + + /** + * Used in fragment argument with Extra key EXTRA_SHOW_FRAGMENT_TAB + */ + public static final int WORK_TAB = 1; + + private ViewGroup mContentView; + + private ViewPager2 mViewPager; + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + // Defines the xml file for the fragment + mContentView = (ViewGroup) inflater.inflate(R.layout.tab_fragment, container, false); + + final Activity activity = getActivity(); + final int titleResId = getTitleResId(); + if (titleResId > 0) { + activity.setTitle(titleResId); + } + final int selectedTab = getTabId(activity, getArguments()); + + final View tabContainer = mContentView.findViewById(R.id.tab_container); + mViewPager = tabContainer.findViewById(R.id.view_pager); + mViewPager.setAdapter(new ProfileViewPagerAdapter(this)); + final TabLayout tabs = tabContainer.findViewById(R.id.tabs); + new TabLayoutMediator(tabs, mViewPager, + (tab, position) -> tab.setText(getPageTitle(position)) + ).attach(); + + tabContainer.setVisibility(View.VISIBLE); + final TabLayout.Tab tab = tabs.getTabAt(selectedTab); + tab.select(); + + return mContentView; + } + + /** + * create Personal or Work profile fragment + *
0: Personal profile. + *
1: Work profile. + */ + public abstract Fragment createFragment(int position); + + /** + * Returns a resource ID of the title + * Override this if the title needs to be updated dynamically. + */ + public int getTitleResId() { + return 0; + } + + int getTabId(Activity activity, Bundle bundle) { + if (bundle != null) { + final int extraTab = bundle.getInt(EXTRA_SHOW_FRAGMENT_TAB, -1); + if (extraTab != -1) { + return extraTab; + } + } + return PERSONAL_TAB; + } + + private CharSequence getPageTitle(int position) { + if (position == WORK_TAB) { + return getContext().getString(R.string.settingslib_category_work); + } + + return getString(R.string.settingslib_category_personal); + } +} diff --git a/packages/SettingsLib/ProfileSelector/src/com/android/settingslib/widget/ProfileViewPagerAdapter.java b/packages/SettingsLib/ProfileSelector/src/com/android/settingslib/widget/ProfileViewPagerAdapter.java new file mode 100644 index 0000000000000..daf2564a674ec --- /dev/null +++ b/packages/SettingsLib/ProfileSelector/src/com/android/settingslib/widget/ProfileViewPagerAdapter.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settingslib.widget; + +import androidx.fragment.app.Fragment; +import androidx.viewpager2.adapter.FragmentStateAdapter; + +/** + * ViewPager Adapter to handle between TabLayout and ViewPager2 + */ +public class ProfileViewPagerAdapter extends FragmentStateAdapter { + + private final ProfileSelectFragment mParentFragments; + + ProfileViewPagerAdapter(ProfileSelectFragment fragment) { + super(fragment); + mParentFragments = fragment; + } + + @Override + public Fragment createFragment(int position) { + return mParentFragments.createFragment(position); + } + + @Override + public int getItemCount() { + return 2; + } +}