• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.providers.media.photopicker.ui.settings;
18 
19 import static com.android.providers.media.photopicker.ui.settings.SettingsCloudMediaSelectFragment.EXTRA_TAB_USER_ID;
20 
21 import android.annotation.NonNull;
22 import android.annotation.UserIdInt;
23 import android.content.Context;
24 import android.os.Bundle;
25 import android.view.View;
26 
27 import androidx.fragment.app.Fragment;
28 import androidx.lifecycle.ViewModelProvider;
29 
30 import com.android.providers.media.photopicker.data.UserIdManager;
31 import com.android.settingslib.widget.ProfileSelectFragment;
32 import com.android.settingslib.widget.R;
33 
34 import com.google.android.material.tabs.TabLayout;
35 
36 /**
37  * This fragment will display swipable "Personal" and "Work" tabs on the settings page.
38  */
39 public class SettingsProfileSelectFragment extends ProfileSelectFragment {
40     @NonNull
41     private SettingsViewModel mSettingsViewModel;
42     @NonNull
43     private TabLayout mTabLayout;
44 
45     @Override
onAttach(@onNull Context context)46     public void onAttach(@NonNull Context context) {
47         super.onAttach(context);
48 
49         mSettingsViewModel =
50                 new ViewModelProvider(requireActivity()).get(SettingsViewModel.class);
51     }
52 
53     @Override
onViewCreated(View view, Bundle savedInstanceState)54     public void onViewCreated(View view, Bundle savedInstanceState) {
55         super.onViewCreated(view, savedInstanceState);
56 
57         mTabLayout = getTabLayout(view);
58     }
59 
60     @Override
createFragment(int tabPosition)61     public Fragment createFragment(int tabPosition) {
62         final int userId = getTabUserId(tabPosition);
63 
64         return getCloudMediaSelectFragment(userId);
65     }
66 
67     @Override
onPause()68     public void onPause() {
69         super.onPause();
70 
71         // Save selected tab state in ViewModel.
72         final int selectedTab = mTabLayout.getSelectedTabPosition();
73         mSettingsViewModel.setSelectedTab(selectedTab);
74     }
75 
76     @Override
onResume()77     public void onResume() {
78         super.onResume();
79 
80         // Set selected tab according to saved state.
81         final int previouslySelectedTab = mSettingsViewModel.getSelectedTab();
82         if (previouslySelectedTab != SettingsViewModel.TAB_NOT_SET) {
83             // Selected tab state has previously been set in onPause() and we should restore it.
84             mTabLayout.getTabAt(previouslySelectedTab).select();
85         }
86     }
87 
88     /**
89      * Create an instance of {@link SettingsCloudMediaSelectFragment}.
90      *
91      * @param userId User will be able to view and update cloud media providers in
92      * {@link SettingsCloudMediaSelectFragment} for the given userId.
93      * @return {@link SettingsCloudMediaSelectFragment} for given userId.
94      */
95     @NonNull
getCloudMediaSelectFragment( @serIdInt int userId)96     public static SettingsCloudMediaSelectFragment getCloudMediaSelectFragment(
97             @UserIdInt int userId) {
98         // Add extras to communicate the fragment can choose cloud media provider for which userId.
99         final SettingsCloudMediaSelectFragment fragment = new SettingsCloudMediaSelectFragment();
100         final Bundle extras = new Bundle();
101         extras.putInt(EXTRA_TAB_USER_ID, userId);
102         fragment.setArguments(extras);
103 
104         return fragment;
105     }
106 
107     /**
108      * Create an instance of {@link SettingsProfileSelectFragment}.
109      *
110      * @param selectedProfileTab is the tab id of the work/profile tab that should be selected when
111      *                           the user first lands on the settings page.
112      * @return A new {@link SettingsProfileSelectFragment} object.
113      */
114     @NonNull
getProfileSelectFragment(int selectedProfileTab)115     public static SettingsProfileSelectFragment getProfileSelectFragment(int selectedProfileTab) {
116         final SettingsProfileSelectFragment fragment = new SettingsProfileSelectFragment();
117         final Bundle extras = new Bundle();
118         extras.putInt(EXTRA_SHOW_FRAGMENT_TAB, selectedProfileTab);
119         fragment.setArguments(extras);
120         return fragment;
121     }
122 
123     @UserIdInt
getTabUserId(int tabPosition)124     private int getTabUserId(int tabPosition) {
125         final UserIdManager userIdManager = mSettingsViewModel.getUserIdManager();
126 
127         switch (tabPosition) {
128             case ProfileSelectFragment.PERSONAL_TAB:
129                 return userIdManager.getPersonalUserId().getIdentifier();
130             case ProfileSelectFragment.WORK_TAB:
131                 return userIdManager.getManagedUserId().getIdentifier();
132             default:
133                 // tabPosition should match one of the cases above.
134                 throw new IllegalArgumentException("Unidentified tab id " + tabPosition);
135         }
136     }
137 
138     @NonNull
getTabLayout(@onNull View view)139     private TabLayout getTabLayout(@NonNull View view) {
140         final View tabContainer = view.findViewById(R.id.tab_container);
141 
142         return tabContainer.findViewById(R.id.tabs);
143     }
144 }
145