• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.permissioncontroller.role.ui;
18 
19 import android.app.Application;
20 import android.content.Context;
21 import android.os.UserHandle;
22 import android.util.Log;
23 
24 import androidx.annotation.NonNull;
25 import androidx.lifecycle.AndroidViewModel;
26 import androidx.lifecycle.LiveData;
27 import androidx.lifecycle.Transformations;
28 import androidx.lifecycle.ViewModel;
29 import androidx.lifecycle.ViewModelProvider;
30 
31 import com.android.permissioncontroller.role.utils.RoleUiBehaviorUtils;
32 import com.android.permissioncontroller.role.utils.UserUtils;
33 import com.android.role.controller.model.Role;
34 
35 import java.util.List;
36 import java.util.function.Predicate;
37 
38 /**
39  * {@link ViewModel} for a default app.
40  */
41 public class DefaultAppViewModel extends AndroidViewModel {
42 
43     private static final String LOG_TAG = DefaultAppViewModel.class.getSimpleName();
44 
45     @NonNull
46     private final Role mRole;
47     @NonNull
48     private final UserHandle mUser;
49 
50     @NonNull
51     private final LiveData<List<RoleApplicationItem>> mRecommendedLiveData;
52 
53     @NonNull
54     private final LiveData<List<RoleApplicationItem>> mLiveData;
55 
56     @NonNull
57     private final ManageRoleHolderStateLiveData mManageRoleHolderStateLiveData =
58             new ManageRoleHolderStateLiveData();
59 
DefaultAppViewModel(@onNull Role role, @NonNull UserHandle user, @NonNull Application application)60     public DefaultAppViewModel(@NonNull Role role, @NonNull UserHandle user,
61             @NonNull Application application) {
62         super(application);
63 
64         mRole = role;
65         // If EXCLUSIVITY_PROFILE_GROUP this user should be profile parent
66         mUser = role.getExclusivity() == Role.EXCLUSIVITY_PROFILE_GROUP
67                 ? UserUtils.getProfileParentOrSelf(user, application)
68                 : user;
69         RoleLiveData userLiveData = new RoleLiveData(role, mUser, application);
70         RoleSortFunction sortFunction = new RoleSortFunction(application);
71         LiveData<List<RoleApplicationItem>> liveData;
72         if (role.getExclusivity() == Role.EXCLUSIVITY_PROFILE_GROUP) {
73             // Context user might be work profile, ensure we get a non-null UserHandle if work
74             // profile exists. getWorkProfile returns null if context user is work profile.
75             UserHandle workProfile  = UserUtils.getWorkProfileOrSelf(application);
76             if (workProfile != null) {
77                 RoleLiveData workLiveData = new RoleLiveData(role, workProfile, application);
78                 liveData = Transformations.map(new MergeRoleLiveData(userLiveData, workLiveData),
79                         sortFunction);
80             } else {
81                 liveData = Transformations.map(userLiveData, sortFunction);
82             }
83         } else {
84             liveData = Transformations.map(userLiveData, sortFunction);
85         }
86         Predicate<RoleApplicationItem> recommendedApplicationFilter =
87                 RoleUiBehaviorUtils.getRecommendedApplicationFilter(role, application);
88         mRecommendedLiveData = Transformations.map(liveData,
89                 new ListLiveDataFilterFunction<>(recommendedApplicationFilter));
90         mLiveData = Transformations.map(liveData,
91                 new ListLiveDataFilterFunction<>(recommendedApplicationFilter.negate()));
92     }
93 
94     @NonNull
getRecommendedLiveData()95     public LiveData<List<RoleApplicationItem>> getRecommendedLiveData() {
96         return mRecommendedLiveData;
97     }
98 
99     @NonNull
getLiveData()100     public LiveData<List<RoleApplicationItem>> getLiveData() {
101         return mLiveData;
102     }
103 
104     @NonNull
getManageRoleHolderStateLiveData()105     public ManageRoleHolderStateLiveData getManageRoleHolderStateLiveData() {
106         return mManageRoleHolderStateLiveData;
107     }
108 
109     /**
110      * Set an application as the default app.
111      *
112      * @param packageName the package name of the application
113      */
setDefaultApp(@onNull String packageName, @NonNull UserHandle user)114     public void setDefaultApp(@NonNull String packageName, @NonNull UserHandle user) {
115         if (mManageRoleHolderStateLiveData.getValue() != ManageRoleHolderStateLiveData.STATE_IDLE) {
116             Log.i(LOG_TAG, "Trying to set default app while another request is on-going");
117             return;
118         }
119         mManageRoleHolderStateLiveData.setRoleHolderAsUser(mRole.getName(), packageName, true, 0,
120                 user, getApplication());
121     }
122 
123     /**
124      * Set "None" as the default app.
125      */
setNoneDefaultApp()126     public void setNoneDefaultApp() {
127         Context context = getApplication();
128         UserHandle user = mRole.getExclusivity() == Role.EXCLUSIVITY_PROFILE_GROUP
129                 ? UserUtils.getProfileParentOrSelf(mUser, context)
130                 : mUser;
131         mRole.onNoneHolderSelectedAsUser(user, context);
132         if (mManageRoleHolderStateLiveData.getValue() != ManageRoleHolderStateLiveData.STATE_IDLE) {
133             Log.i(LOG_TAG, "Trying to set default app while another request is on-going");
134             return;
135         }
136         mManageRoleHolderStateLiveData.clearRoleHoldersAsUser(mRole.getName(), 0, user, context);
137     }
138 
139     /**
140      * {@link ViewModelProvider.Factory} for {@link DefaultAppViewModel}.
141      */
142     public static class Factory implements ViewModelProvider.Factory {
143 
144         @NonNull
145         private Role mRole;
146 
147         @NonNull
148         private UserHandle mUser;
149 
150         @NonNull
151         private Application mApplication;
152 
Factory(@onNull Role role, @NonNull UserHandle user, @NonNull Application application)153         public Factory(@NonNull Role role, @NonNull UserHandle user,
154                 @NonNull Application application) {
155             mRole = role;
156             mUser = user;
157             mApplication = application;
158         }
159 
160         @NonNull
161         @Override
create(@onNull Class<T> modelClass)162         public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
163             //noinspection unchecked
164             return (T) new DefaultAppViewModel(mRole, mUser, mApplication);
165         }
166     }
167 }
168