1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 package com.android.settings.accounts;
15 
16 import static android.provider.Settings.Secure.CROSS_PROFILE_CALENDAR_ENABLED;
17 
18 import android.app.admin.DevicePolicyManager;
19 import android.content.Context;
20 import android.content.pm.PackageManager;
21 import android.os.UserHandle;
22 import android.os.UserManager;
23 import android.provider.Settings;
24 import android.util.Log;
25 
26 import androidx.annotation.VisibleForTesting;
27 
28 import com.android.settings.R;
29 import com.android.settings.Utils;
30 import com.android.settings.core.TogglePreferenceController;
31 
32 import java.util.Set;
33 
34 public class CrossProfileCalendarPreferenceController extends TogglePreferenceController {
35 
36     private static final String TAG = "CrossProfileCalendarPreferenceController";
37 
38     private UserHandle mManagedUser;
39 
CrossProfileCalendarPreferenceController(Context context, String key)40     public CrossProfileCalendarPreferenceController(Context context, String key) {
41         super(context, key);
42         // Set default managed profile for the current user, otherwise isAvailable will be false and
43         // the setting won't be searchable.
44         UserManager userManager = context.getSystemService(UserManager.class);
45         mManagedUser = Utils.getManagedProfile(userManager);
46     }
47 
48     @VisibleForTesting
setManagedUser(UserHandle managedUser)49     void setManagedUser(UserHandle managedUser) {
50         mManagedUser = managedUser;
51     }
52 
53     @Override
getAvailabilityStatus()54     public int getAvailabilityStatus() {
55         if (mManagedUser != null
56                 && !isCrossProfileCalendarDisallowedByAdmin(
57                         mContext, mManagedUser.getIdentifier())) {
58             return AVAILABLE;
59         }
60 
61         return DISABLED_FOR_USER;
62     }
63 
64     @Override
isChecked()65     public boolean isChecked() {
66         if (mManagedUser == null) {
67             return false;
68         }
69         return Settings.Secure.getIntForUser(mContext.getContentResolver(),
70                 CROSS_PROFILE_CALENDAR_ENABLED, /* default= */ 0,
71                 mManagedUser.getIdentifier()) == 1;
72     }
73 
74     @Override
setChecked(boolean isChecked)75     public boolean setChecked(boolean isChecked) {
76         if (mManagedUser == null) {
77             return false;
78         }
79         final int value = isChecked ? 1 : 0;
80         return Settings.Secure.putIntForUser(mContext.getContentResolver(),
81                 CROSS_PROFILE_CALENDAR_ENABLED, value, mManagedUser.getIdentifier());
82     }
83 
84     @Override
getSliceHighlightMenuRes()85     public int getSliceHighlightMenuRes() {
86         return R.string.menu_key_accounts;
87     }
88 
isCrossProfileCalendarDisallowedByAdmin(Context context, int userId)89     static boolean isCrossProfileCalendarDisallowedByAdmin(Context context, int userId) {
90         final Context managedProfileContext = createPackageContextAsUser(context, userId);
91         final DevicePolicyManager dpm = managedProfileContext.getSystemService(
92                 DevicePolicyManager.class);
93         if (dpm == null) {
94             return true;
95         }
96         final Set<String> packages = dpm.getCrossProfileCalendarPackages();
97         return packages != null && packages.isEmpty();
98     }
99 
createPackageContextAsUser(Context context, int userId)100     private static Context createPackageContextAsUser(Context context, int userId) {
101         try {
102             return context.createPackageContextAsUser(
103                     context.getPackageName(), 0 /* flags */, UserHandle.of(userId));
104         } catch (PackageManager.NameNotFoundException e) {
105             Log.e(TAG, "Failed to create user context", e);
106         }
107         return null;
108     }
109 }