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.provider.Settings;
23 import android.util.Log;
24 
25 import com.android.settings.R;
26 import com.android.settings.core.TogglePreferenceController;
27 
28 import java.util.Set;
29 
30 public class CrossProfileCalendarPreferenceController extends TogglePreferenceController {
31 
32     private static final String TAG = "CrossProfileCalendarPreferenceController";
33 
34     private UserHandle mManagedUser;
35 
CrossProfileCalendarPreferenceController(Context context, String key)36     public CrossProfileCalendarPreferenceController(Context context, String key) {
37         super(context, key);
38     }
39 
setManagedUser(UserHandle managedUser)40     public void setManagedUser(UserHandle managedUser) {
41         mManagedUser = managedUser;
42     }
43 
44     @Override
getAvailabilityStatus()45     public int getAvailabilityStatus() {
46         if (mManagedUser != null
47                 && !isCrossProfileCalendarDisallowedByAdmin(
48                         mContext, mManagedUser.getIdentifier())) {
49             return AVAILABLE;
50         }
51 
52         return DISABLED_FOR_USER;
53     }
54 
55     @Override
isChecked()56     public boolean isChecked() {
57         if (mManagedUser == null) {
58             return false;
59         }
60         return Settings.Secure.getIntForUser(mContext.getContentResolver(),
61                 CROSS_PROFILE_CALENDAR_ENABLED, /* default= */ 0,
62                 mManagedUser.getIdentifier()) == 1;
63     }
64 
65     @Override
setChecked(boolean isChecked)66     public boolean setChecked(boolean isChecked) {
67         if (mManagedUser == null) {
68             return false;
69         }
70         final int value = isChecked ? 1 : 0;
71         return Settings.Secure.putIntForUser(mContext.getContentResolver(),
72                 CROSS_PROFILE_CALENDAR_ENABLED, value, mManagedUser.getIdentifier());
73     }
74 
75     @Override
getSliceHighlightMenuRes()76     public int getSliceHighlightMenuRes() {
77         return R.string.menu_key_accounts;
78     }
79 
isCrossProfileCalendarDisallowedByAdmin(Context context, int userId)80     static boolean isCrossProfileCalendarDisallowedByAdmin(Context context, int userId) {
81         final Context managedProfileContext = createPackageContextAsUser(context, userId);
82         final DevicePolicyManager dpm = managedProfileContext.getSystemService(
83                 DevicePolicyManager.class);
84         if (dpm == null) {
85             return true;
86         }
87         final Set<String> packages = dpm.getCrossProfileCalendarPackages();
88         return packages != null && packages.isEmpty();
89     }
90 
createPackageContextAsUser(Context context, int userId)91     private static Context createPackageContextAsUser(Context context, int userId) {
92         try {
93             return context.createPackageContextAsUser(
94                     context.getPackageName(), 0 /* flags */, UserHandle.of(userId));
95         } catch (PackageManager.NameNotFoundException e) {
96             Log.e(TAG, "Failed to create user context", e);
97         }
98         return null;
99     }
100 }