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.content.pm.ApplicationInfo; 22 import android.os.UserHandle; 23 import android.util.Log; 24 import android.util.Pair; 25 26 import androidx.annotation.NonNull; 27 import androidx.lifecycle.AndroidViewModel; 28 import androidx.lifecycle.LiveData; 29 import androidx.lifecycle.Transformations; 30 import androidx.lifecycle.ViewModel; 31 import androidx.lifecycle.ViewModelProvider; 32 33 import com.android.permissioncontroller.role.model.Role; 34 35 import java.util.List; 36 37 /** 38 * {@link ViewModel} for a default app. 39 */ 40 public class DefaultAppViewModel extends AndroidViewModel { 41 42 private static final String LOG_TAG = DefaultAppViewModel.class.getSimpleName(); 43 44 @NonNull 45 private final Role mRole; 46 @NonNull 47 private final UserHandle mUser; 48 49 @NonNull 50 private final LiveData<List<Pair<ApplicationInfo, Boolean>>> mRoleLiveData; 51 52 @NonNull 53 private final ManageRoleHolderStateLiveData mManageRoleHolderStateLiveData = 54 new ManageRoleHolderStateLiveData(); 55 DefaultAppViewModel(@onNull Role role, @NonNull UserHandle user, @NonNull Application application)56 public DefaultAppViewModel(@NonNull Role role, @NonNull UserHandle user, 57 @NonNull Application application) { 58 super(application); 59 60 mRole = role; 61 mUser = user; 62 63 mRoleLiveData = Transformations.map(new RoleLiveData(mRole, mUser, application), 64 new RoleSortFunction(application)); 65 } 66 67 @NonNull getRoleLiveData()68 public LiveData<List<Pair<ApplicationInfo, Boolean>>> getRoleLiveData() { 69 return mRoleLiveData; 70 } 71 72 @NonNull getManageRoleHolderStateLiveData()73 public ManageRoleHolderStateLiveData getManageRoleHolderStateLiveData() { 74 return mManageRoleHolderStateLiveData; 75 } 76 77 /** 78 * Set an application as the default app. 79 * 80 * @param packageName the package name of the application 81 */ setDefaultApp(@onNull String packageName)82 public void setDefaultApp(@NonNull String packageName) { 83 if (mManageRoleHolderStateLiveData.getValue() != ManageRoleHolderStateLiveData.STATE_IDLE) { 84 Log.i(LOG_TAG, "Trying to set default app while another request is on-going"); 85 return; 86 } 87 mManageRoleHolderStateLiveData.setRoleHolderAsUser(mRole.getName(), packageName, true, 0, 88 mUser, getApplication()); 89 } 90 91 /** 92 * Set "None" as the default app. 93 */ setNoneDefaultApp()94 public void setNoneDefaultApp() { 95 Context context = getApplication(); 96 mRole.onNoneHolderSelectedAsUser(mUser, context); 97 if (mManageRoleHolderStateLiveData.getValue() != ManageRoleHolderStateLiveData.STATE_IDLE) { 98 Log.i(LOG_TAG, "Trying to set default app while another request is on-going"); 99 return; 100 } 101 mManageRoleHolderStateLiveData.clearRoleHoldersAsUser(mRole.getName(), 0, mUser, context); 102 } 103 104 /** 105 * {@link ViewModelProvider.Factory} for {@link DefaultAppViewModel}. 106 */ 107 public static class Factory implements ViewModelProvider.Factory { 108 109 @NonNull 110 private Role mRole; 111 112 @NonNull 113 private UserHandle mUser; 114 115 @NonNull 116 private Application mApplication; 117 Factory(@onNull Role role, @NonNull UserHandle user, @NonNull Application application)118 public Factory(@NonNull Role role, @NonNull UserHandle user, 119 @NonNull Application application) { 120 mRole = role; 121 mUser = user; 122 mApplication = application; 123 } 124 125 @NonNull 126 @Override create(@onNull Class<T> modelClass)127 public <T extends ViewModel> T create(@NonNull Class<T> modelClass) { 128 //noinspection unchecked 129 return (T) new DefaultAppViewModel(mRole, mUser, mApplication); 130 } 131 } 132 } 133