[Android] Ask for system settings permission for ringtone

Geoffrey Métais git at videolan.org
Tue Oct 27 17:47:02 CET 2015


vlc-ports/android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue Oct 27 13:05:26 2015 +0100| [9265c02f9633dcc4a78da0f4747bb608b67ad747] | committer: Geoffrey Métais

Ask for system settings permission for ringtone

> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=9265c02f9633dcc4a78da0f4747bb608b67ad747
---

 vlc-android/res/values/strings.xml                 |    2 +
 .../src/org/videolan/vlc/gui/MainActivity.java     |    9 +---
 .../src/org/videolan/vlc/gui/audio/AudioUtil.java  |    8 ++-
 .../src/org/videolan/vlc/util/AndroidDevices.java  |   52 ++++++++++++++++++--
 4 files changed, 59 insertions(+), 12 deletions(-)

diff --git a/vlc-android/res/values/strings.xml b/vlc-android/res/values/strings.xml
index 24e3480..6cf80ec 100644
--- a/vlc-android/res/values/strings.xml
+++ b/vlc-android/res/values/strings.xml
@@ -360,6 +360,8 @@
     <string name="widget_name_b">VLC Dark Widget</string>
     <string name="allow_storage_access_title">Allow VLC to access video and audio media</string>
     <string name="allow_storage_access_description">VLC needs you to grant this permission to work correctly</string>
+    <string name="allow_settings_access_title">Allow VLC to change your ringtone</string>
+    <string name="allow_settings_access_description">VLC needs you to grant \"modify system settings\" permission to set this song as your ringtone</string>
     <string name="permission_ask_again">Grant permission</string>
     <string name="exit_app">Close VLC</string>
 
diff --git a/vlc-android/src/org/videolan/vlc/gui/MainActivity.java b/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
index 18f8af6..e030dc6 100644
--- a/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
@@ -20,11 +20,9 @@
 
 package org.videolan.vlc.gui;
 
-import android.Manifest;
 import android.annotation.TargetApi;
 import android.app.SearchManager;
 import android.content.Context;
-import android.content.DialogInterface;
 import android.content.Intent;
 import android.content.SharedPreferences;
 import android.content.SharedPreferences.Editor;
@@ -35,17 +33,13 @@ import android.os.Build;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.Message;
-import android.support.design.widget.Snackbar;
-import android.support.v4.app.ActivityCompat;
 import android.support.v4.app.Fragment;
 import android.support.v4.app.FragmentActivity;
 import android.support.v4.app.FragmentManager;
 import android.support.v4.app.FragmentTransaction;
-import android.support.v4.content.ContextCompat;
 import android.support.v4.view.GravityCompat;
 import android.support.v4.view.MenuItemCompat;
 import android.support.v7.app.ActionBarDrawerToggle;
-import android.support.v7.app.AlertDialog;
 import android.support.v7.widget.SearchView;
 import android.util.Log;
 import android.view.KeyEvent;
