From ab8948eb5acd70aacd4a9b1b5af19ee764dafdef Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Tue, 17 Apr 2018 17:52:40 +0200 Subject: [PATCH] fix "Send F-Droid via Bluetooth" The 'com.mediatek.bluetooth' Bluetooth or android-23 devices were not being granted URI permissions. --- .../fdroid/views/swap/StartSwapView.java | 4 ++- .../java/org/fdroid/fdroid/FDroidApp.java | 6 ++-- .../fdroid/installer/ApkFileProvider.java | 28 +++++++++++-------- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/app/src/full/java/org/fdroid/fdroid/views/swap/StartSwapView.java b/app/src/full/java/org/fdroid/fdroid/views/swap/StartSwapView.java index 1891468cd..03aeeb2ac 100644 --- a/app/src/full/java/org/fdroid/fdroid/views/swap/StartSwapView.java +++ b/app/src/full/java/org/fdroid/fdroid/views/swap/StartSwapView.java @@ -257,7 +257,9 @@ public class StartSwapView extends RelativeLayout implements SwapWorkflowActivit textBluetoothVisible.setText(textResource); bluetoothSwitch = (SwitchCompat) findViewById(R.id.switch_bluetooth); - Utils.debugLog(TAG, getManager().isBluetoothDiscoverable() ? "Initially marking switch as checked, because Bluetooth is discoverable." : "Initially marking switch as not-checked, because Bluetooth is not discoverable."); + Utils.debugLog(TAG, getManager().isBluetoothDiscoverable() + ? "Initially marking switch as checked, because Bluetooth is discoverable." + : "Initially marking switch as not-checked, because Bluetooth is not discoverable."); bluetoothSwitch.setOnCheckedChangeListener(onBluetoothSwitchToggled); setBluetoothSwitchState(getManager().isBluetoothDiscoverable(), true); diff --git a/app/src/main/java/org/fdroid/fdroid/FDroidApp.java b/app/src/main/java/org/fdroid/fdroid/FDroidApp.java index 6844d0934..90a673fbd 100644 --- a/app/src/main/java/org/fdroid/fdroid/FDroidApp.java +++ b/app/src/main/java/org/fdroid/fdroid/FDroidApp.java @@ -580,7 +580,6 @@ public class FDroidApp extends Application { String bluetoothPackageName = null; String className = null; - boolean found = false; Intent sendBt = null; try { @@ -599,20 +598,19 @@ public class FDroidApp extends Application { if ("com.android.bluetooth".equals(bluetoothPackageName) || "com.mediatek.bluetooth".equals(bluetoothPackageName)) { className = info.activityInfo.name; - found = true; break; } } } catch (PackageManager.NameNotFoundException e) { Log.e(TAG, "Could not get application info to send via bluetooth", e); - found = false; + className = null; } catch (IOException e) { Exception toLog = new RuntimeException("Error preparing file to send via Bluetooth", e); ACRA.getErrorReporter().handleException(toLog, false); } if (sendBt != null) { - if (found) { + if (className != null) { sendBt.setClassName(bluetoothPackageName, className); activity.startActivity(sendBt); } else { diff --git a/app/src/main/java/org/fdroid/fdroid/installer/ApkFileProvider.java b/app/src/main/java/org/fdroid/fdroid/installer/ApkFileProvider.java index ceb903cf9..c1f9fe615 100644 --- a/app/src/main/java/org/fdroid/fdroid/installer/ApkFileProvider.java +++ b/app/src/main/java/org/fdroid/fdroid/installer/ApkFileProvider.java @@ -37,14 +37,18 @@ import java.io.IOException; * either locally or for sending via bluetooth. *

* APK handling for installations: - * 1. APKs are downloaded into a cache directory that is either created on SD card + *

    + *
  1. APKs are downloaded into a cache directory that is either created on SD card * "/Android/data/[app_package_name]/cache/apks" (if card is mounted and app has - * appropriate permission) or on device's file system depending incoming parameters. - * 2. Before installation, the APK is copied into the private data directory of the F-Droid, - * "/data/data/[app_package_name]/files/install-$random.apk". - * 3. The hash of the file is checked against the expected hash from the repository - * 4. For Android < 7, a file Uri pointing to the File is returned, for Android >= 7, - * a content Uri is returned using support lib's FileProvider. + * appropriate permission) or on device's file system depending incoming parameters
  2. + *
  3. Before installation, the APK is copied into the private data directory of the F-Droid, + * "/data/data/[app_package_name]/files/install-$random.apk"
  4. + *
  5. The hash of the file is checked against the expected hash from the repository
  6. + *
  7. For {@link Build.VERSION_CODES#M < android-23}, a {@code file://} {@link Uri} + * pointing to the {@link File} is returned, for {@link Build.VERSION_CODES#M >= android-23}, + * a {@code content://} {@code Uri} is returned using support lib's + * {@link FileProvider}
  8. + *
*/ public class ApkFileProvider extends FileProvider { @@ -52,7 +56,7 @@ public class ApkFileProvider extends FileProvider { public static Uri getSafeUri(Context context, PackageInfo packageInfo) throws IOException { SanitizedFile tempApkFile = ApkCache.copyInstalledApkToFiles(context, packageInfo); - return getSafeUri(context, tempApkFile, Build.VERSION.SDK_INT >= 24); + return getSafeUri(context, tempApkFile, Build.VERSION.SDK_INT >= 23); } /** @@ -89,12 +93,12 @@ public class ApkFileProvider extends FileProvider { context.grantUriPermission(PrivilegedInstaller.PRIVILEGED_EXTENSION_PACKAGE_NAME, apkUri, Intent.FLAG_GRANT_READ_URI_PERMISSION); context.grantUriPermission("com.android.bluetooth", apkUri, Intent.FLAG_GRANT_READ_URI_PERMISSION); + context.grantUriPermission("com.mediatek.bluetooth", apkUri, Intent.FLAG_GRANT_READ_URI_PERMISSION); return apkUri; + } else { + tempFile.setReadable(true, false); + return Uri.fromFile(tempFile); } - - tempFile.setReadable(true, false); - - return Uri.fromFile(tempFile); } }