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