Skip to content

Commit

Permalink
Runtime permissions request (for Android 6 only)
Browse files Browse the repository at this point in the history
In Android 6 (and only Android 6) we need WRITE_EXTERNAL_STORAGE
permission in order to read the OBB file. This is a known Android
6 bug; the OBB file is supposed to be part of the app's internal
storage. See https://code.google.com/p/android/issues/detail?id=197287

In Android target api >= 23, WRITE_EXTERNAL_STORAGE is considered
part of the "dangerous" STORAGE permissions group. So, instead of
asking the user to grant it when the app is installed/upgraded, we
have to prompt the user for it at runtime. That's what this patch
adds.
  • Loading branch information
agwells committed Apr 5, 2017
1 parent d05f409 commit 9d0741a
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions src/android/XAPKReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
import android.os.Bundle;
import android.net.Uri;
import org.json.JSONArray;
import android.content.pm.PackageManager;
import android.Manifest;
import android.os.Build;
import org.json.JSONException;

public class XAPKReader extends CordovaPlugin {
public static final String ACTION_DOWNLOAD_IF_AVAIlABLE = "downloadExpansionIfAvailable";
Expand All @@ -21,8 +25,12 @@ public class XAPKReader extends CordovaPlugin {
private CordovaWebView webView;
private Bundle bundle;

// Request code used when we do runtime permissions requests during initialization.
public static final int STARTUP_REQ_CODE = 0;

@Override
public void initialize(final CordovaInterface cordova, CordovaWebView webView) {

this.cordova = cordova;
this.webView = webView;
this.bundle = new Bundle();
Expand Down Expand Up @@ -74,14 +82,31 @@ public void initialize(final CordovaInterface cordova, CordovaWebView webView) {
// Set the public key.
XAPKDownloaderService.BASE64_PUBLIC_KEY = bundle.getString("xapk_google_play_public_key", "");

boolean autoDownload = bundle.getBoolean("xapk_auto_download", true);
if (autoDownload) {
downloadExpansionIfAvailable();
// Workaround for Android 6 bug wherein downloaded OBB files have the wrong file permissions
// and require WRITE_EXTERNAL_STORAGE permission
if (
Build.VERSION.SDK_INT == 23
&& !cordova.hasPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
) {
// We need the permission; so request it (asynchronously, callback is onRequestPermissionsResult)
cordova.requestPermission(this, STARTUP_REQ_CODE, Manifest.permission.WRITE_EXTERNAL_STORAGE);
} else {
// We don't need the permission, or we already have it.
this.autodownloadIfNecessary();
}

super.initialize(cordova, webView);
}

public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) throws JSONException {
for (int r:grantResults) {
// They granted us WRITE_EXTERNAL_STORAGE (and thus, implicitly, READ_EXTERNAL_STORAGE) permission
if (requestCode == STARTUP_REQ_CODE && r == PackageManager.PERMISSION_GRANTED) {
this.autodownloadIfNecessary();
}
}
}

@Override
public boolean execute(final String action, final JSONArray args, final CallbackContext callContext) {
try {
Expand Down Expand Up @@ -109,6 +134,12 @@ public boolean execute(final String action, final JSONArray args, final Callback
}
}

private void autodownloadIfNecessary() {
boolean autoDownload = bundle.getBoolean("xapk_auto_download", true);
if (autoDownload) {
downloadExpansionIfAvailable();
}
}

private void downloadExpansionIfAvailable() {
cordova.getActivity().runOnUiThread(new Runnable() {
Expand Down

0 comments on commit 9d0741a

Please sign in to comment.