fix: 首次提交
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright (C) 2021 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.os.Build;
|
||||
|
||||
import androidx.annotation.ChecksSdkIntAtLeast;
|
||||
|
||||
/**
|
||||
* An util class to check whether the current OS version is higher or equal to sdk version of
|
||||
* device.
|
||||
*/
|
||||
public final class BuildCompatUtils {
|
||||
|
||||
/**
|
||||
* Implementation of BuildCompat.isAtLeastS() suitable for use in Settings
|
||||
*
|
||||
* @return Whether the current OS version is higher or equal to S.
|
||||
*/
|
||||
@ChecksSdkIntAtLeast(api = Build.VERSION_CODES.S)
|
||||
public static boolean isAtLeastS() {
|
||||
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.S;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of BuildCompat.isAtLeastS() suitable for use in Settings
|
||||
*
|
||||
* @return Whether the current OS version is higher or equal to Sv2.
|
||||
*/
|
||||
@ChecksSdkIntAtLeast(api = Build.VERSION_CODES.S_V2)
|
||||
public static boolean isAtLeastSV2() {
|
||||
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.S_V2;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of BuildCompat.isAtLeastT() suitable for use in Settings
|
||||
*
|
||||
* @return Whether the current OS version is higher or equal to T.
|
||||
*/
|
||||
@ChecksSdkIntAtLeast(api = Build.VERSION_CODES.TIRAMISU)
|
||||
public static boolean isAtLeastT() {
|
||||
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU;
|
||||
}
|
||||
|
||||
private BuildCompatUtils() {}
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
/*
|
||||
* 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 androidx.annotation.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* Utility class for find out when to show WorkPolicyInfo
|
||||
*/
|
||||
public class WorkPolicyUtils {
|
||||
|
||||
private final Context mContext;
|
||||
private final PackageManager mPackageManager;
|
||||
private final UserManager mUserManager;
|
||||
private final DevicePolicyManager mDevicePolicyManager;
|
||||
|
||||
private static final int USER_NULL = -10000;
|
||||
|
||||
public WorkPolicyUtils(
|
||||
Context context
|
||||
) {
|
||||
mContext = context;
|
||||
mPackageManager = context.getPackageManager();
|
||||
mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
|
||||
mDevicePolicyManager =
|
||||
(DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the work policy info intent if the device owner component exists,
|
||||
* and returns {@code null} otherwise
|
||||
*/
|
||||
@Nullable
|
||||
public 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;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private ComponentName getManagedProfileOwnerComponent(int managedUserId) {
|
||||
if (managedUserId == USER_NULL) {
|
||||
return null;
|
||||
}
|
||||
Context managedProfileContext;
|
||||
try {
|
||||
managedProfileContext =
|
||||
mContext.createPackageContextAsUser(
|
||||
mContext.getPackageName(), 0, UserHandle.of(managedUserId)
|
||||
);
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
DevicePolicyManager managedProfileDevicePolicyManager =
|
||||
(DevicePolicyManager)
|
||||
managedProfileContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
|
||||
ComponentName ownerComponent = managedProfileDevicePolicyManager.getProfileOwner();
|
||||
return ownerComponent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the work policy info intent if the profile owner component exists,
|
||||
* and returns {@code null} otherwise
|
||||
*/
|
||||
@Nullable
|
||||
public Intent getWorkPolicyInfoIntentPO() {
|
||||
final int managedUserId = getManagedProfileUserId();
|
||||
ComponentName ownerComponent = getManagedProfileOwnerComponent(managedUserId);
|
||||
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;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private ComponentName getDeviceOwnerComponent() {
|
||||
if (!mPackageManager.hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN)) {
|
||||
return null;
|
||||
}
|
||||
return mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the user id of the managed profile, and returns {@code USER_NULL} otherwise
|
||||
*/
|
||||
public int getManagedProfileUserId() {
|
||||
List<UserHandle> allProfiles = mUserManager.getAllProfiles();
|
||||
for (UserHandle uh : allProfiles) {
|
||||
int id = uh.getIdentifier();
|
||||
if (mUserManager.isManagedProfile(id)) {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
return USER_NULL;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright (C) 2020 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.applications;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Build;
|
||||
import android.os.UserManager;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.RequiresApi;
|
||||
|
||||
import com.android.settingslib.utils.R;
|
||||
|
||||
public class AppUtils {
|
||||
|
||||
private static final String TAG = AppUtils.class.getSimpleName();
|
||||
|
||||
/** Returns the label for a given package. */
|
||||
public static CharSequence getApplicationLabel(
|
||||
PackageManager packageManager, String packageName) {
|
||||
try {
|
||||
final ApplicationInfo appInfo =
|
||||
packageManager.getApplicationInfo(
|
||||
packageName,
|
||||
PackageManager.MATCH_DISABLED_COMPONENTS
|
||||
| PackageManager.MATCH_ANY_USER);
|
||||
return appInfo.loadLabel(packageManager);
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
Log.w(TAG, "Unable to find info for package: " + packageName);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a content description of an app name which distinguishes a personal app from a
|
||||
* work app for accessibility purpose.
|
||||
* If the app is in a work profile, then add a "work" prefix to the app name.
|
||||
*/
|
||||
@RequiresApi(Build.VERSION_CODES.M)
|
||||
public static String getAppContentDescription(Context context, String packageName,
|
||||
int userId) {
|
||||
final CharSequence appLabel = getApplicationLabel(context.getPackageManager(), packageName);
|
||||
if (appLabel == null) {
|
||||
return "";
|
||||
}
|
||||
return context.getSystemService(UserManager.class).isManagedProfile(userId)
|
||||
? context.getString(R.string.accessibility_work_profile_app_description, appLabel)
|
||||
: appLabel.toString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user