@@ -222,7 +216,7 @@ public class MainActivity extends AudioPlayerContainerActivity implements OnItem
     public void onRequestPermissionsResult(int requestCode,
                                            String permissions[], int[] grantResults) {
         switch (requestCode) {
-            case AndroidDevices.PERMISSION_STORAGE_TAG: {
+            case AndroidDevices.PERMISSION_STORAGE_TAG:
                 // If request is cancelled, the result arrays are empty.
                 if (grantResults.length > 0
                         && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
@@ -231,7 +225,6 @@ public class MainActivity extends AudioPlayerContainerActivity implements OnItem
                     AndroidDevices.showStoragePermissionDialog(this, false);
                 }
                 return;
-            }
             // other 'case' lines to check for other
             // permissions this app might request
         }
diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioUtil.java b/vlc-android/src/org/videolan/vlc/gui/audio/AudioUtil.java
index 49ccf1a..0dacbd9 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioUtil.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioUtil.java
@@ -20,6 +20,7 @@
 package org.videolan.vlc.gui.audio;
 
 import android.annotation.SuppressLint;
+import android.app.Activity;
 import android.content.ContentResolver;
 import android.content.ContentValues;
 import android.content.Context;
@@ -83,7 +84,11 @@ public class AudioUtil {
     public static final BitmapDrawable DEFAULT_COVER = new BitmapDrawable(VLCApplication.getAppResources(), BitmapCache.getFromResource(VLCApplication.getAppResources(), R.drawable.icon));
 
     @RequiresPermission(android.Manifest.permission.WRITE_SETTINGS)
-    public static void setRingtone(MediaWrapper song, Context context){
+    public static void setRingtone(MediaWrapper song, Activity context){
+        if (!AndroidDevices.canWriteSettings(context)) {
+            AndroidDevices.checkWriteSettingsPermission(context);
+            return;
+        }
         File newringtone = AndroidUtil.UriToFile(song.getUri());
         if(newringtone == null || !newringtone.exists()) {
             Toast.makeText(context.getApplicationContext(),context.getString(R.string.ringtone_error), Toast.LENGTH_SHORT).show();
@@ -111,6 +116,7 @@ public class AudioUtil {
                     newUri
             );
         } catch(Exception e) {
+            Log.e(TAG, "error setting ringtone", e);
             Toast.makeText(context.getApplicationContext(),
                     context.getString(R.string.ringtone_error),
                     Toast.LENGTH_SHORT).show();
diff --git a/vlc-android/src/org/videolan/vlc/util/AndroidDevices.java b/vlc-android/src/org/videolan/vlc/util/AndroidDevices.java
index 161ff20..0dda697 100644
--- a/vlc-android/src/org/videolan/vlc/util/AndroidDevices.java
+++ b/vlc-android/src/org/videolan/vlc/util/AndroidDevices.java
@@ -35,6 +35,7 @@ import android.net.Uri;
 import android.os.Build.VERSION;
 import android.os.Build.VERSION_CODES;
 import android.os.Environment;
+import android.provider.Settings;
 import android.support.v4.app.ActivityCompat;
 import android.support.v4.content.ContextCompat;
 import android.support.v7.app.AlertDialog;
@@ -63,6 +64,7 @@ public class AndroidDevices {
     public final static String EXTERNAL_PUBLIC_DIRECTORY = Environment.getExternalStorageDirectory().getPath();
 
     public static final int PERMISSION_STORAGE_TAG = 255;
+    public static final int PERMISSION_SETTINGS_TAG = 254;
 
     final static boolean hasNavBar;
     final static boolean hasTsp;
@@ -201,6 +203,11 @@ public class AndroidDevices {
      * Marshmallow permission system management
      */
 
+    @TargetApi(VERSION_CODES.M)
+    public static boolean canWriteSettings(Context context) {
+        return !AndroidUtil.isMarshMallowOrLater() || Settings.System.canWrite(context);
+    }
+
     public static boolean canReadStorage() {
         return ContextCompat.checkSelfPermission(VLCApplication.getAppContext(),
                 Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
@@ -220,8 +227,19 @@ public class AndroidDevices {
         }
     }
 
+    public static void checkWriteSettingsPermission(Activity activity) {
+        if (AndroidUtil.isMarshMallowOrLater() && !canWriteSettings(activity)) {
+            showSettingsPermissionDialog(activity);
+        }
+    }
+
     private static Dialog sAlertDialog;
-    private static boolean firstAttempt = true;
+
+    public static void showSettingsPermissionDialog(final Activity activity) {
+        if (sAlertDialog != null && sAlertDialog.isShowing())
+            return;
+        sAlertDialog = createSettingsDialogCompat(activity);
+    }
 
     public static void showStoragePermissionDialog(final Activity activity, boolean exit) {
         if (sAlertDialog != null && sAlertDialog.isShowing())
@@ -265,7 +283,7 @@ public class AndroidDevices {
                     activity.finish();
                 }
             })
-            .setCancelable(false);
+                    .setCancelable(false);
         }
         return dialogBuilder.show();
     }
@@ -303,14 +321,42 @@ public class AndroidDevices {
                     activity.finish();
                 }
             })
-            .setCancelable(false);
+                    .setCancelable(false);
         }
         return dialogBuilder.show();
     }
 
+    private static Dialog createSettingsDialogCompat(final Activity activity) {
+        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(activity)
+                .setTitle(activity.getString(R.string.allow_settings_access_title))
+                .setMessage(activity.getString(R.string.allow_settings_access_description))
+                .setIcon(android.R.drawable.ic_dialog_alert)
+                .setPositiveButton(activity.getString(R.string.permission_ask_again), new DialogInterface.OnClickListener() {
+                    @Override
+                    public void onClick(DialogInterface dialog, int whichButton) {
+                        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(activity);
+                        Intent i = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
+                        i.setData(Uri.parse("package:" + activity.getPackageName()));
+                        try {
+                            activity.startActivity(i);
+                        } catch (Exception ex) {}
+                        SharedPreferences.Editor editor = settings.edit();
+                        editor.putBoolean("user_declined_settings_access", true);
+                        Util.commitPreferences(editor);
+                    }
+                });
+        return dialogBuilder.show();
+    }
+
     private static void requestStoragePermission(Activity activity){
         ActivityCompat.requestPermissions(activity,
                 new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                 PERMISSION_STORAGE_TAG);
     }
+
+    private static void requestSettingsPermission(Activity activity){
+        ActivityCompat.requestPermissions(activity,
+                new String[]{Manifest.permission.WRITE_SETTINGS},
+                PERMISSION_SETTINGS_TAG);
+    }
 }



More information about the Android mailing list