1 /*
2  * Copyright (C) 2023 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 android.app.Application;
20 import android.content.Context;
21 
22 import androidx.annotation.NonNull;
23 import androidx.lifecycle.AndroidViewModel;
24 
25 import com.android.modules.utils.build.SdkLevel;
26 import com.android.providers.media.ConfigStore;
27 import com.android.providers.media.MediaApplication;
28 import com.android.providers.media.photopicker.data.UserIdManager;
29 import com.android.providers.media.photopicker.data.UserManagerState;
30 
31 /**
32  * SettingsViewModel stores common objects used across PhotoPickerSettingsActivity and
33  * SettingsProfileSelectFragment. It also stores the tab selected state which helps maintain tab
34  * state when activity is destroyed and recreated.
35  */
36 public class SettingsViewModel extends AndroidViewModel {
37     public static final int TAB_NOT_SET = -1;
38 
39     private final UserIdManager mUserIdManager;
40     private final UserManagerState mUserManagerState;
41     private int mSelectedTab;
42     private ConfigStore mConfigStore;
43 
SettingsViewModel(@onNull Application application)44     public SettingsViewModel(@NonNull Application application) {
45         super(application);
46 
47         final Context context = application.getApplicationContext();
48         initConfigStore();
49         if (mConfigStore.isPrivateSpaceInPhotoPickerEnabled() && SdkLevel.isAtLeastS()) {
50             mUserManagerState = UserManagerState.create(context);
51             mUserIdManager = null;
52         } else {
53             mUserIdManager = UserIdManager.create(context);
54             mUserManagerState = null;
55         }
56         mSelectedTab = TAB_NOT_SET;
57     }
58 
initConfigStore()59     private void initConfigStore() {
60         mConfigStore = MediaApplication.getConfigStore();
61     }
62 
63     /**
64      * @return the {@link ConfigStore} for this context.
65      */
getConfigStore()66     public ConfigStore getConfigStore() {
67         return mConfigStore;
68     }
69 
setSelectedTab(int selectedTab)70     public void setSelectedTab(int selectedTab) {
71         mSelectedTab = selectedTab;
72     }
73 
getSelectedTab()74     public int getSelectedTab() {
75         return mSelectedTab;
76     }
77 
getUserIdManager()78     public UserIdManager getUserIdManager() {
79         return mUserIdManager;
80     }
81 
getUserManagerState()82     public UserManagerState getUserManagerState() {
83         return mUserManagerState;
84     }
85 }
86