Apply app ops restriction and filter to restricted switch preference
Bug: 202130031 Test: Able to boot without error Test: atest InstallSourceInfoTest Change-Id: I920e155a3888440eeece0c013ba35459103467a6
This commit is contained in:
@@ -23,7 +23,6 @@ import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
|
||||
import android.app.admin.DevicePolicyManager;
|
||||
import android.content.Context;
|
||||
import android.content.res.TypedArray;
|
||||
import android.os.Build;
|
||||
import android.os.UserHandle;
|
||||
import android.text.TextUtils;
|
||||
import android.util.AttributeSet;
|
||||
@@ -33,8 +32,6 @@ import android.widget.TextView;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceViewHolder;
|
||||
|
||||
import com.android.internal.util.Preconditions;
|
||||
|
||||
/**
|
||||
* Helper class for managing settings preferences that can be disabled
|
||||
* by device admins via user restrictions.
|
||||
@@ -42,8 +39,8 @@ import com.android.internal.util.Preconditions;
|
||||
public class RestrictedPreferenceHelper {
|
||||
private final Context mContext;
|
||||
private final Preference mPreference;
|
||||
final String packageName;
|
||||
final int uid;
|
||||
String packageName;
|
||||
int uid;
|
||||
|
||||
private boolean mDisabledByAdmin;
|
||||
private EnforcedAdmin mEnforcedAdmin;
|
||||
@@ -219,6 +216,11 @@ public class RestrictedPreferenceHelper {
|
||||
return mDisabledByAppOps;
|
||||
}
|
||||
|
||||
public void updatePackageDetails(String packageName, int uid) {
|
||||
this.packageName = packageName;
|
||||
this.uid = uid;
|
||||
}
|
||||
|
||||
private void updateDisabledState() {
|
||||
if (!(mPreference instanceof RestrictedTopLevelPreference)) {
|
||||
mPreference.setEnabled(!(mDisabledByAdmin || mDisabledByAppOps));
|
||||
|
||||
@@ -18,8 +18,11 @@ package com.android.settingslib;
|
||||
|
||||
import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
|
||||
|
||||
import android.annotation.NonNull;
|
||||
import android.app.AppOpsManager;
|
||||
import android.content.Context;
|
||||
import android.content.res.TypedArray;
|
||||
import android.os.Process;
|
||||
import android.os.UserHandle;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.TypedValue;
|
||||
@@ -28,6 +31,7 @@ import android.widget.ImageView;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.core.content.res.TypedArrayUtils;
|
||||
import androidx.preference.PreferenceManager;
|
||||
import androidx.preference.PreferenceViewHolder;
|
||||
@@ -39,6 +43,7 @@ import androidx.preference.SwitchPreference;
|
||||
*/
|
||||
public class RestrictedSwitchPreference extends SwitchPreference {
|
||||
RestrictedPreferenceHelper mHelper;
|
||||
AppOpsManager mAppOpsManager;
|
||||
boolean mUseAdditionalSummary = false;
|
||||
CharSequence mRestrictedSwitchSummary;
|
||||
private int mIconSize;
|
||||
@@ -90,6 +95,11 @@ public class RestrictedSwitchPreference extends SwitchPreference {
|
||||
this(context, null);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public void setAppOps(AppOpsManager appOps) {
|
||||
mAppOpsManager = appOps;
|
||||
}
|
||||
|
||||
public void setIconSize(int iconSize) {
|
||||
mIconSize = iconSize;
|
||||
}
|
||||
@@ -97,6 +107,12 @@ public class RestrictedSwitchPreference extends SwitchPreference {
|
||||
@Override
|
||||
public void onBindViewHolder(PreferenceViewHolder holder) {
|
||||
super.onBindViewHolder(holder);
|
||||
final View switchView = holder.findViewById(android.R.id.switch_widget);
|
||||
if (switchView != null) {
|
||||
final View rootView = switchView.getRootView();
|
||||
rootView.setFilterTouchesWhenObscured(true);
|
||||
}
|
||||
|
||||
mHelper.onBindViewHolder(holder);
|
||||
|
||||
CharSequence switchSummary;
|
||||
@@ -164,11 +180,18 @@ public class RestrictedSwitchPreference extends SwitchPreference {
|
||||
|
||||
@Override
|
||||
public void setEnabled(boolean enabled) {
|
||||
boolean changed = false;
|
||||
if (enabled && isDisabledByAdmin()) {
|
||||
mHelper.setDisabledByAdmin(null);
|
||||
return;
|
||||
changed = true;
|
||||
}
|
||||
if (enabled && isDisabledByAppOps()) {
|
||||
mHelper.setDisabledByAppOps(false);
|
||||
changed = true;
|
||||
}
|
||||
if (!changed) {
|
||||
super.setEnabled(enabled);
|
||||
}
|
||||
super.setEnabled(enabled);
|
||||
}
|
||||
|
||||
public void setDisabledByAdmin(EnforcedAdmin admin) {
|
||||
@@ -180,4 +203,38 @@ public class RestrictedSwitchPreference extends SwitchPreference {
|
||||
public boolean isDisabledByAdmin() {
|
||||
return mHelper.isDisabledByAdmin();
|
||||
}
|
||||
|
||||
private void setDisabledByAppOps(boolean disabled) {
|
||||
if (mHelper.setDisabledByAppOps(disabled)) {
|
||||
notifyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isDisabledByAppOps() {
|
||||
return mHelper.isDisabledByAppOps();
|
||||
}
|
||||
|
||||
public int getUid() {
|
||||
return mHelper != null ? mHelper.uid : Process.INVALID_UID;
|
||||
}
|
||||
|
||||
public String getPackageName() {
|
||||
return mHelper != null ? mHelper.packageName : null;
|
||||
}
|
||||
|
||||
public void updateState(@NonNull String packageName, int uid, boolean isEnabled) {
|
||||
mHelper.updatePackageDetails(packageName, uid);
|
||||
if (mAppOpsManager == null) {
|
||||
mAppOpsManager = getContext().getSystemService(AppOpsManager.class);
|
||||
}
|
||||
final int mode = mAppOpsManager.noteOpNoThrow(
|
||||
AppOpsManager.OP_ACCESS_RESTRICTED_SETTINGS,
|
||||
uid, packageName);
|
||||
final boolean appOpsAllowed = mode == AppOpsManager.MODE_ALLOWED;
|
||||
if (appOpsAllowed || isEnabled) {
|
||||
setEnabled(true);
|
||||
} else {
|
||||
setDisabledByAppOps(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user