diff options
author | Samuel Holland <samuel@sholland.org> | 2017-11-25 23:38:16 -0600 |
---|---|---|
committer | Samuel Holland <samuel@sholland.org> | 2017-11-25 23:39:08 -0600 |
commit | db4e5cc2d5198d5f6fd72f6e68391fdf47bd6c98 (patch) | |
tree | 449b06cde4ea7d12d362104affd754b7757f0a62 /app/src/main/java/com | |
parent | 7f4320ab4ba18e7309f876cf2631373a038ad5bc (diff) |
ConfigImporter: Get filename from content resolver
Diffstat (limited to 'app/src/main/java/com')
-rw-r--r-- | app/src/main/java/com/wireguard/android/backends/VpnService.java | 29 |
1 files changed, 26 insertions, 3 deletions
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"); |