1 /* 2 * Copyright (C) 2021 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 package com.android.car.settings.enterprise; 17 18 import android.app.admin.DevicePolicyManager; 19 import android.car.drivingstate.CarUxRestrictions; 20 import android.content.ComponentName; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.pm.PackageManager; 24 import android.content.pm.ResolveInfo; 25 import android.os.UserHandle; 26 import android.os.UserManager; 27 import android.provider.Settings; 28 29 import androidx.annotation.Nullable; 30 import androidx.preference.Preference; 31 32 import com.android.car.settings.common.FragmentController; 33 import com.android.car.settings.common.Logger; 34 import com.android.car.settings.common.PreferenceController; 35 36 import java.util.List; 37 38 /** 39 * Controller for showing the work policy info in the privacy dashboard. 40 */ 41 public final class WorkPolicyInfoPreferenceController extends PreferenceController<Preference> { 42 43 private static final Logger LOG = new Logger(WorkPolicyInfoPreferenceController.class); 44 private static final int MY_USER_ID = UserHandle.myUserId(); 45 46 private final DevicePolicyManager mDpm; 47 private final PackageManager mPm; 48 private final UserManager mUm; 49 private final boolean mEnabled; 50 51 @Nullable 52 private Intent mIntent; 53 WorkPolicyInfoPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)54 public WorkPolicyInfoPreferenceController(Context context, String preferenceKey, 55 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 56 super(context, preferenceKey, fragmentController, uxRestrictions); 57 58 mDpm = context.getSystemService(DevicePolicyManager.class); 59 mPm = context.getPackageManager(); 60 mUm = context.getSystemService(UserManager.class); 61 mEnabled = mPm.hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN); 62 63 LOG.d("Constructed on user " + MY_USER_ID + ": " + (mEnabled ? "enabled" : "disabled")); 64 } 65 66 @Override getPreferenceType()67 protected Class<Preference> getPreferenceType() { 68 return Preference.class; 69 } 70 71 @Override updateState(Preference preference)72 protected void updateState(Preference preference) { 73 updateIntent(); 74 75 if (mIntent != null) { 76 preference.setIntent(mIntent); 77 } 78 }; 79 80 @Override getAvailabilityStatus()81 protected int getAvailabilityStatus() { 82 if (!mEnabled) return UNSUPPORTED_ON_DEVICE; 83 84 updateIntent(); 85 86 return mIntent == null ? DISABLED_FOR_PROFILE : AVAILABLE; 87 } 88 updateIntent()89 private void updateIntent() { 90 mIntent = null; 91 92 ComponentName admin = mDpm.getProfileOwner(); 93 if (admin == null) { 94 LOG.d("no profile owner for user " + MY_USER_ID + ")"); 95 return; 96 } 97 98 mIntent = new Intent(Settings.ACTION_SHOW_WORK_POLICY_INFO) 99 .setPackage(admin.getPackageName()) 100 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 101 102 List<ResolveInfo> activities = mPm.queryIntentActivities(mIntent, /* flags= */ 0); 103 if (activities.isEmpty()) { 104 LOG.d(admin.flattenToShortString() + " does not declare " 105 + Settings.ACTION_SHOW_WORK_POLICY_INFO); 106 mIntent = null; 107 return; 108 } 109 110 LOG.d("updateIntent(): " + admin.flattenToShortString()); 111 } 112 } 113