FRRO Example

here is a code example demonstrating how to create and use a Fabricated RRO in Android. This example will show how to create a Fabricated Overlay that changes some system UI colors and dimensions.

import android.content.om.FabricatedOverlay;
import android.content.om.OverlayManager;
import android.graphics.Color;
import android.os.RemoteException;
import android.util.Log;

public class FabricatedRROExample {
    private static final String TAG = "FabricatedRROExample";
    private static final String TARGET_PACKAGE = "com.android.systemui";
    private static final String OVERLAY_NAME = "FabricatedThemeExample";
    private static final String OVERLAY_PACKAGE = "com.example.fabricated.theme";

    public static void createAndApplyFabricatedOverlay(OverlayManager overlayManager) {
        try {
            // Create a new FabricatedOverlay using the Builder
            FabricatedOverlay overlay = new FabricatedOverlay.Builder(TARGET_PACKAGE, OVERLAY_NAME, OVERLAY_PACKAGE)
                // Set color resources
                .setResourceValue("color", "accent_color", Color.BLUE)
                .setResourceValue("color", "primary_color", Color.CYAN)
                .setResourceValue("color", "background_color", Color.WHITE)

                // Set dimension resources
                .setResourceValue("dimen", "status_bar_height", 40) // in dp
                .setResourceValue("dimen", "quick_qs_total_height", 128) // in dp

                // Set boolean resource
                .setResourceValue("bool", "config_enableTranslucentDecor", true)

                // Set string resource
                .setResourceValue("string", "clock_12_hours_format", "h:mm a")

                .build();

            // Register the fabricated overlay
            overlayManager.registerFabricatedOverlay(overlay);
            Log.d(TAG, "Fabricated overlay registered successfully");

            // Enable the overlay
            overlayManager.setEnabled(OVERLAY_PACKAGE, true, 0);
            Log.d(TAG, "Fabricated overlay enabled successfully");

        } catch (RemoteException e) {
            Log.e(TAG, "Error creating or applying fabricated overlay", e);
        }
    }

    public static void removeFabricatedOverlay(OverlayManager overlayManager) {
        try {
            // Disable the overlay
            overlayManager.setEnabled(OVERLAY_PACKAGE, false, 0);
            Log.d(TAG, "Fabricated overlay disabled");

            // Unregister the overlay
            overlayManager.unregisterFabricatedOverlay(OVERLAY_PACKAGE);
            Log.d(TAG, "Fabricated overlay unregistered successfully");

        } catch (RemoteException e) {
            Log.e(TAG, "Error removing fabricated overlay", e);
        }
    }
}

This example demonstrates several key aspects of working with Fabricated RROs:

  1. Creating a Fabricated Overlay:

    • We use FabricatedOverlay.Builder to create the overlay.

    • We specify the target package (SystemUI in this case), a name for the overlay, and a unique package name for the overlay.

  2. Setting Resources:

    • We set various types of resources: colors, dimensions, booleans, and strings.

    • Each resource is set using the setResourceValue() method, specifying the resource type, name, and value.

  3. Registering the Overlay:

    • We use the OverlayManager.registerFabricatedOverlay() method to register our created overlay with the system.

  4. Enabling the Overlay:

    • After registration, we enable the overlay using OverlayManager.setEnabled().

  5. Removing the Overlay:

    • We provide a method to disable and unregister the overlay when it's no longer needed.

To use this in your Android system application or service, you would typically:

  1. Obtain an instance of OverlayManager:

    OverlayManager overlayManager = context.getSystemService(OverlayManager.class);
  2. Call the methods:

    FabricatedRROExample.createAndApplyFabricatedOverlay(overlayManager);

    And when you want to remove it:

    FabricatedRROExample.removeFabricatedOverlay(overlayManager);

Remember that working with Fabricated RROs typically requires system-level permissions. This code would usually be run in a system service or a privileged app, not in a regular third-party app.

Also, note that changes may not be immediately visible in all cases. Some changes might require a UI refresh or even a system reboot to take full effect, depending on how the target app handles resource updates.

Last updated