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.os.Process; 21 import android.os.UserHandle; 22 23 import androidx.annotation.NonNull; 24 import androidx.annotation.Nullable; 25 import androidx.lifecycle.AndroidViewModel; 26 import androidx.lifecycle.LiveData; 27 import androidx.lifecycle.Transformations; 28 import androidx.lifecycle.ViewModel; 29 30 import com.android.permissioncontroller.role.utils.UserUtils; 31 32 import java.util.List; 33 34 /** 35 * {@link ViewModel} for the list of default apps. 36 */ 37 public class DefaultAppListViewModel extends AndroidViewModel { 38 39 @NonNull 40 private final UserHandle mUser; 41 @NonNull 42 private final LiveData<List<RoleItem>> mLiveData; 43 @Nullable 44 private final UserHandle mWorkProfile; 45 @Nullable 46 private final LiveData<List<RoleItem>> mWorkLiveData; 47 DefaultAppListViewModel(@onNull Application application)48 public DefaultAppListViewModel(@NonNull Application application) { 49 super(application); 50 51 mUser = Process.myUserHandle(); 52 RoleListSortFunction sortFunction = new RoleListSortFunction(application); 53 mLiveData = Transformations.map(new RoleListLiveData(true, mUser, application), 54 sortFunction); 55 mWorkProfile = UserUtils.getWorkProfile(application); 56 mWorkLiveData = mWorkProfile != null ? Transformations.map(new RoleListLiveData(true, 57 mWorkProfile, application), sortFunction) : null; 58 } 59 60 @NonNull getUser()61 public UserHandle getUser() { 62 return mUser; 63 } 64 65 @NonNull getLiveData()66 public LiveData<List<RoleItem>> getLiveData() { 67 return mLiveData; 68 } 69 70 /** 71 * Check whether the user has a work profile. 72 * 73 * @return whether the user has a work profile. 74 */ hasWorkProfile()75 public boolean hasWorkProfile() { 76 return mWorkProfile != null; 77 } 78 79 @Nullable getWorkProfile()80 public UserHandle getWorkProfile() { 81 return mWorkProfile; 82 } 83 84 @Nullable getWorkLiveData()85 public LiveData<List<RoleItem>> getWorkLiveData() { 86 return mWorkLiveData; 87 } 88 } 89