Create ProfileSelector to provide TabLayout with Material Next style

- Create ProfileSelectFragment
- Move Tabs related resource into ProfileSelector

Bug: 195009187
Test: visual test & robotest
Change-Id: Ica20a6e1e7e534c35cb3ac3ed2d69f3cb9682fb7
This commit is contained in:
Edgar Wang
2022-10-18 17:13:54 +08:00
parent 862f455db7
commit 1724dfdb88
14 changed files with 272 additions and 0 deletions

View File

@@ -54,6 +54,7 @@ android_library {
"SettingsLibSettingsTransition",
"SettingsLibButtonPreference",
"SettingsLibDeviceStateRotationLock",
"SettingsLibProfileSelector",
"setupdesign",
"zxing-core-1.7",
"androidx.room_room-runtime",

View File

@@ -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",
],
}

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2020 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.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.settingslib.widget">
<uses-sdk android:minSdkVersion="23" />
</manifest>

View File

@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
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.
-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:theme="@style/Theme.MaterialComponents.DayNight"
android:id="@+id/tab_container"
android:clipToPadding="true"
android:clipChildren="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabs"
style="@style/SettingsLibTabsStyle"/>
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.viewpager2.widget.ViewPager2>
</LinearLayout>

View File

@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
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.
-->
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<!-- Header for items under the personal user [CHAR LIMIT=30] -->
<string name="settingslib_category_personal">Personal</string>
<!-- Header for items under the work user [CHAR LIMIT=30] -->
<string name="settingslib_category_work">Work</string>
</resources>

View File

@@ -0,0 +1,118 @@
/*
* 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 android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
/**
* Base fragment class for profile settings.
*/
public abstract class ProfileSelectFragment extends Fragment {
/**
* Personal or Work profile tab of {@link ProfileSelectFragment}
* <p>0: Personal tab.
* <p>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
* <p>0: Personal profile.
* <p>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);
}
}

View File

@@ -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;
}
}