Skip to content

Commit

Permalink
Allow to install APK files from filebrowser on click, by @gsantner (PR
Browse files Browse the repository at this point in the history
  • Loading branch information
gsantner authored Aug 26, 2021
1 parent 9e14e17 commit 3d5fc9c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3 deletions.
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
<uses-permission android:name="android.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />


<uses-feature
android:name="android.hardware.touchscreen"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,8 @@ public void onFsViewerDoUiUpdate(FilesystemViewerAdapter adapter) {
public void onFsViewerSelected(String request, File file, final Integer lineNumber) {
if (TextFormat.isTextFile(file)) {
DocumentActivity.launch(MainActivity.this, file, false, null, null, lineNumber);
} else if (file.getName().toLowerCase().endsWith(".apk")) {
_shareUtil.requestApkInstallation(file);
} else {
DocumentActivity.askUserIfWantsToOpenFileInThisApp(MainActivity.this, file);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
newPreview.putExtra(DocumentIO.EXTRA_PATH, file);
newPreview.putExtra(DocumentActivity.EXTRA_DO_PREVIEW, true);
_activity.startActivity(newPreview);
} else if (file.getName().toLowerCase().endsWith(".apk")) {
su.requestApkInstallation(file);
} else if ((mimetype = ContextUtils.getMimeType(url)) != null) {
su.viewFileInOtherApp(file, mimetype);
} else {
Expand Down
3 changes: 0 additions & 3 deletions app/src/main/java/net/gsantner/markor/util/ShareUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,11 @@
import android.webkit.WebView;
import android.widget.Toast;

import net.gsantner.markor.BuildConfig;
import net.gsantner.markor.R;
import net.gsantner.markor.activity.DocumentRelayActivity;
import net.gsantner.markor.model.Document;

public class ShareUtil extends net.gsantner.opoc.util.ShareUtil {
public static final String FILE_PROVIDER_AUTHORITY = BuildConfig.APPLICATION_ID + ".provider";

public ShareUtil(Context context) {
super(context);
setChooserTitle(_context.getString(R.string.share_to_arrow));
Expand Down
33 changes: 33 additions & 0 deletions app/src/main/java/net/gsantner/opoc/util/ShareUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,39 @@ public boolean viewFileInOtherApp(final File file, @Nullable final String type)
return false;
}


/**
* Request installation of APK specified by file
* Permission required:
* <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
*
* @param file The apk file to install
*/
public boolean requestApkInstallation(final File file) {
if (file == null || !file.getName().toLowerCase().endsWith(".apk")) {
return false;
}

Uri fileUri = null;
try {
fileUri = FileProvider.getUriForFile(_context, getFileProviderAuthority(), file);
} catch (Exception ignored) {
try {
fileUri = Uri.fromFile(file);
} catch (Exception ignored2) {
}
}

if (fileUri != null) {
final Intent intent = new Intent(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ? Intent.ACTION_INSTALL_PACKAGE : Intent.ACTION_VIEW)
.setFlags(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ? Intent.FLAG_GRANT_READ_URI_PERMISSION : Intent.FLAG_ACTIVITY_NEW_TASK)
.setDataAndType(fileUri, "application/vnd.android.package-archive");
_context.startActivity(intent);
return true;
}
return false;
}

/**
* Share the given bitmap with given format
*
Expand Down

0 comments on commit 3d5fc9c

Please sign in to comment.