Merge "Add a toggle for enabling/disabling sensitive lockscreen notifications for Private Space" into main

This commit is contained in:
Olivier Nshimiye
2024-02-22 14:22:33 +00:00
committed by Android (Google) Code Review
6 changed files with 329 additions and 5 deletions

View File

@@ -0,0 +1,105 @@
/*
* Copyright (C) 2024 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.settings.privatespace;
import static android.provider.Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS;
import android.content.Context;
import android.os.UserHandle;
import android.provider.Settings;
import androidx.annotation.NonNull;
import com.android.settings.core.TogglePreferenceController;
import java.util.Objects;
/**
* A controller object for sensitive notifications in Private Space settings page.
*/
public class HidePrivateSpaceSensitiveNotificationsController extends TogglePreferenceController {
private final PrivateSpaceMaintainer mPrivateSpaceMaintainer;
private final UserHandle mPrivateProfileId;
public static final int ENABLED = 1;
public static final int DISABLED = 0;
private static final int DEVICE_SENSITIVE_NOTIFICATIONS_DEFAULT = ENABLED;
private static final int DEVICE_LOCK_SCREEN_NOTIFICATIONS_DEFAULT = ENABLED;
private static final int PRIVATE_SPACE_SENSITIVE_NOTIFICATIONS_DEFAULT = DISABLED;
public HidePrivateSpaceSensitiveNotificationsController(@NonNull Context context,
@NonNull String preferenceKey) {
super(context, preferenceKey);
mPrivateSpaceMaintainer = PrivateSpaceMaintainer.getInstance(context);
mPrivateProfileId = Objects.requireNonNull(
mPrivateSpaceMaintainer.getPrivateProfileHandle());
}
@Override
public int getAvailabilityStatus() {
if (!android.os.Flags.allowPrivateProfile()
|| !android.multiuser.Flags.enablePsSensitiveNotificationsToggle()
|| !mPrivateSpaceMaintainer.doesPrivateSpaceExist()) {
return UNSUPPORTED_ON_DEVICE;
}
if (!getLockscreenNotificationsEnabled(mContext)
|| !getLockscreenSensitiveNotificationsEnabledOnDevice(mContext)) {
return DISABLED_DEPENDENT_SETTING;
}
return AVAILABLE;
}
@Override
public boolean isChecked() {
return Settings.Secure.getIntForUser(mContext.getContentResolver(),
LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS,
PRIVATE_SPACE_SENSITIVE_NOTIFICATIONS_DEFAULT, mPrivateProfileId.getIdentifier())
!= DISABLED;
}
@Override
public boolean setChecked(boolean isChecked) {
Settings.Secure.putIntForUser(mContext.getContentResolver(),
Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS,
isChecked ? ENABLED : DISABLED, mPrivateProfileId.getIdentifier());
return true;
}
@Override
public int getSliceHighlightMenuRes() {
return 0;
}
/**
* If notifications are disabled on the device, the toggle for private space sensitive
* notifications should be unavailable.
*/
private static boolean getLockscreenNotificationsEnabled(Context context) {
return Settings.Secure.getInt(context.getContentResolver(),
Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS,
DEVICE_LOCK_SCREEN_NOTIFICATIONS_DEFAULT) != DISABLED;
}
/**
* If sensitive notifications are hidden on the device, they should be hidden for private space
* also.
*/
private static boolean getLockscreenSensitiveNotificationsEnabledOnDevice(Context context) {
return Settings.Secure.getInt(context.getContentResolver(),
Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS,
DEVICE_SENSITIVE_NOTIFICATIONS_DEFAULT) != DISABLED;
}
}

View File

@@ -45,6 +45,7 @@ import com.android.internal.annotations.GuardedBy;
import java.util.List;
// TODO(b/293569406): Update the javadoc when we have the setup flow in place to create PS
/** A class to help with the creation / deletion of Private Space */
public class PrivateSpaceMaintainer {
private static final String TAG = "PrivateSpaceMaintainer";
@@ -65,9 +66,9 @@ public class PrivateSpaceMaintainer {
public static final int PRIVATE_SPACE_AUTO_LOCK_DEFAULT_VAL = PRIVATE_SPACE_AUTO_LOCK_NEVER;
public enum ErrorDeletingPrivateSpace {
DELETE_PS_ERROR_NONE,
DELETE_PS_ERROR_NO_PRIVATE_SPACE,
DELETE_PS_ERROR_INTERNAL
DELETE_PS_ERROR_NONE,
DELETE_PS_ERROR_NO_PRIVATE_SPACE,
DELETE_PS_ERROR_INTERNAL
}
/**
@@ -90,7 +91,7 @@ public class PrivateSpaceMaintainer {
if (mUserHandle == null) {
try {
mUserHandle = mUserManager.createProfile(
userName, USER_TYPE_PROFILE_PRIVATE, new ArraySet<>());
userName, USER_TYPE_PROFILE_PRIVATE, new ArraySet<>());
} catch (Exception e) {
Log.e(TAG, "Error creating private space", e);
return false;
@@ -117,7 +118,8 @@ public class PrivateSpaceMaintainer {
return true;
}
/** Returns the {@link ErrorDeletingPrivateSpace} enum representing the result of operation.
/**
* Returns the {@link ErrorDeletingPrivateSpace} enum representing the result of operation.
*
* <p> This method should be used ONLY by the delete-PS controller in the PS Settings page.
*/
@@ -212,6 +214,7 @@ public class PrivateSpaceMaintainer {
// TODO(b/307281644): Remove this method once new auth change is merged
/**
* Returns true if private space exists and a separate private profile lock is set
* otherwise false when the private space does not exit or exists but does not have a
@@ -290,9 +293,20 @@ public class PrivateSpaceMaintainer {
return false;
}
@GuardedBy("this")
private void resetPrivateSpaceSettings() {
setHidePrivateSpaceEntryPointSetting(HIDE_PRIVATE_SPACE_ENTRY_POINT_DISABLED_VAL);
setPrivateSpaceAutoLockSetting(PRIVATE_SPACE_AUTO_LOCK_DEFAULT_VAL);
setPrivateSpaceSensitiveNotificationsDefaultValue();
}
/** Sets private space sensitive notifications hidden on lockscreen by default */
@GuardedBy("this")
private void setPrivateSpaceSensitiveNotificationsDefaultValue() {
Settings.Secure.putIntForUser(mContext.getContentResolver(),
Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS,
HidePrivateSpaceSensitiveNotificationsController.DISABLED,
mUserHandle.getIdentifier());
}
/**