diff options
238 files changed, 8579 insertions, 1597 deletions
diff --git a/core/api/current.txt b/core/api/current.txt index 8eb881139b34..836d4e99ce45 100644 --- a/core/api/current.txt +++ b/core/api/current.txt @@ -6854,6 +6854,47 @@ package android.app { method public android.app.Notification.MessagingStyle.Message setData(String, android.net.Uri); } + @FlaggedApi("android.app.api_rich_ongoing") public static class Notification.ProgressStyle extends android.app.Notification.Style { + ctor public Notification.ProgressStyle(); + method @NonNull public android.app.Notification.ProgressStyle addProgressSegment(@NonNull android.app.Notification.ProgressStyle.Segment); + method @NonNull public android.app.Notification.ProgressStyle addProgressStep(@NonNull android.app.Notification.ProgressStyle.Step); + method public int getProgress(); + method @Nullable public android.graphics.drawable.Icon getProgressEndIcon(); + method public int getProgressMax(); + method @NonNull public java.util.List<android.app.Notification.ProgressStyle.Segment> getProgressSegments(); + method @Nullable public android.graphics.drawable.Icon getProgressStartIcon(); + method @NonNull public java.util.List<android.app.Notification.ProgressStyle.Step> getProgressSteps(); + method @Nullable public android.graphics.drawable.Icon getProgressTrackerIcon(); + method public boolean isProgressIndeterminate(); + method public boolean isStyledByProgress(); + method @NonNull public android.app.Notification.ProgressStyle setProgress(int); + method @NonNull public android.app.Notification.ProgressStyle setProgressEndIcon(@Nullable android.graphics.drawable.Icon); + method @NonNull public android.app.Notification.ProgressStyle setProgressIndeterminate(boolean); + method @NonNull public android.app.Notification.ProgressStyle setProgressSegments(@NonNull java.util.List<android.app.Notification.ProgressStyle.Segment>); + method @NonNull public android.app.Notification.ProgressStyle setProgressStartIcon(@Nullable android.graphics.drawable.Icon); + method @NonNull public android.app.Notification.ProgressStyle setProgressSteps(@NonNull java.util.List<android.app.Notification.ProgressStyle.Step>); + method @NonNull public android.app.Notification.ProgressStyle setProgressTrackerIcon(@Nullable android.graphics.drawable.Icon); + method @NonNull public android.app.Notification.ProgressStyle setStyledByProgress(boolean); + } + + public static final class Notification.ProgressStyle.Segment { + ctor public Notification.ProgressStyle.Segment(int); + method @ColorInt public int getColor(); + method public int getLength(); + method public int getStableId(); + method @NonNull public android.app.Notification.ProgressStyle.Segment setColor(@ColorInt int); + method @NonNull public android.app.Notification.ProgressStyle.Segment setStableId(int); + } + + public static final class Notification.ProgressStyle.Step { + ctor public Notification.ProgressStyle.Step(int); + method @ColorInt public int getColor(); + method public int getPosition(); + method public int getStableId(); + method @NonNull public android.app.Notification.ProgressStyle.Step setColor(@ColorInt int); + method @NonNull public android.app.Notification.ProgressStyle.Step setStableId(int); + } + public abstract static class Notification.Style { ctor @Deprecated public Notification.Style(); method public android.app.Notification build(); @@ -8732,13 +8773,15 @@ package android.app.admin { package android.app.appfunctions { @FlaggedApi("android.app.appfunctions.flags.enable_app_function_manager") public final class AppFunctionManager { - method @RequiresPermission(anyOf={"android.permission.EXECUTE_APP_FUNCTIONS_TRUSTED", "android.permission.EXECUTE_APP_FUNCTIONS"}, conditional=true) public void executeAppFunction(@NonNull android.app.appfunctions.ExecuteAppFunctionRequest, @NonNull java.util.concurrent.Executor, @NonNull java.util.function.Consumer<android.app.appfunctions.ExecuteAppFunctionResponse>); + method @Deprecated @RequiresPermission(anyOf={"android.permission.EXECUTE_APP_FUNCTIONS_TRUSTED", "android.permission.EXECUTE_APP_FUNCTIONS"}, conditional=true) public void executeAppFunction(@NonNull android.app.appfunctions.ExecuteAppFunctionRequest, @NonNull java.util.concurrent.Executor, @NonNull java.util.function.Consumer<android.app.appfunctions.ExecuteAppFunctionResponse>); + method @RequiresPermission(anyOf={"android.permission.EXECUTE_APP_FUNCTIONS_TRUSTED", "android.permission.EXECUTE_APP_FUNCTIONS"}, conditional=true) public void executeAppFunction(@NonNull android.app.appfunctions.ExecuteAppFunctionRequest, @NonNull java.util.concurrent.Executor, @NonNull android.os.CancellationSignal, @NonNull java.util.function.Consumer<android.app.appfunctions.ExecuteAppFunctionResponse>); } @FlaggedApi("android.app.appfunctions.flags.enable_app_function_manager") public abstract class AppFunctionService extends android.app.Service { ctor public AppFunctionService(); method @NonNull public final android.os.IBinder onBind(@Nullable android.content.Intent); - method @MainThread public abstract void onExecuteFunction(@NonNull android.app.appfunctions.ExecuteAppFunctionRequest, @NonNull java.util.function.Consumer<android.app.appfunctions.ExecuteAppFunctionResponse>); + method @Deprecated @MainThread public abstract void onExecuteFunction(@NonNull android.app.appfunctions.ExecuteAppFunctionRequest, @NonNull java.util.function.Consumer<android.app.appfunctions.ExecuteAppFunctionResponse>); + method @MainThread public void onExecuteFunction(@NonNull android.app.appfunctions.ExecuteAppFunctionRequest, @NonNull android.os.CancellationSignal, @NonNull java.util.function.Consumer<android.app.appfunctions.ExecuteAppFunctionResponse>); field @NonNull public static final String SERVICE_INTERFACE = "android.app.appfunctions.AppFunctionService"; } diff --git a/core/api/test-current.txt b/core/api/test-current.txt index 6511c214ee52..0a10920154b8 100644 --- a/core/api/test-current.txt +++ b/core/api/test-current.txt @@ -1614,15 +1614,15 @@ package android.hardware.camera2 { public final class CameraManager { method @NonNull public android.hardware.camera2.CameraCharacteristics getCameraCharacteristics(@NonNull String, boolean) throws android.hardware.camera2.CameraAccessException; method public String[] getCameraIdListNoLazy() throws android.hardware.camera2.CameraAccessException; - method @FlaggedApi("com.android.window.flags.camera_compat_for_freeform") public static int getRotationOverrideInternal(@Nullable android.content.Context, @Nullable android.content.pm.PackageManager, @Nullable String); + method @FlaggedApi("com.android.window.flags.enable_camera_compat_for_desktop_windowing") public static int getRotationOverrideInternal(@Nullable android.content.Context, @Nullable android.content.pm.PackageManager, @Nullable String); method @RequiresPermission(android.Manifest.permission.CAMERA) public void openCamera(@NonNull String, boolean, @Nullable android.os.Handler, @NonNull android.hardware.camera2.CameraDevice.StateCallback) throws android.hardware.camera2.CameraAccessException; method @RequiresPermission(allOf={android.Manifest.permission.SYSTEM_CAMERA, android.Manifest.permission.CAMERA}) public void openCamera(@NonNull String, int, @NonNull java.util.concurrent.Executor, @NonNull android.hardware.camera2.CameraDevice.StateCallback) throws android.hardware.camera2.CameraAccessException; method public static boolean shouldOverrideToPortrait(@Nullable android.content.pm.PackageManager, @Nullable String); field public static final String LANDSCAPE_TO_PORTRAIT_PROP = "camera.enable_landscape_to_portrait"; field public static final long OVERRIDE_CAMERA_LANDSCAPE_TO_PORTRAIT = 250678880L; // 0xef10e60L - field @FlaggedApi("com.android.window.flags.camera_compat_for_freeform") public static final int ROTATION_OVERRIDE_NONE = 0; // 0x0 - field @FlaggedApi("com.android.window.flags.camera_compat_for_freeform") public static final int ROTATION_OVERRIDE_OVERRIDE_TO_PORTRAIT = 1; // 0x1 - field @FlaggedApi("com.android.window.flags.camera_compat_for_freeform") public static final int ROTATION_OVERRIDE_ROTATION_ONLY = 2; // 0x2 + field @FlaggedApi("com.android.window.flags.enable_camera_compat_for_desktop_windowing") public static final int ROTATION_OVERRIDE_NONE = 0; // 0x0 + field @FlaggedApi("com.android.window.flags.enable_camera_compat_for_desktop_windowing") public static final int ROTATION_OVERRIDE_OVERRIDE_TO_PORTRAIT = 1; // 0x1 + field @FlaggedApi("com.android.window.flags.enable_camera_compat_for_desktop_windowing") public static final int ROTATION_OVERRIDE_ROTATION_ONLY = 2; // 0x2 } public abstract static class CameraManager.AvailabilityCallback { diff --git a/core/java/Android.bp b/core/java/Android.bp index 99046328b1e2..92bca3cfbef2 100644 --- a/core/java/Android.bp +++ b/core/java/Android.bp @@ -21,52 +21,14 @@ filegroup { "**/*.aidl", ":framework-nfc-non-updatable-sources", ":messagequeue-gen", - ":ranging_stack_mock_initializer", ], // Exactly one MessageQueue.java will be added to srcs by messagequeue-gen exclude_srcs: [ "android/os/*MessageQueue/**/*.java", - "android/ranging/**/*.java", ], visibility: ["//frameworks/base"], } -//Mock to allow service registry for ranging stack. -//TODO(b/331206299): Remove this after RELEASE_RANGING_STACK is ramped up to next. -soong_config_module_type { - name: "ranging_stack_framework_mock_init", - module_type: "genrule", - config_namespace: "bootclasspath", - bool_variables: [ - "release_ranging_stack", - ], - properties: [ - "srcs", - "cmd", - "out", - ], -} - -// The actual RangingFrameworkInitializer is present in packages/modules/Uwb/ranging/framework. -// Mock RangingFrameworkInitializer does nothing and allows to successfully build -// SystemServiceRegistry after registering for system service in SystemServiceRegistry both with -// and without build flag RELEASE_RANGING_STACK enabled. -ranging_stack_framework_mock_init { - name: "ranging_stack_mock_initializer", - soong_config_variables: { - release_ranging_stack: { - cmd: "touch $(out)", - // Adding an empty file as out is mandatory. - out: ["android/ranging/empty_ranging_fw.txt"], - conditions_default: { - srcs: ["android/ranging/mock/RangingFrameworkInitializer.java"], - cmd: "mkdir -p android/ranging/; cp $(in) $(out);", - out: ["android/ranging/RangingFrameworkInitializer.java"], - }, - }, - }, -} - // Add selected MessageQueue.java implementation to srcs soong_config_module_type { name: "release_package_messagequeue_implementation_srcs", diff --git a/core/java/android/app/Notification.java b/core/java/android/app/Notification.java index 392a1f113c23..a39f216d033e 100644 --- a/core/java/android/app/Notification.java +++ b/core/java/android/app/Notification.java @@ -121,7 +121,6 @@ import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Array; import java.lang.reflect.Constructor; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Objects; @@ -783,10 +782,32 @@ public class Notification implements Parcelable @FlaggedApi(Flags.FLAG_API_RICH_ONGOING) public static final int FLAG_PROMOTED_ONGOING = 0x00040000; - private static final List<Class<? extends Style>> PLATFORM_STYLE_CLASSES = Arrays.asList( - BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class, - DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class, - MessagingStyle.class, CallStyle.class); + private static final Set<Class<? extends Style>> PLATFORM_STYLE_CLASSES = Set.of( + BigTextStyle.class, + BigPictureStyle.class, + InboxStyle.class, + MediaStyle.class, + DecoratedCustomViewStyle.class, + DecoratedMediaCustomViewStyle.class, + MessagingStyle.class, + CallStyle.class + ); + + private static boolean isPlatformStyle(Style style) { + if (style == null) { + return false; + } + + if (PLATFORM_STYLE_CLASSES.contains(style.getClass())) { + return true; + } + + if (Flags.apiRichOngoing()) { + return style.getClass() == ProgressStyle.class; + } + + return false; + } /** @hide */ @IntDef(flag = true, prefix = {"FLAG_"}, value = { @@ -1620,6 +1641,66 @@ public class Notification implements Parcelable public static final String EXTRA_COLORIZED = "android.colorized"; /** + * {@link #extras} key: an arraylist of {@link android.app.Notification.ProgressStyle.Segment} + * bundles provided by a + * {@link android.app.Notification.ProgressStyle} notification as supplied to + * {@link ProgressStyle#setProgressSegments} + * or {@link ProgressStyle#addProgressSegment(ProgressStyle.Segment)}. + * This extra is a parcelable array list of bundles. + * @hide + */ + @FlaggedApi(Flags.FLAG_API_RICH_ONGOING) + public static final String EXTRA_PROGRESS_SEGMENTS = "android.progressSegments"; + + /** + * {@link #extras} key: an arraylist of {@link android.app.Notification.ProgressStyle.Step} + * bundles provided by a + * {@link android.app.Notification.ProgressStyle} notification as supplied to + * {@link ProgressStyle#setProgressSteps} + * or {@link ProgressStyle#addProgressStep(ProgressStyle.Step)}. + * This extra is a parcelable array list of bundles. + * @hide + */ + @FlaggedApi(Flags.FLAG_API_RICH_ONGOING) + public static final String EXTRA_PROGRESS_STEPS = "android.progressSteps"; + + /** + * {@link #extras} key: whether the progress bar should be styled by its progress as + * supplied to {@link ProgressStyle#setStyledByProgress}. + * This extra is a boolean. + * @hide + */ + @FlaggedApi(Flags.FLAG_API_RICH_ONGOING) + public static final String EXTRA_STYLED_BY_PROGRESS = "android.styledByProgress"; + + /** + * {@link #extras} key: this is an {@link Icon} of an image to be + * shown as progress bar progress tracker icon in {@link ProgressStyle}, supplied to + *{@link ProgressStyle#setProgressTrackerIcon(Icon)}. + * @hide + */ + @FlaggedApi(Flags.FLAG_API_RICH_ONGOING) + public static final String EXTRA_PROGRESS_TRACKER_ICON = "android.progressTrackerIcon"; + + /** + * {@link #extras} key: this is an {@link Icon} of an image to be + * shown at the beginning of the progress bar in {@link ProgressStyle}, supplied to + *{@link ProgressStyle#setProgressStartIcon(Icon)}. + * @hide + */ + @FlaggedApi(Flags.FLAG_API_RICH_ONGOING) + public static final String EXTRA_PROGRESS_START_ICON = "android.progressStartIcon"; + + /** + * {@link #extras} key: this is an {@link Icon} of an image to be + * shown at the end of the progress bar in {@link ProgressStyle}, supplied to + *{@link ProgressStyle#setProgressEndIcon(Icon)}. + * @hide + */ + @FlaggedApi(Flags.FLAG_API_RICH_ONGOING) + public static final String EXTRA_PROGRESS_END_ICON = "android.progressEndIcon"; + + /** * @hide */ public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo"; @@ -3072,6 +3153,9 @@ public class Notification implements Parcelable if (Flags.apiRichOngoing()) { visitIconUri(visitor, extras.getParcelable(EXTRA_ENROUTE_OVERLAY_ICON, Icon.class)); + visitIconUri(visitor, extras.getParcelable(EXTRA_PROGRESS_TRACKER_ICON, Icon.class)); + visitIconUri(visitor, extras.getParcelable(EXTRA_PROGRESS_START_ICON, Icon.class)); + visitIconUri(visitor, extras.getParcelable(EXTRA_PROGRESS_END_ICON, Icon.class)); } if (mBubbleMetadata != null) { @@ -6630,7 +6714,7 @@ public class Notification implements Parcelable // Custom views which come from a platform style class are safe, and thus do not need to // be wrapped. Any subclass of those styles has the opportunity to make arbitrary // changes to the RemoteViews, and thus can't be trusted as a fully vetted view. - if (fromStyle && PLATFORM_STYLE_CLASSES.contains(mStyle.getClass())) { + if (fromStyle && isPlatformStyle(mStyle)) { return false; } return mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.S; @@ -7971,6 +8055,12 @@ public class Notification implements Parcelable return innerClass; } } + + if (Flags.apiRichOngoing()) { + if (templateClass.equals(ProgressStyle.class.getName())) { + return ProgressStyle.class; + } + } return null; } @@ -11220,6 +11310,724 @@ public class Notification implements Parcelable } } + + /** + * A Notification Style used to to define a notification whose expanded state includes + * a highly customizable progress bar with segments, steps, a custom tracker icon, + * and custom icons at the start and end of the progress bar. + * + * This style is suggested for use cases where the app is showing a tracker to the + * user of a thing they are interested in: the location of a car on its way + * to pick them up, food being delivered, or their own progress in a navigation + * journey. + * + * To use this style with your Notification, feed it to + * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so: + * <pre class="prettyprint"> + * new Notification.Builder(context) + * .setSmallIcon(R.drawable.ic_notification) + * .setColor(Color.GREEN) + * .setColorized(true) + * .setContentTitle("Arrive 10:08 AM"). + * .setContentText("Dominique Ansel Bakery Soho") + * .addAction(new Notification.Action("Exit navigation",...)) + * .setStyle(new Notification.ProgressStyle() + * .setStyledByProgress(false) + * .setProgress(456) + * .setProgressTrackerIcon(Icon.createWithResource(R.drawable.ic_driving_tracker)) + * .addProgressSegment(new Segment(41).setColor(Color.BLACK)) + * .addProgressSegment(new Segment(552).setColor(Color.YELLOW)) + * .addProgressSegment(new Segment(253).setColor(Color.YELLOW)) + * .addProgressSegment(new Segment(94).setColor(Color.BLUE)) + * .addProgressStep(new Step(60).setColor(Color.RED)) + * .addProgressStep(new Step(560).setColor(Color.YELLOW)) + * ) + * </pre> + * + * + * + * NOTE: The progress bar layout will be mirrored for RTL layout. + * NOTE: The extras set by {@link Notification.Builder#setProgress} will be overridden by + * the values set on this style object when the notification is built. + * + */ + @FlaggedApi(Flags.FLAG_API_RICH_ONGOING) + public static class ProgressStyle extends Notification.Style { + private static final String KEY_ELEMENT_STABLE_ID = "stableId"; + private static final String KEY_ELEMENT_COLOR = "colorInt"; + private static final String KEY_SEGMENT_LENGTH = "length"; + private static final String KEY_STEP_POSITION = "position"; + + private static final int MAX_PROGRESS_SEGMENT_LIMIT = 15; + private static final int MAX_PROGRESS_STEP_LIMIT = 5; + private static final int DEFAULT_PROGRESS_MAX = 100; + + private List<Segment> mProgressSegments = new ArrayList<>(); + private List<Step> mProgressSteps = new ArrayList<>(); + + private int mProgress = 0; + + private boolean mIndeterminate; + + private boolean mIsStyledByProgress = true; + + @Nullable + private Icon mTrackerIcon; + @Nullable + private Icon mStartIcon; + @Nullable + private Icon mEndIcon; + + /** + * @hide + */ + @Override + public boolean areNotificationsVisiblyDifferent(Style other) { + if (other == null || getClass() != other.getClass()) { + return true; + } + + final ProgressStyle progressStyle = (ProgressStyle) other; + + /** + * @see #setProgressIndeterminate + */ + if (!Objects.equals(mIndeterminate, progressStyle.mIndeterminate)) { + return true; + } + boolean nonIndeterminateCheckResult = false; + if (!mIndeterminate) { + nonIndeterminateCheckResult = !Objects.equals(mProgress, progressStyle.mProgress) + || !Objects.equals(mIsStyledByProgress, progressStyle.mIsStyledByProgress) + || !Objects.equals(mProgressSegments, progressStyle.mProgressSegments) + || !Objects.equals(mProgressSteps, progressStyle.mProgressSteps) + || !Objects.equals(mTrackerIcon, progressStyle.mTrackerIcon); + } + + return !Objects.equals(mStartIcon, progressStyle.mStartIcon) + || !Objects.equals(mEndIcon, progressStyle.mEndIcon) + || nonIndeterminateCheckResult; + } + + /** + * Gets the segments that define the background layer of the progress bar. + * + * If no segments are provided, the progress bar will be rendered with a single segment + * with length 100 and default color. + * + * @see #setProgressSegments + * @see #addProgressSegment + * @see Segment + */ + public @NonNull List<Segment> getProgressSegments() { + return mProgressSegments; + } + + /** + * Sets or replaces the segments of the progress bar. + * + * Segments allow for creating progress bars with multiple colors or sections + * to represent different stages or categories of progress. + * For example, Traffic conditions along a navigation journey. + * @see Segment + */ + public @NonNull ProgressStyle setProgressSegments(@NonNull List<Segment> progressSegments) { + mProgressSegments = new ArrayList<>(progressSegments.size()); + return this; + } + + /** + * Appends a segment to the end of the progress bar. + * + * Segments allow for creating progress bars with multiple colors or sections + * to represent different stages or categories of progress. + * For example, Traffic conditions along a navigation journey. + * @see Segment + */ + public @NonNull ProgressStyle addProgressSegment(@NonNull Segment segment) { + if (mProgressSegments == null) { + mProgressSegments = new ArrayList<>(); + } + mProgressSegments.add(segment); + + return this; + } + + /** + * Gets the steps that are displayed on the progress bar. + *. + * @see #setProgressSteps + * @see #addProgressStep + * @see Step + */ + public @NonNull List<Step> getProgressSteps() { + return mProgressSteps; + } + + /** + * Replaces all the progress steps. + * + * Steps are designated points within a progressbar to visualize + * distinct stages or milestones. + * For example, you might use steps to mark stops in a multi-stop + * navigation journey, where each step represents a destination. + * @see Step + */ + public @NonNull ProgressStyle setProgressSteps(@NonNull List<Step> steps) { + mProgressSteps = new ArrayList<>(steps); + return this; + } + + /** + * Adds another step. + * + * Steps are designated points within a progressbar to visualize + * distinct stages or milestones. + * For example, you might use steps to mark stops in a multi-stop + * navigation journey, where each step represents a destination. + * + * Steps can be added in any order, as their + * position within the progress bar is determined by their individual + * {@link Step#getPosition()}. + * @see Step + */ + public @NonNull ProgressStyle addProgressStep(@NonNull Step step) { + if (mProgressSteps == null) { + mProgressSteps = new ArrayList<>(); + } + mProgressSteps.add(step); + + return this; + } + + /** + * Gets the progress value of the progress bar. + * @see #setProgress + */ + public int getProgress() { + return mProgress; + } + + /** + * Specifies the progress (in the same units as {@link Segment#getLength()}) + * of the tracker along the length of the bar. + * + * The max progress value is the sum of all Segment lengths. + * The default value is 0. + */ + public @NonNull ProgressStyle setProgress(int progress) { + mProgress = progress; + return this; + } + + /** + * Gets the sum of the lengths of all Segments in the style, which + * defines the maximum progress. Defaults to 100 when segments are omitted. + */ + public int getProgressMax() { + final List<Segment> progressSegment = mProgressSegments; + if (progressSegment == null || progressSegment.isEmpty()) { + return DEFAULT_PROGRESS_MAX; + } else { + int progressMax = 0; + int validSegmentCount = 0; + for (int i = 0; i < progressSegment.size() + && validSegmentCount < MAX_PROGRESS_SEGMENT_LIMIT; i++) { + int segmentLength = progressSegment.get(i).getLength(); + if (segmentLength > 0) { + try { + progressMax = Math.addExact(progressMax, segmentLength); + validSegmentCount++; + } catch (ArithmeticException e) { + Log.e(TAG, + "Notification.ProgressStyle segment total overflowed.", e); + return DEFAULT_PROGRESS_MAX; + } + } + } + + if (validSegmentCount == 0) { + return DEFAULT_PROGRESS_MAX; + } + + return progressMax; + } + + } + + /** + * Get indeterminate value of the progress bar. + * @see #setProgressIndeterminate + */ + public boolean isProgressIndeterminate() { + return mIndeterminate; + } + + /** + * Used to indicate an initialization state without a known progress amount. + * When specified, the following fields are ignored: + * @see #setProgress + * @see #setProgressSegments + * @see #setProgressSteps + * @see #setProgressTrackerIcon + * @see #setStyledByProgress + * + * If the app provides exactly one Segment, that segment's color will be + * used to style the indeterminate bar. + */ + public @NonNull ProgressStyle setProgressIndeterminate(boolean indeterminate) { + mIndeterminate = indeterminate; + return this; + } + + /** + * Gets whether the progress bar's style is based on its progress. + * @see #setStyledByProgress + */ + public boolean isStyledByProgress() { + return mIsStyledByProgress; + } + + /** + * Indicates whether the segments and steps will be styled differently + * based on whether they are behind or ahead of the current progress. + * When true, segments appearing ahead of the current progress will be given a + * slightly different appearance to indicate that it is part of the progress bar + * that is not "filled". + * When false, all segments will be given the filled appearance, and it will be + * the app's responsibility to use #setProgressTrackerIcon or segment colors + * to make the current progress clear to the user. + * the default value is true. + */ + public @NonNull ProgressStyle setStyledByProgress(boolean enabled) { + mIsStyledByProgress = enabled; + return this; + } + + + /** + * Gets the progress tracker icon for the progress bar. + * @see #setProgressTrackerIcon + */ + public @Nullable Icon getProgressTrackerIcon() { + return mTrackerIcon; + } + + /** + * An optional icon that can appear as an overlay on the bar at the point of + * current progress. + * Aspect ratio may be anywhere from 2:1 to 1:2; content outside that + * aspect ratio range will be cropped. + * This icon will be mirrored in RTL. + */ + public @NonNull ProgressStyle setProgressTrackerIcon(@Nullable Icon trackerIcon) { + mTrackerIcon = trackerIcon; + return this; + } + + /** + * Gets the progress bar start icon. + * @see #setProgressStartIcon + */ + public @Nullable Icon getProgressStartIcon() { + return mStartIcon; + } + + /** + * An optional square icon that appears at the start of the progress bar. + * This icon will be cropped to its central square. + * This icon will NOT be mirrored in RTL layouts. + */ + public @NonNull ProgressStyle setProgressStartIcon(@Nullable Icon startIcon) { + mStartIcon = startIcon; + return this; + } + + /** + * Gets the progress bar end icon. + * @see #setProgressEndIcon(Icon) + */ + public @Nullable Icon getProgressEndIcon() { + return mEndIcon; + } + + /** + * An optional square icon that appears at the end of the progress bar. + * This icon will be cropped to its central square. + * This icon will NOT be mirrored in RTL layouts. + */ + public @NonNull ProgressStyle setProgressEndIcon(@Nullable Icon endIcon) { + mEndIcon = endIcon; + return this; + } + + /** + * @hide + */ + @Override + public void purgeResources() { + super.purgeResources(); + if (mTrackerIcon != null) { + mTrackerIcon.convertToAshmem(); + } + if (mStartIcon != null) { + mStartIcon.convertToAshmem(); + } + if (mEndIcon != null) { + mEndIcon.convertToAshmem(); + } + } + + /** + * @hide + */ + @Override + public void reduceImageSizes(Context context) { + super.reduceImageSizes(context); + + final Resources resources = context.getResources(); + + int progressIconSize = + resources.getDimensionPixelSize(R.dimen.notification_progress_icon_size); + if (mStartIcon != null) { + mStartIcon.scaleDownIfNecessary(progressIconSize, progressIconSize); + } + if (mEndIcon != null) { + mEndIcon.scaleDownIfNecessary(progressIconSize, progressIconSize); + } + if (mTrackerIcon != null) { + int progressTrackerWidth = resources.getDimensionPixelSize( + R.dimen.notification_progress_tracker_width); + int progressTrackerHeight = resources.getDimensionPixelSize( + R.dimen.notification_progress_tracker_height); + mTrackerIcon.scaleDownIfNecessary(progressTrackerWidth, progressTrackerHeight); + } + } + + /** + * @hide + */ + @Override + public void addExtras(Bundle extras) { + super.addExtras(extras); + extras.putParcelableArrayList(EXTRA_PROGRESS_SEGMENTS, + getProgressSegmentsAsBundleList(mProgressSegments)); + extras.putParcelableArrayList(EXTRA_PROGRESS_STEPS, + getProgressStepsAsBundleList(mProgressSteps)); + + extras.putInt(EXTRA_PROGRESS, mProgress); + extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, mIndeterminate); + extras.putInt(EXTRA_PROGRESS_MAX, getProgressMax()); + extras.putBoolean(EXTRA_STYLED_BY_PROGRESS, mIsStyledByProgress); + + if (mTrackerIcon != null) { + extras.putParcelable(EXTRA_PROGRESS_TRACKER_ICON, mTrackerIcon); + } else { + extras.remove(EXTRA_PROGRESS_TRACKER_ICON); + } + + if (mStartIcon != null) { + extras.putParcelable(EXTRA_PROGRESS_START_ICON, mStartIcon); + } else { + extras.remove(EXTRA_PROGRESS_START_ICON); + } + + if (mEndIcon != null) { + extras.putParcelable(EXTRA_PROGRESS_END_ICON, mEndIcon); + } else { + extras.remove(EXTRA_PROGRESS_END_ICON); + } + } + + /** + * @hide + */ + @Override + protected void restoreFromExtras(Bundle extras) { + super.restoreFromExtras(extras); + mProgressSegments = getProgressSegmentsFromBundleList( + extras.getParcelableArrayList(EXTRA_PROGRESS_SEGMENTS, Bundle.class)); + mProgress = extras.getInt(EXTRA_PROGRESS, 0); + mIndeterminate = extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE, false); + mIsStyledByProgress = extras.getBoolean(EXTRA_STYLED_BY_PROGRESS, true); + mTrackerIcon = extras.getParcelable(EXTRA_PROGRESS_TRACKER_ICON, Icon.class); + mStartIcon = extras.getParcelable(EXTRA_PROGRESS_START_ICON, Icon.class); + mEndIcon = extras.getParcelable(EXTRA_PROGRESS_END_ICON, Icon.class); + mProgressSteps = getProgressStepsFromBundleList( + extras.getParcelableArrayList(EXTRA_PROGRESS_STEPS, Bundle.class)); + } + + /** + * @hide + */ + @Override + public boolean displayCustomViewInline() { + // This is a lie; True is returned for progress notifications to make sure + // that the custom view is not used instead of the template, but it will not + // actually be included. + return true; + } + + private static @NonNull ArrayList<Bundle> getProgressSegmentsAsBundleList( + @Nullable List<Segment> progressSegments) { + final ArrayList<Bundle> segments = new ArrayList<>(); + if (progressSegments != null && !progressSegments.isEmpty()) { + for (int i = 0; i < progressSegments.size(); i++) { + final Segment segment = progressSegments.get(i); + if (segment.getLength() <= 0) { + continue; + } + + final Bundle bundle = new Bundle(); + bundle.putInt(KEY_SEGMENT_LENGTH, segment.getLength()); + bundle.putInt(KEY_ELEMENT_STABLE_ID, segment.getStableId()); + bundle.putInt(KEY_ELEMENT_COLOR, segment.getColor()); + + segments.add(bundle); + } + } + + return segments; + } + + private static @NonNull List<Segment> getProgressSegmentsFromBundleList( + @Nullable List<Bundle> segmentBundleList) { + final ArrayList<Segment> segments = new ArrayList<>(); + if (segmentBundleList != null && !segmentBundleList.isEmpty()) { + for (int i = 0; i < segmentBundleList.size(); i++) { + final Bundle segmentBundle = segmentBundleList.get(i); + final int length = segmentBundle.getInt(KEY_SEGMENT_LENGTH); + if (length <= 0) { + continue; + } + + final int stableId = segmentBundle.getInt(KEY_ELEMENT_STABLE_ID); + final int color = segmentBundle.getInt(KEY_ELEMENT_COLOR, + Notification.COLOR_DEFAULT); + final Segment segment = new Segment(length) + .setStableId(stableId).setColor(color); + + segments.add(segment); + } + } + + return segments; + } + + private static @NonNull ArrayList<Bundle> getProgressStepsAsBundleList( + @Nullable List<Step> progressSteps) { + final ArrayList<Bundle> steps = new ArrayList<>(); + if (progressSteps != null && !progressSteps.isEmpty()) { + for (int i = 0; i < progressSteps.size(); i++) { + final Step step = progressSteps.get(i); + if (step.getPosition() < 0) { + continue; + } + + final Bundle bundle = new Bundle(); + bundle.putInt(KEY_STEP_POSITION, step.getPosition()); + bundle.putInt(KEY_ELEMENT_STABLE_ID, step.getStableId()); + bundle.putInt(KEY_ELEMENT_COLOR, step.getColor()); + + steps.add(bundle); + } + } + + return steps; + } + + private static @NonNull List<Step> getProgressStepsFromBundleList( + @Nullable List<Bundle> stepBundleList) { + final ArrayList<Step> steps = new ArrayList<>(); + + if (stepBundleList != null && !stepBundleList.isEmpty()) { + for (int i = 0; i < stepBundleList.size(); i++) { + final Bundle segmentBundle = stepBundleList.get(i); + final int position = segmentBundle.getInt(KEY_STEP_POSITION); + if (position < 0) { + continue; + } + final int stableId = segmentBundle.getInt(KEY_ELEMENT_STABLE_ID); + final int color = segmentBundle.getInt(KEY_ELEMENT_COLOR, + Notification.COLOR_DEFAULT); + final Step step = new Step(position).setStableId(stableId).setColor(color); + steps.add(step); + } + } + + return steps; + } + + /** + * A segment of the progress bar, which defines its length and color. + * Segments allow for creating progress bars with multiple colors or sections + * to represent different stages or categories of progress. + * For example, Traffic conditions along a navigation journey. + */ + public static final class Segment { + private int mLength; + private int mStableId = 0; + @ColorInt + private int mColor = Notification.COLOR_DEFAULT; + + /** + * Create a segment with a non-zero length. + * @param length + * See {@link #getLength} + */ + public Segment(int length) { + mLength = length; + } + + /** + * The length of this Segment within the progress bar. + * This value has no units, it is just relative to the length of other segments, + * and the value provided to {@link ProgressStyle#setProgress}. + */ + public int getLength() { + return mLength; + } + + /** + * Gets the stable id of this Segment. + * + * @see #setStableId + */ + public int getStableId() { + return mStableId; + } + + /** + * Optional ID used to uniquely identify the element across updates. + */ + public @NonNull Segment setStableId(int stableId) { + mStableId = stableId; + return this; + } + + /** + * Returns the color of this Segment. + * + * @see #setColor + */ + @ColorInt + public int getColor() { + return mColor; + } + + /** + * Optional color of this Segment + */ + public @NonNull Segment setColor(@ColorInt int color) { + mColor = color; + return this; + } + + /** + * Needed for {@link Notification.Style#areNotificationsVisiblyDifferent} + */ + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Segment segment = (Segment) o; + return mLength == segment.mLength && mStableId == segment.mStableId + && mColor == segment.mColor; + } + + @Override + public int hashCode() { + return Objects.hash(mLength, mStableId, mColor); + } + } + + /** + * A step within the progress bar, defining its position and color. + * Steps are designated points within a progressbar to visualize + * distinct stages or milestones. + * For example, you might use steps to mark stops in a multi-stop + * navigation journey, where each step represents a destination. + */ + public static final class Step { + + private int mPosition; + private int mStableId; + @ColorInt + private int mColor = Notification.COLOR_DEFAULT; + + /** + * Create a step element. + * The position of this step on the progress bar + * relative to {@link ProgressStyle#getProgressMax} + * @param position + * See {@link #getPosition} + */ + public Step(int position) { + mPosition = position; + } + + /** + * Gets the position of this Step. + * The position of this step on the progress bar + * relative to {@link ProgressStyle#getProgressMax}. + */ + public int getPosition() { + return mPosition; + } + + + /** + * Optional ID used to uniqurely identify the element across updates. + */ + public int getStableId() { + return mStableId; + } + + /** + * Optional ID used to uniqurely identify the element across updates. + */ + public @NonNull Step setStableId(int stableId) { + mStableId = stableId; + return this; + } + + /** + * Returns the color of this Segment. + * + * @see #setColor + */ + @ColorInt + public int getColor() { + return mColor; + } + + /** + * Optional color of this Segment + */ + public @NonNull Step setColor(@ColorInt int color) { + mColor = color; + return this; + } + + /** + * Needed for {@link Notification.Style#areNotificationsVisiblyDifferent} + */ + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Step step = (Step) o; + return mPosition == step.mPosition && mStableId == step.mStableId + && mColor == step.mColor; + } + + @Override + public int hashCode() { + return Objects.hash(mPosition, mStableId, mColor); + } + } + } + /** * Notification style for custom views that are decorated by the system * diff --git a/core/java/android/app/SystemServiceRegistry.java b/core/java/android/app/SystemServiceRegistry.java index ea4148c8ffa1..c13a58f52ac8 100644 --- a/core/java/android/app/SystemServiceRegistry.java +++ b/core/java/android/app/SystemServiceRegistry.java @@ -230,7 +230,6 @@ import android.print.IPrintManager; import android.print.PrintManager; import android.provider.E2eeContactKeysManager; import android.provider.ProviderFrameworkInitializer; -import android.ranging.RangingFrameworkInitializer; import android.safetycenter.SafetyCenterFrameworkInitializer; import android.scheduling.SchedulingFrameworkInitializer; import android.security.FileIntegrityManager; @@ -1826,12 +1825,6 @@ public final class SystemServiceRegistry { if (android.webkit.Flags.updateServiceIpcWrapper()) { WebViewBootstrapFrameworkInitializer.registerServiceWrappers(); } - // This is guarded by aconfig flag "com.android.ranging.flags.ranging_stack_enabled" - // when the build flag RELEASE_RANGING_STACK is enabled. When disabled, this calls the - // mock RangingFrameworkInitializer#registerServiceWrappers which is no-op. As the - // aconfig lib for ranging module is built only if RELEASE_RANGING_STACK is enabled, - // flagcannot be added here. - RangingFrameworkInitializer.registerServiceWrappers(); } finally { // If any of the above code throws, we're in a pretty bad shape and the process // will likely crash, but we'll reset it just in case there's an exception handler... diff --git a/core/java/android/app/appfunctions/AppFunctionManager.java b/core/java/android/app/appfunctions/AppFunctionManager.java index 4682f3d30e1e..216ba5d994ec 100644 --- a/core/java/android/app/appfunctions/AppFunctionManager.java +++ b/core/java/android/app/appfunctions/AppFunctionManager.java @@ -27,6 +27,8 @@ import android.annotation.RequiresPermission; import android.annotation.SystemService; import android.annotation.UserHandleAware; import android.content.Context; +import android.os.CancellationSignal; +import android.os.ICancellationSignal; import android.os.RemoteException; import java.util.Objects; @@ -73,7 +75,43 @@ public final class AppFunctionManager { * android.permission.EXECUTE_APP_FUNCTIONS_TRUSTED} or {@code * android.permission.EXECUTE_APP_FUNCTIONS}, the execution result will contain {@code * ExecuteAppFunctionResponse.RESULT_DENIED}. + * @deprecated Use {@link #executeAppFunction(ExecuteAppFunctionRequest, Executor, + * CancellationSignal, Consumer)} instead. This method will be removed once usage references + * are updated. */ + @RequiresPermission( + anyOf = { + Manifest.permission.EXECUTE_APP_FUNCTIONS_TRUSTED, + Manifest.permission.EXECUTE_APP_FUNCTIONS + }, + conditional = true) + @UserHandleAware + @Deprecated + public void executeAppFunction( + @NonNull ExecuteAppFunctionRequest request, + @NonNull @CallbackExecutor Executor executor, + @NonNull Consumer<ExecuteAppFunctionResponse> callback) { + executeAppFunction(request, executor, new CancellationSignal(), callback); + } + + /** + * Executes the app function. + * + * <p>Note: Applications can execute functions they define. To execute functions defined in + * another component, apps would need to have {@code + * android.permission.EXECUTE_APP_FUNCTIONS_TRUSTED} or {@code + * android.permission.EXECUTE_APP_FUNCTIONS}. + * + * @param request the request to execute the app function + * @param executor the executor to run the callback + * @param cancellationSignal the cancellation signal to cancel the execution. + * @param callback the callback to receive the function execution result. if the calling app + * does not own the app function or does not have {@code + * android.permission.EXECUTE_APP_FUNCTIONS_TRUSTED} or {@code + * android.permission.EXECUTE_APP_FUNCTIONS}, the execution result will contain {@code + * ExecuteAppFunctionResponse.RESULT_DENIED}. + */ + // TODO(b/357551503): Document the behavior when the cancellation signal is issued. // TODO(b/360864791): Document that apps can opt-out from being executed by callers with // EXECUTE_APP_FUNCTIONS and how a caller knows whether a function is opted out. // TODO(b/357551503): Update documentation when get / set APIs are implemented that this will @@ -88,6 +126,7 @@ public final class AppFunctionManager { public void executeAppFunction( @NonNull ExecuteAppFunctionRequest request, @NonNull @CallbackExecutor Executor executor, + @NonNull CancellationSignal cancellationSignal, @NonNull Consumer<ExecuteAppFunctionResponse> callback) { Objects.requireNonNull(request); Objects.requireNonNull(executor); @@ -96,25 +135,31 @@ public final class AppFunctionManager { ExecuteAppFunctionAidlRequest aidlRequest = new ExecuteAppFunctionAidlRequest( request, mContext.getUser(), mContext.getPackageName()); + try { - mService.executeAppFunction( - aidlRequest, - new IExecuteAppFunctionCallback.Stub() { - @Override - public void onResult(ExecuteAppFunctionResponse result) { - try { - executor.execute(() -> callback.accept(result)); - } catch (RuntimeException e) { - // Ideally shouldn't happen since errors are wrapped into the - // response, but we catch it here for additional safety. - callback.accept( - ExecuteAppFunctionResponse.newFailure( - getResultCode(e), - e.getMessage(), - /* extras= */ null)); - } - } - }); + ICancellationSignal cancellationTransport = + mService.executeAppFunction( + aidlRequest, + new IExecuteAppFunctionCallback.Stub() { + @Override + public void onResult(ExecuteAppFunctionResponse result) { + try { + executor.execute(() -> callback.accept(result)); + } catch (RuntimeException e) { + // Ideally shouldn't happen since errors are wrapped into + // the + // response, but we catch it here for additional safety. + callback.accept( + ExecuteAppFunctionResponse.newFailure( + getResultCode(e), + e.getMessage(), + /* extras= */ null)); + } + } + }); + if (cancellationTransport != null) { + cancellationSignal.setRemote(cancellationTransport); + } } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } diff --git a/core/java/android/app/appfunctions/AppFunctionService.java b/core/java/android/app/appfunctions/AppFunctionService.java index 0d981ea5a679..8e417737515e 100644 --- a/core/java/android/app/appfunctions/AppFunctionService.java +++ b/core/java/android/app/appfunctions/AppFunctionService.java @@ -29,7 +29,12 @@ import android.app.Service; import android.content.Context; import android.content.Intent; import android.os.Binder; +import android.os.Bundle; import android.os.IBinder; +import android.os.ICancellationSignal; +import android.os.CancellationSignal; +import android.os.RemoteCallback; +import android.os.RemoteException; import java.util.function.Consumer; @@ -74,6 +79,7 @@ public abstract class AppFunctionService extends Service { */ void perform( @NonNull ExecuteAppFunctionRequest request, + @NonNull CancellationSignal cancellationSignal, @NonNull Consumer<ExecuteAppFunctionResponse> callback); } @@ -85,6 +91,7 @@ public abstract class AppFunctionService extends Service { @Override public void executeAppFunction( @NonNull ExecuteAppFunctionRequest request, + @NonNull ICancellationCallback cancellationCallback, @NonNull IExecuteAppFunctionCallback callback) { if (context.checkCallingPermission(BIND_APP_FUNCTION_SERVICE) == PERMISSION_DENIED) { @@ -93,7 +100,10 @@ public abstract class AppFunctionService extends Service { SafeOneTimeExecuteAppFunctionCallback safeCallback = new SafeOneTimeExecuteAppFunctionCallback(callback); try { - onExecuteFunction.perform(request, safeCallback::onResult); + onExecuteFunction.perform( + request, + buildCancellationSignal(cancellationCallback), + safeCallback::onResult); } catch (Exception ex) { // Apps should handle exceptions. But if they don't, report the error on // behalf of them. @@ -105,6 +115,21 @@ public abstract class AppFunctionService extends Service { }; } + private static CancellationSignal buildCancellationSignal( + @NonNull ICancellationCallback cancellationCallback) { + final ICancellationSignal cancellationSignalTransport = + CancellationSignal.createTransport(); + CancellationSignal cancellationSignal = + CancellationSignal.fromTransport(cancellationSignalTransport); + try { + cancellationCallback.sendCancellationTransport(cancellationSignalTransport); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } + + return cancellationSignal ; + } + private final Binder mBinder = createBinder( AppFunctionService.this, AppFunctionService.this::onExecuteFunction); @@ -115,6 +140,7 @@ public abstract class AppFunctionService extends Service { return mBinder; } + /** * Called by the system to execute a specific app function. * @@ -134,9 +160,45 @@ public abstract class AppFunctionService extends Service { * * @param request The function execution request. * @param callback A callback to report back the result. + * + * @deprecated Use {@link #onExecuteFunction(ExecuteAppFunctionRequest, CancellationSignal, + * Consumer)} instead. This method will be removed once usage references are updated. */ @MainThread + @Deprecated public abstract void onExecuteFunction( @NonNull ExecuteAppFunctionRequest request, @NonNull Consumer<ExecuteAppFunctionResponse> callback); + + /** + * Called by the system to execute a specific app function. + * + * <p>This method is triggered when the system requests your AppFunctionService to handle a + * particular function you have registered and made available. + * + * <p>To ensure proper routing of function requests, assign a unique identifier to each + * function. This identifier doesn't need to be globally unique, but it must be unique within + * your app. For example, a function to order food could be identified as "orderFood". In most + * cases this identifier should come from the ID automatically generated by the AppFunctions + * SDK. You can determine the specific function to invoke by calling {@link + * ExecuteAppFunctionRequest#getFunctionIdentifier()}. + * + * <p>This method is always triggered in the main thread. You should run heavy tasks on a worker + * thread and dispatch the result with the given callback. You should always report back the + * result using the callback, no matter if the execution was successful or not. + * + * <p>This method also accepts a {@link CancellationSignal} that the app should listen to cancel + * the execution of function if requested by the system. + * + * @param request The function execution request. + * @param cancellationSignal A signal to cancel the execution. + * @param callback A callback to report back the result. + */ + @MainThread + public void onExecuteFunction( + @NonNull ExecuteAppFunctionRequest request, + @NonNull CancellationSignal cancellationSignal, + @NonNull Consumer<ExecuteAppFunctionResponse> callback) { + onExecuteFunction(request, callback); + } } diff --git a/core/java/android/app/appfunctions/GenericDocumentWrapper.java b/core/java/android/app/appfunctions/GenericDocumentWrapper.java index 84b1837f4a2f..b29b64e44d21 100644 --- a/core/java/android/app/appfunctions/GenericDocumentWrapper.java +++ b/core/java/android/app/appfunctions/GenericDocumentWrapper.java @@ -16,10 +16,13 @@ package android.app.appfunctions; +import android.annotation.Nullable; import android.app.appsearch.GenericDocument; import android.os.Parcel; import android.os.Parcelable; +import android.util.MathUtils; +import androidx.annotation.GuardedBy; import androidx.annotation.NonNull; import java.util.Objects; @@ -31,24 +34,33 @@ import java.util.Objects; * <p>{#link {@link Parcel#writeBlob(byte[])}} could take care of whether to pass data via binder * directly or Android shared memory if the data is large. * + * <p>This class performs lazy unparcelling. The `GenericDocument` is only unparcelled + * from the underlying `Parcel` when {@link #getValue()} is called. This optimization + * allows the system server to pass through the generic document, without unparcel and parcel it. + * * @hide * @see Parcel#writeBlob(byte[]) */ public final class GenericDocumentWrapper implements Parcelable { + @Nullable + @GuardedBy("mLock") + private GenericDocument mGenericDocument; + @GuardedBy("mLock") + @Nullable private Parcel mParcel; + private final Object mLock = new Object(); + public static final Creator<GenericDocumentWrapper> CREATOR = new Creator<>() { @Override public GenericDocumentWrapper createFromParcel(Parcel in) { - byte[] dataBlob = Objects.requireNonNull(in.readBlob()); - Parcel unmarshallParcel = Parcel.obtain(); - try { - unmarshallParcel.unmarshall(dataBlob, 0, dataBlob.length); - unmarshallParcel.setDataPosition(0); - return new GenericDocumentWrapper( - GenericDocument.createFromParcel(unmarshallParcel)); - } finally { - unmarshallParcel.recycle(); - } + int length = in.readInt(); + int offset = in.dataPosition(); + in.setDataPosition(MathUtils.addOrThrow(offset, length)); + + Parcel p = Parcel.obtain(); + p.appendFrom(in, offset, length); + p.setDataPosition(0); + return new GenericDocumentWrapper(p); } @Override @@ -56,16 +68,42 @@ public final class GenericDocumentWrapper implements Parcelable { return new GenericDocumentWrapper[size]; } }; - @NonNull private final GenericDocument mGenericDocument; public GenericDocumentWrapper(@NonNull GenericDocument genericDocument) { mGenericDocument = Objects.requireNonNull(genericDocument); + mParcel = null; + } + + public GenericDocumentWrapper(@NonNull Parcel parcel) { + mGenericDocument = null; + mParcel = Objects.requireNonNull(parcel); } /** Returns the wrapped {@link android.app.appsearch.GenericDocument} */ @NonNull public GenericDocument getValue() { - return mGenericDocument; + unparcel(); + synchronized (mLock) { + return Objects.requireNonNull(mGenericDocument); + } + } + + private void unparcel() { + synchronized (mLock) { + if (mGenericDocument != null) { + return; + } + byte[] dataBlob = Objects.requireNonNull(Objects.requireNonNull(mParcel).readBlob()); + Parcel unmarshallParcel = Parcel.obtain(); + try { + unmarshallParcel.unmarshall(dataBlob, 0, dataBlob.length); + unmarshallParcel.setDataPosition(0); + mGenericDocument = GenericDocument.createFromParcel(unmarshallParcel); + mParcel = null; + } finally { + unmarshallParcel.recycle(); + } + } } @Override @@ -75,13 +113,32 @@ public final class GenericDocumentWrapper implements Parcelable { @Override public void writeToParcel(@NonNull Parcel dest, int flags) { - Parcel parcel = Parcel.obtain(); - try { - mGenericDocument.writeToParcel(parcel, flags); - byte[] bytes = parcel.marshall(); - dest.writeBlob(bytes); - } finally { - parcel.recycle(); + synchronized (mLock) { + if (mGenericDocument != null) { + int lengthPos = dest.dataPosition(); + // write a placeholder for length + dest.writeInt(-1); + Parcel tempParcel = Parcel.obtain(); + byte[] bytes; + try { + mGenericDocument.writeToParcel(tempParcel, flags); + bytes = tempParcel.marshall(); + } finally { + tempParcel.recycle(); + } + int startPos = dest.dataPosition(); + dest.writeBlob(bytes); + int endPos = dest.dataPosition(); + dest.setDataPosition(lengthPos); + // Overwrite the length placeholder + dest.writeInt(endPos - startPos); + dest.setDataPosition(endPos); + + } else { + Parcel originalParcel = Objects.requireNonNull(mParcel); + dest.writeInt(originalParcel.dataSize()); + dest.appendFrom(originalParcel, 0, originalParcel.dataSize()); + } } } } diff --git a/core/java/android/app/appfunctions/IAppFunctionManager.aidl b/core/java/android/app/appfunctions/IAppFunctionManager.aidl index 28827bb3052c..c63217ffe850 100644 --- a/core/java/android/app/appfunctions/IAppFunctionManager.aidl +++ b/core/java/android/app/appfunctions/IAppFunctionManager.aidl @@ -18,6 +18,7 @@ package android.app.appfunctions; import android.app.appfunctions.ExecuteAppFunctionAidlRequest; import android.app.appfunctions.IExecuteAppFunctionCallback; +import android.os.ICancellationSignal; /** * Defines the interface for apps to interact with the app function execution service @@ -32,8 +33,8 @@ interface IAppFunctionManager { * @param callback the callback to report the result. */ @JavaPassthrough(annotation="@android.annotation.RequiresPermission(anyOf = {android.Manifest.permission.EXECUTE_APP_FUNCTIONS_TRUSTED,android.Manifest.permission.EXECUTE_APP_FUNCTIONS}, conditional = true)") - void executeAppFunction( + ICancellationSignal executeAppFunction( in ExecuteAppFunctionAidlRequest request, in IExecuteAppFunctionCallback callback ); -}
\ No newline at end of file +} diff --git a/core/java/android/app/appfunctions/IAppFunctionService.aidl b/core/java/android/app/appfunctions/IAppFunctionService.aidl index cc5a20cfa194..291f33ccb1b8 100644 --- a/core/java/android/app/appfunctions/IAppFunctionService.aidl +++ b/core/java/android/app/appfunctions/IAppFunctionService.aidl @@ -16,7 +16,7 @@ package android.app.appfunctions; -import android.os.Bundle; +import android.app.appfunctions.ICancellationCallback; import android.app.appfunctions.IExecuteAppFunctionCallback; import android.app.appfunctions.ExecuteAppFunctionRequest; @@ -34,10 +34,12 @@ oneway interface IAppFunctionService { * Called by the system to execute a specific app function. * * @param request the function execution request. + * @param cancellationCallback a callback to send back the cancellation transport. * @param callback a callback to report back the result. */ void executeAppFunction( in ExecuteAppFunctionRequest request, + in ICancellationCallback cancellationCallback, in IExecuteAppFunctionCallback callback ); } diff --git a/core/java/android/app/appfunctions/ICancellationCallback.aidl b/core/java/android/app/appfunctions/ICancellationCallback.aidl new file mode 100644 index 000000000000..03235aca017a --- /dev/null +++ b/core/java/android/app/appfunctions/ICancellationCallback.aidl @@ -0,0 +1,24 @@ +/* + * 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 android.app.appfunctions; + +import android.os.ICancellationSignal; + +/** {@hide} */ +oneway interface ICancellationCallback { + void sendCancellationTransport(in ICancellationSignal cancellationTransport); +}
\ No newline at end of file diff --git a/core/java/android/hardware/camera2/CameraManager.java b/core/java/android/hardware/camera2/CameraManager.java index 21627920f598..1b21bdf7ba45 100644 --- a/core/java/android/hardware/camera2/CameraManager.java +++ b/core/java/android/hardware/camera2/CameraManager.java @@ -181,7 +181,7 @@ public final class CameraManager { * @hide */ @TestApi - @FlaggedApi(com.android.window.flags.Flags.FLAG_CAMERA_COMPAT_FOR_FREEFORM) + @FlaggedApi(com.android.window.flags.Flags.FLAG_ENABLE_CAMERA_COMPAT_FOR_DESKTOP_WINDOWING) public static final int ROTATION_OVERRIDE_NONE = ICameraService.ROTATION_OVERRIDE_NONE; /** @@ -191,7 +191,7 @@ public final class CameraManager { * @hide */ @TestApi - @FlaggedApi(com.android.window.flags.Flags.FLAG_CAMERA_COMPAT_FOR_FREEFORM) + @FlaggedApi(com.android.window.flags.Flags.FLAG_ENABLE_CAMERA_COMPAT_FOR_DESKTOP_WINDOWING) public static final int ROTATION_OVERRIDE_OVERRIDE_TO_PORTRAIT = ICameraService.ROTATION_OVERRIDE_OVERRIDE_TO_PORTRAIT; @@ -201,7 +201,7 @@ public final class CameraManager { * @hide */ @TestApi - @FlaggedApi(com.android.window.flags.Flags.FLAG_CAMERA_COMPAT_FOR_FREEFORM) + @FlaggedApi(com.android.window.flags.Flags.FLAG_ENABLE_CAMERA_COMPAT_FOR_DESKTOP_WINDOWING) public static final int ROTATION_OVERRIDE_ROTATION_ONLY = ICameraService.ROTATION_OVERRIDE_ROTATION_ONLY; @@ -1562,7 +1562,7 @@ public final class CameraManager { */ public static int getRotationOverride(@Nullable Context context, @Nullable PackageManager packageManager, @Nullable String packageName) { - if (com.android.window.flags.Flags.cameraCompatForFreeform()) { + if (com.android.window.flags.Flags.enableCameraCompatForDesktopWindowing()) { return getRotationOverrideInternal(context, packageManager, packageName); } else { return shouldOverrideToPortrait(packageManager, packageName) @@ -1574,7 +1574,7 @@ public final class CameraManager { /** * @hide */ - @FlaggedApi(com.android.window.flags.Flags.FLAG_CAMERA_COMPAT_FOR_FREEFORM) + @FlaggedApi(com.android.window.flags.Flags.FLAG_ENABLE_CAMERA_COMPAT_FOR_DESKTOP_WINDOWING) @TestApi public static int getRotationOverrideInternal(@Nullable Context context, @Nullable PackageManager packageManager, @Nullable String packageName) { diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java index b8a8be159d12..d82af55e2771 100644 --- a/core/java/android/provider/Settings.java +++ b/core/java/android/provider/Settings.java @@ -10750,6 +10750,16 @@ public final class Settings { "lock_screen_show_only_unseen_notifications"; /** + * Indicates whether to minimalize the number of notifications to show on the lockscreen. + * <p> + * Type: int (0 for false, 1 for true) + * + * @hide + */ + public static final String LOCK_SCREEN_NOTIFICATION_MINIMALISM = + "lock_screen_notification_minimalism"; + + /** * Indicates whether snooze options should be shown on notifications * <p> * Type: int (0 for false, 1 for true) diff --git a/core/java/android/security/responsible_apis_flags.aconfig b/core/java/android/security/responsible_apis_flags.aconfig index 45e9def2c15f..5457bbee8ad3 100644 --- a/core/java/android/security/responsible_apis_flags.aconfig +++ b/core/java/android/security/responsible_apis_flags.aconfig @@ -24,6 +24,17 @@ flag { } flag { + name: "asm_reintroduce_grace_period" + namespace: "responsible_apis" + description: "Allow launches within the grace period for ASM apps" + bug: "367702727" + metadata { + purpose: PURPOSE_BUGFIX + } +} + + +flag { name: "content_uri_permission_apis" is_exported: true namespace: "responsible_apis" diff --git a/core/java/android/view/WindowLayout.java b/core/java/android/view/WindowLayout.java index dda399357d8c..d5ccca992b4f 100644 --- a/core/java/android/view/WindowLayout.java +++ b/core/java/android/view/WindowLayout.java @@ -157,10 +157,10 @@ public class WindowLayout { // which prevents overlap with the DisplayCutout. if (!attachedInParent && !floatingInScreenWindow) { mTempRect.set(outParentFrame); - outParentFrame.intersectUnchecked(displayCutoutSafeExceptMaybeBars); + intersectOrClamp(outParentFrame, displayCutoutSafeExceptMaybeBars); frames.isParentFrameClippedByDisplayCutout = !mTempRect.equals(outParentFrame); } - outDisplayFrame.intersectUnchecked(displayCutoutSafeExceptMaybeBars); + intersectOrClamp(outDisplayFrame, displayCutoutSafeExceptMaybeBars); } final boolean noLimits = (attrs.flags & FLAG_LAYOUT_NO_LIMITS) != 0; @@ -283,6 +283,19 @@ public class WindowLayout { + " requestedInvisibleTypes=" + WindowInsets.Type.toString(~requestedVisibleTypes)); } + /** + * If both rectangles intersect, set inOutRect to that intersection. Otherwise, clamp inOutRect + * to the side (or the corner) that the other rectangle is away from. + * Unlike {@link Rect#intersectUnchecked(Rect)}, this method guarantees that the new rectangle + * is valid and contained in inOutRect if rectangles involved are valid. + */ + private static void intersectOrClamp(Rect inOutRect, Rect other) { + inOutRect.left = Math.min(Math.max(inOutRect.left, other.left), inOutRect.right); + inOutRect.top = Math.min(Math.max(inOutRect.top, other.top), inOutRect.bottom); + inOutRect.right = Math.max(Math.min(inOutRect.right, other.right), inOutRect.left); + inOutRect.bottom = Math.max(Math.min(inOutRect.bottom, other.bottom), inOutRect.top); + } + public static void extendFrameByCutout(Rect displayCutoutSafe, Rect displayFrame, Rect inOutFrame, Rect tempRect) { if (displayCutoutSafe.contains(inOutFrame)) { diff --git a/core/proto/android/server/vibrator/vibratormanagerservice.proto b/core/proto/android/server/vibrator/vibratormanagerservice.proto index e7f0560612cc..258832e3e7ff 100644 --- a/core/proto/android/server/vibrator/vibratormanagerservice.proto +++ b/core/proto/android/server/vibrator/vibratormanagerservice.proto @@ -157,10 +157,8 @@ message VibratorManagerServiceDumpProto { option (.android.msg_privacy).dest = DEST_AUTOMATIC; repeated int32 vibrator_ids = 1; optional VibrationProto current_vibration = 2; - optional bool is_vibrating = 3; optional int32 is_vibrator_controller_registered = 27; optional VibrationProto current_external_vibration = 4; - optional bool vibrator_under_external_control = 5; optional bool low_power_mode = 6; optional bool vibrate_on = 24; reserved 25; // prev keyboard_vibration_on @@ -183,4 +181,6 @@ message VibratorManagerServiceDumpProto { repeated VibrationProto previous_vibrations = 16; repeated VibrationParamProto previous_vibration_params = 28; reserved 17; // prev previous_external_vibrations + reserved 3; // prev is_vibrating, check current_vibration instead + reserved 5; // prev vibrator_under_external_control, check current_external_vibration instead }
\ No newline at end of file diff --git a/core/res/res/values/config.xml b/core/res/res/values/config.xml index 07efad89010a..92c390656da5 100644 --- a/core/res/res/values/config.xml +++ b/core/res/res/values/config.xml @@ -4457,6 +4457,11 @@ <!-- Bytes that the PinnerService will pin for WebView --> <integer name="config_pinnerWebviewPinBytes">0</integer> + <!-- Maximum memory that PinnerService will pin for apps expressed + as a percentage of total device memory [0,100]. + Example: 10, means 10% of total memory will be the maximum pinned memory --> + <integer name="config_pinnerMaxPinnedMemoryPercentage">10</integer> + <!-- Number of days preloaded file cache should be preserved on a device before it can be deleted --> <integer name="config_keepPreloadsMinDays">7</integer> diff --git a/core/res/res/values/dimens.xml b/core/res/res/values/dimens.xml index f397ef2b151c..6683dc044c9a 100644 --- a/core/res/res/values/dimens.xml +++ b/core/res/res/values/dimens.xml @@ -808,6 +808,12 @@ This is bigger than displayed because listeners can use it for other displays e.g. wearables. --> <dimen name="notification_person_icon_max_size">144dp</dimen> + <!-- The size of the progress bar icon --> + <dimen name="notification_progress_icon_size">20dp</dimen> + <!-- The size of the progress tracker width --> + <dimen name="notification_progress_tracker_width">40dp</dimen> + <!-- The size of the progress tracker height --> + <dimen name="notification_progress_tracker_height">20dp</dimen> <!-- The maximum size of the small notification icon on low memory devices. --> <dimen name="notification_small_icon_size_low_ram">@dimen/notification_small_icon_size</dimen> diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml index 06b36b8f74af..5f40a6c7eba4 100644 --- a/core/res/res/values/symbols.xml +++ b/core/res/res/values/symbols.xml @@ -3467,6 +3467,7 @@ <java-symbol type="integer" name="config_pinnerHomePinBytes" /> <java-symbol type="bool" name="config_pinnerAssistantApp" /> <java-symbol type="integer" name="config_pinnerWebviewPinBytes" /> + <java-symbol type="integer" name="config_pinnerMaxPinnedMemoryPercentage" /> <java-symbol type="string" name="config_doubleTouchGestureEnableFile" /> @@ -3854,6 +3855,9 @@ <java-symbol type="dimen" name="notification_custom_view_max_image_height"/> <java-symbol type="dimen" name="notification_custom_view_max_image_width"/> <java-symbol type="dimen" name="notification_person_icon_max_size" /> + <java-symbol type="dimen" name="notification_progress_icon_size" /> + <java-symbol type="dimen" name="notification_progress_tracker_width" /> + <java-symbol type="dimen" name="notification_progress_tracker_height" /> <java-symbol type="dimen" name="notification_small_icon_size_low_ram"/> <java-symbol type="dimen" name="notification_big_picture_max_height_low_ram"/> diff --git a/core/tests/coretests/src/android/app/NotificationTest.java b/core/tests/coretests/src/android/app/NotificationTest.java index 0f73df92ca93..edcea241e620 100644 --- a/core/tests/coretests/src/android/app/NotificationTest.java +++ b/core/tests/coretests/src/android/app/NotificationTest.java @@ -97,7 +97,6 @@ import android.text.style.ForegroundColorSpan; import android.text.style.StyleSpan; import android.text.style.TextAppearanceSpan; import android.util.Pair; -import android.util.Slog; import android.widget.RemoteViews; import androidx.test.InstrumentationRegistry; @@ -118,6 +117,7 @@ import org.junit.Test; import org.junit.rules.TestRule; import org.junit.runner.RunWith; +import java.util.Collections; import java.util.List; import java.util.function.Consumer; @@ -2114,6 +2114,300 @@ public class NotificationTest { assertThat(n.getWhen()).isEqualTo(9); } + @Test + public void getNotificationStyleClass_forPlatformClassName_returnsPlatformClass() { + final List<Class<? extends Notification.Style>> platformStyleClasses = List.of( + Notification.BigTextStyle.class, Notification.BigPictureStyle.class, + Notification.MessagingStyle.class, Notification.CallStyle.class, + Notification.InboxStyle.class, Notification.MediaStyle.class, + Notification.DecoratedCustomViewStyle.class, + Notification.DecoratedMediaCustomViewStyle.class + ); + + for (Class<? extends Notification.Style> platformStyleClass : platformStyleClasses) { + assertThat(Notification.getNotificationStyleClass(platformStyleClass.getName())) + .isEqualTo(platformStyleClass); + } + } + + @Test + public void getNotificationStyleClass_forNotPlatformClassName_returnsNull() { + assertThat(Notification.getNotificationStyleClass(NotAPlatformStyle.class.getName())) + .isNull(); + } + + @Test + @EnableFlags(Flags.FLAG_API_RICH_ONGOING) + public void progressStyle_richOngoingEnabled_platformClass() { + assertThat( + Notification.getNotificationStyleClass(Notification.ProgressStyle.class.getName())) + .isEqualTo(Notification.ProgressStyle.class); + } + + @Test + @DisableFlags(Flags.FLAG_API_RICH_ONGOING) + public void progressStyle_richOngoingDisabled_notPlatformClass() { + assertThat( + Notification.getNotificationStyleClass(Notification.ProgressStyle.class.getName())) + .isNull(); + } + + @Test + @EnableFlags(Flags.FLAG_API_RICH_ONGOING) + public void progressStyle_onSegmentChange_visiblyDifferent() { + final Notification.Builder nProgress1 = new Notification.Builder(mContext, "test") + .setStyle(new Notification.ProgressStyle() + .addProgressSegment(new Notification.ProgressStyle.Segment(100)) + .addProgressSegment(new Notification.ProgressStyle.Segment(50) + .setColor(Color.RED))); + + final Notification.Builder nProgress2 = new Notification.Builder(mContext, "test") + .setStyle(new Notification.ProgressStyle() + .addProgressSegment(new Notification.ProgressStyle.Segment(100)) + .addProgressSegment(new Notification.ProgressStyle.Segment(50) + .setColor(Color.BLUE))); + + assertThat(Notification.areStyledNotificationsVisiblyDifferent(nProgress1, nProgress2)) + .isTrue(); + } + + @Test + @EnableFlags(Flags.FLAG_API_RICH_ONGOING) + public void indeterminateProgressStyle_onSegmentChange_visiblyNotDifferent() { + final Notification.Builder nProgress1 = new Notification.Builder(mContext, "test") + .setStyle(new Notification.ProgressStyle().setProgressIndeterminate(true) + .addProgressSegment(new Notification.ProgressStyle.Segment(100)) + .addProgressSegment(new Notification.ProgressStyle.Segment(50) + .setColor(Color.RED))); + + final Notification.Builder nProgress2 = new Notification.Builder(mContext, "test") + .setStyle(new Notification.ProgressStyle().setProgressIndeterminate(true) + .addProgressSegment(new Notification.ProgressStyle.Segment(100)) + .addProgressSegment(new Notification.ProgressStyle.Segment(50) + .setColor(Color.BLUE))); + + assertThat(Notification.areStyledNotificationsVisiblyDifferent(nProgress1, nProgress2)) + .isFalse(); + } + + @Test + @EnableFlags(Flags.FLAG_API_RICH_ONGOING) + public void progressStyle_onStartIconChange_visiblyDifferent() { + final Icon icon1 = Icon.createWithBitmap(BitmapFactory.decodeResource( + mContext.getResources(), com.android.frameworks.coretests.R.drawable.test128x96)); + + final Icon icon2 = Icon.createWithBitmap( + Bitmap.createBitmap(300, 300, Bitmap.Config.ARGB_8888)); + + final Notification.Builder nProgress1 = new Notification.Builder(mContext, "test") + .setStyle(new Notification.ProgressStyle().setProgressStartIcon(icon1)); + final Notification.Builder nProgress2 = new Notification.Builder(mContext, "test") + .setStyle(new Notification.ProgressStyle().setProgressStartIcon(icon2)); + + assertThat(Notification.areStyledNotificationsVisiblyDifferent(nProgress1, nProgress2)) + .isTrue(); + } + + @Test + @EnableFlags(Flags.FLAG_API_RICH_ONGOING) + public void progressStyle_onEndIconChange_visiblyDifferent() { + final Icon icon1 = Icon.createWithBitmap(BitmapFactory.decodeResource( + mContext.getResources(), com.android.frameworks.coretests.R.drawable.test128x96)); + + final Icon icon2 = Icon.createWithBitmap( + Bitmap.createBitmap(300, 300, Bitmap.Config.ARGB_8888)); + + final Notification.Builder nProgress1 = new Notification.Builder(mContext, "test") + .setStyle(new Notification.ProgressStyle().setProgressEndIcon(icon1)); + final Notification.Builder nProgress2 = new Notification.Builder(mContext, "test") + .setStyle(new Notification.ProgressStyle().setProgressEndIcon(icon2)); + + assertThat(Notification.areStyledNotificationsVisiblyDifferent(nProgress1, nProgress2)) + .isTrue(); + } + + @Test + @EnableFlags(Flags.FLAG_API_RICH_ONGOING) + public void progressStyle_onProgressChange_visiblyDifferent() { + final Notification.Builder nProgress1 = new Notification.Builder(mContext, "test") + .setStyle(new Notification.ProgressStyle().setProgress(20)); + final Notification.Builder nProgress2 = new Notification.Builder(mContext, "test") + .setStyle(new Notification.ProgressStyle().setProgress(21)); + + assertThat(Notification.areStyledNotificationsVisiblyDifferent(nProgress1, nProgress2)) + .isTrue(); + } + + @Test + @EnableFlags(Flags.FLAG_API_RICH_ONGOING) + public void indeterminateProgressStyle_onProgressChange_visiblyNotDifferent() { + final Notification.Builder nProgress1 = new Notification.Builder(mContext, "test") + .setStyle(new Notification.ProgressStyle() + .setProgressIndeterminate(true).setProgress(20)); + final Notification.Builder nProgress2 = new Notification.Builder(mContext, "test") + .setStyle(new Notification.ProgressStyle() + .setProgressIndeterminate(true).setProgress(21)); + + assertThat(Notification.areStyledNotificationsVisiblyDifferent(nProgress1, nProgress2)) + .isFalse(); + } + + @Test + @EnableFlags(Flags.FLAG_API_RICH_ONGOING) + public void progressStyle_onIsStyledByProgressChange_visiblyDifferent() { + final Notification.Builder nProgress1 = new Notification.Builder(mContext, "test") + .setStyle(new Notification.ProgressStyle().setStyledByProgress(true)); + final Notification.Builder nProgress2 = new Notification.Builder(mContext, "test") + .setStyle(new Notification.ProgressStyle().setStyledByProgress(false)); + + assertThat(Notification.areStyledNotificationsVisiblyDifferent(nProgress1, nProgress2)) + .isTrue(); + } + + @Test + @EnableFlags(Flags.FLAG_API_RICH_ONGOING) + public void indeterminateProgressStyle_onIsStyledByProgressChange_visiblyNotDifferent() { + final Notification.Builder nProgress1 = new Notification.Builder(mContext, "test") + .setStyle(new Notification.ProgressStyle() + .setProgressIndeterminate(true).setStyledByProgress(true)); + final Notification.Builder nProgress2 = new Notification.Builder(mContext, "test") + .setStyle(new Notification.ProgressStyle() + .setProgressIndeterminate(true).setStyledByProgress(false)); + + assertThat(Notification.areStyledNotificationsVisiblyDifferent(nProgress1, nProgress2)) + .isFalse(); + } + + @Test + @EnableFlags(Flags.FLAG_API_RICH_ONGOING) + public void progressStyle_onProgressStepChange_visiblyDifferent() { + final Notification.Builder nProgress1 = new Notification.Builder(mContext, "test") + .setStyle(new Notification.ProgressStyle() + .addProgressStep(new Notification.ProgressStyle.Step(10))); + final Notification.Builder nProgress2 = new Notification.Builder(mContext, "test") + .setStyle(new Notification.ProgressStyle() + .addProgressStep(new Notification.ProgressStyle.Step(12))); + + assertThat(Notification.areStyledNotificationsVisiblyDifferent(nProgress1, nProgress2)) + .isTrue(); + } + + @Test + @EnableFlags(Flags.FLAG_API_RICH_ONGOING) + public void indeterminateProgressStyle_onProgressStepChange_visiblyNotDifferent() { + final Notification.Builder nProgress1 = new Notification.Builder(mContext, "test") + .setStyle(new Notification.ProgressStyle().setProgressIndeterminate(true) + .addProgressStep(new Notification.ProgressStyle.Step(10))); + final Notification.Builder nProgress2 = new Notification.Builder(mContext, "test") + .setStyle(new Notification.ProgressStyle().setProgressIndeterminate(true) + .addProgressStep(new Notification.ProgressStyle.Step(12))); + + assertThat(Notification.areStyledNotificationsVisiblyDifferent(nProgress1, nProgress2)) + .isFalse(); + } + + @Test + @EnableFlags(Flags.FLAG_API_RICH_ONGOING) + public void progressStyle_onTrackerIconChange_visiblyDifferent() { + final Icon icon1 = Icon.createWithBitmap(BitmapFactory.decodeResource( + mContext.getResources(), com.android.frameworks.coretests.R.drawable.test128x96)); + + final Icon icon2 = Icon.createWithBitmap( + Bitmap.createBitmap(300, 300, Bitmap.Config.ARGB_8888)); + + final Notification.Builder nProgress1 = new Notification.Builder(mContext, "test") + .setStyle(new Notification.ProgressStyle().setProgressTrackerIcon(icon1)); + final Notification.Builder nProgress2 = new Notification.Builder(mContext, "test") + .setStyle(new Notification.ProgressStyle().setProgressTrackerIcon(icon2)); + + assertThat(Notification.areStyledNotificationsVisiblyDifferent(nProgress1, nProgress2)) + .isTrue(); + } + + @Test + @EnableFlags(Flags.FLAG_API_RICH_ONGOING) + public void indeterminateProgressStyle_onTrackerIconChange_visiblyNotDifferent() { + final Icon icon1 = Icon.createWithBitmap(BitmapFactory.decodeResource( + mContext.getResources(), com.android.frameworks.coretests.R.drawable.test128x96)); + + final Icon icon2 = Icon.createWithBitmap( + Bitmap.createBitmap(300, 300, Bitmap.Config.ARGB_8888)); + + final Notification.Builder nProgress1 = new Notification.Builder(mContext, "test") + .setStyle(new Notification.ProgressStyle().setProgressIndeterminate(true) + .setProgressTrackerIcon(icon1)); + final Notification.Builder nProgress2 = new Notification.Builder(mContext, "test") + .setStyle(new Notification.ProgressStyle() + .setProgressIndeterminate(true).setProgressTrackerIcon(icon2)); + + assertThat(Notification.areStyledNotificationsVisiblyDifferent(nProgress1, nProgress2)) + .isFalse(); + } + + @Test + @EnableFlags(Flags.FLAG_API_RICH_ONGOING) + public void progressStyle_onIndeterminateChange_visiblyDifferent() { + final Notification.Builder nProgress1 = new Notification.Builder(mContext, "test") + .setStyle(new Notification.ProgressStyle().setProgressIndeterminate(true)); + final Notification.Builder nProgress2 = new Notification.Builder(mContext, "test") + .setStyle(new Notification.ProgressStyle().setProgressIndeterminate(false)); + + assertThat(Notification.areStyledNotificationsVisiblyDifferent(nProgress1, nProgress2)) + .isTrue(); + } + + @Test + @EnableFlags(Flags.FLAG_API_RICH_ONGOING) + public void progressStyle_getProgressMax_default100() { + final Notification.ProgressStyle progressStyle = new Notification.ProgressStyle(); + assertThat(progressStyle.getProgressMax()).isEqualTo(100); + } + + @Test + @EnableFlags(Flags.FLAG_API_RICH_ONGOING) + public void progressStyle_getProgressMax_nooSegments_returnsDefault() { + final Notification.ProgressStyle progressStyle = new Notification.ProgressStyle(); + progressStyle.setProgressSegments(Collections.emptyList()); + assertThat(progressStyle.getProgressMax()).isEqualTo(100); + } + + @Test + @EnableFlags(Flags.FLAG_API_RICH_ONGOING) + public void progressStyle_getProgressMax_returnsSumOfSegmentLength() { + final Notification.ProgressStyle progressStyle = new Notification.ProgressStyle(); + progressStyle + .addProgressSegment(new Notification.ProgressStyle.Segment(10)) + .addProgressSegment(new Notification.ProgressStyle.Segment(20)); + + assertThat(progressStyle.getProgressMax()).isEqualTo(30); + } + + @Test + @EnableFlags(Flags.FLAG_API_RICH_ONGOING) + public void progressStyle_getProgressMax_onSegmentOverflow_returnsDefault() { + final Notification.ProgressStyle progressStyle = new Notification.ProgressStyle(); + progressStyle + .addProgressSegment(new Notification.ProgressStyle.Segment(Integer.MAX_VALUE)) + .addProgressSegment(new Notification.ProgressStyle.Segment(10)); + + assertThat(progressStyle.getProgressMax()).isEqualTo(100); + } + + @Test + @EnableFlags(Flags.FLAG_API_RICH_ONGOING) + public void progressStyle_indeterminate_defaultValueFalse() { + final Notification.ProgressStyle progressStyle1 = new Notification.ProgressStyle(); + + assertThat(progressStyle1.isProgressIndeterminate()).isFalse(); + } + + @Test + @EnableFlags(Flags.FLAG_API_RICH_ONGOING) + public void progressStyle_styledByProgress_defaultValueTrue() { + final Notification.ProgressStyle progressStyle1 = new Notification.ProgressStyle(); + + assertThat(progressStyle1.isStyledByProgress()).isTrue(); + } private void assertValid(Notification.Colors c) { // Assert that all colors are populated assertThat(c.getBackgroundColor()).isNotEqualTo(Notification.COLOR_INVALID); @@ -2214,4 +2508,11 @@ public class NotificationTest { new Intent(action).setPackage(mContext.getPackageName()), PendingIntent.FLAG_MUTABLE); } + + private static class NotAPlatformStyle extends Notification.Style { + @Override + public boolean areNotificationsVisiblyDifferent(Notification.Style other) { + return false; + } + } } diff --git a/core/tests/coretests/src/android/view/WindowLayoutTests.java b/core/tests/coretests/src/android/view/WindowLayoutTests.java index 5cac98daee80..d4693e6e7130 100644 --- a/core/tests/coretests/src/android/view/WindowLayoutTests.java +++ b/core/tests/coretests/src/android/view/WindowLayoutTests.java @@ -413,4 +413,19 @@ public class WindowLayoutTests { assertInsetByTopBottom(0, 0, mFrames.parentFrame); assertInsetByTopBottom(0, 0, mFrames.frame); } + + @Test + public void windowBoundsOutsideDisplayCutoutSafe() { + addDisplayCutout(); + mAttrs.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER; + mWindowBounds.set(0, -1000, DISPLAY_WIDTH, 0); + computeFrames(); + + assertRect(WATERFALL_INSETS.left, 0, DISPLAY_WIDTH - WATERFALL_INSETS.right, 0, + mFrames.displayFrame); + assertRect(WATERFALL_INSETS.left, 0, DISPLAY_WIDTH - WATERFALL_INSETS.right, 0, + mFrames.parentFrame); + assertRect(WATERFALL_INSETS.left, 0, DISPLAY_WIDTH - WATERFALL_INSETS.right, 0, + mFrames.frame); + } } diff --git a/libs/WindowManager/Jetpack/src/androidx/window/common/DeviceStateManagerFoldingFeatureProducer.java b/libs/WindowManager/Jetpack/src/androidx/window/common/DeviceStateManagerFoldingFeatureProducer.java index 37f0067de453..089613853555 100644 --- a/libs/WindowManager/Jetpack/src/androidx/window/common/DeviceStateManagerFoldingFeatureProducer.java +++ b/libs/WindowManager/Jetpack/src/androidx/window/common/DeviceStateManagerFoldingFeatureProducer.java @@ -16,12 +16,11 @@ package androidx.window.common; -import static android.hardware.devicestate.DeviceStateManager.INVALID_DEVICE_STATE_IDENTIFIER; +import static android.hardware.devicestate.DeviceStateManager.INVALID_DEVICE_STATE; import static androidx.window.common.layout.CommonFoldingFeature.COMMON_STATE_UNKNOWN; import static androidx.window.common.layout.CommonFoldingFeature.parseListFromString; -import android.annotation.NonNull; import android.content.Context; import android.hardware.devicestate.DeviceState; import android.hardware.devicestate.DeviceStateManager; @@ -31,16 +30,23 @@ import android.text.TextUtils; import android.util.Log; import android.util.SparseIntArray; +import androidx.annotation.BinderThread; +import androidx.annotation.GuardedBy; +import androidx.annotation.MainThread; +import androidx.annotation.NonNull; +import androidx.annotation.VisibleForTesting; import androidx.window.common.layout.CommonFoldingFeature; import androidx.window.common.layout.DisplayFoldFeatureCommon; import com.android.internal.R; +import com.android.window.flags.Flags; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Objects; import java.util.Optional; +import java.util.concurrent.Executor; import java.util.function.Consumer; /** @@ -55,13 +61,6 @@ public final class DeviceStateManagerFoldingFeatureProducer private static final boolean DEBUG = false; /** - * Emulated device state - * {@link DeviceStateManager.DeviceStateCallback#onDeviceStateChanged(DeviceState)} to - * {@link CommonFoldingFeature.State} map. - */ - private final SparseIntArray mDeviceStateToPostureMap = new SparseIntArray(); - - /** * Device state received via * {@link DeviceStateManager.DeviceStateCallback#onDeviceStateChanged(DeviceState)}. * The identifier returned through {@link DeviceState#getIdentifier()} may not correspond 1:1 @@ -71,23 +70,40 @@ public final class DeviceStateManagerFoldingFeatureProducer * "rear display". Concurrent mode for example is activated via public API and can be active in * both the "open" and "half folded" device states. */ - private DeviceState mCurrentDeviceState = new DeviceState( - new DeviceState.Configuration.Builder(INVALID_DEVICE_STATE_IDENTIFIER, - "INVALID").build()); + // TODO: b/337820752 - Add @GuardedBy("mCurrentDeviceStateLock") after flag cleanup. + private DeviceState mCurrentDeviceState = INVALID_DEVICE_STATE; - private List<DeviceState> mSupportedStates; + /** + * Lock to synchronize access to {@link #mCurrentDeviceState}. + * + * <p>This lock is used to ensure thread-safety when accessing and modifying the + * {@link #mCurrentDeviceState} field. It is acquired by both the binder thread (if + * {@link Flags#wlinfoOncreate()} is enabled) and the main thread (if + * {@link Flags#wlinfoOncreate()} is disabled) to prevent race conditions and + * ensure data consistency. + */ + private final Object mCurrentDeviceStateLock = new Object(); @NonNull private final RawFoldingFeatureProducer mRawFoldSupplier; - private final boolean mIsHalfOpenedSupported; - - private final DeviceStateCallback mDeviceStateCallback = new DeviceStateCallback() { + @NonNull + private final DeviceStateMapper mDeviceStateMapper; + + @VisibleForTesting + final DeviceStateCallback mDeviceStateCallback = new DeviceStateCallback() { + // The GuardedBy analysis is intra-procedural, meaning it doesn’t consider the getData() + // implementation. See https://errorprone.info/bugpattern/GuardedBy for limitations. + @SuppressWarnings("GuardedBy") + @BinderThread // When Flags.wlinfoOncreate() is enabled. + @MainThread // When Flags.wlinfoOncreate() is disabled. @Override public void onDeviceStateChanged(@NonNull DeviceState state) { - mCurrentDeviceState = state; - mRawFoldSupplier.getData(DeviceStateManagerFoldingFeatureProducer - .this::notifyFoldingFeatureChange); + synchronized (mCurrentDeviceStateLock) { + mCurrentDeviceState = state; + mRawFoldSupplier.getData(DeviceStateManagerFoldingFeatureProducer.this + ::notifyFoldingFeatureChangeLocked); + } } }; @@ -95,41 +111,14 @@ public final class DeviceStateManagerFoldingFeatureProducer @NonNull RawFoldingFeatureProducer rawFoldSupplier, @NonNull DeviceStateManager deviceStateManager) { mRawFoldSupplier = rawFoldSupplier; - String[] deviceStatePosturePairs = context.getResources() - .getStringArray(R.array.config_device_state_postures); - mSupportedStates = deviceStateManager.getSupportedDeviceStates(); - boolean isHalfOpenedSupported = false; - for (String deviceStatePosturePair : deviceStatePosturePairs) { - String[] deviceStatePostureMapping = deviceStatePosturePair.split(":"); - if (deviceStatePostureMapping.length != 2) { - if (DEBUG) { - Log.e(TAG, "Malformed device state posture pair: " - + deviceStatePosturePair); - } - continue; - } + mDeviceStateMapper = + new DeviceStateMapper(context, deviceStateManager.getSupportedDeviceStates()); - int deviceState; - int posture; - try { - deviceState = Integer.parseInt(deviceStatePostureMapping[0]); - posture = Integer.parseInt(deviceStatePostureMapping[1]); - } catch (NumberFormatException e) { - if (DEBUG) { - Log.e(TAG, "Failed to parse device state or posture: " - + deviceStatePosturePair, - e); - } - continue; - } - isHalfOpenedSupported = isHalfOpenedSupported - || posture == CommonFoldingFeature.COMMON_STATE_HALF_OPENED; - mDeviceStateToPostureMap.put(deviceState, posture); - } - mIsHalfOpenedSupported = isHalfOpenedSupported; - if (mDeviceStateToPostureMap.size() > 0) { + if (!mDeviceStateMapper.isDeviceStateToPostureMapEmpty()) { + final Executor executor = + Flags.wlinfoOncreate() ? Runnable::run : context.getMainExecutor(); Objects.requireNonNull(deviceStateManager) - .registerCallback(context.getMainExecutor(), mDeviceStateCallback); + .registerCallback(executor, mDeviceStateCallback); } } @@ -137,50 +126,51 @@ public final class DeviceStateManagerFoldingFeatureProducer * Add a callback to mCallbacks if there is no device state. This callback will be run * once a device state is set. Otherwise,run the callback immediately. */ - private void runCallbackWhenValidState(@NonNull Consumer<List<CommonFoldingFeature>> callback, - String displayFeaturesString) { - if (isCurrentStateValid()) { - callback.accept(calculateFoldingFeature(displayFeaturesString)); + private void runCallbackWhenValidState(@NonNull DeviceState state, + @NonNull Consumer<List<CommonFoldingFeature>> callback, + @NonNull String displayFeaturesString) { + if (mDeviceStateMapper.isDeviceStateValid(state)) { + callback.accept(calculateFoldingFeature(state, displayFeaturesString)); } else { // This callback will be added to mCallbacks and removed once it runs once. - AcceptOnceConsumer<List<CommonFoldingFeature>> singleRunCallback = + final AcceptOnceConsumer<List<CommonFoldingFeature>> singleRunCallback = new AcceptOnceConsumer<>(this, callback); addDataChangedCallback(singleRunCallback); } } - /** - * Checks to find {@link DeviceStateManagerFoldingFeatureProducer#mCurrentDeviceState} in the - * {@link DeviceStateManagerFoldingFeatureProducer#mDeviceStateToPostureMap} which was - * initialized in the constructor of {@link DeviceStateManagerFoldingFeatureProducer}. - * Returns a boolean value of whether the device state is valid. - */ - private boolean isCurrentStateValid() { - // If the device state is not found in the map, indexOfKey returns a negative number. - return mDeviceStateToPostureMap.indexOfKey(mCurrentDeviceState.getIdentifier()) >= 0; - } - + // The GuardedBy analysis is intra-procedural, meaning it doesn’t consider the implementation of + // addDataChangedCallback(). See https://errorprone.info/bugpattern/GuardedBy for limitations. + @SuppressWarnings("GuardedBy") @Override protected void onListenersChanged() { super.onListenersChanged(); - if (hasListeners()) { - mRawFoldSupplier.addDataChangedCallback(this::notifyFoldingFeatureChange); - } else { - mCurrentDeviceState = new DeviceState( - new DeviceState.Configuration.Builder(INVALID_DEVICE_STATE_IDENTIFIER, - "INVALID").build()); - mRawFoldSupplier.removeDataChangedCallback(this::notifyFoldingFeatureChange); + synchronized (mCurrentDeviceStateLock) { + if (hasListeners()) { + mRawFoldSupplier.addDataChangedCallback(this::notifyFoldingFeatureChangeLocked); + } else { + mCurrentDeviceState = INVALID_DEVICE_STATE; + mRawFoldSupplier.removeDataChangedCallback(this::notifyFoldingFeatureChangeLocked); + } + } + } + + @NonNull + private DeviceState getCurrentDeviceState() { + synchronized (mCurrentDeviceStateLock) { + return mCurrentDeviceState; } } @NonNull @Override public Optional<List<CommonFoldingFeature>> getCurrentData() { - Optional<String> displayFeaturesString = mRawFoldSupplier.getCurrentData(); - if (!isCurrentStateValid()) { + final Optional<String> displayFeaturesString = mRawFoldSupplier.getCurrentData(); + final DeviceState state = getCurrentDeviceState(); + if (!mDeviceStateMapper.isDeviceStateValid(state) || displayFeaturesString.isEmpty()) { return Optional.empty(); } else { - return displayFeaturesString.map(this::calculateFoldingFeature); + return Optional.of(calculateFoldingFeature(state, displayFeaturesString.get())); } } @@ -191,7 +181,7 @@ public final class DeviceStateManagerFoldingFeatureProducer */ @NonNull public List<CommonFoldingFeature> getFoldsWithUnknownState() { - Optional<String> optionalFoldingFeatureString = mRawFoldSupplier.getCurrentData(); + final Optional<String> optionalFoldingFeatureString = mRawFoldSupplier.getCurrentData(); if (optionalFoldingFeatureString.isPresent()) { return CommonFoldingFeature.parseListFromString( @@ -201,7 +191,6 @@ public final class DeviceStateManagerFoldingFeatureProducer return Collections.emptyList(); } - /** * Returns the list of supported {@link DisplayFoldFeatureCommon} calculated from the * {@link DeviceStateManagerFoldingFeatureProducer}. @@ -218,16 +207,16 @@ public final class DeviceStateManagerFoldingFeatureProducer return foldFeatures; } - /** * Returns {@code true} if the device supports half-opened mode, {@code false} otherwise. */ public boolean isHalfOpenedSupported() { - return mIsHalfOpenedSupported; + return mDeviceStateMapper.mIsHalfOpenedSupported; } /** * Adds the data to the storeFeaturesConsumer when the data is ready. + * * @param storeFeaturesConsumer a consumer to collect the data when it is first available. */ @Override @@ -236,38 +225,123 @@ public final class DeviceStateManagerFoldingFeatureProducer if (TextUtils.isEmpty(displayFeaturesString)) { storeFeaturesConsumer.accept(new ArrayList<>()); } else { - runCallbackWhenValidState(storeFeaturesConsumer, displayFeaturesString); + final DeviceState state = getCurrentDeviceState(); + runCallbackWhenValidState(state, storeFeaturesConsumer, displayFeaturesString); } }); } - private void notifyFoldingFeatureChange(String displayFeaturesString) { - if (!isCurrentStateValid()) { + @GuardedBy("mCurrentDeviceStateLock") + private void notifyFoldingFeatureChangeLocked(String displayFeaturesString) { + final DeviceState state = mCurrentDeviceState; + if (!mDeviceStateMapper.isDeviceStateValid(state)) { return; } if (TextUtils.isEmpty(displayFeaturesString)) { notifyDataChanged(new ArrayList<>()); } else { - notifyDataChanged(calculateFoldingFeature(displayFeaturesString)); + notifyDataChanged(calculateFoldingFeature(state, displayFeaturesString)); } } - private List<CommonFoldingFeature> calculateFoldingFeature(String displayFeaturesString) { - return parseListFromString(displayFeaturesString, currentHingeState()); + @NonNull + private List<CommonFoldingFeature> calculateFoldingFeature(@NonNull DeviceState deviceState, + @NonNull String displayFeaturesString) { + @CommonFoldingFeature.State + final int hingeState = mDeviceStateMapper.getHingeState(deviceState); + return parseListFromString(displayFeaturesString, hingeState); } - @CommonFoldingFeature.State - private int currentHingeState() { - @CommonFoldingFeature.State - int posture = mDeviceStateToPostureMap.get(mCurrentDeviceState.getIdentifier(), - COMMON_STATE_UNKNOWN); + /** + * Internal class to map device states to corresponding postures. + * + * <p>This class encapsulates the logic for mapping device states to postures. The mapping is + * immutable after initialization to ensure thread safety. + */ + private static class DeviceStateMapper { + /** + * Emulated device state + * {@link DeviceStateManager.DeviceStateCallback#onDeviceStateChanged(DeviceState)} to + * {@link CommonFoldingFeature.State} map. + * + * <p>This map must be immutable after initialization to ensure thread safety, as it may be + * accessed from multiple threads. Modifications should only occur during object + * construction. + */ + private final SparseIntArray mDeviceStateToPostureMap = new SparseIntArray(); + + /** + * The list of device states that are supported. + * + * <p>This list must be immutable after initialization to ensure thread safety. + */ + @NonNull + private final List<DeviceState> mSupportedStates; + + final boolean mIsHalfOpenedSupported; + + DeviceStateMapper(@NonNull Context context, @NonNull List<DeviceState> supportedStates) { + mSupportedStates = supportedStates; + + final String[] deviceStatePosturePairs = context.getResources() + .getStringArray(R.array.config_device_state_postures); + boolean isHalfOpenedSupported = false; + for (String deviceStatePosturePair : deviceStatePosturePairs) { + final String[] deviceStatePostureMapping = deviceStatePosturePair.split(":"); + if (deviceStatePostureMapping.length != 2) { + if (DEBUG) { + Log.e(TAG, "Malformed device state posture pair: " + + deviceStatePosturePair); + } + continue; + } - if (posture == CommonFoldingFeature.COMMON_STATE_USE_BASE_STATE) { - posture = mDeviceStateToPostureMap.get( - DeviceStateUtil.calculateBaseStateIdentifier(mCurrentDeviceState, - mSupportedStates), COMMON_STATE_UNKNOWN); + final int deviceState; + final int posture; + try { + deviceState = Integer.parseInt(deviceStatePostureMapping[0]); + posture = Integer.parseInt(deviceStatePostureMapping[1]); + } catch (NumberFormatException e) { + if (DEBUG) { + Log.e(TAG, "Failed to parse device state or posture: " + + deviceStatePosturePair, + e); + } + continue; + } + isHalfOpenedSupported = isHalfOpenedSupported + || posture == CommonFoldingFeature.COMMON_STATE_HALF_OPENED; + mDeviceStateToPostureMap.put(deviceState, posture); + } + mIsHalfOpenedSupported = isHalfOpenedSupported; + } + + boolean isDeviceStateToPostureMapEmpty() { + return mDeviceStateToPostureMap.size() == 0; + } + + /** + * Validates if the provided deviceState exists in the {@link #mDeviceStateToPostureMap} + * which was initialized in the constructor of {@link DeviceStateMapper}. + * Returns a boolean value of whether the device state is valid. + */ + boolean isDeviceStateValid(@NonNull DeviceState deviceState) { + // If the device state is not found in the map, indexOfKey returns a negative number. + return mDeviceStateToPostureMap.indexOfKey(deviceState.getIdentifier()) >= 0; } - return posture; + @CommonFoldingFeature.State + int getHingeState(@NonNull DeviceState deviceState) { + @CommonFoldingFeature.State + final int posture = + mDeviceStateToPostureMap.get(deviceState.getIdentifier(), COMMON_STATE_UNKNOWN); + if (posture != CommonFoldingFeature.COMMON_STATE_USE_BASE_STATE) { + return posture; + } + + final int baseStateIdentifier = + DeviceStateUtil.calculateBaseStateIdentifier(deviceState, mSupportedStates); + return mDeviceStateToPostureMap.get(baseStateIdentifier, COMMON_STATE_UNKNOWN); + } } } diff --git a/libs/WindowManager/Jetpack/src/androidx/window/extensions/embedding/TaskContainer.java b/libs/WindowManager/Jetpack/src/androidx/window/extensions/embedding/TaskContainer.java index 74cce68f270b..dcc2d93060c9 100644 --- a/libs/WindowManager/Jetpack/src/androidx/window/extensions/embedding/TaskContainer.java +++ b/libs/WindowManager/Jetpack/src/androidx/window/extensions/embedding/TaskContainer.java @@ -377,8 +377,16 @@ class TaskContainer { @Nullable TaskFragmentContainer getContainerWithActivity(@NonNull IBinder activityToken) { - return getContainer(container -> container.hasAppearedActivity(activityToken) - || container.hasPendingAppearedActivity(activityToken)); + // When the new activity is launched to the topmost TF because the source activity + // was in that TF, and the source activity is finished before resolving the new activity, + // we will try to see if the new activity match a rule with the split activities below. + // If matched, it can be reparented. + final TaskFragmentContainer taskFragmentContainer + = getContainer(container -> container.hasPendingAppearedActivity(activityToken)); + if (taskFragmentContainer != null) { + return taskFragmentContainer; + } + return getContainer(container -> container.hasAppearedActivity(activityToken)); } @Nullable diff --git a/libs/WindowManager/Jetpack/tests/unittest/Android.bp b/libs/WindowManager/Jetpack/tests/unittest/Android.bp index bd430c0e610b..09185ee203b8 100644 --- a/libs/WindowManager/Jetpack/tests/unittest/Android.bp +++ b/libs/WindowManager/Jetpack/tests/unittest/Android.bp @@ -29,6 +29,7 @@ android_test { srcs: [ "**/*.java", + "**/*.kt", ], static_libs: [ @@ -41,6 +42,7 @@ android_test { "androidx.test.ext.junit", "flag-junit", "mockito-target-extended-minus-junit4", + "mockito-kotlin-nodeps", "truth", "testables", "platform-test-annotations", diff --git a/libs/WindowManager/Jetpack/tests/unittest/src/androidx/window/common/DeviceStateManagerFoldingFeatureProducerTest.kt b/libs/WindowManager/Jetpack/tests/unittest/src/androidx/window/common/DeviceStateManagerFoldingFeatureProducerTest.kt new file mode 100644 index 000000000000..90887a747a6f --- /dev/null +++ b/libs/WindowManager/Jetpack/tests/unittest/src/androidx/window/common/DeviceStateManagerFoldingFeatureProducerTest.kt @@ -0,0 +1,341 @@ +/* + * 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 androidx.window.common + +import android.content.Context +import android.content.res.Resources +import android.hardware.devicestate.DeviceState +import android.hardware.devicestate.DeviceStateManager +import android.platform.test.annotations.DisableFlags +import android.platform.test.annotations.EnableFlags +import android.platform.test.flag.junit.SetFlagsRule +import androidx.test.ext.junit.runners.AndroidJUnit4 +import androidx.window.common.layout.CommonFoldingFeature +import androidx.window.common.layout.CommonFoldingFeature.COMMON_STATE_FLAT +import androidx.window.common.layout.CommonFoldingFeature.COMMON_STATE_HALF_OPENED +import androidx.window.common.layout.CommonFoldingFeature.COMMON_STATE_NO_FOLDING_FEATURES +import androidx.window.common.layout.CommonFoldingFeature.COMMON_STATE_UNKNOWN +import androidx.window.common.layout.CommonFoldingFeature.COMMON_STATE_USE_BASE_STATE +import androidx.window.common.layout.DisplayFoldFeatureCommon +import androidx.window.common.layout.DisplayFoldFeatureCommon.DISPLAY_FOLD_FEATURE_PROPERTY_SUPPORTS_HALF_OPENED +import androidx.window.common.layout.DisplayFoldFeatureCommon.DISPLAY_FOLD_FEATURE_TYPE_SCREEN_FOLD_IN +import com.android.internal.R +import com.android.window.flags.Flags +import com.google.common.truth.Truth.assertThat +import java.util.Optional +import java.util.concurrent.Executor +import java.util.function.Consumer +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.ArgumentCaptor +import org.mockito.kotlin.any +import org.mockito.kotlin.doAnswer +import org.mockito.kotlin.doReturn +import org.mockito.kotlin.eq +import org.mockito.kotlin.mock +import org.mockito.kotlin.never +import org.mockito.kotlin.stub +import org.mockito.kotlin.verify + +/** + * Test class for [DeviceStateManagerFoldingFeatureProducer]. + * + * Build/Install/Run: + * atest WMJetpackUnitTests:DeviceStateManagerFoldingFeatureProducerTest + */ +@RunWith(AndroidJUnit4::class) +class DeviceStateManagerFoldingFeatureProducerTest { + @get:Rule + val setFlagsRule: SetFlagsRule = SetFlagsRule() + + private val mMockDeviceStateManager = mock<DeviceStateManager>() + private val mMockResources = mock<Resources> { + on { getStringArray(R.array.config_device_state_postures) } doReturn DEVICE_STATE_POSTURES + } + private val mMockContext = mock<Context> { + on { resources } doReturn mMockResources + } + private val mRawFoldSupplier = mock<RawFoldingFeatureProducer> { + on { currentData } doReturn Optional.of(DISPLAY_FEATURES) + on { getData(any<Consumer<String>>()) } doAnswer { invocation -> + val callback = invocation.getArgument(0) as Consumer<String> + callback.accept(DISPLAY_FEATURES) + } + } + + @Test + @DisableFlags(Flags.FLAG_WLINFO_ONCREATE) + fun testRegisterCallback_whenWlinfoOncreateIsDisabled_usesMainExecutor() { + DeviceStateManagerFoldingFeatureProducer( + mMockContext, + mRawFoldSupplier, + mMockDeviceStateManager, + ) + + verify(mMockDeviceStateManager).registerCallback(eq(mMockContext.mainExecutor), any()) + } + + @Test + @EnableFlags(Flags.FLAG_WLINFO_ONCREATE) + fun testRegisterCallback_whenWlinfoOncreateIsEnabled_usesRunnableRun() { + val executorCaptor = ArgumentCaptor.forClass(Executor::class.java) + val runnable = mock<Runnable>() + + DeviceStateManagerFoldingFeatureProducer( + mMockContext, + mRawFoldSupplier, + mMockDeviceStateManager, + ) + + verify(mMockDeviceStateManager).registerCallback(executorCaptor.capture(), any()) + executorCaptor.value.execute(runnable) + verify(runnable).run() + } + + @Test + fun testGetCurrentData_validCurrentState_returnsFoldingFeatureWithState() { + val ffp = DeviceStateManagerFoldingFeatureProducer( + mMockContext, + mRawFoldSupplier, + mMockDeviceStateManager, + ) + ffp.mDeviceStateCallback.onDeviceStateChanged(DEVICE_STATE_HALF_OPENED) + + val currentData = ffp.getCurrentData() + + assertThat(currentData).isPresent() + assertThat(currentData.get()).containsExactlyElementsIn(HALF_OPENED_FOLDING_FEATURES) + } + + @Test + fun testGetCurrentData_invalidCurrentState_returnsEmptyOptionalFoldingFeature() { + val ffp = DeviceStateManagerFoldingFeatureProducer( + mMockContext, + mRawFoldSupplier, + mMockDeviceStateManager, + ) + + val currentData = ffp.getCurrentData() + + assertThat(currentData).isEmpty() + } + + @Test + fun testGetFoldsWithUnknownState_validFoldingFeature_returnsFoldingFeaturesWithUnknownState() { + val ffp = DeviceStateManagerFoldingFeatureProducer( + mMockContext, + mRawFoldSupplier, + mMockDeviceStateManager, + ) + + val result = ffp.getFoldsWithUnknownState() + + assertThat(result).containsExactlyElementsIn(UNKNOWN_STATE_FOLDING_FEATURES) + } + + @Test + fun testGetFoldsWithUnknownState_emptyFoldingFeature_returnsEmptyList() { + mRawFoldSupplier.stub { + on { currentData } doReturn Optional.empty() + } + val ffp = DeviceStateManagerFoldingFeatureProducer( + mMockContext, + mRawFoldSupplier, + mMockDeviceStateManager, + ) + + val result = ffp.getFoldsWithUnknownState() + + assertThat(result).isEmpty() + } + + @Test + fun testGetDisplayFeatures_validFoldingFeature_returnsDisplayFoldFeatures() { + mRawFoldSupplier.stub { + on { currentData } doReturn Optional.of(DISPLAY_FEATURES_HALF_OPENED_HINGE) + } + val ffp = DeviceStateManagerFoldingFeatureProducer( + mMockContext, + mRawFoldSupplier, + mMockDeviceStateManager, + ) + + val result = ffp.displayFeatures + + assertThat(result).containsExactly( + DisplayFoldFeatureCommon( + DISPLAY_FOLD_FEATURE_TYPE_SCREEN_FOLD_IN, + setOf(DISPLAY_FOLD_FEATURE_PROPERTY_SUPPORTS_HALF_OPENED), + ), + ) + } + + @Test + fun testIsHalfOpenedSupported_withHalfOpenedPostures_returnsTrue() { + val ffp = DeviceStateManagerFoldingFeatureProducer( + mMockContext, + mRawFoldSupplier, + mMockDeviceStateManager, + ) + + assertThat(ffp.isHalfOpenedSupported).isTrue() + } + + @Test + fun testIsHalfOpenedSupported_withEmptyPostures_returnsFalse() { + mMockResources.stub { + on { getStringArray(R.array.config_device_state_postures) } doReturn emptyArray() + } + val ffp = DeviceStateManagerFoldingFeatureProducer( + mMockContext, + mRawFoldSupplier, + mMockDeviceStateManager, + ) + + assertThat(ffp.isHalfOpenedSupported).isFalse() + } + + @Test + fun testGetData_emptyDisplayFeaturesString_callsConsumerWithEmptyList() { + mRawFoldSupplier.stub { + on { getData(any<Consumer<String>>()) } doAnswer { invocation -> + val callback = invocation.getArgument(0) as Consumer<String> + callback.accept("") + } + } + val ffp = DeviceStateManagerFoldingFeatureProducer( + mMockContext, + mRawFoldSupplier, + mMockDeviceStateManager, + ) + val storeFeaturesConsumer = mock<Consumer<List<CommonFoldingFeature>>>() + + ffp.getData(storeFeaturesConsumer) + + verify(storeFeaturesConsumer).accept(emptyList()) + } + + @Test + fun testGetData_validState_callsConsumerWithFoldingFeatures() { + val ffp = DeviceStateManagerFoldingFeatureProducer( + mMockContext, + mRawFoldSupplier, + mMockDeviceStateManager, + ) + ffp.mDeviceStateCallback.onDeviceStateChanged(DEVICE_STATE_HALF_OPENED) + val storeFeaturesConsumer = mock<Consumer<List<CommonFoldingFeature>>>() + + ffp.getData(storeFeaturesConsumer) + + verify(storeFeaturesConsumer).accept(HALF_OPENED_FOLDING_FEATURES) + } + + @Test + fun testGetData_invalidState_addsAcceptOnceConsumerToDataChangedCallback() { + val ffp = DeviceStateManagerFoldingFeatureProducer( + mMockContext, + mRawFoldSupplier, + mMockDeviceStateManager, + ) + val storeFeaturesConsumer = mock<Consumer<List<CommonFoldingFeature>>>() + + ffp.getData(storeFeaturesConsumer) + + verify(storeFeaturesConsumer, never()).accept(any()) + ffp.mDeviceStateCallback.onDeviceStateChanged(DEVICE_STATE_HALF_OPENED) + ffp.mDeviceStateCallback.onDeviceStateChanged(DEVICE_STATE_OPENED) + verify(storeFeaturesConsumer).accept(HALF_OPENED_FOLDING_FEATURES) + } + + @Test + fun testDeviceStateMapper_malformedDeviceStatePosturePair_skipsPair() { + val malformedDeviceStatePostures = arrayOf( + // Missing the posture. + "0", + // Empty string. + "", + // Too many elements. + "0:1:2", + ) + mMockResources.stub { + on { getStringArray(R.array.config_device_state_postures) } doReturn + malformedDeviceStatePostures + } + + DeviceStateManagerFoldingFeatureProducer( + mMockContext, + mRawFoldSupplier, + mMockDeviceStateManager, + ) + + verify(mMockDeviceStateManager, never()).registerCallback(any(), any()) + } + + @Test + fun testDeviceStateMapper_invalidNumberFormat_skipsPair() { + val invalidNumberFormatDeviceStatePostures = arrayOf("a:1", "0:b", "a:b", ":1") + mMockResources.stub { + on { getStringArray(R.array.config_device_state_postures) } doReturn + invalidNumberFormatDeviceStatePostures + } + + DeviceStateManagerFoldingFeatureProducer( + mMockContext, + mRawFoldSupplier, + mMockDeviceStateManager, + ) + + verify(mMockDeviceStateManager, never()).registerCallback(any(), any()) + } + + companion object { + // Supported device states configuration. + private enum class SupportedDeviceStates { + CLOSED, HALF_OPENED, OPENED, REAR_DISPLAY, CONCURRENT; + + override fun toString() = ordinal.toString() + + fun toDeviceState(): DeviceState = + DeviceState(DeviceState.Configuration.Builder(ordinal, name).build()) + } + + // Map of supported device states supplied by DeviceStateManager to WM Jetpack posture. + private val DEVICE_STATE_POSTURES = + arrayOf( + "${SupportedDeviceStates.CLOSED}:$COMMON_STATE_NO_FOLDING_FEATURES", + "${SupportedDeviceStates.HALF_OPENED}:$COMMON_STATE_HALF_OPENED", + "${SupportedDeviceStates.OPENED}:$COMMON_STATE_FLAT", + "${SupportedDeviceStates.REAR_DISPLAY}:$COMMON_STATE_NO_FOLDING_FEATURES", + "${SupportedDeviceStates.CONCURRENT}:$COMMON_STATE_USE_BASE_STATE", + ) + private val DEVICE_STATE_HALF_OPENED = SupportedDeviceStates.HALF_OPENED.toDeviceState() + private val DEVICE_STATE_OPENED = SupportedDeviceStates.OPENED.toDeviceState() + + // WindowsManager Jetpack display features. + private val DISPLAY_FEATURES = "fold-[1104,0,1104,1848]" + private val DISPLAY_FEATURES_HALF_OPENED_HINGE = "$DISPLAY_FEATURES-half-opened" + private val HALF_OPENED_FOLDING_FEATURES = CommonFoldingFeature.parseListFromString( + DISPLAY_FEATURES, + COMMON_STATE_HALF_OPENED, + ) + private val UNKNOWN_STATE_FOLDING_FEATURES = CommonFoldingFeature.parseListFromString( + DISPLAY_FEATURES, + COMMON_STATE_UNKNOWN, + ) + } +} diff --git a/libs/WindowManager/Jetpack/tests/unittest/src/androidx/window/extensions/embedding/TaskFragmentContainerTest.java b/libs/WindowManager/Jetpack/tests/unittest/src/androidx/window/extensions/embedding/TaskFragmentContainerTest.java index 7fab371cb790..bc4916a607a3 100644 --- a/libs/WindowManager/Jetpack/tests/unittest/src/androidx/window/extensions/embedding/TaskFragmentContainerTest.java +++ b/libs/WindowManager/Jetpack/tests/unittest/src/androidx/window/extensions/embedding/TaskFragmentContainerTest.java @@ -535,7 +535,8 @@ public class TaskFragmentContainerTest { // container1. container2.setInfo(mTransaction, mInfo); - assertTrue(container2.hasActivity(mActivity.getActivityToken())); + assertTrue(container1.hasActivity(mActivity.getActivityToken())); + assertFalse(container2.hasActivity(mActivity.getActivityToken())); // When the pending appeared record is removed from container1, we respect the appeared // record in container2. container1.removePendingAppearedActivity(mActivity.getActivityToken()); diff --git a/libs/WindowManager/Shell/aconfig/multitasking.aconfig b/libs/WindowManager/Shell/aconfig/multitasking.aconfig index 63a288079401..cf0a975b6c30 100644 --- a/libs/WindowManager/Shell/aconfig/multitasking.aconfig +++ b/libs/WindowManager/Shell/aconfig/multitasking.aconfig @@ -156,6 +156,13 @@ flag { } flag { + name: "enable_flexible_two_app_split" + namespace: "multitasking" + description: "Enables only 2 app 90:10 split" + bug: "349828130" +} + +flag { name: "enable_flexible_split" namespace: "multitasking" description: "Enables flexibile split feature for split screen" diff --git a/libs/WindowManager/Shell/res/drawable/app_handle_education_tooltip_icon.xml b/libs/WindowManager/Shell/res/drawable/app_handle_education_tooltip_icon.xml new file mode 100644 index 000000000000..07e5ac1a604b --- /dev/null +++ b/libs/WindowManager/Shell/res/drawable/app_handle_education_tooltip_icon.xml @@ -0,0 +1,27 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + ~ 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. + --> + +<vector xmlns:android="http://schemas.android.com/apk/res/android" + android:width="24dp" + android:height="24dp" + android:tint="?android:attr/textColorTertiary" + android:viewportHeight="960" + android:viewportWidth="960"> + <path + android:fillColor="@android:color/system_on_tertiary_fixed" + android:pathData="M419,880Q391,880 366.5,868Q342,856 325,834L107,557L126,537Q146,516 174,512Q202,508 226,523L300,568L300,240Q300,223 311.5,211.5Q323,200 340,200Q357,200 369,211.5Q381,223 381,240L381,712L284,652L388,785Q394,792 402,796Q410,800 419,800L640,800Q673,800 696.5,776.5Q720,753 720,720L720,560Q720,543 708.5,531.5Q697,520 680,520L461,520L461,440L680,440Q730,440 765,475Q800,510 800,560L800,720Q800,786 753,833Q706,880 640,880L419,880ZM167,340Q154,318 147,292.5Q140,267 140,240Q140,157 198.5,98.5Q257,40 340,40Q423,40 481.5,98.5Q540,157 540,240Q540,267 533,292.5Q526,318 513,340L444,300Q452,286 456,271.5Q460,257 460,240Q460,190 425,155Q390,120 340,120Q290,120 255,155Q220,190 220,240Q220,257 224,271.5Q228,286 236,300L167,340ZM502,620L502,620L502,620L502,620Q502,620 502,620Q502,620 502,620L502,620Q502,620 502,620Q502,620 502,620L502,620Q502,620 502,620Q502,620 502,620L502,620L502,620Z" /> +</vector> diff --git a/libs/WindowManager/Shell/res/drawable/desktop_windowing_education_tooltip_background.xml b/libs/WindowManager/Shell/res/drawable/desktop_windowing_education_tooltip_background.xml new file mode 100644 index 000000000000..a12a74658953 --- /dev/null +++ b/libs/WindowManager/Shell/res/drawable/desktop_windowing_education_tooltip_background.xml @@ -0,0 +1,25 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + ~ 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. + --> + +<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> + <item> + <shape android:shape="rectangle"> + <corners android:radius="30dp" /> + <solid android:color="@android:color/system_tertiary_fixed" /> + </shape> + </item> +</layer-list> diff --git a/libs/WindowManager/Shell/res/drawable/desktop_windowing_education_tooltip_left_arrow.xml b/libs/WindowManager/Shell/res/drawable/desktop_windowing_education_tooltip_left_arrow.xml new file mode 100644 index 000000000000..aadffb5a0003 --- /dev/null +++ b/libs/WindowManager/Shell/res/drawable/desktop_windowing_education_tooltip_left_arrow.xml @@ -0,0 +1,27 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + ~ 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. + --> + +<!-- An arrow that points towards left. --> +<vector xmlns:android="http://schemas.android.com/apk/res/android" + android:width="10dp" + android:height="12dp" + android:viewportWidth="10" + android:viewportHeight="12"> + <path + android:pathData="M2.858,4.285C1.564,5.062 1.564,6.938 2.858,7.715L10,12L10,0L2.858,4.285Z" + android:fillColor="@android:color/system_tertiary_fixed"/> +</vector> diff --git a/libs/WindowManager/Shell/res/drawable/desktop_windowing_education_tooltip_top_arrow.xml b/libs/WindowManager/Shell/res/drawable/desktop_windowing_education_tooltip_top_arrow.xml new file mode 100644 index 000000000000..e3c9a662671e --- /dev/null +++ b/libs/WindowManager/Shell/res/drawable/desktop_windowing_education_tooltip_top_arrow.xml @@ -0,0 +1,26 @@ +<!-- + ~ 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. + --> + +<!-- An arrow that points upwards. --> +<vector xmlns:android="http://schemas.android.com/apk/res/android" + android:width="12dp" + android:height="9dp" + android:viewportWidth="12" + android:viewportHeight="9"> + <path + android:pathData="M7.715,1.858C6.938,0.564 5.062,0.564 4.285,1.858L0,9L12,9L7.715,1.858Z" + android:fillColor="@android:color/system_tertiary_fixed"/> +</vector> diff --git a/libs/WindowManager/Shell/res/layout/desktop_windowing_education_left_arrow_tooltip.xml b/libs/WindowManager/Shell/res/layout/desktop_windowing_education_left_arrow_tooltip.xml new file mode 100644 index 000000000000..a269b9ee1dd5 --- /dev/null +++ b/libs/WindowManager/Shell/res/layout/desktop_windowing_education_left_arrow_tooltip.xml @@ -0,0 +1,36 @@ +<?xml version="1.0" encoding="utf-8"?><!-- + ~ 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. + --> + +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:elevation="1dp" + android:orientation="horizontal"> + + <!-- ImageView for the arrow icon, positioned horizontally at the start of the tooltip + container. --> + <ImageView + android:id="@+id/arrow_icon" + android:layout_width="10dp" + android:layout_height="12dp" + android:layout_gravity="center_vertical" + android:src="@drawable/desktop_windowing_education_tooltip_left_arrow" /> + + <!-- Layout for the tooltip, excluding the arrow. Separating the tooltip content from the arrow + allows scaling of only the tooltip container when the content changes, without affecting the + arrow. --> + <include layout="@layout/desktop_windowing_education_tooltip_container" /> +</LinearLayout> diff --git a/libs/WindowManager/Shell/res/layout/desktop_windowing_education_tooltip_container.xml b/libs/WindowManager/Shell/res/layout/desktop_windowing_education_tooltip_container.xml new file mode 100644 index 000000000000..bdee8836dc2e --- /dev/null +++ b/libs/WindowManager/Shell/res/layout/desktop_windowing_education_tooltip_container.xml @@ -0,0 +1,43 @@ +<?xml version="1.0" encoding="utf-8"?><!-- + ~ 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. + --> + +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:id="@+id/tooltip_container" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:background="@drawable/desktop_windowing_education_tooltip_background" + android:orientation="horizontal" + android:padding="@dimen/desktop_windowing_education_tooltip_padding"> + + <ImageView + android:id="@+id/tooltip_icon" + android:layout_width="32dp" + android:layout_height="32dp" + android:layout_gravity="center_vertical" + android:src="@drawable/app_handle_education_tooltip_icon" /> + + <TextView + android:id="@+id/tooltip_text" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_gravity="center_vertical" + android:layout_marginStart="2dp" + android:lineHeight="20dp" + android:maxWidth="150dp" + android:textColor="@android:color/system_on_tertiary_fixed" + android:textFontWeight="500" + android:textSize="14sp" /> +</LinearLayout>
\ No newline at end of file diff --git a/libs/WindowManager/Shell/res/layout/desktop_windowing_education_top_arrow_tooltip.xml b/libs/WindowManager/Shell/res/layout/desktop_windowing_education_top_arrow_tooltip.xml new file mode 100644 index 000000000000..c73c1dad0e18 --- /dev/null +++ b/libs/WindowManager/Shell/res/layout/desktop_windowing_education_top_arrow_tooltip.xml @@ -0,0 +1,35 @@ +<?xml version="1.0" encoding="utf-8"?><!-- + ~ 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. + --> + +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:elevation="1dp" + android:orientation="vertical"> + + <!-- ImageView for the arrow icon, positioned vertically above the tooltip container. --> + <ImageView + android:id="@+id/arrow_icon" + android:layout_width="12dp" + android:layout_height="9dp" + android:layout_gravity="center_horizontal" + android:src="@drawable/desktop_windowing_education_tooltip_top_arrow" /> + + <!-- Layout for the tooltip, excluding the arrow. Separating the tooltip content from the arrow + allows scaling of only the tooltip container when the content changes, without affecting the + arrow. --> + <include layout="@layout/desktop_windowing_education_tooltip_container" /> +</LinearLayout> diff --git a/libs/WindowManager/Shell/res/layout/letterbox_restart_dialog_layout.xml b/libs/WindowManager/Shell/res/layout/letterbox_restart_dialog_layout.xml index 045b975a854e..462a49ccb1eb 100644 --- a/libs/WindowManager/Shell/res/layout/letterbox_restart_dialog_layout.xml +++ b/libs/WindowManager/Shell/res/layout/letterbox_restart_dialog_layout.xml @@ -99,11 +99,11 @@ </LinearLayout> - <FrameLayout + + <LinearLayout android:minHeight="@dimen/letterbox_restart_dialog_button_height" - android:layout_width="match_parent" + android:layout_width="wrap_content" android:layout_height="wrap_content" - style="?android:attr/buttonBarButtonStyle" android:layout_gravity="end"> <Button @@ -133,7 +133,7 @@ android:text="@string/letterbox_restart_restart" android:contentDescription="@string/letterbox_restart_restart"/> - </FrameLayout> + </LinearLayout> </LinearLayout> diff --git a/libs/WindowManager/Shell/res/values/dimen.xml b/libs/WindowManager/Shell/res/values/dimen.xml index 3d8718332199..c7109f5be132 100644 --- a/libs/WindowManager/Shell/res/values/dimen.xml +++ b/libs/WindowManager/Shell/res/values/dimen.xml @@ -608,6 +608,9 @@ <!-- The horizontal inset to apply to the close button's ripple drawable --> <dimen name="desktop_mode_header_close_ripple_inset_horizontal">6dp</dimen> + <!-- The padding added to all sides of windowing education tooltip --> + <dimen name="desktop_windowing_education_tooltip_padding">8dp</dimen> + <!-- The acceptable area ratio of fg icon area/bg icon area, i.e. (72 x 72) / (108 x 108) --> <item type="dimen" format="float" name="splash_icon_enlarge_foreground_threshold">0.44</item> <!-- Scaling factor applied to splash icons without provided background i.e. (192 / 160) --> diff --git a/libs/WindowManager/Shell/res/values/strings.xml b/libs/WindowManager/Shell/res/values/strings.xml index bda56860d3ba..56f25dae3df2 100644 --- a/libs/WindowManager/Shell/res/values/strings.xml +++ b/libs/WindowManager/Shell/res/values/strings.xml @@ -219,6 +219,15 @@ compatibility control. [CHAR LIMIT=NONE] --> <string name="camera_compat_dismiss_button_description">No camera issues? Tap to dismiss.</string> + <!-- App handle education tooltip text for tooltip pointing to app handle --> + <string name="windowing_app_handle_education_tooltip">Tap to open the app menu</string> + + <!-- App handle education tooltip text for tooltip pointing to windowing image button --> + <string name="windowing_desktop_mode_image_button_education_tooltip">Tap to show multiple apps together</string> + + <!-- App handle education tooltip text for tooltip pointing to app chip --> + <string name="windowing_desktop_mode_exit_education_tooltip">Return to fullscreen from the app menu</string> + <!-- The title of the letterbox education dialog. [CHAR LIMIT=NONE] --> <string name="letterbox_education_dialog_title">See and do more</string> @@ -307,12 +316,11 @@ <!-- Maximize menu snap buttons string. --> <string name="desktop_mode_maximize_menu_snap_text">Snap Screen</string> <!-- Snap resizing non-resizable string. --> - <string name="desktop_mode_non_resizable_snap_text">This app can\'t be resized</string> + <string name="desktop_mode_non_resizable_snap_text">App can\'t be moved here</string> <!-- Accessibility text for the Maximize Menu's maximize button [CHAR LIMIT=NONE] --> <string name="desktop_mode_maximize_menu_maximize_button_text">Maximize</string> <!-- Accessibility text for the Maximize Menu's snap left button [CHAR LIMIT=NONE] --> <string name="desktop_mode_maximize_menu_snap_left_button_text">Snap left</string> <!-- Accessibility text for the Maximize Menu's snap right button [CHAR LIMIT=NONE] --> <string name="desktop_mode_maximize_menu_snap_right_button_text">Snap right</string> - </resources> diff --git a/core/java/android/ranging/mock/RangingFrameworkInitializer.java b/libs/WindowManager/Shell/shared/src/com/android/wm/shell/shared/FocusTransitionListener.java index 540f51954a9c..26aae2d2aa78 100644 --- a/core/java/android/ranging/mock/RangingFrameworkInitializer.java +++ b/libs/WindowManager/Shell/shared/src/com/android/wm/shell/shared/FocusTransitionListener.java @@ -14,21 +14,17 @@ * limitations under the License. */ -package android.ranging; +package com.android.wm.shell.shared; -/** -* Mock RangingFrameworkInitializer. -* -* @hide -*/ +import com.android.wm.shell.shared.annotations.ExternalThread; -// TODO(b/331206299): Remove this after RANGING_STACK_ENABLED is ramped up to next. -public final class RangingFrameworkInitializer { - private RangingFrameworkInitializer() {} +/** + * Listener to get focus-related transition callbacks. + */ +@ExternalThread +public interface FocusTransitionListener { /** - * @hide + * Called when a transition changes the top, focused display. */ - public static void registerServiceWrappers() { - // No-op. - } + void onFocusedDisplayChanged(int displayId); } diff --git a/libs/WindowManager/Shell/shared/src/com/android/wm/shell/shared/IFocusTransitionListener.aidl b/libs/WindowManager/Shell/shared/src/com/android/wm/shell/shared/IFocusTransitionListener.aidl new file mode 100644 index 000000000000..b91d5b6e2769 --- /dev/null +++ b/libs/WindowManager/Shell/shared/src/com/android/wm/shell/shared/IFocusTransitionListener.aidl @@ -0,0 +1,28 @@ +/* + * 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.wm.shell.shared; + +/** + * Listener interface that to get focus-related transition callbacks. + */ +oneway interface IFocusTransitionListener { + + /** + * Called when a transition changes the top, focused display. + */ + void onFocusedDisplayChanged(int displayId); +} diff --git a/libs/WindowManager/Shell/shared/src/com/android/wm/shell/shared/IShellTransitions.aidl b/libs/WindowManager/Shell/shared/src/com/android/wm/shell/shared/IShellTransitions.aidl index 3256abf09116..02615a96a86c 100644 --- a/libs/WindowManager/Shell/shared/src/com/android/wm/shell/shared/IShellTransitions.aidl +++ b/libs/WindowManager/Shell/shared/src/com/android/wm/shell/shared/IShellTransitions.aidl @@ -20,6 +20,7 @@ import android.view.SurfaceControl; import android.window.RemoteTransition; import android.window.TransitionFilter; +import com.android.wm.shell.shared.IFocusTransitionListener; import com.android.wm.shell.shared.IHomeTransitionListener; /** @@ -59,4 +60,9 @@ interface IShellTransitions { */ oneway void registerRemoteForTakeover(in TransitionFilter filter, in RemoteTransition remoteTransition) = 6; + + /** + * Set listener that will receive callbacks about transitions involving focus switch. + */ + oneway void setFocusTransitionListener(in IFocusTransitionListener listener) = 7; } diff --git a/libs/WindowManager/Shell/shared/src/com/android/wm/shell/shared/ShellTransitions.java b/libs/WindowManager/Shell/shared/src/com/android/wm/shell/shared/ShellTransitions.java index 6d4ab4c1bd09..2db4311fb771 100644 --- a/libs/WindowManager/Shell/shared/src/com/android/wm/shell/shared/ShellTransitions.java +++ b/libs/WindowManager/Shell/shared/src/com/android/wm/shell/shared/ShellTransitions.java @@ -22,6 +22,8 @@ import android.window.TransitionFilter; import com.android.wm.shell.shared.annotations.ExternalThread; +import java.util.concurrent.Executor; + /** * Interface to manage remote transitions. */ @@ -44,4 +46,15 @@ public interface ShellTransitions { * Unregisters a remote transition for all operations. */ default void unregisterRemote(@NonNull RemoteTransition remoteTransition) {} + + /** + * Sets listener that will receive callbacks about transitions involving focus switch. + */ + default void setFocusTransitionListener(@NonNull FocusTransitionListener listener, + Executor executor) {} + + /** + * Unsets listener that will receive callbacks about transitions involving focus switch. + */ + default void unsetFocusTransitionListener(@NonNull FocusTransitionListener listener) {} } diff --git a/libs/WindowManager/Shell/shared/src/com/android/wm/shell/shared/split/OWNERS b/libs/WindowManager/Shell/shared/src/com/android/wm/shell/shared/split/OWNERS new file mode 100644 index 000000000000..bfb6d4ac5849 --- /dev/null +++ b/libs/WindowManager/Shell/shared/src/com/android/wm/shell/shared/split/OWNERS @@ -0,0 +1,4 @@ +jeremysim@google.com +winsonc@google.com +peanutbutter@google.com +shuminghao@google.com diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/dagger/WMShellBaseModule.java b/libs/WindowManager/Shell/src/com/android/wm/shell/dagger/WMShellBaseModule.java index bec2ea58e106..4227a6e2903f 100644 --- a/libs/WindowManager/Shell/src/com/android/wm/shell/dagger/WMShellBaseModule.java +++ b/libs/WindowManager/Shell/src/com/android/wm/shell/dagger/WMShellBaseModule.java @@ -123,6 +123,7 @@ import com.android.wm.shell.sysui.ShellInterface; import com.android.wm.shell.taskview.TaskViewFactory; import com.android.wm.shell.taskview.TaskViewFactoryController; import com.android.wm.shell.taskview.TaskViewTransitions; +import com.android.wm.shell.transition.FocusTransitionObserver; import com.android.wm.shell.transition.HomeTransitionObserver; import com.android.wm.shell.transition.MixedTransitionHandler; import com.android.wm.shell.transition.Transitions; @@ -742,14 +743,15 @@ public abstract class WMShellBaseModule { @ShellMainThread Handler mainHandler, @ShellAnimationThread ShellExecutor animExecutor, RootTaskDisplayAreaOrganizer rootTaskDisplayAreaOrganizer, - HomeTransitionObserver homeTransitionObserver) { + HomeTransitionObserver homeTransitionObserver, + FocusTransitionObserver focusTransitionObserver) { if (!context.getResources().getBoolean(R.bool.config_registerShellTransitionsOnInit)) { // TODO(b/238217847): Force override shell init if registration is disabled shellInit = new ShellInit(mainExecutor); } return new Transitions(context, shellInit, shellCommandHandler, shellController, organizer, pool, displayController, mainExecutor, mainHandler, animExecutor, - rootTaskDisplayAreaOrganizer, homeTransitionObserver); + rootTaskDisplayAreaOrganizer, homeTransitionObserver, focusTransitionObserver); } @WMSingleton @@ -761,6 +763,12 @@ public abstract class WMShellBaseModule { @WMSingleton @Provides + static FocusTransitionObserver provideFocusTransitionObserver() { + return new FocusTransitionObserver(); + } + + @WMSingleton + @Provides static TaskViewTransitions provideTaskViewTransitions(Transitions transitions) { return new TaskViewTransitions(transitions); } diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/desktopmode/DesktopModeTaskRepository.kt b/libs/WindowManager/Shell/src/com/android/wm/shell/desktopmode/DesktopModeTaskRepository.kt index 759ed035895e..0e8c4e70e05d 100644 --- a/libs/WindowManager/Shell/src/com/android/wm/shell/desktopmode/DesktopModeTaskRepository.kt +++ b/libs/WindowManager/Shell/src/com/android/wm/shell/desktopmode/DesktopModeTaskRepository.kt @@ -456,6 +456,7 @@ class DesktopModeTaskRepository ( pw.println( "${innerPrefix}freeformTasksInZOrder=${data.freeformTasksInZOrder.toDumpString()}" ) + pw.println("${innerPrefix}minimizedTasks=${data.minimizedTasks.toDumpString()}") } } diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/desktopmode/DesktopTasksController.kt b/libs/WindowManager/Shell/src/com/android/wm/shell/desktopmode/DesktopTasksController.kt index 968f40c3df5d..afa27f9f1309 100644 --- a/libs/WindowManager/Shell/src/com/android/wm/shell/desktopmode/DesktopTasksController.kt +++ b/libs/WindowManager/Shell/src/com/android/wm/shell/desktopmode/DesktopTasksController.kt @@ -43,6 +43,7 @@ import android.view.Display.DEFAULT_DISPLAY import android.view.DragEvent import android.view.SurfaceControl import android.view.WindowManager.TRANSIT_CHANGE +import android.view.WindowManager.TRANSIT_CLOSE import android.view.WindowManager.TRANSIT_NONE import android.view.WindowManager.TRANSIT_OPEN import android.view.WindowManager.TRANSIT_TO_FRONT @@ -1061,7 +1062,10 @@ class DesktopTasksController( // Check if freeform task launch during recents should be handled shouldHandleMidRecentsFreeformLaunch -> handleMidRecentsFreeformTaskLaunch(task) // Check if the closing task needs to be handled - TransitionUtil.isClosingType(request.type) -> handleTaskClosing(task) + TransitionUtil.isClosingType(request.type) -> handleTaskClosing( + task, + request.type + ) // Check if the top task shouldn't be allowed to enter desktop mode isIncompatibleTask(task) -> handleIncompatibleTaskLaunch(task) // Check if fullscreen task should be updated @@ -1288,7 +1292,10 @@ class DesktopTasksController( } /** Handle task closing by removing wallpaper activity if it's the last active task */ - private fun handleTaskClosing(task: RunningTaskInfo): WindowContainerTransaction? { + private fun handleTaskClosing( + task: RunningTaskInfo, + transitionType: Int + ): WindowContainerTransaction? { logV("handleTaskClosing") if (!isDesktopModeShowing(task.displayId)) return null @@ -1301,9 +1308,10 @@ class DesktopTasksController( removeWallpaperActivity(wct) } taskRepository.addClosingTask(task.displayId, task.taskId) - // If a CLOSE or TO_BACK is triggered on a desktop task, remove the task. + // If a CLOSE is triggered on a desktop task, remove the task. if (DesktopModeFlags.ENABLE_DESKTOP_WINDOWING_BACK_NAVIGATION.isTrue() && - taskRepository.isVisibleTask(task.taskId) + taskRepository.isVisibleTask(task.taskId) && + transitionType == TRANSIT_CLOSE ) { wct.removeTask(task.token) } diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/desktopmode/DesktopTasksTransitionObserver.kt b/libs/WindowManager/Shell/src/com/android/wm/shell/desktopmode/DesktopTasksTransitionObserver.kt index 0841628853a3..4796c4d0655a 100644 --- a/libs/WindowManager/Shell/src/com/android/wm/shell/desktopmode/DesktopTasksTransitionObserver.kt +++ b/libs/WindowManager/Shell/src/com/android/wm/shell/desktopmode/DesktopTasksTransitionObserver.kt @@ -16,16 +16,19 @@ package com.android.wm.shell.desktopmode +import android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM import android.content.Context import android.os.IBinder import android.view.SurfaceControl import android.view.WindowManager +import android.view.WindowManager.TRANSIT_TO_BACK import android.window.TransitionInfo import android.window.WindowContainerTransaction +import android.window.flags.DesktopModeFlags +import android.window.flags.DesktopModeFlags.ENABLE_DESKTOP_WINDOWING_WALLPAPER_ACTIVITY import com.android.internal.protolog.ProtoLog import com.android.wm.shell.ShellTaskOrganizer import com.android.wm.shell.protolog.ShellProtoLogGroup.WM_SHELL_DESKTOP_MODE -import android.window.flags.DesktopModeFlags.ENABLE_DESKTOP_WINDOWING_WALLPAPER_ACTIVITY import com.android.wm.shell.shared.desktopmode.DesktopModeStatus import com.android.wm.shell.sysui.ShellInit import com.android.wm.shell.transition.Transitions @@ -64,6 +67,30 @@ class DesktopTasksTransitionObserver( ) { // TODO: b/332682201 Update repository state updateWallpaperToken(info) + + if (DesktopModeFlags.ENABLE_DESKTOP_WINDOWING_BACK_NAVIGATION.isTrue()) { + handleBackNavigation(info) + } + } + + private fun handleBackNavigation(info: TransitionInfo) { + // When default back navigation happens, transition type is TO_BACK and the change is + // TO_BACK. Mark the task going to back as minimized. + if (info.type == TRANSIT_TO_BACK) { + for (change in info.changes) { + val taskInfo = change.taskInfo + if (taskInfo == null || taskInfo.taskId == -1) { + continue + } + + if (desktopModeTaskRepository.getVisibleTaskCount(taskInfo.displayId) > 0 && + change.mode == TRANSIT_TO_BACK && + taskInfo.windowingMode == WINDOWING_MODE_FREEFORM + ) { + desktopModeTaskRepository.minimizeTask(taskInfo.displayId, taskInfo.taskId) + } + } + } } override fun onTransitionStarting(transition: IBinder) { diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/PipTransition.java b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/PipTransition.java index 2138acc51eb2..cbb08b804dfe 100644 --- a/libs/WindowManager/Shell/src/com/android/wm/shell/pip/PipTransition.java +++ b/libs/WindowManager/Shell/src/com/android/wm/shell/pip/PipTransition.java @@ -1344,6 +1344,9 @@ public class PipTransition extends PipTransitionController { final SurfaceControl leash = pipChange.getLeash(); final Rect destBounds = mPipOrganizer.getCurrentOrAnimatingBounds(); final boolean isInPip = mPipTransitionState.isInPip(); + ProtoLog.d(ShellProtoLogGroup.WM_SHELL_PICTURE_IN_PICTURE, + "%s: Update pip for unhandled transition, change=%s, destBounds=%s, isInPip=%b", + TAG, pipChange, destBounds, isInPip); mSurfaceTransactionHelper .crop(startTransaction, leash, destBounds) .round(startTransaction, leash, isInPip) diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/transition/FocusTransitionObserver.java b/libs/WindowManager/Shell/src/com/android/wm/shell/transition/FocusTransitionObserver.java new file mode 100644 index 000000000000..2f5059f3161c --- /dev/null +++ b/libs/WindowManager/Shell/src/com/android/wm/shell/transition/FocusTransitionObserver.java @@ -0,0 +1,142 @@ +/* + * 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.wm.shell.transition; + +import static android.view.Display.INVALID_DISPLAY; +import static android.window.TransitionInfo.FLAG_MOVED_TO_TOP; + +import static com.android.window.flags.Flags.enableDisplayFocusInShellTransitions; +import static com.android.wm.shell.transition.Transitions.TransitionObserver; + +import android.annotation.NonNull; +import android.app.ActivityManager.RunningTaskInfo; +import android.os.IBinder; +import android.os.RemoteException; +import android.util.Slog; +import android.view.SurfaceControl; +import android.window.TransitionInfo; + +import com.android.wm.shell.shared.FocusTransitionListener; +import com.android.wm.shell.shared.IFocusTransitionListener; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.Executor; + +/** + * The {@link TransitionObserver} that observes for transitions involving focus switch. + * It reports transitions to callers outside of the process via {@link IFocusTransitionListener}, + * and callers within the process via {@link FocusTransitionListener}. + */ +public class FocusTransitionObserver implements TransitionObserver { + private static final String TAG = FocusTransitionObserver.class.getSimpleName(); + + private IFocusTransitionListener mRemoteListener; + private final Map<FocusTransitionListener, Executor> mLocalListeners = + new HashMap<>(); + + private int mFocusedDisplayId = INVALID_DISPLAY; + + public FocusTransitionObserver() {} + + @Override + public void onTransitionReady(@NonNull IBinder transition, + @NonNull TransitionInfo info, + @NonNull SurfaceControl.Transaction startTransaction, + @NonNull SurfaceControl.Transaction finishTransaction) { + final List<TransitionInfo.Change> changes = info.getChanges(); + for (int i = changes.size() - 1; i >= 0; i--) { + final TransitionInfo.Change change = changes.get(i); + final RunningTaskInfo task = change.getTaskInfo(); + if (task != null && task.isFocused && change.hasFlags(FLAG_MOVED_TO_TOP)) { + if (mFocusedDisplayId != task.displayId) { + mFocusedDisplayId = task.displayId; + notifyFocusedDisplayChanged(); + } + return; + } + } + } + + @Override + public void onTransitionStarting(@NonNull IBinder transition) {} + + @Override + public void onTransitionMerged(@NonNull IBinder merged, @NonNull IBinder playing) {} + + @Override + public void onTransitionFinished(@NonNull IBinder transition, boolean aborted) {} + + /** + * Sets the focus transition listener that receives any transitions resulting in focus switch. + * This is for calls from outside the Shell, within the host process. + * + */ + public void setLocalFocusTransitionListener(FocusTransitionListener listener, + Executor executor) { + if (!enableDisplayFocusInShellTransitions()) { + return; + } + mLocalListeners.put(listener, executor); + executor.execute(() -> listener.onFocusedDisplayChanged(mFocusedDisplayId)); + } + + /** + * Sets the focus transition listener that receives any transitions resulting in focus switch. + * This is for calls from outside the Shell, within the host process. + * + */ + public void unsetLocalFocusTransitionListener(FocusTransitionListener listener) { + if (!enableDisplayFocusInShellTransitions()) { + return; + } + mLocalListeners.remove(listener); + } + + /** + * Sets the focus transition listener that receives any transitions resulting in focus switch. + * This is for calls from outside the host process. + */ + public void setRemoteFocusTransitionListener(Transitions transitions, + IFocusTransitionListener listener) { + if (!enableDisplayFocusInShellTransitions()) { + return; + } + mRemoteListener = listener; + notifyFocusedDisplayChangedToRemote(); + } + + /** + * Notifies the listener that display focus has changed. + */ + public void notifyFocusedDisplayChanged() { + notifyFocusedDisplayChangedToRemote(); + mLocalListeners.forEach((listener, executor) -> + executor.execute(() -> listener.onFocusedDisplayChanged(mFocusedDisplayId))); + } + + private void notifyFocusedDisplayChangedToRemote() { + if (mRemoteListener != null) { + try { + mRemoteListener.onFocusedDisplayChanged(mFocusedDisplayId); + } catch (RemoteException e) { + Slog.w(TAG, "Failed call notifyFocusedDisplayChangedToRemote", e); + } + } + } +} diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/transition/Transitions.java b/libs/WindowManager/Shell/src/com/android/wm/shell/transition/Transitions.java index d03832d3e85e..d280dcd252b4 100644 --- a/libs/WindowManager/Shell/src/com/android/wm/shell/transition/Transitions.java +++ b/libs/WindowManager/Shell/src/com/android/wm/shell/transition/Transitions.java @@ -87,6 +87,8 @@ import com.android.wm.shell.common.RemoteCallable; import com.android.wm.shell.common.ShellExecutor; import com.android.wm.shell.keyguard.KeyguardTransitionHandler; import com.android.wm.shell.protolog.ShellProtoLogGroup; +import com.android.wm.shell.shared.FocusTransitionListener; +import com.android.wm.shell.shared.IFocusTransitionListener; import com.android.wm.shell.shared.IHomeTransitionListener; import com.android.wm.shell.shared.IShellTransitions; import com.android.wm.shell.shared.ShellTransitions; @@ -103,6 +105,7 @@ import com.android.wm.shell.transition.tracing.TransitionTracer; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; +import java.util.concurrent.Executor; /** * Plays transition animations. Within this player, each transition has a lifecycle. @@ -224,6 +227,7 @@ public class Transitions implements RemoteCallable<Transitions>, private final ArrayList<TransitionObserver> mObservers = new ArrayList<>(); private HomeTransitionObserver mHomeTransitionObserver; + private FocusTransitionObserver mFocusTransitionObserver; /** List of {@link Runnable} instances to run when the last active transition has finished. */ private final ArrayList<Runnable> mRunWhenIdleQueue = new ArrayList<>(); @@ -309,10 +313,12 @@ public class Transitions implements RemoteCallable<Transitions>, @NonNull ShellExecutor mainExecutor, @NonNull Handler mainHandler, @NonNull ShellExecutor animExecutor, - @NonNull HomeTransitionObserver observer) { + @NonNull HomeTransitionObserver homeTransitionObserver, + @NonNull FocusTransitionObserver focusTransitionObserver) { this(context, shellInit, new ShellCommandHandler(), shellController, organizer, pool, displayController, mainExecutor, mainHandler, animExecutor, - new RootTaskDisplayAreaOrganizer(mainExecutor, context, shellInit), observer); + new RootTaskDisplayAreaOrganizer(mainExecutor, context, shellInit), + homeTransitionObserver, focusTransitionObserver); } public Transitions(@NonNull Context context, @@ -326,7 +332,8 @@ public class Transitions implements RemoteCallable<Transitions>, @NonNull Handler mainHandler, @NonNull ShellExecutor animExecutor, @NonNull RootTaskDisplayAreaOrganizer rootTDAOrganizer, - @NonNull HomeTransitionObserver observer) { + @NonNull HomeTransitionObserver homeTransitionObserver, + @NonNull FocusTransitionObserver focusTransitionObserver) { mOrganizer = organizer; mContext = context; mMainExecutor = mainExecutor; @@ -345,7 +352,8 @@ public class Transitions implements RemoteCallable<Transitions>, mHandlers.add(mRemoteTransitionHandler); ProtoLog.v(ShellProtoLogGroup.WM_SHELL_TRANSITIONS, "addHandler: Remote"); shellInit.addInitCallback(this::onInit, this); - mHomeTransitionObserver = observer; + mHomeTransitionObserver = homeTransitionObserver; + mFocusTransitionObserver = focusTransitionObserver; if (android.tracing.Flags.perfettoTransitionTracing()) { mTransitionTracer = new PerfettoTransitionTracer(); @@ -384,6 +392,8 @@ public class Transitions implements RemoteCallable<Transitions>, mShellCommandHandler.addCommandCallback("transitions", this, this); mShellCommandHandler.addDumpCallback(this::dump, this); + + registerObserver(mFocusTransitionObserver); } public boolean isRegistered() { @@ -1573,6 +1583,21 @@ public class Transitions implements RemoteCallable<Transitions>, mMainExecutor.execute( () -> mRemoteTransitionHandler.removeFiltered(remoteTransition)); } + + @Override + public void setFocusTransitionListener(FocusTransitionListener listener, + Executor executor) { + mMainExecutor.execute(() -> + mFocusTransitionObserver.setLocalFocusTransitionListener(listener, executor)); + + } + + @Override + public void unsetFocusTransitionListener(FocusTransitionListener listener) { + mMainExecutor.execute(() -> + mFocusTransitionObserver.unsetLocalFocusTransitionListener(listener)); + + } } /** @@ -1634,6 +1659,15 @@ public class Transitions implements RemoteCallable<Transitions>, } @Override + public void setFocusTransitionListener(IFocusTransitionListener listener) { + executeRemoteCallWithTaskPermission(mTransitions, "setFocusTransitionListener", + (transitions) -> { + transitions.mFocusTransitionObserver.setRemoteFocusTransitionListener( + transitions, listener); + }); + } + + @Override public SurfaceControl getHomeTaskOverlayContainer() { SurfaceControl[] result = new SurfaceControl[1]; executeRemoteCallWithTaskPermission(mTransitions, "getHomeTaskOverlayContainer", diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/windowdecor/additionalviewcontainer/AdditionalSystemViewContainer.kt b/libs/WindowManager/Shell/src/com/android/wm/shell/windowdecor/additionalviewcontainer/AdditionalSystemViewContainer.kt index 226b0fb2e1a1..1be26f080ac8 100644 --- a/libs/WindowManager/Shell/src/com/android/wm/shell/windowdecor/additionalviewcontainer/AdditionalSystemViewContainer.kt +++ b/libs/WindowManager/Shell/src/com/android/wm/shell/windowdecor/additionalviewcontainer/AdditionalSystemViewContainer.kt @@ -107,4 +107,27 @@ class AdditionalSystemViewContainer( } windowManagerWrapper.updateViewLayout(view, lp) } + + class Factory { + fun create( + windowManagerWrapper: WindowManagerWrapper, + taskId: Int, + x: Int, + y: Int, + width: Int, + height: Int, + flags: Int, + view: View, + ): AdditionalSystemViewContainer = + AdditionalSystemViewContainer( + windowManagerWrapper = windowManagerWrapper, + taskId = taskId, + x = x, + y = y, + width = width, + height = height, + flags = flags, + view = view + ) + } } diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/windowdecor/education/DesktopWindowingEducationTooltipController.kt b/libs/WindowManager/Shell/src/com/android/wm/shell/windowdecor/education/DesktopWindowingEducationTooltipController.kt new file mode 100644 index 000000000000..98413ee96133 --- /dev/null +++ b/libs/WindowManager/Shell/src/com/android/wm/shell/windowdecor/education/DesktopWindowingEducationTooltipController.kt @@ -0,0 +1,249 @@ +/* + * 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.wm.shell.windowdecor.education + +import android.annotation.DimenRes +import android.annotation.LayoutRes +import android.content.Context +import android.content.res.Resources +import android.graphics.Point +import android.util.Size +import android.view.LayoutInflater +import android.view.MotionEvent +import android.view.View +import android.view.View.MeasureSpec.UNSPECIFIED +import android.view.WindowManager +import android.widget.ImageView +import android.widget.LinearLayout +import android.widget.TextView +import androidx.dynamicanimation.animation.DynamicAnimation +import androidx.dynamicanimation.animation.SpringForce +import com.android.wm.shell.R +import com.android.wm.shell.shared.animation.PhysicsAnimator +import com.android.wm.shell.windowdecor.WindowManagerWrapper +import com.android.wm.shell.windowdecor.additionalviewcontainer.AdditionalSystemViewContainer + +/** + * Controls the lifecycle of an education tooltip, including showing and hiding it. Ensures that + * only one tooltip is displayed at a time. + */ +class DesktopWindowingEducationTooltipController( + private val context: Context, + private val additionalSystemViewContainerFactory: AdditionalSystemViewContainer.Factory, +) { + // TODO: b/369384567 - Set tooltip color scheme to match LT/DT of app theme + private var tooltipView: View? = null + private var animator: PhysicsAnimator<View>? = null + private val springConfig by lazy { + PhysicsAnimator.SpringConfig(SpringForce.STIFFNESS_MEDIUM, SpringForce.DAMPING_RATIO_LOW_BOUNCY) + } + private var popupWindow: AdditionalSystemViewContainer? = null + + /** + * Shows education tooltip. + * + * @param tooltipViewConfig features of tooltip. + * @param taskId is used in the title of popup window created for the tooltip view. + */ + fun showEducationTooltip(tooltipViewConfig: EducationViewConfig, taskId: Int) { + hideEducationTooltip() + tooltipView = createEducationTooltipView(tooltipViewConfig, taskId) + animator = createAnimator() + animateShowTooltipTransition() + } + + /** Hide the current education view if visible */ + private fun hideEducationTooltip() = animateHideTooltipTransition { cleanUp() } + + /** Create education view by inflating layout provided. */ + private fun createEducationTooltipView( + tooltipViewConfig: EducationViewConfig, + taskId: Int, + ): View { + val tooltipView = + LayoutInflater.from(context) + .inflate( + tooltipViewConfig.tooltipViewLayout, /* root= */ null, /* attachToRoot= */ false) + .apply { + alpha = 0f + scaleX = 0f + scaleY = 0f + + requireViewById<TextView>(R.id.tooltip_text).apply { + text = tooltipViewConfig.tooltipText + } + + setOnTouchListener { _, motionEvent -> + if (motionEvent.action == MotionEvent.ACTION_OUTSIDE) { + hideEducationTooltip() + tooltipViewConfig.onDismissAction() + true + } else { + false + } + } + setOnClickListener { + hideEducationTooltip() + tooltipViewConfig.onEducationClickAction() + } + } + + val tooltipDimens = tooltipDimens(tooltipView = tooltipView, tooltipViewConfig.arrowDirection) + val tooltipViewGlobalCoordinates = + tooltipViewGlobalCoordinates( + tooltipViewGlobalCoordinates = tooltipViewConfig.tooltipViewGlobalCoordinates, + arrowDirection = tooltipViewConfig.arrowDirection, + tooltipDimen = tooltipDimens) + createTooltipPopupWindow( + taskId, tooltipViewGlobalCoordinates, tooltipDimens, tooltipView = tooltipView) + + return tooltipView + } + + /** Create animator for education transitions */ + private fun createAnimator(): PhysicsAnimator<View>? = + tooltipView?.let { + PhysicsAnimator.getInstance(it).apply { setDefaultSpringConfig(springConfig) } + } + + /** Animate show transition for the education view */ + private fun animateShowTooltipTransition() { + animator + ?.spring(DynamicAnimation.ALPHA, 1f) + ?.spring(DynamicAnimation.SCALE_X, 1f) + ?.spring(DynamicAnimation.SCALE_Y, 1f) + ?.start() + } + + /** Animate hide transition for the education view */ + private fun animateHideTooltipTransition(endActions: () -> Unit) { + animator + ?.spring(DynamicAnimation.ALPHA, 0f) + ?.spring(DynamicAnimation.SCALE_X, 0f) + ?.spring(DynamicAnimation.SCALE_Y, 0f) + ?.start() + endActions() + } + + /** Remove education tooltip and clean up all relative properties */ + private fun cleanUp() { + tooltipView = null + animator = null + popupWindow?.releaseView() + popupWindow = null + } + + private fun createTooltipPopupWindow( + taskId: Int, + tooltipViewGlobalCoordinates: Point, + tooltipDimen: Size, + tooltipView: View, + ) { + popupWindow = + additionalSystemViewContainerFactory.create( + windowManagerWrapper = + WindowManagerWrapper(context.getSystemService(WindowManager::class.java)), + taskId = taskId, + x = tooltipViewGlobalCoordinates.x, + y = tooltipViewGlobalCoordinates.y, + width = tooltipDimen.width, + height = tooltipDimen.height, + flags = + WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or + WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, + view = tooltipView) + } + + private fun tooltipViewGlobalCoordinates( + tooltipViewGlobalCoordinates: Point, + arrowDirection: TooltipArrowDirection, + tooltipDimen: Size, + ): Point { + var tooltipX = tooltipViewGlobalCoordinates.x + var tooltipY = tooltipViewGlobalCoordinates.y + + // Current values of [tooltipX]/[tooltipY] are the coordinates of tip of the arrow. + // Parameter x and y passed to [AdditionalSystemViewContainer] is the top left position of + // the window to be created. Hence we will need to move the coordinates left/up in order + // to position the tooltip correctly. + if (arrowDirection == TooltipArrowDirection.UP) { + // Arrow is placed at horizontal center on top edge of the tooltip. Hence decrement + // half of tooltip width from [tooltipX] to horizontally position the tooltip. + tooltipX -= tooltipDimen.width / 2 + } else { + // Arrow is placed at vertical center on the left edge of the tooltip. Hence decrement + // half of tooltip height from [tooltipY] to vertically position the tooltip. + tooltipY -= tooltipDimen.height / 2 + } + return Point(tooltipX, tooltipY) + } + + private fun tooltipDimens(tooltipView: View, arrowDirection: TooltipArrowDirection): Size { + val tooltipBackground = tooltipView.requireViewById<LinearLayout>(R.id.tooltip_container) + val arrowView = tooltipView.requireViewById<ImageView>(R.id.arrow_icon) + tooltipBackground.measure( + /* widthMeasureSpec= */ UNSPECIFIED, /* heightMeasureSpec= */ UNSPECIFIED) + arrowView.measure(/* widthMeasureSpec= */ UNSPECIFIED, /* heightMeasureSpec= */ UNSPECIFIED) + + var desiredWidth = + tooltipBackground.measuredWidth + + 2 * loadDimensionPixelSize(R.dimen.desktop_windowing_education_tooltip_padding) + var desiredHeight = + tooltipBackground.measuredHeight + + 2 * loadDimensionPixelSize(R.dimen.desktop_windowing_education_tooltip_padding) + if (arrowDirection == TooltipArrowDirection.UP) { + // desiredHeight currently does not account for the height of arrow, hence adding it. + desiredHeight += arrowView.height + } else { + // desiredWidth currently does not account for the width of arrow, hence adding it. + desiredWidth += arrowView.width + } + + return Size(desiredWidth, desiredHeight) + } + + private fun loadDimensionPixelSize(@DimenRes resourceId: Int): Int { + if (resourceId == Resources.ID_NULL) return 0 + return context.resources.getDimensionPixelSize(resourceId) + } + + /** + * The configuration for education view features: + * + * @property tooltipViewLayout Layout resource ID of the view to be used for education tooltip. + * @property tooltipViewGlobalCoordinates Global (screen) coordinates of the tip of the tooltip + * arrow. + * @property tooltipText Text to be added to the TextView of tooltip. + * @property arrowDirection Direction of arrow of the tooltip. + * @property onEducationClickAction Lambda to be executed when the tooltip is clicked. + * @property onDismissAction Lambda to be executed when the tooltip is dismissed. + */ + data class EducationViewConfig( + @LayoutRes val tooltipViewLayout: Int, + val tooltipViewGlobalCoordinates: Point, + val tooltipText: String, + val arrowDirection: TooltipArrowDirection, + val onEducationClickAction: () -> Unit, + val onDismissAction: () -> Unit, + ) + + /** Direction of arrow of the tooltip */ + enum class TooltipArrowDirection { + UP, + LEFT, + } +} diff --git a/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/desktopmode/DesktopTasksControllerTest.kt b/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/desktopmode/DesktopTasksControllerTest.kt index ee545209904f..94e361659090 100644 --- a/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/desktopmode/DesktopTasksControllerTest.kt +++ b/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/desktopmode/DesktopTasksControllerTest.kt @@ -40,6 +40,7 @@ import android.graphics.Point import android.graphics.PointF import android.graphics.Rect import android.os.Binder +import android.os.Bundle import android.os.Handler import android.platform.test.annotations.DisableFlags import android.platform.test.annotations.EnableFlags @@ -2086,16 +2087,13 @@ class DesktopTasksControllerTest : ShellTestCase() { } @Test - @EnableFlags( - Flags.FLAG_ENABLE_DESKTOP_WINDOWING_WALLPAPER_ACTIVITY, - Flags.FLAG_ENABLE_DESKTOP_WINDOWING_BACK_NAVIGATION - ) - fun handleRequest_backTransition_singleTaskNoToken_withWallpaper_withBackNav_removesTask() { + @EnableFlags(Flags.FLAG_ENABLE_DESKTOP_WINDOWING_WALLPAPER_ACTIVITY,) + fun handleRequest_backTransition_singleTaskNoToken_withWallpaper_removesTask() { val task = setUpFreeformTask() val result = controller.handleRequest(Binder(), createTransition(task, type = TRANSIT_TO_BACK)) - assertNotNull(result, "Should handle request").assertRemoveAt(0, task.token) + assertNull(result, "Should not handle request") } @Test @@ -2137,26 +2135,8 @@ class DesktopTasksControllerTest : ShellTestCase() { } @Test - @EnableFlags( - Flags.FLAG_ENABLE_DESKTOP_WINDOWING_WALLPAPER_ACTIVITY, - Flags.FLAG_ENABLE_DESKTOP_WINDOWING_BACK_NAVIGATION - ) - fun handleRequest_backTransition_singleTask_withWallpaper_withBackNav_removesWallpaperAndTask() { - val task = setUpFreeformTask() - val wallpaperToken = MockToken().token() - - taskRepository.wallpaperActivityToken = wallpaperToken - val result = controller.handleRequest(Binder(), createTransition(task, type = TRANSIT_TO_BACK)) - - // Should create remove wallpaper transaction - assertNotNull(result, "Should handle request").assertRemoveAt(index = 0, wallpaperToken) - result.assertRemoveAt(index = 1, task.token) - } - - @Test @EnableFlags(Flags.FLAG_ENABLE_DESKTOP_WINDOWING_WALLPAPER_ACTIVITY) - @DisableFlags(Flags.FLAG_ENABLE_DESKTOP_WINDOWING_BACK_NAVIGATION) - fun handleRequest_backTransition_singleTaskWithToken_noBackNav_removesWallpaper() { + fun handleRequest_backTransition_singleTaskWithToken_removesWallpaper() { val task = setUpFreeformTask() val wallpaperToken = MockToken().token() @@ -2183,23 +2163,7 @@ class DesktopTasksControllerTest : ShellTestCase() { } @Test - @EnableFlags( - Flags.FLAG_ENABLE_DESKTOP_WINDOWING_WALLPAPER_ACTIVITY, - Flags.FLAG_ENABLE_DESKTOP_WINDOWING_BACK_NAVIGATION - ) - fun handleRequest_backTransition_multipleTasks_withWallpaper_withBackNav_removesTask() { - val task1 = setUpFreeformTask() - setUpFreeformTask() - - taskRepository.wallpaperActivityToken = MockToken().token() - val result = controller.handleRequest(Binder(), createTransition(task1, type = TRANSIT_TO_BACK)) - - assertNotNull(result, "Should handle request").assertRemoveAt(index = 0, task1.token) - } - - @Test @EnableFlags(Flags.FLAG_ENABLE_DESKTOP_WINDOWING_WALLPAPER_ACTIVITY) - @DisableFlags(Flags.FLAG_ENABLE_DESKTOP_WINDOWING_BACK_NAVIGATION) fun handleRequest_backTransition_multipleTasks_noBackNav_doesNotHandle() { val task1 = setUpFreeformTask() setUpFreeformTask() @@ -2226,29 +2190,11 @@ class DesktopTasksControllerTest : ShellTestCase() { // Should create remove wallpaper transaction assertNotNull(result, "Should handle request").assertRemoveAt(index = 0, wallpaperToken) - result.assertRemoveAt(index = 1, task1.token) - } - - @Test - @EnableFlags(Flags.FLAG_ENABLE_DESKTOP_WINDOWING_WALLPAPER_ACTIVITY) - @DisableFlags(Flags.FLAG_ENABLE_DESKTOP_WINDOWING_BACK_NAVIGATION) - fun handleRequest_backTransition_multipleTasksSingleNonClosing_noBackNav_removesWallpaper() { - val task1 = setUpFreeformTask(displayId = DEFAULT_DISPLAY) - val task2 = setUpFreeformTask(displayId = DEFAULT_DISPLAY) - val wallpaperToken = MockToken().token() - - taskRepository.wallpaperActivityToken = wallpaperToken - taskRepository.addClosingTask(displayId = DEFAULT_DISPLAY, taskId = task2.taskId) - val result = controller.handleRequest(Binder(), createTransition(task1, type = TRANSIT_TO_BACK)) - - // Should create remove wallpaper transaction - assertNotNull(result, "Should handle request").assertRemoveAt(index = 0, wallpaperToken) } @Test @EnableFlags( Flags.FLAG_ENABLE_DESKTOP_WINDOWING_WALLPAPER_ACTIVITY, - Flags.FLAG_ENABLE_DESKTOP_WINDOWING_BACK_NAVIGATION ) fun handleRequest_backTransition_multipleTasksSingleNonMinimized_removesWallpaperAndTask() { val task1 = setUpFreeformTask(displayId = DEFAULT_DISPLAY) @@ -2261,23 +2207,6 @@ class DesktopTasksControllerTest : ShellTestCase() { // Should create remove wallpaper transaction assertNotNull(result, "Should handle request").assertRemoveAt(index = 0, wallpaperToken) - result.assertRemoveAt(index = 1, task1.token) - } - - @Test - @EnableFlags(Flags.FLAG_ENABLE_DESKTOP_WINDOWING_WALLPAPER_ACTIVITY) - @DisableFlags(Flags.FLAG_ENABLE_DESKTOP_WINDOWING_BACK_NAVIGATION) - fun handleRequest_backTransition_multipleTasksSingleNonMinimized_noBackNav_removesWallpaper() { - val task1 = setUpFreeformTask(displayId = DEFAULT_DISPLAY) - val task2 = setUpFreeformTask(displayId = DEFAULT_DISPLAY) - val wallpaperToken = MockToken().token() - - taskRepository.wallpaperActivityToken = wallpaperToken - taskRepository.minimizeTask(displayId = DEFAULT_DISPLAY, taskId = task2.taskId) - val result = controller.handleRequest(Binder(), createTransition(task1, type = TRANSIT_TO_BACK)) - - // Should create remove wallpaper transaction - assertNotNull(result, "Should handle request").assertRemoveAt(index = 0, wallpaperToken) } @Test @@ -2937,6 +2866,108 @@ class DesktopTasksControllerTest : ShellTestCase() { } @Test + @EnableFlags(Flags.FLAG_ENABLE_DESKTOP_WINDOWING_MULTI_INSTANCE_FEATURES) + fun newWindow_fromFullscreenOpensInSplit() { + setUpLandscapeDisplay() + val task = setUpFullscreenTask() + val optionsCaptor = ArgumentCaptor.forClass(Bundle::class.java) + runOpenNewWindow(task) + verify(splitScreenController) + .startIntent(any(), anyInt(), any(), any(), + optionsCaptor.capture(), anyOrNull()) + assertThat(ActivityOptions.fromBundle(optionsCaptor.value).launchWindowingMode) + .isEqualTo(WINDOWING_MODE_MULTI_WINDOW) + } + + @Test + @EnableFlags(Flags.FLAG_ENABLE_DESKTOP_WINDOWING_MULTI_INSTANCE_FEATURES) + fun newWindow_fromSplitOpensInSplit() { + setUpLandscapeDisplay() + val task = setUpSplitScreenTask() + val optionsCaptor = ArgumentCaptor.forClass(Bundle::class.java) + runOpenNewWindow(task) + verify(splitScreenController) + .startIntent( + any(), anyInt(), any(), any(), + optionsCaptor.capture(), anyOrNull() + ) + assertThat(ActivityOptions.fromBundle(optionsCaptor.value).launchWindowingMode) + .isEqualTo(WINDOWING_MODE_MULTI_WINDOW) + } + + @Test + @EnableFlags(Flags.FLAG_ENABLE_DESKTOP_WINDOWING_MULTI_INSTANCE_FEATURES) + fun newWindow_fromFreeformAddsNewWindow() { + setUpLandscapeDisplay() + val task = setUpFreeformTask() + val wctCaptor = ArgumentCaptor.forClass(WindowContainerTransaction::class.java) + runOpenNewWindow(task) + verify(transitions).startTransition(anyInt(), wctCaptor.capture(), anyOrNull()) + assertThat(ActivityOptions.fromBundle(wctCaptor.value.hierarchyOps[0].launchOptions) + .launchWindowingMode).isEqualTo(WINDOWING_MODE_FREEFORM) + } + + private fun runOpenNewWindow(task: RunningTaskInfo) { + markTaskVisible(task) + task.baseActivity = mock(ComponentName::class.java) + task.isFocused = true + runningTasks.add(task) + controller.openNewWindow(task) + } + + @Test + @EnableFlags(Flags.FLAG_ENABLE_DESKTOP_WINDOWING_MULTI_INSTANCE_FEATURES) + fun openInstance_fromFullscreenOpensInSplit() { + setUpLandscapeDisplay() + val task = setUpFullscreenTask() + val taskToRequest = setUpFreeformTask() + val optionsCaptor = ArgumentCaptor.forClass(Bundle::class.java) + runOpenInstance(task, taskToRequest.taskId) + verify(splitScreenController) + .startTask(anyInt(), anyInt(), optionsCaptor.capture(), anyOrNull()) + assertThat(ActivityOptions.fromBundle(optionsCaptor.value).launchWindowingMode) + .isEqualTo(WINDOWING_MODE_MULTI_WINDOW) + } + + @Test + @EnableFlags(Flags.FLAG_ENABLE_DESKTOP_WINDOWING_MULTI_INSTANCE_FEATURES) + fun openInstance_fromSplitOpensInSplit() { + setUpLandscapeDisplay() + val task = setUpSplitScreenTask() + val taskToRequest = setUpFreeformTask() + val optionsCaptor = ArgumentCaptor.forClass(Bundle::class.java) + runOpenInstance(task, taskToRequest.taskId) + verify(splitScreenController) + .startTask(anyInt(), anyInt(), optionsCaptor.capture(), anyOrNull()) + assertThat(ActivityOptions.fromBundle(optionsCaptor.value).launchWindowingMode) + .isEqualTo(WINDOWING_MODE_MULTI_WINDOW) + } + + @Test + @EnableFlags(Flags.FLAG_ENABLE_DESKTOP_WINDOWING_MULTI_INSTANCE_FEATURES) + fun openInstance_fromFreeformAddsNewWindow() { + setUpLandscapeDisplay() + val task = setUpFreeformTask() + val taskToRequest = setUpFreeformTask() + val wctCaptor = ArgumentCaptor.forClass(WindowContainerTransaction::class.java) + runOpenInstance(task, taskToRequest.taskId) + verify(transitions).startTransition(anyInt(), wctCaptor.capture(), anyOrNull()) + assertThat(ActivityOptions.fromBundle(wctCaptor.value.hierarchyOps[0].launchOptions) + .launchWindowingMode).isEqualTo(WINDOWING_MODE_FREEFORM) + } + + private fun runOpenInstance( + callingTask: RunningTaskInfo, + requestedTaskId: Int + ) { + markTaskVisible(callingTask) + callingTask.baseActivity = mock(ComponentName::class.java) + callingTask.isFocused = true + runningTasks.add(callingTask) + controller.openInstance(callingTask, requestedTaskId) + } + + @Test fun toggleBounds_togglesToStableBounds() { val bounds = Rect(0, 0, 100, 100) val task = setUpFreeformTask(DEFAULT_DISPLAY, bounds) diff --git a/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/desktopmode/DesktopTasksTransitionObserverTest.kt b/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/desktopmode/DesktopTasksTransitionObserverTest.kt new file mode 100644 index 000000000000..c989d1640f80 --- /dev/null +++ b/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/desktopmode/DesktopTasksTransitionObserverTest.kt @@ -0,0 +1,138 @@ +/* + * 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.wm.shell.desktopmode + +import android.app.ActivityManager.RunningTaskInfo +import android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM +import android.content.ComponentName +import android.content.Context +import android.content.Intent +import android.platform.test.annotations.EnableFlags +import android.view.Display.DEFAULT_DISPLAY +import android.view.WindowManager.TRANSIT_TO_BACK +import android.window.IWindowContainerToken +import android.window.TransitionInfo +import android.window.TransitionInfo.Change +import android.window.WindowContainerToken +import com.android.modules.utils.testing.ExtendedMockitoRule +import com.android.window.flags.Flags +import com.android.wm.shell.ShellTaskOrganizer +import com.android.wm.shell.common.ShellExecutor +import com.android.wm.shell.shared.desktopmode.DesktopModeStatus +import com.android.wm.shell.sysui.ShellInit +import com.android.wm.shell.transition.Transitions +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.mockito.Mockito +import org.mockito.kotlin.any +import org.mockito.kotlin.mock +import org.mockito.kotlin.never +import org.mockito.kotlin.spy +import org.mockito.kotlin.verify +import org.mockito.kotlin.whenever + +class DesktopTasksTransitionObserverTest { + + @JvmField + @Rule + val extendedMockitoRule = + ExtendedMockitoRule.Builder(this) + .mockStatic(DesktopModeStatus::class.java) + .build()!! + + private val testExecutor = mock<ShellExecutor>() + private val mockShellInit = mock<ShellInit>() + private val transitions = mock<Transitions>() + private val context = mock<Context>() + private val shellTaskOrganizer = mock<ShellTaskOrganizer>() + private val taskRepository = mock<DesktopModeTaskRepository>() + + private lateinit var transitionObserver: DesktopTasksTransitionObserver + private lateinit var shellInit: ShellInit + + @Before + fun setup() { + whenever(DesktopModeStatus.canEnterDesktopMode(any())).thenReturn(true) + shellInit = spy(ShellInit(testExecutor)) + + transitionObserver = + DesktopTasksTransitionObserver( + context, taskRepository, transitions, shellTaskOrganizer, shellInit + ) + } + + @Test + @EnableFlags(Flags.FLAG_ENABLE_DESKTOP_WINDOWING_BACK_NAVIGATION) + fun backNavigation_taskMinimized() { + val task = createTaskInfo(1) + whenever(taskRepository.getVisibleTaskCount(any())).thenReturn(1) + + transitionObserver.onTransitionReady( + transition = mock(), + info = + createBackNavigationTransition(task), + startTransaction = mock(), + finishTransaction = mock(), + ) + + verify(taskRepository).minimizeTask(task.displayId, task.taskId) + } + + @Test + @EnableFlags(Flags.FLAG_ENABLE_DESKTOP_WINDOWING_BACK_NAVIGATION) + fun backNavigation_nullTaskInfo_taskNotMinimized() { + val task = createTaskInfo(1) + whenever(taskRepository.getVisibleTaskCount(any())).thenReturn(1) + + transitionObserver.onTransitionReady( + transition = mock(), + info = + createBackNavigationTransition(null), + startTransaction = mock(), + finishTransaction = mock(), + ) + + verify(taskRepository, never()).minimizeTask(task.displayId, task.taskId) + } + + private fun createBackNavigationTransition( + task: RunningTaskInfo? + ): TransitionInfo { + return TransitionInfo(TRANSIT_TO_BACK, 0 /* flags */).apply { + addChange( + Change(mock(), mock()).apply { + mode = TRANSIT_TO_BACK + parent = null + taskInfo = task + flags = flags + } + ) + } + } + + private fun createTaskInfo(id: Int) = + RunningTaskInfo().apply { + taskId = id + displayId = DEFAULT_DISPLAY + configuration.windowConfiguration.windowingMode = WINDOWING_MODE_FREEFORM + token = WindowContainerToken(Mockito.mock(IWindowContainerToken::class.java)) + baseIntent = Intent().apply { + component = ComponentName("package", "component.name") + } + } +} diff --git a/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/splitscreen/StageCoordinatorTests.java b/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/splitscreen/StageCoordinatorTests.java index a6c16c43c8cb..67eda8bfecd1 100644 --- a/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/splitscreen/StageCoordinatorTests.java +++ b/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/splitscreen/StageCoordinatorTests.java @@ -74,6 +74,7 @@ import com.android.wm.shell.splitscreen.SplitScreen.SplitScreenListener; import com.android.wm.shell.sysui.ShellController; import com.android.wm.shell.sysui.ShellInit; import com.android.wm.shell.transition.DefaultMixedHandler; +import com.android.wm.shell.transition.FocusTransitionObserver; import com.android.wm.shell.transition.HomeTransitionObserver; import com.android.wm.shell.transition.Transitions; @@ -429,7 +430,8 @@ public class StageCoordinatorTests extends ShellTestCase { ShellInit shellInit = new ShellInit(mMainExecutor); final Transitions t = new Transitions(mContext, shellInit, mock(ShellController.class), mTaskOrganizer, mTransactionPool, mock(DisplayController.class), mMainExecutor, - mMainHandler, mAnimExecutor, mock(HomeTransitionObserver.class)); + mMainHandler, mAnimExecutor, mock(HomeTransitionObserver.class), + mock(FocusTransitionObserver.class)); shellInit.init(); return t; } diff --git a/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/transition/FocusTransitionObserverTest.java b/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/transition/FocusTransitionObserverTest.java new file mode 100644 index 000000000000..d37b4cf4b4b3 --- /dev/null +++ b/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/transition/FocusTransitionObserverTest.java @@ -0,0 +1,155 @@ +/* + * 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.wm.shell.transition; + +import static android.view.Display.DEFAULT_DISPLAY; +import static android.view.WindowManager.TRANSIT_OPEN; +import static android.view.WindowManager.TRANSIT_TO_FRONT; +import static android.window.TransitionInfo.FLAG_MOVED_TO_TOP; + +import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify; + +import static org.mockito.Mockito.clearInvocations; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.when; + +import android.app.ActivityManager.RunningTaskInfo; +import android.os.Handler; +import android.os.IBinder; +import android.os.Looper; +import android.os.RemoteException; +import android.platform.test.annotations.RequiresFlagsEnabled; +import android.platform.test.flag.junit.CheckFlagsRule; +import android.platform.test.flag.junit.DeviceFlagsValueProvider; +import android.view.SurfaceControl; +import android.window.TransitionInfo; +import android.window.TransitionInfo.TransitionMode; + +import androidx.test.ext.junit.runners.AndroidJUnit4; +import androidx.test.filters.SmallTest; +import androidx.test.platform.app.InstrumentationRegistry; + +import com.android.window.flags.Flags; +import com.android.wm.shell.ShellTaskOrganizer; +import com.android.wm.shell.ShellTestCase; +import com.android.wm.shell.TestShellExecutor; +import com.android.wm.shell.common.DisplayController; +import com.android.wm.shell.shared.IFocusTransitionListener; +import com.android.wm.shell.shared.TransactionPool; +import com.android.wm.shell.sysui.ShellController; +import com.android.wm.shell.sysui.ShellInit; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.util.ArrayList; +import java.util.List; + +/** + * Tests for the focus transition observer. + */ +@SmallTest +@RunWith(AndroidJUnit4.class) +@RequiresFlagsEnabled(Flags.FLAG_ENABLE_DISPLAY_FOCUS_IN_SHELL_TRANSITIONS) +public class FocusTransitionObserverTest extends ShellTestCase { + + static final int SECONDARY_DISPLAY_ID = 1; + + @Rule + public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule(); + private IFocusTransitionListener mListener; + private Transitions mTransition; + private FocusTransitionObserver mFocusTransitionObserver; + + @Before + public void setUp() { + mListener = mock(IFocusTransitionListener.class); + when(mListener.asBinder()).thenReturn(mock(IBinder.class)); + + mFocusTransitionObserver = new FocusTransitionObserver(); + mTransition = + new Transitions(InstrumentationRegistry.getInstrumentation().getTargetContext(), + mock(ShellInit.class), mock(ShellController.class), + mock(ShellTaskOrganizer.class), mock(TransactionPool.class), + mock(DisplayController.class), new TestShellExecutor(), + new Handler(Looper.getMainLooper()), new TestShellExecutor(), + mock(HomeTransitionObserver.class), + mFocusTransitionObserver); + mFocusTransitionObserver.setRemoteFocusTransitionListener(mTransition, mListener); + } + + @Test + public void testTransitionWithMovedToFrontFlagChangesDisplayFocus() throws RemoteException { + final IBinder binder = mock(IBinder.class); + final SurfaceControl.Transaction tx = mock(SurfaceControl.Transaction.class); + + // Open a task on the default display, which doesn't change display focus because the + // default display already has it. + TransitionInfo info = mock(TransitionInfo.class); + final List<TransitionInfo.Change> changes = new ArrayList<>(); + setupChange(changes, 123 /* taskId */, TRANSIT_OPEN, DEFAULT_DISPLAY, + true /* focused */); + when(info.getChanges()).thenReturn(changes); + mFocusTransitionObserver.onTransitionReady(binder, info, tx, tx); + verify(mListener, never()).onFocusedDisplayChanged(SECONDARY_DISPLAY_ID); + clearInvocations(mListener); + + // Open a new task on the secondary display and verify display focus changes to the display. + changes.clear(); + setupChange(changes, 456 /* taskId */, TRANSIT_OPEN, SECONDARY_DISPLAY_ID, + true /* focused */); + when(info.getChanges()).thenReturn(changes); + mFocusTransitionObserver.onTransitionReady(binder, info, tx, tx); + verify(mListener, times(1)).onFocusedDisplayChanged(SECONDARY_DISPLAY_ID); + clearInvocations(mListener); + + // Open the first task to front and verify display focus goes back to the default display. + changes.clear(); + setupChange(changes, 123 /* taskId */, TRANSIT_TO_FRONT, DEFAULT_DISPLAY, + true /* focused */); + when(info.getChanges()).thenReturn(changes); + mFocusTransitionObserver.onTransitionReady(binder, info, tx, tx); + verify(mListener, times(1)).onFocusedDisplayChanged(DEFAULT_DISPLAY); + clearInvocations(mListener); + + // Open another task on the default display and verify no display focus switch as it's + // already on the default display. + changes.clear(); + setupChange(changes, 789 /* taskId */, TRANSIT_OPEN, DEFAULT_DISPLAY, + true /* focused */); + when(info.getChanges()).thenReturn(changes); + mFocusTransitionObserver.onTransitionReady(binder, info, tx, tx); + verify(mListener, never()).onFocusedDisplayChanged(DEFAULT_DISPLAY); + } + + private void setupChange(List<TransitionInfo.Change> changes, int taskId, + @TransitionMode int mode, int displayId, boolean focused) { + TransitionInfo.Change change = mock(TransitionInfo.Change.class); + RunningTaskInfo taskInfo = mock(RunningTaskInfo.class); + taskInfo.taskId = taskId; + taskInfo.isFocused = focused; + when(change.hasFlags(FLAG_MOVED_TO_TOP)).thenReturn(focused); + taskInfo.displayId = displayId; + when(change.getTaskInfo()).thenReturn(taskInfo); + when(change.getMode()).thenReturn(mode); + changes.add(change); + } +} diff --git a/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/transition/HomeTransitionObserverTest.java b/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/transition/HomeTransitionObserverTest.java index 8f49de0a98fb..8dfdfb4dcbcf 100644 --- a/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/transition/HomeTransitionObserverTest.java +++ b/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/transition/HomeTransitionObserverTest.java @@ -100,7 +100,8 @@ public class HomeTransitionObserverTest extends ShellTestCase { mHomeTransitionObserver = new HomeTransitionObserver(mContext, mMainExecutor); mTransition = new Transitions(mContext, mock(ShellInit.class), mock(ShellController.class), mOrganizer, mTransactionPool, mDisplayController, mMainExecutor, - mMainHandler, mAnimExecutor, mHomeTransitionObserver); + mMainHandler, mAnimExecutor, mHomeTransitionObserver, + mock(FocusTransitionObserver.class)); mHomeTransitionObserver.setHomeTransitionListener(mTransition, mListener); } diff --git a/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/transition/ShellTransitionTests.java b/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/transition/ShellTransitionTests.java index aea14b900647..6cde0569796d 100644 --- a/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/transition/ShellTransitionTests.java +++ b/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/transition/ShellTransitionTests.java @@ -158,7 +158,8 @@ public class ShellTransitionTests extends ShellTestCase { ShellInit shellInit = mock(ShellInit.class); final Transitions t = new Transitions(mContext, shellInit, mock(ShellController.class), mOrganizer, mTransactionPool, createTestDisplayController(), mMainExecutor, - mMainHandler, mAnimExecutor, mock(HomeTransitionObserver.class)); + mMainHandler, mAnimExecutor, mock(HomeTransitionObserver.class), + mock(FocusTransitionObserver.class)); // One from Transitions, one from RootTaskDisplayAreaOrganizer verify(shellInit).addInitCallback(any(), eq(t)); verify(shellInit).addInitCallback(any(), isA(RootTaskDisplayAreaOrganizer.class)); @@ -170,7 +171,8 @@ public class ShellTransitionTests extends ShellTestCase { ShellController shellController = mock(ShellController.class); final Transitions t = new Transitions(mContext, shellInit, shellController, mOrganizer, mTransactionPool, createTestDisplayController(), mMainExecutor, - mMainHandler, mAnimExecutor, mock(HomeTransitionObserver.class)); + mMainHandler, mAnimExecutor, mock(HomeTransitionObserver.class), + mock(FocusTransitionObserver.class)); shellInit.init(); verify(shellController, times(1)).addExternalInterface( eq(ShellSharedConstants.KEY_EXTRA_SHELL_SHELL_TRANSITIONS), any(), any()); @@ -1238,7 +1240,8 @@ public class ShellTransitionTests extends ShellTestCase { final Transitions transitions = new Transitions(mContext, shellInit, mock(ShellController.class), mOrganizer, mTransactionPool, createTestDisplayController(), mMainExecutor, - mMainHandler, mAnimExecutor, mock(HomeTransitionObserver.class)); + mMainHandler, mAnimExecutor, mock(HomeTransitionObserver.class), + mock(FocusTransitionObserver.class)); final RecentsTransitionHandler recentsHandler = new RecentsTransitionHandler(shellInit, mock(ShellTaskOrganizer.class), transitions, mock(RecentTasksController.class), mock(HomeTransitionObserver.class)); @@ -1780,7 +1783,8 @@ public class ShellTransitionTests extends ShellTestCase { ShellInit shellInit = new ShellInit(mMainExecutor); final Transitions t = new Transitions(mContext, shellInit, mock(ShellController.class), mOrganizer, mTransactionPool, createTestDisplayController(), mMainExecutor, - mMainHandler, mAnimExecutor, mock(HomeTransitionObserver.class)); + mMainHandler, mAnimExecutor, mock(HomeTransitionObserver.class), + mock(FocusTransitionObserver.class)); shellInit.init(); return t; } diff --git a/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/windowdecor/education/DesktopWindowingEducationTooltipControllerTest.kt b/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/windowdecor/education/DesktopWindowingEducationTooltipControllerTest.kt new file mode 100644 index 000000000000..5594981135b1 --- /dev/null +++ b/libs/WindowManager/Shell/tests/unittest/src/com/android/wm/shell/windowdecor/education/DesktopWindowingEducationTooltipControllerTest.kt @@ -0,0 +1,237 @@ +/* + * 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.wm.shell.windowdecor.education + +import android.annotation.LayoutRes +import android.content.Context +import android.graphics.Point +import android.testing.AndroidTestingRunner +import android.testing.TestableContext +import android.testing.TestableLooper +import android.testing.TestableResources +import android.view.MotionEvent +import android.view.View +import android.view.WindowManager +import android.widget.TextView +import androidx.test.filters.SmallTest +import com.android.wm.shell.R +import com.android.wm.shell.ShellTestCase +import com.android.wm.shell.windowdecor.additionalviewcontainer.AdditionalSystemViewContainer +import com.android.wm.shell.windowdecor.education.DesktopWindowingEducationTooltipController.TooltipArrowDirection +import com.google.common.truth.Truth.assertThat +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.ArgumentMatchers.anyInt +import org.mockito.Mock +import org.mockito.MockitoAnnotations +import org.mockito.kotlin.any +import org.mockito.kotlin.anyOrNull +import org.mockito.kotlin.argumentCaptor +import org.mockito.kotlin.mock +import org.mockito.kotlin.times +import org.mockito.kotlin.verify + +@SmallTest +@TestableLooper.RunWithLooper(setAsMainLooper = true) +@RunWith(AndroidTestingRunner::class) +class DesktopWindowingEducationTooltipControllerTest : ShellTestCase() { + @Mock private lateinit var mockWindowManager: WindowManager + @Mock private lateinit var mockViewContainerFactory: AdditionalSystemViewContainer.Factory + private lateinit var testableResources: TestableResources + private lateinit var testableContext: TestableContext + private lateinit var tooltipController: DesktopWindowingEducationTooltipController + private val tooltipViewArgumentCaptor = argumentCaptor<View>() + + @Before + fun setUp() { + MockitoAnnotations.initMocks(this) + testableContext = TestableContext(mContext) + testableResources = + testableContext.orCreateTestableResources.apply { + addOverride(R.dimen.desktop_windowing_education_tooltip_padding, 10) + } + testableContext.addMockSystemService( + Context.LAYOUT_INFLATER_SERVICE, context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)) + testableContext.addMockSystemService(WindowManager::class.java, mockWindowManager) + tooltipController = + DesktopWindowingEducationTooltipController(testableContext, mockViewContainerFactory) + } + + @Test + fun showEducationTooltip_createsTooltipWithCorrectText() { + val tooltipText = "This is a tooltip" + val tooltipViewConfig = createTooltipConfig(tooltipText = tooltipText) + + tooltipController.showEducationTooltip(tooltipViewConfig = tooltipViewConfig, taskId = 123) + + verify(mockViewContainerFactory, times(1)) + .create( + windowManagerWrapper = any(), + taskId = anyInt(), + x = anyInt(), + y = anyInt(), + width = anyInt(), + height = anyInt(), + flags = anyInt(), + view = tooltipViewArgumentCaptor.capture()) + val tooltipTextView = + tooltipViewArgumentCaptor.lastValue.findViewById<TextView>(R.id.tooltip_text) + assertThat(tooltipTextView.text).isEqualTo(tooltipText) + } + + @Test + fun showEducationTooltip_usesCorrectTaskIdForWindow() { + val tooltipViewConfig = createTooltipConfig() + val taskIdArgumentCaptor = argumentCaptor<Int>() + + tooltipController.showEducationTooltip(tooltipViewConfig = tooltipViewConfig, taskId = 123) + + verify(mockViewContainerFactory, times(1)) + .create( + windowManagerWrapper = any(), + taskId = taskIdArgumentCaptor.capture(), + x = anyInt(), + y = anyInt(), + width = anyInt(), + height = anyInt(), + flags = anyInt(), + view = anyOrNull()) + assertThat(taskIdArgumentCaptor.lastValue).isEqualTo(123) + } + + @Test + fun showEducationTooltip_tooltipPointsUpwards_horizontallyPositionTooltip() { + val initialTooltipX = 0 + val initialTooltipY = 0 + val tooltipViewConfig = + createTooltipConfig( + arrowDirection = TooltipArrowDirection.UP, + tooltipViewGlobalCoordinates = Point(initialTooltipX, initialTooltipY)) + val tooltipXArgumentCaptor = argumentCaptor<Int>() + val tooltipWidthArgumentCaptor = argumentCaptor<Int>() + + tooltipController.showEducationTooltip(tooltipViewConfig = tooltipViewConfig, taskId = 123) + + verify(mockViewContainerFactory, times(1)) + .create( + windowManagerWrapper = any(), + taskId = anyInt(), + x = tooltipXArgumentCaptor.capture(), + y = anyInt(), + width = tooltipWidthArgumentCaptor.capture(), + height = anyInt(), + flags = anyInt(), + view = tooltipViewArgumentCaptor.capture()) + val expectedTooltipX = initialTooltipX - tooltipWidthArgumentCaptor.lastValue / 2 + assertThat(tooltipXArgumentCaptor.lastValue).isEqualTo(expectedTooltipX) + } + + @Test + fun showEducationTooltip_tooltipPointsLeft_verticallyPositionTooltip() { + val initialTooltipX = 0 + val initialTooltipY = 0 + val tooltipViewConfig = + createTooltipConfig( + arrowDirection = TooltipArrowDirection.LEFT, + tooltipViewGlobalCoordinates = Point(initialTooltipX, initialTooltipY)) + val tooltipYArgumentCaptor = argumentCaptor<Int>() + val tooltipHeightArgumentCaptor = argumentCaptor<Int>() + + tooltipController.showEducationTooltip(tooltipViewConfig = tooltipViewConfig, taskId = 123) + + verify(mockViewContainerFactory, times(1)) + .create( + windowManagerWrapper = any(), + taskId = anyInt(), + x = anyInt(), + y = tooltipYArgumentCaptor.capture(), + width = anyInt(), + height = tooltipHeightArgumentCaptor.capture(), + flags = anyInt(), + view = tooltipViewArgumentCaptor.capture()) + val expectedTooltipY = initialTooltipY - tooltipHeightArgumentCaptor.lastValue / 2 + assertThat(tooltipYArgumentCaptor.lastValue).isEqualTo(expectedTooltipY) + } + + @Test + fun showEducationTooltip_touchEventActionOutside_dismissActionPerformed() { + val mockLambda: () -> Unit = mock() + val tooltipViewConfig = createTooltipConfig(onDismissAction = mockLambda) + + tooltipController.showEducationTooltip(tooltipViewConfig = tooltipViewConfig, taskId = 123) + verify(mockViewContainerFactory, times(1)) + .create( + windowManagerWrapper = any(), + taskId = anyInt(), + x = anyInt(), + y = anyInt(), + width = anyInt(), + height = anyInt(), + flags = anyInt(), + view = tooltipViewArgumentCaptor.capture()) + val motionEvent = + MotionEvent.obtain( + /* downTime= */ 0L, + /* eventTime= */ 0L, + MotionEvent.ACTION_OUTSIDE, + /* x= */ 0f, + /* y= */ 0f, + /* metaState= */ 0) + tooltipViewArgumentCaptor.lastValue.dispatchTouchEvent(motionEvent) + + verify(mockLambda).invoke() + } + + @Test + fun showEducationTooltip_tooltipClicked_onClickActionPerformed() { + val mockLambda: () -> Unit = mock() + val tooltipViewConfig = createTooltipConfig(onEducationClickAction = mockLambda) + + tooltipController.showEducationTooltip(tooltipViewConfig = tooltipViewConfig, taskId = 123) + verify(mockViewContainerFactory, times(1)) + .create( + windowManagerWrapper = any(), + taskId = anyInt(), + x = anyInt(), + y = anyInt(), + width = anyInt(), + height = anyInt(), + flags = anyInt(), + view = tooltipViewArgumentCaptor.capture()) + tooltipViewArgumentCaptor.lastValue.performClick() + + verify(mockLambda).invoke() + } + + private fun createTooltipConfig( + @LayoutRes tooltipViewLayout: Int = R.layout.desktop_windowing_education_top_arrow_tooltip, + tooltipViewGlobalCoordinates: Point = Point(0, 0), + tooltipText: String = "This is a tooltip", + arrowDirection: TooltipArrowDirection = TooltipArrowDirection.UP, + onEducationClickAction: () -> Unit = {}, + onDismissAction: () -> Unit = {} + ) = + DesktopWindowingEducationTooltipController.EducationViewConfig( + tooltipViewLayout = tooltipViewLayout, + tooltipViewGlobalCoordinates = tooltipViewGlobalCoordinates, + tooltipText = tooltipText, + arrowDirection = arrowDirection, + onEducationClickAction = onEducationClickAction, + onDismissAction = onDismissAction, + ) +} diff --git a/libs/appfunctions/api/current.txt b/libs/appfunctions/api/current.txt index 504e3290b0ae..3ed33db2222e 100644 --- a/libs/appfunctions/api/current.txt +++ b/libs/appfunctions/api/current.txt @@ -3,13 +3,15 @@ package com.google.android.appfunctions.sidecar { public final class AppFunctionManager { ctor public AppFunctionManager(android.content.Context); - method public void executeAppFunction(@NonNull com.google.android.appfunctions.sidecar.ExecuteAppFunctionRequest, @NonNull java.util.concurrent.Executor, @NonNull java.util.function.Consumer<com.google.android.appfunctions.sidecar.ExecuteAppFunctionResponse>); + method public void executeAppFunction(@NonNull com.google.android.appfunctions.sidecar.ExecuteAppFunctionRequest, @NonNull java.util.concurrent.Executor, @NonNull android.os.CancellationSignal, @NonNull java.util.function.Consumer<com.google.android.appfunctions.sidecar.ExecuteAppFunctionResponse>); + method @Deprecated public void executeAppFunction(@NonNull com.google.android.appfunctions.sidecar.ExecuteAppFunctionRequest, @NonNull java.util.concurrent.Executor, @NonNull java.util.function.Consumer<com.google.android.appfunctions.sidecar.ExecuteAppFunctionResponse>); } public abstract class AppFunctionService extends android.app.Service { ctor public AppFunctionService(); method @NonNull public final android.os.IBinder onBind(@Nullable android.content.Intent); - method @MainThread public abstract void onExecuteFunction(@NonNull com.google.android.appfunctions.sidecar.ExecuteAppFunctionRequest, @NonNull java.util.function.Consumer<com.google.android.appfunctions.sidecar.ExecuteAppFunctionResponse>); + method @MainThread public void onExecuteFunction(@NonNull com.google.android.appfunctions.sidecar.ExecuteAppFunctionRequest, @NonNull android.os.CancellationSignal, @NonNull java.util.function.Consumer<com.google.android.appfunctions.sidecar.ExecuteAppFunctionResponse>); + method @Deprecated @MainThread public abstract void onExecuteFunction(@NonNull com.google.android.appfunctions.sidecar.ExecuteAppFunctionRequest, @NonNull java.util.function.Consumer<com.google.android.appfunctions.sidecar.ExecuteAppFunctionResponse>); field @NonNull public static final String BIND_APP_FUNCTION_SERVICE = "android.permission.BIND_APP_FUNCTION_SERVICE"; field @NonNull public static final String SERVICE_INTERFACE = "android.app.appfunctions.AppFunctionService"; } diff --git a/libs/appfunctions/java/com/google/android/appfunctions/sidecar/AppFunctionManager.java b/libs/appfunctions/java/com/google/android/appfunctions/sidecar/AppFunctionManager.java index b1dd4676a35e..815fe05cc3ab 100644 --- a/libs/appfunctions/java/com/google/android/appfunctions/sidecar/AppFunctionManager.java +++ b/libs/appfunctions/java/com/google/android/appfunctions/sidecar/AppFunctionManager.java @@ -19,12 +19,12 @@ package com.google.android.appfunctions.sidecar; import android.annotation.CallbackExecutor; import android.annotation.NonNull; import android.content.Context; +import android.os.CancellationSignal; import java.util.Objects; import java.util.concurrent.Executor; import java.util.function.Consumer; - /** * Provides app functions related functionalities. * @@ -45,7 +45,7 @@ public final class AppFunctionManager { * * @param context A {@link Context}. * @throws java.lang.IllegalStateException if the underlying {@link - * android.app.appfunctions.AppFunctionManager} is not found. + * android.app.appfunctions.AppFunctionManager} is not found. */ public AppFunctionManager(Context context) { mContext = Objects.requireNonNull(context); @@ -66,6 +66,7 @@ public final class AppFunctionManager { public void executeAppFunction( @NonNull ExecuteAppFunctionRequest sidecarRequest, @NonNull @CallbackExecutor Executor executor, + @NonNull CancellationSignal cancellationSignal, @NonNull Consumer<ExecuteAppFunctionResponse> callback) { Objects.requireNonNull(sidecarRequest); Objects.requireNonNull(executor); @@ -74,9 +75,40 @@ public final class AppFunctionManager { android.app.appfunctions.ExecuteAppFunctionRequest platformRequest = SidecarConverter.getPlatformExecuteAppFunctionRequest(sidecarRequest); mManager.executeAppFunction( - platformRequest, executor, (platformResponse) -> { - callback.accept(SidecarConverter.getSidecarExecuteAppFunctionResponse( - platformResponse)); + platformRequest, + executor, + cancellationSignal, + (platformResponse) -> { + callback.accept( + SidecarConverter.getSidecarExecuteAppFunctionResponse( + platformResponse)); }); } + + /** + * Executes the app function. + * + * <p>Proxies request and response to the underlying {@link + * android.app.appfunctions.AppFunctionManager#executeAppFunction}, converting the request and + * response in the appropriate type required by the function. + * + * @deprecated Use {@link #executeAppFunction(ExecuteAppFunctionRequest, Executor, + * CancellationSignal, Consumer)} instead. This method will be removed once usage references + * are updated. + */ + @Deprecated + public void executeAppFunction( + @NonNull ExecuteAppFunctionRequest sidecarRequest, + @NonNull @CallbackExecutor Executor executor, + @NonNull Consumer<ExecuteAppFunctionResponse> callback) { + Objects.requireNonNull(sidecarRequest); + Objects.requireNonNull(executor); + Objects.requireNonNull(callback); + + executeAppFunction( + sidecarRequest, + executor, + new CancellationSignal(), + callback); + } } diff --git a/libs/appfunctions/java/com/google/android/appfunctions/sidecar/AppFunctionService.java b/libs/appfunctions/java/com/google/android/appfunctions/sidecar/AppFunctionService.java index 65959dfdf561..6023c977bd76 100644 --- a/libs/appfunctions/java/com/google/android/appfunctions/sidecar/AppFunctionService.java +++ b/libs/appfunctions/java/com/google/android/appfunctions/sidecar/AppFunctionService.java @@ -25,6 +25,7 @@ import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; +import android.os.CancellationSignal; import java.util.function.Consumer; @@ -69,10 +70,11 @@ public abstract class AppFunctionService extends Service { private final Binder mBinder = android.app.appfunctions.AppFunctionService.createBinder( /* context= */ this, - /* onExecuteFunction= */ (platformRequest, callback) -> { + /* onExecuteFunction= */ (platformRequest, cancellationSignal, callback) -> { AppFunctionService.this.onExecuteFunction( SidecarConverter.getSidecarExecuteAppFunctionRequest( platformRequest), + cancellationSignal, (sidecarResponse) -> { callback.accept( SidecarConverter.getPlatformExecuteAppFunctionResponse( @@ -105,9 +107,42 @@ public abstract class AppFunctionService extends Service { * result using the callback, no matter if the execution was successful or not. * * @param request The function execution request. + * @param cancellationSignal A {@link CancellationSignal} to cancel the request. * @param callback A callback to report back the result. */ @MainThread + public void onExecuteFunction( + @NonNull ExecuteAppFunctionRequest request, + @NonNull CancellationSignal cancellationSignal, + @NonNull Consumer<ExecuteAppFunctionResponse> callback) { + onExecuteFunction(request, callback); + } + + /** + * Called by the system to execute a specific app function. + * + * <p>This method is triggered when the system requests your AppFunctionService to handle a + * particular function you have registered and made available. + * + * <p>To ensure proper routing of function requests, assign a unique identifier to each + * function. This identifier doesn't need to be globally unique, but it must be unique within + * your app. For example, a function to order food could be identified as "orderFood". In most + * cases this identifier should come from the ID automatically generated by the AppFunctions + * SDK. You can determine the specific function to invoke by calling {@link + * ExecuteAppFunctionRequest#getFunctionIdentifier()}. + * + * <p>This method is always triggered in the main thread. You should run heavy tasks on a worker + * thread and dispatch the result with the given callback. You should always report back the + * result using the callback, no matter if the execution was successful or not. + * + * @param request The function execution request. + * @param callback A callback to report back the result. + * + * @deprecated Use {@link #onExecuteFunction(ExecuteAppFunctionRequest, CancellationSignal, + * Consumer)} instead. This method will be removed once usage references are updated. + */ + @MainThread + @Deprecated public abstract void onExecuteFunction( @NonNull ExecuteAppFunctionRequest request, @NonNull Consumer<ExecuteAppFunctionResponse> callback); diff --git a/media/java/android/media/tv/flags/media_tv.aconfig b/media/java/android/media/tv/flags/media_tv.aconfig index c814c95e09d9..10423b9c1f0f 100644 --- a/media/java/android/media/tv/flags/media_tv.aconfig +++ b/media/java/android/media/tv/flags/media_tv.aconfig @@ -56,3 +56,11 @@ flag { description: "Enhance HDMI-CEC power state and activeness transitions" bug: "332780751" } + +flag { + name: "media_quality_fw" + is_exported: true + namespace: "media_tv" + description: "Media Quality V1.0 APIs for Android W" + bug: "348412562" +} diff --git a/media/jni/android_media_ImageWriter.cpp b/media/jni/android_media_ImageWriter.cpp index 6776f611559c..33650d91e6a3 100644 --- a/media/jni/android_media_ImageWriter.cpp +++ b/media/jni/android_media_ImageWriter.cpp @@ -735,10 +735,15 @@ static void ImageWriter_queueImage(JNIEnv* env, jobject thiz, jlong nativeCtx, j } static status_t attachAndQeueuGraphicBuffer(JNIEnv* env, JNIImageWriterContext *ctx, - sp<Surface> surface, sp<GraphicBuffer> gb, jlong timestampNs, jint dataSpace, + sp<GraphicBuffer> gb, jlong timestampNs, jint dataSpace, jint left, jint top, jint right, jint bottom, jint transform, jint scalingMode) { status_t res = OK; // Step 1. Attach Image + sp<Surface> surface = ctx->getProducer(); + if (surface == nullptr) { + jniThrowException(env, "java/lang/IllegalStateException", + "Producer surface is null, ImageWriter seems already closed"); + } res = surface->attachBuffer(gb.get()); if (res != OK) { ALOGE("Attach image failed: %s (%d)", strerror(-res), res); @@ -835,7 +840,6 @@ static jint ImageWriter_attachAndQueueImage(JNIEnv* env, jobject thiz, jlong nat return -1; } - sp<Surface> surface = ctx->getProducer(); if (isFormatOpaque(ctx->getBufferFormat()) != isFormatOpaque(nativeHalFormat)) { jniThrowException(env, "java/lang/IllegalStateException", "Trying to attach an opaque image into a non-opaque ImageWriter, or vice versa"); @@ -851,7 +855,7 @@ static jint ImageWriter_attachAndQueueImage(JNIEnv* env, jobject thiz, jlong nat return -1; } - return attachAndQeueuGraphicBuffer(env, ctx, surface, buffer->mGraphicBuffer, timestampNs, + return attachAndQeueuGraphicBuffer(env, ctx, buffer->mGraphicBuffer, timestampNs, dataSpace, left, top, right, bottom, transform, scalingMode); } @@ -866,7 +870,6 @@ static jint ImageWriter_attachAndQueueGraphicBuffer(JNIEnv* env, jobject thiz, j return -1; } - sp<Surface> surface = ctx->getProducer(); if (isFormatOpaque(ctx->getBufferFormat()) != isFormatOpaque(nativeHalFormat)) { jniThrowException(env, "java/lang/IllegalStateException", "Trying to attach an opaque image into a non-opaque ImageWriter, or vice versa"); @@ -880,7 +883,8 @@ static jint ImageWriter_attachAndQueueGraphicBuffer(JNIEnv* env, jobject thiz, j "Trying to attach an invalid graphic buffer"); return -1; } - return attachAndQeueuGraphicBuffer(env, ctx, surface, graphicBuffer, timestampNs, + + return attachAndQeueuGraphicBuffer(env, ctx, graphicBuffer, timestampNs, dataSpace, left, top, right, bottom, transform, scalingMode); } diff --git a/packages/SettingsLib/Android.bp b/packages/SettingsLib/Android.bp index d4851e1ad698..af07686f064c 100644 --- a/packages/SettingsLib/Android.bp +++ b/packages/SettingsLib/Android.bp @@ -33,6 +33,7 @@ android_library { "SettingsLibBarChartPreference", "SettingsLibButtonPreference", "SettingsLibBulletPreference", + "SettingsLibCardPreference", "SettingsLibCollapsingToolbarBaseActivity", "SettingsLibDeviceStateRotationLock", "SettingsLibDisplayUtils", diff --git a/packages/SettingsLib/CardPreference/Android.bp b/packages/SettingsLib/CardPreference/Android.bp new file mode 100644 index 000000000000..1d871d168ee5 --- /dev/null +++ b/packages/SettingsLib/CardPreference/Android.bp @@ -0,0 +1,33 @@ +package { + // See: http://go/android-license-faq + // A large-scale-change added 'default_applicable_licenses' to import + // all of the 'license_kinds' from "frameworks_base_license" + // to get the below license kinds: + // SPDX-license-identifier-Apache-2.0 + default_applicable_licenses: ["frameworks_base_license"], +} + +android_library { + name: "SettingsLibCardPreference", + use_resource_processor: true, + defaults: [ + "SettingsLintDefaults", + ], + + srcs: [ + "src/**/*.java", + "src/**/*.kt", + ], + resource_dirs: ["res"], + + static_libs: [ + "androidx.annotation_annotation", + "androidx.preference_preference", + "SettingsLibSettingsTheme", + ], + sdk_version: "system_current", + min_sdk_version: "21", + apex_available: [ + "//apex_available:platform", + ], +} diff --git a/packages/SettingsLib/CardPreference/AndroidManifest.xml b/packages/SettingsLib/CardPreference/AndroidManifest.xml new file mode 100644 index 000000000000..717f66e0296c --- /dev/null +++ b/packages/SettingsLib/CardPreference/AndroidManifest.xml @@ -0,0 +1,23 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + 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. + --> + +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="com.android.settingslib.widget.preference.card"> + + <uses-sdk android:minSdkVersion="21" /> + +</manifest> diff --git a/packages/SettingsLib/CardPreference/res/layout/settingslib_expressive_preference_card.xml b/packages/SettingsLib/CardPreference/res/layout/settingslib_expressive_preference_card.xml new file mode 100644 index 000000000000..716ed412eb5c --- /dev/null +++ b/packages/SettingsLib/CardPreference/res/layout/settingslib_expressive_preference_card.xml @@ -0,0 +1,88 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + 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. +--> +<com.google.android.material.card.MaterialCardView + xmlns:android="http://schemas.android.com/apk/res/android" + style="@style/SettingsLibCardStyle"> + + <LinearLayout + android:id="@+id/card_container" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:baselineAligned="false" + android:minHeight="@dimen/settingslib_expressive_space_large3" + android:paddingStart="@dimen/settingslib_expressive_space_small1" + android:paddingEnd="@dimen/settingslib_expressive_space_small1" + android:orientation="horizontal" + android:gravity="center_vertical"> + + <LinearLayout + android:id="@+id/icon_frame" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:minWidth="@dimen/settingslib_expressive_space_medium3" + android:minHeight="@dimen/settingslib_expressive_space_medium3" + android:gravity="center" + android:orientation="horizontal"> + + <ImageView + android:id="@android:id/icon" + android:src="@drawable/settingslib_arrow_drop_down" + android:layout_width="@dimen/settingslib_expressive_space_medium3" + android:layout_height="@dimen/settingslib_expressive_space_medium3" + android:scaleType="centerInside"/> + + </LinearLayout> + + <LinearLayout + android:id="@+id/text_container" + android:layout_width="0dp" + android:layout_height="wrap_content" + android:layout_weight="1" + android:paddingHorizontal="@dimen/settingslib_expressive_space_small1" + android:paddingVertical="@dimen/settingslib_expressive_space_small2" + android:orientation="vertical"> + + <TextView + android:id="@android:id/title" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:hyphenationFrequency="normalFast" + android:lineBreakWordStyle="phrase" + android:textAppearance="@style/TextAppearance.CardTitle.SettingsLib"/> + + <TextView + android:id="@android:id/summary" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:hyphenationFrequency="normalFast" + android:lineBreakWordStyle="phrase" + android:textAppearance="@style/TextAppearance.CardSummary.SettingsLib"/> + + </LinearLayout> + + <ImageView + android:id="@android:id/closeButton" + android:layout_width="@dimen/settingslib_expressive_space_medium4" + android:layout_height="@dimen/settingslib_expressive_space_medium4" + android:padding="@dimen/settingslib_expressive_space_extrasmall4" + android:layout_gravity="center" + android:src="@drawable/settingslib_expressive_icon_close" + android:background="?android:attr/selectableItemBackground" /> + + </LinearLayout> + +</com.google.android.material.card.MaterialCardView>
\ No newline at end of file diff --git a/packages/SettingsLib/CardPreference/res/values/styles_expressive.xml b/packages/SettingsLib/CardPreference/res/values/styles_expressive.xml new file mode 100644 index 000000000000..4cbdea52d439 --- /dev/null +++ b/packages/SettingsLib/CardPreference/res/values/styles_expressive.xml @@ -0,0 +1,30 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + 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. +--> + +<resources> + <style name="TextAppearance.CardTitle.SettingsLib" + parent="@style/TextAppearance.PreferenceTitle.SettingsLib"> + <item name="android:textColor">@color/settingslib_materialColorOnPrimary</item> + <item name="android:textSize">20sp</item> + </style> + + <style name="TextAppearance.CardSummary.SettingsLib" + parent="@style/TextAppearance.PreferenceSummary.SettingsLib"> + <item name="android:textColor">@color/settingslib_materialColorOnSecondary</item> + <item name="android:textSize">14sp</item> + </style> +</resources>
\ No newline at end of file diff --git a/packages/SettingsLib/CardPreference/src/com/android/settingslib/widget/CardPreference.kt b/packages/SettingsLib/CardPreference/src/com/android/settingslib/widget/CardPreference.kt new file mode 100644 index 000000000000..eb14746a0f22 --- /dev/null +++ b/packages/SettingsLib/CardPreference/src/com/android/settingslib/widget/CardPreference.kt @@ -0,0 +1,58 @@ +/* + * 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.widget + +import android.content.Context +import android.util.AttributeSet +import android.view.View +import androidx.preference.Preference +import androidx.preference.PreferenceViewHolder +import com.android.settingslib.widget.preference.card.R + +/** + * The CardPreference shows a card like suggestion in homepage, which also support dismiss. + */ +class CardPreference @JvmOverloads constructor( + context: Context, + attrs: AttributeSet? = null, + defStyleAttr: Int = 0, + defStyleRes: Int = 0 +) : Preference(context, attrs, defStyleAttr, defStyleRes) { + + init { + layoutResource = R.layout.settingslib_expressive_preference_card + } + private var dismissible = false + set(value) { + if (field != value) { + field = value + notifyChanged() + } + } + + override fun onBindViewHolder(holder: PreferenceViewHolder) { + super.onBindViewHolder(holder) + holder.isDividerAllowedBelow = false + holder.isDividerAllowedAbove = false + + holder.findViewById(android.R.id.closeButton)?.let { dismissButton -> + dismissButton.visibility = if (dismissible) View.VISIBLE else View.GONE + dismissButton.setOnClickListener { + isVisible = false + } + } + } +}
\ No newline at end of file diff --git a/packages/SettingsLib/SettingsTheme/res/drawable-v35/settingslib_expressive_icon_collapse.xml b/packages/SettingsLib/SettingsTheme/res/drawable-v35/settingslib_expressive_icon_collapse.xml new file mode 100644 index 000000000000..161ece73f21c --- /dev/null +++ b/packages/SettingsLib/SettingsTheme/res/drawable-v35/settingslib_expressive_icon_collapse.xml @@ -0,0 +1,36 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + 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. + --> +<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> + <item> + <shape + android:shape="oval"> + <size android:width="24dp" android:height="24dp"/> + <solid android:color="@color/settingslib_materialColorSurfaceDim"/> + </shape> + </item> + <item> + <vector + android:width="24dp" + android:height="24dp" + android:viewportWidth="960" + android:viewportHeight="960"> + <path + android:fillColor="@color/settingslib_materialColorOnSurface" + android:pathData="M480,432L296,616L240,560L480,320L720,560L664,616L480,432Z"/> + </vector> + </item> +</layer-list>
\ No newline at end of file diff --git a/packages/SettingsLib/SettingsTheme/res/drawable-v35/settingslib_expressive_icon_expand.xml b/packages/SettingsLib/SettingsTheme/res/drawable-v35/settingslib_expressive_icon_expand.xml new file mode 100644 index 000000000000..1b5d5182d9b2 --- /dev/null +++ b/packages/SettingsLib/SettingsTheme/res/drawable-v35/settingslib_expressive_icon_expand.xml @@ -0,0 +1,37 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + 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. + --> + +<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> + <item> + <shape + android:shape="oval"> + <size android:width="24dp" android:height="24dp"/> + <solid android:color="@color/settingslib_materialColorSurfaceDim"/> + </shape> + </item> + <item> + <vector + android:width="24dp" + android:height="24dp" + android:viewportWidth="960" + android:viewportHeight="960"> + <path + android:fillColor="@color/settingslib_materialColorOnSurface" + android:pathData="M480,616L240,376L296,320L480,504L664,320L720,376L480,616Z"/> + </vector> + </item> +</layer-list>
\ No newline at end of file diff --git a/packages/SettingsLib/SettingsTheme/res/layout-v35/settingslib_expressive_collapsable_textview.xml b/packages/SettingsLib/SettingsTheme/res/layout-v35/settingslib_expressive_collapsable_textview.xml new file mode 100644 index 000000000000..245d3682636b --- /dev/null +++ b/packages/SettingsLib/SettingsTheme/res/layout-v35/settingslib_expressive_collapsable_textview.xml @@ -0,0 +1,51 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + 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. + --> + +<androidx.constraintlayout.widget.ConstraintLayout + xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:app="http://schemas.android.com/apk/res-auto" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:paddingBottom="@dimen/settingslib_expressive_space_small1" + android:paddingTop="@dimen/settingslib_expressive_space_extrasmall4" + android:orientation="vertical" + android:animateLayoutChanges="true" + android:background="?android:attr/selectableItemBackground" + android:clipToPadding="false"> + + <TextView + android:id="@android:id/title" + android:layout_width="0dp" + android:layout_height="wrap_content" + app:layout_constraintTop_toTopOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintEnd_toEndOf="parent" + android:textAlignment="viewStart" + android:clickable="false" + android:longClickable="false" + android:maxLines="10" + android:ellipsize="end" + android:textAppearance="@style/TextAppearance.TopIntroText"/> + + <com.google.android.material.button.MaterialButton + android:id="@+id/collapse_button" + app:layout_constraintTop_toBottomOf="@android:id/title" + app:layout_constraintStart_toStartOf="parent" + android:text="@string/settingslib_expressive_text_expand" + app:icon="@drawable/settingslib_expressive_icon_expand" + style="@style/SettingslibTextButtonStyle.Expressive"/> +</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file diff --git a/packages/SettingsLib/SettingsTheme/res/values-v35/attrs_expressive.xml b/packages/SettingsLib/SettingsTheme/res/values-v35/attrs_expressive.xml new file mode 100644 index 000000000000..857dd7953234 --- /dev/null +++ b/packages/SettingsLib/SettingsTheme/res/values-v35/attrs_expressive.xml @@ -0,0 +1,25 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- 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. +--> + +<resources> + <declare-styleable name="CollapsableTextView"> + <attr name="android:gravity"/> + <!-- The minimum number of lines when the textView collapsed. --> + <attr name="android:minLines"/> + <!-- Specifies that the textView is collapsable. --> + <attr name="isCollapsable" format="boolean"/> + </declare-styleable> +</resources>
\ No newline at end of file diff --git a/packages/SettingsLib/SettingsTheme/res/values-v35/strings.xml b/packages/SettingsLib/SettingsTheme/res/values-v35/strings.xml new file mode 100644 index 000000000000..22734068733a --- /dev/null +++ b/packages/SettingsLib/SettingsTheme/res/values-v35/strings.xml @@ -0,0 +1,24 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + 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. + --> + +<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> + + <!-- text of button to indicate user the textView is expandable [CHAR LIMIT=NONE] --> + <string name="settingslib_expressive_text_expand">Expand</string> + <!-- text of button to indicate user the textView is collapsable [CHAR LIMIT=NONE] --> + <string name="settingslib_expressive_text_collapse">Collapse</string> +</resources>
\ No newline at end of file diff --git a/packages/SettingsLib/SettingsTheme/res/values-v35/styles_expressive.xml b/packages/SettingsLib/SettingsTheme/res/values-v35/styles_expressive.xml index 816433c1a18b..250c27e8b581 100644 --- a/packages/SettingsLib/SettingsTheme/res/values-v35/styles_expressive.xml +++ b/packages/SettingsLib/SettingsTheme/res/values-v35/styles_expressive.xml @@ -170,6 +170,52 @@ <item name="thumbIcon">@drawable/settingslib_expressive_switch_thumb_icon</item> </style> + <style name="SettingslibMainSwitchStyle.Expressive" parent="SettingslibSwitchStyle.Expressive"> + <item name="android:layout_gravity">center</item> + <item name="trackTint">@color/settingslib_expressive_color_main_switch_track</item> + </style> + + <style name="SettingsLibCardStyle" parent=""> + <item name="android:layout_width">match_parent</item> + <item name="android:layout_height">wrap_content</item> + <item name="android:layout_marginHorizontal">?android:attr/listPreferredItemPaddingStart</item> + <item name="android:layout_marginVertical">@dimen/settingslib_expressive_space_extrasmall4</item> + <item name="cardBackgroundColor">@color/settingslib_materialColorPrimary</item> + <item name="cardCornerRadius">@dimen/settingslib_expressive_radius_extralarge3</item> + <item name="cardElevation">0dp</item> + <item name="rippleColor">?android:attr/colorControlHighlight</item> + </style> + + <style name="SettingsLibButtonStyle.Expressive.Filled" + parent="@style/Widget.Material3.Button"> + <item name="android:theme">@style/Theme.Material3.DynamicColors.DayNight</item> + <item name="android:layout_width">wrap_content</item> + <item name="android:layout_height">wrap_content</item> + <item name="android:gravity">center</item> + <item name="android:minWidth">@dimen/settingslib_expressive_space_medium4</item> + <item name="android:minHeight">@dimen/settingslib_expressive_space_medium4</item> + <item name="android:paddingVertical">@dimen/settingslib_expressive_space_extrasmall5</item> + <item name="android:paddingHorizontal">@dimen/settingslib_expressive_space_small1</item> + <item name="android:backgroundTint">@color/settingslib_materialColorPrimary</item> + <item name="android:textAppearance">@android:style/TextAppearance.DeviceDefault.Medium</item> + <item name="android:textColor">@color/settingslib_materialColorOnPrimary</item> + <item name="android:textSize">14sp</item> + <item name="iconGravity">textStart</item> + <item name="iconTint">@color/settingslib_materialColorOnPrimary</item> + <item name="iconSize">@dimen/settingslib_expressive_space_small4</item> + </style> + + <style name="SettingsLibButtonStyle.Expressive.Filled.Large"> + <item name="android:paddingVertical">@dimen/settingslib_expressive_space_small1</item> + <item name="android:paddingHorizontal">@dimen/settingslib_expressive_space_small4</item> + <item name="android:textSize">16sp</item> + </style> + + <style name="SettingsLibButtonStyle.Expressive.Filled.Extra" + parent="@style/SettingsLibButtonStyle.Expressive.Filled.Large"> + <item name="android:layout_width">match_parent</item> + </style> + <style name="SettingsLibButtonStyle.Expressive.Tonal" parent="@style/Widget.Material3.Button.TonalButton"> <item name="android:theme">@style/Theme.Material3.DynamicColors.DayNight</item> @@ -189,8 +235,59 @@ <item name="iconSize">@dimen/settingslib_expressive_space_small4</item> </style> - <style name="SettingslibMainSwitchStyle.Expressive" parent="SettingslibSwitchStyle.Expressive"> - <item name="android:layout_gravity">center</item> - <item name="trackTint">@color/settingslib_expressive_color_main_switch_track</item> + <style name="SettingsLibButtonStyle.Expressive.Tonal.Large"> + <item name="android:paddingVertical">@dimen/settingslib_expressive_space_small1</item> + <item name="android:paddingHorizontal">@dimen/settingslib_expressive_space_small4</item> + <item name="android:textSize">16sp</item> + </style> + + <style name="SettingsLibButtonStyle.Expressive.Tonal.Extra" + parent="@style/SettingsLibButtonStyle.Expressive.Tonal.Large"> + <item name="android:layout_width">match_parent</item> + </style> + + <style name="SettingsLibButtonStyle.Expressive.Outline" + parent="@style/Widget.Material3.Button.OutlinedButton.Icon"> + <item name="android:theme">@style/Theme.Material3.DynamicColors.DayNight</item> + <item name="android:layout_width">wrap_content</item> + <item name="android:layout_height">wrap_content</item> + <item name="android:gravity">center</item> + <item name="android:minWidth">@dimen/settingslib_expressive_space_medium4</item> + <item name="android:minHeight">@dimen/settingslib_expressive_space_medium4</item> + <item name="android:paddingVertical">@dimen/settingslib_expressive_space_extrasmall5</item> + <item name="android:paddingHorizontal">@dimen/settingslib_expressive_space_small1</item> + <item name="android:textAppearance">@android:style/TextAppearance.DeviceDefault.Medium</item> + <item name="android:textColor">@color/settingslib_materialColorPrimary</item> + <item name="android:textSize">14sp</item> + <item name="iconTint">@color/settingslib_materialColorPrimary</item> + <item name="iconGravity">textStart</item> + <item name="iconSize">@dimen/settingslib_expressive_space_small4</item> + <item name="iconPadding">@dimen/settingslib_expressive_space_extrasmall4</item> + <item name="strokeColor">@color/settingslib_materialColorOutlineVariant</item> + + </style> + + <style name="SettingsLibButtonStyle.Expressive.Outline.Large"> + <item name="android:paddingVertical">@dimen/settingslib_expressive_space_small1</item> + <item name="android:paddingHorizontal">@dimen/settingslib_expressive_space_small4</item> + <item name="android:textSize">16sp</item> + </style> + + <style name="SettingsLibButtonStyle.Expressive.Outline.Extra" + parent="@style/SettingsLibButtonStyle.Expressive.Outline.Large"> + <item name="android:layout_width">match_parent</item> + </style> + + <style name="SettingslibTextButtonStyle.Expressive" + parent="@style/Widget.Material3.Button.TextButton.Icon"> + <item name="android:theme">@style/Theme.Material3.DynamicColors.DayNight</item> + <item name="android:layout_width">wrap_content</item> + <item name="android:layout_height">wrap_content</item> + <item name="android:textAppearance">@android:style/TextAppearance.DeviceDefault.Medium</item> + <item name="android:textSize">16sp</item> + <item name="android:textColor">@color/settingslib_materialColorOnSurface</item> + <item name="iconTint">@null</item> + <item name="iconPadding">@dimen/settingslib_expressive_space_extrasmall4</item> + <item name="rippleColor">?android:attr/colorControlHighlight</item> </style> </resources>
\ No newline at end of file diff --git a/packages/SettingsLib/SettingsTheme/src/com/android/settingslib/widget/CollapsableTextView.kt b/packages/SettingsLib/SettingsTheme/src/com/android/settingslib/widget/CollapsableTextView.kt new file mode 100644 index 000000000000..127f21a540ab --- /dev/null +++ b/packages/SettingsLib/SettingsTheme/src/com/android/settingslib/widget/CollapsableTextView.kt @@ -0,0 +1,150 @@ +/* + * 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.settingslib.widget + +import android.content.Context +import android.graphics.drawable.Drawable +import android.util.AttributeSet +import android.view.Gravity +import android.view.LayoutInflater +import android.view.View +import android.widget.TextView +import androidx.constraintlayout.widget.ConstraintLayout +import com.android.settingslib.widget.theme.R +import com.google.android.material.button.MaterialButton + +class CollapsableTextView @JvmOverloads constructor( + context: Context, + attrs: AttributeSet? = null, + defStyleAttr: Int = 0 +) : ConstraintLayout(context, attrs, defStyleAttr) { + + private var isCollapsable: Boolean = false + private var isCollapsed: Boolean = false + private var minLines: Int = DEFAULT_MIN_LINES + + private val titleTextView: TextView + private val collapseButton: MaterialButton + private val collapseButtonResources: CollapseButtonResources + + init { + LayoutInflater.from(context) + .inflate(R.layout.settingslib_expressive_collapsable_textview, this) + titleTextView = findViewById(android.R.id.title) + collapseButton = findViewById(R.id.collapse_button) + + collapseButtonResources = CollapseButtonResources( + context.getDrawable(R.drawable.settingslib_expressive_icon_collapse)!!, + context.getDrawable(R.drawable.settingslib_expressive_icon_expand)!!, + context.getString(R.string.settingslib_expressive_text_collapse), + context.getString(R.string.settingslib_expressive_text_expand) + ) + + collapseButton.setOnClickListener { + isCollapsed = !isCollapsed + updateView() + } + + initAttributes(context, attrs, defStyleAttr) + } + + private fun initAttributes(context: Context, attrs: AttributeSet?, defStyleAttr: Int) { + context.obtainStyledAttributes( + attrs, Attrs, defStyleAttr, 0 + ).apply { + val gravity = getInt(GravityAttr, Gravity.START) + when (gravity) { + Gravity.CENTER_VERTICAL, Gravity.CENTER, Gravity.CENTER_HORIZONTAL -> { + centerHorizontally(titleTextView) + centerHorizontally(collapseButton) + } + } + recycle() + } + } + + private fun centerHorizontally(view: View) { + (view.layoutParams as LayoutParams).apply { + startToStart = LayoutParams.PARENT_ID + endToEnd = LayoutParams.PARENT_ID + horizontalBias = 0.5f + } + } + + /** + * Sets the text content of the CollapsableTextView. + * @param text The text to display. + */ + fun setText(text: String) { + titleTextView.text = text + } + + /** + * Sets whether the text view is collapsable. + * @param collapsable True if the text view should be collapsable, false otherwise. + */ + fun setCollapsable(collapsable: Boolean) { + isCollapsable = collapsable + updateView() + } + + /** + * Sets the minimum number of lines to display when collapsed. + * @param lines The minimum number of lines. + */ + fun setMinLines(line: Int) { + minLines = line.coerceIn(1, DEFAULT_MAX_LINES) + updateView() + } + + private fun updateView() { + when { + isCollapsed -> { + collapseButton.apply { + text = collapseButtonResources.expandText + icon = collapseButtonResources.expandIcon + } + titleTextView.maxLines = minLines + } + + else -> { + collapseButton.apply { + text = collapseButtonResources.collapseText + icon = collapseButtonResources.collapseIcon + } + titleTextView.maxLines = DEFAULT_MAX_LINES + } + } + collapseButton.visibility = if (isCollapsable) VISIBLE else GONE + } + + private data class CollapseButtonResources( + val collapseIcon: Drawable, + val expandIcon: Drawable, + val collapseText: String, + val expandText: String + ) + + companion object { + private const val DEFAULT_MAX_LINES = 10 + private const val DEFAULT_MIN_LINES = 2 + + private val Attrs = R.styleable.CollapsableTextView + private val GravityAttr = R.styleable.CollapsableTextView_android_gravity + } +} + diff --git a/packages/SettingsLib/Spa/spa/src/com/android/settingslib/spa/framework/theme/SettingsTypography.kt b/packages/SettingsLib/Spa/spa/src/com/android/settingslib/spa/framework/theme/SettingsTypography.kt index 460bf9993b41..965c97124329 100644 --- a/packages/SettingsLib/Spa/spa/src/com/android/settingslib/spa/framework/theme/SettingsTypography.kt +++ b/packages/SettingsLib/Spa/spa/src/com/android/settingslib/spa/framework/theme/SettingsTypography.kt @@ -162,3 +162,6 @@ internal fun rememberSettingsTypography(): Typography { /** Creates a new [TextStyle] which font weight set to medium. */ internal fun TextStyle.toMediumWeight() = copy(fontWeight = FontWeight.Medium, letterSpacing = 0.01.em) + +internal fun TextStyle.toSemiBoldWeight() = + copy(fontWeight = FontWeight.SemiBold, letterSpacing = 0.01.em) diff --git a/packages/SettingsLib/Spa/spa/src/com/android/settingslib/spa/widget/button/ActionButtons.kt b/packages/SettingsLib/Spa/spa/src/com/android/settingslib/spa/widget/button/ActionButtons.kt index 7e1df1694b10..5bb57b8ed1df 100644 --- a/packages/SettingsLib/Spa/spa/src/com/android/settingslib/spa/widget/button/ActionButtons.kt +++ b/packages/SettingsLib/Spa/spa/src/com/android/settingslib/spa/widget/button/ActionButtons.kt @@ -110,7 +110,7 @@ private fun RowScope.ActionButton(actionButton: ActionButton) { shape = RectangleShape, colors = ButtonDefaults.filledTonalButtonColors( containerColor = MaterialTheme.colorScheme.primaryContainer, - contentColor = MaterialTheme.colorScheme.onPrimary, + contentColor = MaterialTheme.colorScheme.onPrimaryContainer, disabledContainerColor = MaterialTheme.colorScheme.surface, ), contentPadding = PaddingValues(horizontal = 24.dp, vertical = 16.dp), diff --git a/packages/SettingsLib/Spa/spa/src/com/android/settingslib/spa/widget/scaffold/Actions.kt b/packages/SettingsLib/Spa/spa/src/com/android/settingslib/spa/widget/scaffold/Actions.kt index 94d2c210daab..f99d20669183 100644 --- a/packages/SettingsLib/Spa/spa/src/com/android/settingslib/spa/widget/scaffold/Actions.kt +++ b/packages/SettingsLib/Spa/spa/src/com/android/settingslib/spa/widget/scaffold/Actions.kt @@ -41,9 +41,7 @@ import com.android.settingslib.spa.framework.theme.isSpaExpressiveEnabled internal fun NavigateBack() { val navController = LocalNavController.current val contentDescription = stringResource(R.string.abc_action_bar_up_description) - BackAction(contentDescription) { - navController.navigateBack() - } + BackAction(contentDescription) { navController.navigateBack() } } /** Action that collapses the search bar. */ @@ -55,15 +53,35 @@ internal fun CollapseAction(onClick: () -> Unit) { @Composable private fun BackAction(contentDescription: String, onClick: () -> Unit) { - IconButton(onClick) { + IconButton( + onClick = onClick, + modifier = + if (isSpaExpressiveEnabled) + Modifier + .padding( + start = SettingsDimension.paddingLarge, + end = SettingsDimension.paddingSmall, + top = SettingsDimension.paddingExtraSmall, + bottom = SettingsDimension.paddingExtraSmall, + ) + .size(SettingsDimension.actionIconWidth, SettingsDimension.actionIconHeight) + .clip(SettingsShape.CornerExtraLarge) + else Modifier, + ) { Icon( imageVector = Icons.AutoMirrored.Outlined.ArrowBack, contentDescription = contentDescription, - modifier = if (isSpaExpressiveEnabled) Modifier - .size(SettingsDimension.actionIconWidth, SettingsDimension.actionIconHeight) - .clip(SettingsShape.CornerExtraLarge) - .background(MaterialTheme.colorScheme.surfaceContainerHigh) - .padding(SettingsDimension.actionIconPadding) else Modifier + modifier = + if (isSpaExpressiveEnabled) + Modifier + .size( + SettingsDimension.actionIconWidth, + SettingsDimension.actionIconHeight, + ) + .clip(SettingsShape.CornerExtraLarge) + .background(MaterialTheme.colorScheme.surfaceContainerHighest) + .padding(SettingsDimension.actionIconPadding) + else Modifier, ) } } diff --git a/packages/SettingsLib/Spa/spa/src/com/android/settingslib/spa/widget/scaffold/CustomizedAppBar.kt b/packages/SettingsLib/Spa/spa/src/com/android/settingslib/spa/widget/scaffold/CustomizedAppBar.kt index 2ae3b569bc70..2c55779c9a01 100644 --- a/packages/SettingsLib/Spa/spa/src/com/android/settingslib/spa/widget/scaffold/CustomizedAppBar.kt +++ b/packages/SettingsLib/Spa/spa/src/com/android/settingslib/spa/widget/scaffold/CustomizedAppBar.kt @@ -78,7 +78,9 @@ import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.Velocity import androidx.compose.ui.unit.dp import com.android.settingslib.spa.framework.theme.SettingsDimension +import com.android.settingslib.spa.framework.theme.isSpaExpressiveEnabled import com.android.settingslib.spa.framework.theme.settingsBackground +import com.android.settingslib.spa.framework.theme.toSemiBoldWeight import kotlin.math.abs import kotlin.math.max import kotlin.math.roundToInt @@ -116,8 +118,12 @@ internal fun CustomizedLargeTopAppBar( ) { TwoRowsTopAppBar( title = { Title(title = title, maxLines = 3) }, - titleTextStyle = MaterialTheme.typography.displaySmall, - smallTitleTextStyle = MaterialTheme.typography.titleMedium, + titleTextStyle = + if (isSpaExpressiveEnabled) MaterialTheme.typography.displaySmall.toSemiBoldWeight() + else MaterialTheme.typography.displaySmall, + smallTitleTextStyle = + if (isSpaExpressiveEnabled) MaterialTheme.typography.titleLarge.toSemiBoldWeight() + else MaterialTheme.typography.titleLarge, titleBottomPadding = LargeTitleBottomPadding, smallTitle = { Title(title = title, maxLines = 1) }, modifier = modifier, @@ -136,7 +142,9 @@ private fun Title(title: String, maxLines: Int = Int.MAX_VALUE) { text = title, modifier = Modifier.padding( - start = SettingsDimension.itemPaddingAround, + start = + if (isSpaExpressiveEnabled) SettingsDimension.paddingExtraSmall + else SettingsDimension.itemPaddingAround, end = SettingsDimension.itemPaddingEnd, ) .semantics { heading() }, @@ -194,7 +202,7 @@ private class TopAppBarColors( return lerp( containerColor, scrolledContainerColor, - FastOutLinearInEasing.transform(colorTransitionFraction) + FastOutLinearInEasing.transform(colorTransitionFraction), ) } @@ -241,7 +249,7 @@ private fun SingleRowTopAppBar( Row( horizontalArrangement = Arrangement.End, verticalAlignment = Alignment.CenterVertically, - content = actions + content = actions, ) } @@ -296,7 +304,7 @@ private fun TwoRowsTopAppBar( windowInsets: WindowInsets, colors: TopAppBarColors, pinnedHeight: Dp, - scrollBehavior: TopAppBarScrollBehavior? + scrollBehavior: TopAppBarScrollBehavior?, ) { if (MaxHeightWithoutTitle <= pinnedHeight) { throw IllegalArgumentException( @@ -333,7 +341,7 @@ private fun TwoRowsTopAppBar( Row( horizontalArrangement = Arrangement.End, verticalAlignment = Alignment.CenterVertically, - content = actions + content = actions, ) } val topTitleAlpha = { TopTitleAlphaEasing.transform(colorTransitionFraction()) } @@ -356,9 +364,9 @@ private fun TwoRowsTopAppBar( scrollBehavior.state, velocity, scrollBehavior.flingAnimationSpec, - scrollBehavior.snapAnimationSpec + scrollBehavior.snapAnimationSpec, ) - } + }, ) } else { Modifier @@ -412,7 +420,8 @@ private fun TwoRowsTopAppBar( val measuredMaxHeightPx = density.run { MaxHeightWithoutTitle.toPx() + - coordinates.size.height.toFloat() + coordinates.size.height.toFloat() + + titleBaselineHeight.toPx() } // Allow larger max height for multi-line title, but do not reduce // max height to prevent flaky. @@ -430,7 +439,7 @@ private fun TwoRowsTopAppBar( titleBottomPadding = titleBottomPaddingPx, hideTitleSemantics = hideBottomRowSemantics, navigationIcon = {}, - actions = {} + actions = {}, ) } } @@ -485,7 +494,7 @@ private fun TopAppBarLayout( Box(Modifier.layoutId("navigationIcon").padding(start = TopAppBarHorizontalPadding)) { CompositionLocalProvider( LocalContentColor provides navigationIconContentColor, - content = navigationIcon + content = navigationIcon, ) } Box( @@ -504,18 +513,18 @@ private fun TopAppBarLayout( fontScale = if (titleScaleDisabled) 1f else fontScale, ) }, - content = title + content = title, ) } } Box(Modifier.layoutId("actionIcons").padding(end = TopAppBarHorizontalPadding)) { CompositionLocalProvider( LocalContentColor provides actionIconContentColor, - content = actions + content = actions, ) } }, - modifier = modifier + modifier = modifier, ) { measurables, constraints -> val navigationIconPlaceable = measurables @@ -552,7 +561,7 @@ private fun TopAppBarLayout( // Navigation icon navigationIconPlaceable.placeRelative( x = 0, - y = (layoutHeight - navigationIconPlaceable.height) / 2 + y = (layoutHeight - navigationIconPlaceable.height) / 2, ) // Title @@ -570,17 +579,17 @@ private fun TopAppBarLayout( titlePlaceable.height - max( 0, - titleBottomPadding - titlePlaceable.height + titleBaseline + titleBottomPadding - titlePlaceable.height + titleBaseline, ) // Arrangement.Top else -> 0 - } + }, ) // Action icons actionIconsPlaceable.placeRelative( x = constraints.maxWidth - actionIconsPlaceable.width, - y = (layoutHeight - actionIconsPlaceable.height) / 2 + y = (layoutHeight - actionIconsPlaceable.height) / 2, ) } } @@ -595,7 +604,7 @@ private suspend fun settleAppBar( state: TopAppBarState, velocity: Float, flingAnimationSpec: DecayAnimationSpec<Float>?, - snapAnimationSpec: AnimationSpec<Float>? + snapAnimationSpec: AnimationSpec<Float>?, ): Velocity { // Check if the app bar is completely collapsed/expanded. If so, no need to settle the app bar, // and just return Zero Velocity. @@ -609,20 +618,18 @@ private suspend fun settleAppBar( // continue the motion to expand or collapse the app bar. if (flingAnimationSpec != null && abs(velocity) > 1f) { var lastValue = 0f - AnimationState( - initialValue = 0f, - initialVelocity = velocity, - ) - .animateDecay(flingAnimationSpec) { - val delta = value - lastValue - val initialHeightOffset = state.heightOffset - state.heightOffset = initialHeightOffset + delta - val consumed = abs(initialHeightOffset - state.heightOffset) - lastValue = value - remainingVelocity = this.velocity - // avoid rounding errors and stop if anything is unconsumed - if (abs(delta - consumed) > 0.5f) this.cancelAnimation() - } + AnimationState(initialValue = 0f, initialVelocity = velocity).animateDecay( + flingAnimationSpec + ) { + val delta = value - lastValue + val initialHeightOffset = state.heightOffset + state.heightOffset = initialHeightOffset + delta + val consumed = abs(initialHeightOffset - state.heightOffset) + lastValue = value + remainingVelocity = this.velocity + // avoid rounding errors and stop if anything is unconsumed + if (abs(delta - consumed) > 0.5f) this.cancelAnimation() + } } // Snap if animation specs were provided. if (snapAnimationSpec != null) { @@ -633,7 +640,7 @@ private suspend fun settleAppBar( } else { state.heightOffsetLimit }, - animationSpec = snapAnimationSpec + animationSpec = snapAnimationSpec, ) { state.heightOffset = value } @@ -647,9 +654,10 @@ private suspend fun settleAppBar( // Medium or Large app bar. private val TopTitleAlphaEasing = CubicBezierEasing(.8f, 0f, .8f, .15f) -internal val MaxHeightWithoutTitle = 124.dp +internal val MaxHeightWithoutTitle = if (isSpaExpressiveEnabled) 84.dp else 124.dp internal val DefaultTitleHeight = 52.dp internal val ContainerHeight = 56.dp +private val titleBaselineHeight = if (isSpaExpressiveEnabled) 8.dp else 0.dp private val LargeTitleBottomPadding = 28.dp private val TopAppBarHorizontalPadding = 4.dp diff --git a/packages/SettingsLib/TopIntroPreference/Android.bp b/packages/SettingsLib/TopIntroPreference/Android.bp index e70201b0feb7..76e36dc5ff7d 100644 --- a/packages/SettingsLib/TopIntroPreference/Android.bp +++ b/packages/SettingsLib/TopIntroPreference/Android.bp @@ -14,7 +14,10 @@ android_library { "SettingsLintDefaults", ], - srcs: ["src/**/*.java"], + srcs: [ + "src/**/*.java", + "src/**/*.kt", + ], resource_dirs: ["res"], static_libs: [ diff --git a/packages/SettingsLib/TopIntroPreference/res/layout-v35/settingslib_expressive_top_intro.xml b/packages/SettingsLib/TopIntroPreference/res/layout-v35/settingslib_expressive_top_intro.xml new file mode 100644 index 000000000000..fb13ef79cc3b --- /dev/null +++ b/packages/SettingsLib/TopIntroPreference/res/layout-v35/settingslib_expressive_top_intro.xml @@ -0,0 +1,27 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + 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. + --> +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:paddingEnd="?android:attr/listPreferredItemPaddingEnd" + android:paddingStart="?android:attr/listPreferredItemPaddingStart"> + + <com.android.settingslib.widget.CollapsableTextView + android:id="@+id/collapsable_text_view" + android:layout_width="match_parent" + android:layout_height="wrap_content" /> +</LinearLayout>
\ No newline at end of file diff --git a/packages/SettingsLib/TopIntroPreference/src/com/android/settingslib/widget/TopIntroPreference.java b/packages/SettingsLib/TopIntroPreference/src/com/android/settingslib/widget/TopIntroPreference.java deleted file mode 100644 index 1bbd76d86b7f..000000000000 --- a/packages/SettingsLib/TopIntroPreference/src/com/android/settingslib/widget/TopIntroPreference.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * 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.widget; - -import android.content.Context; -import android.util.AttributeSet; - -import androidx.preference.Preference; -import androidx.preference.PreferenceViewHolder; - -import com.android.settingslib.widget.preference.topintro.R; - -/** - * The TopIntroPreference shows a text which describe a feature. Gernerally, we expect this - * preference always shows on the top of screen. - */ -public class TopIntroPreference extends Preference { - - public TopIntroPreference(Context context) { - super(context); - setLayoutResource(R.layout.top_intro_preference); - setSelectable(false); - } - - public TopIntroPreference(Context context, AttributeSet attrs) { - super(context, attrs); - setLayoutResource(R.layout.top_intro_preference); - setSelectable(false); - } - - @Override - public void onBindViewHolder(PreferenceViewHolder holder) { - super.onBindViewHolder(holder); - holder.setDividerAllowedAbove(false); - holder.setDividerAllowedBelow(false); - } -} diff --git a/packages/SettingsLib/TopIntroPreference/src/com/android/settingslib/widget/TopIntroPreference.kt b/packages/SettingsLib/TopIntroPreference/src/com/android/settingslib/widget/TopIntroPreference.kt new file mode 100644 index 000000000000..afced0c8d638 --- /dev/null +++ b/packages/SettingsLib/TopIntroPreference/src/com/android/settingslib/widget/TopIntroPreference.kt @@ -0,0 +1,108 @@ +/* + * 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.settingslib.widget + +import android.content.Context +import android.os.Build +import android.util.AttributeSet +import androidx.annotation.RequiresApi +import androidx.preference.Preference +import androidx.preference.PreferenceViewHolder +import com.android.settingslib.widget.preference.topintro.R + +open class TopIntroPreference @JvmOverloads constructor( + context: Context, + attrs: AttributeSet? = null, + defStyleAttr: Int = 0, + defStyleRes: Int = 0 +) : Preference(context, attrs, defStyleAttr, defStyleRes) { + + private var isCollapsable: Boolean = false + private var minLines: Int = 2 + + init { + if (SettingsThemeHelper.isExpressiveTheme(context)) { + layoutResource = R.layout.settingslib_expressive_top_intro + initAttributes(context, attrs, defStyleAttr) + } else { + layoutResource = R.layout.top_intro_preference + } + isSelectable = false + } + + private fun initAttributes(context: Context, attrs: AttributeSet?, defStyleAttr: Int) { + context.obtainStyledAttributes( + attrs, + COLLAPSABLE_TEXT_VIEW_ATTRS, defStyleAttr, 0 + ).apply { + isCollapsable = getBoolean(IS_COLLAPSABLE, false) + minLines = getInt( + MIN_LINES, + if (isCollapsable) DEFAULT_MIN_LINES else DEFAULT_MAX_LINES + ).coerceIn(1, DEFAULT_MAX_LINES) + recycle() + } + } + + override fun onBindViewHolder(holder: PreferenceViewHolder) { + super.onBindViewHolder(holder) + holder.isDividerAllowedAbove = false + holder.isDividerAllowedBelow = false + + if (!SettingsThemeHelper.isExpressiveTheme(context)) { + return + } + + (holder.findViewById(R.id.collapsable_text_view) as? CollapsableTextView)?.apply { + setCollapsable(isCollapsable) + setMinLines(minLines) + setText(title.toString()) + } + } + + /** + * Sets whether the text view is collapsable. + * @param collapsable True if the text view should be collapsable, false otherwise. + */ + @RequiresApi(Build.VERSION_CODES.VANILLA_ICE_CREAM) + fun setCollapsable(collapsable: Boolean) { + isCollapsable = collapsable + notifyChanged() + } + + /** + * Sets the minimum number of lines to display when collapsed. + * @param lines The minimum number of lines. + */ + @RequiresApi(Build.VERSION_CODES.VANILLA_ICE_CREAM) + fun setMinLines(lines: Int) { + minLines = lines.coerceIn(1, DEFAULT_MAX_LINES) + notifyChanged() + } + + companion object { + private const val DEFAULT_MAX_LINES = 10 + private const val DEFAULT_MIN_LINES = 2 + + private val COLLAPSABLE_TEXT_VIEW_ATTRS = + com.android.settingslib.widget.theme.R.styleable.CollapsableTextView + private val MIN_LINES = + com.android.settingslib.widget.theme.R.styleable.CollapsableTextView_android_minLines + private val IS_COLLAPSABLE = + com.android.settingslib.widget.theme.R.styleable.CollapsableTextView_isCollapsable + } +} diff --git a/packages/SettingsLib/res/values/strings.xml b/packages/SettingsLib/res/values/strings.xml index 34e33c0df8f5..efc98dbf7102 100644 --- a/packages/SettingsLib/res/values/strings.xml +++ b/packages/SettingsLib/res/values/strings.xml @@ -1645,13 +1645,13 @@ <string name="media_transfer_headphone_name">Headphone</string> <!-- Name of the usb audio device speaker, used in desktop devices. [CHAR LIMIT=50] --> - <string name="media_transfer_usb_speaker_name">USB speaker</string> + <string name="media_transfer_usb_audio_name">USB audio</string> <!-- Name of the 3.5mm audio device mic. [CHAR LIMIT=50] --> <string name="media_transfer_wired_device_mic_name">Mic jack</string> <!-- Name of the usb audio device mic. [CHAR LIMIT=50] --> - <string name="media_transfer_usb_device_mic_name">USB mic</string> + <string name="media_transfer_usb_device_mic_name">USB microphone</string> <!-- Label for Wifi hotspot switch on. Toggles hotspot on [CHAR LIMIT=30] --> <string name="wifi_hotspot_switch_on_text">On</string> diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/devicesettings/DeviceSettingId.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/devicesettings/DeviceSettingId.java index 58dc8c7aad6c..e7c7476d4797 100644 --- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/devicesettings/DeviceSettingId.java +++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/devicesettings/DeviceSettingId.java @@ -45,6 +45,7 @@ import java.lang.annotation.RetentionPolicy; DeviceSettingId.DEVICE_SETTING_ID_KEYBOARD_SETTINGS, DeviceSettingId.DEVICE_SETTING_ID_DEVICE_DETAILS_FOOTER, DeviceSettingId.DEVICE_SETTING_ID_ANC, + DeviceSettingId.DEVICE_SETTING_ID_GENERAL_BLUETOOTH_DEVICE_HEADER, }, open = true) public @interface DeviceSettingId { @@ -114,6 +115,9 @@ public @interface DeviceSettingId { /** Device setting ID for "More Settings" page. */ int DEVICE_SETTING_ID_MORE_SETTINGS = 21; + /** Device setting ID for general bluetooth device header. */ + int DEVICE_SETTING_ID_GENERAL_BLUETOOTH_DEVICE_HEADER = 22; + /** Device setting ID for ANC. */ int DEVICE_SETTING_ID_ANC = 1001; } diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/devicesettings/DeviceSettingItem.kt b/packages/SettingsLib/src/com/android/settingslib/bluetooth/devicesettings/DeviceSettingItem.kt index 38183d5a01fd..da01b3bcaafb 100644 --- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/devicesettings/DeviceSettingItem.kt +++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/devicesettings/DeviceSettingItem.kt @@ -32,9 +32,9 @@ import android.os.Parcelable */ data class DeviceSettingItem( @DeviceSettingId val settingId: Int, - val packageName: String, - val className: String, - val intentAction: String, + val packageName: String? = null, + val className: String? = null, + val intentAction: String? = null, val preferenceKey: String? = null, val highlighted: Boolean = false, val extras: Bundle = Bundle.EMPTY, @@ -62,11 +62,11 @@ data class DeviceSettingItem( parcel.run { DeviceSettingItem( settingId = readInt(), - packageName = readString() ?: "", - className = readString() ?: "", - intentAction = readString() ?: "", + packageName = readString(), + className = readString(), + intentAction = readString(), highlighted = readBoolean(), - preferenceKey = readString() ?: "", + preferenceKey = readString(), extras = readBundle((Bundle::class.java.classLoader)) ?: Bundle.EMPTY, ) } diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/devicesettings/DeviceSettingsConfigServiceStatus.aidl b/packages/SettingsLib/src/com/android/settingslib/bluetooth/devicesettings/DeviceSettingsConfigServiceStatus.aidl new file mode 100644 index 000000000000..d8378067b115 --- /dev/null +++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/devicesettings/DeviceSettingsConfigServiceStatus.aidl @@ -0,0 +1,19 @@ +/* + * 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.settingslib.bluetooth.devicesettings; + +parcelable DeviceSettingsConfigServiceStatus;
\ No newline at end of file diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/devicesettings/DeviceSettingsConfigServiceStatus.kt b/packages/SettingsLib/src/com/android/settingslib/bluetooth/devicesettings/DeviceSettingsConfigServiceStatus.kt new file mode 100644 index 000000000000..ae867713c831 --- /dev/null +++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/devicesettings/DeviceSettingsConfigServiceStatus.kt @@ -0,0 +1,60 @@ +/* + * 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.settingslib.bluetooth.devicesettings + +import android.os.Bundle +import android.os.Parcel +import android.os.Parcelable + +/** + * A data class representing a device settings config service status. + * + * @property success Whether the status is succeed. + * @property extras Extra bundle + */ +data class DeviceSettingsConfigServiceStatus( + val success: Boolean, + val extras: Bundle = Bundle.EMPTY, +) : Parcelable { + + override fun describeContents(): Int = 0 + + override fun writeToParcel(parcel: Parcel, flags: Int) { + parcel.run { + writeBoolean(success) + writeBundle(extras) + } + } + + companion object { + @JvmField + val CREATOR: Parcelable.Creator<DeviceSettingsConfigServiceStatus> = + object : Parcelable.Creator<DeviceSettingsConfigServiceStatus> { + override fun createFromParcel(parcel: Parcel) = + parcel.run { + DeviceSettingsConfigServiceStatus( + success = readBoolean(), + extras = readBundle((Bundle::class.java.classLoader)) ?: Bundle.EMPTY, + ) + } + + override fun newArray(size: Int): Array<DeviceSettingsConfigServiceStatus?> { + return arrayOfNulls(size) + } + } + } +} diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/devicesettings/DeviceSettingsProviderServiceStatus.kt b/packages/SettingsLib/src/com/android/settingslib/bluetooth/devicesettings/DeviceSettingsProviderServiceStatus.kt index 977849e75556..77d790e7f773 100644 --- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/devicesettings/DeviceSettingsProviderServiceStatus.kt +++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/devicesettings/DeviceSettingsProviderServiceStatus.kt @@ -21,7 +21,7 @@ import android.os.Parcel import android.os.Parcelable /** - * A data class representing a device settings item in bluetooth device details config. + * A data class representing a device settings provider service status. * * @property enabled Whether the service is enabled. * @property extras Extra bundle diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/devicesettings/IDeviceSettingsConfigProviderService.aidl b/packages/SettingsLib/src/com/android/settingslib/bluetooth/devicesettings/IDeviceSettingsConfigProviderService.aidl index 647611ed8ef4..9cf49070a62c 100644 --- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/devicesettings/IDeviceSettingsConfigProviderService.aidl +++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/devicesettings/IDeviceSettingsConfigProviderService.aidl @@ -17,8 +17,8 @@ package com.android.settingslib.bluetooth.devicesettings; import com.android.settingslib.bluetooth.devicesettings.DeviceInfo; -import com.android.settingslib.bluetooth.devicesettings.DeviceSettingsConfig; +import com.android.settingslib.bluetooth.devicesettings.IGetDeviceSettingsConfigCallback; interface IDeviceSettingsConfigProviderService { - DeviceSettingsConfig getDeviceSettingsConfig(in DeviceInfo device); + oneway void getDeviceSettingsConfig(in DeviceInfo device, in IGetDeviceSettingsConfigCallback callback); }
\ No newline at end of file diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/devicesettings/IGetDeviceSettingsConfigCallback.aidl b/packages/SettingsLib/src/com/android/settingslib/bluetooth/devicesettings/IGetDeviceSettingsConfigCallback.aidl new file mode 100644 index 000000000000..403cdd9e4d70 --- /dev/null +++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/devicesettings/IGetDeviceSettingsConfigCallback.aidl @@ -0,0 +1,24 @@ +/* + * 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.settingslib.bluetooth.devicesettings; + +import com.android.settingslib.bluetooth.devicesettings.DeviceSettingsConfig; +import com.android.settingslib.bluetooth.devicesettings.DeviceSettingsConfigServiceStatus; + +interface IGetDeviceSettingsConfigCallback { + oneway void onResult(in DeviceSettingsConfigServiceStatus status, in DeviceSettingsConfig config) = 0; +}
\ No newline at end of file diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/devicesettings/data/repository/DeviceSettingServiceConnection.kt b/packages/SettingsLib/src/com/android/settingslib/bluetooth/devicesettings/data/repository/DeviceSettingServiceConnection.kt index 3d8ff86c9377..4af0504bd73a 100644 --- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/devicesettings/data/repository/DeviceSettingServiceConnection.kt +++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/devicesettings/data/repository/DeviceSettingServiceConnection.kt @@ -33,12 +33,15 @@ import com.android.settingslib.bluetooth.devicesettings.DeviceSettingId import com.android.settingslib.bluetooth.devicesettings.DeviceSettingPreferenceState import com.android.settingslib.bluetooth.devicesettings.DeviceSettingState import com.android.settingslib.bluetooth.devicesettings.DeviceSettingsConfig +import com.android.settingslib.bluetooth.devicesettings.DeviceSettingsConfigServiceStatus import com.android.settingslib.bluetooth.devicesettings.IDeviceSettingsConfigProviderService import com.android.settingslib.bluetooth.devicesettings.IDeviceSettingsListener import com.android.settingslib.bluetooth.devicesettings.IDeviceSettingsProviderService +import com.android.settingslib.bluetooth.devicesettings.IGetDeviceSettingsConfigCallback import com.android.settingslib.bluetooth.devicesettings.data.model.ServiceConnectionStatus import java.util.concurrent.ConcurrentHashMap import kotlin.coroutines.CoroutineContext +import kotlin.coroutines.resume import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineStart import kotlinx.coroutines.ExperimentalCoroutinesApi @@ -63,6 +66,7 @@ import kotlinx.coroutines.flow.shareIn import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.launch import kotlinx.coroutines.plus +import kotlinx.coroutines.suspendCancellableCoroutine import kotlinx.coroutines.withContext @OptIn(ExperimentalCoroutinesApi::class) @@ -74,22 +78,22 @@ class DeviceSettingServiceConnection( private val backgroundCoroutineContext: CoroutineContext, ) { data class EndPoint( - private val packageName: String, + private val packageName: String?, private val className: String?, - private val intentAction: String, + private val intentAction: String?, ) { - fun toIntent(): Intent = - Intent().apply { + fun toIntent(): Intent? { + if (TextUtils.isEmpty(packageName) || TextUtils.isEmpty(intentAction)) { + return null + } + return Intent().apply { if (className.isNullOrBlank()) { setPackage(packageName) } else { - setClassName(packageName, className) + setClassName(packageName!!, className) } setAction(intentAction) } - - fun isValid(): Boolean { - return !TextUtils.isEmpty(packageName) && !TextUtils.isEmpty(intentAction) } } @@ -126,8 +130,9 @@ class DeviceSettingServiceConnection( when (it) { is ServiceConnectionStatus.Connected -> flowOf( - it.service.getDeviceSettingsConfig( - deviceInfo { setBluetoothAddress(cachedDevice.address) } + getDeviceSettingsConfigFromService( + deviceInfo { setBluetoothAddress(cachedDevice.address) }, + it.service, ) ) ServiceConnectionStatus.Connecting -> flowOf() @@ -137,6 +142,27 @@ class DeviceSettingServiceConnection( .first() } + private suspend fun getDeviceSettingsConfigFromService( + deviceInfo: DeviceInfo, + service: IDeviceSettingsConfigProviderService, + ): DeviceSettingsConfig? = suspendCancellableCoroutine { continuation -> + service.getDeviceSettingsConfig( + deviceInfo, + object : IGetDeviceSettingsConfigCallback.Stub() { + override fun onResult( + status: DeviceSettingsConfigServiceStatus, + config: DeviceSettingsConfig?, + ) { + if (!status.success) { + continuation.resume(null) + } else { + continuation.resume(config) + } + } + }, + ) + } + private val settingIdToItemMapping = flow { if (!isServiceEnabled.await()) { @@ -160,6 +186,12 @@ class DeviceSettingServiceConnection( } .shareIn(scope = coroutineScope, started = SharingStarted.WhileSubscribed(), replay = 1) + private val services = + ConcurrentHashMap< + EndPoint, + StateFlow<ServiceConnectionStatus<IDeviceSettingsProviderService>>, + >() + /** Gets [DeviceSettingsConfig] for the device, return null when failed. */ suspend fun getDeviceSettingsConfig(): DeviceSettingsConfig? { if (!isServiceEnabled.await()) { @@ -222,24 +254,23 @@ class DeviceSettingServiceConnection( ) } } - ?.filter { it.isValid() } ?.distinct() - ?.associateBy( - { it }, - { endpoint -> - services.computeIfAbsent(endpoint) { - getService( - endpoint.toIntent(), - IDeviceSettingsProviderService.Stub::asInterface, - ) - .stateIn( - coroutineScope.plus(backgroundCoroutineContext), - SharingStarted.WhileSubscribed(), - ServiceConnectionStatus.Connecting, - ) - } - }, - ) + ?.mapNotNull { endpoint -> + endpoint.toIntent()?.let { intent -> + Pair( + endpoint, + services.computeIfAbsent(endpoint) { + getService(intent, IDeviceSettingsProviderService.Stub::asInterface) + .stateIn( + coroutineScope.plus(backgroundCoroutineContext), + SharingStarted.WhileSubscribed(), + ServiceConnectionStatus.Connecting, + ) + }, + ) + } + } + ?.toMap() private fun getDeviceSettingsFromService( cachedDevice: CachedBluetoothDevice, @@ -320,11 +351,5 @@ class DeviceSettingServiceConnection( const val CONFIG_SERVICE_PACKAGE_NAME = "DEVICE_SETTINGS_CONFIG_PACKAGE_NAME" const val CONFIG_SERVICE_CLASS_NAME = "DEVICE_SETTINGS_CONFIG_CLASS" const val CONFIG_SERVICE_INTENT_ACTION = "DEVICE_SETTINGS_CONFIG_ACTION" - - val services = - ConcurrentHashMap< - EndPoint, - StateFlow<ServiceConnectionStatus<IDeviceSettingsProviderService>>, - >() } } diff --git a/packages/SettingsLib/src/com/android/settingslib/graph/SignalDrawable.java b/packages/SettingsLib/src/com/android/settingslib/graph/SignalDrawable.java index ef0f6cbc6ed9..13a06017abbc 100644 --- a/packages/SettingsLib/src/com/android/settingslib/graph/SignalDrawable.java +++ b/packages/SettingsLib/src/com/android/settingslib/graph/SignalDrawable.java @@ -42,6 +42,8 @@ import androidx.annotation.Nullable; import com.android.settingslib.R; import com.android.settingslib.Utils; +import java.util.Objects; + /** * Drawable displaying a mobile cell signal indicator. */ @@ -90,6 +92,10 @@ public class SignalDrawable extends DrawableWrapper { private int mCurrentDot; public SignalDrawable(Context context) { + this(context, new Handler()); + } + + public SignalDrawable(@NonNull Context context, @NonNull Handler handler) { super(context.getDrawable(ICON_RES)); final String attributionPathString = context.getString( com.android.internal.R.string.config_signalAttributionPath); @@ -106,7 +112,7 @@ public class SignalDrawable extends DrawableWrapper { mIntrinsicSize = context.getResources().getDimensionPixelSize(R.dimen.signal_icon_size); mTransparentPaint.setColor(context.getColor(android.R.color.transparent)); mTransparentPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); - mHandler = new Handler(); + mHandler = handler; setDarkIntensity(0); } @@ -304,6 +310,17 @@ public class SignalDrawable extends DrawableWrapper { | level; } + @Override + public boolean equals(@Nullable Object other) { + return other instanceof SignalDrawable + && ((SignalDrawable) other).getLevel() == this.getLevel(); + } + + @Override + public int hashCode() { + return Objects.hash(getLevel()); + } + /** Returns the state representing empty mobile signal with the given number of levels. */ public static int getEmptyState(int numLevels) { return getState(0, numLevels, true); diff --git a/packages/SettingsLib/src/com/android/settingslib/media/PhoneMediaDevice.java b/packages/SettingsLib/src/com/android/settingslib/media/PhoneMediaDevice.java index 0b8fb22cef3a..feaf7fbc4b64 100644 --- a/packages/SettingsLib/src/com/android/settingslib/media/PhoneMediaDevice.java +++ b/packages/SettingsLib/src/com/android/settingslib/media/PhoneMediaDevice.java @@ -97,7 +97,7 @@ public class PhoneMediaDevice extends MediaDevice { case TYPE_USB_ACCESSORY: name = inputRoutingEnabledAndIsDesktop() - ? context.getString(R.string.media_transfer_usb_speaker_name) + ? context.getString(R.string.media_transfer_usb_audio_name) : context.getString(R.string.media_transfer_wired_headphone_name); break; case TYPE_DOCK: diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/devicesettings/DeviceSettingsConfigServiceStatusTest.kt b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/devicesettings/DeviceSettingsConfigServiceStatusTest.kt new file mode 100644 index 000000000000..3149acf6fc3c --- /dev/null +++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/devicesettings/DeviceSettingsConfigServiceStatusTest.kt @@ -0,0 +1,51 @@ +/* + * 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.settingslib.bluetooth.devicesettings + +import android.os.Bundle +import android.os.Parcel +import com.google.common.truth.Truth.assertThat +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner + +@RunWith(RobolectricTestRunner::class) +class DeviceSettingsConfigServiceStatusTest { + + @Test + fun parcelOperation() { + val item = + DeviceSettingsConfigServiceStatus( + success = true, + extras = Bundle().apply { putString("key1", "value1") }, + ) + + val fromParcel = writeAndRead(item) + + assertThat(fromParcel.success).isEqualTo(item.success) + assertThat(fromParcel.extras.getString("key1")).isEqualTo(item.extras.getString("key1")) + } + + private fun writeAndRead( + item: DeviceSettingsConfigServiceStatus + ): DeviceSettingsConfigServiceStatus { + val parcel = Parcel.obtain() + item.writeToParcel(parcel, 0) + parcel.setDataPosition(0) + return DeviceSettingsConfigServiceStatus.CREATOR.createFromParcel(parcel) + } +} diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/devicesettings/data/repository/DeviceSettingRepositoryTest.kt b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/devicesettings/data/repository/DeviceSettingRepositoryTest.kt index 0cb6bc1b1261..4e62fd3b27c5 100644 --- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/devicesettings/data/repository/DeviceSettingRepositoryTest.kt +++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/devicesettings/data/repository/DeviceSettingRepositoryTest.kt @@ -33,10 +33,12 @@ import com.android.settingslib.bluetooth.devicesettings.DeviceSettingId import com.android.settingslib.bluetooth.devicesettings.DeviceSettingItem import com.android.settingslib.bluetooth.devicesettings.DeviceSettingState import com.android.settingslib.bluetooth.devicesettings.DeviceSettingsConfig +import com.android.settingslib.bluetooth.devicesettings.DeviceSettingsConfigServiceStatus import com.android.settingslib.bluetooth.devicesettings.DeviceSettingsProviderServiceStatus import com.android.settingslib.bluetooth.devicesettings.IDeviceSettingsConfigProviderService import com.android.settingslib.bluetooth.devicesettings.IDeviceSettingsListener import com.android.settingslib.bluetooth.devicesettings.IDeviceSettingsProviderService +import com.android.settingslib.bluetooth.devicesettings.IGetDeviceSettingsConfigCallback import com.android.settingslib.bluetooth.devicesettings.MultiTogglePreference import com.android.settingslib.bluetooth.devicesettings.MultiTogglePreferenceState import com.android.settingslib.bluetooth.devicesettings.ToggleInfo @@ -53,7 +55,6 @@ import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.test.TestScope import kotlinx.coroutines.test.runCurrent import kotlinx.coroutines.test.runTest -import org.junit.After import org.junit.Before import org.junit.Rule import org.junit.Test @@ -140,15 +141,10 @@ class DeviceSettingRepositoryTest { ) } - @After - fun clean() { - DeviceSettingServiceConnection.services.clear() - } - @Test fun getDeviceSettingsConfig_withMetadata_success() { testScope.runTest { - `when`(configService.getDeviceSettingsConfig(any())).thenReturn(DEVICE_SETTING_CONFIG) + setUpConfigService(true, DEVICE_SETTING_CONFIG) `when`(settingProviderService1.serviceStatus) .thenReturn(DeviceSettingsProviderServiceStatus(true)) `when`(settingProviderService2.serviceStatus) @@ -179,7 +175,7 @@ class DeviceSettingRepositoryTest { ) ) .thenReturn("".toByteArray()) - `when`(configService.getDeviceSettingsConfig(any())).thenReturn(DEVICE_SETTING_CONFIG) + setUpConfigService(true, DEVICE_SETTING_CONFIG) `when`(settingProviderService1.serviceStatus) .thenReturn(DeviceSettingsProviderServiceStatus(true)) `when`(settingProviderService2.serviceStatus) @@ -194,7 +190,7 @@ class DeviceSettingRepositoryTest { @Test fun getDeviceSettingsConfig_providerServiceNotEnabled_returnNull() { testScope.runTest { - `when`(configService.getDeviceSettingsConfig(any())).thenReturn(DEVICE_SETTING_CONFIG) + setUpConfigService(true, DEVICE_SETTING_CONFIG) `when`(settingProviderService1.serviceStatus) .thenReturn(DeviceSettingsProviderServiceStatus(false)) `when`(settingProviderService2.serviceStatus) @@ -209,7 +205,7 @@ class DeviceSettingRepositoryTest { @Test fun getDeviceSettingsConfig_bindingServiceFail_returnNull() { testScope.runTest { - `when`(configService.getDeviceSettingsConfig(any())).thenReturn(DEVICE_SETTING_CONFIG) + setUpConfigService(true, DEVICE_SETTING_CONFIG) doReturn(false).`when`(context).bindService(any(), anyInt(), any(), any()) val config = underTest.getDeviceSettingsConfig(cachedDevice) @@ -221,7 +217,7 @@ class DeviceSettingRepositoryTest { @Test fun getDeviceSetting_actionSwitchPreference_success() { testScope.runTest { - `when`(configService.getDeviceSettingsConfig(any())).thenReturn(DEVICE_SETTING_CONFIG) + setUpConfigService(true, DEVICE_SETTING_CONFIG) `when`(settingProviderService1.registerDeviceSettingsListener(any(), any())).then { input -> input @@ -247,7 +243,7 @@ class DeviceSettingRepositoryTest { @Test fun getDeviceSetting_multiTogglePreference_success() { testScope.runTest { - `when`(configService.getDeviceSettingsConfig(any())).thenReturn(DEVICE_SETTING_CONFIG) + setUpConfigService(true, DEVICE_SETTING_CONFIG) `when`(settingProviderService2.registerDeviceSettingsListener(any(), any())).then { input -> input @@ -273,7 +269,7 @@ class DeviceSettingRepositoryTest { @Test fun getDeviceSetting_helpPreference_success() { testScope.runTest { - `when`(configService.getDeviceSettingsConfig(any())).thenReturn(DEVICE_SETTING_CONFIG) + setUpConfigService(true, DEVICE_SETTING_CONFIG) `when`(settingProviderService2.registerDeviceSettingsListener(any(), any())).then { input -> input @@ -299,6 +295,7 @@ class DeviceSettingRepositoryTest { @Test fun getDeviceSetting_noConfig_returnNull() { testScope.runTest { + setUpConfigService(false, null) `when`(settingProviderService1.registerDeviceSettingsListener(any(), any())).then { input -> input @@ -320,7 +317,7 @@ class DeviceSettingRepositoryTest { @Test fun updateDeviceSettingState_switchState_success() { testScope.runTest { - `when`(configService.getDeviceSettingsConfig(any())).thenReturn(DEVICE_SETTING_CONFIG) + setUpConfigService(true, DEVICE_SETTING_CONFIG) `when`(settingProviderService1.registerDeviceSettingsListener(any(), any())).then { input -> input @@ -358,7 +355,7 @@ class DeviceSettingRepositoryTest { @Test fun updateDeviceSettingState_multiToggleState_success() { testScope.runTest { - `when`(configService.getDeviceSettingsConfig(any())).thenReturn(DEVICE_SETTING_CONFIG) + setUpConfigService(true, DEVICE_SETTING_CONFIG) `when`(settingProviderService2.registerDeviceSettingsListener(any(), any())).then { input -> input @@ -459,6 +456,17 @@ class DeviceSettingRepositoryTest { assertThat(actual.settingId).isEqualTo(serviceResponse.settingId) } + private fun setUpConfigService(success: Boolean, config: DeviceSettingsConfig?) { + `when`(configService.getDeviceSettingsConfig(any(), any())).then { input -> + input + .getArgument<IGetDeviceSettingsConfigCallback>(1) + .onResult( + DeviceSettingsConfigServiceStatus(success = success), + config + ) + } + } + private companion object { const val BLUETOOTH_ADDRESS = "12:34:56:78" const val CONFIG_SERVICE_PACKAGE_NAME = "com.android.fake.configservice" diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/media/PhoneMediaDeviceTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/media/PhoneMediaDeviceTest.java index da5f428ce23b..1739c0e5e2bf 100644 --- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/media/PhoneMediaDeviceTest.java +++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/media/PhoneMediaDeviceTest.java @@ -136,7 +136,7 @@ public class PhoneMediaDeviceTest { when(mInfo.getType()).thenReturn(TYPE_USB_DEVICE); assertThat(mPhoneMediaDevice.getName()) - .isEqualTo(mContext.getString(R.string.media_transfer_usb_speaker_name)); + .isEqualTo(mContext.getString(R.string.media_transfer_usb_audio_name)); when(mInfo.getType()).thenReturn(TYPE_BUILTIN_SPEAKER); diff --git a/packages/SettingsProvider/src/android/provider/settings/backup/SecureSettings.java b/packages/SettingsProvider/src/android/provider/settings/backup/SecureSettings.java index d7109398b956..5e31da411e49 100644 --- a/packages/SettingsProvider/src/android/provider/settings/backup/SecureSettings.java +++ b/packages/SettingsProvider/src/android/provider/settings/backup/SecureSettings.java @@ -164,6 +164,7 @@ public class SecureSettings { Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, Settings.Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, Settings.Secure.LOCK_SCREEN_SHOW_ONLY_UNSEEN_NOTIFICATIONS, + Settings.Secure.LOCK_SCREEN_NOTIFICATION_MINIMALISM, Settings.Secure.SHOW_NOTIFICATION_SNOOZE, Settings.Secure.NOTIFICATION_HISTORY_ENABLED, Settings.Secure.ZEN_DURATION, diff --git a/packages/SettingsProvider/src/android/provider/settings/validators/SecureSettingsValidators.java b/packages/SettingsProvider/src/android/provider/settings/validators/SecureSettingsValidators.java index fa16a44f4592..b3f73749f393 100644 --- a/packages/SettingsProvider/src/android/provider/settings/validators/SecureSettingsValidators.java +++ b/packages/SettingsProvider/src/android/provider/settings/validators/SecureSettingsValidators.java @@ -243,6 +243,7 @@ public class SecureSettingsValidators { VALIDATORS.put(Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, BOOLEAN_VALIDATOR); VALIDATORS.put(Secure.LOCK_SCREEN_SHOW_SILENT_NOTIFICATIONS, BOOLEAN_VALIDATOR); VALIDATORS.put(Secure.LOCK_SCREEN_SHOW_ONLY_UNSEEN_NOTIFICATIONS, BOOLEAN_VALIDATOR); + VALIDATORS.put(Secure.LOCK_SCREEN_NOTIFICATION_MINIMALISM, BOOLEAN_VALIDATOR); VALIDATORS.put(Secure.SHOW_NOTIFICATION_SNOOZE, BOOLEAN_VALIDATOR); VALIDATORS.put(Secure.NOTIFICATION_HISTORY_ENABLED, BOOLEAN_VALIDATOR); VALIDATORS.put(Secure.ZEN_DURATION, ANY_INTEGER_VALIDATOR); diff --git a/packages/SystemUI/aconfig/systemui.aconfig b/packages/SystemUI/aconfig/systemui.aconfig index f8383d94b1ab..88cc152b3819 100644 --- a/packages/SystemUI/aconfig/systemui.aconfig +++ b/packages/SystemUI/aconfig/systemui.aconfig @@ -40,16 +40,6 @@ flag { } flag { - name: "notification_minimalism_prototype" - namespace: "systemui" - description: "Prototype of notification minimalism; the new 'Intermediate' lockscreen customization proposal." - bug: "330387368" - metadata { - purpose: PURPOSE_BUGFIX - } -} - -flag { name: "notification_view_flipper_pausing_v2" namespace: "systemui" description: "Pause ViewFlippers inside Notification custom layouts when the shade is closed." diff --git a/packages/SystemUI/animation/lib/src/com/android/systemui/animation/OriginRemoteTransition.java b/packages/SystemUI/animation/lib/src/com/android/systemui/animation/OriginRemoteTransition.java new file mode 100644 index 000000000000..08db95e5a795 --- /dev/null +++ b/packages/SystemUI/animation/lib/src/com/android/systemui/animation/OriginRemoteTransition.java @@ -0,0 +1,374 @@ +/* + * 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.systemui.animation; + +import android.animation.Animator; +import android.animation.Animator.AnimatorListener; +import android.animation.ValueAnimator; +import android.annotation.Nullable; +import android.content.Context; +import android.graphics.Rect; +import android.hardware.display.DisplayManager; +import android.os.Handler; +import android.os.IBinder; +import android.os.RemoteException; +import android.util.DisplayMetrics; +import android.util.Log; +import android.view.SurfaceControl; +import android.window.IRemoteTransition; +import android.window.IRemoteTransitionFinishedCallback; +import android.window.TransitionInfo; +import android.window.TransitionInfo.Change; +import android.window.WindowAnimationState; + +import com.android.internal.policy.ScreenDecorationsUtils; +import com.android.wm.shell.shared.TransitionUtil; + +import java.util.ArrayList; +import java.util.List; + +/** + * An implementation of {@link IRemoteTransition} that accepts a {@link UIComponent} as the origin + * and automatically attaches it to the transition leash before the transition starts. + */ +public class OriginRemoteTransition extends IRemoteTransition.Stub { + private static final String TAG = "OriginRemoteTransition"; + + private final Context mContext; + private final boolean mIsEntry; + private final UIComponent mOrigin; + private final TransitionPlayer mPlayer; + private final long mDuration; + private final Handler mHandler; + + @Nullable private SurfaceControl.Transaction mStartTransaction; + @Nullable private IRemoteTransitionFinishedCallback mFinishCallback; + @Nullable private UIComponent.Transaction mOriginTransaction; + @Nullable private ValueAnimator mAnimator; + @Nullable private SurfaceControl mOriginLeash; + private boolean mCancelled; + + OriginRemoteTransition( + Context context, + boolean isEntry, + UIComponent origin, + TransitionPlayer player, + long duration, + Handler handler) { + mContext = context; + mIsEntry = isEntry; + mOrigin = origin; + mPlayer = player; + mDuration = duration; + mHandler = handler; + } + + @Override + public void startAnimation( + IBinder token, + TransitionInfo info, + SurfaceControl.Transaction t, + IRemoteTransitionFinishedCallback finishCallback) { + logD("startAnimation - " + info); + mHandler.post( + () -> { + mStartTransaction = t; + mFinishCallback = finishCallback; + startAnimationInternal(info); + }); + } + + @Override + public void mergeAnimation( + IBinder transition, + TransitionInfo info, + SurfaceControl.Transaction t, + IBinder mergeTarget, + IRemoteTransitionFinishedCallback finishCallback) { + logD("mergeAnimation - " + info); + mHandler.post(this::cancel); + } + + @Override + public void takeOverAnimation( + IBinder transition, + TransitionInfo info, + SurfaceControl.Transaction t, + IRemoteTransitionFinishedCallback finishCallback, + WindowAnimationState[] states) { + logD("takeOverAnimation - " + info); + } + + @Override + public void onTransitionConsumed(IBinder transition, boolean aborted) { + logD("onTransitionConsumed - aborted: " + aborted); + mHandler.post(this::cancel); + } + + private void startAnimationInternal(TransitionInfo info) { + if (!prepareUIs(info)) { + logE("Unable to prepare UI!"); + finishAnimation(/* finished= */ false); + return; + } + // Notify player that we are starting. + mPlayer.onStart(info, mStartTransaction, mOrigin, mOriginTransaction); + + // Start the animator. + mAnimator = ValueAnimator.ofFloat(0.0f, 1.0f); + mAnimator.setDuration(mDuration); + mAnimator.addListener( + new AnimatorListener() { + @Override + public void onAnimationStart(Animator a) {} + + @Override + public void onAnimationEnd(Animator a) { + finishAnimation(/* finished= */ !mCancelled); + } + + @Override + public void onAnimationCancel(Animator a) { + mCancelled = true; + } + + @Override + public void onAnimationRepeat(Animator a) {} + }); + mAnimator.addUpdateListener( + a -> { + mPlayer.onProgress((float) a.getAnimatedValue()); + }); + mAnimator.start(); + } + + private boolean prepareUIs(TransitionInfo info) { + if (info.getRootCount() == 0) { + logE("prepareUIs: no root leash!"); + return false; + } + if (info.getRootCount() > 1) { + logE("prepareUIs: multi-display transition is not supported yet!"); + return false; + } + if (info.getChanges().isEmpty()) { + logE("prepareUIs: no changes!"); + return false; + } + + SurfaceControl rootLeash = info.getRoot(0).getLeash(); + int displayId = info.getChanges().get(0).getEndDisplayId(); + Rect displayBounds = getDisplayBounds(displayId); + float windowRadius = ScreenDecorationsUtils.getWindowCornerRadius(mContext); + logD("prepareUIs: windowRadius=" + windowRadius + ", displayBounds=" + displayBounds); + + // Create the origin leash and add to the transition root leash. + mOriginLeash = + new SurfaceControl.Builder().setName("OriginTransition-origin-leash").build(); + mStartTransaction + .reparent(mOriginLeash, rootLeash) + .show(mOriginLeash) + .setCornerRadius(mOriginLeash, windowRadius) + .setWindowCrop(mOriginLeash, displayBounds.width(), displayBounds.height()); + + // Process surfaces + List<SurfaceControl> openingSurfaces = new ArrayList<>(); + List<SurfaceControl> closingSurfaces = new ArrayList<>(); + for (Change change : info.getChanges()) { + int mode = change.getMode(); + SurfaceControl leash = change.getLeash(); + // Reparent leash to the transition root. + mStartTransaction.reparent(leash, rootLeash); + if (TransitionUtil.isOpeningMode(mode)) { + openingSurfaces.add(change.getLeash()); + // For opening surfaces, ending bounds are base bound. Apply corner radius if + // it's full screen. + Rect bounds = change.getEndAbsBounds(); + if (displayBounds.equals(bounds)) { + mStartTransaction + .setCornerRadius(leash, windowRadius) + .setWindowCrop(leash, bounds.width(), bounds.height()); + } + } else if (TransitionUtil.isClosingMode(mode)) { + closingSurfaces.add(change.getLeash()); + // For closing surfaces, starting bounds are base bounds. Apply corner radius if + // it's full screen. + Rect bounds = change.getStartAbsBounds(); + if (displayBounds.equals(bounds)) { + mStartTransaction + .setCornerRadius(leash, windowRadius) + .setWindowCrop(leash, bounds.width(), bounds.height()); + } + } + } + + // Set relative order: + // ---- App1 ---- + // ---- origin ---- + // ---- App2 ---- + if (mIsEntry) { + mStartTransaction + .setRelativeLayer(mOriginLeash, closingSurfaces.get(0), 1) + .setRelativeLayer( + openingSurfaces.get(openingSurfaces.size() - 1), mOriginLeash, 1); + } else { + mStartTransaction + .setRelativeLayer(mOriginLeash, openingSurfaces.get(0), 1) + .setRelativeLayer( + closingSurfaces.get(closingSurfaces.size() - 1), mOriginLeash, 1); + } + + // Attach origin UIComponent to origin leash. + mOriginTransaction = mOrigin.newTransaction(); + mOriginTransaction + .attachToTransitionLeash( + mOrigin, mOriginLeash, displayBounds.width(), displayBounds.height()) + .commit(); + + // Apply all surface changes. + mStartTransaction.apply(); + return true; + } + + private Rect getDisplayBounds(int displayId) { + DisplayManager dm = mContext.getSystemService(DisplayManager.class); + DisplayMetrics metrics = new DisplayMetrics(); + dm.getDisplay(displayId).getMetrics(metrics); + return new Rect(0, 0, metrics.widthPixels, metrics.heightPixels); + } + + private void finishAnimation(boolean finished) { + logD("finishAnimation: finished=" + finished); + if (mAnimator == null) { + // The transition didn't start. Ensure we apply the start transaction and report + // finish afterwards. + mStartTransaction + .addTransactionCommittedListener( + mContext.getMainExecutor(), this::finishInternal) + .apply(); + return; + } + mAnimator = null; + // Notify client that we have ended. + mPlayer.onEnd(finished); + // Detach the origin from the transition leash and report finish after it's done. + mOriginTransaction + .detachFromTransitionLeash( + mOrigin, mContext.getMainExecutor(), this::finishInternal) + .commit(); + } + + private void finishInternal() { + logD("finishInternal"); + if (mOriginLeash != null) { + // Release origin leash. + mOriginLeash.release(); + mOriginLeash = null; + } + try { + mFinishCallback.onTransitionFinished(null, null); + } catch (RemoteException e) { + logE("Unable to report transition finish!", e); + } + mStartTransaction = null; + mOriginTransaction = null; + mFinishCallback = null; + } + + private void cancel() { + if (mAnimator != null) { + mAnimator.cancel(); + } + } + + private static void logD(String msg) { + if (OriginTransitionSession.DEBUG) { + Log.d(TAG, msg); + } + } + + private static void logE(String msg) { + Log.e(TAG, msg); + } + + private static void logE(String msg, Throwable e) { + Log.e(TAG, msg, e); + } + + private static UIComponent wrapSurfaces(TransitionInfo info, boolean isOpening) { + List<SurfaceControl> surfaces = new ArrayList<>(); + Rect maxBounds = new Rect(); + for (Change change : info.getChanges()) { + int mode = change.getMode(); + if (TransitionUtil.isOpeningMode(mode) == isOpening) { + surfaces.add(change.getLeash()); + Rect bounds = isOpening ? change.getEndAbsBounds() : change.getStartAbsBounds(); + maxBounds.union(bounds); + } + } + return new SurfaceUIComponent( + surfaces, + /* alpha= */ 1.0f, + /* visible= */ true, + /* bounds= */ maxBounds, + /* baseBounds= */ maxBounds); + } + + /** An interface that represents an origin transitions. */ + public interface TransitionPlayer { + + /** + * Called when an origin transition starts. This method exposes the raw {@link + * TransitionInfo} so that clients can extract more information from it. + */ + default void onStart( + TransitionInfo transitionInfo, + SurfaceControl.Transaction sfTransaction, + UIComponent origin, + UIComponent.Transaction uiTransaction) { + // Wrap transactions. + Transactions transactions = + new Transactions() + .registerTransactionForClass(origin.getClass(), uiTransaction) + .registerTransactionForClass( + SurfaceUIComponent.class, + new SurfaceUIComponent.Transaction(sfTransaction)); + // Wrap surfaces and start. + onStart( + transactions, + origin, + wrapSurfaces(transitionInfo, /* isOpening= */ false), + wrapSurfaces(transitionInfo, /* isOpening= */ true)); + } + + /** + * Called when an origin transition starts. This method exposes the opening and closing + * windows as wrapped {@link UIComponent} to provide simplified interface to clients. + */ + void onStart( + UIComponent.Transaction transaction, + UIComponent origin, + UIComponent closingApp, + UIComponent openingApp); + + /** Called to update the transition frame. */ + void onProgress(float progress); + + /** Called when the transition ended. */ + void onEnd(boolean finished); + } +} diff --git a/packages/SystemUI/animation/lib/src/com/android/systemui/animation/OriginTransitionSession.java b/packages/SystemUI/animation/lib/src/com/android/systemui/animation/OriginTransitionSession.java index 64bedd347d7a..23693b68a920 100644 --- a/packages/SystemUI/animation/lib/src/com/android/systemui/animation/OriginTransitionSession.java +++ b/packages/SystemUI/animation/lib/src/com/android/systemui/animation/OriginTransitionSession.java @@ -24,11 +24,14 @@ import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Build; +import android.os.Handler; +import android.os.Looper; import android.os.RemoteException; import android.util.Log; import android.window.IRemoteTransition; import android.window.RemoteTransition; +import com.android.systemui.animation.OriginRemoteTransition.TransitionPlayer; import com.android.systemui.animation.shared.IOriginTransitions; import java.lang.annotation.Retention; @@ -182,6 +185,7 @@ public class OriginTransitionSession { @Nullable private final IOriginTransitions mOriginTransitions; @Nullable private Supplier<IRemoteTransition> mEntryTransitionSupplier; @Nullable private Supplier<IRemoteTransition> mExitTransitionSupplier; + private Handler mHandler = new Handler(Looper.getMainLooper()); private String mName; @Nullable private Predicate<RemoteTransition> mIntentStarter; @@ -259,12 +263,48 @@ public class OriginTransitionSession { return this; } + /** Add an origin entry transition to the builder. */ + public Builder withEntryTransition( + UIComponent entryOrigin, TransitionPlayer entryPlayer, long entryDuration) { + mEntryTransitionSupplier = + () -> + new OriginRemoteTransition( + mContext, + /* isEntry= */ true, + entryOrigin, + entryPlayer, + entryDuration, + mHandler); + return this; + } + /** Add an exit transition to the builder. */ public Builder withExitTransition(IRemoteTransition transition) { mExitTransitionSupplier = () -> transition; return this; } + /** Add an origin exit transition to the builder. */ + public Builder withExitTransition( + UIComponent exitTarget, TransitionPlayer exitPlayer, long exitDuration) { + mExitTransitionSupplier = + () -> + new OriginRemoteTransition( + mContext, + /* isEntry= */ false, + exitTarget, + exitPlayer, + exitDuration, + mHandler); + return this; + } + + /** Supply a handler where transition callbacks will run. */ + public Builder withHandler(Handler handler) { + mHandler = handler; + return this; + } + /** Build an {@link OriginTransitionSession}. */ public OriginTransitionSession build() { if (mIntentStarter == null) { diff --git a/packages/SystemUI/animation/lib/src/com/android/systemui/animation/SurfaceUIComponent.java b/packages/SystemUI/animation/lib/src/com/android/systemui/animation/SurfaceUIComponent.java new file mode 100644 index 000000000000..24387360936b --- /dev/null +++ b/packages/SystemUI/animation/lib/src/com/android/systemui/animation/SurfaceUIComponent.java @@ -0,0 +1,169 @@ +/* + * 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.systemui.animation; + +import android.graphics.Matrix; +import android.graphics.Rect; +import android.graphics.RectF; +import android.view.SurfaceControl; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.concurrent.Executor; + +/** A {@link UIComponent} representing a {@link SurfaceControl}. */ +public class SurfaceUIComponent implements UIComponent { + private final Collection<SurfaceControl> mSurfaces; + private final Rect mBaseBounds; + private final float[] mFloat9 = new float[9]; + + private float mAlpha; + private boolean mVisible; + private Rect mBounds; + + public SurfaceUIComponent( + SurfaceControl sc, float alpha, boolean visible, Rect bounds, Rect baseBounds) { + this(Arrays.asList(sc), alpha, visible, bounds, baseBounds); + } + + public SurfaceUIComponent( + Collection<SurfaceControl> surfaces, + float alpha, + boolean visible, + Rect bounds, + Rect baseBounds) { + mSurfaces = surfaces; + mAlpha = alpha; + mVisible = visible; + mBounds = bounds; + mBaseBounds = baseBounds; + } + + @Override + public float getAlpha() { + return mAlpha; + } + + @Override + public boolean isVisible() { + return mVisible; + } + + @Override + public Rect getBounds() { + return mBounds; + } + + @Override + public Transaction newTransaction() { + return new Transaction(new SurfaceControl.Transaction()); + } + + @Override + public String toString() { + return "SurfaceUIComponent{mSurfaces=" + + mSurfaces + + ", mAlpha=" + + mAlpha + + ", mVisible=" + + mVisible + + ", mBounds=" + + mBounds + + ", mBaseBounds=" + + mBaseBounds + + "}"; + } + + /** A {@link Transaction} wrapping a {@link SurfaceControl.Transaction}. */ + public static class Transaction implements UIComponent.Transaction<SurfaceUIComponent> { + private final SurfaceControl.Transaction mTransaction; + private final ArrayList<Runnable> mChanges = new ArrayList<>(); + + public Transaction(SurfaceControl.Transaction transaction) { + mTransaction = transaction; + } + + @Override + public Transaction setAlpha(SurfaceUIComponent ui, float alpha) { + mChanges.add( + () -> { + ui.mAlpha = alpha; + ui.mSurfaces.forEach(s -> mTransaction.setAlpha(s, alpha)); + }); + return this; + } + + @Override + public Transaction setVisible(SurfaceUIComponent ui, boolean visible) { + mChanges.add( + () -> { + ui.mVisible = visible; + if (visible) { + ui.mSurfaces.forEach(s -> mTransaction.show(s)); + } else { + ui.mSurfaces.forEach(s -> mTransaction.hide(s)); + } + }); + return this; + } + + @Override + public Transaction setBounds(SurfaceUIComponent ui, Rect bounds) { + mChanges.add( + () -> { + if (ui.mBounds.equals(bounds)) { + return; + } + ui.mBounds = bounds; + Matrix matrix = new Matrix(); + matrix.setRectToRect( + new RectF(ui.mBaseBounds), + new RectF(ui.mBounds), + Matrix.ScaleToFit.FILL); + ui.mSurfaces.forEach(s -> mTransaction.setMatrix(s, matrix, ui.mFloat9)); + }); + return this; + } + + @Override + public Transaction attachToTransitionLeash( + SurfaceUIComponent ui, SurfaceControl transitionLeash, int w, int h) { + mChanges.add( + () -> ui.mSurfaces.forEach(s -> mTransaction.reparent(s, transitionLeash))); + return this; + } + + @Override + public Transaction detachFromTransitionLeash( + SurfaceUIComponent ui, Executor executor, Runnable onDone) { + mChanges.add( + () -> { + ui.mSurfaces.forEach(s -> mTransaction.reparent(s, null)); + mTransaction.addTransactionCommittedListener(executor, onDone::run); + }); + return this; + } + + @Override + public void commit() { + mChanges.forEach(Runnable::run); + mChanges.clear(); + mTransaction.apply(); + } + } +} diff --git a/packages/SystemUI/animation/lib/src/com/android/systemui/animation/Transactions.java b/packages/SystemUI/animation/lib/src/com/android/systemui/animation/Transactions.java new file mode 100644 index 000000000000..5240d99a9217 --- /dev/null +++ b/packages/SystemUI/animation/lib/src/com/android/systemui/animation/Transactions.java @@ -0,0 +1,86 @@ +/* + * 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.systemui.animation; + +import android.annotation.FloatRange; +import android.graphics.Rect; +import android.util.ArrayMap; +import android.view.SurfaceControl; + +import java.util.Map; +import java.util.concurrent.Executor; + +/** + * A composite {@link UIComponent.Transaction} that combines multiple other transactions for each ui + * type. + */ +public class Transactions implements UIComponent.Transaction<UIComponent> { + private final Map<Class, UIComponent.Transaction> mTransactions = new ArrayMap<>(); + + /** Register a transaction object for updating a certain {@link UIComponent} type. */ + public <T extends UIComponent> Transactions registerTransactionForClass( + Class<T> clazz, UIComponent.Transaction transaction) { + mTransactions.put(clazz, transaction); + return this; + } + + private UIComponent.Transaction getTransactionFor(UIComponent ui) { + UIComponent.Transaction transaction = mTransactions.get(ui.getClass()); + if (transaction == null) { + transaction = ui.newTransaction(); + mTransactions.put(ui.getClass(), transaction); + } + return transaction; + } + + @Override + public Transactions setAlpha(UIComponent ui, @FloatRange(from = 0.0, to = 1.0) float alpha) { + getTransactionFor(ui).setAlpha(ui, alpha); + return this; + } + + @Override + public Transactions setVisible(UIComponent ui, boolean visible) { + getTransactionFor(ui).setVisible(ui, visible); + return this; + } + + @Override + public Transactions setBounds(UIComponent ui, Rect bounds) { + getTransactionFor(ui).setBounds(ui, bounds); + return this; + } + + @Override + public Transactions attachToTransitionLeash( + UIComponent ui, SurfaceControl transitionLeash, int w, int h) { + getTransactionFor(ui).attachToTransitionLeash(ui, transitionLeash, w, h); + return this; + } + + @Override + public Transactions detachFromTransitionLeash( + UIComponent ui, Executor executor, Runnable onDone) { + getTransactionFor(ui).detachFromTransitionLeash(ui, executor, onDone); + return this; + } + + @Override + public void commit() { + mTransactions.values().forEach(UIComponent.Transaction::commit); + } +} diff --git a/packages/SystemUI/animation/lib/src/com/android/systemui/animation/UIComponent.java b/packages/SystemUI/animation/lib/src/com/android/systemui/animation/UIComponent.java new file mode 100644 index 000000000000..747e4d1eb278 --- /dev/null +++ b/packages/SystemUI/animation/lib/src/com/android/systemui/animation/UIComponent.java @@ -0,0 +1,72 @@ +/* + * 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.systemui.animation; + +import android.annotation.FloatRange; +import android.graphics.Rect; +import android.view.SurfaceControl; + +import java.util.concurrent.Executor; + +/** An interface representing an UI component on the display. */ +public interface UIComponent { + + /** Get the current alpha of this UI. */ + float getAlpha(); + + /** Check if this UI is visible. */ + boolean isVisible(); + + /** Get the bounds of this UI in its display. */ + Rect getBounds(); + + /** Create a new {@link Transaction} that can update this UI. */ + Transaction newTransaction(); + + /** + * A transaction class for updating {@link UIComponent}. + * + * @param <T> the subtype of {@link UIComponent} that this {@link Transaction} can handle. + */ + interface Transaction<T extends UIComponent> { + /** Update alpha of an UI. Execution will be delayed until {@link #commit()} is called. */ + Transaction setAlpha(T ui, @FloatRange(from = 0.0, to = 1.0) float alpha); + + /** + * Update visibility of an UI. Execution will be delayed until {@link #commit()} is called. + */ + Transaction setVisible(T ui, boolean visible); + + /** Update bounds of an UI. Execution will be delayed until {@link #commit()} is called. */ + Transaction setBounds(T ui, Rect bounds); + + /** + * Attach a ui to the transition leash. Execution will be delayed until {@link #commit()} is + * called. + */ + Transaction attachToTransitionLeash(T ui, SurfaceControl transitionLeash, int w, int h); + + /** + * Detach a ui from the transition leash. Execution will be delayed until {@link #commit} is + * called. + */ + Transaction detachFromTransitionLeash(T ui, Executor executor, Runnable onDone); + + /** Commit any pending changes added to this transaction. */ + void commit(); + } +} diff --git a/packages/SystemUI/animation/lib/src/com/android/systemui/animation/ViewUIComponent.java b/packages/SystemUI/animation/lib/src/com/android/systemui/animation/ViewUIComponent.java new file mode 100644 index 000000000000..313789c4ca7e --- /dev/null +++ b/packages/SystemUI/animation/lib/src/com/android/systemui/animation/ViewUIComponent.java @@ -0,0 +1,278 @@ +/* + * 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.systemui.animation; + +import android.annotation.Nullable; +import android.graphics.Canvas; +import android.graphics.PorterDuff; +import android.graphics.Rect; +import android.os.Build; +import android.util.Log; +import android.view.Surface; +import android.view.SurfaceControl; +import android.view.View; +import android.view.ViewGroup; +import android.view.ViewTreeObserver.OnDrawListener; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.Executor; + +/** + * A {@link UIComponent} wrapping a {@link View}. After being attached to the transition leash, this + * class will draw the content of the {@link View} directly into the leash, and the actual View will + * be changed to INVISIBLE in its view tree. This allows the {@link View} to transform in the + * full-screen size leash without being constrained by the view tree's boundary or inheriting its + * parent's alpha and transformation. + */ +public class ViewUIComponent implements UIComponent { + private static final String TAG = "ViewUIComponent"; + private static final boolean DEBUG = Build.IS_USERDEBUG || Log.isLoggable(TAG, Log.DEBUG); + private final OnDrawListener mOnDrawListener = this::postDraw; + private final View mView; + + @Nullable private SurfaceControl mSurfaceControl; + @Nullable private Surface mSurface; + @Nullable private Rect mViewBoundsOverride; + private boolean mVisibleOverride; + private boolean mDirty; + + public ViewUIComponent(View view) { + mView = view; + } + + @Override + public float getAlpha() { + return mView.getAlpha(); + } + + @Override + public boolean isVisible() { + return isAttachedToLeash() ? mVisibleOverride : mView.getVisibility() == View.VISIBLE; + } + + @Override + public Rect getBounds() { + if (isAttachedToLeash() && mViewBoundsOverride != null) { + return mViewBoundsOverride; + } + return getRealBounds(); + } + + @Override + public Transaction newTransaction() { + return new Transaction(); + } + + private void attachToTransitionLeash(SurfaceControl transitionLeash, int w, int h) { + logD("attachToTransitionLeash"); + // Remember current visibility. + mVisibleOverride = mView.getVisibility() == View.VISIBLE; + + // Create the surface + mSurfaceControl = + new SurfaceControl.Builder().setName("ViewUIComponent").setBufferSize(w, h).build(); + mSurface = new Surface(mSurfaceControl); + forceDraw(); + + // Attach surface to transition leash + SurfaceControl.Transaction t = new SurfaceControl.Transaction(); + t.reparent(mSurfaceControl, transitionLeash).show(mSurfaceControl); + + // Make sure view draw triggers surface draw. + mView.getViewTreeObserver().addOnDrawListener(mOnDrawListener); + + // Make the view invisible AFTER the surface is shown. + t.addTransactionCommittedListener( + mView.getContext().getMainExecutor(), + () -> mView.setVisibility(View.INVISIBLE)) + .apply(); + } + + private void detachFromTransitionLeash(Executor executor, Runnable onDone) { + logD("detachFromTransitionLeash"); + Surface s = mSurface; + SurfaceControl sc = mSurfaceControl; + mSurface = null; + mSurfaceControl = null; + mView.getViewTreeObserver().removeOnDrawListener(mOnDrawListener); + // Restore view visibility + mView.setVisibility(mVisibleOverride ? View.VISIBLE : View.INVISIBLE); + mView.invalidate(); + // Clean up surfaces. + SurfaceControl.Transaction t = new SurfaceControl.Transaction(); + t.reparent(sc, null) + .addTransactionCommittedListener( + mView.getContext().getMainExecutor(), + () -> { + s.release(); + sc.release(); + executor.execute(onDone); + }); + // Apply transaction AFTER the view is drawn. + mView.getRootSurfaceControl().applyTransactionOnDraw(t); + } + + @Override + public String toString() { + return "ViewUIComponent{" + + "alpha=" + + getAlpha() + + ", visible=" + + isVisible() + + ", bounds=" + + getBounds() + + ", attached=" + + isAttachedToLeash() + + "}"; + } + + private void draw() { + if (!mDirty) { + // No need to draw. This is probably a duplicate call. + logD("draw: skipped - clean"); + return; + } + mDirty = false; + if (!isAttachedToLeash()) { + // Not attached. + logD("draw: skipped - not attached"); + return; + } + ViewGroup.LayoutParams params = mView.getLayoutParams(); + if (params == null || params.width == 0 || params.height == 0) { + // layout pass didn't happen. + logD("draw: skipped - no layout"); + return; + } + Canvas canvas = mSurface.lockHardwareCanvas(); + // Clear the canvas first. + canvas.drawColor(0, PorterDuff.Mode.CLEAR); + if (mVisibleOverride) { + Rect realBounds = getRealBounds(); + Rect renderBounds = getBounds(); + canvas.translate(renderBounds.left, renderBounds.top); + canvas.scale( + (float) renderBounds.width() / realBounds.width(), + (float) renderBounds.height() / realBounds.height()); + canvas.saveLayerAlpha(null, (int) (255 * mView.getAlpha())); + mView.draw(canvas); + canvas.restore(); + } + mSurface.unlockCanvasAndPost(canvas); + logD("draw: done"); + } + + private void forceDraw() { + mDirty = true; + draw(); + } + + private Rect getRealBounds() { + Rect output = new Rect(); + mView.getBoundsOnScreen(output); + return output; + } + + private boolean isAttachedToLeash() { + return mSurfaceControl != null && mSurface != null; + } + + private void logD(String msg) { + if (DEBUG) { + Log.d(TAG, msg); + } + } + + private void setVisible(boolean visible) { + logD("setVisibility: " + visible); + if (isAttachedToLeash()) { + mVisibleOverride = visible; + postDraw(); + } else { + mView.setVisibility(visible ? View.VISIBLE : View.INVISIBLE); + } + } + + private void setBounds(Rect bounds) { + logD("setBounds: " + bounds); + mViewBoundsOverride = bounds; + if (isAttachedToLeash()) { + postDraw(); + } else { + Log.w(TAG, "setBounds: not attached to leash!"); + } + } + + private void setAlpha(float alpha) { + logD("setAlpha: " + alpha); + mView.setAlpha(alpha); + if (isAttachedToLeash()) { + postDraw(); + } + } + + private void postDraw() { + if (mDirty) { + return; + } + mDirty = true; + mView.post(this::draw); + } + + public static class Transaction implements UIComponent.Transaction<ViewUIComponent> { + private final List<Runnable> mChanges = new ArrayList<>(); + + @Override + public Transaction setAlpha(ViewUIComponent ui, float alpha) { + mChanges.add(() -> ui.setAlpha(alpha)); + return this; + } + + @Override + public Transaction setVisible(ViewUIComponent ui, boolean visible) { + mChanges.add(() -> ui.setVisible(visible)); + return this; + } + + @Override + public Transaction setBounds(ViewUIComponent ui, Rect bounds) { + mChanges.add(() -> ui.setBounds(bounds)); + return this; + } + + @Override + public Transaction attachToTransitionLeash( + ViewUIComponent ui, SurfaceControl transitionLeash, int w, int h) { + mChanges.add(() -> ui.attachToTransitionLeash(transitionLeash, w, h)); + return this; + } + + @Override + public Transaction detachFromTransitionLeash( + ViewUIComponent ui, Executor executor, Runnable onDone) { + mChanges.add(() -> ui.detachFromTransitionLeash(executor, onDone)); + return this; + } + + @Override + public void commit() { + mChanges.forEach(Runnable::run); + mChanges.clear(); + } + } +} diff --git a/packages/SystemUI/compose/core/src/com/android/compose/PlatformButtons.kt b/packages/SystemUI/compose/core/src/com/android/compose/PlatformButtons.kt index a5f8057b524f..20efea513b3a 100644 --- a/packages/SystemUI/compose/core/src/com/android/compose/PlatformButtons.kt +++ b/packages/SystemUI/compose/core/src/com/android/compose/PlatformButtons.kt @@ -28,11 +28,11 @@ import androidx.compose.material3.Icon import androidx.compose.material3.IconButton import androidx.compose.material3.IconButtonColors import androidx.compose.material3.IconButtonDefaults +import androidx.compose.material3.MaterialTheme import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.res.painterResource import androidx.compose.ui.unit.dp -import com.android.compose.theme.LocalAndroidColorScheme @Composable fun PlatformButton( @@ -100,12 +100,7 @@ fun PlatformIconButton( @DrawableRes iconResource: Int, contentDescription: String?, ) { - IconButton( - modifier = modifier, - onClick = onClick, - enabled = enabled, - colors = colors, - ) { + IconButton(modifier = modifier, onClick = onClick, enabled = enabled, colors = colors) { Icon( painter = painterResource(id = iconResource), contentDescription = contentDescription, @@ -118,7 +113,7 @@ private val ButtonPaddings = PaddingValues(horizontal = 16.dp, vertical = 8.dp) @Composable private fun filledButtonColors(): ButtonColors { - val colors = LocalAndroidColorScheme.current + val colors = MaterialTheme.colorScheme return ButtonDefaults.buttonColors( containerColor = colors.primary, contentColor = colors.onPrimary, @@ -127,27 +122,22 @@ private fun filledButtonColors(): ButtonColors { @Composable private fun outlineButtonColors(): ButtonColors { - return ButtonDefaults.outlinedButtonColors( - contentColor = LocalAndroidColorScheme.current.onSurface, - ) + return ButtonDefaults.outlinedButtonColors(contentColor = MaterialTheme.colorScheme.onSurface) } @Composable private fun iconButtonColors(): IconButtonColors { return IconButtonDefaults.filledIconButtonColors( - contentColor = LocalAndroidColorScheme.current.onSurface, + contentColor = MaterialTheme.colorScheme.onSurface ) } @Composable private fun outlineButtonBorder(): BorderStroke { - return BorderStroke( - width = 1.dp, - color = LocalAndroidColorScheme.current.primary, - ) + return BorderStroke(width = 1.dp, color = MaterialTheme.colorScheme.primary) } @Composable private fun textButtonColors(): ButtonColors { - return ButtonDefaults.textButtonColors(contentColor = LocalAndroidColorScheme.current.primary) + return ButtonDefaults.textButtonColors(contentColor = MaterialTheme.colorScheme.primary) } diff --git a/packages/SystemUI/compose/features/src/com/android/systemui/dialog/ui/composable/AlertDialogContent.kt b/packages/SystemUI/compose/features/src/com/android/systemui/dialog/ui/composable/AlertDialogContent.kt index 0b9669410b8e..69ca0a5f476c 100644 --- a/packages/SystemUI/compose/features/src/com/android/systemui/dialog/ui/composable/AlertDialogContent.kt +++ b/packages/SystemUI/compose/features/src/com/android/systemui/dialog/ui/composable/AlertDialogContent.kt @@ -38,7 +38,6 @@ import androidx.compose.ui.layout.Placeable import androidx.compose.ui.layout.layoutId import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.dp -import com.android.compose.theme.LocalAndroidColorScheme import kotlin.math.roundToInt /** @@ -69,7 +68,7 @@ fun AlertDialogContent( Modifier.defaultMinSize(minWidth = defaultSize, minHeight = defaultSize), propagateMinConstraints = true, ) { - val iconColor = LocalAndroidColorScheme.current.primary + val iconColor = MaterialTheme.colorScheme.primary CompositionLocalProvider(LocalContentColor provides iconColor) { icon() } } @@ -77,7 +76,7 @@ fun AlertDialogContent( } // Title. - val titleColor = LocalAndroidColorScheme.current.onSurface + val titleColor = MaterialTheme.colorScheme.onSurface CompositionLocalProvider(LocalContentColor provides titleColor) { ProvideTextStyle( MaterialTheme.typography.headlineSmall.copy(textAlign = TextAlign.Center) @@ -88,7 +87,7 @@ fun AlertDialogContent( Spacer(Modifier.height(16.dp)) // Content. - val contentColor = LocalAndroidColorScheme.current.onSurfaceVariant + val contentColor = MaterialTheme.colorScheme.onSurfaceVariant Box { CompositionLocalProvider(LocalContentColor provides contentColor) { ProvideTextStyle( @@ -169,7 +168,7 @@ private fun AlertDialogButtons( negative.width - positive.width - horizontalSpacing.roundToInt(), - 0 + 0, ) } } diff --git a/packages/SystemUI/multivalentTests/src/com/android/systemui/keyguard/domain/interactor/KeyguardDismissActionInteractorTest.kt b/packages/SystemUI/multivalentTests/src/com/android/systemui/keyguard/domain/interactor/KeyguardDismissActionInteractorTest.kt index ba689179c33d..29035ce2aa0a 100644 --- a/packages/SystemUI/multivalentTests/src/com/android/systemui/keyguard/domain/interactor/KeyguardDismissActionInteractorTest.kt +++ b/packages/SystemUI/multivalentTests/src/com/android/systemui/keyguard/domain/interactor/KeyguardDismissActionInteractorTest.kt @@ -38,7 +38,6 @@ import com.android.systemui.power.shared.model.WakefulnessState import com.android.systemui.scene.data.repository.Idle import com.android.systemui.scene.data.repository.Transition import com.android.systemui.scene.data.repository.setSceneTransition -import com.android.systemui.scene.domain.interactor.sceneInteractor import com.android.systemui.scene.shared.model.Scenes import com.android.systemui.shade.domain.interactor.shadeInteractor import com.android.systemui.testKosmos @@ -76,7 +75,6 @@ class KeyguardDismissActionInteractorTest : SysuiTestCase() { transitionInteractor = kosmos.keyguardTransitionInteractor, dismissInteractor = dismissInteractor, applicationScope = testScope.backgroundScope, - sceneInteractor = { kosmos.sceneInteractor }, deviceUnlockedInteractor = { kosmos.deviceUnlockedInteractor }, powerInteractor = kosmos.powerInteractor, alternateBouncerInteractor = kosmos.alternateBouncerInteractor, diff --git a/packages/SystemUI/multivalentTests/src/com/android/systemui/notifications/ui/viewmodel/NotificationsShadeOverlayContentViewModelTest.kt b/packages/SystemUI/multivalentTests/src/com/android/systemui/notifications/ui/viewmodel/NotificationsShadeOverlayContentViewModelTest.kt index 3388c75bcf78..ada2138d4d52 100644 --- a/packages/SystemUI/multivalentTests/src/com/android/systemui/notifications/ui/viewmodel/NotificationsShadeOverlayContentViewModelTest.kt +++ b/packages/SystemUI/multivalentTests/src/com/android/systemui/notifications/ui/viewmodel/NotificationsShadeOverlayContentViewModelTest.kt @@ -21,19 +21,34 @@ import android.testing.TestableLooper import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.filters.SmallTest import com.android.systemui.SysuiTestCase +import com.android.systemui.authentication.data.repository.FakeAuthenticationRepository +import com.android.systemui.authentication.domain.interactor.AuthenticationResult +import com.android.systemui.authentication.domain.interactor.authenticationInteractor import com.android.systemui.coroutines.collectLastValue import com.android.systemui.flags.EnableSceneContainer import com.android.systemui.kosmos.testScope +import com.android.systemui.lifecycle.activateIn +import com.android.systemui.power.domain.interactor.PowerInteractor.Companion.setAsleepForTest +import com.android.systemui.power.domain.interactor.PowerInteractor.Companion.setAwakeForTest +import com.android.systemui.power.domain.interactor.powerInteractor import com.android.systemui.scene.domain.interactor.sceneInteractor +import com.android.systemui.scene.domain.startable.sceneContainerStartable import com.android.systemui.scene.shared.model.Overlays +import com.android.systemui.scene.shared.model.Scenes +import com.android.systemui.shade.domain.interactor.shadeInteractor import com.android.systemui.shade.shared.flag.DualShade import com.android.systemui.shade.ui.viewmodel.notificationsShadeOverlayContentViewModel import com.android.systemui.testKosmos import com.google.common.truth.Truth.assertThat +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.TestScope +import kotlinx.coroutines.test.runCurrent import kotlinx.coroutines.test.runTest +import org.junit.Before import org.junit.Test import org.junit.runner.RunWith +@OptIn(ExperimentalCoroutinesApi::class) @SmallTest @RunWith(AndroidJUnit4::class) @TestableLooper.RunWithLooper @@ -47,18 +62,84 @@ class NotificationsShadeOverlayContentViewModelTest : SysuiTestCase() { private val underTest by lazy { kosmos.notificationsShadeOverlayContentViewModel } + @Before + fun setUp() { + kosmos.sceneContainerStartable.start() + underTest.activateIn(testScope) + } + @Test fun onScrimClicked_hidesShade() = testScope.runTest { val currentOverlays by collectLastValue(sceneInteractor.currentOverlays) - sceneInteractor.showOverlay( - overlay = Overlays.NotificationsShade, - loggingReason = "test", - ) + sceneInteractor.showOverlay(Overlays.NotificationsShade, "test") assertThat(currentOverlays).contains(Overlays.NotificationsShade) underTest.onScrimClicked() assertThat(currentOverlays).doesNotContain(Overlays.NotificationsShade) } + + @Test + fun deviceLocked_hidesShade() = + testScope.runTest { + val currentOverlays by collectLastValue(sceneInteractor.currentOverlays) + unlockDevice() + sceneInteractor.showOverlay(Overlays.NotificationsShade, "test") + assertThat(currentOverlays).contains(Overlays.NotificationsShade) + + lockDevice() + + assertThat(currentOverlays).doesNotContain(Overlays.NotificationsShade) + } + + @Test + fun bouncerShown_hidesShade() = + testScope.runTest { + val currentOverlays by collectLastValue(sceneInteractor.currentOverlays) + lockDevice() + sceneInteractor.showOverlay(Overlays.NotificationsShade, "test") + assertThat(currentOverlays).contains(Overlays.NotificationsShade) + + sceneInteractor.changeScene(Scenes.Bouncer, "test") + runCurrent() + + assertThat(currentOverlays).doesNotContain(Overlays.NotificationsShade) + } + + @Test + fun shadeNotTouchable_hidesShade() = + testScope.runTest { + val currentOverlays by collectLastValue(sceneInteractor.currentOverlays) + val isShadeTouchable by collectLastValue(kosmos.shadeInteractor.isShadeTouchable) + assertThat(isShadeTouchable).isTrue() + sceneInteractor.showOverlay(Overlays.NotificationsShade, "test") + assertThat(currentOverlays).contains(Overlays.NotificationsShade) + + lockDevice() + assertThat(isShadeTouchable).isFalse() + assertThat(currentOverlays).doesNotContain(Overlays.NotificationsShade) + } + + private fun TestScope.lockDevice() { + val currentScene by collectLastValue(sceneInteractor.currentScene) + kosmos.powerInteractor.setAsleepForTest() + runCurrent() + + assertThat(currentScene).isEqualTo(Scenes.Lockscreen) + } + + private suspend fun TestScope.unlockDevice() { + val currentScene by collectLastValue(sceneInteractor.currentScene) + kosmos.powerInteractor.setAwakeForTest() + runCurrent() + assertThat( + kosmos.authenticationInteractor.authenticate( + FakeAuthenticationRepository.DEFAULT_PIN + ) + ) + .isEqualTo(AuthenticationResult.SUCCEEDED) + + assertThat(currentScene).isEqualTo(Scenes.Gone) + } } diff --git a/packages/SystemUI/multivalentTests/src/com/android/systemui/qs/tileimpl/QSIconViewImplTest.java b/packages/SystemUI/multivalentTests/src/com/android/systemui/qs/tileimpl/QSIconViewImplTest.java index 2580ac2c8da7..7798f46fdb46 100644 --- a/packages/SystemUI/multivalentTests/src/com/android/systemui/qs/tileimpl/QSIconViewImplTest.java +++ b/packages/SystemUI/multivalentTests/src/com/android/systemui/qs/tileimpl/QSIconViewImplTest.java @@ -14,6 +14,8 @@ package com.android.systemui.qs.tileimpl; +import static com.android.systemui.Flags.FLAG_QS_NEW_TILES; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.mockito.ArgumentMatchers.any; @@ -21,11 +23,16 @@ import static org.mockito.ArgumentMatchers.argThat; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import android.content.Context; import android.graphics.drawable.AnimatedVectorDrawable; import android.graphics.drawable.Drawable; +import android.platform.test.annotations.DisableFlags; +import android.platform.test.annotations.EnableFlags; import android.service.quicksettings.Tile; import android.testing.UiThreadTest; import android.widget.ImageView; @@ -47,7 +54,6 @@ import org.mockito.Mockito; @UiThreadTest @SmallTest public class QSIconViewImplTest extends SysuiTestCase { - private QSIconViewImpl mIconView; @Before @@ -106,6 +112,34 @@ public class QSIconViewImplTest extends SysuiTestCase { verify(iv).setImageTintList(argThat(stateList -> stateList.getColors()[0] == desiredColor)); } + + @EnableFlags(FLAG_QS_NEW_TILES) + @Test + public void testIconPreloaded_withFlagOn_immediatelyLoadsAll3TintColors() { + Context ctx = spy(mContext); + + QSIconViewImpl iconView = new QSIconViewImpl(ctx); + + verify(ctx, times(3)).obtainStyledAttributes(any()); + + iconView.getColor(new State()); // this should not increase the call count + + verify(ctx, times(3)).obtainStyledAttributes(any()); + } + + @DisableFlags(FLAG_QS_NEW_TILES) + @Test + public void testIconPreloaded_withFlagOff_loadsOneTintColorAfterIconColorIsRead() { + Context ctx = spy(mContext); + QSIconViewImpl iconView = new QSIconViewImpl(ctx); + + verify(ctx, never()).obtainStyledAttributes(any()); // none of the colors are preloaded + + iconView.getColor(new State()); + + verify(ctx, times(1)).obtainStyledAttributes(any()); + } + @Test public void testStateSetCorrectly_toString() { ImageView iv = mock(ImageView.class); diff --git a/packages/SystemUI/multivalentTests/src/com/android/systemui/qs/tiles/impl/internet/domain/InternetTileMapperTest.kt b/packages/SystemUI/multivalentTests/src/com/android/systemui/qs/tiles/impl/internet/domain/InternetTileMapperTest.kt index 620e90dcaa62..d32ba47204c0 100644 --- a/packages/SystemUI/multivalentTests/src/com/android/systemui/qs/tiles/impl/internet/domain/InternetTileMapperTest.kt +++ b/packages/SystemUI/multivalentTests/src/com/android/systemui/qs/tiles/impl/internet/domain/InternetTileMapperTest.kt @@ -17,13 +17,17 @@ package com.android.systemui.qs.tiles.impl.internet.domain import android.graphics.drawable.TestStubDrawable +import android.os.fakeExecutorHandler import android.widget.Switch import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.filters.SmallTest +import com.android.settingslib.graph.SignalDrawable import com.android.systemui.SysuiTestCase import com.android.systemui.common.shared.model.ContentDescription +import com.android.systemui.common.shared.model.ContentDescription.Companion.loadContentDescription import com.android.systemui.common.shared.model.Icon import com.android.systemui.common.shared.model.Text +import com.android.systemui.common.shared.model.Text.Companion.loadText import com.android.systemui.kosmos.Kosmos import com.android.systemui.qs.tiles.impl.custom.QSTileStateSubject import com.android.systemui.qs.tiles.impl.internet.domain.model.InternetTileModel @@ -31,6 +35,9 @@ import com.android.systemui.qs.tiles.impl.internet.qsInternetTileConfig import com.android.systemui.qs.tiles.viewmodel.QSTileState import com.android.systemui.res.R import com.android.systemui.statusbar.connectivity.WifiIcons.WIFI_FULL_ICONS +import com.android.systemui.statusbar.pipeline.mobile.domain.model.SignalIconModel +import com.android.systemui.statusbar.pipeline.satellite.ui.model.SatelliteIconModel +import com.android.systemui.statusbar.pipeline.shared.ui.model.InternetTileIconModel import org.junit.Test import org.junit.runner.RunWith @@ -39,25 +46,93 @@ import org.junit.runner.RunWith class InternetTileMapperTest : SysuiTestCase() { private val kosmos = Kosmos() private val internetTileConfig = kosmos.qsInternetTileConfig + private val handler = kosmos.fakeExecutorHandler private val mapper by lazy { InternetTileMapper( context.orCreateTestableResources .apply { addOverride(R.drawable.ic_qs_no_internet_unavailable, TestStubDrawable()) + addOverride(R.drawable.ic_satellite_connected_2, TestStubDrawable()) addOverride(wifiRes, TestStubDrawable()) } .resources, context.theme, - context + context, + handler, ) } @Test - fun withActiveModel_mappedStateMatchesDataModel() { + fun withActiveCellularModel_mappedStateMatchesDataModel() { val inputModel = InternetTileModel.Active( secondaryLabel = Text.Resource(R.string.quick_settings_networks_available), - iconId = wifiRes, + icon = InternetTileIconModel.Cellular(3), + stateDescription = null, + contentDescription = + ContentDescription.Resource(R.string.quick_settings_internet_label), + ) + + val outputState = mapper.map(internetTileConfig, inputModel) + + val signalDrawable = SignalDrawable(context, handler) + signalDrawable.setLevel(3) + val expectedState = + createInternetTileState( + QSTileState.ActivationState.ACTIVE, + context.getString(R.string.quick_settings_networks_available), + Icon.Loaded(signalDrawable, null), + null, + context.getString(R.string.quick_settings_internet_label), + ) + QSTileStateSubject.assertThat(outputState).isEqualTo(expectedState) + } + + @Test + fun withActiveSatelliteModel_mappedStateMatchesDataModel() { + val inputIcon = + SignalIconModel.Satellite( + 3, + Icon.Resource( + res = R.drawable.ic_satellite_connected_2, + contentDescription = + ContentDescription.Resource( + R.string.accessibility_status_bar_satellite_good_connection + ), + ), + ) + val inputModel = + InternetTileModel.Active( + secondaryLabel = Text.Resource(R.string.quick_settings_networks_available), + icon = InternetTileIconModel.Satellite(inputIcon.icon), + stateDescription = null, + contentDescription = + ContentDescription.Resource( + R.string.accessibility_status_bar_satellite_good_connection + ), + ) + + val outputState = mapper.map(internetTileConfig, inputModel) + + val expectedSatIcon = SatelliteIconModel.fromSignalStrength(3) + + val expectedState = + createInternetTileState( + QSTileState.ActivationState.ACTIVE, + inputModel.secondaryLabel.loadText(context).toString(), + Icon.Loaded(context.getDrawable(expectedSatIcon!!.res)!!, null), + expectedSatIcon.res, + expectedSatIcon.contentDescription.loadContentDescription(context).toString(), + ) + QSTileStateSubject.assertThat(outputState).isEqualTo(expectedState) + } + + @Test + fun withActiveWifiModel_mappedStateMatchesDataModel() { + val inputModel = + InternetTileModel.Active( + secondaryLabel = Text.Resource(R.string.quick_settings_networks_available), + icon = InternetTileIconModel.ResourceId(wifiRes), stateDescription = null, contentDescription = ContentDescription.Resource(R.string.quick_settings_internet_label), @@ -71,7 +146,7 @@ class InternetTileMapperTest : SysuiTestCase() { context.getString(R.string.quick_settings_networks_available), Icon.Loaded(context.getDrawable(wifiRes)!!, contentDescription = null), wifiRes, - context.getString(R.string.quick_settings_internet_label) + context.getString(R.string.quick_settings_internet_label), ) QSTileStateSubject.assertThat(outputState).isEqualTo(expectedState) } @@ -81,7 +156,7 @@ class InternetTileMapperTest : SysuiTestCase() { val inputModel = InternetTileModel.Inactive( secondaryLabel = Text.Resource(R.string.quick_settings_networks_unavailable), - iconId = R.drawable.ic_qs_no_internet_unavailable, + icon = InternetTileIconModel.ResourceId(R.drawable.ic_qs_no_internet_unavailable), stateDescription = null, contentDescription = ContentDescription.Resource(R.string.quick_settings_networks_unavailable), @@ -95,10 +170,10 @@ class InternetTileMapperTest : SysuiTestCase() { context.getString(R.string.quick_settings_networks_unavailable), Icon.Loaded( context.getDrawable(R.drawable.ic_qs_no_internet_unavailable)!!, - contentDescription = null + contentDescription = null, ), R.drawable.ic_qs_no_internet_unavailable, - context.getString(R.string.quick_settings_networks_unavailable) + context.getString(R.string.quick_settings_networks_unavailable), ) QSTileStateSubject.assertThat(outputState).isEqualTo(expectedState) } @@ -107,7 +182,7 @@ class InternetTileMapperTest : SysuiTestCase() { activationState: QSTileState.ActivationState, secondaryLabel: String, icon: Icon, - iconRes: Int, + iconRes: Int? = null, contentDescription: String, ): QSTileState { val label = context.getString(R.string.quick_settings_internet_label) @@ -120,13 +195,13 @@ class InternetTileMapperTest : SysuiTestCase() { setOf( QSTileState.UserAction.CLICK, QSTileState.UserAction.TOGGLE_CLICK, - QSTileState.UserAction.LONG_CLICK + QSTileState.UserAction.LONG_CLICK, ), contentDescription, null, QSTileState.SideViewIcon.Chevron, QSTileState.EnabledState.ENABLED, - Switch::class.qualifiedName + Switch::class.qualifiedName, ) } diff --git a/packages/SystemUI/multivalentTests/src/com/android/systemui/qs/tiles/impl/internet/domain/interactor/InternetTileDataInteractorTest.kt b/packages/SystemUI/multivalentTests/src/com/android/systemui/qs/tiles/impl/internet/domain/interactor/InternetTileDataInteractorTest.kt index 5a4506086058..5259aa84b193 100644 --- a/packages/SystemUI/multivalentTests/src/com/android/systemui/qs/tiles/impl/internet/domain/interactor/InternetTileDataInteractorTest.kt +++ b/packages/SystemUI/multivalentTests/src/com/android/systemui/qs/tiles/impl/internet/domain/interactor/InternetTileDataInteractorTest.kt @@ -18,14 +18,12 @@ package com.android.systemui.qs.tiles.impl.internet.domain.interactor import android.graphics.drawable.TestStubDrawable import android.os.UserHandle -import android.testing.TestableLooper import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.filters.SmallTest import com.android.settingslib.AccessibilityContentDescriptions import com.android.systemui.SysuiTestCase import com.android.systemui.common.shared.model.ContentDescription import com.android.systemui.common.shared.model.ContentDescription.Companion.loadContentDescription -import com.android.systemui.common.shared.model.Icon import com.android.systemui.common.shared.model.Text import com.android.systemui.common.shared.model.Text.Companion.loadText import com.android.systemui.coroutines.collectLastValue @@ -49,6 +47,7 @@ import com.android.systemui.statusbar.pipeline.mobile.domain.interactor.MobileIc import com.android.systemui.statusbar.pipeline.mobile.util.FakeMobileMappingsProxy import com.android.systemui.statusbar.pipeline.shared.data.model.DefaultConnectionModel import com.android.systemui.statusbar.pipeline.shared.data.repository.FakeConnectivityRepository +import com.android.systemui.statusbar.pipeline.shared.ui.model.InternetTileIconModel import com.android.systemui.statusbar.pipeline.wifi.data.repository.FakeWifiRepository import com.android.systemui.statusbar.pipeline.wifi.domain.interactor.WifiInteractorImpl import com.android.systemui.statusbar.pipeline.wifi.shared.model.WifiNetworkModel @@ -60,9 +59,7 @@ import com.android.systemui.util.mockito.mock import com.google.common.truth.Truth.assertThat import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.flowOf -import kotlinx.coroutines.test.runCurrent import kotlinx.coroutines.test.runTest -import org.junit.Assume.assumeFalse import org.junit.Before import org.junit.Test import org.junit.runner.RunWith @@ -144,7 +141,6 @@ class InternetTileDataInteractorTest : SysuiTestCase() { underTest = InternetTileDataInteractor( context, - testScope.coroutineContext, testScope.backgroundScope, airplaneModeRepository, connectivityRepository, @@ -164,9 +160,11 @@ class InternetTileDataInteractorTest : SysuiTestCase() { connectivityRepository.defaultConnections.value = DefaultConnectionModel() + val expectedIcon = + InternetTileIconModel.ResourceId(R.drawable.ic_qs_no_internet_unavailable) assertThat(latest?.secondaryLabel) .isEqualTo(Text.Resource(R.string.quick_settings_networks_unavailable)) - assertThat(latest?.iconId).isEqualTo(R.drawable.ic_qs_no_internet_unavailable) + assertThat(latest?.icon).isEqualTo(expectedIcon) } @Test @@ -183,11 +181,8 @@ class InternetTileDataInteractorTest : SysuiTestCase() { underTest.tileData(testUser, flowOf(DataUpdateTrigger.InitialRequest)) ) - val networkModel = - WifiNetworkModel.Active.of( - level = 4, - ssid = "test ssid", - ) + val networkModel = WifiNetworkModel.Active.of(level = 4, ssid = "test ssid") + val wifiIcon = WifiIcon.fromModel(model = networkModel, context = context, showHotspotInfo = true) as WifiIcon.Visible @@ -198,12 +193,9 @@ class InternetTileDataInteractorTest : SysuiTestCase() { assertThat(latest?.secondaryTitle).isEqualTo("test ssid") assertThat(latest?.secondaryLabel).isNull() - val expectedIcon = - Icon.Loaded(context.getDrawable(WifiIcons.WIFI_NO_INTERNET_ICONS[4])!!, null) - val actualIcon = latest?.icon - assertThat(actualIcon).isEqualTo(expectedIcon) - assertThat(latest?.iconId).isEqualTo(WifiIcons.WIFI_NO_INTERNET_ICONS[4]) + val expectedIcon = InternetTileIconModel.ResourceId(WifiIcons.WIFI_NO_INTERNET_ICONS[4]) + assertThat(latest?.icon).isEqualTo(expectedIcon) assertThat(latest?.contentDescription.loadContentDescription(context)) .isEqualTo("$internet,test ssid") val expectedSd = wifiIcon.contentDescription @@ -229,8 +221,7 @@ class InternetTileDataInteractorTest : SysuiTestCase() { wifiRepository.setIsWifiDefault(true) wifiRepository.setWifiNetwork(networkModel) - val expectedIcon = - Icon.Loaded(context.getDrawable(WifiIcons.WIFI_NO_INTERNET_ICONS[4])!!, null) + val expectedIcon = InternetTileIconModel.ResourceId(WifiIcons.WIFI_NO_INTERNET_ICONS[4]) assertThat(latest?.icon).isEqualTo(expectedIcon) assertThat(latest?.stateDescription.loadContentDescription(context)) .doesNotContain( @@ -249,9 +240,8 @@ class InternetTileDataInteractorTest : SysuiTestCase() { setWifiNetworkWithHotspot(WifiNetworkModel.HotspotDeviceType.TABLET) val expectedIcon = - Icon.Loaded( - context.getDrawable(com.android.settingslib.R.drawable.ic_hotspot_tablet)!!, - null + InternetTileIconModel.ResourceId( + com.android.settingslib.R.drawable.ic_hotspot_tablet ) assertThat(latest?.icon).isEqualTo(expectedIcon) assertThat(latest?.stateDescription.loadContentDescription(context)) @@ -271,9 +261,8 @@ class InternetTileDataInteractorTest : SysuiTestCase() { setWifiNetworkWithHotspot(WifiNetworkModel.HotspotDeviceType.LAPTOP) val expectedIcon = - Icon.Loaded( - context.getDrawable(com.android.settingslib.R.drawable.ic_hotspot_laptop)!!, - null + InternetTileIconModel.ResourceId( + com.android.settingslib.R.drawable.ic_hotspot_laptop ) assertThat(latest?.icon).isEqualTo(expectedIcon) assertThat(latest?.stateDescription.loadContentDescription(context)) @@ -293,10 +282,10 @@ class InternetTileDataInteractorTest : SysuiTestCase() { setWifiNetworkWithHotspot(WifiNetworkModel.HotspotDeviceType.WATCH) val expectedIcon = - Icon.Loaded( - context.getDrawable(com.android.settingslib.R.drawable.ic_hotspot_watch)!!, - null + InternetTileIconModel.ResourceId( + com.android.settingslib.R.drawable.ic_hotspot_watch ) + assertThat(latest?.icon).isEqualTo(expectedIcon) assertThat(latest?.stateDescription.loadContentDescription(context)) .isEqualTo( @@ -315,10 +304,7 @@ class InternetTileDataInteractorTest : SysuiTestCase() { setWifiNetworkWithHotspot(WifiNetworkModel.HotspotDeviceType.AUTO) val expectedIcon = - Icon.Loaded( - context.getDrawable(com.android.settingslib.R.drawable.ic_hotspot_auto)!!, - null - ) + InternetTileIconModel.ResourceId(com.android.settingslib.R.drawable.ic_hotspot_auto) assertThat(latest?.icon).isEqualTo(expectedIcon) assertThat(latest?.stateDescription.loadContentDescription(context)) .isEqualTo( @@ -336,9 +322,8 @@ class InternetTileDataInteractorTest : SysuiTestCase() { setWifiNetworkWithHotspot(WifiNetworkModel.HotspotDeviceType.PHONE) val expectedIcon = - Icon.Loaded( - context.getDrawable(com.android.settingslib.R.drawable.ic_hotspot_phone)!!, - null + InternetTileIconModel.ResourceId( + com.android.settingslib.R.drawable.ic_hotspot_phone ) assertThat(latest?.icon).isEqualTo(expectedIcon) assertThat(latest?.stateDescription.loadContentDescription(context)) @@ -358,9 +343,8 @@ class InternetTileDataInteractorTest : SysuiTestCase() { setWifiNetworkWithHotspot(WifiNetworkModel.HotspotDeviceType.UNKNOWN) val expectedIcon = - Icon.Loaded( - context.getDrawable(com.android.settingslib.R.drawable.ic_hotspot_phone)!!, - null + InternetTileIconModel.ResourceId( + com.android.settingslib.R.drawable.ic_hotspot_phone ) assertThat(latest?.icon).isEqualTo(expectedIcon) assertThat(latest?.stateDescription.loadContentDescription(context)) @@ -380,10 +364,10 @@ class InternetTileDataInteractorTest : SysuiTestCase() { setWifiNetworkWithHotspot(WifiNetworkModel.HotspotDeviceType.INVALID) val expectedIcon = - Icon.Loaded( - context.getDrawable(com.android.settingslib.R.drawable.ic_hotspot_phone)!!, - null + InternetTileIconModel.ResourceId( + com.android.settingslib.R.drawable.ic_hotspot_phone ) + assertThat(latest?.icon).isEqualTo(expectedIcon) assertThat(latest?.stateDescription.loadContentDescription(context)) .isEqualTo( @@ -426,8 +410,9 @@ class InternetTileDataInteractorTest : SysuiTestCase() { assertThat(latest?.secondaryLabel).isNull() assertThat(latest?.secondaryTitle) .isEqualTo(context.getString(R.string.quick_settings_networks_available)) - assertThat(latest?.icon).isNull() - assertThat(latest?.iconId).isEqualTo(R.drawable.ic_qs_no_internet_available) + val expectedIcon = + InternetTileIconModel.ResourceId(R.drawable.ic_qs_no_internet_available) + assertThat(latest?.icon).isEqualTo(expectedIcon) assertThat(latest?.stateDescription).isNull() val expectedCd = "$internet,${context.getString(R.string.quick_settings_networks_available)}" @@ -435,54 +420,19 @@ class InternetTileDataInteractorTest : SysuiTestCase() { .isEqualTo(expectedCd) } - /** - * We expect a RuntimeException because [underTest] instantiates a SignalDrawable on the - * provided context, and so the SignalDrawable constructor attempts to instantiate a Handler() - * on the mentioned context. Since that context does not have a looper assigned to it, the - * handler instantiation will throw a RuntimeException. - * - * TODO(b/338068066): Robolectric behavior differs in that it does not throw the exception So - * either we should make Robolectric behave similar to the device test, or change this test to - * look for a different signal than the exception, when run by Robolectric. For now we just - * assume the test is not Robolectric. - */ - @Test(expected = java.lang.RuntimeException::class) - fun mobileDefault_usesNetworkNameAndIcon_throwsRunTimeException() = - testScope.runTest { - assumeFalse(isRobolectricTest()) - - collectLastValue(underTest.tileData(testUser, flowOf(DataUpdateTrigger.InitialRequest))) - - connectivityRepository.setMobileConnected() - mobileConnectionsRepository.mobileIsDefault.value = true - mobileConnectionRepository.apply { - setAllLevels(3) - setAllRoaming(false) - networkName.value = NetworkNameModel.Default("test network") - } - - runCurrent() - } - - /** - * See [mobileDefault_usesNetworkNameAndIcon_throwsRunTimeException] for description of the - * problem this test solves. The solution here is to assign a looper to the context via - * RunWithLooper. In the production code, the solution is to use a Main CoroutineContext for - * creating the SignalDrawable. - */ - @TestableLooper.RunWithLooper @Test - fun mobileDefault_run_withLooper_usesNetworkNameAndIcon() = + fun mobileDefault_usesNetworkNameAndIcon() = testScope.runTest { val latest by collectLastValue( underTest.tileData(testUser, flowOf(DataUpdateTrigger.InitialRequest)) ) + val iconLevel = 3 connectivityRepository.setMobileConnected() mobileConnectionsRepository.mobileIsDefault.value = true mobileConnectionRepository.apply { - setAllLevels(3) + setAllLevels(iconLevel) setAllRoaming(false) networkName.value = NetworkNameModel.Default("test network") } @@ -491,8 +441,9 @@ class InternetTileDataInteractorTest : SysuiTestCase() { assertThat(latest?.secondaryTitle).isNotNull() assertThat(latest?.secondaryTitle.toString()).contains("test network") assertThat(latest?.secondaryLabel).isNull() - assertThat(latest?.icon).isInstanceOf(Icon.Loaded::class.java) - assertThat(latest?.iconId).isNull() + val expectedIcon = InternetTileIconModel.Cellular(iconLevel) + + assertThat(latest?.icon).isEqualTo(expectedIcon) assertThat(latest?.stateDescription.loadContentDescription(context)) .isEqualTo(latest?.secondaryTitle.toString()) assertThat(latest?.contentDescription.loadContentDescription(context)) @@ -513,8 +464,8 @@ class InternetTileDataInteractorTest : SysuiTestCase() { assertThat(latest?.secondaryLabel.loadText(context)) .isEqualTo(ethernetIcon!!.contentDescription.loadContentDescription(context)) assertThat(latest?.secondaryTitle).isNull() - assertThat(latest?.iconId).isEqualTo(R.drawable.stat_sys_ethernet_fully) - assertThat(latest?.icon).isNull() + val expectedIcon = InternetTileIconModel.ResourceId(R.drawable.stat_sys_ethernet_fully) + assertThat(latest?.icon).isEqualTo(expectedIcon) assertThat(latest?.stateDescription).isNull() assertThat(latest?.contentDescription.loadContentDescription(context)) .isEqualTo(latest?.secondaryLabel.loadText(context)) @@ -534,8 +485,8 @@ class InternetTileDataInteractorTest : SysuiTestCase() { assertThat(latest?.secondaryLabel.loadText(context)) .isEqualTo(ethernetIcon!!.contentDescription.loadContentDescription(context)) assertThat(latest?.secondaryTitle).isNull() - assertThat(latest?.iconId).isEqualTo(R.drawable.stat_sys_ethernet) - assertThat(latest?.icon).isNull() + val expectedIcon = InternetTileIconModel.ResourceId(R.drawable.stat_sys_ethernet) + assertThat(latest?.icon).isEqualTo(expectedIcon) assertThat(latest?.stateDescription).isNull() assertThat(latest?.contentDescription.loadContentDescription(context)) .isEqualTo(latest?.secondaryLabel.loadText(context)) @@ -543,11 +494,7 @@ class InternetTileDataInteractorTest : SysuiTestCase() { private fun setWifiNetworkWithHotspot(hotspot: WifiNetworkModel.HotspotDeviceType) { val networkModel = - WifiNetworkModel.Active.of( - level = 4, - ssid = "test ssid", - hotspotDeviceType = hotspot, - ) + WifiNetworkModel.Active.of(level = 4, ssid = "test ssid", hotspotDeviceType = hotspot) connectivityRepository.setWifiConnected() wifiRepository.setIsWifiDefault(true) @@ -560,7 +507,7 @@ class InternetTileDataInteractorTest : SysuiTestCase() { val NOT_CONNECTED_NETWORKS_UNAVAILABLE = InternetTileModel.Inactive( secondaryLabel = Text.Resource(R.string.quick_settings_networks_unavailable), - iconId = R.drawable.ic_qs_no_internet_unavailable, + icon = InternetTileIconModel.ResourceId(R.drawable.ic_qs_no_internet_unavailable), stateDescription = null, contentDescription = ContentDescription.Resource(R.string.quick_settings_networks_unavailable), diff --git a/packages/SystemUI/multivalentTests/src/com/android/systemui/qs/ui/viewmodel/QuickSettingsShadeOverlayContentViewModelTest.kt b/packages/SystemUI/multivalentTests/src/com/android/systemui/qs/ui/viewmodel/QuickSettingsShadeOverlayContentViewModelTest.kt index 8c7ec4743e7c..f32894dfd383 100644 --- a/packages/SystemUI/multivalentTests/src/com/android/systemui/qs/ui/viewmodel/QuickSettingsShadeOverlayContentViewModelTest.kt +++ b/packages/SystemUI/multivalentTests/src/com/android/systemui/qs/ui/viewmodel/QuickSettingsShadeOverlayContentViewModelTest.kt @@ -21,18 +21,33 @@ import android.testing.TestableLooper import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.filters.SmallTest import com.android.systemui.SysuiTestCase +import com.android.systemui.authentication.data.repository.FakeAuthenticationRepository +import com.android.systemui.authentication.domain.interactor.AuthenticationResult +import com.android.systemui.authentication.domain.interactor.authenticationInteractor import com.android.systemui.coroutines.collectLastValue import com.android.systemui.flags.EnableSceneContainer import com.android.systemui.kosmos.testScope +import com.android.systemui.lifecycle.activateIn +import com.android.systemui.power.domain.interactor.PowerInteractor.Companion.setAsleepForTest +import com.android.systemui.power.domain.interactor.PowerInteractor.Companion.setAwakeForTest +import com.android.systemui.power.domain.interactor.powerInteractor import com.android.systemui.scene.domain.interactor.sceneInteractor +import com.android.systemui.scene.domain.startable.sceneContainerStartable import com.android.systemui.scene.shared.model.Overlays +import com.android.systemui.scene.shared.model.Scenes +import com.android.systemui.shade.domain.interactor.shadeInteractor import com.android.systemui.shade.shared.flag.DualShade import com.android.systemui.testKosmos import com.google.common.truth.Truth.assertThat +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.TestScope +import kotlinx.coroutines.test.runCurrent import kotlinx.coroutines.test.runTest +import org.junit.Before import org.junit.Test import org.junit.runner.RunWith +@OptIn(ExperimentalCoroutinesApi::class) @SmallTest @RunWith(AndroidJUnit4::class) @TestableLooper.RunWithLooper @@ -46,18 +61,84 @@ class QuickSettingsShadeOverlayContentViewModelTest : SysuiTestCase() { private val underTest by lazy { kosmos.quickSettingsShadeOverlayContentViewModel } + @Before + fun setUp() { + kosmos.sceneContainerStartable.start() + underTest.activateIn(testScope) + } + @Test fun onScrimClicked_hidesShade() = testScope.runTest { val currentOverlays by collectLastValue(sceneInteractor.currentOverlays) - sceneInteractor.showOverlay( - overlay = Overlays.QuickSettingsShade, - loggingReason = "test", - ) + sceneInteractor.showOverlay(Overlays.QuickSettingsShade, "test") assertThat(currentOverlays).contains(Overlays.QuickSettingsShade) underTest.onScrimClicked() assertThat(currentOverlays).doesNotContain(Overlays.QuickSettingsShade) } + + @Test + fun deviceLocked_hidesShade() = + testScope.runTest { + val currentOverlays by collectLastValue(sceneInteractor.currentOverlays) + unlockDevice() + sceneInteractor.showOverlay(Overlays.QuickSettingsShade, "test") + assertThat(currentOverlays).contains(Overlays.QuickSettingsShade) + + lockDevice() + + assertThat(currentOverlays).isEmpty() + } + + @Test + fun bouncerShown_hidesShade() = + testScope.runTest { + val currentOverlays by collectLastValue(sceneInteractor.currentOverlays) + lockDevice() + sceneInteractor.showOverlay(Overlays.QuickSettingsShade, "test") + assertThat(currentOverlays).contains(Overlays.QuickSettingsShade) + + sceneInteractor.changeScene(Scenes.Bouncer, "test") + runCurrent() + + assertThat(currentOverlays).doesNotContain(Overlays.QuickSettingsShade) + } + + @Test + fun shadeNotTouchable_hidesShade() = + testScope.runTest { + val currentOverlays by collectLastValue(sceneInteractor.currentOverlays) + val isShadeTouchable by collectLastValue(kosmos.shadeInteractor.isShadeTouchable) + assertThat(isShadeTouchable).isTrue() + sceneInteractor.showOverlay(Overlays.QuickSettingsShade, "test") + assertThat(currentOverlays).contains(Overlays.QuickSettingsShade) + + lockDevice() + assertThat(isShadeTouchable).isFalse() + assertThat(currentOverlays).doesNotContain(Overlays.QuickSettingsShade) + } + + private fun TestScope.lockDevice() { + val currentScene by collectLastValue(sceneInteractor.currentScene) + kosmos.powerInteractor.setAsleepForTest() + runCurrent() + + assertThat(currentScene).isEqualTo(Scenes.Lockscreen) + } + + private suspend fun TestScope.unlockDevice() { + val currentScene by collectLastValue(sceneInteractor.currentScene) + kosmos.powerInteractor.setAwakeForTest() + runCurrent() + assertThat( + kosmos.authenticationInteractor.authenticate( + FakeAuthenticationRepository.DEFAULT_PIN + ) + ) + .isEqualTo(AuthenticationResult.SUCCEEDED) + + assertThat(currentScene).isEqualTo(Scenes.Gone) + } } diff --git a/packages/SystemUI/multivalentTests/src/com/android/systemui/recordissue/IssueRecordingServiceCommandHandlerTest.kt b/packages/SystemUI/multivalentTests/src/com/android/systemui/recordissue/IssueRecordingServiceSessionTest.kt index 57cfe1b9e902..3e5dee69c85c 100644 --- a/packages/SystemUI/multivalentTests/src/com/android/systemui/recordissue/IssueRecordingServiceCommandHandlerTest.kt +++ b/packages/SystemUI/multivalentTests/src/com/android/systemui/recordissue/IssueRecordingServiceSessionTest.kt @@ -47,7 +47,7 @@ import org.mockito.kotlin.verify @SmallTest @RunWith(AndroidJUnit4::class) @TestableLooper.RunWithLooper(setAsMainLooper = true) -class IssueRecordingServiceCommandHandlerTest : SysuiTestCase() { +class IssueRecordingServiceSessionTest : SysuiTestCase() { private val kosmos = Kosmos().also { it.testCase = this } private val bgExecutor = kosmos.fakeExecutor @@ -61,13 +61,13 @@ class IssueRecordingServiceCommandHandlerTest : SysuiTestCase() { private val notificationManager = mock<NotificationManager>() private val panelInteractor = mock<PanelInteractor>() - private lateinit var underTest: IssueRecordingServiceCommandHandler + private lateinit var underTest: IssueRecordingServiceSession @Before fun setup() { traceurMessageSender = mock<TraceurMessageSender>() underTest = - IssueRecordingServiceCommandHandler( + IssueRecordingServiceSession( bgExecutor, dialogTransitionAnimator, panelInteractor, @@ -75,13 +75,13 @@ class IssueRecordingServiceCommandHandlerTest : SysuiTestCase() { issueRecordingState, iActivityManager, notificationManager, - userContextProvider + userContextProvider, ) } @Test fun startsTracing_afterReceivingActionStartCommand() { - underTest.handleStartCommand() + underTest.start() bgExecutor.runAllReady() Truth.assertThat(issueRecordingState.isRecording).isTrue() @@ -90,7 +90,7 @@ class IssueRecordingServiceCommandHandlerTest : SysuiTestCase() { @Test fun stopsTracing_afterReceivingStopTracingCommand() { - underTest.handleStopCommand(mContext.contentResolver) + underTest.stop(mContext.contentResolver) bgExecutor.runAllReady() Truth.assertThat(issueRecordingState.isRecording).isFalse() @@ -99,7 +99,7 @@ class IssueRecordingServiceCommandHandlerTest : SysuiTestCase() { @Test fun cancelsNotification_afterReceivingShareCommand() { - underTest.handleShareCommand(0, null, mContext) + underTest.share(0, null, mContext) bgExecutor.runAllReady() verify(notificationManager).cancelAsUser(isNull(), anyInt(), any<UserHandle>()) @@ -110,7 +110,7 @@ class IssueRecordingServiceCommandHandlerTest : SysuiTestCase() { issueRecordingState.takeBugreport = true val uri = mock<Uri>() - underTest.handleShareCommand(0, uri, mContext) + underTest.share(0, uri, mContext) bgExecutor.runAllReady() verify(iActivityManager).requestBugReportWithExtraAttachment(uri) @@ -121,7 +121,7 @@ class IssueRecordingServiceCommandHandlerTest : SysuiTestCase() { issueRecordingState.takeBugreport = false val uri = mock<Uri>() - underTest.handleShareCommand(0, uri, mContext) + underTest.share(0, uri, mContext) bgExecutor.runAllReady() verify(traceurMessageSender).shareTraces(mContext, uri) @@ -131,7 +131,7 @@ class IssueRecordingServiceCommandHandlerTest : SysuiTestCase() { fun closesShade_afterReceivingShareCommand() { val uri = mock<Uri>() - underTest.handleShareCommand(0, uri, mContext) + underTest.share(0, uri, mContext) bgExecutor.runAllReady() verify(panelInteractor).collapsePanels() diff --git a/packages/SystemUI/multivalentTests/src/com/android/systemui/statusbar/notification/collection/coordinator/LockScreenMinimalismCoordinatorTest.kt b/packages/SystemUI/multivalentTests/src/com/android/systemui/statusbar/notification/collection/coordinator/LockScreenMinimalismCoordinatorTest.kt index 7b87aeb60c13..d772e3effbeb 100644 --- a/packages/SystemUI/multivalentTests/src/com/android/systemui/statusbar/notification/collection/coordinator/LockScreenMinimalismCoordinatorTest.kt +++ b/packages/SystemUI/multivalentTests/src/com/android/systemui/statusbar/notification/collection/coordinator/LockScreenMinimalismCoordinatorTest.kt @@ -42,7 +42,8 @@ import com.android.systemui.statusbar.notification.collection.modifyEntry import com.android.systemui.statusbar.notification.collection.notifcollection.NotifCollectionListener import com.android.systemui.statusbar.notification.data.repository.FakeHeadsUpRowRepository import com.android.systemui.statusbar.notification.data.repository.activeNotificationListRepository -import com.android.systemui.statusbar.notification.shared.NotificationMinimalismPrototype +import com.android.systemui.statusbar.notification.domain.interactor.lockScreenNotificationMinimalismSetting +import com.android.systemui.statusbar.notification.shared.NotificationMinimalism import com.android.systemui.statusbar.notification.stack.data.repository.headsUpNotificationRepository import com.android.systemui.testKosmos import com.android.systemui.util.settings.FakeSettings @@ -66,7 +67,7 @@ import org.mockito.kotlin.whenever @SmallTest @RunWith(AndroidJUnit4::class) -@EnableFlags(NotificationMinimalismPrototype.FLAG_NAME) +@EnableFlags(NotificationMinimalism.FLAG_NAME) class LockScreenMinimalismCoordinatorTest : SysuiTestCase() { private val kosmos = @@ -76,7 +77,7 @@ class LockScreenMinimalismCoordinatorTest : SysuiTestCase() { mock<SysuiStatusBarStateController>().also { mock -> doAnswer { statusBarState.ordinal }.whenever(mock).state } - fakeSettings.putInt(Settings.Secure.LOCK_SCREEN_SHOW_ONLY_UNSEEN_NOTIFICATIONS, 1) + lockScreenNotificationMinimalismSetting = true } private val notifPipeline: NotifPipeline = mock() private var statusBarState: StatusBarState = StatusBarState.KEYGUARD @@ -193,7 +194,7 @@ class LockScreenMinimalismCoordinatorTest : SysuiTestCase() { kosmos.activeNotificationListRepository.topUnseenNotificationKey.value = child2.key assertThat(promoter.shouldPromoteToTopLevel(child1)).isFalse() assertThat(promoter.shouldPromoteToTopLevel(child2)) - .isEqualTo(NotificationMinimalismPrototype.ungroupTopUnseen) + .isEqualTo(NotificationMinimalism.ungroupTopUnseen) assertThat(promoter.shouldPromoteToTopLevel(child3)).isFalse() assertThat(promoter.shouldPromoteToTopLevel(parent)).isFalse() @@ -201,7 +202,7 @@ class LockScreenMinimalismCoordinatorTest : SysuiTestCase() { kosmos.activeNotificationListRepository.topUnseenNotificationKey.value = child2.key assertThat(promoter.shouldPromoteToTopLevel(child1)).isTrue() assertThat(promoter.shouldPromoteToTopLevel(child2)) - .isEqualTo(NotificationMinimalismPrototype.ungroupTopUnseen) + .isEqualTo(NotificationMinimalism.ungroupTopUnseen) assertThat(promoter.shouldPromoteToTopLevel(child3)).isFalse() assertThat(promoter.shouldPromoteToTopLevel(parent)).isFalse() } diff --git a/packages/SystemUI/multivalentTests/src/com/android/systemui/statusbar/notification/domain/interactor/SeenNotificationsInteractorTest.kt b/packages/SystemUI/multivalentTests/src/com/android/systemui/statusbar/notification/domain/interactor/SeenNotificationsInteractorTest.kt index 2159b864d2a2..ea2e25e8eb1c 100644 --- a/packages/SystemUI/multivalentTests/src/com/android/systemui/statusbar/notification/domain/interactor/SeenNotificationsInteractorTest.kt +++ b/packages/SystemUI/multivalentTests/src/com/android/systemui/statusbar/notification/domain/interactor/SeenNotificationsInteractorTest.kt @@ -21,7 +21,7 @@ import androidx.test.filters.SmallTest import com.android.systemui.SysuiTestCase import com.android.systemui.coroutines.collectLastValue import com.android.systemui.statusbar.notification.collection.NotificationEntryBuilder -import com.android.systemui.statusbar.notification.shared.NotificationMinimalismPrototype +import com.android.systemui.statusbar.notification.shared.NotificationMinimalism import com.android.systemui.testKosmos import com.google.common.truth.Truth.assertThat import kotlinx.coroutines.test.runTest @@ -57,7 +57,7 @@ class SeenNotificationsInteractorTest : SysuiTestCase() { } @Test - @EnableFlags(NotificationMinimalismPrototype.FLAG_NAME) + @EnableFlags(NotificationMinimalism.FLAG_NAME) fun topOngoingAndUnseenNotification() = runTest { val entry1 = NotificationEntryBuilder().setTag("entry1").build() val entry2 = NotificationEntryBuilder().setTag("entry2").build() @@ -91,4 +91,17 @@ class SeenNotificationsInteractorTest : SysuiTestCase() { testScheduler.runCurrent() assertThat(settingEnabled).isTrue() } + + fun testLockScreenNotificationMinimalismSetting() = runTest { + val settingEnabled by + collectLastValue(underTest.isLockScreenNotificationMinimalismEnabled()) + + kosmos.lockScreenNotificationMinimalismSetting = false + testScheduler.runCurrent() + assertThat(settingEnabled).isFalse() + + kosmos.lockScreenNotificationMinimalismSetting = true + testScheduler.runCurrent() + assertThat(settingEnabled).isTrue() + } } diff --git a/packages/SystemUI/multivalentTests/src/com/android/systemui/statusbar/notification/emptyshade/ui/viewmodel/EmptyShadeViewModelTest.kt b/packages/SystemUI/multivalentTests/src/com/android/systemui/statusbar/notification/emptyshade/ui/viewmodel/EmptyShadeViewModelTest.kt index f9b77697b767..28857a08c2bd 100644 --- a/packages/SystemUI/multivalentTests/src/com/android/systemui/statusbar/notification/emptyshade/ui/viewmodel/EmptyShadeViewModelTest.kt +++ b/packages/SystemUI/multivalentTests/src/com/android/systemui/statusbar/notification/emptyshade/ui/viewmodel/EmptyShadeViewModelTest.kt @@ -30,6 +30,7 @@ import com.android.systemui.SysuiTestCase import com.android.systemui.coroutines.collectLastValue import com.android.systemui.flags.andSceneContainer import com.android.systemui.kosmos.testScope +import com.android.systemui.shared.settings.data.repository.fakeSecureSettingsRepository import com.android.systemui.statusbar.notification.data.repository.activeNotificationListRepository import com.android.systemui.statusbar.notification.emptyshade.shared.ModesEmptyShadeFix import com.android.systemui.statusbar.notification.footer.shared.FooterViewRefactor @@ -53,6 +54,7 @@ class EmptyShadeViewModelTest(flags: FlagsParameterization) : SysuiTestCase() { private val testScope = kosmos.testScope private val zenModeRepository = kosmos.zenModeRepository private val activeNotificationListRepository = kosmos.activeNotificationListRepository + private val fakeSecureSettingsRepository = kosmos.fakeSecureSettingsRepository private val underTest = kosmos.emptyShadeViewModel @@ -205,4 +207,84 @@ class EmptyShadeViewModelTest(flags: FlagsParameterization) : SysuiTestCase() { assertThat(footerVisible).isTrue() } + + @EnableFlags(ModesEmptyShadeFix.FLAG_NAME) + @Test + fun onClick_whenHistoryDisabled_leadsToSettingsPage() = + testScope.runTest { + val onClick by collectLastValue(underTest.onClick) + runCurrent() + + fakeSecureSettingsRepository.setInt(Settings.Secure.NOTIFICATION_HISTORY_ENABLED, 0) + + assertThat(onClick?.targetIntent?.action) + .isEqualTo(Settings.ACTION_NOTIFICATION_SETTINGS) + assertThat(onClick?.backStack).isEmpty() + } + + @EnableFlags(ModesEmptyShadeFix.FLAG_NAME) + @Test + fun onClick_whenHistoryEnabled_leadsToHistoryPage() = + testScope.runTest { + val onClick by collectLastValue(underTest.onClick) + runCurrent() + + fakeSecureSettingsRepository.setInt(Settings.Secure.NOTIFICATION_HISTORY_ENABLED, 1) + + assertThat(onClick?.targetIntent?.action) + .isEqualTo(Settings.ACTION_NOTIFICATION_HISTORY) + assertThat(onClick?.backStack?.map { it.action }) + .containsExactly(Settings.ACTION_NOTIFICATION_SETTINGS) + } + + @EnableFlags(ModesEmptyShadeFix.FLAG_NAME) + @Test + fun onClick_whenOneModeHidingNotifications_leadsToModeSettings() = + testScope.runTest { + val onClick by collectLastValue(underTest.onClick) + runCurrent() + + zenModeRepository.addMode( + TestModeBuilder() + .setId("ID") + .setActive(true) + .setVisualEffect(VISUAL_EFFECT_NOTIFICATION_LIST, /* allowed= */ false) + .build() + ) + runCurrent() + + assertThat(onClick?.targetIntent?.action) + .isEqualTo(Settings.ACTION_AUTOMATIC_ZEN_RULE_SETTINGS) + assertThat( + onClick?.targetIntent?.extras?.getString(Settings.EXTRA_AUTOMATIC_ZEN_RULE_ID) + ) + .isEqualTo("ID") + assertThat(onClick?.backStack?.map { it.action }) + .containsExactly(Settings.ACTION_ZEN_MODE_SETTINGS) + } + + @EnableFlags(ModesEmptyShadeFix.FLAG_NAME) + @Test + fun onClick_whenMultipleModesHidingNotifications_leadsToGeneralModesSettings() = + testScope.runTest { + val onClick by collectLastValue(underTest.onClick) + runCurrent() + + zenModeRepository.addMode( + TestModeBuilder() + .setActive(true) + .setVisualEffect(VISUAL_EFFECT_NOTIFICATION_LIST, /* allowed= */ false) + .build() + ) + zenModeRepository.addMode( + TestModeBuilder() + .setActive(true) + .setVisualEffect(VISUAL_EFFECT_NOTIFICATION_LIST, /* allowed= */ false) + .build() + ) + runCurrent() + + assertThat(onClick?.targetIntent?.action).isEqualTo(Settings.ACTION_ZEN_MODE_SETTINGS) + assertThat(onClick?.backStack).isEmpty() + } } diff --git a/packages/SystemUI/plugin/src/com/android/systemui/plugins/qs/QSTile.java b/packages/SystemUI/plugin/src/com/android/systemui/plugins/qs/QSTile.java index be44dee0aae6..73626b457dcf 100644 --- a/packages/SystemUI/plugin/src/com/android/systemui/plugins/qs/QSTile.java +++ b/packages/SystemUI/plugin/src/com/android/systemui/plugins/qs/QSTile.java @@ -184,7 +184,10 @@ public interface QSTile { } } - /** Get the text for secondaryLabel. */ + /** + * If the current secondaryLabel value is not empty, ignore the given input and return + * the current value. Otherwise return current value. + */ public CharSequence getSecondaryLabel(CharSequence stateText) { // Use a local reference as the value might change from other threads CharSequence localSecondaryLabel = secondaryLabel; diff --git a/packages/SystemUI/shared/Android.bp b/packages/SystemUI/shared/Android.bp index 8f55961af4e9..0f1da509468a 100644 --- a/packages/SystemUI/shared/Android.bp +++ b/packages/SystemUI/shared/Android.bp @@ -70,6 +70,7 @@ android_library { "jsr330", "//frameworks/libs/systemui:com_android_systemui_shared_flags_lib", "//frameworks/libs/systemui:msdl", + "//frameworks/libs/systemui:view_capture", ], resource_dirs: [ "res", diff --git a/packages/SystemUI/shared/src/com/android/systemui/shared/rotation/FloatingRotationButton.java b/packages/SystemUI/shared/src/com/android/systemui/shared/rotation/FloatingRotationButton.java index f358ba2d3ccd..4db6ab6ea579 100644 --- a/packages/SystemUI/shared/src/com/android/systemui/shared/rotation/FloatingRotationButton.java +++ b/packages/SystemUI/shared/src/com/android/systemui/shared/rotation/FloatingRotationButton.java @@ -16,6 +16,9 @@ package com.android.systemui.shared.rotation; +import static com.android.app.viewcapture.ViewCaptureFactory.getViewCaptureAwareWindowManagerInstance; +import static com.android.systemui.Flags.enableViewCaptureTracing; + import android.annotation.DimenRes; import android.annotation.IdRes; import android.annotation.LayoutRes; @@ -30,7 +33,6 @@ import android.graphics.drawable.Drawable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.view.WindowManager; import android.view.WindowManager.LayoutParams; import android.view.animation.AccelerateDecelerateInterpolator; import android.widget.FrameLayout; @@ -38,6 +40,7 @@ import android.widget.FrameLayout; import androidx.annotation.BoolRes; import androidx.core.view.OneShotPreDrawListener; +import com.android.app.viewcapture.ViewCaptureAwareWindowManager; import com.android.systemui.shared.rotation.FloatingRotationButtonPositionCalculator.Position; /** @@ -47,7 +50,7 @@ public class FloatingRotationButton implements RotationButton { private static final int MARGIN_ANIMATION_DURATION_MILLIS = 300; - private final WindowManager mWindowManager; + private final ViewCaptureAwareWindowManager mWindowManager; private final ViewGroup mKeyButtonContainer; private final FloatingRotationButtonView mKeyButtonView; @@ -88,7 +91,8 @@ public class FloatingRotationButton implements RotationButton { @DimenRes int taskbarBottomMargin, @DimenRes int buttonDiameter, @DimenRes int rippleMaxWidth, @BoolRes int floatingRotationBtnPositionLeftResource) { mContext = context; - mWindowManager = mContext.getSystemService(WindowManager.class); + mWindowManager = getViewCaptureAwareWindowManagerInstance(mContext, + enableViewCaptureTracing()); mKeyButtonContainer = (ViewGroup) LayoutInflater.from(mContext).inflate(layout, null); mKeyButtonView = mKeyButtonContainer.findViewById(keyButtonId); mKeyButtonView.setVisibility(View.VISIBLE); diff --git a/packages/SystemUI/src/com/android/systemui/dreams/DreamOverlayService.java b/packages/SystemUI/src/com/android/systemui/dreams/DreamOverlayService.java index 113e0011f5bd..83f86a718029 100644 --- a/packages/SystemUI/src/com/android/systemui/dreams/DreamOverlayService.java +++ b/packages/SystemUI/src/com/android/systemui/dreams/DreamOverlayService.java @@ -65,6 +65,7 @@ import com.android.systemui.dreams.dagger.DreamOverlayComponent; import com.android.systemui.keyguard.domain.interactor.KeyguardInteractor; import com.android.systemui.navigationbar.gestural.domain.GestureInteractor; import com.android.systemui.navigationbar.gestural.domain.TaskMatcher; +import com.android.systemui.scene.shared.flag.SceneContainerFlag; import com.android.systemui.shade.ShadeExpansionChangeEvent; import com.android.systemui.touch.TouchInsetManager; import com.android.systemui.util.concurrency.DelayableExecutor; @@ -499,8 +500,11 @@ public class DreamOverlayService extends android.service.dreams.DreamOverlayServ mDreamOverlayContainerViewController = dreamOverlayComponent.getDreamOverlayContainerViewController(); - mTouchMonitor = ambientTouchComponent.getTouchMonitor(); - mTouchMonitor.init(); + + if (!SceneContainerFlag.isEnabled()) { + mTouchMonitor = ambientTouchComponent.getTouchMonitor(); + mTouchMonitor.init(); + } mStateController.setShouldShowComplications(shouldShowComplications()); diff --git a/packages/SystemUI/src/com/android/systemui/flags/FlagDependencies.kt b/packages/SystemUI/src/com/android/systemui/flags/FlagDependencies.kt index 820c102e81d8..47f0ecfb237a 100644 --- a/packages/SystemUI/src/com/android/systemui/flags/FlagDependencies.kt +++ b/packages/SystemUI/src/com/android/systemui/flags/FlagDependencies.kt @@ -34,12 +34,14 @@ import com.android.systemui.dagger.SysUISingleton import com.android.systemui.keyguard.MigrateClocksToBlueprint import com.android.systemui.scene.shared.flag.SceneContainerFlag import com.android.systemui.shade.shared.flag.DualShade +import com.android.systemui.statusbar.core.StatusBarConnectedDisplays +import com.android.systemui.statusbar.core.StatusBarSimpleFragment import com.android.systemui.statusbar.notification.collection.SortBySectionTimeFlag import com.android.systemui.statusbar.notification.emptyshade.shared.ModesEmptyShadeFix import com.android.systemui.statusbar.notification.footer.shared.FooterViewRefactor import com.android.systemui.statusbar.notification.interruption.VisualInterruptionRefactor import com.android.systemui.statusbar.notification.shared.NotificationAvalancheSuppression -import com.android.systemui.statusbar.notification.shared.NotificationMinimalismPrototype +import com.android.systemui.statusbar.notification.shared.NotificationMinimalism import com.android.systemui.statusbar.notification.shared.NotificationThrottleHun import com.android.systemui.statusbar.notification.shared.PriorityPeopleSection import javax.inject.Inject @@ -57,7 +59,7 @@ class FlagDependencies @Inject constructor(featureFlags: FeatureFlagsClassic, ha // Internal notification frontend dependencies NotificationAvalancheSuppression.token dependsOn VisualInterruptionRefactor.token PriorityPeopleSection.token dependsOn SortBySectionTimeFlag.token - NotificationMinimalismPrototype.token dependsOn NotificationThrottleHun.token + NotificationMinimalism.token dependsOn NotificationThrottleHun.token ModesEmptyShadeFix.token dependsOn FooterViewRefactor.token ModesEmptyShadeFix.token dependsOn modesUi @@ -73,6 +75,8 @@ class FlagDependencies @Inject constructor(featureFlags: FeatureFlagsClassic, ha // Status bar chip dependencies statusBarCallChipNotificationIconToken dependsOn statusBarUseReposForCallChipToken statusBarCallChipNotificationIconToken dependsOn statusBarScreenSharingChipsToken + + StatusBarConnectedDisplays.token dependsOn StatusBarSimpleFragment.token } private inline val politeNotifications diff --git a/packages/SystemUI/src/com/android/systemui/inputdevice/tutorial/KeyboardTouchpadTutorialMetricsLogger.kt b/packages/SystemUI/src/com/android/systemui/inputdevice/tutorial/KeyboardTouchpadTutorialMetricsLogger.kt new file mode 100644 index 000000000000..144c5ead1bb8 --- /dev/null +++ b/packages/SystemUI/src/com/android/systemui/inputdevice/tutorial/KeyboardTouchpadTutorialMetricsLogger.kt @@ -0,0 +1,55 @@ +/* + * 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.systemui.inputdevice.tutorial + +import com.android.systemui.inputdevice.tutorial.ui.view.KeyboardTouchpadTutorialActivity.Companion.INTENT_TUTORIAL_ENTRY_POINT_CONTEXTUAL_EDU +import com.android.systemui.inputdevice.tutorial.ui.view.KeyboardTouchpadTutorialActivity.Companion.INTENT_TUTORIAL_ENTRY_POINT_SCHEDULER +import com.android.systemui.inputdevice.tutorial.ui.view.KeyboardTouchpadTutorialActivity.Companion.INTENT_TUTORIAL_TYPE_KEYBOARD +import com.android.systemui.inputdevice.tutorial.ui.view.KeyboardTouchpadTutorialActivity.Companion.INTENT_TUTORIAL_TYPE_TOUCHPAD +import com.android.systemui.shared.system.SysUiStatsLog +import javax.inject.Inject + +class KeyboardTouchpadTutorialMetricsLogger @Inject constructor() { + + fun logPeripheralTutorialLaunched(entryPointExtra: String?, tutorialTypeExtra: String?) { + val entryPoint = + when (entryPointExtra) { + INTENT_TUTORIAL_ENTRY_POINT_SCHEDULER -> + SysUiStatsLog.PERIPHERAL_TUTORIAL_LAUNCHED__ENTRY_POINT__SCHEDULED + INTENT_TUTORIAL_ENTRY_POINT_CONTEXTUAL_EDU -> + SysUiStatsLog.PERIPHERAL_TUTORIAL_LAUNCHED__ENTRY_POINT__CONTEXTUAL_EDU + else -> SysUiStatsLog.PERIPHERAL_TUTORIAL_LAUNCHED__ENTRY_POINT__APP + } + + val tutorialType = + when (tutorialTypeExtra) { + INTENT_TUTORIAL_TYPE_KEYBOARD -> + SysUiStatsLog.PERIPHERAL_TUTORIAL_LAUNCHED__TUTORIAL_TYPE__KEYBOARD + INTENT_TUTORIAL_TYPE_TOUCHPAD -> + SysUiStatsLog.PERIPHERAL_TUTORIAL_LAUNCHED__TUTORIAL_TYPE__TOUCHPAD + else -> SysUiStatsLog.PERIPHERAL_TUTORIAL_LAUNCHED__TUTORIAL_TYPE__BOTH + } + + SysUiStatsLog.write(SysUiStatsLog.PERIPHERAL_TUTORIAL_LAUNCHED, entryPoint, tutorialType) + } + + fun logPeripheralTutorialLaunchedFromSettings() { + val entryPoint = SysUiStatsLog.PERIPHERAL_TUTORIAL_LAUNCHED__ENTRY_POINT__SETTINGS + val tutorialType = SysUiStatsLog.PERIPHERAL_TUTORIAL_LAUNCHED__TUTORIAL_TYPE__TOUCHPAD + SysUiStatsLog.write(SysUiStatsLog.PERIPHERAL_TUTORIAL_LAUNCHED, entryPoint, tutorialType) + } +} diff --git a/packages/SystemUI/src/com/android/systemui/inputdevice/tutorial/ui/TutorialNotificationCoordinator.kt b/packages/SystemUI/src/com/android/systemui/inputdevice/tutorial/ui/TutorialNotificationCoordinator.kt index 5d9dda3899cd..f2afaee1870b 100644 --- a/packages/SystemUI/src/com/android/systemui/inputdevice/tutorial/ui/TutorialNotificationCoordinator.kt +++ b/packages/SystemUI/src/com/android/systemui/inputdevice/tutorial/ui/TutorialNotificationCoordinator.kt @@ -31,6 +31,8 @@ import com.android.systemui.inputdevice.tutorial.domain.interactor.TutorialSched import com.android.systemui.inputdevice.tutorial.domain.interactor.TutorialSchedulerInteractor.Companion.TAG import com.android.systemui.inputdevice.tutorial.domain.interactor.TutorialSchedulerInteractor.TutorialType import com.android.systemui.inputdevice.tutorial.ui.view.KeyboardTouchpadTutorialActivity +import com.android.systemui.inputdevice.tutorial.ui.view.KeyboardTouchpadTutorialActivity.Companion.INTENT_TUTORIAL_ENTRY_POINT_KEY +import com.android.systemui.inputdevice.tutorial.ui.view.KeyboardTouchpadTutorialActivity.Companion.INTENT_TUTORIAL_ENTRY_POINT_SCHEDULER import com.android.systemui.inputdevice.tutorial.ui.view.KeyboardTouchpadTutorialActivity.Companion.INTENT_TUTORIAL_TYPE_BOTH import com.android.systemui.inputdevice.tutorial.ui.view.KeyboardTouchpadTutorialActivity.Companion.INTENT_TUTORIAL_TYPE_KEY import com.android.systemui.inputdevice.tutorial.ui.view.KeyboardTouchpadTutorialActivity.Companion.INTENT_TUTORIAL_TYPE_KEYBOARD @@ -48,7 +50,7 @@ constructor( @Background private val backgroundScope: CoroutineScope, @Application private val context: Context, private val tutorialSchedulerInteractor: TutorialSchedulerInteractor, - private val notificationManager: NotificationManager + private val notificationManager: NotificationManager, ) { fun start() { backgroundScope.launch { @@ -68,7 +70,7 @@ constructor( val extras = Bundle() extras.putString( Notification.EXTRA_SUBSTITUTE_APP_NAME, - context.getString(com.android.internal.R.string.android_system_label) + context.getString(com.android.internal.R.string.android_system_label), ) val info = getNotificationInfo(tutorialType)!! @@ -91,7 +93,7 @@ constructor( NotificationChannel( CHANNEL_ID, context.getString(com.android.internal.R.string.android_system_label), - NotificationManager.IMPORTANCE_DEFAULT + NotificationManager.IMPORTANCE_DEFAULT, ) notificationManager.createNotificationChannel(channel) } @@ -100,13 +102,14 @@ constructor( val intent = Intent(context, KeyboardTouchpadTutorialActivity::class.java).apply { putExtra(INTENT_TUTORIAL_TYPE_KEY, tutorialType) + putExtra(INTENT_TUTORIAL_ENTRY_POINT_KEY, INTENT_TUTORIAL_ENTRY_POINT_SCHEDULER) flags = Intent.FLAG_ACTIVITY_NEW_TASK } return PendingIntent.getActivity( context, /* requestCode= */ 0, intent, - PendingIntent.FLAG_IMMUTABLE + PendingIntent.FLAG_IMMUTABLE, ) } @@ -118,13 +121,13 @@ constructor( NotificationInfo( context.getString(R.string.launch_keyboard_tutorial_notification_title), context.getString(R.string.launch_keyboard_tutorial_notification_content), - INTENT_TUTORIAL_TYPE_KEYBOARD + INTENT_TUTORIAL_TYPE_KEYBOARD, ) TutorialType.TOUCHPAD -> NotificationInfo( context.getString(R.string.launch_touchpad_tutorial_notification_title), context.getString(R.string.launch_touchpad_tutorial_notification_content), - INTENT_TUTORIAL_TYPE_TOUCHPAD + INTENT_TUTORIAL_TYPE_TOUCHPAD, ) TutorialType.BOTH -> NotificationInfo( @@ -134,7 +137,7 @@ constructor( context.getString( R.string.launch_keyboard_touchpad_tutorial_notification_content ), - INTENT_TUTORIAL_TYPE_BOTH + INTENT_TUTORIAL_TYPE_BOTH, ) TutorialType.NONE -> null } diff --git a/packages/SystemUI/src/com/android/systemui/inputdevice/tutorial/ui/view/KeyboardTouchpadTutorialActivity.kt b/packages/SystemUI/src/com/android/systemui/inputdevice/tutorial/ui/view/KeyboardTouchpadTutorialActivity.kt index c130c6c7fe12..29febd32e925 100644 --- a/packages/SystemUI/src/com/android/systemui/inputdevice/tutorial/ui/view/KeyboardTouchpadTutorialActivity.kt +++ b/packages/SystemUI/src/com/android/systemui/inputdevice/tutorial/ui/view/KeyboardTouchpadTutorialActivity.kt @@ -30,6 +30,7 @@ import androidx.lifecycle.lifecycleScope import com.android.compose.theme.PlatformTheme import com.android.systemui.inputdevice.tutorial.InputDeviceTutorialLogger import com.android.systemui.inputdevice.tutorial.InputDeviceTutorialLogger.TutorialContext +import com.android.systemui.inputdevice.tutorial.KeyboardTouchpadTutorialMetricsLogger import com.android.systemui.inputdevice.tutorial.TouchpadTutorialScreensProvider import com.android.systemui.inputdevice.tutorial.ui.composable.ActionKeyTutorialScreen import com.android.systemui.inputdevice.tutorial.ui.viewmodel.KeyboardTouchpadTutorialViewModel @@ -51,6 +52,7 @@ constructor( private val viewModelFactoryAssistedProvider: ViewModelFactoryAssistedProvider, private val touchpadTutorialScreensProvider: Optional<TouchpadTutorialScreensProvider>, private val logger: InputDeviceTutorialLogger, + private val metricsLogger: KeyboardTouchpadTutorialMetricsLogger, ) : ComponentActivity() { companion object { @@ -58,6 +60,9 @@ constructor( const val INTENT_TUTORIAL_TYPE_TOUCHPAD = "touchpad" const val INTENT_TUTORIAL_TYPE_KEYBOARD = "keyboard" const val INTENT_TUTORIAL_TYPE_BOTH = "both" + const val INTENT_TUTORIAL_ENTRY_POINT_KEY = "entry_point" + const val INTENT_TUTORIAL_ENTRY_POINT_SCHEDULER = "scheduler" + const val INTENT_TUTORIAL_ENTRY_POINT_CONTEXTUAL_EDU = "contextual_edu" } private val vm by @@ -86,6 +91,10 @@ constructor( PlatformTheme { KeyboardTouchpadTutorialContainer(vm, touchpadTutorialScreensProvider) } } if (savedInstanceState == null) { + metricsLogger.logPeripheralTutorialLaunched( + intent.getStringExtra(INTENT_TUTORIAL_ENTRY_POINT_KEY), + intent.getStringExtra(INTENT_TUTORIAL_TYPE_KEY), + ) logger.logOpenTutorial(TutorialContext.KEYBOARD_TOUCHPAD_TUTORIAL) } } @@ -109,7 +118,7 @@ fun KeyboardTouchpadTutorialContainer( ACTION_KEY -> ActionKeyTutorialScreen( onDoneButtonClicked = vm::onDoneButtonClicked, - onBack = vm::onBack + onBack = vm::onBack, ) } } diff --git a/packages/SystemUI/src/com/android/systemui/keyboard/shortcut/ui/view/ShortcutHelperActivity.kt b/packages/SystemUI/src/com/android/systemui/keyboard/shortcut/ui/view/ShortcutHelperActivity.kt index b9a16c402e59..52263ce64a85 100644 --- a/packages/SystemUI/src/com/android/systemui/keyboard/shortcut/ui/view/ShortcutHelperActivity.kt +++ b/packages/SystemUI/src/com/android/systemui/keyboard/shortcut/ui/view/ShortcutHelperActivity.kt @@ -18,6 +18,7 @@ package com.android.systemui.keyboard.shortcut.ui.view import android.content.ActivityNotFoundException import android.content.Intent +import android.content.Intent.FLAG_ACTIVITY_NEW_TASK import android.content.res.Configuration import android.os.Bundle import android.provider.Settings @@ -125,7 +126,7 @@ constructor(private val userTracker: UserTracker, private val viewModel: Shortcu private fun onKeyboardSettingsClicked() { try { startActivityAsUser( - Intent(Settings.ACTION_HARD_KEYBOARD_SETTINGS), + Intent(Settings.ACTION_HARD_KEYBOARD_SETTINGS).addFlags(FLAG_ACTIVITY_NEW_TASK), userTracker.userHandle, ) } catch (e: ActivityNotFoundException) { diff --git a/packages/SystemUI/src/com/android/systemui/keyguard/domain/interactor/KeyguardDismissActionInteractor.kt b/packages/SystemUI/src/com/android/systemui/keyguard/domain/interactor/KeyguardDismissActionInteractor.kt index ea80911335fa..18b14951ee70 100644 --- a/packages/SystemUI/src/com/android/systemui/keyguard/domain/interactor/KeyguardDismissActionInteractor.kt +++ b/packages/SystemUI/src/com/android/systemui/keyguard/domain/interactor/KeyguardDismissActionInteractor.kt @@ -28,7 +28,6 @@ import com.android.systemui.keyguard.shared.model.KeyguardDone import com.android.systemui.keyguard.shared.model.KeyguardState.GONE import com.android.systemui.keyguard.shared.model.KeyguardState.PRIMARY_BOUNCER import com.android.systemui.power.domain.interactor.PowerInteractor -import com.android.systemui.scene.domain.interactor.SceneInteractor import com.android.systemui.scene.shared.flag.SceneContainerFlag import com.android.systemui.scene.shared.model.Scenes import com.android.systemui.shade.domain.interactor.ShadeInteractor @@ -60,7 +59,6 @@ constructor( transitionInteractor: KeyguardTransitionInteractor, val dismissInteractor: KeyguardDismissInteractor, @Application private val applicationScope: CoroutineScope, - sceneInteractor: Lazy<SceneInteractor>, deviceUnlockedInteractor: Lazy<DeviceUnlockedInteractor>, powerInteractor: PowerInteractor, alternateBouncerInteractor: AlternateBouncerInteractor, @@ -102,20 +100,20 @@ constructor( private val isOnShadeWhileUnlocked: Flow<Boolean> = if (SceneContainerFlag.isEnabled) { combine( - sceneInteractor.get().currentScene, + shadeInteractor.get().isAnyExpanded, deviceUnlockedInteractor.get().deviceUnlockStatus, - ) { scene, unlockStatus -> - unlockStatus.isUnlocked && - (scene == Scenes.QuickSettings || scene == Scenes.Shade) + ) { isAnyExpanded, unlockStatus -> + isAnyExpanded && unlockStatus.isUnlocked } .distinctUntilChanged() } else if (ComposeBouncerFlags.isOnlyComposeBouncerEnabled()) { combine( - shadeInteractor.get().isAnyExpanded, - keyguardInteractor.get().isKeyguardDismissible, - ) { isAnyExpanded, keyguardDismissible -> - isAnyExpanded && keyguardDismissible - } + shadeInteractor.get().isAnyExpanded, + keyguardInteractor.get().isKeyguardDismissible, + ) { isAnyExpanded, keyguardDismissible -> + isAnyExpanded && keyguardDismissible + } + .distinctUntilChanged() } else { flow { error( diff --git a/packages/SystemUI/src/com/android/systemui/media/controls/domain/pipeline/MediaTimeoutListener.kt b/packages/SystemUI/src/com/android/systemui/media/controls/domain/pipeline/MediaTimeoutListener.kt index 275f1eecd4db..39cedc36dbec 100644 --- a/packages/SystemUI/src/com/android/systemui/media/controls/domain/pipeline/MediaTimeoutListener.kt +++ b/packages/SystemUI/src/com/android/systemui/media/controls/domain/pipeline/MediaTimeoutListener.kt @@ -128,7 +128,7 @@ constructor( data: MediaData, immediately: Boolean, receivedSmartspaceCardLatency: Int, - isSsReactivated: Boolean + isSsReactivated: Boolean, ) { var reusedListener: PlaybackStateListener? = null @@ -183,7 +183,7 @@ constructor( override fun onSmartspaceMediaDataLoaded( key: String, data: SmartspaceMediaData, - shouldPrioritize: Boolean + shouldPrioritize: Boolean, ) { if (!mediaFlags.isPersistentSsCardEnabled()) return @@ -259,7 +259,9 @@ constructor( } override fun onPlaybackStateChanged(state: PlaybackState?) { - processState(state, dispatchEvents = true, currentResumption = resumption) + bgExecutor.execute { + processState(state, dispatchEvents = true, currentResumption = resumption) + } } override fun onSessionDestroyed() { @@ -276,6 +278,7 @@ constructor( } } + @WorkerThread private fun processState( state: PlaybackState?, dispatchEvents: Boolean, diff --git a/packages/SystemUI/src/com/android/systemui/mediaprojection/appselector/MediaProjectionAppSelectorActivity.kt b/packages/SystemUI/src/com/android/systemui/mediaprojection/appselector/MediaProjectionAppSelectorActivity.kt index 228b57603bed..d413474fde90 100644 --- a/packages/SystemUI/src/com/android/systemui/mediaprojection/appselector/MediaProjectionAppSelectorActivity.kt +++ b/packages/SystemUI/src/com/android/systemui/mediaprojection/appselector/MediaProjectionAppSelectorActivity.kt @@ -54,6 +54,7 @@ import com.android.systemui.mediaprojection.MediaProjectionServiceHelper import com.android.systemui.mediaprojection.appselector.data.RecentTask import com.android.systemui.mediaprojection.appselector.view.MediaProjectionRecentsViewController import com.android.systemui.res.R +import com.android.systemui.shared.system.ActivityManagerWrapper import com.android.systemui.statusbar.policy.ConfigurationController import com.android.systemui.util.AsyncActivityLauncher import java.lang.IllegalArgumentException @@ -62,9 +63,10 @@ import javax.inject.Inject class MediaProjectionAppSelectorActivity( private val componentFactory: MediaProjectionAppSelectorComponent.Factory, private val activityLauncher: AsyncActivityLauncher, + private val activityManager: ActivityManagerWrapper, /** This is used to override the dependency in a screenshot test */ @VisibleForTesting - private val listControllerFactory: ((userHandle: UserHandle) -> ResolverListController)? + private val listControllerFactory: ((userHandle: UserHandle) -> ResolverListController)?, ) : ChooserActivity(), MediaProjectionAppSelectorView, @@ -74,8 +76,9 @@ class MediaProjectionAppSelectorActivity( @Inject constructor( componentFactory: MediaProjectionAppSelectorComponent.Factory, - activityLauncher: AsyncActivityLauncher - ) : this(componentFactory, activityLauncher, listControllerFactory = null) + activityLauncher: AsyncActivityLauncher, + activityManager: ActivityManagerWrapper, + ) : this(componentFactory, activityLauncher, activityManager, listControllerFactory = null) private val lifecycleRegistry = LifecycleRegistry(this) override val lifecycle = lifecycleRegistry @@ -100,7 +103,7 @@ class MediaProjectionAppSelectorActivity( callingPackage = callingPackage, view = this, resultHandler = this, - isFirstStart = savedInstanceState == null + isFirstStart = savedInstanceState == null, ) component.lifecycleObservers.forEach { lifecycle.addObserver(it) } @@ -113,7 +116,7 @@ class MediaProjectionAppSelectorActivity( intent.configureChooserIntent( resources, component.hostUserHandle, - component.personalProfileUserHandle + component.personalProfileUserHandle, ) reviewGrantedConsentRequired = @@ -180,7 +183,13 @@ class MediaProjectionAppSelectorActivity( // is created and ready to be captured. val activityStarted = activityLauncher.startActivityAsUser(intent, userHandle, activityOptions.toBundle()) { - returnSelectedApp(launchCookie, taskId = -1) + if (targetInfo.resolvedComponentName == callingActivity) { + // If attempting to launch the app used to launch the MediaProjection, then + // provide the task id since the launch cookie won't match the existing task + returnSelectedApp(launchCookie, taskId = activityManager.runningTask.taskId) + } else { + returnSelectedApp(launchCookie, taskId = -1) + } } // Rely on the ActivityManager to pop up a dialog regarding app suspension @@ -213,7 +222,7 @@ class MediaProjectionAppSelectorActivity( MediaProjectionServiceHelper.setReviewedConsentIfNeeded( RECORD_CANCEL, reviewGrantedConsentRequired, - /* projection= */ null + /* projection= */ null, ) if (isFinishing) { // Only log dismissed when actually finishing, and not when changing configuration. @@ -246,7 +255,7 @@ class MediaProjectionAppSelectorActivity( val resultReceiver = intent.getParcelableExtra( EXTRA_CAPTURE_REGION_RESULT_RECEIVER, - ResultReceiver::class.java + ResultReceiver::class.java, ) as ResultReceiver val captureRegion = MediaProjectionCaptureTarget(launchCookie, taskId) val data = Bundle().apply { putParcelable(KEY_CAPTURE_TARGET, captureRegion) } @@ -260,8 +269,8 @@ class MediaProjectionAppSelectorActivity( val mediaProjectionBinder = intent.getIBinderExtra(EXTRA_MEDIA_PROJECTION) val projection = IMediaProjection.Stub.asInterface(mediaProjectionBinder) - projection.setLaunchCookie(launchCookie) - projection.setTaskId(taskId) + projection.launchCookie = launchCookie + projection.taskId = taskId val intent = Intent() intent.putExtra(EXTRA_MEDIA_PROJECTION, projection.asBinder()) @@ -270,7 +279,7 @@ class MediaProjectionAppSelectorActivity( MediaProjectionServiceHelper.setReviewedConsentIfNeeded( RECORD_CONTENT_TASK, reviewGrantedConsentRequired, - projection + projection, ) } @@ -457,7 +466,7 @@ class MediaProjectionAppSelectorActivity( */ private class RecyclerViewExpandingAccessibilityDelegate( rdl: ResolverDrawerLayout, - view: RecyclerView + view: RecyclerView, ) : RecyclerViewAccessibilityDelegate(view) { private val delegate = AppListAccessibilityDelegate(rdl) @@ -465,7 +474,7 @@ class MediaProjectionAppSelectorActivity( override fun onRequestSendAccessibilityEvent( host: ViewGroup, child: View, - event: AccessibilityEvent + event: AccessibilityEvent, ): Boolean { super.onRequestSendAccessibilityEvent(host, child, event) return delegate.onRequestSendAccessibilityEvent(host, child, event) diff --git a/packages/SystemUI/src/com/android/systemui/mediaprojection/permission/MediaProjectionPermissionActivity.java b/packages/SystemUI/src/com/android/systemui/mediaprojection/permission/MediaProjectionPermissionActivity.java index 8351597f35de..c3729c0dcdfd 100644 --- a/packages/SystemUI/src/com/android/systemui/mediaprojection/permission/MediaProjectionPermissionActivity.java +++ b/packages/SystemUI/src/com/android/systemui/mediaprojection/permission/MediaProjectionPermissionActivity.java @@ -68,12 +68,12 @@ import com.android.systemui.res.R; import com.android.systemui.statusbar.phone.AlertDialogWithDelegate; import com.android.systemui.statusbar.phone.SystemUIDialog; +import dagger.Lazy; + import java.util.function.Consumer; import javax.inject.Inject; -import dagger.Lazy; - public class MediaProjectionPermissionActivity extends Activity { private static final String TAG = "MediaProjectionPermissionActivity"; private static final float MAX_APP_NAME_SIZE_PX = 500f; @@ -132,8 +132,7 @@ public class MediaProjectionPermissionActivity extends Activity { mPackageName = launchingIntent.getStringExtra( EXTRA_PACKAGE_REUSING_GRANTED_CONSENT); } else { - setResult(RESULT_CANCELED); - finish(RECORD_CANCEL, /* projection= */ null); + finishAsCancelled(); return; } } @@ -145,8 +144,7 @@ public class MediaProjectionPermissionActivity extends Activity { mUid = aInfo.uid; } catch (PackageManager.NameNotFoundException e) { Log.e(TAG, "Unable to look up package name", e); - setResult(RESULT_CANCELED); - finish(RECORD_CANCEL, /* projection= */ null); + finishAsCancelled(); return; } @@ -176,15 +174,13 @@ public class MediaProjectionPermissionActivity extends Activity { } } catch (RemoteException e) { Log.e(TAG, "Error checking projection permissions", e); - setResult(RESULT_CANCELED); - finish(RECORD_CANCEL, /* projection= */ null); + finishAsCancelled(); return; } if (mFeatureFlags.isEnabled(Flags.WM_ENABLE_PARTIAL_SCREEN_SHARING_ENTERPRISE_POLICIES)) { if (showScreenCaptureDisabledDialogIfNeeded()) { - setResult(RESULT_CANCELED); - finish(RECORD_CANCEL, /* projection= */ null); + finishAsCancelled(); return; } } @@ -346,6 +342,21 @@ public class MediaProjectionPermissionActivity extends Activity { private void requestDeviceUnlock() { mKeyguardManager.requestDismissKeyguard(this, new KeyguardManager.KeyguardDismissCallback() { + + @Override + public void onDismissError() { + if (com.android.systemui.Flags.mediaProjectionDialogBehindLockscreen()) { + finishAsCancelled(); + } + } + + @Override + public void onDismissCancelled() { + if (com.android.systemui.Flags.mediaProjectionDialogBehindLockscreen()) { + finishAsCancelled(); + } + } + @Override public void onDismissSucceeded() { mDialog.show(); @@ -386,8 +397,7 @@ public class MediaProjectionPermissionActivity extends Activity { } } catch (RemoteException e) { Log.e(TAG, "Error granting projection permission", e); - setResult(RESULT_CANCELED); - finish(RECORD_CANCEL, /* projection= */ null); + finishAsCancelled(); } finally { if (mDialog != null) { mDialog.dismiss(); @@ -436,6 +446,14 @@ public class MediaProjectionPermissionActivity extends Activity { } } + /** + * Finishes this activity and cancel the projection request. + */ + private void finishAsCancelled() { + setResult(RESULT_CANCELED); + finish(RECORD_CANCEL, /* projection= */ null); + } + @Nullable private MediaProjectionConfig getMediaProjectionConfig() { Intent intent = getIntent(); diff --git a/packages/SystemUI/src/com/android/systemui/notifications/ui/viewmodel/NotificationsShadeOverlayContentViewModel.kt b/packages/SystemUI/src/com/android/systemui/notifications/ui/viewmodel/NotificationsShadeOverlayContentViewModel.kt index 219e45c36b50..0e5404164ba1 100644 --- a/packages/SystemUI/src/com/android/systemui/notifications/ui/viewmodel/NotificationsShadeOverlayContentViewModel.kt +++ b/packages/SystemUI/src/com/android/systemui/notifications/ui/viewmodel/NotificationsShadeOverlayContentViewModel.kt @@ -16,11 +16,19 @@ package com.android.systemui.notifications.ui.viewmodel +import com.android.systemui.lifecycle.ExclusiveActivatable +import com.android.systemui.scene.domain.interactor.SceneInteractor +import com.android.systemui.scene.shared.model.Scenes import com.android.systemui.shade.domain.interactor.ShadeInteractor import com.android.systemui.shade.ui.viewmodel.ShadeHeaderViewModel import com.android.systemui.statusbar.notification.stack.ui.viewmodel.NotificationsPlaceholderViewModel import dagger.assisted.AssistedFactory import dagger.assisted.AssistedInject +import kotlinx.coroutines.awaitCancellation +import kotlinx.coroutines.coroutineScope +import kotlinx.coroutines.flow.distinctUntilChanged +import kotlinx.coroutines.flow.filter +import kotlinx.coroutines.launch /** * Models UI state used to render the content of the notifications shade overlay. @@ -33,10 +41,40 @@ class NotificationsShadeOverlayContentViewModel constructor( val shadeHeaderViewModelFactory: ShadeHeaderViewModel.Factory, val notificationsPlaceholderViewModelFactory: NotificationsPlaceholderViewModel.Factory, + val sceneInteractor: SceneInteractor, private val shadeInteractor: ShadeInteractor, -) { +) : ExclusiveActivatable() { + + override suspend fun onActivated(): Nothing { + coroutineScope { + launch { + sceneInteractor.currentScene.collect { currentScene -> + when (currentScene) { + // TODO(b/369513770): The ShadeSession should be preserved in this scenario. + Scenes.Bouncer -> + shadeInteractor.collapseNotificationsShade( + loggingReason = "bouncer shown while shade is open" + ) + } + } + } + + launch { + shadeInteractor.isShadeTouchable + .distinctUntilChanged() + .filter { !it } + .collect { + shadeInteractor.collapseNotificationsShade( + loggingReason = "device became non-interactive" + ) + } + } + } + awaitCancellation() + } + fun onScrimClicked() { - shadeInteractor.collapseNotificationsShade(loggingReason = "Shade scrim clicked") + shadeInteractor.collapseNotificationsShade(loggingReason = "shade scrim clicked") } @AssistedFactory diff --git a/packages/SystemUI/src/com/android/systemui/qs/logging/QSLogger.kt b/packages/SystemUI/src/com/android/systemui/qs/logging/QSLogger.kt index 278352c6f69b..ead38f3f9b52 100644 --- a/packages/SystemUI/src/com/android/systemui/qs/logging/QSLogger.kt +++ b/packages/SystemUI/src/com/android/systemui/qs/logging/QSLogger.kt @@ -33,6 +33,7 @@ import com.android.systemui.log.core.LogLevel.VERBOSE import com.android.systemui.log.dagger.QSConfigLog import com.android.systemui.log.dagger.QSLog import com.android.systemui.plugins.qs.QSTile +import com.android.systemui.plugins.qs.QSTile.State import com.android.systemui.statusbar.StatusBarState import com.google.errorprone.annotations.CompileTimeConstant import javax.inject.Inject @@ -57,6 +58,7 @@ constructor( fun d(@CompileTimeConstant msg: String, arg: Any) { buffer.log(TAG, DEBUG, { str1 = arg.toString() }, { "$msg: $str1" }) } + fun i(@CompileTimeConstant msg: String, arg: Any) { buffer.log(TAG, INFO, { str1 = arg.toString() }, { "$msg: $str1" }) } @@ -73,7 +75,19 @@ constructor( str1 = tileSpec str2 = reason }, - { "[$str1] Tile destroyed. Reason: $str2" } + { "[$str1] Tile destroyed. Reason: $str2" }, + ) + } + + fun logStateChanged(tileSpec: String, state: State) { + buffer.log( + TAG, + DEBUG, + { + str1 = tileSpec + str2 = state.toString() + }, + { "[$str1] Tile state=$str2" }, ) } @@ -85,7 +99,7 @@ constructor( bool1 = listening str1 = tileSpec }, - { "[$str1] Tile listening=$bool1" } + { "[$str1] Tile listening=$bool1" }, ) } @@ -98,7 +112,7 @@ constructor( str1 = containerName str2 = allSpecs }, - { "Tiles listening=$bool1 in $str1. $str2" } + { "Tiles listening=$bool1 in $str1. $str2" }, ) } @@ -112,7 +126,7 @@ constructor( str2 = StatusBarState.toString(statusBarState) str3 = toStateString(state) }, - { "[$str1][$int1] Tile clicked. StatusBarState=$str2. TileState=$str3" } + { "[$str1][$int1] Tile clicked. StatusBarState=$str2. TileState=$str3" }, ) } @@ -124,7 +138,7 @@ constructor( str1 = tileSpec int1 = eventId }, - { "[$str1][$int1] Tile handling click." } + { "[$str1][$int1] Tile handling click." }, ) } @@ -138,7 +152,7 @@ constructor( str2 = StatusBarState.toString(statusBarState) str3 = toStateString(state) }, - { "[$str1][$int1] Tile secondary clicked. StatusBarState=$str2. TileState=$str3" } + { "[$str1][$int1] Tile secondary clicked. StatusBarState=$str2. TileState=$str3" }, ) } @@ -150,7 +164,7 @@ constructor( str1 = tileSpec int1 = eventId }, - { "[$str1][$int1] Tile handling secondary click." } + { "[$str1][$int1] Tile handling secondary click." }, ) } @@ -164,7 +178,7 @@ constructor( str2 = StatusBarState.toString(statusBarState) str3 = toStateString(state) }, - { "[$str1][$int1] Tile long clicked. StatusBarState=$str2. TileState=$str3" } + { "[$str1][$int1] Tile long clicked. StatusBarState=$str2. TileState=$str3" }, ) } @@ -176,7 +190,7 @@ constructor( str1 = tileSpec int1 = eventId }, - { "[$str1][$int1] Tile handling long click." } + { "[$str1][$int1] Tile handling long click." }, ) } @@ -189,7 +203,7 @@ constructor( int1 = lastType str2 = callback }, - { "[$str1] mLastTileState=$int1, Callback=$str2." } + { "[$str1] mLastTileState=$int1, Callback=$str2." }, ) } @@ -198,7 +212,7 @@ constructor( tileSpec: String, state: Int, disabledByPolicy: Boolean, - color: Int + color: Int, ) { // This method is added to further debug b/250618218 which has only been observed from the // InternetTile, so we are only logging the background color change for the InternetTile @@ -215,7 +229,7 @@ constructor( bool1 = disabledByPolicy int2 = color }, - { "[$str1] state=$int1, disabledByPolicy=$bool1, color=$int2." } + { "[$str1] state=$int1, disabledByPolicy=$bool1, color=$int2." }, ) } @@ -229,7 +243,7 @@ constructor( str3 = state.icon?.toString() int1 = state.state }, - { "[$str1] Tile updated. Label=$str2. State=$int1. Icon=$str3." } + { "[$str1] Tile updated. Label=$str2. State=$int1. Icon=$str3." }, ) } @@ -241,7 +255,7 @@ constructor( str1 = containerName bool1 = expanded }, - { "$str1 expanded=$bool1" } + { "$str1 expanded=$bool1" }, ) } @@ -253,7 +267,7 @@ constructor( str1 = containerName int1 = orientation }, - { "onViewAttached: $str1 orientation $int1" } + { "onViewAttached: $str1 orientation $int1" }, ) } @@ -265,7 +279,7 @@ constructor( str1 = containerName int1 = orientation }, - { "onViewDetached: $str1 orientation $int1" } + { "onViewDetached: $str1 orientation $int1" }, ) } @@ -276,7 +290,7 @@ constructor( newShouldUseSplitShade: Boolean, oldScreenLayout: Int, newScreenLayout: Int, - containerName: String + containerName: String, ) { configChangedBuffer.log( TAG, @@ -297,7 +311,7 @@ constructor( "screen layout=${toScreenLayoutString(long1.toInt())} " + "(was ${toScreenLayoutString(long2.toInt())}), " + "splitShade=$bool2 (was $bool1)" - } + }, ) } @@ -305,7 +319,7 @@ constructor( after: Boolean, before: Boolean, force: Boolean, - containerName: String + containerName: String, ) { buffer.log( TAG, @@ -316,7 +330,7 @@ constructor( bool2 = before bool3 = force }, - { "change tile layout: $str1 horizontal=$bool1 (was $bool2), force? $bool3" } + { "change tile layout: $str1 horizontal=$bool1 (was $bool2), force? $bool3" }, ) } @@ -328,7 +342,7 @@ constructor( int1 = tilesPerPageCount int2 = totalTilesCount }, - { "Distributing tiles: [tilesPerPageCount=$int1] [totalTilesCount=$int2]" } + { "Distributing tiles: [tilesPerPageCount=$int1] [totalTilesCount=$int2]" }, ) } @@ -340,7 +354,7 @@ constructor( str1 = tileName int1 = pageIndex }, - { "Adding $str1 to page number $int1" } + { "Adding $str1 to page number $int1" }, ) } @@ -361,7 +375,7 @@ constructor( str1 = viewName str2 = toVisibilityString(visibility) }, - { "$str1 visibility: $str2" } + { "$str1 visibility: $str2" }, ) } diff --git a/packages/SystemUI/src/com/android/systemui/qs/tileimpl/QSIconViewImpl.java b/packages/SystemUI/src/com/android/systemui/qs/tileimpl/QSIconViewImpl.java index 5ea8c2183295..a4f3c7aa2652 100644 --- a/packages/SystemUI/src/com/android/systemui/qs/tileimpl/QSIconViewImpl.java +++ b/packages/SystemUI/src/com/android/systemui/qs/tileimpl/QSIconViewImpl.java @@ -14,6 +14,7 @@ package com.android.systemui.qs.tileimpl; +import static com.android.systemui.Flags.qsNewTiles; import static com.android.systemui.Flags.removeUpdateListenerInQsIconViewImpl; import android.animation.Animator; @@ -66,12 +67,22 @@ public class QSIconViewImpl extends QSIconView { private ValueAnimator mColorAnimator = new ValueAnimator(); + private int mColorUnavailable; + private int mColorInactive; + private int mColorActive; + public QSIconViewImpl(Context context) { super(context); final Resources res = context.getResources(); mIconSizePx = res.getDimensionPixelSize(R.dimen.qs_icon_size); + if (qsNewTiles()) { // pre-load icon tint colors + mColorUnavailable = Utils.getColorAttrDefaultColor(context, R.attr.outline); + mColorInactive = Utils.getColorAttrDefaultColor(context, R.attr.onShadeInactiveVariant); + mColorActive = Utils.getColorAttrDefaultColor(context, R.attr.onShadeActive); + } + mIcon = createIcon(); addView(mIcon); mColorAnimator.setDuration(QS_ANIM_LENGTH); @@ -195,7 +206,11 @@ public class QSIconViewImpl extends QSIconView { } protected int getColor(QSTile.State state) { - return getIconColorForState(getContext(), state); + if (qsNewTiles()) { + return getCachedIconColorForState(state); + } else { + return getIconColorForState(getContext(), state); + } } private void animateGrayScale(int fromColor, int toColor, ImageView iv, @@ -267,6 +282,19 @@ public class QSIconViewImpl extends QSIconView { } } + private int getCachedIconColorForState(QSTile.State state) { + if (state.disabledByPolicy || state.state == Tile.STATE_UNAVAILABLE) { + return mColorUnavailable; + } else if (state.state == Tile.STATE_INACTIVE) { + return mColorInactive; + } else if (state.state == Tile.STATE_ACTIVE) { + return mColorActive; + } else { + Log.e("QSIconView", "Invalid state " + state); + return 0; + } + } + private static class EndRunnableAnimatorListener extends AnimatorListenerAdapter { private Runnable mRunnable; diff --git a/packages/SystemUI/src/com/android/systemui/qs/tileimpl/QSTileViewImpl.kt b/packages/SystemUI/src/com/android/systemui/qs/tileimpl/QSTileViewImpl.kt index 4f3ea8331a17..18b1f071f44e 100644 --- a/packages/SystemUI/src/com/android/systemui/qs/tileimpl/QSTileViewImpl.kt +++ b/packages/SystemUI/src/com/android/systemui/qs/tileimpl/QSTileViewImpl.kt @@ -643,7 +643,6 @@ constructor( } // HANDLE STATE CHANGES RELATED METHODS - protected open fun handleStateChanged(state: QSTile.State) { val allowAnimations = animationsEnabled() isClickable = state.state != Tile.STATE_UNAVAILABLE diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/InternetDialogController.java b/packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/InternetDialogController.java index 5ea9e6ae0a70..301ab2bcdd65 100644 --- a/packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/InternetDialogController.java +++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/dialog/InternetDialogController.java @@ -311,10 +311,7 @@ public class InternetDialogController implements AccessPointController.AccessPoi mConfig = MobileMappings.Config.readConfig(mContext); mTelephonyManager = mTelephonyManager.createForSubscriptionId(mDefaultDataSubId); mSubIdTelephonyManagerMap.put(mDefaultDataSubId, mTelephonyManager); - InternetTelephonyCallback telephonyCallback = - new InternetTelephonyCallback(mDefaultDataSubId); - mSubIdTelephonyCallbackMap.put(mDefaultDataSubId, telephonyCallback); - mTelephonyManager.registerTelephonyCallback(mExecutor, telephonyCallback); + registerInternetTelephonyCallback(mTelephonyManager, mDefaultDataSubId); // Listen the connectivity changes mConnectivityManager.registerDefaultNetworkCallback(mConnectivityManagerNetworkCallback); mCanConfigWifi = canConfigWifi; @@ -346,6 +343,23 @@ public class InternetDialogController implements AccessPointController.AccessPoi mCallback = null; } + /** + * This is to generate and register the new callback to Telephony for uncached subscription id, + * then cache it. Telephony also cached this callback into + * {@link com.android.server.TelephonyRegistry}, so if subscription id and callback were cached + * already, it shall do nothing to avoid registering redundant callback to Telephony. + */ + private void registerInternetTelephonyCallback( + TelephonyManager telephonyManager, int subId) { + if (mSubIdTelephonyCallbackMap.containsKey(subId)) { + // Avoid to generate and register unnecessary callback to Telephony. + return; + } + InternetTelephonyCallback telephonyCallback = new InternetTelephonyCallback(subId); + mSubIdTelephonyCallbackMap.put(subId, telephonyCallback); + telephonyManager.registerTelephonyCallback(mExecutor, telephonyCallback); + } + boolean isAirplaneModeEnabled() { return mGlobalSettings.getInt(Settings.Global.AIRPLANE_MODE_ON, 0) != 0; } @@ -673,9 +687,7 @@ public class InternetDialogController implements AccessPointController.AccessPoi int subId = subInfo.getSubscriptionId(); if (mSubIdTelephonyManagerMap.get(subId) == null) { TelephonyManager secondaryTm = mTelephonyManager.createForSubscriptionId(subId); - InternetTelephonyCallback telephonyCallback = new InternetTelephonyCallback(subId); - secondaryTm.registerTelephonyCallback(mExecutor, telephonyCallback); - mSubIdTelephonyCallbackMap.put(subId, telephonyCallback); + registerInternetTelephonyCallback(secondaryTm, subId); mSubIdTelephonyManagerMap.put(subId, secondaryTm); } return subId; @@ -1351,6 +1363,7 @@ public class InternetDialogController implements AccessPointController.AccessPoi if (DEBUG) { Log.d(TAG, "DDS: defaultDataSubId:" + defaultDataSubId); } + if (SubscriptionManager.isUsableSubscriptionId(defaultDataSubId)) { // clean up old defaultDataSubId TelephonyCallback oldCallback = mSubIdTelephonyCallbackMap.get(mDefaultDataSubId); @@ -1366,9 +1379,7 @@ public class InternetDialogController implements AccessPointController.AccessPoi // create for new defaultDataSubId mTelephonyManager = mTelephonyManager.createForSubscriptionId(defaultDataSubId); mSubIdTelephonyManagerMap.put(defaultDataSubId, mTelephonyManager); - InternetTelephonyCallback newCallback = new InternetTelephonyCallback(defaultDataSubId); - mSubIdTelephonyCallbackMap.put(defaultDataSubId, newCallback); - mTelephonyManager.registerTelephonyCallback(mHandler::post, newCallback); + registerInternetTelephonyCallback(mTelephonyManager, defaultDataSubId); mCallback.onSubscriptionsChanged(defaultDataSubId); } mDefaultDataSubId = defaultDataSubId; diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/impl/internet/domain/InternetTileMapper.kt b/packages/SystemUI/src/com/android/systemui/qs/tiles/impl/internet/domain/InternetTileMapper.kt index 8965ef2bc493..bb0b9b7084fa 100644 --- a/packages/SystemUI/src/com/android/systemui/qs/tiles/impl/internet/domain/InternetTileMapper.kt +++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/impl/internet/domain/InternetTileMapper.kt @@ -18,7 +18,9 @@ package com.android.systemui.qs.tiles.impl.internet.domain import android.content.Context import android.content.res.Resources +import android.os.Handler import android.widget.Switch +import com.android.settingslib.graph.SignalDrawable import com.android.systemui.common.shared.model.ContentDescription.Companion.loadContentDescription import com.android.systemui.common.shared.model.Icon import com.android.systemui.common.shared.model.Text.Companion.loadText @@ -28,6 +30,7 @@ import com.android.systemui.qs.tiles.impl.internet.domain.model.InternetTileMode import com.android.systemui.qs.tiles.viewmodel.QSTileConfig import com.android.systemui.qs.tiles.viewmodel.QSTileState import com.android.systemui.res.R +import com.android.systemui.statusbar.pipeline.shared.ui.model.InternetTileIconModel import javax.inject.Inject /** Maps [InternetTileModel] to [QSTileState]. */ @@ -37,6 +40,7 @@ constructor( @Main private val resources: Resources, private val theme: Resources.Theme, private val context: Context, + @Main private val handler: Handler, ) : QSTileDataToStateMapper<InternetTileModel> { override fun map(config: QSTileConfig, data: InternetTileModel): QSTileState = @@ -44,25 +48,42 @@ constructor( label = resources.getString(R.string.quick_settings_internet_label) expandedAccessibilityClass = Switch::class - if (data.secondaryLabel != null) { - secondaryLabel = data.secondaryLabel.loadText(context) - } else { - secondaryLabel = data.secondaryTitle - } + secondaryLabel = + if (data.secondaryLabel != null) { + data.secondaryLabel.loadText(context) + } else { + data.secondaryTitle + } stateDescription = data.stateDescription.loadContentDescription(context) contentDescription = data.contentDescription.loadContentDescription(context) - iconRes = data.iconId - if (data.icon != null) { - this.icon = { data.icon } - } else if (data.iconId != null) { - val loadedIcon = - Icon.Loaded( - resources.getDrawable(data.iconId!!, theme), - contentDescription = null - ) - this.icon = { loadedIcon } + when (val dataIcon = data.icon) { + is InternetTileIconModel.ResourceId -> { + iconRes = dataIcon.resId + icon = { + Icon.Loaded( + resources.getDrawable(dataIcon.resId, theme), + contentDescription = null, + ) + } + } + + is InternetTileIconModel.Cellular -> { + val signalDrawable = SignalDrawable(context, handler) + signalDrawable.setLevel(dataIcon.level) + icon = { Icon.Loaded(signalDrawable, contentDescription = null) } + } + + is InternetTileIconModel.Satellite -> { + iconRes = dataIcon.resourceIcon.res // level is inferred from res + icon = { + Icon.Loaded( + resources.getDrawable(dataIcon.resourceIcon.res, theme), + contentDescription = null, + ) + } + } } sideViewIcon = QSTileState.SideViewIcon.Chevron @@ -75,7 +96,7 @@ constructor( setOf( QSTileState.UserAction.CLICK, QSTileState.UserAction.TOGGLE_CLICK, - QSTileState.UserAction.LONG_CLICK + QSTileState.UserAction.LONG_CLICK, ) } } diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/impl/internet/domain/interactor/InternetTileDataInteractor.kt b/packages/SystemUI/src/com/android/systemui/qs/tiles/impl/internet/domain/interactor/InternetTileDataInteractor.kt index 204ead3fe29c..6fe3979fa446 100644 --- a/packages/SystemUI/src/com/android/systemui/qs/tiles/impl/internet/domain/interactor/InternetTileDataInteractor.kt +++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/impl/internet/domain/interactor/InternetTileDataInteractor.kt @@ -20,13 +20,10 @@ import android.annotation.StringRes import android.content.Context import android.os.UserHandle import android.text.Html -import com.android.settingslib.graph.SignalDrawable import com.android.systemui.common.shared.model.ContentDescription import com.android.systemui.common.shared.model.ContentDescription.Companion.loadContentDescription -import com.android.systemui.common.shared.model.Icon import com.android.systemui.common.shared.model.Text import com.android.systemui.dagger.qualifiers.Application -import com.android.systemui.dagger.qualifiers.Main import com.android.systemui.qs.tiles.base.interactor.DataUpdateTrigger import com.android.systemui.qs.tiles.base.interactor.QSTileDataInteractor import com.android.systemui.qs.tiles.impl.internet.domain.model.InternetTileModel @@ -36,12 +33,12 @@ import com.android.systemui.statusbar.pipeline.ethernet.domain.EthernetInteracto import com.android.systemui.statusbar.pipeline.mobile.domain.interactor.MobileIconsInteractor import com.android.systemui.statusbar.pipeline.mobile.domain.model.SignalIconModel import com.android.systemui.statusbar.pipeline.shared.data.repository.ConnectivityRepository +import com.android.systemui.statusbar.pipeline.shared.ui.model.InternetTileIconModel import com.android.systemui.statusbar.pipeline.wifi.domain.interactor.WifiInteractor import com.android.systemui.statusbar.pipeline.wifi.shared.model.WifiNetworkModel import com.android.systemui.statusbar.pipeline.wifi.ui.model.WifiIcon import com.android.systemui.utils.coroutines.flow.mapLatestConflated import javax.inject.Inject -import kotlin.coroutines.CoroutineContext import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.Flow @@ -51,7 +48,6 @@ import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.flatMapLatest import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.flow.stateIn -import kotlinx.coroutines.withContext @OptIn(ExperimentalCoroutinesApi::class) /** Observes internet state changes providing the [InternetTileModel]. */ @@ -59,7 +55,6 @@ class InternetTileDataInteractor @Inject constructor( private val context: Context, - @Main private val mainCoroutineContext: CoroutineContext, @Application private val scope: CoroutineScope, airplaneModeRepository: AirplaneModeRepository, private val connectivityRepository: ConnectivityRepository, @@ -79,8 +74,7 @@ constructor( flowOf( InternetTileModel.Active( secondaryTitle = secondary, - iconId = wifiIcon.icon.res, - icon = Icon.Loaded(context.getDrawable(wifiIcon.icon.res)!!, null), + icon = InternetTileIconModel.ResourceId(wifiIcon.icon.res), stateDescription = wifiIcon.contentDescription, contentDescription = ContentDescription.Loaded("$internetLabel,$secondary"), ) @@ -116,11 +110,10 @@ constructor( if (it == null) { notConnectedFlow } else { - combine( - it.networkName, - it.signalLevelIcon, - mobileDataContentName, - ) { networkNameModel, signalIcon, dataContentDescription -> + combine(it.networkName, it.signalLevelIcon, mobileDataContentName) { + networkNameModel, + signalIcon, + dataContentDescription -> Triple(networkNameModel, signalIcon, dataContentDescription) } .mapLatestConflated { (networkNameModel, signalIcon, dataContentDescription) -> @@ -129,17 +122,12 @@ constructor( val secondary = mobileDataContentConcat( networkNameModel.name, - dataContentDescription + dataContentDescription, ) - val drawable = - withContext(mainCoroutineContext) { SignalDrawable(context) } - drawable.setLevel(signalIcon.level) - val loadedIcon = Icon.Loaded(drawable, null) - InternetTileModel.Active( secondaryTitle = secondary, - icon = loadedIcon, + icon = InternetTileIconModel.Cellular(signalIcon.level), stateDescription = ContentDescription.Loaded(secondary.toString()), contentDescription = ContentDescription.Loaded(internetLabel), @@ -150,9 +138,10 @@ constructor( signalIcon.icon.contentDescription.loadContentDescription( context ) + InternetTileModel.Active( secondaryTitle = secondary, - iconId = signalIcon.icon.res, + icon = InternetTileIconModel.Satellite(signalIcon.icon), stateDescription = ContentDescription.Loaded(secondary), contentDescription = ContentDescription.Loaded(internetLabel), ) @@ -164,7 +153,7 @@ constructor( private fun mobileDataContentConcat( networkName: String?, - dataContentDescription: CharSequence? + dataContentDescription: CharSequence?, ): CharSequence { if (dataContentDescription == null) { return networkName ?: "" @@ -177,9 +166,9 @@ constructor( context.getString( R.string.mobile_carrier_text_format, networkName, - dataContentDescription + dataContentDescription, ), - 0 + 0, ) } @@ -199,7 +188,7 @@ constructor( flowOf( InternetTileModel.Active( secondaryLabel = secondary?.toText(), - iconId = it.res, + icon = InternetTileIconModel.ResourceId(it.res), stateDescription = null, contentDescription = secondary, ) @@ -208,16 +197,18 @@ constructor( } private val notConnectedFlow: StateFlow<InternetTileModel> = - combine( - wifiInteractor.areNetworksAvailable, - airplaneModeRepository.isAirplaneMode, - ) { networksAvailable, isAirplaneMode -> + combine(wifiInteractor.areNetworksAvailable, airplaneModeRepository.isAirplaneMode) { + networksAvailable, + isAirplaneMode -> when { isAirplaneMode -> { val secondary = context.getString(R.string.status_bar_airplane) InternetTileModel.Inactive( secondaryTitle = secondary, - iconId = R.drawable.ic_qs_no_internet_unavailable, + icon = + InternetTileIconModel.ResourceId( + R.drawable.ic_qs_no_internet_unavailable + ), stateDescription = null, contentDescription = ContentDescription.Loaded(secondary), ) @@ -227,10 +218,13 @@ constructor( context.getString(R.string.quick_settings_networks_available) InternetTileModel.Inactive( secondaryTitle = secondary, - iconId = R.drawable.ic_qs_no_internet_available, + icon = + InternetTileIconModel.ResourceId( + R.drawable.ic_qs_no_internet_available + ), stateDescription = null, contentDescription = - ContentDescription.Loaded("$internetLabel,$secondary") + ContentDescription.Loaded("$internetLabel,$secondary"), ) } else -> { @@ -248,7 +242,7 @@ constructor( */ override fun tileData( user: UserHandle, - triggers: Flow<DataUpdateTrigger> + triggers: Flow<DataUpdateTrigger>, ): Flow<InternetTileModel> = connectivityRepository.defaultConnections.flatMapLatest { when { @@ -265,7 +259,7 @@ constructor( val NOT_CONNECTED_NETWORKS_UNAVAILABLE = InternetTileModel.Inactive( secondaryLabel = Text.Resource(R.string.quick_settings_networks_unavailable), - iconId = R.drawable.ic_qs_no_internet_unavailable, + icon = InternetTileIconModel.ResourceId(R.drawable.ic_qs_no_internet_unavailable), stateDescription = null, contentDescription = ContentDescription.Resource(R.string.quick_settings_networks_unavailable), diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/impl/internet/domain/model/InternetTileModel.kt b/packages/SystemUI/src/com/android/systemui/qs/tiles/impl/internet/domain/model/InternetTileModel.kt index ece904611782..15b4e472eec7 100644 --- a/packages/SystemUI/src/com/android/systemui/qs/tiles/impl/internet/domain/model/InternetTileModel.kt +++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/impl/internet/domain/model/InternetTileModel.kt @@ -17,23 +17,21 @@ package com.android.systemui.qs.tiles.impl.internet.domain.model import com.android.systemui.common.shared.model.ContentDescription -import com.android.systemui.common.shared.model.Icon import com.android.systemui.common.shared.model.Text +import com.android.systemui.statusbar.pipeline.shared.ui.model.InternetTileIconModel /** Model describing the state that the QS Internet tile should be in. */ sealed interface InternetTileModel { val secondaryTitle: CharSequence? val secondaryLabel: Text? - val iconId: Int? - val icon: Icon? + val icon: InternetTileIconModel val stateDescription: ContentDescription? val contentDescription: ContentDescription? data class Active( override val secondaryTitle: CharSequence? = null, override val secondaryLabel: Text? = null, - override val iconId: Int? = null, - override val icon: Icon? = null, + override val icon: InternetTileIconModel = InternetTileIconModel.Cellular(1), override val stateDescription: ContentDescription? = null, override val contentDescription: ContentDescription? = null, ) : InternetTileModel @@ -41,8 +39,7 @@ sealed interface InternetTileModel { data class Inactive( override val secondaryTitle: CharSequence? = null, override val secondaryLabel: Text? = null, - override val iconId: Int? = null, - override val icon: Icon? = null, + override val icon: InternetTileIconModel = InternetTileIconModel.Cellular(1), override val stateDescription: ContentDescription? = null, override val contentDescription: ContentDescription? = null, ) : InternetTileModel diff --git a/packages/SystemUI/src/com/android/systemui/qs/ui/viewmodel/QuickSettingsShadeOverlayContentViewModel.kt b/packages/SystemUI/src/com/android/systemui/qs/ui/viewmodel/QuickSettingsShadeOverlayContentViewModel.kt index 7c8fbeaec0d5..afb9a788ec24 100644 --- a/packages/SystemUI/src/com/android/systemui/qs/ui/viewmodel/QuickSettingsShadeOverlayContentViewModel.kt +++ b/packages/SystemUI/src/com/android/systemui/qs/ui/viewmodel/QuickSettingsShadeOverlayContentViewModel.kt @@ -16,10 +16,18 @@ package com.android.systemui.qs.ui.viewmodel +import com.android.systemui.lifecycle.ExclusiveActivatable +import com.android.systemui.scene.domain.interactor.SceneInteractor +import com.android.systemui.scene.shared.model.Scenes import com.android.systemui.shade.domain.interactor.ShadeInteractor import com.android.systemui.shade.ui.viewmodel.ShadeHeaderViewModel import dagger.assisted.AssistedFactory import dagger.assisted.AssistedInject +import kotlinx.coroutines.awaitCancellation +import kotlinx.coroutines.coroutineScope +import kotlinx.coroutines.flow.distinctUntilChanged +import kotlinx.coroutines.flow.filter +import kotlinx.coroutines.launch /** * Models UI state used to render the content of the quick settings shade overlay. @@ -31,11 +39,42 @@ class QuickSettingsShadeOverlayContentViewModel @AssistedInject constructor( val shadeInteractor: ShadeInteractor, + val sceneInteractor: SceneInteractor, val shadeHeaderViewModelFactory: ShadeHeaderViewModel.Factory, val quickSettingsContainerViewModel: QuickSettingsContainerViewModel, -) { +) : ExclusiveActivatable() { + + override suspend fun onActivated(): Nothing { + coroutineScope { + launch { + sceneInteractor.currentScene.collect { currentScene -> + when (currentScene) { + // TODO(b/369513770): The ShadeSession should be preserved in this scenario. + Scenes.Bouncer -> + shadeInteractor.collapseQuickSettingsShade( + loggingReason = "bouncer shown while shade is open" + ) + } + } + } + + launch { + shadeInteractor.isShadeTouchable + .distinctUntilChanged() + .filter { !it } + .collect { + shadeInteractor.collapseQuickSettingsShade( + loggingReason = "device became non-interactive" + ) + } + } + } + + awaitCancellation() + } + fun onScrimClicked() { - shadeInteractor.collapseQuickSettingsShade(loggingReason = "Shade scrim clicked") + shadeInteractor.collapseQuickSettingsShade(loggingReason = "shade scrim clicked") } @AssistedFactory diff --git a/packages/SystemUI/src/com/android/systemui/recordissue/IssueRecordingService.kt b/packages/SystemUI/src/com/android/systemui/recordissue/IssueRecordingService.kt index a5f4a8959569..4d2bc91aa52a 100644 --- a/packages/SystemUI/src/com/android/systemui/recordissue/IssueRecordingService.kt +++ b/packages/SystemUI/src/com/android/systemui/recordissue/IssueRecordingService.kt @@ -61,11 +61,11 @@ constructor( uiEventLogger, notificationManager, userContextProvider, - keyguardDismissUtil + keyguardDismissUtil, ) { - private val commandHandler = - IssueRecordingServiceCommandHandler( + private val session = + IssueRecordingServiceSession( bgExecutor, dialogTransitionAnimator, panelInteractor, @@ -86,7 +86,7 @@ constructor( Log.d(getTag(), "handling action: ${intent?.action}") when (intent?.action) { ACTION_START -> { - commandHandler.handleStartCommand() + session.start() if (!issueRecordingState.recordScreen) { // If we don't want to record the screen, the ACTION_SHOW_START_NOTIF action // will circumvent the RecordingService's screen recording start code. @@ -94,12 +94,12 @@ constructor( } } ACTION_STOP, - ACTION_STOP_NOTIF -> commandHandler.handleStopCommand(contentResolver) + ACTION_STOP_NOTIF -> session.stop(contentResolver) ACTION_SHARE -> { - commandHandler.handleShareCommand( + session.share( intent.getIntExtra(EXTRA_NOTIFICATION_ID, mNotificationId), intent.getParcelableExtra(EXTRA_PATH, Uri::class.java), - this + this, ) // Unlike all other actions, action_share has different behavior for the screen // recording qs tile than it does for the record issue qs tile. Return sticky to diff --git a/packages/SystemUI/src/com/android/systemui/recordissue/IssueRecordingServiceCommandHandler.kt b/packages/SystemUI/src/com/android/systemui/recordissue/IssueRecordingServiceSession.kt index 32de0f353502..e4d3e6cae502 100644 --- a/packages/SystemUI/src/com/android/systemui/recordissue/IssueRecordingServiceCommandHandler.kt +++ b/packages/SystemUI/src/com/android/systemui/recordissue/IssueRecordingServiceSession.kt @@ -34,9 +34,11 @@ private const val DISABLED = 0 /** * This class exists to unit test the business logic encapsulated in IssueRecordingService. Android * specifically calls out that there is no supported way to test IntentServices here: - * https://developer.android.com/training/testing/other-components/services + * https://developer.android.com/training/testing/other-components/services, and mentions that the + * best way to add unit tests, is to introduce a separate class containing the business logic of + * that service, and test the functionality via that class. */ -class IssueRecordingServiceCommandHandler( +class IssueRecordingServiceSession( private val bgExecutor: Executor, private val dialogTransitionAnimator: DialogTransitionAnimator, private val panelInteractor: PanelInteractor, @@ -47,12 +49,12 @@ class IssueRecordingServiceCommandHandler( private val userContextProvider: UserContextProvider, ) { - fun handleStartCommand() { + fun start() { bgExecutor.execute { traceurMessageSender.startTracing(issueRecordingState.traceConfig) } issueRecordingState.isRecording = true } - fun handleStopCommand(contentResolver: ContentResolver) { + fun stop(contentResolver: ContentResolver) { bgExecutor.execute { if (issueRecordingState.traceConfig.longTrace) { Settings.Global.putInt(contentResolver, NOTIFY_SESSION_ENDED_SETTING, DISABLED) @@ -62,12 +64,12 @@ class IssueRecordingServiceCommandHandler( issueRecordingState.isRecording = false } - fun handleShareCommand(notificationId: Int, screenRecording: Uri?, context: Context) { + fun share(notificationId: Int, screenRecording: Uri?, context: Context) { bgExecutor.execute { notificationManager.cancelAsUser( null, notificationId, - UserHandle(userContextProvider.userContext.userId) + UserHandle(userContextProvider.userContext.userId), ) if (issueRecordingState.takeBugreport) { diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/core/StatusBarConntectedDisplays.kt b/packages/SystemUI/src/com/android/systemui/statusbar/core/StatusBarConntectedDisplays.kt new file mode 100644 index 000000000000..54a18f764406 --- /dev/null +++ b/packages/SystemUI/src/com/android/systemui/statusbar/core/StatusBarConntectedDisplays.kt @@ -0,0 +1,61 @@ +/* + * 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.systemui.statusbar.core + +import com.android.systemui.Flags +import com.android.systemui.flags.FlagToken +import com.android.systemui.flags.RefactorFlagUtils + +/** Helper for reading or using the status bar connected displays flag state. */ +@Suppress("NOTHING_TO_INLINE") +object StatusBarConnectedDisplays { + /** The aconfig flag name */ + const val FLAG_NAME = Flags.FLAG_STATUS_BAR_CONNECTED_DISPLAYS + + /** A token used for dependency declaration */ + val token: FlagToken + get() = FlagToken(FLAG_NAME, isEnabled) + + /** Is the refactor enabled */ + @JvmStatic + inline val isEnabled + get() = Flags.statusBarConnectedDisplays() + + /** + * Called to ensure code is only run when the flag is enabled. This protects users from the + * unintended behaviors caused by accidentally running new logic, while also crashing on an eng + * build to ensure that the refactor author catches issues in testing. + */ + @JvmStatic + inline fun isUnexpectedlyInLegacyMode() = + RefactorFlagUtils.isUnexpectedlyInLegacyMode(isEnabled, FLAG_NAME) + + /** + * Called to ensure code is only run when the flag is enabled. This will throw an exception if + * the flag is not enabled to ensure that the refactor author catches issues in testing. + * Caution!! Using this check incorrectly will cause crashes in nextfood builds! + */ + @JvmStatic + inline fun assertInNewMode() = RefactorFlagUtils.assertInNewMode(isEnabled, FLAG_NAME) + + /** + * Called to ensure code is only run when the flag is disabled. This will throw an exception if + * the flag is enabled to ensure that the refactor author catches issues in testing. + */ + @JvmStatic + inline fun assertInLegacyMode() = RefactorFlagUtils.assertInLegacyMode(isEnabled, FLAG_NAME) +} diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/NotificationActivityStarter.kt b/packages/SystemUI/src/com/android/systemui/statusbar/notification/NotificationActivityStarter.kt index 0f93b5d1ea12..231a0b0b21cb 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/NotificationActivityStarter.kt +++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/NotificationActivityStarter.kt @@ -16,6 +16,11 @@ package com.android.systemui.statusbar.notification import android.content.Intent +import android.provider.Settings.ACTION_AUTOMATIC_ZEN_RULE_SETTINGS +import android.provider.Settings.ACTION_NOTIFICATION_HISTORY +import android.provider.Settings.ACTION_NOTIFICATION_SETTINGS +import android.provider.Settings.ACTION_ZEN_MODE_SETTINGS +import android.provider.Settings.EXTRA_AUTOMATIC_ZEN_RULE_ID import android.view.View import com.android.systemui.statusbar.notification.collection.NotificationEntry import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow @@ -25,6 +30,7 @@ import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow * (e.g. clicking on a notification, tapping on the settings icon in the notification guts) */ interface NotificationActivityStarter { + /** Called when the user clicks on the notification bubble icon. */ fun onNotificationBubbleIconClicked(entry: NotificationEntry?) @@ -35,14 +41,63 @@ interface NotificationActivityStarter { fun startNotificationGutsIntent(intent: Intent?, appUid: Int, row: ExpandableNotificationRow?) /** - * Called when the user clicks "Manage" or "History" in the Shade, or the "No notifications" - * text. + * Called when the user clicks "Manage" or "History" in the Shade. Prefer using + * [startSettingsIntent] instead. */ fun startHistoryIntent(view: View?, showHistory: Boolean) + /** + * Called to open a settings intent from a launchable view (such as the "Manage" or "History" + * button in the shade, or the "No notifications" text). + * + * @param view the view to perform the launch animation from (must extend [LaunchableView]) + * @param intentInfo information about the (settings) intent to be launched + */ + fun startSettingsIntent(view: View, intentInfo: SettingsIntent) + /** Called when the user succeed to drop notification to proper target view. */ fun onDragSuccess(entry: NotificationEntry?) val isCollapsingToShowActivityOverLockscreen: Boolean get() = false + + /** + * Information about a settings intent to be launched. + * + * If the [targetIntent] is T and [backStack] is [A, B, C], the stack will look like + * [A, B, C, T]. + */ + data class SettingsIntent( + var targetIntent: Intent, + var backStack: List<Intent> = emptyList(), + var cujType: Int? = null, + ) { + // Utility factory methods for known intents + companion object { + fun forNotificationSettings(cujType: Int? = null) = + SettingsIntent( + targetIntent = Intent(ACTION_NOTIFICATION_SETTINGS), + cujType = cujType, + ) + + fun forNotificationHistory(cujType: Int? = null) = + SettingsIntent( + targetIntent = Intent(ACTION_NOTIFICATION_HISTORY), + backStack = listOf(Intent(ACTION_NOTIFICATION_SETTINGS)), + cujType = cujType, + ) + + fun forModesSettings(cujType: Int? = null) = + SettingsIntent(targetIntent = Intent(ACTION_ZEN_MODE_SETTINGS), cujType = cujType) + + fun forModeSettings(modeId: String, cujType: Int? = null) = + SettingsIntent( + targetIntent = + Intent(ACTION_AUTOMATIC_ZEN_RULE_SETTINGS) + .putExtra(EXTRA_AUTOMATIC_ZEN_RULE_ID, modeId), + backStack = listOf(Intent(ACTION_ZEN_MODE_SETTINGS)), + cujType = cujType, + ) + } + } } diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/NotificationSectionsFeatureManager.kt b/packages/SystemUI/src/com/android/systemui/statusbar/notification/NotificationSectionsFeatureManager.kt index b342722ebb09..b67092ca9348 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/NotificationSectionsFeatureManager.kt +++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/NotificationSectionsFeatureManager.kt @@ -22,7 +22,7 @@ import com.android.internal.annotations.VisibleForTesting import com.android.internal.config.sysui.SystemUiDeviceConfigFlags.NOTIFICATIONS_USE_PEOPLE_FILTERING import com.android.systemui.dagger.SysUISingleton import com.android.systemui.statusbar.notification.collection.NotificationClassificationFlag -import com.android.systemui.statusbar.notification.shared.NotificationMinimalismPrototype +import com.android.systemui.statusbar.notification.shared.NotificationMinimalism import com.android.systemui.statusbar.notification.shared.PriorityPeopleSection import com.android.systemui.statusbar.notification.stack.BUCKET_ALERTING import com.android.systemui.statusbar.notification.stack.BUCKET_FOREGROUND_SERVICE @@ -54,7 +54,7 @@ constructor(val proxy: DeviceConfigProxy, val context: Context) { fun getNotificationBuckets(): IntArray { if ( PriorityPeopleSection.isEnabled || - NotificationMinimalismPrototype.isEnabled || + NotificationMinimalism.isEnabled || NotificationClassificationFlag.isEnabled ) { // We don't need this list to be adaptive, it can be the superset of all features. diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/coordinator/LockScreenMinimalismCoordinator.kt b/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/coordinator/LockScreenMinimalismCoordinator.kt index a621b2a02c5d..4e63b920a73d 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/coordinator/LockScreenMinimalismCoordinator.kt +++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/coordinator/LockScreenMinimalismCoordinator.kt @@ -35,7 +35,7 @@ import com.android.systemui.statusbar.notification.collection.listbuilder.plugga import com.android.systemui.statusbar.notification.collection.notifcollection.NotifCollectionListener import com.android.systemui.statusbar.notification.domain.interactor.HeadsUpNotificationInteractor import com.android.systemui.statusbar.notification.domain.interactor.SeenNotificationsInteractor -import com.android.systemui.statusbar.notification.shared.NotificationMinimalismPrototype +import com.android.systemui.statusbar.notification.shared.NotificationMinimalism import com.android.systemui.statusbar.notification.stack.BUCKET_TOP_ONGOING import com.android.systemui.statusbar.notification.stack.BUCKET_TOP_UNSEEN import com.android.systemui.util.asIndenting @@ -77,7 +77,7 @@ constructor( private var unseenFilterEnabled = false override fun attach(pipeline: NotifPipeline) { - if (NotificationMinimalismPrototype.isUnexpectedlyInLegacyMode()) { + if (NotificationMinimalism.isUnexpectedlyInLegacyMode()) { return } pipeline.addPromoter(unseenNotifPromoter) @@ -129,26 +129,25 @@ constructor( } } - private fun unseenFeatureEnabled(): Flow<Boolean> { - // TODO(b/330387368): create LOCK_SCREEN_NOTIFICATION_MINIMALISM setting to use here? - // Or should we actually just repurpose using the existing setting? - if (NotificationMinimalismPrototype.isEnabled) { - return flowOf(true) + private fun minimalismFeatureSettingEnabled(): Flow<Boolean> { + if (!NotificationMinimalism.isEnabled) { + return flowOf(false) } - return seenNotificationsInteractor.isLockScreenShowOnlyUnseenNotificationsEnabled() + return seenNotificationsInteractor.isLockScreenNotificationMinimalismEnabled() } private suspend fun trackUnseenFilterSettingChanges() { - unseenFeatureEnabled().collectLatest { isSettingEnabled -> + // Only filter the seen notifs when the lock screen minimalism feature settings is on. + minimalismFeatureSettingEnabled().collectLatest { isMinimalismSettingEnabled -> // update local field and invalidate if necessary - if (isSettingEnabled != unseenFilterEnabled) { - unseenFilterEnabled = isSettingEnabled + if (isMinimalismSettingEnabled != unseenFilterEnabled) { + unseenFilterEnabled = isMinimalismSettingEnabled unseenNotifications.clear() unseenNotifPromoter.invalidateList("unseen setting changed") } // if the setting is enabled, then start tracking and filtering unseen notifications - logger.logTrackingUnseen(isSettingEnabled) - if (isSettingEnabled) { + logger.logTrackingUnseen(isMinimalismSettingEnabled) + if (isMinimalismSettingEnabled) { trackSeenNotifications() } } @@ -178,7 +177,7 @@ constructor( } private fun pickOutTopUnseenNotifs(list: List<ListEntry>) { - if (NotificationMinimalismPrototype.isUnexpectedlyInLegacyMode()) return + if (NotificationMinimalism.isUnexpectedlyInLegacyMode()) return if (!unseenFilterEnabled) return // Only ever elevate a top unseen notification on keyguard, not even locked shade if (statusBarStateController.state != StatusBarState.KEYGUARD) { @@ -215,9 +214,9 @@ constructor( object : NotifPromoter(TAG) { override fun shouldPromoteToTopLevel(child: NotificationEntry): Boolean = when { - NotificationMinimalismPrototype.isUnexpectedlyInLegacyMode() -> false + NotificationMinimalism.isUnexpectedlyInLegacyMode() -> false seenNotificationsInteractor.isTopOngoingNotification(child) -> true - !NotificationMinimalismPrototype.ungroupTopUnseen -> false + !NotificationMinimalism.ungroupTopUnseen -> false else -> seenNotificationsInteractor.isTopUnseenNotification(child) } } @@ -225,7 +224,7 @@ constructor( val topOngoingSectioner = object : NotifSectioner("TopOngoing", BUCKET_TOP_ONGOING) { override fun isInSection(entry: ListEntry): Boolean { - if (NotificationMinimalismPrototype.isUnexpectedlyInLegacyMode()) return false + if (NotificationMinimalism.isUnexpectedlyInLegacyMode()) return false return entry.anyEntry { notificationEntry -> seenNotificationsInteractor.isTopOngoingNotification(notificationEntry) } @@ -235,7 +234,7 @@ constructor( val topUnseenSectioner = object : NotifSectioner("TopUnseen", BUCKET_TOP_UNSEEN) { override fun isInSection(entry: ListEntry): Boolean { - if (NotificationMinimalismPrototype.isUnexpectedlyInLegacyMode()) return false + if (NotificationMinimalism.isUnexpectedlyInLegacyMode()) return false return entry.anyEntry { notificationEntry -> seenNotificationsInteractor.isTopUnseenNotification(notificationEntry) } diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/coordinator/LockScreenMinimalismCoordinatorLogger.kt b/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/coordinator/LockScreenMinimalismCoordinatorLogger.kt index e44a77c30999..a4fa72942380 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/coordinator/LockScreenMinimalismCoordinatorLogger.kt +++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/coordinator/LockScreenMinimalismCoordinatorLogger.kt @@ -34,7 +34,10 @@ constructor( TAG, LogLevel.DEBUG, messageInitializer = { bool1 = trackingUnseen }, - messagePrinter = { "${if (bool1) "Start" else "Stop"} tracking unseen notifications." }, + messagePrinter = { + "${if (bool1) "Start" else "Stop"} " + + "tracking unseen notifications because of settings change." + }, ) fun logShadeVisible(numUnseen: Int) { diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/coordinator/NotifCoordinators.kt b/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/coordinator/NotifCoordinators.kt index 73ce48b2324a..96c260bb0852 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/coordinator/NotifCoordinators.kt +++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/coordinator/NotifCoordinators.kt @@ -25,7 +25,7 @@ import com.android.systemui.statusbar.notification.collection.SortBySectionTimeF import com.android.systemui.statusbar.notification.collection.coordinator.dagger.CoordinatorScope import com.android.systemui.statusbar.notification.collection.listbuilder.pluggable.NotifSectioner import com.android.systemui.statusbar.notification.collection.provider.SectionStyleProvider -import com.android.systemui.statusbar.notification.shared.NotificationMinimalismPrototype +import com.android.systemui.statusbar.notification.shared.NotificationMinimalism import com.android.systemui.statusbar.notification.shared.NotificationsLiveDataStoreRefactor import com.android.systemui.statusbar.notification.shared.PriorityPeopleSection import javax.inject.Inject @@ -88,11 +88,10 @@ constructor( mCoordinators.add(hideLocallyDismissedNotifsCoordinator) mCoordinators.add(hideNotifsForOtherUsersCoordinator) mCoordinators.add(keyguardCoordinator) - if (NotificationMinimalismPrototype.isEnabled) { + if (NotificationMinimalism.isEnabled) { mCoordinators.add(lockScreenMinimalismCoordinator) - } else { - mCoordinators.add(unseenKeyguardCoordinator) } + mCoordinators.add(unseenKeyguardCoordinator) mCoordinators.add(rankingCoordinator) mCoordinators.add(colorizedFgsCoordinator) mCoordinators.add(deviceProvisionedCoordinator) @@ -125,11 +124,11 @@ constructor( } // Manually add Ordered Sections - if (NotificationMinimalismPrototype.isEnabled) { + if (NotificationMinimalism.isEnabled) { mOrderedSections.add(lockScreenMinimalismCoordinator.topOngoingSectioner) // Top Ongoing } mOrderedSections.add(headsUpCoordinator.sectioner) // HeadsUp - if (NotificationMinimalismPrototype.isEnabled) { + if (NotificationMinimalism.isEnabled) { mOrderedSections.add(lockScreenMinimalismCoordinator.topUnseenSectioner) // Top Unseen } mOrderedSections.add(colorizedFgsCoordinator.sectioner) // ForegroundService diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/coordinator/OriginalUnseenKeyguardCoordinator.kt b/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/coordinator/OriginalUnseenKeyguardCoordinator.kt index bfea2ba6b839..cf1329c6b564 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/coordinator/OriginalUnseenKeyguardCoordinator.kt +++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/coordinator/OriginalUnseenKeyguardCoordinator.kt @@ -35,7 +35,6 @@ import com.android.systemui.statusbar.notification.collection.coordinator.dagger import com.android.systemui.statusbar.notification.collection.listbuilder.pluggable.NotifFilter import com.android.systemui.statusbar.notification.collection.notifcollection.NotifCollectionListener import com.android.systemui.statusbar.notification.domain.interactor.SeenNotificationsInteractor -import com.android.systemui.statusbar.notification.shared.NotificationMinimalismPrototype import com.android.systemui.statusbar.policy.HeadsUpManager import com.android.systemui.statusbar.policy.headsUpEvents import com.android.systemui.util.asIndenting @@ -51,7 +50,6 @@ import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.distinctUntilChanged -import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch @@ -87,7 +85,6 @@ constructor( private var unseenFilterEnabled = false override fun attach(pipeline: NotifPipeline) { - NotificationMinimalismPrototype.assertInLegacyMode() pipeline.addFinalizeFilter(unseenNotifFilter) pipeline.addCollectionListener(collectionListener) scope.launch { trackUnseenFilterSettingChanges() } @@ -253,10 +250,6 @@ constructor( } private fun unseenFeatureEnabled(): Flow<Boolean> { - if (NotificationMinimalismPrototype.isEnabled) { - // TODO(b/330387368): should this really just be turned off? If so, hide the setting. - return flowOf(false) - } return seenNotificationsInteractor.isLockScreenShowOnlyUnseenNotificationsEnabled() } diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/coordinator/RemoteInputCoordinator.kt b/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/coordinator/RemoteInputCoordinator.kt index 5ff5d2d9a7e5..1fe32c9a873a 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/coordinator/RemoteInputCoordinator.kt +++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/coordinator/RemoteInputCoordinator.kt @@ -117,18 +117,12 @@ constructor( (entry.getSbn().getNotification().flags and FLAG_LIFETIME_EXTENDED_BY_DIRECT_REPLY) > 0 ) { + // If we've received an update from the system and the entry is marked + // as lifetime extended, that means system server has received a + // cancelation in response to a direct reply, and sent an update to + // let system ui know that it should rebuild the notification with + // that direct reply. if ( - mNotificationRemoteInputManager.shouldKeepForRemoteInputHistory( - entry - ) - ) { - val newSbn = mRebuilder.rebuildForRemoteInputReply(entry) - entry.onRemoteInputInserted() - mNotifUpdater.onInternalNotificationUpdate( - newSbn, - "Extending lifetime of notification with remote input", - ) - } else if ( mNotificationRemoteInputManager.shouldKeepForSmartReplyHistory( entry ) @@ -140,16 +134,11 @@ constructor( "Extending lifetime of notification with smart reply", ) } else { - // The app may have re-cancelled a notification after it had already - // been lifetime extended. - // Rebuild the notification with the replies it already had to - // ensure - // those replies continue to be displayed. - val newSbn = mRebuilder.rebuildWithExistingReplies(entry) + val newSbn = mRebuilder.rebuildForRemoteInputReply(entry) + entry.onRemoteInputInserted() mNotifUpdater.onInternalNotificationUpdate( newSbn, - "Extending lifetime of notification that has already been " + - "lifetime extended.", + "Extending lifetime of notification with remote input", ) } } else { diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/coordinator/VisualStabilityCoordinator.java b/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/coordinator/VisualStabilityCoordinator.java index 6d0148a24cf8..41419f31eb7a 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/coordinator/VisualStabilityCoordinator.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/collection/coordinator/VisualStabilityCoordinator.java @@ -41,7 +41,7 @@ import com.android.systemui.statusbar.notification.collection.NotificationEntry; import com.android.systemui.statusbar.notification.collection.listbuilder.pluggable.NotifStabilityManager; import com.android.systemui.statusbar.notification.collection.provider.VisualStabilityProvider; import com.android.systemui.statusbar.notification.domain.interactor.SeenNotificationsInteractor; -import com.android.systemui.statusbar.notification.shared.NotificationMinimalismPrototype; +import com.android.systemui.statusbar.notification.shared.NotificationMinimalism; import com.android.systemui.statusbar.policy.HeadsUpManager; import com.android.systemui.util.concurrency.DelayableExecutor; import com.android.systemui.util.kotlin.BooleanFlowOperators; @@ -170,7 +170,7 @@ public class VisualStabilityCoordinator implements Coordinator, Dumpable { if (entry == null) { return false; } - boolean isTopUnseen = NotificationMinimalismPrototype.isEnabled() + boolean isTopUnseen = NotificationMinimalism.isEnabled() && (mSeenNotificationsInteractor.isTopUnseenNotification(entry) || mSeenNotificationsInteractor.isTopOngoingNotification(entry)); if (isTopUnseen || mHeadsUpManager.isHeadsUpEntry(entry.getKey())) { diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/domain/interactor/SeenNotificationsInteractor.kt b/packages/SystemUI/src/com/android/systemui/statusbar/notification/domain/interactor/SeenNotificationsInteractor.kt index 29564326481f..1babe47559e5 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/domain/interactor/SeenNotificationsInteractor.kt +++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/domain/interactor/SeenNotificationsInteractor.kt @@ -23,7 +23,7 @@ import com.android.systemui.dagger.SysUISingleton import com.android.systemui.dagger.qualifiers.Background import com.android.systemui.statusbar.notification.collection.NotificationEntry import com.android.systemui.statusbar.notification.data.repository.ActiveNotificationListRepository -import com.android.systemui.statusbar.notification.shared.NotificationMinimalismPrototype +import com.android.systemui.statusbar.notification.shared.NotificationMinimalism import com.android.systemui.util.printSection import com.android.systemui.util.settings.SecureSettings import com.android.systemui.util.settings.SettingsProxyExt.observerFlow @@ -57,25 +57,25 @@ constructor( /** Set the entry that is identified as the top ongoing notification. */ fun setTopOngoingNotification(entry: NotificationEntry?) { - if (NotificationMinimalismPrototype.isUnexpectedlyInLegacyMode()) return + if (NotificationMinimalism.isUnexpectedlyInLegacyMode()) return notificationListRepository.topOngoingNotificationKey.value = entry?.key } /** Determine if the given notification is the top ongoing notification. */ fun isTopOngoingNotification(entry: NotificationEntry?): Boolean = - if (NotificationMinimalismPrototype.isUnexpectedlyInLegacyMode()) false + if (NotificationMinimalism.isUnexpectedlyInLegacyMode()) false else entry != null && notificationListRepository.topOngoingNotificationKey.value == entry.key /** Set the entry that is identified as the top unseen notification. */ fun setTopUnseenNotification(entry: NotificationEntry?) { - if (NotificationMinimalismPrototype.isUnexpectedlyInLegacyMode()) return + if (NotificationMinimalism.isUnexpectedlyInLegacyMode()) return notificationListRepository.topUnseenNotificationKey.value = entry?.key } /** Determine if the given notification is the top unseen notification. */ fun isTopUnseenNotification(entry: NotificationEntry?): Boolean = - if (NotificationMinimalismPrototype.isUnexpectedlyInLegacyMode()) false + if (NotificationMinimalism.isUnexpectedlyInLegacyMode()) false else entry != null && notificationListRepository.topUnseenNotificationKey.value == entry.key fun dump(pw: IndentingPrintWriter) = @@ -120,4 +120,29 @@ constructor( // only track the most recent emission, if events are happening faster than they can be // consumed .conflate() + + fun isLockScreenNotificationMinimalismEnabled(): Flow<Boolean> = + secureSettings + // emit whenever the setting has changed + .observerFlow( + UserHandle.USER_ALL, + Settings.Secure.LOCK_SCREEN_NOTIFICATION_MINIMALISM, + ) + // perform a query immediately + .onStart { emit(Unit) } + // for each change, lookup the new value + .map { + secureSettings.getIntForUser( + name = Settings.Secure.LOCK_SCREEN_NOTIFICATION_MINIMALISM, + default = 1, + userHandle = UserHandle.USER_CURRENT, + ) == 1 + } + // don't emit anything if nothing has changed + .distinctUntilChanged() + // perform lookups on the bg thread pool + .flowOn(bgDispatcher) + // only track the most recent emission, if events are happening faster than they can be + // consumed + .conflate() } diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/emptyshade/ui/viewbinder/EmptyShadeViewBinder.kt b/packages/SystemUI/src/com/android/systemui/statusbar/notification/emptyshade/ui/viewbinder/EmptyShadeViewBinder.kt index 102a11c2314c..7f1b04358546 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/emptyshade/ui/viewbinder/EmptyShadeViewBinder.kt +++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/emptyshade/ui/viewbinder/EmptyShadeViewBinder.kt @@ -17,6 +17,7 @@ package com.android.systemui.statusbar.notification.emptyshade.ui.viewbinder import android.view.View +import com.android.systemui.statusbar.notification.NotificationActivityStarter import com.android.systemui.statusbar.notification.emptyshade.ui.view.EmptyShadeView import com.android.systemui.statusbar.notification.emptyshade.ui.viewmodel.EmptyShadeViewModel import kotlinx.coroutines.coroutineScope @@ -26,18 +27,16 @@ object EmptyShadeViewBinder { suspend fun bind( view: EmptyShadeView, viewModel: EmptyShadeViewModel, - launchNotificationSettings: View.OnClickListener, - launchNotificationHistory: View.OnClickListener, + notificationActivityStarter: NotificationActivityStarter, ) = coroutineScope { launch { viewModel.text.collect { view.setText(it) } } launch { - viewModel.tappingShouldLaunchHistory.collect { shouldLaunchHistory -> - if (shouldLaunchHistory) { - view.setOnClickListener(launchNotificationHistory) - } else { - view.setOnClickListener(launchNotificationSettings) + viewModel.onClick.collect { settingsIntent -> + val onClickListener = { view: View -> + notificationActivityStarter.startSettingsIntent(view, settingsIntent) } + view.setOnClickListener(onClickListener) } } diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/emptyshade/ui/viewmodel/EmptyShadeViewModel.kt b/packages/SystemUI/src/com/android/systemui/statusbar/notification/emptyshade/ui/viewmodel/EmptyShadeViewModel.kt index d5417e7ae8f6..8c8f200f78b7 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/emptyshade/ui/viewmodel/EmptyShadeViewModel.kt +++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/emptyshade/ui/viewmodel/EmptyShadeViewModel.kt @@ -22,6 +22,7 @@ import com.android.systemui.dump.DumpManager import com.android.systemui.modes.shared.ModesUi import com.android.systemui.res.R import com.android.systemui.shared.notifications.domain.interactor.NotificationSettingsInteractor +import com.android.systemui.statusbar.notification.NotificationActivityStarter.SettingsIntent import com.android.systemui.statusbar.notification.domain.interactor.SeenNotificationsInteractor import com.android.systemui.statusbar.notification.emptyshade.shared.ModesEmptyShadeFix import com.android.systemui.statusbar.notification.footer.shared.FooterViewRefactor @@ -34,6 +35,7 @@ import java.util.Locale import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.flow.map @@ -80,8 +82,7 @@ constructor( if (ModesUi.isEnabled) { zenModeInteractor.modesHidingNotifications.map { modes -> // Create a string that is either "No notifications" if no modes are filtering - // them - // out, or something like "Notifications paused by SomeMode" otherwise. + // them out, or something like "Notifications paused by SomeMode" otherwise. val msgFormat = MessageFormat( context.getString(R.string.modes_suppressing_shade_text), @@ -116,9 +117,26 @@ constructor( ) } - val tappingShouldLaunchHistory by lazy { + val onClick: Flow<SettingsIntent> by lazy { ModesEmptyShadeFix.assertInNewMode() - notificationSettingsInteractor.isNotificationHistoryEnabled + combine( + zenModeInteractor.modesHidingNotifications, + notificationSettingsInteractor.isNotificationHistoryEnabled, + ) { modes, isNotificationHistoryEnabled -> + if (modes.isNotEmpty()) { + if (modes.size == 1) { + SettingsIntent.forModeSettings(modes[0].id) + } else { + SettingsIntent.forModesSettings() + } + } else { + if (isNotificationHistoryEnabled) { + SettingsIntent.forNotificationHistory() + } else { + SettingsIntent.forNotificationSettings() + } + } + } } @AssistedFactory diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/footer/ui/viewbinder/FooterViewBinder.kt b/packages/SystemUI/src/com/android/systemui/statusbar/notification/footer/ui/viewbinder/FooterViewBinder.kt index 920541d101cf..22bec5a43230 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/footer/ui/viewbinder/FooterViewBinder.kt +++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/footer/ui/viewbinder/FooterViewBinder.kt @@ -19,6 +19,8 @@ package com.android.systemui.statusbar.notification.footer.ui.viewbinder import android.view.View import androidx.lifecycle.lifecycleScope import com.android.systemui.lifecycle.repeatWhenAttached +import com.android.systemui.statusbar.notification.NotificationActivityStarter +import com.android.systemui.statusbar.notification.emptyshade.shared.ModesEmptyShadeFix import com.android.systemui.statusbar.notification.footer.ui.view.FooterView import com.android.systemui.statusbar.notification.footer.ui.viewmodel.FooterViewModel import com.android.systemui.util.ui.isAnimating @@ -36,6 +38,7 @@ object FooterViewBinder { clearAllNotifications: View.OnClickListener, launchNotificationSettings: View.OnClickListener, launchNotificationHistory: View.OnClickListener, + notificationActivityStarter: NotificationActivityStarter, ): DisposableHandle { return footer.repeatWhenAttached { lifecycleScope.launch { @@ -45,6 +48,7 @@ object FooterViewBinder { clearAllNotifications, launchNotificationSettings, launchNotificationHistory, + notificationActivityStarter, ) } } @@ -56,6 +60,7 @@ object FooterViewBinder { clearAllNotifications: View.OnClickListener, launchNotificationSettings: View.OnClickListener, launchNotificationHistory: View.OnClickListener, + notificationActivityStarter: NotificationActivityStarter, ) = coroutineScope { launch { bindClearAllButton(footer, viewModel, clearAllNotifications) } launch { @@ -64,6 +69,7 @@ object FooterViewBinder { viewModel, launchNotificationSettings, launchNotificationHistory, + notificationActivityStarter, ) } launch { bindMessage(footer, viewModel) } @@ -113,13 +119,23 @@ object FooterViewBinder { viewModel: FooterViewModel, launchNotificationSettings: View.OnClickListener, launchNotificationHistory: View.OnClickListener, + notificationActivityStarter: NotificationActivityStarter, ) = coroutineScope { launch { - viewModel.manageButtonShouldLaunchHistory.collect { shouldLaunchHistory -> - if (shouldLaunchHistory) { - footer.setManageButtonClickListener(launchNotificationHistory) - } else { - footer.setManageButtonClickListener(launchNotificationSettings) + if (ModesEmptyShadeFix.isEnabled) { + viewModel.manageOrHistoryButtonClick.collect { settingsIntent -> + val onClickListener = { view: View -> + notificationActivityStarter.startSettingsIntent(view, settingsIntent) + } + footer.setManageButtonClickListener(onClickListener) + } + } else { + viewModel.manageButtonShouldLaunchHistory.collect { shouldLaunchHistory -> + if (shouldLaunchHistory) { + footer.setManageButtonClickListener(launchNotificationHistory) + } else { + footer.setManageButtonClickListener(launchNotificationSettings) + } } } } diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/footer/ui/viewmodel/FooterViewModel.kt b/packages/SystemUI/src/com/android/systemui/statusbar/notification/footer/ui/viewmodel/FooterViewModel.kt index 90fb7285e939..a3f4cd225130 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/footer/ui/viewmodel/FooterViewModel.kt +++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/footer/ui/viewmodel/FooterViewModel.kt @@ -16,12 +16,17 @@ package com.android.systemui.statusbar.notification.footer.ui.viewmodel +import android.content.Intent +import android.provider.Settings +import com.android.internal.jank.InteractionJankMonitor import com.android.systemui.dagger.SysUISingleton import com.android.systemui.res.R import com.android.systemui.shade.domain.interactor.ShadeInteractor import com.android.systemui.shared.notifications.domain.interactor.NotificationSettingsInteractor +import com.android.systemui.statusbar.notification.NotificationActivityStarter.SettingsIntent import com.android.systemui.statusbar.notification.domain.interactor.ActiveNotificationsInteractor import com.android.systemui.statusbar.notification.domain.interactor.SeenNotificationsInteractor +import com.android.systemui.statusbar.notification.emptyshade.shared.ModesEmptyShadeFix import com.android.systemui.statusbar.notification.footer.shared.FooterViewRefactor import com.android.systemui.statusbar.notification.footer.ui.view.FooterView import com.android.systemui.util.kotlin.sample @@ -80,7 +85,7 @@ class FooterViewModel( combine( shadeInteractor.isShadeFullyExpanded, shadeInteractor.isShadeTouchable, - ::Pair + ::Pair, ) .onStart { emit(Pair(false, false)) } ) { clearAllButtonVisible, (isShadeFullyExpanded, animationsEnabled) -> @@ -93,8 +98,28 @@ class FooterViewModel( val manageButtonShouldLaunchHistory = notificationSettingsInteractor.isNotificationHistoryEnabled + // TODO(b/366003631): When inlining the flag, consider adding this to FooterButtonViewModel. + val manageOrHistoryButtonClick: Flow<SettingsIntent> by lazy { + if (ModesEmptyShadeFix.isUnexpectedlyInLegacyMode()) { + flowOf(SettingsIntent(Intent(Settings.ACTION_NOTIFICATION_SETTINGS))) + } else { + notificationSettingsInteractor.isNotificationHistoryEnabled.map { + isNotificationHistoryEnabled -> + if (isNotificationHistoryEnabled) { + SettingsIntent.forNotificationHistory( + cujType = InteractionJankMonitor.CUJ_SHADE_APP_LAUNCH_FROM_HISTORY_BUTTON + ) + } else { + SettingsIntent.forNotificationSettings( + cujType = InteractionJankMonitor.CUJ_SHADE_APP_LAUNCH_FROM_HISTORY_BUTTON + ) + } + } + } + } + private val manageOrHistoryButtonText: Flow<Int> = - manageButtonShouldLaunchHistory.map { shouldLaunchHistory -> + notificationSettingsInteractor.isNotificationHistoryEnabled.map { shouldLaunchHistory -> if (shouldLaunchHistory) R.string.manage_notifications_history_text else R.string.manage_notifications_text } @@ -128,7 +153,7 @@ object FooterViewModelModule { activeNotificationsInteractor.get(), notificationSettingsInteractor.get(), seenNotificationsInteractor.get(), - shadeInteractor.get() + shadeInteractor.get(), ) ) } else { diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/shared/NotificationMinimalismPrototype.kt b/packages/SystemUI/src/com/android/systemui/statusbar/notification/shared/NotificationMinimalism.kt index 06f3db504aaf..70bb2722c678 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/shared/NotificationMinimalismPrototype.kt +++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/shared/NotificationMinimalism.kt @@ -17,23 +17,23 @@ package com.android.systemui.statusbar.notification.shared import android.os.SystemProperties -import com.android.systemui.Flags +import com.android.server.notification.Flags import com.android.systemui.flags.FlagToken import com.android.systemui.flags.RefactorFlagUtils /** Helper for reading or using the minimalism prototype flag state. */ @Suppress("NOTHING_TO_INLINE") -object NotificationMinimalismPrototype { - const val FLAG_NAME = Flags.FLAG_NOTIFICATION_MINIMALISM_PROTOTYPE +object NotificationMinimalism { + const val FLAG_NAME = Flags.FLAG_NOTIFICATION_MINIMALISM /** A token used for dependency declaration */ val token: FlagToken get() = FlagToken(FLAG_NAME, isEnabled) - /** Is the heads-up cycling animation enabled */ + /** Is the notification minimalism enabled */ @JvmStatic inline val isEnabled - get() = Flags.notificationMinimalismPrototype() + get() = Flags.notificationMinimalism() /** * The prototype will (by default) use a promoter to ensure that the top unseen notification is diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/AmbientState.java b/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/AmbientState.java index d246b04b7957..129d4cee9cdb 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/AmbientState.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/AmbientState.java @@ -224,6 +224,7 @@ public class AmbientState implements Dumpable { * @param isSwipingUp Whether we are swiping up. */ public void setSwipingUp(boolean isSwipingUp) { + SceneContainerFlag.assertInLegacyMode(); if (!isSwipingUp && mIsSwipingUp) { // Just stopped swiping up. mIsFlingRequiredAfterLockScreenSwipeUp = true; @@ -242,6 +243,7 @@ public class AmbientState implements Dumpable { * @param isFlinging Whether we are flinging the shade open or closed. */ public void setFlinging(boolean isFlinging) { + SceneContainerFlag.assertInLegacyMode(); if (isOnKeyguard() && !isFlinging && mIsFlinging) { // Just stopped flinging. mIsFlingRequiredAfterLockScreenSwipeUp = false; @@ -717,6 +719,7 @@ public class AmbientState implements Dumpable { * @return Whether we need to do a fling down after swiping up on lockscreen. */ public boolean isFlingingAfterSwipeUpOnLockscreen() { + SceneContainerFlag.assertInLegacyMode(); return mIsFlinging && mIsFlingRequiredAfterLockScreenSwipeUp; } diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/NotificationStackScrollLayout.java b/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/NotificationStackScrollLayout.java index 0a44a2bc9d93..b466bf02387f 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/NotificationStackScrollLayout.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/NotificationStackScrollLayout.java @@ -568,6 +568,7 @@ public class NotificationStackScrollLayout private boolean mHasFilteredOutSeenNotifications; @Nullable private SplitShadeStateController mSplitShadeStateController = null; private boolean mIsSmallLandscapeLockscreenEnabled = false; + private boolean mSuppressHeightUpdates; /** Pass splitShadeStateController to view and update split shade */ public void passSplitShadeStateController(SplitShadeStateController splitShadeStateController) { @@ -1458,9 +1459,13 @@ public class NotificationStackScrollLayout * 2) Swiping up on lockscreen or flinging down after swipe up */ private boolean shouldSkipHeightUpdate() { - return mAmbientState.isOnKeyguard() - && (mAmbientState.isSwipingUp() - || mAmbientState.isFlingingAfterSwipeUpOnLockscreen()); + if (SceneContainerFlag.isEnabled()) { + return mSuppressHeightUpdates; + } else { + return mAmbientState.isOnKeyguard() + && (mAmbientState.isSwipingUp() + || mAmbientState.isFlingingAfterSwipeUpOnLockscreen()); + } } /** @@ -5399,6 +5404,7 @@ public class NotificationStackScrollLayout } public void setPanelFlinging(boolean flinging) { + SceneContainerFlag.assertInLegacyMode(); mAmbientState.setFlinging(flinging); if (!flinging) { // re-calculate the stack height which was frozen while flinging @@ -5406,6 +5412,12 @@ public class NotificationStackScrollLayout } } + @Override + public void suppressHeightUpdates(boolean suppress) { + if (SceneContainerFlag.isUnexpectedlyInLegacyMode()) return; + mSuppressHeightUpdates = suppress; + } + public void setHeadsUpGoingAwayAnimationsAllowed(boolean headsUpGoingAwayAnimationsAllowed) { mHeadsUpGoingAwayAnimationsAllowed = headsUpGoingAwayAnimationsAllowed; } diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/NotificationStackScrollLayoutController.java b/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/NotificationStackScrollLayoutController.java index 9c5fecf0338e..7b02d0cebfb3 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/NotificationStackScrollLayoutController.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/NotificationStackScrollLayoutController.java @@ -1439,6 +1439,7 @@ public class NotificationStackScrollLayoutController implements Dumpable { } public void setPanelFlinging(boolean flinging) { + SceneContainerFlag.assertInLegacyMode(); mView.setPanelFlinging(flinging); } diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/NotificationStackSizeCalculator.kt b/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/NotificationStackSizeCalculator.kt index 06222fdb2761..3bc549543ef2 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/NotificationStackSizeCalculator.kt +++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/NotificationStackSizeCalculator.kt @@ -29,7 +29,7 @@ import com.android.systemui.statusbar.StatusBarState.KEYGUARD import com.android.systemui.statusbar.SysuiStatusBarStateController import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow import com.android.systemui.statusbar.notification.row.ExpandableView -import com.android.systemui.statusbar.notification.shared.NotificationMinimalismPrototype +import com.android.systemui.statusbar.notification.shared.NotificationMinimalism import com.android.systemui.statusbar.policy.SplitShadeStateController import com.android.systemui.util.Compile import com.android.systemui.util.children @@ -74,7 +74,7 @@ constructor( /** Whether we allow keyguard to show less important notifications above the shelf. */ private val limitLockScreenToOneImportant - get() = NotificationMinimalismPrototype.isEnabled + get() = NotificationMinimalism.isEnabled /** Minimum space between two notifications, see [calculateGapAndDividerHeight]. */ private var dividerHeight by notNull<Float>() @@ -406,7 +406,7 @@ constructor( fun updateResources() { maxKeyguardNotifications = infiniteIfNegative(resources.getInteger(R.integer.keyguard_max_notification_count)) - maxNotificationsExcludesMedia = NotificationMinimalismPrototype.isEnabled + maxNotificationsExcludesMedia = NotificationMinimalism.isEnabled dividerHeight = max(1f, resources.getDimensionPixelSize(R.dimen.notification_divider_height).toFloat()) diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/ui/view/NotificationScrollView.kt b/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/ui/view/NotificationScrollView.kt index 0113e361b3d7..dbe81c10e2fd 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/ui/view/NotificationScrollView.kt +++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/ui/view/NotificationScrollView.kt @@ -124,4 +124,7 @@ interface NotificationScrollView { /** @see addHeadsUpHeightChangedListener */ fun removeHeadsUpHeightChangedListener(runnable: Runnable) + + /** Sets whether updates to the stack are are suppressed. */ + fun suppressHeightUpdates(suppress: Boolean) } diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/ui/viewbinder/NotificationListViewBinder.kt b/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/ui/viewbinder/NotificationListViewBinder.kt index 3dad32662893..ebae235f88d6 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/ui/viewbinder/NotificationListViewBinder.kt +++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/ui/viewbinder/NotificationListViewBinder.kt @@ -187,6 +187,7 @@ constructor( }, launchNotificationSettings, launchNotificationHistory, + notificationActivityStarter.get(), ) if (SceneContainerFlag.isEnabled) { launch { @@ -266,8 +267,7 @@ constructor( EmptyShadeViewBinder.bind( emptyShadeView, emptyShadeViewModel, - launchNotificationSettings, - launchNotificationHistory, + notificationActivityStarter.get(), ) } } diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/ui/viewbinder/NotificationScrollViewBinder.kt b/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/ui/viewbinder/NotificationScrollViewBinder.kt index 99ff678d10dd..87d70ba12012 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/ui/viewbinder/NotificationScrollViewBinder.kt +++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/ui/viewbinder/NotificationScrollViewBinder.kt @@ -111,6 +111,7 @@ constructor( launch { viewModel.shouldCloseGuts.filter { it }.collect { view.closeGutsOnSceneTouch() } } + launch { viewModel.suppressHeightUpdates.collect { view.suppressHeightUpdates(it) } } launchAndDispose { view.setSyntheticScrollConsumer(viewModel.syntheticScrollConsumer) diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/ui/viewmodel/NotificationScrollViewModel.kt b/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/ui/viewmodel/NotificationScrollViewModel.kt index cd9c07e38b3a..c9eaec7c5b85 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/ui/viewmodel/NotificationScrollViewModel.kt +++ b/packages/SystemUI/src/com/android/systemui/statusbar/notification/stack/ui/viewmodel/NotificationScrollViewModel.kt @@ -18,6 +18,7 @@ package com.android.systemui.statusbar.notification.stack.ui.viewmodel import com.android.compose.animation.scene.ContentKey +import com.android.compose.animation.scene.ObservableTransitionState import com.android.compose.animation.scene.ObservableTransitionState.Idle import com.android.compose.animation.scene.ObservableTransitionState.Transition import com.android.compose.animation.scene.ObservableTransitionState.Transition.ChangeScene @@ -48,6 +49,7 @@ import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.flowOf +import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.mapNotNull /** ViewModel which represents the state of the NSSL/Controller in the world of flexiglass */ @@ -129,6 +131,14 @@ constructor( } } + /** Are notification stack height updates suppressed? */ + val suppressHeightUpdates: Flow<Boolean> = + sceneInteractor.transitionState.map { transition: ObservableTransitionState -> + transition is Transition && + transition.fromContent == Scenes.Lockscreen && + (transition.toContent == Scenes.Bouncer || transition.toContent == Scenes.Gone) + } + /** * The expansion fraction of the notification stack. It should go from 0 to 1 when transitioning * from Gone to Shade scenes, and remain at 1 when in Lockscreen or Shade scenes and while diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarNotificationActivityStarter.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarNotificationActivityStarter.java index 0a6e7f59e24e..ee961955df39 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarNotificationActivityStarter.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/StatusBarNotificationActivityStarter.java @@ -43,6 +43,7 @@ import android.text.TextUtils; import android.util.EventLog; import android.view.View; +import androidx.annotation.NonNull; import androidx.annotation.VisibleForTesting; import com.android.internal.jank.InteractionJankMonitor; @@ -74,6 +75,7 @@ import com.android.systemui.statusbar.notification.NotificationLaunchAnimatorCon import com.android.systemui.statusbar.notification.collection.NotificationEntry; import com.android.systemui.statusbar.notification.collection.provider.LaunchFullScreenIntentProvider; import com.android.systemui.statusbar.notification.collection.render.NotificationVisibilityProvider; +import com.android.systemui.statusbar.notification.emptyshade.shared.ModesEmptyShadeFix; import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow; import com.android.systemui.statusbar.notification.row.ExpandableNotificationRowDragController; import com.android.systemui.statusbar.notification.row.OnUserInteractionCallback; @@ -249,7 +251,7 @@ public class StatusBarNotificationActivityStarter implements NotificationActivit * Called when a notification is clicked. * * @param entry notification that was clicked - * @param row row for that notification + * @param row row for that notification */ @Override public void onNotificationClicked(NotificationEntry entry, ExpandableNotificationRow row) { @@ -547,8 +549,8 @@ public class StatusBarNotificationActivityStarter implements NotificationActivit (adapter) -> TaskStackBuilder.create(mContext) .addNextIntentWithParentStack(intent) .startActivities(getActivityOptions( - mDisplayId, - adapter), + mDisplayId, + adapter), new UserHandle(UserHandle.getUserId(appUid)))); }); return true; @@ -565,6 +567,7 @@ public class StatusBarNotificationActivityStarter implements NotificationActivit @Override public void startHistoryIntent(View view, boolean showHistory) { + ModesEmptyShadeFix.assertInLegacyMode(); boolean animate = mActivityStarter.shouldAnimateLaunch(true /* isActivityIntent */); ActivityStarter.OnDismissAction onDismissAction = new ActivityStarter.OnDismissAction() { @Override @@ -585,14 +588,14 @@ public class StatusBarNotificationActivityStarter implements NotificationActivit ); ActivityTransitionAnimator.Controller animationController = viewController == null ? null - : new StatusBarTransitionAnimatorController( - viewController, - mShadeAnimationInteractor, - mShadeController, - mNotificationShadeWindowController, - mCommandQueue, - mDisplayId, - true /* isActivityIntent */); + : new StatusBarTransitionAnimatorController( + viewController, + mShadeAnimationInteractor, + mShadeController, + mNotificationShadeWindowController, + mCommandQueue, + mDisplayId, + true /* isActivityIntent */); mActivityTransitionAnimator.startIntentWithAnimation( animationController, animate, intent.getPackage(), @@ -612,6 +615,51 @@ public class StatusBarNotificationActivityStarter implements NotificationActivit false /* afterKeyguardGone */); } + @Override + public void startSettingsIntent(@NonNull View view, @NonNull SettingsIntent intentInfo) { + boolean animate = mActivityStarter.shouldAnimateLaunch(true /* isActivityIntent */); + ActivityStarter.OnDismissAction onDismissAction = new ActivityStarter.OnDismissAction() { + @Override + public boolean onDismiss() { + AsyncTask.execute(() -> { + TaskStackBuilder tsb = TaskStackBuilder.create(mContext); + for (Intent intent : intentInfo.getBackStack()) { + tsb.addNextIntent(intent); + } + tsb.addNextIntent(intentInfo.getTargetIntent()); + + ActivityTransitionAnimator.Controller viewController = + ActivityTransitionAnimator.Controller.fromView(view, + intentInfo.getCujType()); + ActivityTransitionAnimator.Controller animationController = + viewController == null ? null + : new StatusBarTransitionAnimatorController( + viewController, + mShadeAnimationInteractor, + mShadeController, + mNotificationShadeWindowController, + mCommandQueue, + mDisplayId, + true /* isActivityIntent */); + + mActivityTransitionAnimator.startIntentWithAnimation( + animationController, animate, intentInfo.getTargetIntent().getPackage(), + (adapter) -> tsb.startActivities( + getActivityOptions(mDisplayId, adapter), + mUserTracker.getUserHandle())); + }); + return true; + } + + @Override + public boolean willRunAnimationOnKeyguard() { + return animate; + } + }; + mActivityStarter.dismissKeyguardThenExecute(onDismissAction, null, + false /* afterKeyguardGone */); + } + private void removeHunAfterClick(ExpandableNotificationRow row) { String key = row.getEntry().getSbn().getKey(); if (mHeadsUpManager != null && mHeadsUpManager.isHeadsUpEntry(key)) { diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/pipeline/shared/ui/model/InternetTileIconModel.kt b/packages/SystemUI/src/com/android/systemui/statusbar/pipeline/shared/ui/model/InternetTileIconModel.kt new file mode 100644 index 000000000000..f8958e0d002f --- /dev/null +++ b/packages/SystemUI/src/com/android/systemui/statusbar/pipeline/shared/ui/model/InternetTileIconModel.kt @@ -0,0 +1,27 @@ +/* + * 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.systemui.statusbar.pipeline.shared.ui.model + +import com.android.systemui.common.shared.model.Icon + +sealed interface InternetTileIconModel { + data class ResourceId(val resId: Int) : InternetTileIconModel + + data class Cellular(val level: Int) : InternetTileIconModel + + data class Satellite(val resourceIcon: Icon.Resource) : InternetTileIconModel +} diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/ui/dialog/ModesDialogDelegate.kt b/packages/SystemUI/src/com/android/systemui/statusbar/policy/ui/dialog/ModesDialogDelegate.kt index e1dcc524c486..78edd3916a88 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/ui/dialog/ModesDialogDelegate.kt +++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/ui/dialog/ModesDialogDelegate.kt @@ -19,8 +19,10 @@ package com.android.systemui.statusbar.policy.ui.dialog import android.content.Intent import android.provider.Settings import android.util.Log +import androidx.compose.foundation.isSystemInDarkTheme import androidx.compose.material3.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.remember import androidx.compose.ui.Modifier import androidx.compose.ui.platform.testTag import androidx.compose.ui.res.stringResource @@ -30,6 +32,7 @@ import androidx.lifecycle.DefaultLifecycleObserver import androidx.lifecycle.LifecycleOwner import com.android.compose.PlatformButton import com.android.compose.PlatformOutlinedButton +import com.android.compose.theme.PlatformTheme import com.android.internal.annotations.VisibleForTesting import com.android.internal.jank.InteractionJankMonitor import com.android.systemui.animation.DialogCuj @@ -74,7 +77,7 @@ constructor( currentDialog?.dismiss() } - currentDialog = sysuiDialogFactory.create() { ModesDialogContent(it) } + currentDialog = sysuiDialogFactory.create { ModesDialogContent(it) } currentDialog ?.lifecycle ?.addObserver( @@ -91,28 +94,34 @@ constructor( @Composable private fun ModesDialogContent(dialog: SystemUIDialog) { - AlertDialogContent( - modifier = Modifier.semantics { - testTagsAsResourceId = true - }, - title = { - Text( - modifier = Modifier.testTag("modes_title"), - text = stringResource(R.string.zen_modes_dialog_title) - ) - }, - content = { ModeTileGrid(viewModel.get()) }, - neutralButton = { - PlatformOutlinedButton(onClick = { openSettings(dialog) }) { - Text(stringResource(R.string.zen_modes_dialog_settings)) - } - }, - positiveButton = { - PlatformButton(onClick = { dialog.dismiss() }) { - Text(stringResource(R.string.zen_modes_dialog_done)) - } - }, - ) + // TODO(b/369376884): The composable does correctly update when the theme changes + // while the dialog is open, but the background (which we don't control here) + // doesn't, which causes us to show things like white text on a white background. + // as a workaround, we remember the original theme and keep it on recomposition. + val isCurrentlyInDarkTheme = isSystemInDarkTheme() + val cachedDarkTheme = remember { isCurrentlyInDarkTheme } + PlatformTheme(isDarkTheme = cachedDarkTheme) { + AlertDialogContent( + modifier = Modifier.semantics { testTagsAsResourceId = true }, + title = { + Text( + modifier = Modifier.testTag("modes_title"), + text = stringResource(R.string.zen_modes_dialog_title), + ) + }, + content = { ModeTileGrid(viewModel.get()) }, + neutralButton = { + PlatformOutlinedButton(onClick = { openSettings(dialog) }) { + Text(stringResource(R.string.zen_modes_dialog_settings)) + } + }, + positiveButton = { + PlatformButton(onClick = { dialog.dismiss() }) { + Text(stringResource(R.string.zen_modes_dialog_done)) + } + }, + ) + } } @VisibleForTesting @@ -129,7 +138,7 @@ constructor( activityStarter.startActivity( ZEN_MODE_SETTINGS_INTENT, true /* dismissShade */, - animationController + animationController, ) } @@ -163,7 +172,7 @@ constructor( Log.w( TAG, "Cannot launch from dialog, the dialog is not present. " + - "Will launch activity without animating." + "Will launch activity without animating.", ) } @@ -172,11 +181,7 @@ constructor( if (animationController == null) { currentDialog?.dismiss() } - activityStarter.startActivity( - intent, - true, /* dismissShade */ - animationController, - ) + activityStarter.startActivity(intent, true, /* dismissShade */ animationController) } companion object { diff --git a/packages/SystemUI/src/com/android/systemui/touchpad/tutorial/ui/view/TouchpadTutorialActivity.kt b/packages/SystemUI/src/com/android/systemui/touchpad/tutorial/ui/view/TouchpadTutorialActivity.kt index d03b2e717398..e1f7bd59005c 100644 --- a/packages/SystemUI/src/com/android/systemui/touchpad/tutorial/ui/view/TouchpadTutorialActivity.kt +++ b/packages/SystemUI/src/com/android/systemui/touchpad/tutorial/ui/view/TouchpadTutorialActivity.kt @@ -29,6 +29,7 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle import com.android.compose.theme.PlatformTheme import com.android.systemui.inputdevice.tutorial.InputDeviceTutorialLogger import com.android.systemui.inputdevice.tutorial.InputDeviceTutorialLogger.TutorialContext +import com.android.systemui.inputdevice.tutorial.KeyboardTouchpadTutorialMetricsLogger import com.android.systemui.touchpad.tutorial.ui.composable.BackGestureTutorialScreen import com.android.systemui.touchpad.tutorial.ui.composable.HomeGestureTutorialScreen import com.android.systemui.touchpad.tutorial.ui.composable.RecentAppsGestureTutorialScreen @@ -45,6 +46,7 @@ class TouchpadTutorialActivity constructor( private val viewModelFactory: TouchpadTutorialViewModel.Factory, private val logger: InputDeviceTutorialLogger, + private val metricsLogger: KeyboardTouchpadTutorialMetricsLogger, ) : ComponentActivity() { private val vm by viewModels<TouchpadTutorialViewModel>(factoryProducer = { viewModelFactory }) @@ -57,6 +59,7 @@ constructor( } // required to handle 3+ fingers on touchpad window.addPrivateFlags(WindowManager.LayoutParams.PRIVATE_FLAG_TRUSTED_OVERLAY) + metricsLogger.logPeripheralTutorialLaunchedFromSettings() logger.logOpenTutorial(TutorialContext.TOUCHPAD_TUTORIAL) } @@ -85,7 +88,7 @@ fun TouchpadTutorialScreen(vm: TouchpadTutorialViewModel, closeTutorial: () -> U onBackTutorialClicked = { vm.goTo(BACK_GESTURE) }, onHomeTutorialClicked = { vm.goTo(HOME_GESTURE) }, onRecentAppsTutorialClicked = { vm.goTo(RECENT_APPS_GESTURE) }, - onDoneButtonClicked = closeTutorial + onDoneButtonClicked = closeTutorial, ) BACK_GESTURE -> BackGestureTutorialScreen( diff --git a/packages/SystemUI/tests/src/com/android/systemui/media/controls/domain/pipeline/MediaTimeoutListenerTest.kt b/packages/SystemUI/tests/src/com/android/systemui/media/controls/domain/pipeline/MediaTimeoutListenerTest.kt index 680df1584f89..dcf32a5f574d 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/media/controls/domain/pipeline/MediaTimeoutListenerTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/media/controls/domain/pipeline/MediaTimeoutListenerTest.kt @@ -137,7 +137,7 @@ class MediaTimeoutListenerTest : SysuiTestCase() { MediaTestUtils.emptyMediaData.copy( app = PACKAGE, packageName = PACKAGE, - token = session.sessionToken + token = session.sessionToken, ) resumeData = mediaData.copy(token = null, active = false, resumption = true) @@ -237,7 +237,7 @@ class MediaTimeoutListenerTest : SysuiTestCase() { // Assuming we're registered testOnMediaDataLoaded_registersPlaybackListener() - mediaCallbackCaptor.value.onPlaybackStateChanged( + onPlaybackStateChanged( PlaybackState.Builder().setState(PlaybackState.STATE_PAUSED, 0L, 0f).build() ) assertThat(mainExecutor.numPending()).isEqualTo(1) @@ -249,7 +249,7 @@ class MediaTimeoutListenerTest : SysuiTestCase() { // Assuming we have a pending timeout testOnPlaybackStateChanged_schedulesTimeout_whenPaused() - mediaCallbackCaptor.value.onPlaybackStateChanged( + onPlaybackStateChanged( PlaybackState.Builder().setState(PlaybackState.STATE_PLAYING, 0L, 0f).build() ) assertThat(mainExecutor.numPending()).isEqualTo(0) @@ -261,7 +261,7 @@ class MediaTimeoutListenerTest : SysuiTestCase() { // Assuming we have a pending timeout testOnPlaybackStateChanged_schedulesTimeout_whenPaused() - mediaCallbackCaptor.value.onPlaybackStateChanged( + onPlaybackStateChanged( PlaybackState.Builder().setState(PlaybackState.STATE_STOPPED, 0L, 0f).build() ) assertThat(mainExecutor.numPending()).isEqualTo(1) @@ -435,7 +435,7 @@ class MediaTimeoutListenerTest : SysuiTestCase() { // When the playback state changes, and has different actions val playingState = PlaybackState.Builder().setActions(PlaybackState.ACTION_PLAY).build() - mediaCallbackCaptor.value.onPlaybackStateChanged(playingState) + onPlaybackStateChanged(playingState) assertThat(uiExecutor.runAllReady()).isEqualTo(1) // Then the callback is invoked @@ -448,7 +448,7 @@ class MediaTimeoutListenerTest : SysuiTestCase() { PlaybackState.CustomAction.Builder( "ACTION_1", "custom action 1", - android.R.drawable.ic_media_ff + android.R.drawable.ic_media_ff, ) .build() val pausedState = @@ -463,7 +463,7 @@ class MediaTimeoutListenerTest : SysuiTestCase() { PlaybackState.CustomAction.Builder( "ACTION_2", "custom action 2", - android.R.drawable.ic_media_rew + android.R.drawable.ic_media_rew, ) .build() val pausedStateTwoActions = @@ -472,7 +472,7 @@ class MediaTimeoutListenerTest : SysuiTestCase() { .addCustomAction(customOne) .addCustomAction(customTwo) .build() - mediaCallbackCaptor.value.onPlaybackStateChanged(pausedStateTwoActions) + onPlaybackStateChanged(pausedStateTwoActions) assertThat(uiExecutor.runAllReady()).isEqualTo(1) // Then the callback is invoked @@ -485,7 +485,7 @@ class MediaTimeoutListenerTest : SysuiTestCase() { loadMediaDataWithPlaybackState(stateWithActions) // When the playback state updates with the same actions - mediaCallbackCaptor.value.onPlaybackStateChanged(stateWithActions) + onPlaybackStateChanged(stateWithActions) // Then the callback is not invoked again verify(stateCallback, never()).invoke(eq(KEY), any()) @@ -512,7 +512,7 @@ class MediaTimeoutListenerTest : SysuiTestCase() { .setActions(PlaybackState.ACTION_PAUSE) .addCustomAction(customTwo) .build() - mediaCallbackCaptor.value.onPlaybackStateChanged(stateTwo) + onPlaybackStateChanged(stateTwo) // Then the callback is not invoked verify(stateCallback, never()).invoke(eq(KEY), any()) @@ -544,7 +544,7 @@ class MediaTimeoutListenerTest : SysuiTestCase() { // When the playback state changes to playing val playingState = PlaybackState.Builder().setState(PlaybackState.STATE_PLAYING, 0L, 1f).build() - mediaCallbackCaptor.value.onPlaybackStateChanged(playingState) + onPlaybackStateChanged(playingState) uiExecutor.runAllReady() // Then the callback is invoked @@ -561,7 +561,7 @@ class MediaTimeoutListenerTest : SysuiTestCase() { // When the playback state is updated, but still not playing val playingState = PlaybackState.Builder().setState(PlaybackState.STATE_STOPPED, 0L, 0f).build() - mediaCallbackCaptor.value.onPlaybackStateChanged(playingState) + onPlaybackStateChanged(playingState) // Then the callback is not invoked verify(stateCallback, never()).invoke(eq(KEY), eq(playingState!!)) @@ -571,7 +571,7 @@ class MediaTimeoutListenerTest : SysuiTestCase() { fun testTimeoutCallback_dozedPastTimeout_invokedOnWakeup() { // When paused media is loaded testOnMediaDataLoaded_registersPlaybackListener() - mediaCallbackCaptor.value.onPlaybackStateChanged( + onPlaybackStateChanged( PlaybackState.Builder().setState(PlaybackState.STATE_PAUSED, 0L, 0f).build() ) verify(statusBarStateController).addCallback(capture(dozingCallbackCaptor)) @@ -597,7 +597,7 @@ class MediaTimeoutListenerTest : SysuiTestCase() { val time = clock.currentTimeMillis() clock.setElapsedRealtime(time) testOnMediaDataLoaded_registersPlaybackListener() - mediaCallbackCaptor.value.onPlaybackStateChanged( + onPlaybackStateChanged( PlaybackState.Builder().setState(PlaybackState.STATE_PAUSED, 0L, 0f).build() ) verify(statusBarStateController).addCallback(capture(dozingCallbackCaptor)) @@ -706,4 +706,9 @@ class MediaTimeoutListenerTest : SysuiTestCase() { bgExecutor.runAllReady() uiExecutor.runAllReady() } + + private fun onPlaybackStateChanged(state: PlaybackState) { + mediaCallbackCaptor.value.onPlaybackStateChanged(state) + bgExecutor.runAllReady() + } } diff --git a/packages/SystemUI/tests/src/com/android/systemui/qs/tiles/dialog/InternetDialogDelegateControllerTest.java b/packages/SystemUI/tests/src/com/android/systemui/qs/tiles/dialog/InternetDialogDelegateControllerTest.java index eea02eec7099..2f8f45cb0197 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/qs/tiles/dialog/InternetDialogDelegateControllerTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/qs/tiles/dialog/InternetDialogDelegateControllerTest.java @@ -29,6 +29,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.reset; import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -887,6 +888,34 @@ public class InternetDialogDelegateControllerTest extends SysuiTestCase { } @Test + public void getActiveAutoSwitchNonDdsSubId_registerCallbackForExistedSubId_notRegister() { + mFlags.set(Flags.QS_SECONDARY_DATA_SUB_INFO, true); + + // Adds non DDS subId + SubscriptionInfo info = mock(SubscriptionInfo.class); + doReturn(SUB_ID2).when(info).getSubscriptionId(); + doReturn(false).when(info).isOpportunistic(); + when(mSubscriptionManager.getActiveSubscriptionInfo(anyInt())).thenReturn(info); + + mInternetDialogController.getActiveAutoSwitchNonDdsSubId(); + + // 1st time is onStart(), 2nd time is getActiveAutoSwitchNonDdsSubId() + verify(mTelephonyManager, times(2)).registerTelephonyCallback(any(), any()); + assertThat(mInternetDialogController.mSubIdTelephonyCallbackMap.size() == 2); + + // Adds non DDS subId again + doReturn(SUB_ID2).when(info).getSubscriptionId(); + doReturn(false).when(info).isOpportunistic(); + when(mSubscriptionManager.getActiveSubscriptionInfo(anyInt())).thenReturn(info); + + mInternetDialogController.getActiveAutoSwitchNonDdsSubId(); + + // Does not add due to cached subInfo in mSubIdTelephonyCallbackMap. + verify(mTelephonyManager, times(2)).registerTelephonyCallback(any(), any()); + assertThat(mInternetDialogController.mSubIdTelephonyCallbackMap.size() == 2); + } + + @Test public void getMobileNetworkSummary() { mFlags.set(Flags.QS_SECONDARY_DATA_SUB_INFO, true); Resources res1 = mock(Resources.class); diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/NotificationSectionsFeatureManagerTest.kt b/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/NotificationSectionsFeatureManagerTest.kt index 0407fc14d35a..ac7388281a15 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/NotificationSectionsFeatureManagerTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/NotificationSectionsFeatureManagerTest.kt @@ -24,7 +24,7 @@ import androidx.test.filters.SmallTest import com.android.dx.mockito.inline.extended.ExtendedMockito import com.android.internal.config.sysui.SystemUiDeviceConfigFlags.NOTIFICATIONS_USE_PEOPLE_FILTERING import com.android.systemui.SysuiTestCase -import com.android.systemui.statusbar.notification.shared.NotificationMinimalismPrototype +import com.android.systemui.statusbar.notification.shared.NotificationMinimalism import com.android.systemui.statusbar.notification.shared.PriorityPeopleSection import com.android.systemui.util.DeviceConfigProxyFake import com.android.systemui.util.Utils @@ -42,7 +42,7 @@ import org.mockito.quality.Strictness @RunWith(AndroidJUnit4::class) @SmallTest // this class has no testable logic with either of these flags enabled -@DisableFlags(PriorityPeopleSection.FLAG_NAME, NotificationMinimalismPrototype.FLAG_NAME) +@DisableFlags(PriorityPeopleSection.FLAG_NAME, NotificationMinimalism.FLAG_NAME) class NotificationSectionsFeatureManagerTest : SysuiTestCase() { lateinit var manager: NotificationSectionsFeatureManager private val proxyFake = DeviceConfigProxyFake() diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/collection/coordinator/RemoteInputCoordinatorTest.kt b/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/collection/coordinator/RemoteInputCoordinatorTest.kt index deb3fc1224ce..a3f845225a99 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/collection/coordinator/RemoteInputCoordinatorTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/collection/coordinator/RemoteInputCoordinatorTest.kt @@ -15,8 +15,8 @@ */ package com.android.systemui.statusbar.notification.collection.coordinator -import android.app.Flags.lifetimeExtensionRefactor import android.app.Flags.FLAG_LIFETIME_EXTENSION_REFACTOR +import android.app.Flags.lifetimeExtensionRefactor import android.app.Notification import android.app.RemoteInputHistoryItem import android.os.Handler @@ -47,10 +47,10 @@ import org.junit.Before import org.junit.Test import org.junit.runner.RunWith import org.mockito.Mock -import org.mockito.Mockito.`when` import org.mockito.Mockito.never import org.mockito.Mockito.times import org.mockito.Mockito.verify +import org.mockito.Mockito.`when` import org.mockito.MockitoAnnotations.initMocks @SmallTest @@ -78,21 +78,20 @@ class RemoteInputCoordinatorTest : SysuiTestCase() { @Before fun setUp() { initMocks(this) - coordinator = RemoteInputCoordinator( + coordinator = + RemoteInputCoordinator( dumpManager, rebuilder, remoteInputManager, mainHandler, - smartReplyController - ) + smartReplyController, + ) `when`(pipeline.addNotificationLifetimeExtender(any())).thenAnswer { (it.arguments[0] as NotifLifetimeExtender).setCallback(lifetimeExtensionCallback) } `when`(pipeline.getInternalNotifUpdater(any())).thenReturn(notifUpdater) coordinator.attach(pipeline) - listener = withArgCaptor { - verify(remoteInputManager).setRemoteInputListener(capture()) - } + listener = withArgCaptor { verify(remoteInputManager).setRemoteInputListener(capture()) } entry1 = NotificationEntryBuilder().setId(1).build() entry2 = NotificationEntryBuilder().setId(2).build() `when`(rebuilder.rebuildForCanceledSmartReplies(any())).thenReturn(sbn) @@ -101,13 +100,17 @@ class RemoteInputCoordinatorTest : SysuiTestCase() { `when`(rebuilder.rebuildWithExistingReplies(any())).thenReturn(sbn) } - val remoteInputActiveExtender get() = coordinator.mRemoteInputActiveExtender - val remoteInputHistoryExtender get() = coordinator.mRemoteInputHistoryExtender - val smartReplyHistoryExtender get() = coordinator.mSmartReplyHistoryExtender + val remoteInputActiveExtender + get() = coordinator.mRemoteInputActiveExtender - val collectionListeners get() = captureMany { - verify(pipeline, times(1)).addCollectionListener(capture()) - } + val remoteInputHistoryExtender + get() = coordinator.mRemoteInputHistoryExtender + + val smartReplyHistoryExtender + get() = coordinator.mSmartReplyHistoryExtender + + val collectionListeners + get() = captureMany { verify(pipeline, times(1)).addCollectionListener(capture()) } @Test fun testRemoteInputActive() { @@ -179,7 +182,8 @@ class RemoteInputCoordinatorTest : SysuiTestCase() { @EnableFlags(FLAG_LIFETIME_EXTENSION_REFACTOR) fun testRemoteInputLifetimeExtensionListenerTrigger() { // Create notification with LIFETIME_EXTENDED_BY_DIRECT_REPLY flag. - val entry = NotificationEntryBuilder() + val entry = + NotificationEntryBuilder() .setId(3) .setTag("entry") .setFlag(mContext, Notification.FLAG_LIFETIME_EXTENDED_BY_DIRECT_REPLY, true) @@ -187,9 +191,7 @@ class RemoteInputCoordinatorTest : SysuiTestCase() { `when`(remoteInputManager.shouldKeepForRemoteInputHistory(entry)).thenReturn(true) `when`(remoteInputManager.shouldKeepForSmartReplyHistory(entry)).thenReturn(false) - collectionListeners.forEach { - it.onEntryUpdated(entry, true) - } + collectionListeners.forEach { it.onEntryUpdated(entry, true) } verify(rebuilder, times(1)).rebuildForRemoteInputReply(entry) } @@ -198,16 +200,15 @@ class RemoteInputCoordinatorTest : SysuiTestCase() { @EnableFlags(FLAG_LIFETIME_EXTENSION_REFACTOR) fun testSmartReplyLifetimeExtensionListenerTrigger() { // Create notification with LIFETIME_EXTENDED_BY_DIRECT_REPLY flag. - val entry = NotificationEntryBuilder() + val entry = + NotificationEntryBuilder() .setId(3) .setTag("entry") .setFlag(mContext, Notification.FLAG_LIFETIME_EXTENDED_BY_DIRECT_REPLY, true) .build() `when`(remoteInputManager.shouldKeepForRemoteInputHistory(entry)).thenReturn(false) `when`(remoteInputManager.shouldKeepForSmartReplyHistory(entry)).thenReturn(true) - collectionListeners.forEach { - it.onEntryUpdated(entry, true) - } + collectionListeners.forEach { it.onEntryUpdated(entry, true) } verify(rebuilder, times(1)).rebuildForCanceledSmartReplies(entry) verify(smartReplyController, times(1)).stopSending(entry) @@ -217,25 +218,25 @@ class RemoteInputCoordinatorTest : SysuiTestCase() { @EnableFlags(FLAG_LIFETIME_EXTENSION_REFACTOR) fun testRepeatedUpdateTriggersRebuild() { // Create notification with LIFETIME_EXTENDED_BY_DIRECT_REPLY flag. - val entry = NotificationEntryBuilder() + val entry = + NotificationEntryBuilder() .setId(3) .setTag("entry") .setFlag(mContext, Notification.FLAG_LIFETIME_EXTENDED_BY_DIRECT_REPLY, true) .build() `when`(remoteInputManager.shouldKeepForRemoteInputHistory(entry)).thenReturn(false) `when`(remoteInputManager.shouldKeepForSmartReplyHistory(entry)).thenReturn(false) - collectionListeners.forEach { - it.onEntryUpdated(entry, true) - } + collectionListeners.forEach { it.onEntryUpdated(entry, true) } - verify(rebuilder, times(1)).rebuildWithExistingReplies(entry) + verify(rebuilder, times(1)).rebuildForRemoteInputReply(entry) } @Test @EnableFlags(FLAG_LIFETIME_EXTENSION_REFACTOR) fun testLifetimeExtensionListenerClearsRemoteInputs() { // Create notification with LIFETIME_EXTENDED_BY_DIRECT_REPLY flag. - val entry = NotificationEntryBuilder() + val entry = + NotificationEntryBuilder() .setId(3) .setTag("entry") .setFlag(mContext, Notification.FLAG_LIFETIME_EXTENDED_BY_DIRECT_REPLY, false) @@ -245,9 +246,7 @@ class RemoteInputCoordinatorTest : SysuiTestCase() { `when`(remoteInputManager.shouldKeepForRemoteInputHistory(entry)).thenReturn(false) `when`(remoteInputManager.shouldKeepForSmartReplyHistory(entry)).thenReturn(false) - collectionListeners.forEach { - it.onEntryUpdated(entry, true) - } + collectionListeners.forEach { it.onEntryUpdated(entry, true) } assertThat(entry.remoteInputs).isNull() } diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/footer/ui/viewmodel/FooterViewModelTest.kt b/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/footer/ui/viewmodel/FooterViewModelTest.kt index 83ad18b6468b..46f3a6b66429 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/footer/ui/viewmodel/FooterViewModelTest.kt +++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/footer/ui/viewmodel/FooterViewModelTest.kt @@ -38,6 +38,7 @@ import com.android.systemui.shade.shadeTestUtil import com.android.systemui.shared.settings.data.repository.fakeSecureSettingsRepository import com.android.systemui.statusbar.notification.collection.render.NotifStats import com.android.systemui.statusbar.notification.data.repository.activeNotificationListRepository +import com.android.systemui.statusbar.notification.emptyshade.shared.ModesEmptyShadeFix import com.android.systemui.statusbar.notification.footer.shared.FooterViewRefactor import com.android.systemui.testKosmos import com.android.systemui.util.ui.isAnimating @@ -254,6 +255,39 @@ class FooterViewModelTest(flags: FlagsParameterization) : SysuiTestCase() { assertThat(buttonLabel).isEqualTo(R.string.manage_notifications_history_text) } + @EnableFlags(ModesEmptyShadeFix.FLAG_NAME) + @Test + fun manageButtonOnClick_whenHistoryDisabled() = + testScope.runTest { + val onClick by collectLastValue(underTest.manageOrHistoryButtonClick) + runCurrent() + + // WHEN notification history is disabled + fakeSecureSettingsRepository.setInt(Settings.Secure.NOTIFICATION_HISTORY_ENABLED, 0) + + // THEN onClick leads to settings page + assertThat(onClick?.targetIntent?.action) + .isEqualTo(Settings.ACTION_NOTIFICATION_SETTINGS) + assertThat(onClick?.backStack).isEmpty() + } + + @EnableFlags(ModesEmptyShadeFix.FLAG_NAME) + @Test + fun historyButtonOnClick_whenHistoryEnabled() = + testScope.runTest { + val onClick by collectLastValue(underTest.manageOrHistoryButtonClick) + runCurrent() + + // WHEN notification history is enabled + fakeSecureSettingsRepository.setInt(Settings.Secure.NOTIFICATION_HISTORY_ENABLED, 1) + + // THEN onClick leads to history page + assertThat(onClick?.targetIntent?.action) + .isEqualTo(Settings.ACTION_NOTIFICATION_HISTORY) + assertThat(onClick?.backStack?.map { it.action }) + .containsExactly(Settings.ACTION_NOTIFICATION_SETTINGS) + } + @Test fun manageButtonVisible_whenMessageVisible() = testScope.runTest { diff --git a/packages/SystemUI/tests/utils/src/com/android/systemui/keyguard/domain/interactor/KeyguardDismissActionInteractorKosmos.kt b/packages/SystemUI/tests/utils/src/com/android/systemui/keyguard/domain/interactor/KeyguardDismissActionInteractorKosmos.kt index e2b283b06562..38bc758232a2 100644 --- a/packages/SystemUI/tests/utils/src/com/android/systemui/keyguard/domain/interactor/KeyguardDismissActionInteractorKosmos.kt +++ b/packages/SystemUI/tests/utils/src/com/android/systemui/keyguard/domain/interactor/KeyguardDismissActionInteractorKosmos.kt @@ -22,7 +22,6 @@ import com.android.systemui.keyguard.data.repository.keyguardRepository import com.android.systemui.kosmos.Kosmos import com.android.systemui.kosmos.testScope import com.android.systemui.power.domain.interactor.powerInteractor -import com.android.systemui.scene.domain.interactor.sceneInteractor import com.android.systemui.shade.domain.interactor.shadeInteractor import kotlinx.coroutines.ExperimentalCoroutinesApi @@ -34,7 +33,6 @@ val Kosmos.keyguardDismissActionInteractor by transitionInteractor = keyguardTransitionInteractor, dismissInteractor = keyguardDismissInteractor, applicationScope = testScope.backgroundScope, - sceneInteractor = { sceneInteractor }, deviceUnlockedInteractor = { deviceUnlockedInteractor }, powerInteractor = powerInteractor, alternateBouncerInteractor = alternateBouncerInteractor, diff --git a/packages/SystemUI/tests/utils/src/com/android/systemui/qs/ui/viewmodel/QuickSettingsShadeOverlayContentViewModelKosmos.kt b/packages/SystemUI/tests/utils/src/com/android/systemui/qs/ui/viewmodel/QuickSettingsShadeOverlayContentViewModelKosmos.kt index a80a4095a264..6540ed6bba45 100644 --- a/packages/SystemUI/tests/utils/src/com/android/systemui/qs/ui/viewmodel/QuickSettingsShadeOverlayContentViewModelKosmos.kt +++ b/packages/SystemUI/tests/utils/src/com/android/systemui/qs/ui/viewmodel/QuickSettingsShadeOverlayContentViewModelKosmos.kt @@ -17,6 +17,7 @@ package com.android.systemui.qs.ui.viewmodel import com.android.systemui.kosmos.Kosmos +import com.android.systemui.scene.domain.interactor.sceneInteractor import com.android.systemui.shade.domain.interactor.shadeInteractor import com.android.systemui.shade.ui.viewmodel.shadeHeaderViewModelFactory @@ -24,6 +25,7 @@ val Kosmos.quickSettingsShadeOverlayContentViewModel: QuickSettingsShadeOverlayC Kosmos.Fixture { QuickSettingsShadeOverlayContentViewModel( shadeInteractor = shadeInteractor, + sceneInteractor = sceneInteractor, shadeHeaderViewModelFactory = shadeHeaderViewModelFactory, quickSettingsContainerViewModel = quickSettingsContainerViewModel, ) diff --git a/packages/SystemUI/tests/utils/src/com/android/systemui/shade/ui/viewmodel/NotificationsShadeOverlayContentViewModelKosmos.kt b/packages/SystemUI/tests/utils/src/com/android/systemui/shade/ui/viewmodel/NotificationsShadeOverlayContentViewModelKosmos.kt index 7a15fdf95734..718347fc3490 100644 --- a/packages/SystemUI/tests/utils/src/com/android/systemui/shade/ui/viewmodel/NotificationsShadeOverlayContentViewModelKosmos.kt +++ b/packages/SystemUI/tests/utils/src/com/android/systemui/shade/ui/viewmodel/NotificationsShadeOverlayContentViewModelKosmos.kt @@ -19,6 +19,7 @@ package com.android.systemui.shade.ui.viewmodel import com.android.systemui.kosmos.Kosmos import com.android.systemui.kosmos.Kosmos.Fixture import com.android.systemui.notifications.ui.viewmodel.NotificationsShadeOverlayContentViewModel +import com.android.systemui.scene.domain.interactor.sceneInteractor import com.android.systemui.shade.domain.interactor.shadeInteractor import com.android.systemui.statusbar.notification.stack.ui.viewmodel.notificationsPlaceholderViewModelFactory @@ -27,6 +28,7 @@ val Kosmos.notificationsShadeOverlayContentViewModel: NotificationsShadeOverlayContentViewModel( shadeHeaderViewModelFactory = shadeHeaderViewModelFactory, notificationsPlaceholderViewModelFactory = notificationsPlaceholderViewModelFactory, + sceneInteractor = sceneInteractor, shadeInteractor = shadeInteractor, ) } diff --git a/packages/SystemUI/tests/utils/src/com/android/systemui/statusbar/notification/domain/interactor/SeenNotificationsInteractorKosmos.kt b/packages/SystemUI/tests/utils/src/com/android/systemui/statusbar/notification/domain/interactor/SeenNotificationsInteractorKosmos.kt index b19e221d099c..3d2bd6cf49d4 100644 --- a/packages/SystemUI/tests/utils/src/com/android/systemui/statusbar/notification/domain/interactor/SeenNotificationsInteractorKosmos.kt +++ b/packages/SystemUI/tests/utils/src/com/android/systemui/statusbar/notification/domain/interactor/SeenNotificationsInteractorKosmos.kt @@ -45,3 +45,17 @@ var Kosmos.lockScreenShowOnlyUnseenNotificationsSetting: Boolean UserHandle.USER_CURRENT, ) } + +var Kosmos.lockScreenNotificationMinimalismSetting: Boolean + get() = + fakeSettings.getIntForUser( + Settings.Secure.LOCK_SCREEN_NOTIFICATION_MINIMALISM, + UserHandle.USER_CURRENT, + ) == 1 + set(value) { + fakeSettings.putIntForUser( + Settings.Secure.LOCK_SCREEN_NOTIFICATION_MINIMALISM, + if (value) 1 else 0, + UserHandle.USER_CURRENT, + ) + } diff --git a/services/appfunctions/java/com/android/server/appfunctions/AppFunctionManagerServiceImpl.java b/services/appfunctions/java/com/android/server/appfunctions/AppFunctionManagerServiceImpl.java index c8f8c2a6b223..082459b8c863 100644 --- a/services/appfunctions/java/com/android/server/appfunctions/AppFunctionManagerServiceImpl.java +++ b/services/appfunctions/java/com/android/server/appfunctions/AppFunctionManagerServiceImpl.java @@ -21,11 +21,13 @@ import static com.android.server.appfunctions.AppFunctionExecutors.THREAD_POOL_E import android.annotation.NonNull; import android.annotation.Nullable; import android.annotation.WorkerThread; +import android.app.appfunctions.AppFunctionService; import android.app.appfunctions.AppFunctionStaticMetadataHelper; import android.app.appfunctions.ExecuteAppFunctionAidlRequest; import android.app.appfunctions.ExecuteAppFunctionResponse; import android.app.appfunctions.IAppFunctionManager; import android.app.appfunctions.IAppFunctionService; +import android.app.appfunctions.ICancellationCallback; import android.app.appfunctions.IExecuteAppFunctionCallback; import android.app.appfunctions.SafeOneTimeExecuteAppFunctionCallback; import android.app.appsearch.AppSearchManager; @@ -37,11 +39,15 @@ import android.app.appsearch.observer.SchemaChangeInfo; import android.content.Context; import android.content.Intent; import android.os.Binder; +import android.os.CancellationSignal; +import android.os.IBinder; +import android.os.ICancellationSignal; import android.os.UserHandle; import android.text.TextUtils; import android.util.Slog; import com.android.internal.annotations.VisibleForTesting; +import com.android.internal.infra.AndroidFuture; import com.android.server.SystemService.TargetUser; import com.android.server.appfunctions.RemoteServiceCaller.RunServiceCallCallback; import com.android.server.appfunctions.RemoteServiceCaller.ServiceUsageCompleteListener; @@ -99,7 +105,7 @@ public class AppFunctionManagerServiceImpl extends IAppFunctionManager.Stub { } @Override - public void executeAppFunction( + public ICancellationSignal executeAppFunction( @NonNull ExecuteAppFunctionAidlRequest requestInternal, @NonNull IExecuteAppFunctionCallback executeAppFunctionCallback) { Objects.requireNonNull(requestInternal); @@ -120,11 +126,14 @@ public class AppFunctionManagerServiceImpl extends IAppFunctionManager.Stub { ExecuteAppFunctionResponse.RESULT_DENIED, exception.getMessage(), /* extras= */ null)); - return; + return null; } int callingUid = Binder.getCallingUid(); - int callingPid = Binder.getCallingUid(); + int callingPid = Binder.getCallingPid(); + + ICancellationSignal localCancelTransport = CancellationSignal.createTransport(); + THREAD_POOL_EXECUTOR.execute( () -> { try { @@ -132,12 +141,14 @@ public class AppFunctionManagerServiceImpl extends IAppFunctionManager.Stub { requestInternal, callingUid, callingPid, + localCancelTransport, safeExecuteAppFunctionCallback); } catch (Exception e) { safeExecuteAppFunctionCallback.onResult( mapExceptionToExecuteAppFunctionResponse(e)); } }); + return localCancelTransport; } @WorkerThread @@ -145,6 +156,7 @@ public class AppFunctionManagerServiceImpl extends IAppFunctionManager.Stub { ExecuteAppFunctionAidlRequest requestInternal, int callingUid, int callingPid, + ICancellationSignal localCancelTransport, SafeOneTimeExecuteAppFunctionCallback safeExecuteAppFunctionCallback) { UserHandle targetUser = requestInternal.getUserHandle(); // TODO(b/354956319): Add and honor the new enterprise policies. @@ -203,6 +215,7 @@ public class AppFunctionManagerServiceImpl extends IAppFunctionManager.Stub { requestInternal, serviceIntent, targetUser, + localCancelTransport, safeExecuteAppFunctionCallback, /* bindFlags= */ Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE); @@ -219,8 +232,19 @@ public class AppFunctionManagerServiceImpl extends IAppFunctionManager.Stub { @NonNull ExecuteAppFunctionAidlRequest requestInternal, @NonNull Intent serviceIntent, @NonNull UserHandle targetUser, + @NonNull ICancellationSignal cancellationSignalTransport, @NonNull SafeOneTimeExecuteAppFunctionCallback safeExecuteAppFunctionCallback, int bindFlags) { + CancellationSignal cancellationSignal = + CancellationSignal.fromTransport(cancellationSignalTransport); + ICancellationCallback cancellationCallback = + new ICancellationCallback.Stub() { + @Override + public void sendCancellationTransport( + @NonNull ICancellationSignal cancellationTransport) { + cancellationSignal.setRemote(cancellationTransport); + } + }; boolean bindServiceResult = mRemoteServiceCaller.runServiceCall( serviceIntent, @@ -236,6 +260,7 @@ public class AppFunctionManagerServiceImpl extends IAppFunctionManager.Stub { try { service.executeAppFunction( requestInternal.getClientRequest(), + cancellationCallback, new IExecuteAppFunctionCallback.Stub() { @Override public void onResult( diff --git a/services/core/java/android/os/BatteryStatsInternal.java b/services/core/java/android/os/BatteryStatsInternal.java index 0713999d4354..60b826b50045 100644 --- a/services/core/java/android/os/BatteryStatsInternal.java +++ b/services/core/java/android/os/BatteryStatsInternal.java @@ -41,6 +41,7 @@ public abstract class BatteryStatsInternal { public static final int CPU_WAKEUP_SUBSYSTEM_SOUND_TRIGGER = 3; public static final int CPU_WAKEUP_SUBSYSTEM_SENSOR = 4; public static final int CPU_WAKEUP_SUBSYSTEM_CELLULAR_DATA = 5; + public static final int CPU_WAKEUP_SUBSYSTEM_BLUETOOTH = 6; /** @hide */ @IntDef(prefix = {"CPU_WAKEUP_SUBSYSTEM_"}, value = { @@ -50,6 +51,7 @@ public abstract class BatteryStatsInternal { CPU_WAKEUP_SUBSYSTEM_SOUND_TRIGGER, CPU_WAKEUP_SUBSYSTEM_SENSOR, CPU_WAKEUP_SUBSYSTEM_CELLULAR_DATA, + CPU_WAKEUP_SUBSYSTEM_BLUETOOTH, }) @Retention(RetentionPolicy.SOURCE) public @interface CpuWakeupSubsystem { @@ -99,6 +101,14 @@ public abstract class BatteryStatsInternal { public abstract void noteCpuWakingNetworkPacket(Network network, long elapsedMillis, int uid); /** + * Informs battery stats of a sysproxy packet that woke up the CPU + * + * @param uid The uid that received the packet. + * @param elapsedMillis The time of the packet's arrival in elapsed timebase. + */ + public abstract void noteCpuWakingBluetoothProxyPacket(int uid, long elapsedMillis); + + /** * Informs battery stats of binder stats for the given work source UID. */ public abstract void noteBinderCallStats(int workSourceUid, long incrementalBinderCallCount, diff --git a/services/core/java/com/android/server/StorageManagerService.java b/services/core/java/com/android/server/StorageManagerService.java index d86bae19f174..e64a4803b14f 100644 --- a/services/core/java/com/android/server/StorageManagerService.java +++ b/services/core/java/com/android/server/StorageManagerService.java @@ -219,7 +219,7 @@ class StorageManagerService extends IStorageManager.Stub public static final int FAILED_MOUNT_RESET_TIMEOUT_SECONDS = 10; /** Extended timeout for the system server watchdog. */ - private static final int SLOW_OPERATION_WATCHDOG_TIMEOUT_MS = 20 * 1000; + private static final int SLOW_OPERATION_WATCHDOG_TIMEOUT_MS = 30 * 1000; /** Extended timeout for the system server watchdog for vold#partition operation. */ private static final int PARTITION_OPERATION_WATCHDOG_TIMEOUT_MS = 3 * 60 * 1000; @@ -3251,7 +3251,7 @@ class StorageManagerService extends IStorageManager.Stub if (Binder.getCallingUid() != android.os.Process.SYSTEM_UID) { throw new SecurityException("no permission to commit checkpoint changes"); } - + extendWatchdogTimeout("vold#commitChanges might be slow"); mVold.commitChanges(); } diff --git a/services/core/java/com/android/server/am/BatteryStatsService.java b/services/core/java/com/android/server/am/BatteryStatsService.java index 15277cebac6e..ef82c7477558 100644 --- a/services/core/java/com/android/server/am/BatteryStatsService.java +++ b/services/core/java/com/android/server/am/BatteryStatsService.java @@ -664,6 +664,8 @@ public final class BatteryStatsService extends IBatteryStats.Stub } else if (nc.hasTransport(TRANSPORT_CELLULAR)) { return CPU_WAKEUP_SUBSYSTEM_CELLULAR_DATA; } + // For TRANSPORT_BLUETOOTH, we have a separate channel to catch Bluetooth wakeups. + // See noteCpuWakingSysproxyPacket method. return CPU_WAKEUP_SUBSYSTEM_UNKNOWN; } @@ -686,6 +688,15 @@ public final class BatteryStatsService extends IBatteryStats.Stub } @Override + public void noteCpuWakingBluetoothProxyPacket(int uid, long elapsedMillis) { + if (uid < 0) { + Slog.e(TAG, "Invalid uid for waking bluetooth proxy packet: " + uid); + return; + } + noteCpuWakingActivity(CPU_WAKEUP_SUBSYSTEM_BLUETOOTH, elapsedMillis, uid); + } + + @Override public void noteBinderCallStats(int workSourceUid, long incrementatCallCount, Collection<BinderCallsStats.CallStat> callStats) { synchronized (BatteryStatsService.this.mLock) { diff --git a/services/core/java/com/android/server/appop/AppOpsService.java b/services/core/java/com/android/server/appop/AppOpsService.java index a6389f7f5311..e0cf96fbccd0 100644 --- a/services/core/java/com/android/server/appop/AppOpsService.java +++ b/services/core/java/com/android/server/appop/AppOpsService.java @@ -1031,6 +1031,9 @@ public class AppOpsService extends IAppOpsService.Stub { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); + if (action == null) { + return; + } String pkgName = intent.getData().getEncodedSchemeSpecificPart().intern(); int uid = intent.getIntExtra(Intent.EXTRA_UID, Process.INVALID_UID); diff --git a/services/core/java/com/android/server/display/DisplayManagerService.java b/services/core/java/com/android/server/display/DisplayManagerService.java index 88907e35854f..1f9eb082aaf4 100644 --- a/services/core/java/com/android/server/display/DisplayManagerService.java +++ b/services/core/java/com/android/server/display/DisplayManagerService.java @@ -3377,10 +3377,18 @@ public final class DisplayManagerService extends SystemService { private void dumpInternal(PrintWriter pw) { pw.println("DISPLAY MANAGER (dumpsys display)"); BrightnessTracker brightnessTrackerLocal; + SparseArray<DisplayPowerController> displayPowerControllersLocal = new SparseArray<>(); + int displayPowerControllerCount; synchronized (mSyncRoot) { brightnessTrackerLocal = mBrightnessTracker; + displayPowerControllerCount = mDisplayPowerControllers.size(); + for (int i = 0; i < displayPowerControllerCount; i++) { + displayPowerControllersLocal.put( + mDisplayPowerControllers.keyAt(i), mDisplayPowerControllers.valueAt(i)); + } + pw.println(" mSafeMode=" + mSafeMode); pw.println(" mPendingTraversal=" + mPendingTraversal); pw.println(" mViewports=" + mViewports); @@ -3451,13 +3459,6 @@ public final class DisplayManagerService extends SystemService { + ", mWifiDisplayScanRequested=" + callback.mWifiDisplayScanRequested); } - final int displayPowerControllerCount = mDisplayPowerControllers.size(); - pw.println(); - pw.println("Display Power Controllers: size=" + displayPowerControllerCount); - for (int i = 0; i < displayPowerControllerCount; i++) { - mDisplayPowerControllers.valueAt(i).dump(pw); - } - pw.println(); mPersistentDataStore.dump(pw); @@ -3470,6 +3471,12 @@ public final class DisplayManagerService extends SystemService { mDisplayWindowPolicyControllers.valueAt(i).second.dump(" ", pw); } } + pw.println(); + pw.println("Display Power Controllers: size=" + displayPowerControllerCount); + for (int i = 0; i < displayPowerControllerCount; i++) { + displayPowerControllersLocal.valueAt(i).dump(pw); + } + if (brightnessTrackerLocal != null) { pw.println(); brightnessTrackerLocal.dump(pw); diff --git a/services/core/java/com/android/server/flags/pinner.aconfig b/services/core/java/com/android/server/flags/pinner.aconfig index 2f817dbb9a7f..345366882d5a 100644 --- a/services/core/java/com/android/server/flags/pinner.aconfig +++ b/services/core/java/com/android/server/flags/pinner.aconfig @@ -9,8 +9,11 @@ flag { } flag { - name: "skip_home_art_pins" - namespace: "system_performance" - description: "Ablation study flag that controls if home app odex/vdex files should be pinned in memory." - bug: "340935152" -}
\ No newline at end of file + name: "pin_global_quota" + namespace: "system_performance" + description: "This flag controls whether pinner will use a global quota or not" + bug: "340935152" + metadata { + purpose: PURPOSE_BUGFIX + } +} diff --git a/services/core/java/com/android/server/inputmethod/HardwareKeyboardShortcutController.java b/services/core/java/com/android/server/inputmethod/HardwareKeyboardShortcutController.java index 41313fa1fb2c..ef1220fb1786 100644 --- a/services/core/java/com/android/server/inputmethod/HardwareKeyboardShortcutController.java +++ b/services/core/java/com/android/server/inputmethod/HardwareKeyboardShortcutController.java @@ -33,9 +33,6 @@ final class HardwareKeyboardShortcutController { @GuardedBy("ImfLock.class") private final ArrayList<InputMethodSubtypeHandle> mSubtypeHandles = new ArrayList<>(); - HardwareKeyboardShortcutController() { - } - @GuardedBy("ImfLock.class") void update(@NonNull InputMethodSettings settings) { mSubtypeHandles.clear(); diff --git a/services/core/java/com/android/server/inputmethod/InputMethodDeviceConfigs.java b/services/core/java/com/android/server/inputmethod/InputMethodDeviceConfigs.java index 6cd2493cfdff..fc4c0fc798db 100644 --- a/services/core/java/com/android/server/inputmethod/InputMethodDeviceConfigs.java +++ b/services/core/java/com/android/server/inputmethod/InputMethodDeviceConfigs.java @@ -40,6 +40,7 @@ final class InputMethodDeviceConfigs { if (KEY_HIDE_IME_WHEN_NO_EDITOR_FOCUS.equals(name)) { mHideImeWhenNoEditorFocus = properties.getBoolean(name, true /* defaultValue */); + break; } } }; diff --git a/services/core/java/com/android/server/inputmethod/ZeroJankProxy.java b/services/core/java/com/android/server/inputmethod/ZeroJankProxy.java index 214aa1d904fa..49d4332d9e2a 100644 --- a/services/core/java/com/android/server/inputmethod/ZeroJankProxy.java +++ b/services/core/java/com/android/server/inputmethod/ZeroJankProxy.java @@ -394,6 +394,7 @@ final class ZeroJankProxy implements IInputMethodManagerImpl.Callback { flags), this::offload).get(); } catch (InterruptedException e) { + Thread.currentThread().interrupt(); throw new RuntimeException(e); } catch (ExecutionException e) { throw new RuntimeException(e); diff --git a/services/core/java/com/android/server/media/projection/MediaProjectionManagerService.java b/services/core/java/com/android/server/media/projection/MediaProjectionManagerService.java index 48d24f2e14dd..47f579db604f 100644 --- a/services/core/java/com/android/server/media/projection/MediaProjectionManagerService.java +++ b/services/core/java/com/android/server/media/projection/MediaProjectionManagerService.java @@ -195,10 +195,9 @@ public final class MediaProjectionManagerService extends SystemService == PackageManager.PERMISSION_GRANTED) { return true; } - boolean operationActive = mAppOps.isOperationActive(AppOpsManager.OP_PROJECT_MEDIA, - mProjectionGrant.uid, - mProjectionGrant.packageName); - if (operationActive) { + if (AppOpsManager.MODE_ALLOWED == mAppOps.noteOpNoThrow(AppOpsManager.OP_PROJECT_MEDIA, + mProjectionGrant.uid, mProjectionGrant.packageName, /* attributionTag= */ null, + "recording lockscreen")) { // Some tools use media projection by granting the OP_PROJECT_MEDIA app // op via a shell command. Those tools can be granted keyguard capture return true; diff --git a/services/core/java/com/android/server/notification/NotificationManagerService.java b/services/core/java/com/android/server/notification/NotificationManagerService.java index c3a714b0eef0..655f2e4596aa 100644 --- a/services/core/java/com/android/server/notification/NotificationManagerService.java +++ b/services/core/java/com/android/server/notification/NotificationManagerService.java @@ -2579,6 +2579,7 @@ public class NotificationManagerService extends SystemService { mNotificationChannelLogger, mAppOps, mUserProfiles, + mUgmInternal, mShowReviewPermissionsNotification, Clock.systemUTC()); mRankingHelper = new RankingHelper(getContext(), mRankingHandler, mPreferencesHelper, @@ -6247,6 +6248,7 @@ public class NotificationManagerService extends SystemService { int callingUid = Binder.getCallingUid(); @ZenModeConfig.ConfigOrigin int origin = computeZenOrigin(fromUser); + boolean isSystemCaller = isCallerSystemOrSystemUiOrShell(); boolean shouldApplyAsImplicitRule = android.app.Flags.modesApi() && !canManageGlobalZenPolicy(pkg, callingUid); @@ -6283,11 +6285,33 @@ public class NotificationManagerService extends SystemService { policy.priorityCallSenders, policy.priorityMessageSenders, policy.suppressedVisualEffects, currPolicy.priorityConversationSenders); } + int newVisualEffects = calculateSuppressedVisualEffects( policy, currPolicy, applicationInfo.targetSdkVersion); - policy = new Policy(policy.priorityCategories, - policy.priorityCallSenders, policy.priorityMessageSenders, - newVisualEffects, policy.priorityConversationSenders); + + if (android.app.Flags.modesUi()) { + // 1. Callers should not modify STATE_CHANNELS_BYPASSING_DND, which is + // internally calculated and only indicates whether channels that want to bypass + // DND _exist_. + // 2. Only system callers should modify STATE_PRIORITY_CHANNELS_BLOCKED because + // it is @hide. + // 3. If the policy has been modified by the targetSdkVersion checks above then + // it has lost its state flags and that's fine (STATE_PRIORITY_CHANNELS_BLOCKED + // didn't exist until V). + int newState = Policy.STATE_UNSET; + if (isSystemCaller && policy.state != Policy.STATE_UNSET) { + newState = Policy.policyState( + currPolicy.hasPriorityChannels(), + policy.allowPriorityChannels()); + } + policy = new Policy(policy.priorityCategories, + policy.priorityCallSenders, policy.priorityMessageSenders, + newVisualEffects, newState, policy.priorityConversationSenders); + } else { + policy = new Policy(policy.priorityCategories, + policy.priorityCallSenders, policy.priorityMessageSenders, + newVisualEffects, policy.priorityConversationSenders); + } if (shouldApplyAsImplicitRule) { mZenModeHelper.applyGlobalPolicyAsImplicitZenRule(pkg, callingUid, policy); @@ -6672,13 +6696,7 @@ public class NotificationManagerService extends SystemService { final Uri originalSoundUri = (originalChannel != null) ? originalChannel.getSound() : null; if (soundUri != null && !Objects.equals(originalSoundUri, soundUri)) { - Binder.withCleanCallingIdentity(() -> { - mUgmInternal.checkGrantUriPermission(sourceUid, null, - ContentProvider.getUriWithoutUserId(soundUri), - Intent.FLAG_GRANT_READ_URI_PERMISSION, - ContentProvider.getUserIdFromUri(soundUri, - UserHandle.getUserId(sourceUid))); - }); + PermissionHelper.grantUriPermission(mUgmInternal, soundUri, sourceUid); } } diff --git a/services/core/java/com/android/server/notification/NotificationRecord.java b/services/core/java/com/android/server/notification/NotificationRecord.java index b9f0968b5864..3ba93845a290 100644 --- a/services/core/java/com/android/server/notification/NotificationRecord.java +++ b/services/core/java/com/android/server/notification/NotificationRecord.java @@ -1493,14 +1493,23 @@ public final class NotificationRecord { final Notification notification = getNotification(); notification.visitUris((uri) -> { - visitGrantableUri(uri, false, false); + if (com.android.server.notification.Flags.notificationVerifyChannelSoundUri()) { + visitGrantableUri(uri, false, false); + } else { + oldVisitGrantableUri(uri, false, false); + } }); if (notification.getChannelId() != null) { NotificationChannel channel = getChannel(); if (channel != null) { - visitGrantableUri(channel.getSound(), (channel.getUserLockedFields() - & NotificationChannel.USER_LOCKED_SOUND) != 0, true); + if (com.android.server.notification.Flags.notificationVerifyChannelSoundUri()) { + visitGrantableUri(channel.getSound(), (channel.getUserLockedFields() + & NotificationChannel.USER_LOCKED_SOUND) != 0, true); + } else { + oldVisitGrantableUri(channel.getSound(), (channel.getUserLockedFields() + & NotificationChannel.USER_LOCKED_SOUND) != 0, true); + } } } } finally { @@ -1516,7 +1525,7 @@ public final class NotificationRecord { * {@link #mGrantableUris}. Otherwise, this will either log or throw * {@link SecurityException} depending on target SDK of enqueuing app. */ - private void visitGrantableUri(Uri uri, boolean userOverriddenUri, boolean isSound) { + private void oldVisitGrantableUri(Uri uri, boolean userOverriddenUri, boolean isSound) { if (uri == null || !ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) return; if (mGrantableUris != null && mGrantableUris.contains(uri)) { @@ -1555,6 +1564,45 @@ public final class NotificationRecord { } } + /** + * Note the presence of a {@link Uri} that should have permission granted to + * whoever will be rendering it. + * <p> + * If the enqueuing app has the ability to grant access, it will be added to + * {@link #mGrantableUris}. Otherwise, this will either log or throw + * {@link SecurityException} depending on target SDK of enqueuing app. + */ + private void visitGrantableUri(Uri uri, boolean userOverriddenUri, + boolean isSound) { + if (mGrantableUris != null && mGrantableUris.contains(uri)) { + return; // already verified this URI + } + + final int sourceUid = getSbn().getUid(); + try { + PermissionHelper.grantUriPermission(mUgmInternal, uri, sourceUid); + + if (mGrantableUris == null) { + mGrantableUris = new ArraySet<>(); + } + mGrantableUris.add(uri); + } catch (SecurityException e) { + if (!userOverriddenUri) { + if (isSound) { + mSound = Settings.System.DEFAULT_NOTIFICATION_URI; + Log.w(TAG, "Replacing " + uri + " from " + sourceUid + ": " + e.getMessage()); + } else { + if (mTargetSdkVersion >= Build.VERSION_CODES.P) { + throw e; + } else { + Log.w(TAG, + "Ignoring " + uri + " from " + sourceUid + ": " + e.getMessage()); + } + } + } + } + } + public LogMaker getLogMaker(long now) { LogMaker lm = getSbn().getLogMaker() .addTaggedData(MetricsEvent.FIELD_NOTIFICATION_CHANNEL_IMPORTANCE, mImportance) diff --git a/services/core/java/com/android/server/notification/PermissionHelper.java b/services/core/java/com/android/server/notification/PermissionHelper.java index b6f48890c528..1464d481311a 100644 --- a/services/core/java/com/android/server/notification/PermissionHelper.java +++ b/services/core/java/com/android/server/notification/PermissionHelper.java @@ -25,19 +25,25 @@ import android.Manifest; import android.annotation.NonNull; import android.annotation.UserIdInt; import android.companion.virtual.VirtualDeviceManager; +import android.content.ContentProvider; +import android.content.ContentResolver; import android.content.Context; +import android.content.Intent; import android.content.pm.IPackageManager; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.ParceledListSlice; +import android.net.Uri; import android.os.Binder; import android.os.RemoteException; +import android.os.UserHandle; import android.permission.IPermissionManager; import android.util.ArrayMap; import android.util.Pair; import android.util.Slog; import com.android.internal.util.ArrayUtils; +import com.android.server.uri.UriGrantsManagerInternal; import java.util.Collections; import java.util.HashSet; @@ -58,7 +64,7 @@ public final class PermissionHelper { private final IPermissionManager mPermManager; public PermissionHelper(Context context, IPackageManager packageManager, - IPermissionManager permManager) { + IPermissionManager permManager) { mContext = context; mPackageManager = packageManager; mPermManager = permManager; @@ -298,6 +304,19 @@ public final class PermissionHelper { return false; } + static void grantUriPermission(final UriGrantsManagerInternal ugmInternal, Uri uri, + int sourceUid) { + if (uri == null || !ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) return; + + Binder.withCleanCallingIdentity(() -> { + // This will throw a SecurityException if the caller can't grant. + ugmInternal.checkGrantUriPermission(sourceUid, null, + ContentProvider.getUriWithoutUserId(uri), + Intent.FLAG_GRANT_READ_URI_PERMISSION, + ContentProvider.getUserIdFromUri(uri, UserHandle.getUserId(sourceUid))); + }); + } + public static class PackagePermission { public final String packageName; public final @UserIdInt int userId; diff --git a/services/core/java/com/android/server/notification/PreferencesHelper.java b/services/core/java/com/android/server/notification/PreferencesHelper.java index 85c395781d0a..9e70f815dff9 100644 --- a/services/core/java/com/android/server/notification/PreferencesHelper.java +++ b/services/core/java/com/android/server/notification/PreferencesHelper.java @@ -94,6 +94,7 @@ import com.android.internal.util.XmlUtils; import com.android.modules.utils.TypedXmlPullParser; import com.android.modules.utils.TypedXmlSerializer; import com.android.server.notification.PermissionHelper.PackagePermission; +import com.android.server.uri.UriGrantsManagerInternal; import org.json.JSONArray; import org.json.JSONException; @@ -219,6 +220,7 @@ public class PreferencesHelper implements RankingConfig { private final NotificationChannelLogger mNotificationChannelLogger; private final AppOpsManager mAppOps; private final ManagedServices.UserProfiles mUserProfiles; + private final UriGrantsManagerInternal mUgmInternal; private SparseBooleanArray mBadgingEnabled; private SparseBooleanArray mBubblesEnabled; @@ -239,6 +241,7 @@ public class PreferencesHelper implements RankingConfig { ZenModeHelper zenHelper, PermissionHelper permHelper, PermissionManager permManager, NotificationChannelLogger notificationChannelLogger, AppOpsManager appOpsManager, ManagedServices.UserProfiles userProfiles, + UriGrantsManagerInternal ugmInternal, boolean showReviewPermissionsNotification, Clock clock) { mContext = context; mZenModeHelper = zenHelper; @@ -249,6 +252,7 @@ public class PreferencesHelper implements RankingConfig { mNotificationChannelLogger = notificationChannelLogger; mAppOps = appOpsManager; mUserProfiles = userProfiles; + mUgmInternal = ugmInternal; mShowReviewPermissionsNotification = showReviewPermissionsNotification; mIsMediaNotificationFilteringEnabled = context.getResources() .getBoolean(R.bool.config_quickSettingsShowMediaPlayer); @@ -1169,6 +1173,13 @@ public class PreferencesHelper implements RankingConfig { } clearLockedFieldsLocked(channel); + // Verify that the app has permission to read the sound Uri + // Only check for new channels, as regular apps can only set sound + // before creating. See: {@link NotificationChannel#setSound} + if (Flags.notificationVerifyChannelSoundUri()) { + PermissionHelper.grantUriPermission(mUgmInternal, channel.getSound(), uid); + } + channel.setImportanceLockedByCriticalDeviceFunction( r.defaultAppLockedImportance || r.fixedImportance); diff --git a/services/core/java/com/android/server/notification/flags.aconfig b/services/core/java/com/android/server/notification/flags.aconfig index be3adc142fa4..0b34177d7413 100644 --- a/services/core/java/com/android/server/notification/flags.aconfig +++ b/services/core/java/com/android/server/notification/flags.aconfig @@ -144,6 +144,13 @@ flag { } flag { + name: "notification_minimalism" + namespace: "systemui" + description: "Minimize the notifications to show on the lockscreen." + bug: "330387368" +} + +flag { name: "notification_force_group_singletons" namespace: "systemui" description: "This flag enables forced auto-grouping singleton groups" @@ -163,3 +170,13 @@ flag { description: "This flag enables sound uri with vibration source" bug: "358524009" } + +flag { + name: "notification_verify_channel_sound_uri" + namespace: "systemui" + description: "Verify Uri permission for sound when creating a notification channel" + bug: "337775777" + metadata { + purpose: PURPOSE_BUGFIX + } +} diff --git a/services/core/java/com/android/server/pinner/PinRangeSource.java b/services/core/java/com/android/server/pinner/PinRangeSource.java new file mode 100644 index 000000000000..5f9641122294 --- /dev/null +++ b/services/core/java/com/android/server/pinner/PinRangeSource.java @@ -0,0 +1,27 @@ +/* + * 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.server.pinner; + +/* package */ abstract class PinRangeSource { + /** + * Retrieve a range to pin. + * + * @param outPinRange Receives the pin region + * @return True if we filled in outPinRange or false if we're out of pin entries + */ + abstract boolean read(PinnerService.PinRange outPinRange); +}
\ No newline at end of file diff --git a/services/core/java/com/android/server/pinner/PinRangeSourceStatic.java b/services/core/java/com/android/server/pinner/PinRangeSourceStatic.java new file mode 100644 index 000000000000..d6fc48790883 --- /dev/null +++ b/services/core/java/com/android/server/pinner/PinRangeSourceStatic.java @@ -0,0 +1,37 @@ +/* + * 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.server.pinner; + +/* package */ class PinRangeSourceStatic extends PinRangeSource { + private final int mPinStart; + private final int mPinLength; + private boolean mDone = false; + + PinRangeSourceStatic(int pinStart, int pinLength) { + mPinStart = pinStart; + mPinLength = pinLength; + } + + @Override + boolean read(PinnerService.PinRange outPinRange) { + outPinRange.start = mPinStart; + outPinRange.length = mPinLength; + boolean done = mDone; + mDone = true; + return !done; + } +}
\ No newline at end of file diff --git a/services/core/java/com/android/server/pinner/PinRangeSourceStream.java b/services/core/java/com/android/server/pinner/PinRangeSourceStream.java new file mode 100644 index 000000000000..79900b9de463 --- /dev/null +++ b/services/core/java/com/android/server/pinner/PinRangeSourceStream.java @@ -0,0 +1,43 @@ +/* + * 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.server.pinner; + +import java.io.DataInputStream; +import java.io.IOException; +import java.io.InputStream; + +/* package */ final class PinRangeSourceStream extends PinRangeSource { + private final DataInputStream mStream; + private boolean mDone = false; + + PinRangeSourceStream(InputStream stream) { + mStream = new DataInputStream(stream); + } + + @Override + boolean read(PinnerService.PinRange outPinRange) { + if (!mDone) { + try { + outPinRange.start = mStream.readInt(); + outPinRange.length = mStream.readInt(); + } catch (IOException ex) { + mDone = true; + } + } + return !mDone; + } +}
\ No newline at end of file diff --git a/services/core/java/com/android/server/pinner/PinnedFile.java b/services/core/java/com/android/server/pinner/PinnedFile.java new file mode 100644 index 000000000000..a8de344d10af --- /dev/null +++ b/services/core/java/com/android/server/pinner/PinnedFile.java @@ -0,0 +1,61 @@ +/* + * 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.server.pinner; + +import com.android.internal.annotations.VisibleForTesting; + +import java.util.ArrayList; + +@VisibleForTesting +public final class PinnedFile implements AutoCloseable { + private long mAddress; + final long mapSize; + final String fileName; + public final long bytesPinned; + + // Whether this file was pinned using a pinlist + boolean used_pinlist; + + // User defined group name for pinner accounting + String groupName = ""; + ArrayList<PinnedFile> pinnedDeps = new ArrayList<>(); + + public PinnedFile(long address, long mapSize, String fileName, long bytesPinned) { + mAddress = address; + this.mapSize = mapSize; + this.fileName = fileName; + this.bytesPinned = bytesPinned; + } + + @Override + public void close() { + if (mAddress >= 0) { + PinnerUtils.safeMunmap(mAddress, mapSize); + mAddress = -1; + } + for (PinnedFile dep : pinnedDeps) { + if (dep != null) { + dep.close(); + } + } + } + + @Override + public void finalize() { + close(); + } +}
\ No newline at end of file diff --git a/services/core/java/com/android/server/PinnerService.java b/services/core/java/com/android/server/pinner/PinnerService.java index ef03888d6620..d7ac5203ff53 100644 --- a/services/core/java/com/android/server/PinnerService.java +++ b/services/core/java/com/android/server/pinner/PinnerService.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2023 The Android Open Source Project + * 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. @@ -14,14 +14,14 @@ * limitations under the License. */ -package com.android.server; +package com.android.server.pinner; import static android.app.ActivityManager.UID_OBSERVER_ACTIVE; import static android.app.ActivityManager.UID_OBSERVER_GONE; import static android.os.Process.SYSTEM_UID; +import static com.android.server.flags.Flags.pinGlobalQuota; import static com.android.server.flags.Flags.pinWebview; -import static com.android.server.flags.Flags.skipHomeArtPins; import android.annotation.EnforcePermission; import android.annotation.IntDef; @@ -49,6 +49,7 @@ import android.os.Handler; import android.os.HandlerExecutor; import android.os.Looper; import android.os.Message; +import android.os.Process; import android.os.RemoteException; import android.os.ResultReceiver; import android.os.ShellCallback; @@ -72,13 +73,13 @@ import com.android.internal.app.ResolverActivity; import com.android.internal.os.BackgroundThread; import com.android.internal.util.DumpUtils; import com.android.internal.util.function.pooled.PooledLambda; +import com.android.server.LocalServices; +import com.android.server.SystemService; import com.android.server.wm.ActivityTaskManagerInternal; import dalvik.system.DexFile; import dalvik.system.VMRuntime; -import java.io.Closeable; -import java.io.DataInputStream; import java.io.FileDescriptor; import java.io.FileOutputStream; import java.io.IOException; @@ -110,8 +111,7 @@ public final class PinnerService extends SystemService { private static final String PIN_META_FILENAME = "pinlist.meta"; private static final int PAGE_SIZE = (int) Os.sysconf(OsConstants._SC_PAGESIZE); private static final int MATCH_FLAGS = PackageManager.MATCH_DEFAULT_ONLY - | PackageManager.MATCH_DIRECT_BOOT_AWARE - | PackageManager.MATCH_DIRECT_BOOT_UNAWARE; + | PackageManager.MATCH_DIRECT_BOOT_AWARE | PackageManager.MATCH_DIRECT_BOOT_UNAWARE; private static final int KEY_CAMERA = 0; private static final int KEY_HOME = 1; @@ -126,6 +126,8 @@ public final class PinnerService extends SystemService { public static final String ANON_REGION_STAT_NAME = "[anon]"; + private static final String SYSTEM_GROUP_NAME = "system"; + @IntDef({KEY_CAMERA, KEY_HOME, KEY_ASSISTANT}) @Retention(RetentionPolicy.SOURCE) public @interface AppKey {} @@ -139,7 +141,8 @@ public final class PinnerService extends SystemService { private final UserManager mUserManager; /** The list of the statically pinned files. */ - @GuardedBy("this") private final ArrayMap<String, PinnedFile> mPinnedFiles = new ArrayMap<>(); + @GuardedBy("this") + private final ArrayMap<String, PinnedFile> mPinnedFiles = new ArrayMap<>(); /** The list of the pinned apps. This is a map from {@link AppKey} to a pinned app. */ @GuardedBy("this") @@ -159,8 +162,8 @@ public final class PinnerService extends SystemService { /** * A set of {@link AppKey} that are configured to be pinned. */ - @GuardedBy("this") - private ArraySet<Integer> mPinKeys; + @GuardedBy("this") private + ArraySet<Integer> mPinKeys; // Note that we don't use the `_BOOT` namespace for anonymous pinnings, as we want // them to be responsive to dynamic flag changes for experimentation. @@ -180,14 +183,23 @@ public final class PinnerService extends SystemService { private final boolean mConfiguredToPinAssistant; private final int mConfiguredWebviewPinBytes; + // This is the percentage of total device memory that will be used to set the global quota. + private final int mConfiguredMaxPinnedMemoryPercentage; + + // This is the global pinner quota that can be pinned. + private long mConfiguredMaxPinnedMemory; + + // This is the currently pinned memory. + private long mCurrentPinnedMemory = 0; + private BinderService mBinderService; private PinnerHandler mPinnerHandler = null; private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { - // If an app has updated, update pinned files accordingly. - if (Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())) { + // If an app has updated, update pinned files accordingly. + if (Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())) { Uri packageUri = intent.getData(); String packageName = packageUri.getSchemeSpecificPart(); ArraySet<String> updatedPackages = new ArraySet<>(); @@ -210,7 +222,7 @@ public final class PinnerService extends SystemService { /** Utility class for testing. */ @VisibleForTesting - static class Injector { + public static class Injector { protected DeviceConfigInterface getDeviceConfigInterface() { return DeviceConfigInterface.REAL; } @@ -219,9 +231,9 @@ public final class PinnerService extends SystemService { service.publishBinderService("pinner", binderService); } - protected PinnedFile pinFileInternal(String fileToPin, - int maxBytesToPin, boolean attemptPinIntrospection) { - return PinnerService.pinFileInternal(fileToPin, maxBytesToPin, attemptPinIntrospection); + protected PinnedFile pinFileInternal(PinnerService service, String fileToPin, + long maxBytesToPin, boolean attemptPinIntrospection) { + return service.pinFileInternal(fileToPin, maxBytesToPin, attemptPinIntrospection); } } @@ -230,7 +242,7 @@ public final class PinnerService extends SystemService { } @VisibleForTesting - PinnerService(Context context, Injector injector) { + public PinnerService(Context context, Injector injector) { super(context); mContext = context; @@ -244,6 +256,9 @@ public final class PinnerService extends SystemService { com.android.internal.R.bool.config_pinnerAssistantApp); mConfiguredWebviewPinBytes = context.getResources().getInteger( com.android.internal.R.integer.config_pinnerWebviewPinBytes); + mConfiguredMaxPinnedMemoryPercentage = context.getResources().getInteger( + com.android.internal.R.integer.config_pinnerMaxPinnedMemoryPercentage); + mPinKeys = createPinKeys(); mPinnerHandler = new PinnerHandler(BackgroundThread.get().getLooper()); @@ -261,10 +276,8 @@ public final class PinnerService extends SystemService { registerUidListener(); registerUserSetupCompleteListener(); - mDeviceConfigInterface.addOnPropertiesChangedListener( - DEVICE_CONFIG_NAMESPACE_ANON_SIZE, - new HandlerExecutor(mPinnerHandler), - mDeviceConfigAnonSizeListener); + mDeviceConfigInterface.addOnPropertiesChangedListener(DEVICE_CONFIG_NAMESPACE_ANON_SIZE, + new HandlerExecutor(mPinnerHandler), mDeviceConfigAnonSizeListener); } @Override @@ -272,6 +285,10 @@ public final class PinnerService extends SystemService { if (DEBUG) { Slog.i(TAG, "Starting PinnerService"); } + mConfiguredMaxPinnedMemory = + (Process.getTotalMemory() + * Math.clamp(mConfiguredMaxPinnedMemoryPercentage, 0, 100)) + / 100; mBinderService = new BinderService(); mInjector.publishBinderService(this, mBinderService); publishLocalService(PinnerService.class, this); @@ -348,7 +365,7 @@ public final class PinnerService extends SystemService { protected PinnedFileStats(int uid, PinnedFile file) { this.uid = uid; this.filename = file.fileName.substring(file.fileName.lastIndexOf('/') + 1); - this.sizeKb = file.bytesPinned / 1024; + this.sizeKb = (int) file.bytesPinned / 1024; } } @@ -358,20 +375,11 @@ public final class PinnerService extends SystemService { private void handlePinOnStart() { // Files to pin come from the overlay and can be specified per-device config String[] filesToPin = mContext.getResources().getStringArray( - com.android.internal.R.array.config_defaultPinnerServiceFiles); + com.android.internal.R.array.config_defaultPinnerServiceFiles); // Continue trying to pin each file even if we fail to pin some of them for (String fileToPin : filesToPin) { - PinnedFile pf = mInjector.pinFileInternal(fileToPin, Integer.MAX_VALUE, - /*attemptPinIntrospection=*/false); - if (pf == null) { - Slog.e(TAG, "Failed to pin file = " + fileToPin); - continue; - } - synchronized (this) { - mPinnedFiles.put(pf.fileName, pf); - } - pf.groupName = "system"; - pinOptimizedDexDependencies(pf, Integer.MAX_VALUE, null); + pinFile(fileToPin, Integer.MAX_VALUE, /*appInfo=*/null, /*groupName=*/SYSTEM_GROUP_NAME, + true); } refreshPinAnonConfig(); @@ -383,10 +391,9 @@ public final class PinnerService extends SystemService { * regular home app. */ private void registerUserSetupCompleteListener() { - Uri userSetupCompleteUri = Settings.Secure.getUriFor( - Settings.Secure.USER_SETUP_COMPLETE); - mContext.getContentResolver().registerContentObserver(userSetupCompleteUri, - false, new ContentObserver(null) { + Uri userSetupCompleteUri = Settings.Secure.getUriFor(Settings.Secure.USER_SETUP_COMPLETE); + mContext.getContentResolver().registerContentObserver( + userSetupCompleteUri, false, new ContentObserver(null) { @Override public void onChange(boolean selfChange, Uri uri) { if (userSetupCompleteUri.equals(uri)) { @@ -409,7 +416,7 @@ public final class PinnerService extends SystemService { } @Override - public void onUidActive(int uid) { + public void onUidActive(int uid) { mPinnerHandler.sendMessage(PooledLambda.obtainMessage( PinnerService::handleUidActive, PinnerService.this, uid)); } @@ -423,7 +430,6 @@ public final class PinnerService extends SystemService { updateActiveState(uid, false /* active */); int key; synchronized (this) { - // In case we have a pending repin, repin now. See mPendingRepin for more information. key = mPendingRepin.getOrDefault(uid, -1); if (key == -1) { @@ -491,8 +497,8 @@ public final class PinnerService extends SystemService { private ApplicationInfo getCameraInfo(int userHandle) { Intent cameraIntent = new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA); - ApplicationInfo info = getApplicationInfoForIntent(cameraIntent, userHandle, - false /* defaultToSystemApp */); + ApplicationInfo info = getApplicationInfoForIntent( + cameraIntent, userHandle, false /* defaultToSystemApp */); // If the STILL_IMAGE_CAMERA intent doesn't resolve, try the _SECURE intent. // We don't use _SECURE first because it will never get set on a device @@ -501,16 +507,16 @@ public final class PinnerService extends SystemService { // preference using this intent. if (info == null) { cameraIntent = new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA_SECURE); - info = getApplicationInfoForIntent(cameraIntent, userHandle, - false /* defaultToSystemApp */); + info = getApplicationInfoForIntent( + cameraIntent, userHandle, false /* defaultToSystemApp */); } // If the _SECURE intent doesn't resolve, try the original intent but request // the system app for camera if there was more than one result. if (info == null) { cameraIntent = new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA); - info = getApplicationInfoForIntent(cameraIntent, userHandle, - true /* defaultToSystemApp */); + info = getApplicationInfoForIntent( + cameraIntent, userHandle, true /* defaultToSystemApp */); } return info; } @@ -525,14 +531,14 @@ public final class PinnerService extends SystemService { return getApplicationInfoForIntent(intent, userHandle, true); } - private ApplicationInfo getApplicationInfoForIntent(Intent intent, int userHandle, - boolean defaultToSystemApp) { + private ApplicationInfo getApplicationInfoForIntent( + Intent intent, int userHandle, boolean defaultToSystemApp) { if (intent == null) { return null; } - ResolveInfo resolveInfo = mContext.getPackageManager().resolveActivityAsUser(intent, - MATCH_FLAGS, userHandle); + ResolveInfo resolveInfo = + mContext.getPackageManager().resolveActivityAsUser(intent, MATCH_FLAGS, userHandle); // If this intent can resolve to only one app, choose that one. // Otherwise, if we've requested to default to the system app, return it; @@ -547,12 +553,11 @@ public final class PinnerService extends SystemService { } if (defaultToSystemApp) { - List<ResolveInfo> infoList = mContext.getPackageManager() - .queryIntentActivitiesAsUser(intent, MATCH_FLAGS, userHandle); + List<ResolveInfo> infoList = mContext.getPackageManager().queryIntentActivitiesAsUser( + intent, MATCH_FLAGS, userHandle); ApplicationInfo systemAppInfo = null; for (ResolveInfo info : infoList) { - if ((info.activityInfo.applicationInfo.flags - & ApplicationInfo.FLAG_SYSTEM) != 0) { + if ((info.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { if (systemAppInfo == null) { systemAppInfo = info.activityInfo.applicationInfo; } else { @@ -568,13 +573,13 @@ public final class PinnerService extends SystemService { } private void sendPinAppsMessage(int userHandle) { - mPinnerHandler.sendMessage(PooledLambda.obtainMessage(PinnerService::pinApps, this, - userHandle)); + mPinnerHandler.sendMessage( + PooledLambda.obtainMessage(PinnerService::pinApps, this, userHandle)); } private void sendPinAppsWithUpdatedKeysMessage(int userHandle) { - mPinnerHandler.sendMessage(PooledLambda.obtainMessage(PinnerService::pinAppsWithUpdatedKeys, - this, userHandle)); + mPinnerHandler.sendMessage(PooledLambda.obtainMessage( + PinnerService::pinAppsWithUpdatedKeys, this, userHandle)); } private void sendUnpinAppsMessage() { mPinnerHandler.sendMessage(PooledLambda.obtainMessage(PinnerService::unpinApps, this)); @@ -586,8 +591,7 @@ public final class PinnerService extends SystemService { // phenotype property is not set. boolean shouldPinCamera = mConfiguredToPinCamera && mDeviceConfigInterface.getBoolean(DeviceConfig.NAMESPACE_RUNTIME_NATIVE_BOOT, - "pin_camera", - SystemProperties.getBoolean("pinner.pin_camera", true)); + "pin_camera", SystemProperties.getBoolean("pinner.pin_camera", true)); if (shouldPinCamera) { pinKeys.add(KEY_CAMERA); } else if (DEBUG) { @@ -626,8 +630,9 @@ public final class PinnerService extends SystemService { synchronized (this) { // This code path demands preceding unpinApps() call. if (!mPinnedApps.isEmpty()) { - Slog.e(TAG, "Attempted to update a list of apps, " - + "but apps were already pinned. Skipping."); + Slog.e(TAG, + "Attempted to update a list of apps, " + + "but apps were already pinned. Skipping."); return; } @@ -646,8 +651,8 @@ public final class PinnerService extends SystemService { * @see #pinApp(int, int, boolean) */ private void sendPinAppMessage(int key, int userHandle, boolean force) { - mPinnerHandler.sendMessage(PooledLambda.obtainMessage(PinnerService::pinApp, this, - key, userHandle, force)); + mPinnerHandler.sendMessage( + PooledLambda.obtainMessage(PinnerService::pinApp, this, key, userHandle, force)); } /** @@ -667,10 +672,10 @@ public final class PinnerService extends SystemService { } return; } - unpinApp(key); ApplicationInfo info = getInfoForKey(key, userHandle); + unpinApp(key); if (info != null) { - pinApp(key, info); + pinAppInternal(key, info); } } @@ -682,9 +687,7 @@ public final class PinnerService extends SystemService { private int getUidForKey(@AppKey int key) { synchronized (this) { PinnedApp existing = mPinnedApps.get(key); - return existing != null && existing.active - ? existing.uid - : -1; + return existing != null && existing.active ? existing.uid : -1; } } @@ -727,11 +730,8 @@ public final class PinnerService extends SystemService { * Handle any changes in the anon region pinner config. */ private void refreshPinAnonConfig() { - long newPinAnonSize = - mDeviceConfigInterface.getLong( - DEVICE_CONFIG_NAMESPACE_ANON_SIZE, - DEVICE_CONFIG_KEY_ANON_SIZE, - DEFAULT_ANON_SIZE); + long newPinAnonSize = mDeviceConfigInterface.getLong( + DEVICE_CONFIG_NAMESPACE_ANON_SIZE, DEVICE_CONFIG_KEY_ANON_SIZE, DEFAULT_ANON_SIZE); newPinAnonSize = Math.max(0, Math.min(newPinAnonSize, MAX_ANON_SIZE)); if (newPinAnonSize != mPinAnonSize) { mPinAnonSize = newPinAnonSize; @@ -765,10 +765,9 @@ public final class PinnerService extends SystemService { try { // Map as SHARED to avoid changing rss.anon for system_server (per /proc/*/status). // The mapping is visible in other rss metrics, and as private dirty in smaps/meminfo. - address = Os.mmap(0, alignedPinSize, - OsConstants.PROT_READ | OsConstants.PROT_WRITE, - OsConstants.MAP_SHARED | OsConstants.MAP_ANONYMOUS, - new FileDescriptor(), /*offset=*/0); + address = Os.mmap(0, alignedPinSize, OsConstants.PROT_READ | OsConstants.PROT_WRITE, + OsConstants.MAP_SHARED | OsConstants.MAP_ANONYMOUS, new FileDescriptor(), + /*offset=*/0); Unsafe tempUnsafe = null; Class<sun.misc.Unsafe> clazz = sun.misc.Unsafe.class; @@ -794,14 +793,14 @@ public final class PinnerService extends SystemService { return; } finally { if (address >= 0) { - safeMunmap(address, alignedPinSize); + PinnerUtils.safeMunmap(address, alignedPinSize); } } } private void unpinAnonRegion() { if (mPinAnonAddress != 0) { - safeMunmap(mPinAnonAddress, mCurrentlyPinnedAnonSize); + PinnerUtils.safeMunmap(mPinAnonAddress, mCurrentlyPinnedAnonSize); } mPinAnonAddress = 0; mCurrentlyPinnedAnonSize = 0; @@ -824,12 +823,20 @@ public final class PinnerService extends SystemService { } /** + * Retrieves remaining quota for pinner service, once it reaches 0 it will no longer + * pin any file. + */ + private long getAvailableGlobalQuota() { + return mConfiguredMaxPinnedMemory - mCurrentPinnedMemory; + } + + /** * Pins an application. * * @param key The key of the app to pin. * @param appInfo The corresponding app info. */ - private void pinApp(@AppKey int key, @Nullable ApplicationInfo appInfo) { + private void pinAppInternal(@AppKey int key, @Nullable ApplicationInfo appInfo) { if (appInfo == null) { return; } @@ -839,7 +846,6 @@ public final class PinnerService extends SystemService { mPinnedApps.put(key, pinnedApp); } - // pin APK final int pinSizeLimit = getSizeLimitForKey(key); List<String> apks = new ArrayList<>(); @@ -851,36 +857,31 @@ public final class PinnerService extends SystemService { } } - int apkPinSizeLimit = pinSizeLimit; - - boolean shouldSkipArtPins = key == KEY_HOME && skipHomeArtPins(); + long apkPinSizeLimit = pinSizeLimit; - for (String apk: apks) { + for (String apk : apks) { if (apkPinSizeLimit <= 0) { Slog.w(TAG, "Reached to the pin size limit. Skipping: " + apk); // Continue instead of break to print all skipped APK names. continue; } - PinnedFile pf = mInjector.pinFileInternal(apk, apkPinSizeLimit, /*attemptPinIntrospection=*/true); + String pinGroup = getNameForKey(key); + boolean shouldPinDeps = apk.equals(appInfo.sourceDir); + PinnedFile pf = pinFile(apk, apkPinSizeLimit, appInfo, pinGroup, shouldPinDeps); if (pf == null) { Slog.e(TAG, "Failed to pin " + apk); continue; } - pf.groupName = getNameForKey(key); if (DEBUG) { Slog.i(TAG, "Pinned " + pf.fileName); } synchronized (this) { pinnedApp.mFiles.add(pf); - mPinnedFiles.put(pf.fileName, pf); } apkPinSizeLimit -= pf.bytesPinned; - if (apk.equals(appInfo.sourceDir) && !shouldSkipArtPins) { - pinOptimizedDexDependencies(pf, Integer.MAX_VALUE, appInfo); - } } } @@ -892,19 +893,23 @@ public final class PinnerService extends SystemService { * that related to the file but not within itself. * * @param fileToPin File to pin - * @param maxBytesToPin maximum quota allowed for pinning - * @return total bytes that were pinned. + * @param bytesRequestedToPin maximum bytes requested to pin for {@code fileToPin}. + * @param pinOptimizedDeps whether optimized dependencies such as odex,vdex, etc be pinned. + * Note: {@code bytesRequestedToPin} limit will not apply to optimized + * dependencies pinned, only global quotas will apply instead. + * @return pinned file */ - public int pinFile(String fileToPin, int maxBytesToPin, @Nullable ApplicationInfo appInfo, - @Nullable String groupName) { + public PinnedFile pinFile(String fileToPin, long bytesRequestedToPin, + @Nullable ApplicationInfo appInfo, @Nullable String groupName, + boolean pinOptimizedDeps) { PinnedFile existingPin; - synchronized(this) { + synchronized (this) { existingPin = mPinnedFiles.get(fileToPin); } if (existingPin != null) { - if (existingPin.bytesPinned == maxBytesToPin) { + if (existingPin.bytesPinned == bytesRequestedToPin) { // Duplicate pin requesting same amount of bytes, lets just bail out. - return 0; + return null; } else { // User decided to pin a different amount of bytes than currently pinned // so this is a valid pin request. Unpin the previous version before repining. @@ -915,26 +920,38 @@ public final class PinnerService extends SystemService { } } + long remainingQuota = getAvailableGlobalQuota(); + + if (pinGlobalQuota()) { + if (remainingQuota <= 0) { + Slog.w(TAG, "Reached pin quota, skipping file: " + fileToPin); + return null; + } + bytesRequestedToPin = Math.min(bytesRequestedToPin, remainingQuota); + } + boolean isApk = fileToPin.endsWith(".apk"); - int bytesPinned = 0; - PinnedFile pf = mInjector.pinFileInternal(fileToPin, maxBytesToPin, + + PinnedFile pf = mInjector.pinFileInternal(this, fileToPin, bytesRequestedToPin, /*attemptPinIntrospection=*/isApk); if (pf == null) { Slog.e(TAG, "Failed to pin file = " + fileToPin); - return 0; + return null; } pf.groupName = groupName != null ? groupName : ""; - bytesPinned += pf.bytesPinned; - maxBytesToPin -= bytesPinned; + mCurrentPinnedMemory += pf.bytesPinned; synchronized (this) { mPinnedFiles.put(pf.fileName, pf); } - if (maxBytesToPin > 0) { - pinOptimizedDexDependencies(pf, maxBytesToPin, appInfo); + + if (pinOptimizedDeps) { + mCurrentPinnedMemory += + pinOptimizedDexDependencies(pf, getAvailableGlobalQuota(), appInfo); } - return bytesPinned; + + return pf; } /** @@ -945,13 +962,13 @@ public final class PinnerService extends SystemService { * to null it will use the default supported ABI by the device. * @return total bytes pinned. */ - private int pinOptimizedDexDependencies( - PinnedFile pinnedFile, int maxBytesToPin, @Nullable ApplicationInfo appInfo) { + private long pinOptimizedDexDependencies( + PinnedFile pinnedFile, long maxBytesToPin, @Nullable ApplicationInfo appInfo) { if (pinnedFile == null) { return 0; } - int bytesPinned = 0; + long bytesPinned = 0; if (pinnedFile.fileName.endsWith(".jar") | pinnedFile.fileName.endsWith(".apk")) { String abi = null; if (appInfo != null) { @@ -974,7 +991,7 @@ public final class PinnerService extends SystemService { // Unpin if it was already pinned prior to re-pinning. unpinFile(file); - PinnedFile df = mInjector.pinFileInternal(file, maxBytesToPin, + PinnedFile df = mInjector.pinFileInternal(this, file, maxBytesToPin, /*attemptPinIntrospection=*/false); if (df == null) { Slog.i(TAG, "Failed to pin ART file = " + file); @@ -992,7 +1009,8 @@ public final class PinnerService extends SystemService { return bytesPinned; } - /** mlock length bytes of fileToPin in memory + /** + * mlock length bytes of fileToPin in memory * * If attemptPinIntrospection is true, then treat the file to pin as a zip file and * look for a "pinlist.meta" file in the archive root directory. The structure of this @@ -1029,8 +1047,8 @@ public final class PinnerService extends SystemService { * zip in order to extract the * @return Pinned memory resource owner thing or null on error */ - private static PinnedFile pinFileInternal( - String fileToPin, int maxBytesToPin, boolean attemptPinIntrospection) { + private PinnedFile pinFileInternal( + String fileToPin, long maxBytesToPin, boolean attemptPinIntrospection) { if (DEBUG) { Slog.d(TAG, "pin file: " + fileToPin + " use-pinlist: " + attemptPinIntrospection); } @@ -1054,8 +1072,8 @@ public final class PinnerService extends SystemService { } return pinnedFile; } finally { - safeClose(pinRangeStream); - safeClose(fileAsZip); // Also closes any streams we've opened + PinnerUtils.safeClose(pinRangeStream); + PinnerUtils.safeClose(fileAsZip); // Also closes any streams we've opened } } @@ -1068,11 +1086,8 @@ public final class PinnerService extends SystemService { try { zip = new ZipFile(fileName); } catch (IOException ex) { - Slog.w(TAG, - String.format( - "could not open \"%s\" as zip: pinning as blob", - fileName), - ex); + Slog.w(TAG, String.format("could not open \"%s\" as zip: pinning as blob", fileName), + ex); } return zip; } @@ -1112,9 +1127,9 @@ public final class PinnerService extends SystemService { pinMetaStream = zipFile.getInputStream(pinMetaEntry); } catch (IOException ex) { Slog.w(TAG, - String.format("error reading pin metadata \"%s\": pinning as blob", - fileName), - ex); + String.format( + "error reading pin metadata \"%s\": pinning as blob", fileName), + ex); } } else { Slog.w(TAG, @@ -1124,57 +1139,6 @@ public final class PinnerService extends SystemService { return pinMetaStream; } - private static abstract class PinRangeSource { - /** Retrive a range to pin. - * - * @param outPinRange Receives the pin region - * @return True if we filled in outPinRange or false if we're out of pin entries - */ - abstract boolean read(PinRange outPinRange); - } - - private static final class PinRangeSourceStatic extends PinRangeSource { - private final int mPinStart; - private final int mPinLength; - private boolean mDone = false; - - PinRangeSourceStatic(int pinStart, int pinLength) { - mPinStart = pinStart; - mPinLength = pinLength; - } - - @Override - boolean read(PinRange outPinRange) { - outPinRange.start = mPinStart; - outPinRange.length = mPinLength; - boolean done = mDone; - mDone = true; - return !done; - } - } - - private static final class PinRangeSourceStream extends PinRangeSource { - private final DataInputStream mStream; - private boolean mDone = false; - - PinRangeSourceStream(InputStream stream) { - mStream = new DataInputStream(stream); - } - - @Override - boolean read(PinRange outPinRange) { - if (!mDone) { - try { - outPinRange.start = mStream.readInt(); - outPinRange.length = mStream.readInt(); - } catch (IOException ex) { - mDone = true; - } - } - return !mDone; - } - } - /** * Helper for pinFile. * @@ -1185,25 +1149,20 @@ public final class PinnerService extends SystemService { * @return PinnedFile or null on error */ private static PinnedFile pinFileRanges( - String fileToPin, - int maxBytesToPin, - PinRangeSource pinRangeSource) - { + String fileToPin, long maxBytesToPin, PinRangeSource pinRangeSource) { FileDescriptor fd = new FileDescriptor(); long address = -1; - int mapSize = 0; + long mapSize = 0; try { int openFlags = (OsConstants.O_RDONLY | OsConstants.O_CLOEXEC); fd = Os.open(fileToPin, openFlags, 0); mapSize = (int) Math.min(Os.fstat(fd).st_size, Integer.MAX_VALUE); - address = Os.mmap(0, mapSize, - OsConstants.PROT_READ, - OsConstants.MAP_SHARED, - fd, /*offset=*/0); + address = Os.mmap( + 0, mapSize, OsConstants.PROT_READ, OsConstants.MAP_SHARED, fd, /*offset=*/0); PinRange pinRange = new PinRange(); - int bytesPinned = 0; + long bytesPinned = 0; // We pin at page granularity, so make sure the limit is page-aligned if (maxBytesToPin % PAGE_SIZE != 0) { @@ -1211,10 +1170,10 @@ public final class PinnerService extends SystemService { } while (bytesPinned < maxBytesToPin && pinRangeSource.read(pinRange)) { - int pinStart = pinRange.start; - int pinLength = pinRange.length; - pinStart = clamp(0, pinStart, mapSize); - pinLength = clamp(0, pinLength, mapSize - pinStart); + long pinStart = pinRange.start; + long pinLength = pinRange.length; + pinStart = PinnerUtils.clamp(0, pinStart, mapSize); + pinLength = PinnerUtils.clamp(0, pinLength, mapSize - pinStart); pinLength = Math.min(maxBytesToPin - bytesPinned, pinLength); // mlock doesn't require the region to be page-aligned, but we snap the @@ -1229,14 +1188,13 @@ public final class PinnerService extends SystemService { if (pinLength % PAGE_SIZE != 0) { pinLength += PAGE_SIZE - pinLength % PAGE_SIZE; } - pinLength = clamp(0, pinLength, maxBytesToPin - bytesPinned); + pinLength = PinnerUtils.clamp(0, pinLength, maxBytesToPin - bytesPinned); if (pinLength > 0) { if (DEBUG) { Slog.d(TAG, - String.format( - "pinning at %s %s bytes of %s", - pinStart, pinLength, fileToPin)); + String.format("pinning at %s %s bytes of %s", pinStart, pinLength, + fileToPin)); } Os.mlock(address + pinStart, pinLength); } @@ -1244,15 +1202,15 @@ public final class PinnerService extends SystemService { } PinnedFile pinnedFile = new PinnedFile(address, mapSize, fileToPin, bytesPinned); - address = -1; // Ownership transferred + address = -1; // Ownership transferred return pinnedFile; } catch (ErrnoException ex) { Slog.e(TAG, "Could not pin file " + fileToPin, ex); return null; } finally { - safeClose(fd); + PinnerUtils.safeClose(fd); if (address >= 0) { - safeMunmap(address, mapSize); + PinnerUtils.safeMunmap(address, mapSize); } } } @@ -1273,81 +1231,50 @@ public final class PinnerService extends SystemService { } } - public void unpinFile(String filename) { + /** + * Unpin a file and its optimized dependencies. + * + * @param filename file to unpin. + * @return number of bytes unpinned, 0 in case of failure or nothing to unpin. + */ + public long unpinFile(String filename) { PinnedFile pinnedFile; synchronized (this) { pinnedFile = mPinnedFiles.get(filename); } if (pinnedFile == null) { // File not pinned, nothing to do. - return; + return 0; } + long unpinnedBytes = pinnedFile.bytesPinned; pinnedFile.close(); synchronized (this) { if (DEBUG) { Slog.d(TAG, "Unpinned file: " + filename); } + mCurrentPinnedMemory -= pinnedFile.bytesPinned; + mPinnedFiles.remove(pinnedFile.fileName); for (PinnedFile dep : pinnedFile.pinnedDeps) { if (dep == null) { continue; } + unpinnedBytes -= dep.bytesPinned; + mCurrentPinnedMemory -= dep.bytesPinned; mPinnedFiles.remove(dep.fileName); if (DEBUG) { Slog.d(TAG, "Unpinned dependency: " + dep.fileName); } } } - } - private static int clamp(int min, int value, int max) { - return Math.max(min, Math.min(value, max)); - } - - private static void safeMunmap(long address, long mapSize) { - try { - Os.munmap(address, mapSize); - } catch (ErrnoException ex) { - Slog.w(TAG, "ignoring error in unmap", ex); - } - } - - /** - * Close FD, swallowing irrelevant errors. - */ - private static void safeClose(@Nullable FileDescriptor fd) { - if (fd != null && fd.valid()) { - try { - Os.close(fd); - } catch (ErrnoException ex) { - // Swallow the exception: non-EBADF errors in close(2) - // indicate deferred paging write errors, which we - // don't care about here. The underlying file - // descriptor is always closed. - if (ex.errno == OsConstants.EBADF) { - throw new AssertionError(ex); - } - } - } - } - - /** - * Close closeable thing, swallowing errors. - */ - private static void safeClose(@Nullable Closeable thing) { - if (thing != null) { - try { - thing.close(); - } catch (IOException ex) { - Slog.w(TAG, "ignoring error closing resource: " + thing, ex); - } - } + return unpinnedBytes; } public List<PinnedFileStat> getPinnerStats() { ArrayList<PinnedFileStat> stats = new ArrayList<>(); Collection<PinnedFile> pinnedFiles; - synchronized(this) { + synchronized (this) { pinnedFiles = mPinnedFiles.values(); } for (PinnedFile pf : pinnedFiles) { @@ -1355,8 +1282,8 @@ public final class PinnerService extends SystemService { stats.add(stat); } if (mCurrentlyPinnedAnonSize > 0) { - stats.add(new PinnedFileStat(ANON_REGION_STAT_NAME, - mCurrentlyPinnedAnonSize, ANON_REGION_STAT_NAME)); + stats.add(new PinnedFileStat( + ANON_REGION_STAT_NAME, mCurrentlyPinnedAnonSize, ANON_REGION_STAT_NAME)); } return stats; } @@ -1364,71 +1291,124 @@ public final class PinnerService extends SystemService { public final class BinderService extends IPinnerService.Stub { @Override protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { - if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return; + if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) + return; HashSet<PinnedFile> shownPins = new HashSet<>(); - HashSet<String> groups = new HashSet<>(); - final int bytesPerMB = 1024 * 1024; + HashSet<String> shownGroups = new HashSet<>(); + HashSet<String> groupsToPrint = new HashSet<>(); + final double bytesPerMB = 1024 * 1024; + pw.format("Pinner Configs:\n"); + pw.format(" Total Pinner quota: %d%% of total device memory\n", + mConfiguredMaxPinnedMemoryPercentage); + pw.format(" Maximum Pinner quota: %d bytes (%.2f MB)\n", mConfiguredMaxPinnedMemory, + mConfiguredMaxPinnedMemory / bytesPerMB); + pw.format(" Max Home App Pin Bytes (without deps): %d\n", mConfiguredHomePinBytes); + pw.format("\nPinned Files:\n"); synchronized (PinnerService.this) { long totalSize = 0; + + // We print apps separately from regular pins as they contain extra information that + // other pins do not. for (int key : mPinnedApps.keySet()) { PinnedApp app = mPinnedApps.get(key); pw.print(getNameForKey(key)); - pw.print(" uid="); pw.print(app.uid); - pw.print(" active="); pw.print(app.active); + pw.print(" uid="); + pw.print(app.uid); + pw.print(" active="); + pw.print(app.active); + + if (!app.mFiles.isEmpty()) { + shownGroups.add(app.mFiles.getFirst().groupName); + } pw.println(); + long bytesPinnedForApp = 0; + long bytesPinnedForAppDeps = 0; for (PinnedFile pf : mPinnedApps.get(key).mFiles) { pw.print(" "); - pw.format("%s pinned:%d bytes (%d MB) pinlist:%b\n", pf.fileName, + pw.format("%s pinned:%d bytes (%.2f MB) pinlist:%b\n", pf.fileName, pf.bytesPinned, pf.bytesPinned / bytesPerMB, pf.used_pinlist); totalSize += pf.bytesPinned; + bytesPinnedForApp += pf.bytesPinned; shownPins.add(pf); for (PinnedFile dep : pf.pinnedDeps) { pw.print(" "); - pw.format("%s pinned:%d bytes (%d MB) pinlist:%b (Dependency)\n", dep.fileName, - dep.bytesPinned, dep.bytesPinned / bytesPerMB, dep.used_pinlist); + pw.format("%s pinned:%d bytes (%.2f MB) pinlist:%b (Dependency)\n", + dep.fileName, dep.bytesPinned, dep.bytesPinned / bytesPerMB, + dep.used_pinlist); totalSize += dep.bytesPinned; + bytesPinnedForAppDeps += dep.bytesPinned; shownPins.add(dep); } } + long bytesPinnedForAppAndDeps = bytesPinnedForApp + bytesPinnedForAppDeps; + pw.format("Total Pinned = %d (%.2f MB) [App=%d (%.2f MB), " + + "Dependencies=%d (%.2f MB)]\n\n", + bytesPinnedForAppAndDeps, bytesPinnedForAppAndDeps / bytesPerMB, + bytesPinnedForApp, bytesPinnedForApp / bytesPerMB, + bytesPinnedForAppDeps, bytesPinnedForAppDeps / bytesPerMB); } pw.println(); for (PinnedFile pinnedFile : mPinnedFiles.values()) { - if (!groups.contains(pinnedFile.groupName)) { - groups.add(pinnedFile.groupName); + if (!groupsToPrint.contains(pinnedFile.groupName) + && !shownGroups.contains(pinnedFile.groupName)) { + groupsToPrint.add(pinnedFile.groupName); } } - boolean firstPinInGroup = true; - for (String group : groups) { + + // Print all the non app groups. + for (String group : groupsToPrint) { List<PinnedFile> groupPins = getAllPinsForGroup(group); + pw.print("\nGroup:" + group); + long bytesPinnedForGroupNoDeps = 0; + long bytesPinnedForGroupDeps = 0; + pw.println(); for (PinnedFile pinnedFile : groupPins) { if (shownPins.contains(pinnedFile)) { - // Already showed in the dump and accounted for, skip. + // Already displayed and accounted for, skip. continue; } - if (firstPinInGroup) { - firstPinInGroup = false; - // Ensure we only print when there are pins for groups not yet shown - // in the pinned app section. - pw.print("Group:" + group); - pw.println(); - } - pw.format(" %s pinned:%d bytes (%d MB) pinlist:%b\n", pinnedFile.fileName, - pinnedFile.bytesPinned, pinnedFile.bytesPinned / bytesPerMB, - pinnedFile.used_pinlist); + pw.format(" %s pinned: %d bytes (%.2f MB) pinlist:%b\n", + pinnedFile.fileName, pinnedFile.bytesPinned, + pinnedFile.bytesPinned / bytesPerMB, pinnedFile.used_pinlist); totalSize += pinnedFile.bytesPinned; + bytesPinnedForGroupNoDeps += pinnedFile.bytesPinned; + shownPins.add(pinnedFile); + for (PinnedFile dep : pinnedFile.pinnedDeps) { + if (shownPins.contains(dep)) { + // Already displayed and accounted for, skip. + continue; + } + pw.print(" "); + pw.format("%s pinned:%d bytes (%.2f MB) pinlist:%b (Dependency)\n", + dep.fileName, dep.bytesPinned, dep.bytesPinned / bytesPerMB, + dep.used_pinlist); + totalSize += dep.bytesPinned; + bytesPinnedForGroupDeps += dep.bytesPinned; + shownPins.add(dep); + } } + long bytesPinnedForGroup = bytesPinnedForGroupNoDeps + bytesPinnedForGroupDeps; + pw.format("Total Pinned = %d (%.2f MB) [Main=%d (%.2f MB), " + + "Dependencies=%d (%.2f MB)]\n\n", + bytesPinnedForGroup, bytesPinnedForGroup / bytesPerMB, + bytesPinnedForGroupNoDeps, bytesPinnedForGroupNoDeps / bytesPerMB, + bytesPinnedForGroupDeps, bytesPinnedForGroupDeps / bytesPerMB); } pw.println(); if (mPinAnonAddress != 0) { - pw.format("Pinned anon region: %d (%d MB)\n", mCurrentlyPinnedAnonSize, mCurrentlyPinnedAnonSize / bytesPerMB); + pw.format("Pinned anon region: %d (%.2f MB)\n", mCurrentlyPinnedAnonSize, + mCurrentlyPinnedAnonSize / bytesPerMB); totalSize += mCurrentlyPinnedAnonSize; } - pw.format("Total pinned: %s bytes (%s MB)\n", totalSize, totalSize / bytesPerMB); + pw.format("Total pinned: %d bytes (%.2f MB)\n", totalSize, totalSize / bytesPerMB); + pw.format("Available Pinner quota: %d bytes (%.2f MB)\n", getAvailableGlobalQuota(), + getAvailableGlobalQuota() / bytesPerMB); pw.println(); if (!mPendingRepin.isEmpty()) { pw.print("Pending repin: "); for (int key : mPendingRepin.values()) { - pw.print(getNameForKey(key)); pw.print(' '); + pw.print(getNameForKey(key)); + pw.print(' '); } pw.println(); } @@ -1462,8 +1442,9 @@ public final class PinnerService extends SystemService { repin(); break; default: - printError(out, String.format( - "Unknown pinner command: %s. Supported commands: repin", command)); + printError(out, + String.format("Unknown pinner command: %s. Supported commands: repin", + command)); resultReceiver.send(-1, null); return; } @@ -1479,46 +1460,6 @@ public final class PinnerService extends SystemService { } } - @VisibleForTesting - public static final class PinnedFile implements AutoCloseable { - private long mAddress; - final int mapSize; - final String fileName; - final int bytesPinned; - - // Whether this file was pinned using a pinlist - boolean used_pinlist; - - // User defined group name for pinner accounting - String groupName = ""; - ArrayList<PinnedFile> pinnedDeps = new ArrayList<>(); - - PinnedFile(long address, int mapSize, String fileName, int bytesPinned) { - mAddress = address; - this.mapSize = mapSize; - this.fileName = fileName; - this.bytesPinned = bytesPinned; - } - - @Override - public void close() { - if (mAddress >= 0) { - safeMunmap(mAddress, mapSize); - mAddress = -1; - } - for (PinnedFile dep : pinnedDeps) { - if (dep != null) { - dep.close(); - } - } - } - - @Override - public void finalize() { - close(); - } - } - final static class PinRange { int start; int length; @@ -1528,7 +1469,6 @@ public final class PinnerService extends SystemService { * Represents an app that was pinned. */ private final class PinnedApp { - /** * The uid of the package being pinned. This stays constant while the package stays * installed. @@ -1557,11 +1497,9 @@ public final class PinnerService extends SystemService { @Override public void handleMessage(Message msg) { switch (msg.what) { - case PIN_ONSTART_MSG: - { + case PIN_ONSTART_MSG: { handlePinOnStart(); - } - break; + } break; default: super.handleMessage(msg); diff --git a/services/core/java/com/android/server/pinner/PinnerUtils.java b/services/core/java/com/android/server/pinner/PinnerUtils.java new file mode 100644 index 000000000000..a836a83dedab --- /dev/null +++ b/services/core/java/com/android/server/pinner/PinnerUtils.java @@ -0,0 +1,75 @@ +/* + * 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.server.pinner; + +import android.annotation.Nullable; +import android.system.ErrnoException; +import android.system.Os; +import android.system.OsConstants; +import android.util.Slog; + +import java.io.Closeable; +import java.io.FileDescriptor; +import java.io.IOException; + +/* package */ final class PinnerUtils { + private static final String TAG = "PinnerUtils"; + + public static long clamp(long min, long value, long max) { + return Math.max(min, Math.min(value, max)); + } + + public static void safeMunmap(long address, long mapSize) { + try { + Os.munmap(address, mapSize); + } catch (ErrnoException ex) { + Slog.w(TAG, "ignoring error in unmap", ex); + } + } + + /** + * Close FD, swallowing irrelevant errors. + */ + public static void safeClose(@Nullable FileDescriptor fd) { + if (fd != null && fd.valid()) { + try { + Os.close(fd); + } catch (ErrnoException ex) { + // Swallow the exception: non-EBADF errors in close(2) + // indicate deferred paging write errors, which we + // don't care about here. The underlying file + // descriptor is always closed. + if (ex.errno == OsConstants.EBADF) { + throw new AssertionError(ex); + } + } + } + } + + /** + * Close closeable thing, swallowing errors. + */ + public static void safeClose(@Nullable Closeable thing) { + if (thing != null) { + try { + thing.close(); + } catch (IOException ex) { + Slog.w(TAG, "ignoring error closing resource: " + thing, ex); + } + } + } +} diff --git a/services/core/java/com/android/server/pm/DexOptHelper.java b/services/core/java/com/android/server/pm/DexOptHelper.java index 9ecc7b9a805d..1569fa0aa8d7 100644 --- a/services/core/java/com/android/server/pm/DexOptHelper.java +++ b/services/core/java/com/android/server/pm/DexOptHelper.java @@ -70,13 +70,13 @@ import com.android.internal.util.FrameworkStatsLog; import com.android.internal.util.IndentingPrintWriter; import com.android.server.LocalManagerRegistry; import com.android.server.LocalServices; -import com.android.server.PinnerService; import com.android.server.art.ArtManagerLocal; import com.android.server.art.DexUseManagerLocal; import com.android.server.art.ReasonMapping; import com.android.server.art.model.ArtFlags; import com.android.server.art.model.DexoptParams; import com.android.server.art.model.DexoptResult; +import com.android.server.pinner.PinnerService; import com.android.server.pm.PackageDexOptimizer.DexOptResult; import com.android.server.pm.dex.DexManager; import com.android.server.pm.dex.DexoptOptions; diff --git a/services/core/java/com/android/server/power/stats/wakeups/CpuWakeupStats.java b/services/core/java/com/android/server/power/stats/wakeups/CpuWakeupStats.java index f047f564538d..ab630eef4644 100644 --- a/services/core/java/com/android/server/power/stats/wakeups/CpuWakeupStats.java +++ b/services/core/java/com/android/server/power/stats/wakeups/CpuWakeupStats.java @@ -17,6 +17,7 @@ package com.android.server.power.stats.wakeups; import static android.os.BatteryStatsInternal.CPU_WAKEUP_SUBSYSTEM_ALARM; +import static android.os.BatteryStatsInternal.CPU_WAKEUP_SUBSYSTEM_BLUETOOTH; import static android.os.BatteryStatsInternal.CPU_WAKEUP_SUBSYSTEM_CELLULAR_DATA; import static android.os.BatteryStatsInternal.CPU_WAKEUP_SUBSYSTEM_SENSOR; import static android.os.BatteryStatsInternal.CPU_WAKEUP_SUBSYSTEM_SOUND_TRIGGER; @@ -63,6 +64,7 @@ public class CpuWakeupStats { private static final String SUBSYSTEM_SOUND_TRIGGER_STRING = "Sound_trigger"; private static final String SUBSYSTEM_SENSOR_STRING = "Sensor"; private static final String SUBSYSTEM_CELLULAR_DATA_STRING = "Cellular_data"; + private static final String SUBSYSTEM_BLUETOOTH_STRING = "Bluetooth"; private static final String TRACE_TRACK_WAKEUP_ATTRIBUTION = "wakeup_attribution"; private static final long WAKEUP_WRITE_DELAY_MS = TimeUnit.SECONDS.toMillis(30); @@ -512,6 +514,8 @@ public class CpuWakeupStats { return CPU_WAKEUP_SUBSYSTEM_SENSOR; case SUBSYSTEM_CELLULAR_DATA_STRING: return CPU_WAKEUP_SUBSYSTEM_CELLULAR_DATA; + case SUBSYSTEM_BLUETOOTH_STRING: + return CPU_WAKEUP_SUBSYSTEM_BLUETOOTH; } return CPU_WAKEUP_SUBSYSTEM_UNKNOWN; } @@ -528,6 +532,8 @@ public class CpuWakeupStats { return SUBSYSTEM_SENSOR_STRING; case CPU_WAKEUP_SUBSYSTEM_CELLULAR_DATA: return SUBSYSTEM_CELLULAR_DATA_STRING; + case CPU_WAKEUP_SUBSYSTEM_BLUETOOTH: + return SUBSYSTEM_BLUETOOTH_STRING; case CPU_WAKEUP_SUBSYSTEM_UNKNOWN: return "Unknown"; } diff --git a/services/core/java/com/android/server/stats/pull/StatsPullAtomService.java b/services/core/java/com/android/server/stats/pull/StatsPullAtomService.java index b35a0a772ff2..74c1124e1f16 100644 --- a/services/core/java/com/android/server/stats/pull/StatsPullAtomService.java +++ b/services/core/java/com/android/server/stats/pull/StatsPullAtomService.java @@ -216,13 +216,13 @@ import com.android.role.RoleManagerLocal; import com.android.server.BinderCallsStatsService; import com.android.server.LocalManagerRegistry; import com.android.server.LocalServices; -import com.android.server.PinnerService; -import com.android.server.PinnerService.PinnedFileStats; import com.android.server.SystemService; import com.android.server.SystemServiceManager; import com.android.server.am.MemoryStatUtil.MemoryStat; import com.android.server.health.HealthServiceWrapper; import com.android.server.notification.NotificationManagerService; +import com.android.server.pinner.PinnerService; +import com.android.server.pinner.PinnerService.PinnedFileStats; import com.android.server.pm.UserManagerInternal; import com.android.server.power.stats.KernelWakelockReader; import com.android.server.power.stats.KernelWakelockStats; diff --git a/services/core/java/com/android/server/vibrator/VibratorController.java b/services/core/java/com/android/server/vibrator/VibratorController.java index c120fc7d82f5..6aed00e0e32b 100644 --- a/services/core/java/com/android/server/vibrator/VibratorController.java +++ b/services/core/java/com/android/server/vibrator/VibratorController.java @@ -57,8 +57,7 @@ final class VibratorController { // for a snippet of the current known vibrator state/info. private volatile VibratorInfo mVibratorInfo; private volatile boolean mVibratorInfoLoadSuccessful; - private volatile boolean mIsVibrating; - private volatile boolean mIsUnderExternalControl; + private volatile VibratorState mCurrentState; private volatile float mCurrentAmplitude; /** @@ -75,6 +74,11 @@ final class VibratorController { void onComplete(int vibratorId, long vibrationId); } + /** Representation of the vibrator state based on the interactions through this controller. */ + private enum VibratorState { + IDLE, VIBRATING, UNDER_EXTERNAL_CONTROL + } + VibratorController(int vibratorId, OnVibrationCompleteListener listener) { this(vibratorId, listener, new NativeWrapper()); } @@ -87,6 +91,7 @@ final class VibratorController { VibratorInfo.Builder vibratorInfoBuilder = new VibratorInfo.Builder(vibratorId); mVibratorInfoLoadSuccessful = mNativeWrapper.getInfo(vibratorInfoBuilder); mVibratorInfo = vibratorInfoBuilder.build(); + mCurrentState = VibratorState.IDLE; if (!mVibratorInfoLoadSuccessful) { Slog.e(TAG, @@ -106,7 +111,7 @@ final class VibratorController { return false; } // Notify its callback after new client registered. - notifyStateListener(listener, mIsVibrating); + notifyStateListener(listener, isVibrating(mCurrentState)); } return true; } finally { @@ -166,7 +171,7 @@ final class VibratorController { * automatically notified to any registered {@link IVibratorStateListener} on change. */ public boolean isVibrating() { - return mIsVibrating; + return isVibrating(mCurrentState); } /** @@ -184,11 +189,6 @@ final class VibratorController { return mCurrentAmplitude; } - /** Return {@code true} if this vibrator is under external control, false otherwise. */ - public boolean isUnderExternalControl() { - return mIsUnderExternalControl; - } - /** * Check against this vibrator capabilities. * @@ -214,7 +214,7 @@ final class VibratorController { /** * Set the vibrator control to be external or not, based on given flag. * - * <p>This will affect the state of {@link #isUnderExternalControl()}. + * <p>This will affect the state of {@link #isVibrating()}. */ public void setExternalControl(boolean externalControl) { Trace.traceBegin(TRACE_TAG_VIBRATOR, @@ -224,9 +224,11 @@ final class VibratorController { if (!mVibratorInfo.hasCapability(IVibrator.CAP_EXTERNAL_CONTROL)) { return; } + VibratorState newState = + externalControl ? VibratorState.UNDER_EXTERNAL_CONTROL : VibratorState.IDLE; synchronized (mLock) { - mIsUnderExternalControl = externalControl; mNativeWrapper.setExternalControl(externalControl); + updateStateAndNotifyListenersLocked(newState); } } finally { Trace.traceEnd(TRACE_TAG_VIBRATOR); @@ -264,7 +266,7 @@ final class VibratorController { if (mVibratorInfo.hasCapability(IVibrator.CAP_AMPLITUDE_CONTROL)) { mNativeWrapper.setAmplitude(amplitude); } - if (mIsVibrating) { + if (mCurrentState == VibratorState.VIBRATING) { mCurrentAmplitude = amplitude; } } @@ -289,7 +291,7 @@ final class VibratorController { long duration = mNativeWrapper.on(milliseconds, vibrationId); if (duration > 0) { mCurrentAmplitude = -1; - notifyListenerOnVibrating(true); + updateStateAndNotifyListenersLocked(VibratorState.VIBRATING); } return duration; } @@ -319,7 +321,7 @@ final class VibratorController { vendorEffect.getAdaptiveScale(), vibrationId); if (duration > 0) { mCurrentAmplitude = -1; - notifyListenerOnVibrating(true); + updateStateAndNotifyListenersLocked(VibratorState.VIBRATING); } return duration; } finally { @@ -346,7 +348,7 @@ final class VibratorController { prebaked.getEffectStrength(), vibrationId); if (duration > 0) { mCurrentAmplitude = -1; - notifyListenerOnVibrating(true); + updateStateAndNotifyListenersLocked(VibratorState.VIBRATING); } return duration; } @@ -374,7 +376,7 @@ final class VibratorController { long duration = mNativeWrapper.compose(primitives, vibrationId); if (duration > 0) { mCurrentAmplitude = -1; - notifyListenerOnVibrating(true); + updateStateAndNotifyListenersLocked(VibratorState.VIBRATING); } return duration; } @@ -402,7 +404,7 @@ final class VibratorController { long duration = mNativeWrapper.composePwle(primitives, braking, vibrationId); if (duration > 0) { mCurrentAmplitude = -1; - notifyListenerOnVibrating(true); + updateStateAndNotifyListenersLocked(VibratorState.VIBRATING); } return duration; } @@ -422,7 +424,7 @@ final class VibratorController { synchronized (mLock) { mNativeWrapper.off(); mCurrentAmplitude = 0; - notifyListenerOnVibrating(false); + updateStateAndNotifyListenersLocked(VibratorState.IDLE); } } finally { Trace.traceEnd(TRACE_TAG_VIBRATOR); @@ -443,9 +445,8 @@ final class VibratorController { return "VibratorController{" + "mVibratorInfo=" + mVibratorInfo + ", mVibratorInfoLoadSuccessful=" + mVibratorInfoLoadSuccessful - + ", mIsVibrating=" + mIsVibrating + + ", mCurrentState=" + mCurrentState.name() + ", mCurrentAmplitude=" + mCurrentAmplitude - + ", mIsUnderExternalControl=" + mIsUnderExternalControl + ", mVibratorStateListeners count=" + mVibratorStateListeners.getRegisteredCallbackCount() + '}'; @@ -454,8 +455,7 @@ final class VibratorController { void dump(IndentingPrintWriter pw) { pw.println("Vibrator (id=" + mVibratorInfo.getId() + "):"); pw.increaseIndent(); - pw.println("isVibrating = " + mIsVibrating); - pw.println("isUnderExternalControl = " + mIsUnderExternalControl); + pw.println("currentState = " + mCurrentState.name()); pw.println("currentAmplitude = " + mCurrentAmplitude); pw.println("vibratorInfoLoadSuccessful = " + mVibratorInfoLoadSuccessful); pw.println("vibratorStateListener size = " @@ -464,14 +464,19 @@ final class VibratorController { pw.decreaseIndent(); } + /** + * Updates current vibrator state and notify listeners if {@link #isVibrating()} result changed. + */ @GuardedBy("mLock") - private void notifyListenerOnVibrating(boolean isVibrating) { - if (mIsVibrating != isVibrating) { - mIsVibrating = isVibrating; + private void updateStateAndNotifyListenersLocked(VibratorState state) { + boolean previousIsVibrating = isVibrating(mCurrentState); + final boolean newIsVibrating = isVibrating(state); + mCurrentState = state; + if (previousIsVibrating != newIsVibrating) { // The broadcast method is safe w.r.t. register/unregister listener methods, but lock // is required here to guarantee delivery order. mVibratorStateListeners.broadcast( - listener -> notifyStateListener(listener, isVibrating)); + listener -> notifyStateListener(listener, newIsVibrating)); } } @@ -483,6 +488,11 @@ final class VibratorController { } } + /** Returns true only if given state is not {@link VibratorState#IDLE}. */ + private static boolean isVibrating(VibratorState state) { + return state != VibratorState.IDLE; + } + /** Wrapper around the static-native methods of {@link VibratorController} for tests. */ @VisibleForTesting public static class NativeWrapper { diff --git a/services/core/java/com/android/server/vibrator/VibratorManagerService.java b/services/core/java/com/android/server/vibrator/VibratorManagerService.java index 95c648334327..07473d10b217 100644 --- a/services/core/java/com/android/server/vibrator/VibratorManagerService.java +++ b/services/core/java/com/android/server/vibrator/VibratorManagerService.java @@ -809,17 +809,9 @@ public class VibratorManagerService extends IVibratorManagerService.Stub { mCurrentExternalVibration.getDebugInfo().dump(proto, VibratorManagerServiceDumpProto.CURRENT_EXTERNAL_VIBRATION); } - - boolean isVibrating = false; - boolean isUnderExternalControl = false; for (int i = 0; i < mVibrators.size(); i++) { proto.write(VibratorManagerServiceDumpProto.VIBRATOR_IDS, mVibrators.keyAt(i)); - isVibrating |= mVibrators.valueAt(i).isVibrating(); - isUnderExternalControl |= mVibrators.valueAt(i).isUnderExternalControl(); } - proto.write(VibratorManagerServiceDumpProto.IS_VIBRATING, isVibrating); - proto.write(VibratorManagerServiceDumpProto.VIBRATOR_UNDER_EXTERNAL_CONTROL, - isUnderExternalControl); } mVibratorManagerRecords.dump(proto); mVibratorControlService.dump(proto); diff --git a/services/core/java/com/android/server/webkit/SystemImpl.java b/services/core/java/com/android/server/webkit/SystemImpl.java index 67401530763b..ab5316f46d78 100644 --- a/services/core/java/com/android/server/webkit/SystemImpl.java +++ b/services/core/java/com/android/server/webkit/SystemImpl.java @@ -41,7 +41,8 @@ import android.webkit.WebViewZygote; import com.android.internal.util.XmlUtils; import com.android.server.LocalServices; -import com.android.server.PinnerService; +import com.android.server.pinner.PinnedFile; +import com.android.server.pinner.PinnerService; import org.xmlpull.v1.XmlPullParserException; @@ -318,8 +319,9 @@ public class SystemImpl implements SystemInterface { if (webviewPinQuota <= 0) { break; } - int bytesPinned = pinnerService.pinFile(apk, webviewPinQuota, appInfo, PIN_GROUP); - webviewPinQuota -= bytesPinned; + PinnedFile pf = pinnerService.pinFile( + apk, webviewPinQuota, appInfo, PIN_GROUP, /*pinOptimizedDeps=*/true); + webviewPinQuota -= pf.bytesPinned; } } diff --git a/services/core/java/com/android/server/wm/AppCompatCameraOverrides.java b/services/core/java/com/android/server/wm/AppCompatCameraOverrides.java index 241390c12818..fbf9478b4fd9 100644 --- a/services/core/java/com/android/server/wm/AppCompatCameraOverrides.java +++ b/services/core/java/com/android/server/wm/AppCompatCameraOverrides.java @@ -170,7 +170,7 @@ class AppCompatCameraOverrides { * </ul> */ boolean shouldApplyFreeformTreatmentForCameraCompat() { - return Flags.cameraCompatForFreeform() && !isChangeEnabled(mActivityRecord, + return Flags.enableCameraCompatForDesktopWindowing() && !isChangeEnabled(mActivityRecord, OVERRIDE_CAMERA_COMPAT_DISABLE_FREEFORM_WINDOWING_TREATMENT); } diff --git a/services/core/java/com/android/server/wm/AppCompatCameraPolicy.java b/services/core/java/com/android/server/wm/AppCompatCameraPolicy.java index 67bfd7605128..5338c01666fe 100644 --- a/services/core/java/com/android/server/wm/AppCompatCameraPolicy.java +++ b/services/core/java/com/android/server/wm/AppCompatCameraPolicy.java @@ -48,8 +48,9 @@ class AppCompatCameraPolicy { // without the need to restart the device. final boolean needsDisplayRotationCompatPolicy = wmService.mAppCompatConfiguration.isCameraCompatTreatmentEnabledAtBuildTime(); - final boolean needsCameraCompatFreeformPolicy = Flags.cameraCompatForFreeform() - && DesktopModeHelper.canEnterDesktopMode(wmService.mContext); + final boolean needsCameraCompatFreeformPolicy = + Flags.enableCameraCompatForDesktopWindowing() + && DesktopModeHelper.canEnterDesktopMode(wmService.mContext); if (needsDisplayRotationCompatPolicy || needsCameraCompatFreeformPolicy) { mCameraStateMonitor = new CameraStateMonitor(displayContent, wmService.mH); mActivityRefresher = new ActivityRefresher(wmService, wmService.mH); diff --git a/services/core/java/com/android/server/wm/BackgroundActivityStartController.java b/services/core/java/com/android/server/wm/BackgroundActivityStartController.java index 2259b5a5b08c..515f148ac2ff 100644 --- a/services/core/java/com/android/server/wm/BackgroundActivityStartController.java +++ b/services/core/java/com/android/server/wm/BackgroundActivityStartController.java @@ -1120,7 +1120,9 @@ public class BackgroundActivityStartController { @Nullable Task targetTask, int launchFlags, int balCode, int callingUid, int realCallingUid, TaskDisplayArea preferredTaskDisplayArea) { // BAL Exception allowed in all cases - if (balCode == BAL_ALLOW_ALLOWLISTED_UID) { + if (balCode == BAL_ALLOW_ALLOWLISTED_UID + || (android.security.Flags.asmReintroduceGracePeriod() + && balCode == BAL_ALLOW_GRACE_PERIOD)) { return true; } @@ -1173,10 +1175,15 @@ public class BackgroundActivityStartController { ArrayList<Task> visibleTasks = displayArea.getVisibleTasks(); for (int i = 0; i < visibleTasks.size(); i++) { Task task = visibleTasks.get(i); - if (visibleTasks.size() == 1 && task.isActivityTypeHomeOrRecents()) { - bas.optedIn(task.getTopMostActivity()); - } else { + if (android.security.Flags.asmReintroduceGracePeriod()) { bas = checkTopActivityForAsm(task, callingUid, /*sourceRecord*/null, bas); + } else { + if (visibleTasks.size() == 1 && task.isActivityTypeHomeOrRecents()) { + bas.optedIn(task.getTopMostActivity()); + } else { + bas = checkTopActivityForAsm( + task, callingUid, /*sourceRecord*/null, bas); + } } } } diff --git a/services/core/java/com/android/server/wm/CameraCompatFreeformPolicy.java b/services/core/java/com/android/server/wm/CameraCompatFreeformPolicy.java index e3232e08749e..d6caa1a248b4 100644 --- a/services/core/java/com/android/server/wm/CameraCompatFreeformPolicy.java +++ b/services/core/java/com/android/server/wm/CameraCompatFreeformPolicy.java @@ -124,7 +124,7 @@ final class CameraCompatFreeformPolicy implements CameraStateMonitor.CameraCompa */ @VisibleForTesting boolean shouldApplyFreeformTreatmentForCameraCompat(@NonNull ActivityRecord activity) { - return Flags.cameraCompatForFreeform() && !activity.info.isChangeEnabled( + return Flags.enableCameraCompatForDesktopWindowing() && !activity.info.isChangeEnabled( ActivityInfo.OVERRIDE_CAMERA_COMPAT_DISABLE_FREEFORM_WINDOWING_TREATMENT); } diff --git a/services/core/java/com/android/server/wm/Task.java b/services/core/java/com/android/server/wm/Task.java index 86bb75ab3f8c..14f034bb8445 100644 --- a/services/core/java/com/android/server/wm/Task.java +++ b/services/core/java/com/android/server/wm/Task.java @@ -66,6 +66,7 @@ import static com.android.internal.protolog.ProtoLogGroup.WM_DEBUG_ADD_REMOVE; import static com.android.internal.protolog.ProtoLogGroup.WM_DEBUG_LOCKTASK; import static com.android.internal.protolog.ProtoLogGroup.WM_DEBUG_STATES; import static com.android.internal.protolog.ProtoLogGroup.WM_DEBUG_TASKS; +import static com.android.internal.protolog.ProtoLogGroup.WM_DEBUG_WINDOW_TRANSITIONS_MIN; import static com.android.server.wm.ActivityRecord.State.PAUSED; import static com.android.server.wm.ActivityRecord.State.PAUSING; import static com.android.server.wm.ActivityRecord.State.RESUMED; @@ -6177,6 +6178,8 @@ class Task extends TaskFragment { void maybeApplyLastRecentsAnimationTransaction() { if (mLastRecentsAnimationTransaction != null) { + ProtoLog.d(WM_DEBUG_WINDOW_TRANSITIONS_MIN, + "Applying last recents animation transaction."); final SurfaceControl.Transaction tx = getPendingTransaction(); if (mLastRecentsAnimationOverlay != null) { tx.reparent(mLastRecentsAnimationOverlay, mSurfaceControl); diff --git a/services/core/java/com/android/server/wm/WindowManagerService.java b/services/core/java/com/android/server/wm/WindowManagerService.java index b8f47cce6005..942634704ff5 100644 --- a/services/core/java/com/android/server/wm/WindowManagerService.java +++ b/services/core/java/com/android/server/wm/WindowManagerService.java @@ -9007,7 +9007,9 @@ public class WindowManagerService extends IWindowManager.Stub final boolean isInputTargetNotFocused = mFocusedInputTarget != t && mFocusedInputTarget != null; - if (!isInputTargetNotFocused) { + final boolean isTouchOnFocusedDisplay = mFocusedInputTarget != null + && t.getDisplayId() == mFocusedInputTarget.getDisplayId(); + if (!(isInputTargetNotFocused && isTouchOnFocusedDisplay)) { return false; } diff --git a/services/core/java/com/android/server/wm/WindowOrganizerController.java b/services/core/java/com/android/server/wm/WindowOrganizerController.java index 476443aa2050..f35f2b30c5d4 100644 --- a/services/core/java/com/android/server/wm/WindowOrganizerController.java +++ b/services/core/java/com/android/server/wm/WindowOrganizerController.java @@ -799,7 +799,12 @@ class WindowOrganizerController extends IWindowOrganizerController.Stub } } finally { if (deferTransitionReady) { - chain.mTransition.continueTransitionReady(); + if (chain.mTransition.isCollecting()) { + chain.mTransition.continueTransitionReady(); + } else { + Slog.wtf(TAG, "Too late, transition : " + chain.mTransition.getSyncId() + + " state: " + chain.mTransition.getState() + " is not collecting"); + } } mService.mTaskSupervisor.setDeferRootVisibilityUpdate(false /* deferUpdate */); if (deferResume) { diff --git a/services/core/java/com/android/server/wm/WindowState.java b/services/core/java/com/android/server/wm/WindowState.java index 4c4b4f65edf5..0e3ab63aefb9 100644 --- a/services/core/java/com/android/server/wm/WindowState.java +++ b/services/core/java/com/android/server/wm/WindowState.java @@ -1969,6 +1969,13 @@ class WindowState extends WindowContainer<WindowState> implements WindowManagerP boolean isReadyForDisplay() { final boolean parentAndClientVisible = !isParentWindowHidden() && mViewVisibility == View.VISIBLE; + // TODO(b/338426357): Remove this once the last target using legacy transitions is moved to + // shell transitions + if (!mTransitionController.isShellTransitionsEnabled()) { + return mHasSurface && isVisibleByPolicy() && !mDestroying + && ((parentAndClientVisible && mToken.isVisible()) + || isAnimating(TRANSITION | PARENTS)); + } return mHasSurface && isVisibleByPolicy() && !mDestroying && mToken.isVisible() && (parentAndClientVisible || isAnimating(TRANSITION | PARENTS)); } diff --git a/services/java/com/android/server/SystemServer.java b/services/java/com/android/server/SystemServer.java index 5cf260adece6..ce6f1ecc9463 100644 --- a/services/java/com/android/server/SystemServer.java +++ b/services/java/com/android/server/SystemServer.java @@ -205,6 +205,7 @@ import com.android.server.os.SchedulingPolicyService; import com.android.server.pdb.PersistentDataBlockService; import com.android.server.people.PeopleService; import com.android.server.permission.access.AccessCheckingService; +import com.android.server.pinner.PinnerService; import com.android.server.pm.ApexManager; import com.android.server.pm.ApexSystemServiceInfo; import com.android.server.pm.BackgroundInstallControlService; diff --git a/services/tests/RemoteProvisioningServiceTests/Android.bp b/services/tests/RemoteProvisioningServiceTests/Android.bp index 19c913620760..3a73c3954d52 100644 --- a/services/tests/RemoteProvisioningServiceTests/Android.bp +++ b/services/tests/RemoteProvisioningServiceTests/Android.bp @@ -31,7 +31,6 @@ android_test { "service-rkp.impl", "services.core", "truth", - "truth-java8-extension", ], test_suites: [ "device-tests", diff --git a/services/tests/RemoteProvisioningServiceTests/src/com/android/server/security/rkp/RemoteProvisioningShellCommandTest.java b/services/tests/RemoteProvisioningServiceTests/src/com/android/server/security/rkp/RemoteProvisioningShellCommandTest.java index 007c0db1b731..a1616c676dbd 100644 --- a/services/tests/RemoteProvisioningServiceTests/src/com/android/server/security/rkp/RemoteProvisioningShellCommandTest.java +++ b/services/tests/RemoteProvisioningServiceTests/src/com/android/server/security/rkp/RemoteProvisioningShellCommandTest.java @@ -17,7 +17,6 @@ package com.android.server.security.rkp; import static com.google.common.truth.Truth.assertThat; -import static com.google.common.truth.Truth8.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyBoolean; diff --git a/services/tests/appfunctions/src/android/app/appfunctions/GenericDocumentWrapperTest.kt b/services/tests/appfunctions/src/android/app/appfunctions/GenericDocumentWrapperTest.kt new file mode 100644 index 000000000000..413eb314c41d --- /dev/null +++ b/services/tests/appfunctions/src/android/app/appfunctions/GenericDocumentWrapperTest.kt @@ -0,0 +1,78 @@ +/* + * 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 android.app.appfunctions + +import android.app.appsearch.GenericDocument +import android.os.Parcel +import com.google.common.truth.Truth.assertThat +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 + + +@RunWith(JUnit4::class) +class GenericDocumentWrapperTest { + + @Test + fun parcelUnparcel() { + val doc = + GenericDocument.Builder<GenericDocument.Builder<*>>("", "", "") + .setPropertyLong("test", 42) + .build() + val wrapper = GenericDocumentWrapper(doc) + + val recovered = parcelUnparcel(wrapper) + + assertThat(recovered.value.getPropertyLong("test")).isEqualTo(42) + } + + @Test + fun parcelUnparcel_afterGetValue() { + val doc = + GenericDocument.Builder<GenericDocument.Builder<*>>("", "", "") + .setPropertyLong("test", 42) + .build() + val wrapper = GenericDocumentWrapper(doc) + assertThat(wrapper.value.getPropertyLong("test")).isEqualTo(42) + + val recovered = parcelUnparcel(wrapper) + + assertThat(recovered.value.getPropertyLong("test")).isEqualTo(42) + } + + + @Test + fun getValue() { + val doc = + GenericDocument.Builder<GenericDocument.Builder<*>>("", "", "") + .setPropertyLong("test", 42) + .build() + val wrapper = GenericDocumentWrapper(doc) + + assertThat(wrapper.value.getPropertyLong("test")).isEqualTo(42) + } + + private fun parcelUnparcel(obj: GenericDocumentWrapper): GenericDocumentWrapper { + val parcel = Parcel.obtain() + try { + obj.writeToParcel(parcel, 0) + parcel.setDataPosition(0) + return GenericDocumentWrapper.CREATOR.createFromParcel(parcel) + } finally { + parcel.recycle() + } + } +}
\ No newline at end of file diff --git a/services/tests/powerstatstests/res/xml/irq_device_map_3.xml b/services/tests/powerstatstests/res/xml/irq_device_map_3.xml index fd55428c48df..c3df0785bd9b 100644 --- a/services/tests/powerstatstests/res/xml/irq_device_map_3.xml +++ b/services/tests/powerstatstests/res/xml/irq_device_map_3.xml @@ -32,4 +32,7 @@ <device name="test.sensor.device"> <subsystem>Sensor</subsystem> </device> + <device name="test.bluetooth.device"> + <subsystem>Bluetooth</subsystem> + </device> </irq-device-map>
\ No newline at end of file diff --git a/services/tests/powerstatstests/src/com/android/server/power/stats/wakeups/CpuWakeupStatsTest.java b/services/tests/powerstatstests/src/com/android/server/power/stats/wakeups/CpuWakeupStatsTest.java index 0dc836ba0400..fe4d971face5 100644 --- a/services/tests/powerstatstests/src/com/android/server/power/stats/wakeups/CpuWakeupStatsTest.java +++ b/services/tests/powerstatstests/src/com/android/server/power/stats/wakeups/CpuWakeupStatsTest.java @@ -17,6 +17,7 @@ package com.android.server.power.stats.wakeups; import static android.os.BatteryStatsInternal.CPU_WAKEUP_SUBSYSTEM_ALARM; +import static android.os.BatteryStatsInternal.CPU_WAKEUP_SUBSYSTEM_BLUETOOTH; import static android.os.BatteryStatsInternal.CPU_WAKEUP_SUBSYSTEM_CELLULAR_DATA; import static android.os.BatteryStatsInternal.CPU_WAKEUP_SUBSYSTEM_SENSOR; import static android.os.BatteryStatsInternal.CPU_WAKEUP_SUBSYSTEM_SOUND_TRIGGER; @@ -52,6 +53,7 @@ public class CpuWakeupStatsTest { private static final String KERNEL_REASON_SOUND_TRIGGER_IRQ = "129 test.sound_trigger.device"; private static final String KERNEL_REASON_SENSOR_IRQ = "15 test.sensor.device"; private static final String KERNEL_REASON_CELLULAR_DATA_IRQ = "18 test.cellular_data.device"; + private static final String KERNEL_REASON_BLUETOOTH_IRQ = "19 test.bluetooth.device"; private static final String KERNEL_REASON_UNKNOWN_IRQ = "140 test.unknown.device"; private static final String KERNEL_REASON_UNKNOWN_FORMAT = "free-form-reason test.alarm.device"; private static final String KERNEL_REASON_ALARM_ABNORMAL = "-1 test.alarm.device"; @@ -62,12 +64,14 @@ public class CpuWakeupStatsTest { private static final int TEST_UID_3 = 92261423; private static final int TEST_UID_4 = 56926423; private static final int TEST_UID_5 = 76421423; + private static final int TEST_UID_6 = 62345353; private static final int TEST_PROC_STATE_1 = 72331; private static final int TEST_PROC_STATE_2 = 792351; private static final int TEST_PROC_STATE_3 = 138831; private static final int TEST_PROC_STATE_4 = 23231; private static final int TEST_PROC_STATE_5 = 42; + private static final int TEST_PROC_STATE_6 = 129942; private static final Context sContext = InstrumentationRegistry.getTargetContext(); private final Handler mHandler = Mockito.mock(Handler.class); @@ -79,6 +83,7 @@ public class CpuWakeupStatsTest { obj.mUidProcStates.put(TEST_UID_3, TEST_PROC_STATE_3); obj.mUidProcStates.put(TEST_UID_4, TEST_PROC_STATE_4); obj.mUidProcStates.put(TEST_UID_5, TEST_PROC_STATE_5); + obj.mUidProcStates.put(TEST_UID_6, TEST_PROC_STATE_6); } @Test @@ -118,6 +123,7 @@ public class CpuWakeupStatsTest { CPU_WAKEUP_SUBSYSTEM_SOUND_TRIGGER, CPU_WAKEUP_SUBSYSTEM_SENSOR, CPU_WAKEUP_SUBSYSTEM_CELLULAR_DATA, + CPU_WAKEUP_SUBSYSTEM_BLUETOOTH, }; final String[] kernelReasons = new String[] { @@ -126,10 +132,11 @@ public class CpuWakeupStatsTest { KERNEL_REASON_SOUND_TRIGGER_IRQ, KERNEL_REASON_SENSOR_IRQ, KERNEL_REASON_CELLULAR_DATA_IRQ, + KERNEL_REASON_BLUETOOTH_IRQ, }; final int[] uids = new int[] { - TEST_UID_2, TEST_UID_3, TEST_UID_4, TEST_UID_1, TEST_UID_5 + TEST_UID_2, TEST_UID_3, TEST_UID_4, TEST_UID_1, TEST_UID_5, TEST_UID_6 }; final int[] procStates = new int[] { @@ -137,7 +144,8 @@ public class CpuWakeupStatsTest { TEST_PROC_STATE_3, TEST_PROC_STATE_4, TEST_PROC_STATE_1, - TEST_PROC_STATE_5 + TEST_PROC_STATE_5, + TEST_PROC_STATE_6 }; final int total = subsystems.length; @@ -285,6 +293,40 @@ public class CpuWakeupStatsTest { } @Test + public void bluetoothIrqAttributionSolo() { + final CpuWakeupStats obj = new CpuWakeupStats(sContext, R.xml.irq_device_map_3, mHandler); + final long wakeupTime = 1236121; + + populateDefaultProcStates(obj); + + obj.noteWakeupTimeAndReason(wakeupTime, 1, KERNEL_REASON_BLUETOOTH_IRQ); + + // Outside the window, so should be ignored. + obj.noteWakingActivity(CPU_WAKEUP_SUBSYSTEM_BLUETOOTH, + wakeupTime - obj.mConfig.WAKEUP_MATCHING_WINDOW_MS - 1, TEST_UID_1); + obj.noteWakingActivity(CPU_WAKEUP_SUBSYSTEM_BLUETOOTH, + wakeupTime + obj.mConfig.WAKEUP_MATCHING_WINDOW_MS + 1, TEST_UID_2); + // Should be attributed + obj.noteWakingActivity(CPU_WAKEUP_SUBSYSTEM_BLUETOOTH, wakeupTime + 5, TEST_UID_3, + TEST_UID_5); + + final SparseArray<SparseIntArray> attribution = obj.mWakeupAttribution.get(wakeupTime); + assertThat(attribution).isNotNull(); + assertThat(attribution.size()).isEqualTo(1); + assertThat(attribution.contains(CPU_WAKEUP_SUBSYSTEM_BLUETOOTH)).isTrue(); + assertThat(attribution.get(CPU_WAKEUP_SUBSYSTEM_BLUETOOTH).indexOfKey( + TEST_UID_1)).isLessThan(0); + assertThat(attribution.get(CPU_WAKEUP_SUBSYSTEM_BLUETOOTH).indexOfKey( + TEST_UID_2)).isLessThan(0); + assertThat(attribution.get(CPU_WAKEUP_SUBSYSTEM_BLUETOOTH).get(TEST_UID_3)).isEqualTo( + TEST_PROC_STATE_3); + assertThat(attribution.get(CPU_WAKEUP_SUBSYSTEM_BLUETOOTH).indexOfKey( + TEST_UID_4)).isLessThan(0); + assertThat(attribution.get(CPU_WAKEUP_SUBSYSTEM_BLUETOOTH).get(TEST_UID_5)).isEqualTo( + TEST_PROC_STATE_5); + } + + @Test public void alarmAndWifiIrqAttribution() { final CpuWakeupStats obj = new CpuWakeupStats(sContext, R.xml.irq_device_map_3, mHandler); final long wakeupTime = 92123210; @@ -400,6 +442,47 @@ public class CpuWakeupStatsTest { } @Test + public void unknownAndBluetoothAttribution() { + final CpuWakeupStats obj = new CpuWakeupStats(sContext, R.xml.irq_device_map_3, mHandler); + final long wakeupTime = 92123520; + + populateDefaultProcStates(obj); + + obj.noteWakeupTimeAndReason(wakeupTime, 24, + KERNEL_REASON_UNKNOWN_IRQ + ":" + KERNEL_REASON_BLUETOOTH_IRQ); + + // Bluetooth activity + // Outside the window, so should be ignored. + obj.noteWakingActivity(CPU_WAKEUP_SUBSYSTEM_BLUETOOTH, + wakeupTime - obj.mConfig.WAKEUP_MATCHING_WINDOW_MS - 1, TEST_UID_4); + // Should be attributed + obj.noteWakingActivity(CPU_WAKEUP_SUBSYSTEM_BLUETOOTH, wakeupTime + 2, TEST_UID_1); + obj.noteWakingActivity(CPU_WAKEUP_SUBSYSTEM_BLUETOOTH, wakeupTime - 1, TEST_UID_3, + TEST_UID_5); + + // Unrelated, should be ignored. + obj.noteWakingActivity(CPU_WAKEUP_SUBSYSTEM_ALARM, wakeupTime + 5, TEST_UID_3); + + final SparseArray<SparseIntArray> attribution = obj.mWakeupAttribution.get(wakeupTime); + assertThat(attribution).isNotNull(); + assertThat(attribution.size()).isEqualTo(2); + assertThat(attribution.contains(CPU_WAKEUP_SUBSYSTEM_BLUETOOTH)).isTrue(); + assertThat(attribution.get(CPU_WAKEUP_SUBSYSTEM_BLUETOOTH).get(TEST_UID_1)).isEqualTo( + TEST_PROC_STATE_1); + assertThat(attribution.get(CPU_WAKEUP_SUBSYSTEM_BLUETOOTH) + .indexOfKey(TEST_UID_2)).isLessThan(0); + assertThat(attribution.get(CPU_WAKEUP_SUBSYSTEM_BLUETOOTH).get(TEST_UID_3)).isEqualTo( + TEST_PROC_STATE_3); + assertThat(attribution.get(CPU_WAKEUP_SUBSYSTEM_BLUETOOTH) + .indexOfKey(TEST_UID_4)).isLessThan(0); + assertThat(attribution.get(CPU_WAKEUP_SUBSYSTEM_BLUETOOTH).get(TEST_UID_5)).isEqualTo( + TEST_PROC_STATE_5); + assertThat(attribution.contains(CPU_WAKEUP_SUBSYSTEM_UNKNOWN)).isTrue(); + assertThat(attribution.get(CPU_WAKEUP_SUBSYSTEM_UNKNOWN)).isNull(); + assertThat(attribution.contains(CPU_WAKEUP_SUBSYSTEM_ALARM)).isFalse(); + } + + @Test public void unknownFormatWakeupIgnored() { final CpuWakeupStats obj = new CpuWakeupStats(sContext, R.xml.irq_device_map_3, mHandler); final long wakeupTime = 72123210; diff --git a/services/tests/servicestests/Android.bp b/services/tests/servicestests/Android.bp index ac1b7c6876f7..cbe6700f4d41 100644 --- a/services/tests/servicestests/Android.bp +++ b/services/tests/servicestests/Android.bp @@ -52,6 +52,7 @@ android_test { "services.credentials", "services.devicepolicy", "services.flags", + "com.android.server.flags.services-aconfig-java", "services.net", "services.people", "services.supervision", @@ -81,6 +82,7 @@ android_test { // TODO: remove once Android migrates to JUnit 4.12, // which provides assertThrows "testng", + "flag-junit", "junit", "junit-params", "ActivityContext", diff --git a/services/tests/servicestests/src/com/android/server/PinnerServiceTest.java b/services/tests/servicestests/src/com/android/server/PinnerServiceTest.java index ec78bcea7539..c18faef2c028 100644 --- a/services/tests/servicestests/src/com/android/server/PinnerServiceTest.java +++ b/services/tests/servicestests/src/com/android/server/PinnerServiceTest.java @@ -31,6 +31,9 @@ import android.content.pm.ResolveInfo; import android.os.Binder; import android.os.Handler; import android.os.Looper; +import android.platform.test.annotations.DisableFlags; +import android.platform.test.annotations.EnableFlags; +import android.platform.test.flag.junit.SetFlagsRule; import android.provider.DeviceConfig; import android.provider.DeviceConfigInterface; import android.testing.TestableContext; @@ -43,6 +46,9 @@ import androidx.test.InstrumentationRegistry; import androidx.test.filters.SmallTest; import androidx.test.runner.AndroidJUnit4; +import com.android.server.flags.Flags; +import com.android.server.pinner.PinnedFile; +import com.android.server.pinner.PinnerService; import com.android.server.testutils.FakeDeviceConfigInterface; import com.android.server.wm.ActivityTaskManagerInternal; @@ -73,15 +79,18 @@ public class PinnerServiceTest { private static final long WAIT_FOR_PINNER_TIMEOUT = TimeUnit.SECONDS.toMillis(2); + private static final int MEMORY_PERCENTAGE_FOR_QUOTA = 10; + @Rule public TestableContext mContext = new TestableContext(InstrumentationRegistry.getContext(), null); + @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(); + private final ArraySet<String> mUpdatedPackages = new ArraySet<>(); private ResolveInfo mHomePackageResolveInfo; private FakeDeviceConfigInterface mFakeDeviceConfigInterface; private PinnerService.Injector mInjector; - @Before public void setUp() { MockitoAnnotations.initMocks(this); @@ -114,6 +123,8 @@ public class PinnerServiceTest { resources.addOverride(com.android.internal.R.bool.config_pinnerCameraApp, false); resources.addOverride(com.android.internal.R.integer.config_pinnerHomePinBytes, 0); resources.addOverride(com.android.internal.R.bool.config_pinnerAssistantApp, false); + resources.addOverride(com.android.internal.R.integer.config_pinnerMaxPinnedMemoryPercentage, + MEMORY_PERCENTAGE_FOR_QUOTA); mFakeDeviceConfigInterface = new FakeDeviceConfigInterface(); setDeviceConfigPinnedAnonSize(0); @@ -138,10 +149,9 @@ public class PinnerServiceTest { } @Override - protected PinnerService.PinnedFile pinFileInternal(String fileToPin, - int maxBytesToPin, boolean attemptPinIntrospection) { - return new PinnerService.PinnedFile(-1, - maxBytesToPin, fileToPin, maxBytesToPin); + protected PinnedFile pinFileInternal(PinnerService service, String fileToPin, + long maxBytesToPin, boolean attemptPinIntrospection) { + return new PinnedFile(-1, maxBytesToPin, fileToPin, maxBytesToPin); } }; } @@ -167,6 +177,12 @@ public class PinnerServiceTest { unpinAnonRegionMethod.invoke(pinnerService); } + private long getGlobalPinQuota(PinnerService service) throws Exception { + Method getQuotaMethod = PinnerService.class.getDeclaredMethod("getAvailableGlobalQuota"); + getQuotaMethod.setAccessible(true); + return (long) getQuotaMethod.invoke(service); + } + private void waitForPinnerService(PinnerService pinnerService) throws NoSuchFieldException, IllegalAccessException { // There's no notification/callback when pinning finished @@ -315,15 +331,121 @@ public class PinnerServiceTest { PinnerService pinnerService = new PinnerService(mContext, mInjector); pinnerService.onStart(); - pinnerService.pinFile("test_file", 4096, null, "my_group"); + pinnerService.pinFile("test_file", 4096, null, "my_group", false); - assertThat(getPinnedSize(pinnerService)).isGreaterThan(0); - assertThat(getTotalPinnedFiles(pinnerService)).isGreaterThan(0); + assertThat(getPinnedSize(pinnerService)).isEqualTo(4096); + assertThat(getTotalPinnedFiles(pinnerService)).isEqualTo(1); + + unpinAll(pinnerService); + } + + @Test + @EnableFlags(Flags.FLAG_PIN_GLOBAL_QUOTA) + public void testPinAllQuota() throws Exception { + PinnerService pinnerService = new PinnerService(mContext, mInjector); + pinnerService.onStart(); + + long quota = getGlobalPinQuota(pinnerService); + + pinnerService.pinFile("test_file", Long.MAX_VALUE, null, "my_group", false); + + assertThat(getPinnedSize(pinnerService)).isEqualTo(quota); unpinAll(pinnerService); } @Test + @EnableFlags(Flags.FLAG_PIN_GLOBAL_QUOTA) + public void testGlobalPinQuotaAsDevicePercentage() throws Exception { + PinnerService pinnerService = new PinnerService(mContext, mInjector); + pinnerService.onStart(); + long origQuota = getGlobalPinQuota(pinnerService); + + long totalMem = android.os.Process.getTotalMemory(); + + // Verify that pin quota is the set percentage of device total memory + assertThat(origQuota).isEqualTo((totalMem * MEMORY_PERCENTAGE_FOR_QUOTA) / 100); + + pinnerService.pinFile("test_file", 4096, null, "my_group", false); + assertThat(getGlobalPinQuota(pinnerService)).isEqualTo(origQuota - 4096); + } + + @Test + @EnableFlags(Flags.FLAG_PIN_GLOBAL_QUOTA) + public void testGlobalPinWhenNoQuota() throws Exception { + TestableResources resources = mContext.getOrCreateTestableResources(); + resources.addOverride( + com.android.internal.R.integer.config_pinnerMaxPinnedMemoryPercentage, 0); + + PinnerService pinnerService = new PinnerService(mContext, mInjector); + pinnerService.onStart(); + + // Verify that pin quota is zero + assertThat(getGlobalPinQuota(pinnerService)).isEqualTo(0); + + pinnerService.pinFile("test_file", 4096, null, "my_group", false); + assertThat(getTotalPinnedFiles(pinnerService)).isEqualTo(0); + } + + /** + * This test is temporary, it should be cleaned up when removing the pin_global_quota bugfix + * flag. + */ + @Test + @DisableFlags(Flags.FLAG_PIN_GLOBAL_QUOTA) + public void testGlobalQuotaDisabled() throws Exception { + TestableResources resources = mContext.getOrCreateTestableResources(); + resources.addOverride( + com.android.internal.R.integer.config_pinnerMaxPinnedMemoryPercentage, 0); + + PinnerService pinnerService = new PinnerService(mContext, mInjector); + pinnerService.onStart(); + + // The quota parameter exists but it should have no effect on pinning + long quota = getGlobalPinQuota(pinnerService); + + pinnerService.pinFile("test_file", quota + 1, null, "my_group", false); + + // Verify that we can pin past the quota as it is disabled + assertThat(getPinnedSize(pinnerService)).isEqualTo(quota + 1); + } + + @Test + @EnableFlags(Flags.FLAG_PIN_GLOBAL_QUOTA) + public void testUnpinReleasesQuota() throws Exception { + PinnerService pinnerService = new PinnerService(mContext, mInjector); + pinnerService.onStart(); + long origQuota = getGlobalPinQuota(pinnerService); + + // Verify that pin quota exists and is non zero. + assertThat(getGlobalPinQuota(pinnerService)).isGreaterThan(0); + + pinnerService.pinFile("test_file", origQuota, null, "my_group", false); + + // Make sure all the quota was consumed + assertThat(getPinnedSize(pinnerService)).isEqualTo(origQuota); + + // Unpin the file and verify that the quota has been released. + pinnerService.unpinFile("test_file"); + assertThat(getPinnedSize(pinnerService)).isEqualTo(0); + assertThat(getGlobalPinQuota(pinnerService)).isEqualTo(origQuota); + } + + @Test + @EnableFlags(Flags.FLAG_PIN_GLOBAL_QUOTA) + public void testGlobalPinQuotaNegative() throws Exception { + TestableResources resources = mContext.getOrCreateTestableResources(); + resources.addOverride( + com.android.internal.R.integer.config_pinnerMaxPinnedMemoryPercentage, -10); + + PinnerService pinnerService = new PinnerService(mContext, mInjector); + pinnerService.onStart(); + + // Verify that pin quota is zero + assertThat(getGlobalPinQuota(pinnerService)).isEqualTo(0); + } + + @Test public void testPinAnonRegion() throws Exception { setDeviceConfigPinnedAnonSize(32768); diff --git a/services/tests/servicestests/src/com/android/server/locksettings/RebootEscrowManagerTests.java b/services/tests/servicestests/src/com/android/server/locksettings/RebootEscrowManagerTests.java index d071c159d6f5..ae781dcb834a 100644 --- a/services/tests/servicestests/src/com/android/server/locksettings/RebootEscrowManagerTests.java +++ b/services/tests/servicestests/src/com/android/server/locksettings/RebootEscrowManagerTests.java @@ -60,6 +60,7 @@ import android.os.RemoteException; import android.os.ServiceSpecificException; import android.os.UserManager; import android.platform.test.annotations.Presubmit; +import android.platform.test.annotations.RequiresFlagsDisabled; import android.platform.test.annotations.RequiresFlagsEnabled; import android.platform.test.flag.junit.CheckFlagsRule; import android.platform.test.flag.junit.DeviceFlagsValueProvider; @@ -130,6 +131,7 @@ public class RebootEscrowManagerTests { private SecretKey mAesKey; private MockInjector mMockInjector; private Handler mHandler; + private Network mNetwork; public interface MockableRebootEscrowInjected { int getBootCount(); @@ -342,6 +344,7 @@ public class RebootEscrowManagerTests { when(mCallbacks.isUserSecure(NONSECURE_SECONDARY_USER_ID)).thenReturn(false); when(mCallbacks.isUserSecure(SECURE_SECONDARY_USER_ID)).thenReturn(true); mInjected = mock(MockableRebootEscrowInjected.class); + mNetwork = mock(Network.class); mMockInjector = new MockInjector( mContext, @@ -351,6 +354,10 @@ public class RebootEscrowManagerTests { mKeyStoreManager, mStorage, mInjected); + mMockInjector.mNetworkConsumer = + (callback) -> { + callback.onAvailable(mNetwork); + }; HandlerThread thread = new HandlerThread("RebootEscrowManagerTest"); thread.start(); mHandler = new Handler(thread.getLooper()); @@ -367,6 +374,10 @@ public class RebootEscrowManagerTests { mKeyStoreManager, mStorage, mInjected); + mMockInjector.mNetworkConsumer = + (callback) -> { + callback.onAvailable(mNetwork); + }; mService = new RebootEscrowManager(mMockInjector, mCallbacks, mStorage, mHandler); } @@ -621,7 +632,7 @@ public class RebootEscrowManagerTests { // pretend reboot happens here when(mInjected.getBootCount()).thenReturn(1); - mService.loadRebootEscrowDataIfAvailable(null); + mService.loadRebootEscrowDataIfAvailable(mHandler); verify(mServiceConnection, never()).unwrap(any(), anyLong()); verify(mCallbacks, never()).onRebootEscrowRestored(anyByte(), any(), anyInt()); } @@ -678,7 +689,7 @@ public class RebootEscrowManagerTests { when(mServiceConnection.unwrap(any(), anyLong())) .thenAnswer(invocation -> invocation.getArgument(0)); - mService.loadRebootEscrowDataIfAvailable(null); + mService.loadRebootEscrowDataIfAvailable(mHandler); verify(mServiceConnection).unwrap(any(), anyLong()); verify(mCallbacks).onRebootEscrowRestored(anyByte(), any(), eq(PRIMARY_USER_ID)); @@ -734,7 +745,7 @@ public class RebootEscrowManagerTests { when(mServiceConnection.unwrap(any(), anyLong())) .thenAnswer(invocation -> invocation.getArgument(0)); - mService.loadRebootEscrowDataIfAvailable(null); + mService.loadRebootEscrowDataIfAvailable(mHandler); verify(mServiceConnection).unwrap(any(), anyLong()); verify(mCallbacks).onRebootEscrowRestored(anyByte(), any(), eq(PRIMARY_USER_ID)); @@ -783,7 +794,7 @@ public class RebootEscrowManagerTests { when(mServiceConnection.unwrap(any(), anyLong())) .thenAnswer(invocation -> invocation.getArgument(0)); - mService.loadRebootEscrowDataIfAvailable(null); + mService.loadRebootEscrowDataIfAvailable(mHandler); verify(mServiceConnection).unwrap(any(), anyLong()); assertTrue(metricsSuccessCaptor.getValue()); verify(mKeyStoreManager).clearKeyStoreEncryptionKey(); @@ -827,7 +838,7 @@ public class RebootEscrowManagerTests { anyInt()); when(mServiceConnection.unwrap(any(), anyLong())).thenThrow(RemoteException.class); - mService.loadRebootEscrowDataIfAvailable(null); + mService.loadRebootEscrowDataIfAvailable(mHandler); verify(mServiceConnection).unwrap(any(), anyLong()); assertFalse(metricsSuccessCaptor.getValue()); assertEquals( @@ -836,6 +847,7 @@ public class RebootEscrowManagerTests { } @Test + @RequiresFlagsDisabled(Flags.FLAG_WAIT_FOR_INTERNET_ROR) public void loadRebootEscrowDataIfAvailable_ServerBasedIoError_RetryFailure() throws Exception { setServerBasedRebootEscrowProvider(); @@ -930,114 +942,6 @@ public class RebootEscrowManagerTests { @Test @RequiresFlagsEnabled(Flags.FLAG_WAIT_FOR_INTERNET_ROR) - public void loadRebootEscrowDataIfAvailable_serverBasedWaitForInternet_success() - throws Exception { - setServerBasedRebootEscrowProvider(); - - when(mInjected.getBootCount()).thenReturn(0); - RebootEscrowListener mockListener = mock(RebootEscrowListener.class); - mService.setRebootEscrowListener(mockListener); - mService.prepareRebootEscrow(); - - clearInvocations(mServiceConnection); - callToRebootEscrowIfNeededAndWait(PRIMARY_USER_ID); - verify(mockListener).onPreparedForReboot(eq(true)); - verify(mServiceConnection, never()).wrapBlob(any(), anyLong(), anyLong()); - - // Use x -> x for both wrap & unwrap functions. - when(mServiceConnection.wrapBlob(any(), anyLong(), anyLong())) - .thenAnswer(invocation -> invocation.getArgument(0)); - assertEquals(ARM_REBOOT_ERROR_NONE, mService.armRebootEscrowIfNeeded()); - verify(mServiceConnection).wrapBlob(any(), anyLong(), anyLong()); - assertTrue(mStorage.hasRebootEscrowServerBlob()); - - // pretend reboot happens here - when(mInjected.getBootCount()).thenReturn(1); - ArgumentCaptor<Boolean> metricsSuccessCaptor = ArgumentCaptor.forClass(Boolean.class); - doNothing() - .when(mInjected) - .reportMetric( - metricsSuccessCaptor.capture(), - eq(0) /* error code */, - eq(2) /* Server based */, - eq(1) /* attempt count */, - anyInt(), - eq(0) /* vbmeta status */, - anyInt()); - - // load escrow data - when(mServiceConnection.unwrap(any(), anyLong())) - .thenAnswer(invocation -> invocation.getArgument(0)); - Network mockNetwork = mock(Network.class); - mMockInjector.mNetworkConsumer = - (callback) -> { - callback.onAvailable(mockNetwork); - }; - - mService.loadRebootEscrowDataIfAvailable(mHandler); - verify(mServiceConnection).unwrap(any(), anyLong()); - assertTrue(metricsSuccessCaptor.getValue()); - verify(mKeyStoreManager).clearKeyStoreEncryptionKey(); - assertNull(mMockInjector.mNetworkCallback); - } - - @Test - @RequiresFlagsEnabled(Flags.FLAG_WAIT_FOR_INTERNET_ROR) - public void loadRebootEscrowDataIfAvailable_serverBasedWaitForInternetRemoteException_Failure() - throws Exception { - setServerBasedRebootEscrowProvider(); - - when(mInjected.getBootCount()).thenReturn(0); - RebootEscrowListener mockListener = mock(RebootEscrowListener.class); - mService.setRebootEscrowListener(mockListener); - mService.prepareRebootEscrow(); - - clearInvocations(mServiceConnection); - callToRebootEscrowIfNeededAndWait(PRIMARY_USER_ID); - verify(mockListener).onPreparedForReboot(eq(true)); - verify(mServiceConnection, never()).wrapBlob(any(), anyLong(), anyLong()); - - // Use x -> x for both wrap & unwrap functions. - when(mServiceConnection.wrapBlob(any(), anyLong(), anyLong())) - .thenAnswer(invocation -> invocation.getArgument(0)); - assertEquals(ARM_REBOOT_ERROR_NONE, mService.armRebootEscrowIfNeeded()); - verify(mServiceConnection).wrapBlob(any(), anyLong(), anyLong()); - assertTrue(mStorage.hasRebootEscrowServerBlob()); - - // pretend reboot happens here - when(mInjected.getBootCount()).thenReturn(1); - ArgumentCaptor<Boolean> metricsSuccessCaptor = ArgumentCaptor.forClass(Boolean.class); - ArgumentCaptor<Integer> metricsErrorCodeCaptor = ArgumentCaptor.forClass(Integer.class); - doNothing() - .when(mInjected) - .reportMetric( - metricsSuccessCaptor.capture(), - metricsErrorCodeCaptor.capture(), - eq(2) /* Server based */, - eq(1) /* attempt count */, - anyInt(), - eq(0) /* vbmeta status */, - anyInt()); - - // load escrow data - when(mServiceConnection.unwrap(any(), anyLong())).thenThrow(RemoteException.class); - Network mockNetwork = mock(Network.class); - mMockInjector.mNetworkConsumer = - (callback) -> { - callback.onAvailable(mockNetwork); - }; - - mService.loadRebootEscrowDataIfAvailable(mHandler); - verify(mServiceConnection).unwrap(any(), anyLong()); - assertFalse(metricsSuccessCaptor.getValue()); - assertEquals( - Integer.valueOf(RebootEscrowManager.ERROR_LOAD_ESCROW_KEY), - metricsErrorCodeCaptor.getValue()); - assertNull(mMockInjector.mNetworkCallback); - } - - @Test - @RequiresFlagsEnabled(Flags.FLAG_WAIT_FOR_INTERNET_ROR) public void loadRebootEscrowDataIfAvailable_waitForInternet_networkUnavailable() throws Exception { setServerBasedRebootEscrowProvider(); diff --git a/services/tests/servicestests/src/com/android/server/media/projection/MediaProjectionManagerServiceTest.java b/services/tests/servicestests/src/com/android/server/media/projection/MediaProjectionManagerServiceTest.java index abc9ce3fdc36..ee63d5d32ff1 100644 --- a/services/tests/servicestests/src/com/android/server/media/projection/MediaProjectionManagerServiceTest.java +++ b/services/tests/servicestests/src/com/android/server/media/projection/MediaProjectionManagerServiceTest.java @@ -38,6 +38,7 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.nullable; import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doReturn; @@ -91,6 +92,7 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.Answers; import org.mockito.ArgumentCaptor; import org.mockito.Captor; import org.mockito.Mock; @@ -174,8 +176,8 @@ public class MediaProjectionManagerServiceTest { private PackageManager mPackageManager; @Mock private KeyguardManager mKeyguardManager; - @Mock - AppOpsManager mAppOpsManager; + + private AppOpsManager mAppOpsManager; @Mock private IMediaProjectionWatcherCallback mWatcherCallback; @Mock @@ -193,6 +195,7 @@ public class MediaProjectionManagerServiceTest { LocalServices.removeServiceForTest(WindowManagerInternal.class); LocalServices.addService(WindowManagerInternal.class, mWindowManagerInternal); + mAppOpsManager = mockAppOpsManager(); mContext.addMockSystemService(AppOpsManager.class, mAppOpsManager); mContext.addMockSystemService(KeyguardManager.class, mKeyguardManager); mContext.setMockPackageManager(mPackageManager); @@ -206,6 +209,17 @@ public class MediaProjectionManagerServiceTest { mService = new MediaProjectionManagerService(mContext); } + private static AppOpsManager mockAppOpsManager() { + return mock(AppOpsManager.class, invocationOnMock -> { + if (invocationOnMock.getMethod().getName().startsWith("noteOp")) { + // Mockito will return 0 for non-stubbed method which corresponds to MODE_ALLOWED + // and is not what we want. + return AppOpsManager.MODE_IGNORED; + } + return Answers.RETURNS_DEFAULTS.answer(invocationOnMock); + }); + } + @After public void tearDown() { LocalServices.removeServiceForTest(ActivityManagerInternal.class); @@ -305,8 +319,10 @@ public class MediaProjectionManagerServiceTest { public void testCreateProjection_keyguardLocked_AppOpMediaProjection() throws NameNotFoundException { MediaProjectionManagerService.MediaProjection projection = startProjectionPreconditions(); - doReturn(true).when(mAppOpsManager).isOperationActive(eq(AppOpsManager.OP_PROJECT_MEDIA), - eq(projection.uid), eq(projection.packageName)); + doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOpsManager) + .noteOpNoThrow(eq(AppOpsManager.OP_PROJECT_MEDIA), + eq(projection.uid), eq(projection.packageName), nullable(String.class), + nullable(String.class)); doReturn(true).when(mKeyguardManager).isKeyguardLocked(); doReturn(PackageManager.PERMISSION_DENIED).when(mPackageManager).checkPermission( @@ -1159,7 +1175,7 @@ public class MediaProjectionManagerServiceTest { doReturn(mAppInfo).when(mPackageManager).getApplicationInfoAsUser(anyString(), any(ApplicationInfoFlags.class), any(UserHandle.class)); return service.createProjectionInternal(UID, PACKAGE_NAME, - TYPE_MIRRORING, /* isPermanentGrant= */ true, UserHandle.CURRENT); + TYPE_MIRRORING, /* isPermanentGrant= */ false, UserHandle.CURRENT); } // Set up preconditions for starting a projection, with no foreground service requirements. diff --git a/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java b/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java index 6c9015d72d5a..bbf2cbdbc145 100644 --- a/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java +++ b/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java @@ -193,6 +193,7 @@ import android.app.Notification.MessagingStyle.Message; import android.app.NotificationChannel; import android.app.NotificationChannelGroup; import android.app.NotificationManager; +import android.app.NotificationManager.Policy; import android.app.PendingIntent; import android.app.Person; import android.app.RemoteInput; @@ -655,7 +656,8 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { when(mAtm.getTaskToShowPermissionDialogOn(anyString(), anyInt())) .thenReturn(INVALID_TASK_ID); mContext.addMockSystemService(AppOpsManager.class, mock(AppOpsManager.class)); - when(mUm.getProfileIds(eq(mUserId), eq(false))).thenReturn(new int[] { mUserId }); + when(mUm.getProfileIds(eq(mUserId), anyBoolean())).thenReturn(new int[]{mUserId}); + when(mUmInternal.getProfileIds(eq(mUserId), anyBoolean())).thenReturn(new int[]{mUserId}); when(mAmi.getCurrentUserId()).thenReturn(mUserId); when(mPackageManagerClient.hasSystemFeature(FEATURE_TELECOM)).thenReturn(true); @@ -4652,7 +4654,42 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { doThrow(new SecurityException("no access")).when(mUgmInternal) .checkGrantUriPermission(eq(Process.myUid()), any(), eq(soundUri), - anyInt(), eq(Process.myUserHandle().getIdentifier())); + anyInt(), eq(Process.myUserHandle().getIdentifier())); + + mBinderService.updateNotificationChannelFromPrivilegedListener( + null, mPkg, Process.myUserHandle(), updatedNotificationChannel); + + verify(mPreferencesHelper, times(1)).updateNotificationChannel( + anyString(), anyInt(), any(), anyBoolean(), anyInt(), anyBoolean()); + + verify(mListeners, never()).notifyNotificationChannelChanged(eq(mPkg), + eq(Process.myUserHandle()), eq(mTestNotificationChannel), + eq(NotificationListenerService.NOTIFICATION_CHANNEL_OR_GROUP_UPDATED)); + } + + @Test + public void + testUpdateNotificationChannelFromPrivilegedListener_oldSoundNoUriPerm_newSoundHasUriPerm() + throws Exception { + mService.setPreferencesHelper(mPreferencesHelper); + when(mCompanionMgr.getAssociations(mPkg, mUserId)) + .thenReturn(singletonList(mock(AssociationInfo.class))); + when(mPreferencesHelper.getNotificationChannel(eq(mPkg), anyInt(), + eq(mTestNotificationChannel.getId()), anyBoolean())) + .thenReturn(mTestNotificationChannel); + + // Missing Uri permissions for the old channel sound + final Uri oldSoundUri = Settings.System.DEFAULT_NOTIFICATION_URI; + doThrow(new SecurityException("no access")).when(mUgmInternal) + .checkGrantUriPermission(eq(Process.myUid()), any(), eq(oldSoundUri), + anyInt(), eq(Process.myUserHandle().getIdentifier())); + + // Has Uri permissions for the old channel sound + final Uri newSoundUri = Uri.parse("content://media/test/sound/uri"); + final NotificationChannel updatedNotificationChannel = new NotificationChannel( + TEST_CHANNEL_ID, TEST_CHANNEL_ID, IMPORTANCE_DEFAULT); + updatedNotificationChannel.setSound(newSoundUri, + updatedNotificationChannel.getAudioAttributes()); mBinderService.updateNotificationChannelFromPrivilegedListener( null, mPkg, Process.myUserHandle(), updatedNotificationChannel); @@ -15936,6 +15973,57 @@ public class NotificationManagerServiceTest extends UiServiceTestCase { assertThat(updatedRule.getValue().isEnabled()).isFalse(); } + @Test + @EnableFlags({android.app.Flags.FLAG_MODES_API, android.app.Flags.FLAG_MODES_UI}) + public void setNotificationPolicy_fromSystemApp_appliesPriorityChannelsAllowed() + throws Exception { + setUpRealZenTest(); + // Start with hasPriorityChannels=true, allowPriorityChannels=true ("default"). + mService.mZenModeHelper.setNotificationPolicy(new Policy(0, 0, 0, 0, + Policy.policyState(true, true), 0), + ZenModeConfig.ORIGIN_SYSTEM, Process.SYSTEM_UID); + + // The caller will supply states with "wrong" hasPriorityChannels. + int stateBlockingPriorityChannels = Policy.policyState(false, false); + mBinderService.setNotificationPolicy(mPkg, + new Policy(1, 0, 0, 0, stateBlockingPriorityChannels, 0), false); + + // hasPriorityChannels is untouched and allowPriorityChannels was updated. + assertThat(mBinderService.getNotificationPolicy(mPkg).priorityCategories).isEqualTo(1); + assertThat(mBinderService.getNotificationPolicy(mPkg).state).isEqualTo( + Policy.policyState(true, false)); + + // Same but setting allowPriorityChannels to true. + int stateAllowingPriorityChannels = Policy.policyState(false, true); + mBinderService.setNotificationPolicy(mPkg, + new Policy(2, 0, 0, 0, stateAllowingPriorityChannels, 0), false); + + assertThat(mBinderService.getNotificationPolicy(mPkg).priorityCategories).isEqualTo(2); + assertThat(mBinderService.getNotificationPolicy(mPkg).state).isEqualTo( + Policy.policyState(true, true)); + } + + @Test + @EnableFlags({android.app.Flags.FLAG_MODES_API, android.app.Flags.FLAG_MODES_UI}) + @DisableCompatChanges(NotificationManagerService.MANAGE_GLOBAL_ZEN_VIA_IMPLICIT_RULES) + public void setNotificationPolicy_fromRegularAppThatCanModifyPolicy_ignoresState() + throws Exception { + setUpRealZenTest(); + // Start with hasPriorityChannels=true, allowPriorityChannels=true ("default"). + mService.mZenModeHelper.setNotificationPolicy(new Policy(0, 0, 0, 0, + Policy.policyState(true, true), 0), + ZenModeConfig.ORIGIN_SYSTEM, Process.SYSTEM_UID); + mService.setCallerIsNormalPackage(); + + mBinderService.setNotificationPolicy(mPkg, + new Policy(1, 0, 0, 0, Policy.policyState(false, false), 0), false); + + // Policy was updated but the attempt to change state was ignored (it's a @hide API). + assertThat(mBinderService.getNotificationPolicy(mPkg).priorityCategories).isEqualTo(1); + assertThat(mBinderService.getNotificationPolicy(mPkg).state).isEqualTo( + Policy.policyState(true, true)); + } + /** Prepares for a zen-related test that uses the real {@link ZenModeHelper}. */ private void setUpRealZenTest() throws Exception { when(mConditionProviders.isPackageOrComponentAllowed(anyString(), anyInt())) diff --git a/services/tests/uiservicestests/src/com/android/server/notification/PreferencesHelperTest.java b/services/tests/uiservicestests/src/com/android/server/notification/PreferencesHelperTest.java index a0c0df8853f9..d64b9e858c64 100644 --- a/services/tests/uiservicestests/src/com/android/server/notification/PreferencesHelperTest.java +++ b/services/tests/uiservicestests/src/com/android/server/notification/PreferencesHelperTest.java @@ -45,11 +45,13 @@ import static android.app.NotificationManager.IMPORTANCE_MAX; import static android.app.NotificationManager.IMPORTANCE_NONE; import static android.app.NotificationManager.IMPORTANCE_UNSPECIFIED; import static android.app.NotificationManager.VISIBILITY_NO_OVERRIDE; +import static android.content.ContentResolver.SCHEME_ANDROID_RESOURCE; +import static android.content.ContentResolver.SCHEME_CONTENT; +import static android.content.ContentResolver.SCHEME_FILE; import static android.media.AudioAttributes.CONTENT_TYPE_SONIFICATION; import static android.media.AudioAttributes.USAGE_NOTIFICATION; import static android.os.UserHandle.USER_ALL; import static android.os.UserHandle.USER_SYSTEM; - import static android.platform.test.flag.junit.SetFlagsRule.DefaultInitValueType.DEVICE_DEFAULT; import static android.service.notification.Flags.FLAG_NOTIFICATION_CLASSIFICATION; import static android.service.notification.Flags.notificationClassification; @@ -59,6 +61,7 @@ import static com.android.internal.util.FrameworkStatsLog.PACKAGE_NOTIFICATION_P import static com.android.internal.util.FrameworkStatsLog.PACKAGE_NOTIFICATION_PREFERENCES__FSI_STATE__GRANTED; import static com.android.internal.util.FrameworkStatsLog.PACKAGE_NOTIFICATION_PREFERENCES__FSI_STATE__NOT_REQUESTED; import static com.android.server.notification.Flags.FLAG_ALL_NOTIFS_NEED_TTL; +import static com.android.server.notification.Flags.FLAG_NOTIFICATION_VERIFY_CHANNEL_SOUND_URI; import static com.android.server.notification.Flags.FLAG_PERSIST_INCOMPLETE_RESTORE_DATA; import static com.android.server.notification.NotificationChannelLogger.NotificationChannelEvent.NOTIFICATION_CHANNEL_UPDATED_BY_USER; import static com.android.server.notification.PreferencesHelper.DEFAULT_BUBBLE_PREFERENCE; @@ -84,6 +87,7 @@ import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.clearInvocations; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.reset; @@ -369,10 +373,10 @@ public class PreferencesHelperTest extends UiServiceTestCase { mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper, mPermissionHelper, mPermissionManager, mLogger, mAppOpsManager, mUserProfiles, - false, mClock); + mUgmInternal, false, mClock); mXmlHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper, mPermissionHelper, mPermissionManager, mLogger, mAppOpsManager, mUserProfiles, - false, mClock); + mUgmInternal, false, mClock); resetZenModeHelper(); mAudioAttributes = new AudioAttributes.Builder() @@ -783,7 +787,7 @@ public class PreferencesHelperTest extends UiServiceTestCase { public void testReadXml_oldXml_migrates() throws Exception { mXmlHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper, mPermissionHelper, mPermissionManager, mLogger, mAppOpsManager, mUserProfiles, - /* showReviewPermissionsNotification= */ true, mClock); + mUgmInternal, /* showReviewPermissionsNotification= */ true, mClock); String xml = "<ranking version=\"2\">\n" + "<package name=\"" + PKG_N_MR1 + "\" uid=\"" + UID_N_MR1 @@ -919,7 +923,7 @@ public class PreferencesHelperTest extends UiServiceTestCase { public void testReadXml_newXml_noMigration_showPermissionNotification() throws Exception { mXmlHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper, mPermissionHelper, mPermissionManager, mLogger, mAppOpsManager, mUserProfiles, - /* showReviewPermissionsNotification= */ true, mClock); + mUgmInternal, /* showReviewPermissionsNotification= */ true, mClock); String xml = "<ranking version=\"3\">\n" + "<package name=\"" + PKG_N_MR1 + "\" show_badge=\"true\">\n" @@ -978,7 +982,7 @@ public class PreferencesHelperTest extends UiServiceTestCase { public void testReadXml_newXml_permissionNotificationOff() throws Exception { mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper, mPermissionHelper, mPermissionManager, mLogger, mAppOpsManager, mUserProfiles, - /* showReviewPermissionsNotification= */ false, mClock); + mUgmInternal, /* showReviewPermissionsNotification= */ false, mClock); String xml = "<ranking version=\"3\">\n" + "<package name=\"" + PKG_N_MR1 + "\" show_badge=\"true\">\n" @@ -1037,7 +1041,7 @@ public class PreferencesHelperTest extends UiServiceTestCase { public void testReadXml_newXml_noMigration_noPermissionNotification() throws Exception { mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper, mPermissionHelper, mPermissionManager, mLogger, mAppOpsManager, mUserProfiles, - /* showReviewPermissionsNotification= */ true, mClock); + mUgmInternal, /* showReviewPermissionsNotification= */ true, mClock); String xml = "<ranking version=\"4\">\n" + "<package name=\"" + PKG_N_MR1 + "\" show_badge=\"true\">\n" @@ -1709,7 +1713,7 @@ public class PreferencesHelperTest extends UiServiceTestCase { // simulate load after reboot mXmlHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper, mPermissionHelper, mPermissionManager, mLogger, mAppOpsManager, mUserProfiles, - false, mClock); + mUgmInternal, false, mClock); loadByteArrayXml(baos.toByteArray(), false, USER_ALL); // Trigger 2nd restore pass @@ -1764,7 +1768,7 @@ public class PreferencesHelperTest extends UiServiceTestCase { // simulate load after reboot mXmlHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper, mPermissionHelper, mPermissionManager, mLogger, mAppOpsManager, mUserProfiles, - false, mClock); + mUgmInternal, false, mClock); loadByteArrayXml(xml.getBytes(), false, USER_ALL); // Trigger 2nd restore pass @@ -1842,10 +1846,10 @@ public class PreferencesHelperTest extends UiServiceTestCase { mHelper = new PreferencesHelper(mContext, mPm, mHandler, mMockZenModeHelper, mPermissionHelper, mPermissionManager, mLogger, mAppOpsManager, mUserProfiles, - false, mClock); + mUgmInternal, false, mClock); mXmlHelper = new PreferencesHelper(mContext, mPm, mHandler, mMockZenModeHelper, mPermissionHelper, mPermissionManager, mLogger, mAppOpsManager, mUserProfiles, - false, mClock); + mUgmInternal, false, mClock); NotificationChannel channel = new NotificationChannel("id", "name", IMPORTANCE_LOW); @@ -3049,6 +3053,64 @@ public class PreferencesHelperTest extends UiServiceTestCase { } @Test + @EnableFlags(FLAG_NOTIFICATION_VERIFY_CHANNEL_SOUND_URI) + public void testCreateChannel_noSoundUriPermission_contentSchemeVerified() { + final Uri sound = Uri.parse(SCHEME_CONTENT + "://media/test/sound/uri"); + + doThrow(new SecurityException("no access")).when(mUgmInternal) + .checkGrantUriPermission(eq(UID_N_MR1), any(), eq(sound), + anyInt(), eq(Process.myUserHandle().getIdentifier())); + + final NotificationChannel channel = new NotificationChannel("id2", "name2", + NotificationManager.IMPORTANCE_DEFAULT); + channel.setSound(sound, mAudioAttributes); + + assertThrows(SecurityException.class, + () -> mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, + true, false, UID_N_MR1, false)); + assertThat(mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId(), true)) + .isNull(); + } + + @Test + @EnableFlags(FLAG_NOTIFICATION_VERIFY_CHANNEL_SOUND_URI) + public void testCreateChannel_noSoundUriPermission_fileSchemaIgnored() { + final Uri sound = Uri.parse(SCHEME_FILE + "://path/sound"); + + doThrow(new SecurityException("no access")).when(mUgmInternal) + .checkGrantUriPermission(eq(UID_N_MR1), any(), any(), + anyInt(), eq(Process.myUserHandle().getIdentifier())); + + final NotificationChannel channel = new NotificationChannel("id2", "name2", + NotificationManager.IMPORTANCE_DEFAULT); + channel.setSound(sound, mAudioAttributes); + + mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false, UID_N_MR1, + false); + assertThat(mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId(), true) + .getSound()).isEqualTo(sound); + } + + @Test + @EnableFlags(FLAG_NOTIFICATION_VERIFY_CHANNEL_SOUND_URI) + public void testCreateChannel_noSoundUriPermission_resourceSchemaIgnored() { + final Uri sound = Uri.parse(SCHEME_ANDROID_RESOURCE + "://resId/sound"); + + doThrow(new SecurityException("no access")).when(mUgmInternal) + .checkGrantUriPermission(eq(UID_N_MR1), any(), any(), + anyInt(), eq(Process.myUserHandle().getIdentifier())); + + final NotificationChannel channel = new NotificationChannel("id2", "name2", + NotificationManager.IMPORTANCE_DEFAULT); + channel.setSound(sound, mAudioAttributes); + + mHelper.createNotificationChannel(PKG_N_MR1, UID_N_MR1, channel, true, false, UID_N_MR1, + false); + assertThat(mHelper.getNotificationChannel(PKG_N_MR1, UID_N_MR1, channel.getId(), true) + .getSound()).isEqualTo(sound); + } + + @Test public void testPermanentlyDeleteChannels() throws Exception { NotificationChannel channel1 = new NotificationChannel("id1", "name1", NotificationManager.IMPORTANCE_HIGH); diff --git a/services/tests/vibrator/src/com/android/server/vibrator/VibratorControllerTest.java b/services/tests/vibrator/src/com/android/server/vibrator/VibratorControllerTest.java index 0d13be6d5ab2..e8ca8bf8ec63 100644 --- a/services/tests/vibrator/src/com/android/server/vibrator/VibratorControllerTest.java +++ b/services/tests/vibrator/src/com/android/server/vibrator/VibratorControllerTest.java @@ -127,13 +127,13 @@ public class VibratorControllerTest { public void setExternalControl_withCapability_enablesExternalControl() { mockVibratorCapabilities(IVibrator.CAP_EXTERNAL_CONTROL); VibratorController controller = createController(); - assertFalse(controller.isUnderExternalControl()); + assertFalse(controller.isVibrating()); controller.setExternalControl(true); - assertTrue(controller.isUnderExternalControl()); + assertTrue(controller.isVibrating()); controller.setExternalControl(false); - assertFalse(controller.isUnderExternalControl()); + assertFalse(controller.isVibrating()); InOrder inOrderVerifier = inOrder(mNativeWrapperMock); inOrderVerifier.verify(mNativeWrapperMock).setExternalControl(eq(true)); @@ -143,10 +143,10 @@ public class VibratorControllerTest { @Test public void setExternalControl_withNoCapability_ignoresExternalControl() { VibratorController controller = createController(); - assertFalse(controller.isUnderExternalControl()); + assertFalse(controller.isVibrating()); controller.setExternalControl(true); - assertFalse(controller.isUnderExternalControl()); + assertFalse(controller.isVibrating()); verify(mNativeWrapperMock, never()).setExternalControl(anyBoolean()); } @@ -181,6 +181,38 @@ public class VibratorControllerTest { } @Test + public void setAmplitude_vibratorIdle_ignoresAmplitude() { + VibratorController controller = createController(); + assertFalse(controller.isVibrating()); + + controller.setAmplitude(1); + assertEquals(0, controller.getCurrentAmplitude(), /* delta= */ 0); + } + + @Test + public void setAmplitude_vibratorUnderExternalControl_ignoresAmplitude() { + mockVibratorCapabilities(IVibrator.CAP_EXTERNAL_CONTROL); + VibratorController controller = createController(); + controller.setExternalControl(true); + assertTrue(controller.isVibrating()); + + controller.setAmplitude(1); + assertEquals(0, controller.getCurrentAmplitude(), /* delta= */ 0); + } + + @Test + public void setAmplitude_vibratorVibrating_setsAmplitude() { + when(mNativeWrapperMock.on(anyLong(), anyLong())).thenAnswer(args -> args.getArgument(0)); + VibratorController controller = createController(); + controller.on(100, /* vibrationId= */ 1); + assertTrue(controller.isVibrating()); + assertEquals(-1, controller.getCurrentAmplitude(), /* delta= */ 0); + + controller.setAmplitude(1); + assertEquals(1, controller.getCurrentAmplitude(), /* delta= */ 0); + } + + @Test public void on_withDuration_turnsVibratorOn() { when(mNativeWrapperMock.on(anyLong(), anyLong())).thenAnswer(args -> args.getArgument(0)); VibratorController controller = createController(); diff --git a/services/tests/vibrator/src/com/android/server/vibrator/VibratorManagerServiceTest.java b/services/tests/vibrator/src/com/android/server/vibrator/VibratorManagerServiceTest.java index d99b20c689dd..538c3fc2ddae 100644 --- a/services/tests/vibrator/src/com/android/server/vibrator/VibratorManagerServiceTest.java +++ b/services/tests/vibrator/src/com/android/server/vibrator/VibratorManagerServiceTest.java @@ -266,12 +266,13 @@ public class VibratorManagerServiceTest { @After public void tearDown() throws Exception { if (mService != null) { - if (!mPendingVibrations.stream().allMatch(HalVibration::hasEnded)) { - // Cancel any pending vibration from tests. - cancelVibrate(mService); - for (HalVibration vibration : mPendingVibrations) { - vibration.waitForEnd(); - } + // Make sure we have permission to cancel test vibrations, even if the test denied them. + grantPermission(android.Manifest.permission.VIBRATE); + // Cancel any pending vibration from tests, including external vibrations. + cancelVibrate(mService); + // Wait until pending vibrations end asynchronously. + for (HalVibration vibration : mPendingVibrations) { + vibration.waitForEnd(); } // Wait until all vibrators have stopped vibrating, waiting for ramp-down. // Note: if a test is flaky here something is wrong with the vibration finalization. @@ -2242,7 +2243,7 @@ public class VibratorManagerServiceTest { VibratorManagerService service = createSystemReadyService(); VibrationEffect effect = VibrationEffect.createOneShot(10 * TEST_TIMEOUT_MILLIS, 100); - vibrate(service, effect, HAPTIC_FEEDBACK_ATTRS); + HalVibration vibration = vibrate(service, effect, HAPTIC_FEEDBACK_ATTRS); // VibrationThread will start this vibration async, so wait until vibration is triggered. assertTrue(waitUntil(s -> s.isVibrating(1), service, TEST_TIMEOUT_MILLIS)); @@ -2255,7 +2256,8 @@ public class VibratorManagerServiceTest { assertNotEquals(ExternalVibrationScale.ScaleLevel.SCALE_MUTE, scale.scaleLevel); // Vibration is cancelled. - assertTrue(waitUntil(s -> !s.isVibrating(1), service, TEST_TIMEOUT_MILLIS)); + vibration.waitForEnd(); + assertThat(vibration.getStatus()).isEqualTo(Status.CANCELLED_SUPERSEDED); assertEquals(Arrays.asList(false, true), mVibratorProviders.get(1).getExternalControlStates()); } @@ -2296,7 +2298,7 @@ public class VibratorManagerServiceTest { VibrationEffect repeatingEffect = VibrationEffect.createWaveform( new long[]{100, 200, 300}, new int[]{128, 255, 255}, 1); - vibrate(service, repeatingEffect, ALARM_ATTRS); + HalVibration repeatingVibration = vibrate(service, repeatingEffect, ALARM_ATTRS); // VibrationThread will start this vibration async, so wait until vibration is triggered. assertTrue(waitUntil(s -> s.isVibrating(1), service, TEST_TIMEOUT_MILLIS)); @@ -2308,7 +2310,8 @@ public class VibratorManagerServiceTest { assertNotEquals(ExternalVibrationScale.ScaleLevel.SCALE_MUTE, scale.scaleLevel); // Vibration is cancelled. - assertTrue(waitUntil(s -> !s.isVibrating(1), service, TEST_TIMEOUT_MILLIS)); + repeatingVibration.waitForEnd(); + assertThat(repeatingVibration.getStatus()).isEqualTo(Status.CANCELLED_SUPERSEDED); assertEquals(Arrays.asList(false, true), mVibratorProviders.get(1).getExternalControlStates()); } diff --git a/services/tests/wmtests/src/com/android/server/wm/AppCompatCameraOverridesTest.java b/services/tests/wmtests/src/com/android/server/wm/AppCompatCameraOverridesTest.java index d66c21a77fcd..b91a5b7afe26 100644 --- a/services/tests/wmtests/src/com/android/server/wm/AppCompatCameraOverridesTest.java +++ b/services/tests/wmtests/src/com/android/server/wm/AppCompatCameraOverridesTest.java @@ -28,7 +28,7 @@ import static android.view.WindowManager.PROPERTY_CAMERA_COMPAT_ENABLE_REFRESH_V import static android.view.WindowManager.PROPERTY_COMPAT_ALLOW_MIN_ASPECT_RATIO_OVERRIDE; import static com.android.dx.mockito.inline.extended.ExtendedMockito.spyOn; -import static com.android.window.flags.Flags.FLAG_CAMERA_COMPAT_FOR_FREEFORM; +import static com.android.window.flags.Flags.FLAG_ENABLE_CAMERA_COMPAT_FOR_DESKTOP_WINDOWING; import android.compat.testing.PlatformCompatChangeRule; import android.platform.test.annotations.DisableFlags; @@ -218,7 +218,7 @@ public class AppCompatCameraOverridesTest extends WindowTestsBase { } @Test - @DisableFlags(FLAG_CAMERA_COMPAT_FOR_FREEFORM) + @DisableFlags(FLAG_ENABLE_CAMERA_COMPAT_FOR_DESKTOP_WINDOWING) public void testShouldApplyCameraCompatFreeformTreatment_flagIsDisabled_returnsFalse() { runTestScenario((robot) -> { robot.activity().createActivityWithComponentInNewTask(); @@ -229,7 +229,7 @@ public class AppCompatCameraOverridesTest extends WindowTestsBase { @Test @EnableCompatChanges({OVERRIDE_CAMERA_COMPAT_DISABLE_FREEFORM_WINDOWING_TREATMENT}) - @EnableFlags(FLAG_CAMERA_COMPAT_FOR_FREEFORM) + @EnableFlags(FLAG_ENABLE_CAMERA_COMPAT_FOR_DESKTOP_WINDOWING) public void testShouldApplyCameraCompatFreeformTreatment_overrideEnabled_returnsFalse() { runTestScenario((robot) -> { robot.activity().createActivityWithComponentInNewTask(); @@ -240,7 +240,7 @@ public class AppCompatCameraOverridesTest extends WindowTestsBase { @Test @EnableCompatChanges({OVERRIDE_CAMERA_COMPAT_DISABLE_FREEFORM_WINDOWING_TREATMENT}) - @EnableFlags(FLAG_CAMERA_COMPAT_FOR_FREEFORM) + @EnableFlags(FLAG_ENABLE_CAMERA_COMPAT_FOR_DESKTOP_WINDOWING) public void testShouldApplyCameraCompatFreeformTreatment_disabledByOverride_returnsFalse() { runTestScenario((robot) -> { robot.activity().createActivityWithComponentInNewTask(); @@ -250,7 +250,7 @@ public class AppCompatCameraOverridesTest extends WindowTestsBase { } @Test - @EnableFlags(FLAG_CAMERA_COMPAT_FOR_FREEFORM) + @EnableFlags(FLAG_ENABLE_CAMERA_COMPAT_FOR_DESKTOP_WINDOWING) public void testShouldApplyCameraCompatFreeformTreatment_notDisabledByOverride_returnsTrue() { runTestScenario((robot) -> { robot.activity().createActivityWithComponentInNewTask(); diff --git a/services/tests/wmtests/src/com/android/server/wm/AppCompatCameraPolicyTest.java b/services/tests/wmtests/src/com/android/server/wm/AppCompatCameraPolicyTest.java index d91b38efd40b..41102d6922da 100644 --- a/services/tests/wmtests/src/com/android/server/wm/AppCompatCameraPolicyTest.java +++ b/services/tests/wmtests/src/com/android/server/wm/AppCompatCameraPolicyTest.java @@ -20,7 +20,7 @@ import static android.content.pm.ActivityInfo.OVERRIDE_MIN_ASPECT_RATIO_ONLY_FOR import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn; import static com.android.dx.mockito.inline.extended.ExtendedMockito.spyOn; -import static com.android.window.flags.Flags.FLAG_CAMERA_COMPAT_FOR_FREEFORM; +import static com.android.window.flags.Flags.FLAG_ENABLE_CAMERA_COMPAT_FOR_DESKTOP_WINDOWING; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -85,7 +85,7 @@ public class AppCompatCameraPolicyTest extends WindowTestsBase { } @Test - @EnableFlags(FLAG_CAMERA_COMPAT_FOR_FREEFORM) + @EnableFlags(FLAG_ENABLE_CAMERA_COMPAT_FOR_DESKTOP_WINDOWING) public void testCameraCompatFreeformPolicy_presentWhenEnabledAndDW() { runTestScenario((robot) -> { robot.allowEnterDesktopMode(/* isAllowed= */ true); @@ -95,7 +95,7 @@ public class AppCompatCameraPolicyTest extends WindowTestsBase { } @Test - @EnableFlags(FLAG_CAMERA_COMPAT_FOR_FREEFORM) + @EnableFlags(FLAG_ENABLE_CAMERA_COMPAT_FOR_DESKTOP_WINDOWING) public void testCameraCompatFreeformPolicy_notPresentWhenNoDW() { runTestScenario((robot) -> { robot.allowEnterDesktopMode(/* isAllowed= */ false); @@ -105,7 +105,7 @@ public class AppCompatCameraPolicyTest extends WindowTestsBase { } @Test - @DisableFlags(FLAG_CAMERA_COMPAT_FOR_FREEFORM) + @DisableFlags(FLAG_ENABLE_CAMERA_COMPAT_FOR_DESKTOP_WINDOWING) public void testCameraCompatFreeformPolicy_notPresentWhenNoFlag() { runTestScenario((robot) -> { robot.allowEnterDesktopMode(/* isAllowed= */ true); @@ -115,7 +115,7 @@ public class AppCompatCameraPolicyTest extends WindowTestsBase { } @Test - @EnableFlags(FLAG_CAMERA_COMPAT_FOR_FREEFORM) + @EnableFlags(FLAG_ENABLE_CAMERA_COMPAT_FOR_DESKTOP_WINDOWING) public void testCameraCompatFreeformPolicy_notPresentWhenNoFlagAndNoDW() { runTestScenario((robot) -> { robot.allowEnterDesktopMode(/* isAllowed= */ false); @@ -125,7 +125,7 @@ public class AppCompatCameraPolicyTest extends WindowTestsBase { } @Test - @EnableFlags(FLAG_CAMERA_COMPAT_FOR_FREEFORM) + @EnableFlags(FLAG_ENABLE_CAMERA_COMPAT_FOR_DESKTOP_WINDOWING) public void testCameraCompatFreeformPolicy_startedWhenEnabledAndDW() { runTestScenario((robot) -> { robot.allowEnterDesktopMode(/* isAllowed= */ true); @@ -136,7 +136,7 @@ public class AppCompatCameraPolicyTest extends WindowTestsBase { } @Test - @EnableFlags(FLAG_CAMERA_COMPAT_FOR_FREEFORM) + @EnableFlags(FLAG_ENABLE_CAMERA_COMPAT_FOR_DESKTOP_WINDOWING) public void testCameraStateManager_existsWhenCameraCompatFreeformExists() { runTestScenario((robot) -> { robot.allowEnterDesktopMode(true); @@ -147,7 +147,7 @@ public class AppCompatCameraPolicyTest extends WindowTestsBase { } @Test - @EnableFlags(FLAG_CAMERA_COMPAT_FOR_FREEFORM) + @EnableFlags(FLAG_ENABLE_CAMERA_COMPAT_FOR_DESKTOP_WINDOWING) public void testCameraStateManager_startedWhenCameraCompatFreeformExists() { runTestScenario((robot) -> { robot.allowEnterDesktopMode(true); @@ -180,7 +180,7 @@ public class AppCompatCameraPolicyTest extends WindowTestsBase { } @Test - @DisableFlags(FLAG_CAMERA_COMPAT_FOR_FREEFORM) + @DisableFlags(FLAG_ENABLE_CAMERA_COMPAT_FOR_DESKTOP_WINDOWING) public void testCameraStateManager_doesNotExistWhenNoPolicyExists() { runTestScenario((robot) -> { robot.conf().enableCameraCompatTreatmentAtBuildTime(/* enabled= */ false); diff --git a/services/tests/wmtests/src/com/android/server/wm/CameraCompatFreeformPolicyTests.java b/services/tests/wmtests/src/com/android/server/wm/CameraCompatFreeformPolicyTests.java index a48813d775d1..dbcef10a6be2 100644 --- a/services/tests/wmtests/src/com/android/server/wm/CameraCompatFreeformPolicyTests.java +++ b/services/tests/wmtests/src/com/android/server/wm/CameraCompatFreeformPolicyTests.java @@ -35,7 +35,7 @@ import static com.android.dx.mockito.inline.extended.ExtendedMockito.doAnswer; import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn; import static com.android.dx.mockito.inline.extended.ExtendedMockito.spyOn; import static com.android.dx.mockito.inline.extended.ExtendedMockito.when; -import static com.android.window.flags.Flags.FLAG_CAMERA_COMPAT_FOR_FREEFORM; +import static com.android.window.flags.Flags.FLAG_ENABLE_CAMERA_COMPAT_FOR_DESKTOP_WINDOWING; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -60,6 +60,7 @@ import android.content.res.Configuration.Orientation; import android.graphics.Rect; import android.hardware.camera2.CameraManager; import android.os.Handler; +import android.platform.test.annotations.EnableFlags; import android.platform.test.annotations.Presubmit; import android.view.DisplayInfo; import android.view.Surface; @@ -135,7 +136,6 @@ public class CameraCompatFreeformPolicyTests extends WindowTestsBase { }); mActivityRefresher = new ActivityRefresher(mDisplayContent.mWmService, mMockHandler); - mSetFlagsRule.enableFlags(FLAG_CAMERA_COMPAT_FOR_FREEFORM); CameraStateMonitor cameraStateMonitor = new CameraStateMonitor(mDisplayContent, mMockHandler); mCameraCompatFreeformPolicy = @@ -147,6 +147,7 @@ public class CameraCompatFreeformPolicyTests extends WindowTestsBase { cameraStateMonitor.startListeningToCameraState(); } + @EnableFlags(FLAG_ENABLE_CAMERA_COMPAT_FOR_DESKTOP_WINDOWING) @Test public void testFullscreen_doesNotActivateCameraCompatMode() { configureActivity(SCREEN_ORIENTATION_PORTRAIT, WINDOWING_MODE_FULLSCREEN); @@ -157,6 +158,7 @@ public class CameraCompatFreeformPolicyTests extends WindowTestsBase { assertNotInCameraCompatMode(); } + @EnableFlags(FLAG_ENABLE_CAMERA_COMPAT_FOR_DESKTOP_WINDOWING) @Test public void testOrientationUnspecified_doesNotActivateCameraCompatMode() { configureActivity(SCREEN_ORIENTATION_UNSPECIFIED); @@ -164,12 +166,14 @@ public class CameraCompatFreeformPolicyTests extends WindowTestsBase { assertNotInCameraCompatMode(); } + @EnableFlags(FLAG_ENABLE_CAMERA_COMPAT_FOR_DESKTOP_WINDOWING) @Test public void testNoCameraConnection_doesNotActivateCameraCompatMode() { configureActivity(SCREEN_ORIENTATION_PORTRAIT); assertNotInCameraCompatMode(); } + @EnableFlags(FLAG_ENABLE_CAMERA_COMPAT_FOR_DESKTOP_WINDOWING) @Test public void testCameraConnected_deviceInPortrait_portraitCameraCompatMode() throws Exception { configureActivity(SCREEN_ORIENTATION_PORTRAIT); @@ -210,6 +214,7 @@ public class CameraCompatFreeformPolicyTests extends WindowTestsBase { assertActivityRefreshRequested(/* refreshRequested */ false); } + @EnableFlags(FLAG_ENABLE_CAMERA_COMPAT_FOR_DESKTOP_WINDOWING) @Test public void testCameraReconnected_cameraCompatModeAndRefresh() throws Exception { configureActivity(SCREEN_ORIENTATION_PORTRAIT); @@ -235,6 +240,7 @@ public class CameraCompatFreeformPolicyTests extends WindowTestsBase { } @Test + @EnableFlags(FLAG_ENABLE_CAMERA_COMPAT_FOR_DESKTOP_WINDOWING) @EnableCompatChanges({OVERRIDE_CAMERA_COMPAT_DISABLE_FREEFORM_WINDOWING_TREATMENT}) public void testShouldApplyCameraCompatFreeformTreatment_overrideEnabled_returnsFalse() { configureActivity(SCREEN_ORIENTATION_PORTRAIT); @@ -246,6 +252,7 @@ public class CameraCompatFreeformPolicyTests extends WindowTestsBase { } @Test + @EnableFlags(FLAG_ENABLE_CAMERA_COMPAT_FOR_DESKTOP_WINDOWING) public void testShouldApplyCameraCompatFreeformTreatment_notDisabledByOverride_returnsTrue() { configureActivity(SCREEN_ORIENTATION_PORTRAIT); @@ -254,6 +261,7 @@ public class CameraCompatFreeformPolicyTests extends WindowTestsBase { } @Test + @EnableFlags(FLAG_ENABLE_CAMERA_COMPAT_FOR_DESKTOP_WINDOWING) public void testOnActivityConfigurationChanging_refreshDisabledViaFlag_noRefresh() throws Exception { configureActivity(SCREEN_ORIENTATION_PORTRAIT); @@ -268,6 +276,7 @@ public class CameraCompatFreeformPolicyTests extends WindowTestsBase { } @Test + @EnableFlags(FLAG_ENABLE_CAMERA_COMPAT_FOR_DESKTOP_WINDOWING) public void testOnActivityConfigurationChanging_cycleThroughStopDisabled() throws Exception { when(mAppCompatConfiguration.isCameraCompatRefreshCycleThroughStopEnabled()) .thenReturn(false); @@ -281,6 +290,7 @@ public class CameraCompatFreeformPolicyTests extends WindowTestsBase { } @Test + @EnableFlags(FLAG_ENABLE_CAMERA_COMPAT_FOR_DESKTOP_WINDOWING) public void testOnActivityConfigurationChanging_cycleThroughStopDisabledForApp() throws Exception { configureActivity(SCREEN_ORIENTATION_PORTRAIT); diff --git a/services/tests/wmtests/src/com/android/server/wm/DisplayContentTests.java b/services/tests/wmtests/src/com/android/server/wm/DisplayContentTests.java index 85cb1bcc01fb..5c0d424f4f42 100644 --- a/services/tests/wmtests/src/com/android/server/wm/DisplayContentTests.java +++ b/services/tests/wmtests/src/com/android/server/wm/DisplayContentTests.java @@ -83,7 +83,7 @@ import static com.android.server.wm.SurfaceAnimator.ANIMATION_TYPE_TOKEN_TRANSFO import static com.android.server.wm.WindowContainer.AnimationFlags.PARENTS; import static com.android.server.wm.WindowContainer.POSITION_TOP; import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_NORMAL; -import static com.android.window.flags.Flags.FLAG_CAMERA_COMPAT_FOR_FREEFORM; +import static com.android.window.flags.Flags.FLAG_ENABLE_CAMERA_COMPAT_FOR_DESKTOP_WINDOWING; import static com.android.window.flags.Flags.FLAG_ENABLE_DESKTOP_WINDOWING_MODE; import static com.google.common.truth.Truth.assertThat; @@ -2820,7 +2820,7 @@ public class DisplayContentTests extends WindowTestsBase { verify(mWm.mUmInternal, never()).isUserVisible(userId2, displayId); } - @EnableFlags(FLAG_CAMERA_COMPAT_FOR_FREEFORM) + @EnableFlags(FLAG_ENABLE_CAMERA_COMPAT_FOR_DESKTOP_WINDOWING) @Test public void cameraCompatFreeformFlagEnabled_cameraCompatFreeformPolicyNotNull() { doReturn(true).when(() -> @@ -2829,7 +2829,7 @@ public class DisplayContentTests extends WindowTestsBase { assertTrue(createNewDisplay().mAppCompatCameraPolicy.hasCameraCompatFreeformPolicy()); } - @DisableFlags(FLAG_CAMERA_COMPAT_FOR_FREEFORM) + @DisableFlags(FLAG_ENABLE_CAMERA_COMPAT_FOR_DESKTOP_WINDOWING) @Test public void cameraCompatFreeformFlagNotEnabled_cameraCompatFreeformPolicyIsNull() { doReturn(true).when(() -> @@ -2838,7 +2838,7 @@ public class DisplayContentTests extends WindowTestsBase { assertFalse(createNewDisplay().mAppCompatCameraPolicy.hasCameraCompatFreeformPolicy()); } - @EnableFlags(FLAG_CAMERA_COMPAT_FOR_FREEFORM) + @EnableFlags(FLAG_ENABLE_CAMERA_COMPAT_FOR_DESKTOP_WINDOWING) @DisableFlags(FLAG_ENABLE_DESKTOP_WINDOWING_MODE) @Test public void desktopWindowingFlagNotEnabled_cameraCompatFreeformPolicyIsNull() { diff --git a/telephony/java/android/telephony/satellite/SatelliteManager.java b/telephony/java/android/telephony/satellite/SatelliteManager.java index 4eefaaca71f4..bd5c7597ba14 100644 --- a/telephony/java/android/telephony/satellite/SatelliteManager.java +++ b/telephony/java/android/telephony/satellite/SatelliteManager.java @@ -1113,6 +1113,12 @@ public final class SatelliteManager { * @hide */ public static final int DATAGRAM_TYPE_SMS = 6; + /** + * Datagram type indicating that the message to be sent is an SMS checking + * for pending incoming SMS. + * @hide + */ + public static final int DATAGRAM_TYPE_CHECK_PENDING_INCOMING_SMS = 7; /** @hide */ @IntDef(prefix = "DATAGRAM_TYPE_", value = { @@ -1122,7 +1128,8 @@ public final class SatelliteManager { DATAGRAM_TYPE_KEEP_ALIVE, DATAGRAM_TYPE_LAST_SOS_MESSAGE_STILL_NEED_HELP, DATAGRAM_TYPE_LAST_SOS_MESSAGE_NO_HELP_NEEDED, - DATAGRAM_TYPE_SMS + DATAGRAM_TYPE_SMS, + DATAGRAM_TYPE_CHECK_PENDING_INCOMING_SMS }) @Retention(RetentionPolicy.SOURCE) public @interface DatagramType {} diff --git a/tests/testables/Android.bp b/tests/testables/Android.bp index 7596ee722d01..f2111856c666 100644 --- a/tests/testables/Android.bp +++ b/tests/testables/Android.bp @@ -25,7 +25,10 @@ package { java_library { name: "testables", - srcs: ["src/**/*.java"], + srcs: [ + "src/**/*.java", + "src/**/*.kt", + ], libs: [ "android.test.runner.stubs.system", "android.test.mock.stubs.system", diff --git a/tests/testables/src/android/testing/TestWithLooperRule.java b/tests/testables/src/android/testing/TestWithLooperRule.java index 37b39c314e53..10df17f991d3 100644 --- a/tests/testables/src/android/testing/TestWithLooperRule.java +++ b/tests/testables/src/android/testing/TestWithLooperRule.java @@ -34,13 +34,13 @@ import java.util.List; * Looper for the Statement. */ public class TestWithLooperRule implements MethodRule { - /* * This rule requires to be the inner most Rule, so the next statement is RunAfters * instead of another rule. You can set it by '@Rule(order = Integer.MAX_VALUE)' */ @Override public Statement apply(Statement base, FrameworkMethod method, Object target) { + // getting testRunner check, if AndroidTestingRunning then we skip this rule RunWith runWithAnnotation = target.getClass().getAnnotation(RunWith.class); if (runWithAnnotation != null) { @@ -97,6 +97,9 @@ public class TestWithLooperRule implements MethodRule { case "InvokeParameterizedMethod": this.wrapFieldMethodFor(next, "frameworkMethod", method, target); return; + case "ExpectException": + next = this.getNextStatement(next, "next"); + break; default: throw new Exception( String.format("Unexpected Statement received: [%s]", diff --git a/tests/testables/tests/Android.bp b/tests/testables/tests/Android.bp index 1eb36fa5f908..c23f41a6c3d4 100644 --- a/tests/testables/tests/Android.bp +++ b/tests/testables/tests/Android.bp @@ -34,6 +34,7 @@ android_test { "androidx.core_core-animation", "androidx.core_core-ktx", "androidx.test.rules", + "androidx.test.ext.junit", "hamcrest-library", "mockito-target-inline-minus-junit4", "testables", diff --git a/tests/testables/tests/src/android/testing/TestableLooperJUnit4Test.java b/tests/testables/tests/src/android/testing/TestableLooperJUnit4Test.java new file mode 100644 index 000000000000..b7d5e0e12942 --- /dev/null +++ b/tests/testables/tests/src/android/testing/TestableLooperJUnit4Test.java @@ -0,0 +1,42 @@ +/* + * 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 android.testing; + +import android.testing.TestableLooper.RunWithLooper; + +import androidx.test.ext.junit.runners.AndroidJUnit4; +import androidx.test.filters.SmallTest; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** + * Test that TestableLooper now handles expected exceptions in tests + */ +@SmallTest +@RunWith(AndroidJUnit4.class) +@RunWithLooper +public class TestableLooperJUnit4Test { + @Rule + public final TestWithLooperRule mTestWithLooperRule = new TestWithLooperRule(); + + @Test(expected = Exception.class) + public void testException() throws Exception { + throw new Exception("this exception is expected"); + } +} + |