Merge "Migrate to using fixed permissions"

This commit is contained in:
Julia Reynolds
2021-11-02 20:47:58 +00:00
committed by Android (Google) Code Review
21 changed files with 272 additions and 211 deletions

View File

@@ -20,6 +20,7 @@ import static android.app.NotificationManager.IMPORTANCE_UNSPECIFIED;
import static android.content.pm.LauncherApps.ShortcutQuery.FLAG_MATCH_CACHED;
import static android.content.pm.LauncherApps.ShortcutQuery.FLAG_MATCH_DYNAMIC;
import static android.content.pm.LauncherApps.ShortcutQuery.FLAG_MATCH_PINNED_BY_ANY_LAUNCHER;
import static android.os.UserHandle.USER_SYSTEM;
import android.app.INotificationManager;
import android.app.NotificationChannel;
@@ -44,6 +45,7 @@ import android.graphics.drawable.Drawable;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.UserHandle;
import android.provider.Settings;
import android.service.notification.ConversationChannelWrapper;
import android.service.notification.NotificationListenerFilter;
import android.text.format.DateUtils;
@@ -112,15 +114,25 @@ public class NotificationBackend {
void recordCanBeBlocked(Context context, PackageManager pm, RoleManager rm, PackageInfo app,
AppRow row) {
row.systemApp = Utils.isSystemPackage(context.getResources(), pm, app);
List<String> roles = rm.getHeldRolesFromController(app.packageName);
if (roles.contains(RoleManager.ROLE_DIALER)
|| roles.contains(RoleManager.ROLE_EMERGENCY)) {
row.systemApp = true;
if (Settings.Secure.getIntForUser(context.getContentResolver(),
Settings.Secure.NOTIFICATION_PERMISSION_ENABLED, 0, USER_SYSTEM) != 0) {
try {
row.systemApp = row.lockedImportance =
sINM.isPermissionFixed(app.packageName, row.userId);
} catch (RemoteException e) {
Log.w(TAG, "Error calling NMS", e);
}
} else {
row.systemApp = Utils.isSystemPackage(context.getResources(), pm, app);
List<String> roles = rm.getHeldRolesFromController(app.packageName);
if (roles.contains(RoleManager.ROLE_DIALER)
|| roles.contains(RoleManager.ROLE_EMERGENCY)) {
row.systemApp = true;
}
final String[] nonBlockablePkgs = context.getResources().getStringArray(
com.android.internal.R.array.config_nonBlockableNotificationPackages);
markAppRowWithBlockables(nonBlockablePkgs, row, app.packageName);
}
final String[] nonBlockablePkgs = context.getResources().getStringArray(
com.android.internal.R.array.config_nonBlockableNotificationPackages);
markAppRowWithBlockables(nonBlockablePkgs, row, app.packageName);
}
@VisibleForTesting static void markAppRowWithBlockables(String[] nonBlockablePkgs, AppRow row,
@@ -653,6 +665,11 @@ public class NotificationBackend {
return false;
}
@VisibleForTesting
void setNm(INotificationManager inm) {
sINM = inm;
}
/**
* NotificationsSentState contains how often an app sends notifications and how recently it sent
* one.

View File

@@ -80,7 +80,7 @@ public class BlockPreferenceController extends NotificationPreferenceController
}
bar.setDisabledByAdmin(mAdmin);
if (mChannel != null && !isChannelBlockable()) {
if (mChannel != null && (!isChannelBlockable() || !isChannelConfigurable(mChannel))) {
bar.setSwitchBarEnabled(false);
}
@@ -88,8 +88,7 @@ public class BlockPreferenceController extends NotificationPreferenceController
bar.setSwitchBarEnabled(false);
}
if (mChannel == null && mAppRow.systemApp
&& (!mAppRow.banned || mAppRow.lockedImportance)) {
if (mChannel == null && !isAppBlockable()) {
bar.setSwitchBarEnabled(false);
}

View File

@@ -62,9 +62,9 @@ public class ConversationPriorityPreferenceController extends NotificationPrefer
public void updateState(Preference preference) {
if (mAppRow != null) {
preference.setEnabled(mAdmin == null && !mChannel.isImportanceLockedByOEM());
preference.setEnabled(mAdmin == null && isChannelConfigurable(mChannel));
ConversationPriorityPreference pref = (ConversationPriorityPreference) preference;
pref.setConfigurable(!mChannel.isImportanceLockedByOEM());
pref.setConfigurable(isChannelConfigurable(mChannel));
pref.setImportance(mChannel.getImportance());
pref.setOriginalImportance(mChannel.getOriginalImportance());
pref.setPriorityConversation(mChannel.isImportantConversation());

View File

@@ -68,7 +68,7 @@ public class HighImportancePreferenceController extends NotificationPreferenceCo
@Override
public void updateState(Preference preference) {
if (mAppRow != null && mChannel != null) {
preference.setEnabled(mAdmin == null && !mChannel.isImportanceLockedByOEM());
preference.setEnabled(mAdmin == null && isChannelConfigurable(mChannel));
RestrictedSwitchPreference pref = (RestrictedSwitchPreference) preference;
pref.setChecked(mChannel.getImportance() >= IMPORTANCE_HIGH);

View File

@@ -66,9 +66,9 @@ public class ImportancePreferenceController extends NotificationPreferenceContro
@Override
public void updateState(Preference preference) {
if (mAppRow!= null && mChannel != null) {
preference.setEnabled(mAdmin == null && !mChannel.isImportanceLockedByOEM());
preference.setEnabled(mAdmin == null && isChannelConfigurable(mChannel));
ImportancePreference pref = (ImportancePreference) preference;
pref.setConfigurable(!mChannel.isImportanceLockedByOEM());
pref.setConfigurable(isChannelConfigurable(mChannel));
pref.setImportance(mChannel.getImportance());
pref.setDisplayInStatusBar(mBackend.showSilentInStatusBar(mContext.getPackageName()));
pref.setDisplayOnLockscreen(Settings.Secure.getInt(mContext.getContentResolver(),

View File

@@ -68,7 +68,7 @@ public class MinImportancePreferenceController extends NotificationPreferenceCon
@Override
public void updateState(Preference preference) {
if (mAppRow != null && mChannel != null) {
preference.setEnabled(mAdmin == null && !mChannel.isImportanceLockedByOEM());
preference.setEnabled(mAdmin == null && isChannelConfigurable(mChannel));
RestrictedSwitchPreference pref = (RestrictedSwitchPreference) preference;
pref.setChecked(mChannel.getImportance() == IMPORTANCE_MIN);

View File

@@ -17,6 +17,7 @@
package com.android.settings.notification.app;
import static android.app.NotificationManager.IMPORTANCE_NONE;
import static android.os.UserHandle.USER_SYSTEM;
import android.annotation.Nullable;
import android.app.NotificationChannel;
@@ -27,6 +28,7 @@ import android.content.pm.PackageManager;
import android.content.pm.ShortcutInfo;
import android.graphics.drawable.Drawable;
import android.os.UserManager;
import android.provider.Settings;
import android.util.Log;
import androidx.preference.Preference;
@@ -62,6 +64,12 @@ public abstract class NotificationPreferenceController extends AbstractPreferenc
@Nullable
protected ShortcutInfo mConversationInfo;
protected List<String> mPreferenceFilter;
boolean mMigratedPermission;
boolean overrideCanBlock;
boolean overrideCanConfigure;
boolean overrideCanBlockValue;
boolean overrideCanConfigureValue;
public NotificationPreferenceController(Context context, NotificationBackend backend) {
super(context);
@@ -70,6 +78,8 @@ public abstract class NotificationPreferenceController extends AbstractPreferenc
mBackend = backend;
mUm = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
mPm = mContext.getPackageManager();
mMigratedPermission = Settings.Secure.getIntForUser(context.getContentResolver(),
Settings.Secure.NOTIFICATION_PERMISSION_ENABLED, 0, USER_SYSTEM) != 0;
}
/**
@@ -138,10 +148,18 @@ public abstract class NotificationPreferenceController extends AbstractPreferenc
}
protected boolean isChannelBlockable(NotificationChannel channel) {
if (overrideCanBlock) {
return overrideCanBlockValue;
}
if (overrideCanConfigure) {
return overrideCanConfigureValue;
}
if (channel != null && mAppRow != null) {
if (channel.isImportanceLockedByCriticalDeviceFunction()
|| channel.isImportanceLockedByOEM()) {
return channel.getImportance() == IMPORTANCE_NONE;
boolean locked = mMigratedPermission ? mAppRow.lockedImportance
: channel.isImportanceLockedByCriticalDeviceFunction()
|| channel.isImportanceLockedByOEM();
if (locked) {
return channel.isBlockable() || channel.getImportance() == IMPORTANCE_NONE;
}
return channel.isBlockable() || !mAppRow.systemApp
@@ -150,9 +168,27 @@ public abstract class NotificationPreferenceController extends AbstractPreferenc
return false;
}
protected boolean isAppBlockable() {
if (overrideCanBlock) {
return overrideCanBlockValue;
}
if (overrideCanConfigure) {
return overrideCanConfigureValue;
}
if (mAppRow != null) {
return !mAppRow.systemApp && !mAppRow.lockedImportance;
}
return true;
}
protected boolean isChannelConfigurable(NotificationChannel channel) {
if (overrideCanConfigure) {
return overrideCanConfigureValue;
}
if (channel != null && mAppRow != null) {
return !channel.isImportanceLockedByOEM();
boolean locked = mMigratedPermission ? mAppRow.lockedImportance
: channel.isImportanceLockedByOEM();
return !locked || channel.isBlockable();
}
return false;
}
@@ -162,8 +198,14 @@ public abstract class NotificationPreferenceController extends AbstractPreferenc
}
protected boolean isChannelGroupBlockable(NotificationChannelGroup group) {
if (overrideCanBlock) {
return overrideCanBlockValue;
}
if (overrideCanConfigure) {
return overrideCanConfigureValue;
}
if (group != null && mAppRow != null) {
if (!mAppRow.systemApp) {
if (!mAppRow.systemApp && !mAppRow.lockedImportance) {
return true;
}
@@ -183,6 +225,16 @@ public abstract class NotificationPreferenceController extends AbstractPreferenc
return Objects.equals(NotificationChannel.DEFAULT_CHANNEL_ID, mChannel.getId());
}
protected final void setOverrideCanBlock(boolean canBlock) {
overrideCanBlock = true;
overrideCanBlockValue = canBlock;
}
protected final void setOverrideCanConfigure(boolean canConfigure) {
overrideCanConfigure = true;
overrideCanConfigureValue = canConfigure;
}
public static final Comparator<NotificationChannelGroup> CHANNEL_GROUP_COMPARATOR =
new Comparator<NotificationChannelGroup>() {
@Override