• 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;
18 
19 import static com.android.settingslib.widget.ProfileSelectFragment.PERSONAL_TAB;
20 import static com.android.settingslib.widget.ProfileSelectFragment.WORK_TAB;
21 
22 import android.annotation.NonNull;
23 import android.annotation.UserIdInt;
24 import android.os.Bundle;
25 import android.os.UserManager;
26 import android.util.Log;
27 import android.view.MenuItem;
28 
29 import androidx.appcompat.app.ActionBar;
30 import androidx.appcompat.app.AppCompatActivity;
31 import androidx.appcompat.widget.Toolbar;
32 import androidx.fragment.app.Fragment;
33 import androidx.fragment.app.FragmentManager;
34 import androidx.lifecycle.ViewModelProvider;
35 
36 import com.android.providers.media.R;
37 import com.android.providers.media.photopicker.data.UserIdManager;
38 import com.android.providers.media.photopicker.ui.settings.SettingsProfileSelectFragment;
39 import com.android.providers.media.photopicker.ui.settings.SettingsViewModel;
40 
41 /**
42  * Photo Picker settings page where user can view/edit current cloud media provider.
43  */
44 public class PhotoPickerSettingsActivity extends AppCompatActivity {
45     private static final String TAG = "PickerSettings";
46     static final String EXTRA_CURRENT_USER_ID = "user_id";
47     private static final int DEFAULT_EXTRA_USER_ID = -1;
48     private static final int DEFAULT_TAB = PERSONAL_TAB;
49 
50     @NonNull
51     private SettingsViewModel mSettingsViewModel;
52 
53     @Override
onCreate(Bundle savedInstanceState)54     protected void onCreate(Bundle savedInstanceState) {
55         // We use the device default theme as the base theme. Apply the material theme for the
56         // material components. We use force "false" here, only values that are not already defined
57         // in the base theme will be copied.
58         getTheme().applyStyle(R.style.PickerMaterialTheme, /* force */ false);
59 
60         super.onCreate(savedInstanceState);
61 
62         mSettingsViewModel =
63                 new ViewModelProvider(this).get(SettingsViewModel.class);
64         final Bundle extras = getIntent().getExtras();
65         final int callingUserId;
66         if (extras != null) {
67             callingUserId = extras.getInt(EXTRA_CURRENT_USER_ID, DEFAULT_EXTRA_USER_ID);
68         } else {
69             callingUserId = DEFAULT_EXTRA_USER_ID;
70         }
71 
72         setContentView(R.layout.activity_photo_picker_settings);
73         displayActionBar();
74         createAndShowFragmentIfNeeded(callingUserId);
75     }
76 
displayActionBar()77     private void displayActionBar() {
78         final Toolbar toolbar = findViewById(R.id.picker_settings_toolbar);
79         setSupportActionBar(toolbar);
80         final ActionBar actionBar = getSupportActionBar();
81         actionBar.setDisplayHomeAsUpEnabled(true);
82         actionBar.setDisplayShowTitleEnabled(false);
83     }
84 
85     @Override
onOptionsItemSelected(@onNull MenuItem item)86     public boolean onOptionsItemSelected(@NonNull MenuItem item) {
87         if (item.getItemId() == android.R.id.home) {
88             // Stop PhotoPickerSettingsActivity when back button is pressed.
89             finish();
90             return true;
91         }
92         return false;
93     }
94 
createAndShowFragmentIfNeeded(@serIdInt int callingUserId)95     private void createAndShowFragmentIfNeeded(@UserIdInt int callingUserId) {
96         final FragmentManager fragmentManager = getSupportFragmentManager();
97         if (fragmentManager.findFragmentById(R.id.settings_fragment_container) != null) {
98             // Fragment already exists and is attached to this Activity.
99             // Nothing further needs to be done.
100             Log.d(TAG, "An instance of target fragment is already attached to the "
101                     + "PhotoPickerSettingsActivity. Not creating a new fragment.");
102             return;
103         }
104 
105         // Create a new fragment and attach it to this Activity. The new fragment could be of type
106         // SettingsProfileSelectFragment or SettingsCloudMediaSelectFragment.
107         final Fragment targetFragment = getTargetFragment(callingUserId);
108         fragmentManager.beginTransaction()
109                 .replace(R.id.settings_fragment_container, targetFragment)
110                 .commitAllowingStateLoss();
111         fragmentManager.executePendingTransactions();
112     }
113 
114     @NonNull
getTargetFragment(@serIdInt int callingUserId)115     private Fragment getTargetFragment(@UserIdInt int callingUserId) {
116         // Target fragment is SettingsProfileSelectFragment if there exists more than one
117         // UserHandles for profiles associated with the context user, including the user itself.
118         // Else target fragment is SettingsCloudMediaSelectFragment
119         final UserIdManager userIdManager = mSettingsViewModel.getUserIdManager();
120         if (userIdManager.isMultiUserProfiles()) {
121             // In case work profile exists and is turned off, do not show the work tab.
122             userIdManager.updateWorkProfileOffValue();
123             if (!userIdManager.isWorkProfileOff()) {
124                 final int selectedProfileTab = getInitialProfileTab(callingUserId);
125                 return SettingsProfileSelectFragment.getProfileSelectFragment(selectedProfileTab);
126             }
127         }
128         return getCloudMediaSelectFragment();
129     }
130 
131     @NonNull
getCloudMediaSelectFragment()132     private Fragment getCloudMediaSelectFragment() {
133         final UserIdManager userIdManager = mSettingsViewModel.getUserIdManager();
134         final int userId = userIdManager.getCurrentUserProfileId().getIdentifier();
135         return SettingsProfileSelectFragment.getCloudMediaSelectFragment(userId);
136     }
137 
138     /**
139      * @return the tab position that should be open when user initially lands on the Settings page.
140      */
getInitialProfileTab(@serIdInt int callingUserId)141     private int getInitialProfileTab(@UserIdInt int callingUserId) {
142         final UserManager userManager = getApplicationContext().getSystemService(UserManager.class);
143         if (userManager == null || callingUserId == DEFAULT_EXTRA_USER_ID) {
144             return DEFAULT_TAB;
145         }
146         return userManager.isManagedProfile(callingUserId) ? WORK_TAB : PERSONAL_TAB;
147     }
148 }
149