SettingsCompat Documentation

repository·master·Indexed 21 days ago

https://github.com/czy1121/settingscompat

A compatibility library for Android 'Special Permissions', specifically SYSTEM_ALERT_WINDOW and WRITE_SETTINGS. It provides methods to check permission status and request permissions via system intents, handling fragmentation across Android versions (API 18+) and manufacturer-specific ROMs such as Huawei, Xiaomi, Meizu, and Vivo.

Tokens
906
Snippets
4
Records
5
Agent score
26%

What's inside SettingsCompat

  1. Understanding Special Permission Compatibility

    master

    SettingsCompat handles the fragmentation of special permissions across different Android versions and manufacturer ROMs:

    Detection Logic

    • API 23+ (Android 6.0/M): Uses Settings.canDrawOverlays and Settings.System.canWrite.
    • API 18+ (Android 4.3/JellyBean MR2): Uses reflection to access AppOpsManager.checkOp.
    • API < 18: Permissions are enabled by default; always returns true.

    Authorization Logic

    • API 23+: Redirects to system settings via Settings.ACTION_MANAGE_OVERLAY_PERMISSION or Settings.ACTION_MANAGE_WRITE_SETTINGS.
    • API 18+: Permissions are enabled by default.
    • API < 18: No action required.

    ROM-Specific Behavior

    Some manufacturers (e.g., Huawei, Xiaomi, Meizu, Vivo) use custom permission management systems instead of standard Android settings. In these cases, the library attempts to detect the ROM type and redirect the user to the specific security center or app details page relevant to that manufacturer.

  2. Install SettingsCompat via Gradle

    master

    To use SettingsCompat in your Android project, add the JitPack repository to your repositories block and include the dependency in your dependencies block.

    repositories {
        maven { url "https://jitpack.io" }
    } 
    dependencies {
        compile 'com.github.czy1121:settingscompat:1.1.4'
    }
  3. Request Special Permissions

    master

    If permission checks return false, you must direct the user to the appropriate system settings page to grant the permission.

    • manageDrawOverlays(Context): Redirects to the overlay permission settings.
    • manageWriteSettings(Context): Redirects to the system settings modification page (Note: currently only supports Android 6.0+).
    // Redirect user to grant overlay permission
    SettingsCompat.manageDrawOverlays(context);
    
    // Redirect user to grant system settings modification permission (Android 6.0+)
    SettingsCompat.manageWriteSettings(context);
  4. Check Special Permission Status

    master

    SettingsCompat provides methods to check if your application currently has the required special permissions. This is necessary because Android 6.0+ requires explicit user consent via a system intent, whereas older versions granted these by default upon manifest declaration.

    // Check if the app has permission to draw over other apps (SYSTEM_ALERT_WINDOW)
    boolean canDraw = SettingsCompat.canDrawOverlays(context);
    
    // Check if the app has permission to modify system settings (WRITE_SETTINGS)
    boolean canWrite = SettingsCompat.canWriteSettings(context);
  5. Set Permission Status (Android 4.3/4.4 only)

    master

    On non-heavily customized Android 4.3 and 4.4 devices, you can attempt to programmatically toggle the permission state using AppOpsManager.setMode. This is NOT available on newer Android versions or heavily customized ROMs.

    // Only works on non-customized Android 4.3/4.4
    SettingsCompat.setDrawOverlays(context, true);
    SettingsCompat.setWriteSettings(context, true);