package net.oldev.aBrightnessQS;

import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import android.service.quicksettings.TileService;

/**
 * Helper to manage System Settings Permission UI, in the context of TileService.
 *
 * Android's guide provides a pattern (and helpers) for doing so in an Activity.
 * This helper mimics a simplified version in the context of a TileService.
 *
 * @see https://developer.android.com/training/permissions/requesting.html the pattern followed
 *
 */
public class SystemSettingsPermissionUIUtil {

    public static void requestPermission(TileService tileService) {
        // The BrightnessTileService is *not functional* without the permission to modify system settings.
        // Instead of launching an extra dialog as the general pattern suggested,
        // here we launch the actual permission activity, along with an explanation
        // of the reason doing so.

        // Note: PowerToggle has an implementation that mimics the general pattern.
        // https://github.com/sunnygoyal/PowerToggles/blob/master/src/com/painless/pc/PermissionDialog.java

        String permissionNeededMsg = tileService.getString(R.string.sys_permission_required_msg);
        // TODO: Consider to use a snack bar as it is more prominent?!
        android.widget.Toast.makeText(tileService.getApplicationContext(), permissionNeededMsg,
                android.widget.Toast.LENGTH_LONG).show();

        doRequestPermission(tileService);
    }

    // Analogous to ActivityCompat.requestPermissions(Activity ...)
    private static void doRequestPermission(TileService tileService) {

        // Use TileService.startActivityAndCollapse to close out the Quick Settings Panel
        // Otherwise, the activity will only be launched in background, pending closing the quick settings panel
        tileService.startActivityAndCollapse(new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS)
                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                .setData(Uri.parse("package:" + tileService.getPackageName())));
    }

}

@@ -27,16 +27,48 @@
 essQS;%0A%0A
+import android.content.Context;%0A
 import a
@@ -1180,59 +1180,33 @@
     
-String permissionNeededMsg = tileService.getString(
+showMessage(tileService, 
 R.st
@@ -1240,24 +1240,25 @@
 d_msg);%0A
+%0A
         
 // TODO:
@@ -1253,120 +1253,400 @@
     
-// TODO: Consider to use a snack bar as it is more prominent?!%0A        android.widget.Toast.makeText(tileService
+doRequestPermission(tileService);%0A    %7D%0A%0A    private static void showMessage(Context context, int msgResId) %7B%0A        // SnackBar would be more prominent, plus it can stay until the uer dismisses it.%0A        // SnackBar, however, is tied to an Activity, of which we do not have here.%0A        // So we stick with Toast%0A        android.widget.Toast toast = android.widget.Toast.makeText(context
 .get
@@ -1671,27 +1671,16 @@
 (), 
-permissionNeededMsg
+msgResId
 ,%0A  
@@ -1730,57 +1730,160 @@
 ONG)
-.show();%0A%0A        doRequestPermission(tileService
+;%0A        // Show toast at center to make it more prominent to the user%0A        toast.setGravity(android.view.Gravity.CENTER, 0, 0);%0A        toast.show(
 );%0A 

Modify system settings permission UI prompt: UI refinement on toast message