diff options
| author | 2022-04-22 09:09:24 +0100 | |
|---|---|---|
| committer | 2022-04-23 20:13:15 +0100 | |
| commit | d9d8a96821f0d85f37e4738221ccc9b7c757a7f5 (patch) | |
| tree | 7571497a9160bdb8f2d79028b4c5d308fac79e27 | |
| parent | a756b84369e05ad99749e96c5a73b1fd6f82b68f (diff) | |
Add util methods for WorkPolicy in SettingsLib
Moving logic for WorkPolicyInfo to SettingLib so that they can be
accessed from PermissionController
Bug: b/229889270
Test: manual
Change-Id: I282a4e05791a8d738795999ca5c3ab68e0cd0985
| -rw-r--r-- | packages/SettingsLib/Utils/src/com/android/settingslib/utils/WorkPolicyUtils.java | 159 |
1 files changed, 159 insertions, 0 deletions
diff --git a/packages/SettingsLib/Utils/src/com/android/settingslib/utils/WorkPolicyUtils.java b/packages/SettingsLib/Utils/src/com/android/settingslib/utils/WorkPolicyUtils.java new file mode 100644 index 000000000000..b038d59e9fe4 --- /dev/null +++ b/packages/SettingsLib/Utils/src/com/android/settingslib/utils/WorkPolicyUtils.java @@ -0,0 +1,159 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settingslib.utils; + + +import android.app.admin.DevicePolicyManager; +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; +import android.os.UserHandle; +import android.os.UserManager; +import android.provider.Settings; + +import java.util.List; + +/** + * Utility class for find out when to show WorkPolicyInfo + */ +public class WorkPolicyUtils { + + Context mContext; + PackageManager mPackageManager; + UserManager mUserManager; + DevicePolicyManager mDevicePolicyManager; + + private static final int USER_NULL = -10000; + + public WorkPolicyUtils( + Context applicationContext, + PackageManager mPm, + UserManager mUm, + DevicePolicyManager mDpm + ) { + mContext = applicationContext; + mPackageManager = mPm; + mUserManager = mUm; + mDevicePolicyManager = mDpm; + } + + /** + * Returns {@code true} if it is possilbe to resolve an Intent to launch the "Your work policy + * info" page provided by the active Device Owner or Profile Owner app if it exists, {@code + * false} otherwise. + */ + public boolean hasWorkPolicy() { + return getWorkPolicyInfoIntentDO() != null || getWorkPolicyInfoIntentPO() != null; + } + + /** + * Launches the Device Owner or Profile Owner's activity that displays the "Your work policy + * info" page. Returns {@code true} if the activity has indeed been launched. + */ + public boolean showWorkPolicyInfo(Context activityContext) { + Intent intent = getWorkPolicyInfoIntentDO(); + if (intent != null) { + activityContext.startActivity(intent); + return true; + } + + intent = getWorkPolicyInfoIntentPO(); + final int userId = getManagedProfileUserId(); + if (intent != null && userId != USER_NULL) { + activityContext.startActivityAsUser(intent, UserHandle.of(userId)); + return true; + } + + return false; + } + + private Intent getWorkPolicyInfoIntentDO() { + final ComponentName ownerComponent = getDeviceOwnerComponent(); + if (ownerComponent == null) { + return null; + } + + // Only search for the required action in the Device Owner's package + final Intent intent = + new Intent(Settings.ACTION_SHOW_WORK_POLICY_INFO) + .setPackage(ownerComponent.getPackageName()); + final List<ResolveInfo> activities = mPackageManager.queryIntentActivities(intent, 0); + if (activities.size() != 0) { + return intent; + } + + return null; + } + + private Intent getWorkPolicyInfoIntentPO() { + try { + final int managedUserId = getManagedProfileUserId(); + if (managedUserId == USER_NULL) { + return null; + } + + Context managedProfileContext = + mContext.createPackageContextAsUser( + mContext.getPackageName(), 0, UserHandle.of(managedUserId) + ); + + DevicePolicyManager managedProfileDevicePolicyManager = + (DevicePolicyManager) + managedProfileContext.getSystemService(Context.DEVICE_POLICY_SERVICE); + ComponentName ownerComponent = managedProfileDevicePolicyManager.getProfileOwner(); + if (ownerComponent == null) { + return null; + } + + // Only search for the required action in the Profile Owner's package + final Intent intent = + new Intent(Settings.ACTION_SHOW_WORK_POLICY_INFO) + .setPackage(ownerComponent.getPackageName()); + final List<ResolveInfo> activities = + mPackageManager.queryIntentActivitiesAsUser( + intent, 0, UserHandle.of(managedUserId)); + if (activities.size() != 0) { + return intent; + } + + return null; + } catch (PackageManager.NameNotFoundException e) { + return null; + } + } + + private ComponentName getDeviceOwnerComponent() { + if (!mPackageManager.hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN)) { + return null; + } + return mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser(); + } + + private int getManagedProfileUserId() { + List<UserHandle> allProfiles = mUserManager.getAllProfiles(); + for (UserHandle uh : allProfiles) { + int id = uh.getIdentifier(); + if (mUserManager.isManagedProfile(id)) { + return id; + } + } + return USER_NULL; + } + +} |