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