Skip to content

Commit 3d5fc9c

Browse files
authored
Allow to install APK files from filebrowser on click, by @gsantner (PR #1417)
1 parent 9e14e17 commit 3d5fc9c

File tree

5 files changed

+39
-3
lines changed

5 files changed

+39
-3
lines changed

app/src/main/AndroidManifest.xml

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
<uses-permission android:name="android.permission.INSTALL_SHORTCUT" />
2323
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
2424
<uses-permission android:name="android.permission.RECORD_AUDIO" />
25+
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
26+
2527

2628
<uses-feature
2729
android:name="android.hardware.touchscreen"

app/src/main/java/net/gsantner/markor/activity/MainActivity.java

+2
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,8 @@ public void onFsViewerDoUiUpdate(FilesystemViewerAdapter adapter) {
412412
public void onFsViewerSelected(String request, File file, final Integer lineNumber) {
413413
if (TextFormat.isTextFile(file)) {
414414
DocumentActivity.launch(MainActivity.this, file, false, null, null, lineNumber);
415+
} else if (file.getName().toLowerCase().endsWith(".apk")) {
416+
_shareUtil.requestApkInstallation(file);
415417
} else {
416418
DocumentActivity.askUserIfWantsToOpenFileInThisApp(MainActivity.this, file);
417419
}

app/src/main/java/net/gsantner/markor/util/MarkorWebViewClient.java

+2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
5959
newPreview.putExtra(DocumentIO.EXTRA_PATH, file);
6060
newPreview.putExtra(DocumentActivity.EXTRA_DO_PREVIEW, true);
6161
_activity.startActivity(newPreview);
62+
} else if (file.getName().toLowerCase().endsWith(".apk")) {
63+
su.requestApkInstallation(file);
6264
} else if ((mimetype = ContextUtils.getMimeType(url)) != null) {
6365
su.viewFileInOtherApp(file, mimetype);
6466
} else {

app/src/main/java/net/gsantner/markor/util/ShareUtil.java

-3
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,11 @@
2020
import android.webkit.WebView;
2121
import android.widget.Toast;
2222

23-
import net.gsantner.markor.BuildConfig;
2423
import net.gsantner.markor.R;
2524
import net.gsantner.markor.activity.DocumentRelayActivity;
2625
import net.gsantner.markor.model.Document;
2726

2827
public class ShareUtil extends net.gsantner.opoc.util.ShareUtil {
29-
public static final String FILE_PROVIDER_AUTHORITY = BuildConfig.APPLICATION_ID + ".provider";
30-
3128
public ShareUtil(Context context) {
3229
super(context);
3330
setChooserTitle(_context.getString(R.string.share_to_arrow));

app/src/main/java/net/gsantner/opoc/util/ShareUtil.java

+33
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,39 @@ public boolean viewFileInOtherApp(final File file, @Nullable final String type)
317317
return false;
318318
}
319319

320+
321+
/**
322+
* Request installation of APK specified by file
323+
* Permission required:
324+
* <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
325+
*
326+
* @param file The apk file to install
327+
*/
328+
public boolean requestApkInstallation(final File file) {
329+
if (file == null || !file.getName().toLowerCase().endsWith(".apk")) {
330+
return false;
331+
}
332+
333+
Uri fileUri = null;
334+
try {
335+
fileUri = FileProvider.getUriForFile(_context, getFileProviderAuthority(), file);
336+
} catch (Exception ignored) {
337+
try {
338+
fileUri = Uri.fromFile(file);
339+
} catch (Exception ignored2) {
340+
}
341+
}
342+
343+
if (fileUri != null) {
344+
final Intent intent = new Intent(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ? Intent.ACTION_INSTALL_PACKAGE : Intent.ACTION_VIEW)
345+
.setFlags(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ? Intent.FLAG_GRANT_READ_URI_PERMISSION : Intent.FLAG_ACTIVITY_NEW_TASK)
346+
.setDataAndType(fileUri, "application/vnd.android.package-archive");
347+
_context.startActivity(intent);
348+
return true;
349+
}
350+
return false;
351+
}
352+
320353
/**
321354
* Share the given bitmap with given format
322355
*

0 commit comments

Comments
 (0)