From db4e5cc2d5198d5f6fd72f6e68391fdf47bd6c98 Mon Sep 17 00:00:00 2001 From: Samuel Holland Date: Sat, 25 Nov 2017 23:38:16 -0600 Subject: ConfigImporter: Get filename from content resolver --- .../com/wireguard/android/backends/VpnService.java | 29 +++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) (limited to 'app/src/main/java/com') diff --git a/app/src/main/java/com/wireguard/android/backends/VpnService.java b/app/src/main/java/com/wireguard/android/backends/VpnService.java index 275b719c..084a2407 100644 --- a/app/src/main/java/com/wireguard/android/backends/VpnService.java +++ b/app/src/main/java/com/wireguard/android/backends/VpnService.java @@ -5,12 +5,14 @@ import android.content.ComponentName; import android.content.ContentResolver; import android.content.Intent; import android.content.SharedPreferences; +import android.database.Cursor; import android.net.Uri; import android.os.AsyncTask; import android.os.Binder; import android.os.Build; import android.os.IBinder; import android.preference.PreferenceManager; +import android.provider.OpenableColumns; import android.service.quicksettings.TileService; import android.util.Log; @@ -285,11 +287,32 @@ public class VpnService extends Service for (final Uri uri : uris) { if (isCancelled()) return null; - String name = Uri.decode(uri.getLastPathSegment()); - if (name.indexOf('/') >= 0) - name = name.substring(name.lastIndexOf('/')); + String name = null; + if ("file".equals(uri.getScheme())) { + name = uri.getLastPathSegment(); + } else { + final String[] columns = {OpenableColumns.DISPLAY_NAME}; + try (final Cursor cursor = + getContentResolver().query(uri, columns, null, null, null)) { + if (cursor != null && cursor.moveToFirst() && !cursor.isNull(0)) { + name = cursor.getString(0); + Log.v(getClass().getSimpleName(), "Got name via cursor"); + } + } + if (name == null) { + name = Uri.decode(uri.getLastPathSegment()); + if (name.indexOf('/') >= 0) + name = name.substring(name.lastIndexOf('/') + 1); + Log.v(getClass().getSimpleName(), "Got name from urlencoded path"); + } + } if (!name.endsWith(".conf")) name = name + ".conf"; + if (!Config.isNameValid(name.substring(0, name.length() - 5))) { + Log.v(getClass().getSimpleName(), "Detected name is not valid: " + name); + name = "ImportedConfig.conf"; + } + Log.d(getClass().getSimpleName(), "Mapped URI " + uri + " to file name " + name); final File output = new File(getFilesDir(), name); if (output.exists()) { Log.w(getClass().getSimpleName(), "Config file for " + uri + " already exists"); -- cgit v1.2.3