Skip to content

Commit

Permalink
Fix Apk install (fix #200)
Browse files Browse the repository at this point in the history
  • Loading branch information
fm-sys committed Feb 5, 2023
1 parent f7c034a commit 82f71be
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
4 changes: 2 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="28" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="32"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" /> <!-- Needed to prompt the user to give permission to install a downloaded apk -->
<uses-permission-sdk-23 android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
<uses-permission-sdk-23 android:name="android.permission.REQUEST_INSTALL_PACKAGES" /> <!-- Needed to prompt the user to give permission to install a downloaded apk -->

<uses-feature
android:name="android.hardware.wifi"
Expand Down
7 changes: 4 additions & 3 deletions app/src/main/java/com/fmsys/snapdrop/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -633,10 +633,11 @@ public void onCompleted(@NonNull final Object file) {

private void fileDownloadedIntent(final Uri uri, final JavaScriptInterface.FileHeader fileHeader) {
final int notificationId = (int) SystemClock.uptimeMillis();
boolean isApk = fileHeader.getName().toLowerCase().endsWith(".apk");

final Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(uri, fileHeader.getMime());
intent.setAction(isApk ? Intent.ACTION_INSTALL_PACKAGE : Intent.ACTION_VIEW);
intent.setDataAndType(uri, isApk ? "application/vnd.android.package-archive" : fileHeader.getMime());
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

final PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 1, intent, Build.VERSION.SDK_INT >= 23 ? PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_CANCEL_CURRENT : PendingIntent.FLAG_CANCEL_CURRENT);
Expand Down Expand Up @@ -674,7 +675,7 @@ private void fileDownloadedIntent(final Uri uri, final JavaScriptInterface.FileH
}

final Snackbar snackbar = Snackbar.make(binding.pullToRefresh, R.string.download_successful, Snackbar.LENGTH_LONG)
.setAction(R.string.open, button -> {
.setAction(isApk ? R.string.install : R.string.open, button -> {
try {
startActivity(intent);
notificationManager.cancel(notificationId);
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

<!-- Action Buttons -->
<string name="open">open</string>
<string name="install">install</string>
<string name="reset">reset</string>
<string name="copy">copy</string>

Expand Down

0 comments on commit 82f71be

Please sign in to comment.