Merge "Add util methods for WorkPolicy in SettingsLib" into tm-dev
This commit is contained in:
@@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user