1 /* 2 * Copyright (C) 2020 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.settings.applications.specialaccess.interactacrossprofiles; 18 19 import android.content.Context; 20 import android.content.pm.CrossProfileApps; 21 import android.content.pm.PackageManager; 22 import android.content.pm.UserInfo; 23 import android.os.UserHandle; 24 import android.os.UserManager; 25 26 import com.android.settings.R; 27 import com.android.settings.core.BasePreferenceController; 28 import com.android.settingslib.utils.StringUtil; 29 30 import java.util.List; 31 32 /** 33 * Controller to decide when to show the "Connected work and personal apps" option in the 34 * Special access screen. 35 */ 36 public class InteractAcrossProfilesController extends BasePreferenceController { 37 38 private final Context mContext; 39 private final UserManager mUserManager; 40 private final PackageManager mPackageManager; 41 private final CrossProfileApps mCrossProfileApps; 42 InteractAcrossProfilesController(Context context, String preferenceKey)43 public InteractAcrossProfilesController(Context context, String preferenceKey) { 44 super(context, preferenceKey); 45 46 mContext = context; 47 mUserManager = mContext.getSystemService(UserManager.class); 48 mCrossProfileApps = mContext.getSystemService(CrossProfileApps.class); 49 mPackageManager = mContext.getPackageManager(); 50 } 51 52 @Override getAvailabilityStatus()53 public int getAvailabilityStatus() { 54 final List<UserInfo> profiles = mUserManager.getProfiles(UserHandle.myUserId()); 55 for (final UserInfo userInfo : profiles) { 56 if (userInfo.isManagedProfile()) { 57 return AVAILABLE; 58 } 59 } 60 return DISABLED_FOR_USER; 61 } 62 63 @Override getSummary()64 public CharSequence getSummary() { 65 final int connectedApps = InteractAcrossProfilesSettings.getNumberOfEnabledApps( 66 mContext, mPackageManager, mUserManager, mCrossProfileApps); 67 return connectedApps == 0 68 ? mContext.getResources().getString( 69 R.string.interact_across_profiles_number_of_connected_apps_none) 70 : StringUtil.getIcuPluralsString(mContext, connectedApps, 71 R.string.interact_across_profiles_number_of_connected_apps); 72 } 73 } 74