Skip to content

Commit

Permalink
Merge pull request #8 from infinyte7/app-permission
Browse files Browse the repository at this point in the history
Storage and AnkiDroid database read / write runtime permission.
  • Loading branch information
krmanik authored Nov 28, 2020
2 parents 9813479 + eb87f8e commit 2b4db6d
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 34 deletions.
1 change: 1 addition & 0 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ dependencies {
// SUB-PROJECT DEPENDENCIES START
implementation(project(path: ":CordovaLib"))
implementation "com.ichi2.anki:api:1.1.0alpha6"
implementation 'com.android.support:support-compat:28.0.0'
// SUB-PROJECT DEPENDENCIES END
}

Expand Down
2 changes: 1 addition & 1 deletion android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
</manifest>
</manifest>
2 changes: 1 addition & 1 deletion android/app/src/main/assets/www/common.html
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@
<br><br>
<div><b>License</b></div>
<div>
MIT License
GPL 3.0 License
<br>
Copyright (c) 2020 Mani
</div>
Expand Down
2 changes: 1 addition & 1 deletion android/app/src/main/assets/www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
</svg>
</div>

<div class="sidenav-version">Version 1.3.6</div>
<div class="sidenav-version">Version 1.3.7</div>

<!-- github logo -->
<div style="position: absolute; bottom: 8px; right: 10px;">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;

/**
* This class echoes a string called from JavaScript.
Expand Down Expand Up @@ -85,8 +83,6 @@ private void deleteAllImages(File dir) {
}
}



private void copyFileFromDirectory(File sourceLocation, File targetLocation)
{
if (sourceLocation.isDirectory())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,25 @@ Licensed to the Apache Software Foundation (ASF) under one

package io.infinyte7.ankiimageocclusion;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Toast;

import org.apache.cordova.*;

import com.ichi2.anki.api.AddContentApi;

public class MainActivity extends CordovaActivity
{
private static final int PERMISSION_REQUEST_CODE = 200;
@Override
public void onCreate(Bundle savedInstanceState)
{
Expand All @@ -43,26 +50,12 @@ public void onCreate(Bundle savedInstanceState)
moveTaskToBack(true);
}

boolean permissionAllowed = true;

Context context = this.cordovaInterface.getActivity().getApplicationContext();

final PackageManager pm = context.getPackageManager();
try {
PackageInfo packageInfo = pm.getPackageInfo(context.getPackageName(), PackageManager.GET_PERMISSIONS);
String[] requestedPermissions = packageInfo.requestedPermissions;
if ( requestedPermissions != null ) {
for (int i = 0; i < requestedPermissions.length; i++) {
if ((packageInfo.requestedPermissionsFlags[i] & PackageInfo.REQUESTED_PERMISSION_GRANTED) == 0) {
Toast.makeText(context, "Allow storage and additional permissions from app settings", Toast.LENGTH_LONG).show();
permissionAllowed = false;
break;
}
}
}
}
catch ( PackageManager.NameNotFoundException e ) {
Toast.makeText(context, "AnkiDroid not installed.", Toast.LENGTH_LONG).show();
if (checkPermission()) {
// Set by <content src="index.html" /> in config.xml
loadUrl(launchUrl);
} else {
requestPermission();
loadUrl("file:///android_asset/www/permissions.html");
}

super.appView.getView().setHorizontalScrollBarEnabled(false);
Expand All @@ -74,11 +67,61 @@ public void onCreate(Bundle savedInstanceState)
settings.setDefaultZoom(WebSettings.ZoomDensity.MEDIUM);
settings.setSupportZoom(true);

if (permissionAllowed) {
// Set by <content src="index.html" /> in config.xml
loadUrl(launchUrl);
} else {
loadUrl("file:///android_asset/www/permissions.html");
}

/* https://stackoverflow.com/questions/42275906/how-to-ask-runtime-permissions-for-camera-in-android-runtime-storage-permissio */
private boolean checkPermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
&& ContextCompat.checkSelfPermission(this, AddContentApi.READ_WRITE_PERMISSION) != PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
return false;
}
return true;
}

private void requestPermission() {

ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, AddContentApi.READ_WRITE_PERMISSION},
PERMISSION_REQUEST_CODE);
}

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getApplicationContext(), "Permission Granted", Toast.LENGTH_SHORT).show();
loadUrl(launchUrl);
} else {
Toast.makeText(getApplicationContext(), "Permission Denied", Toast.LENGTH_SHORT).show();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(this, AddContentApi.READ_WRITE_PERMISSION)
!= PackageManager.PERMISSION_GRANTED) {
showMessageOKCancel("You need to allow Storage and AnkiDroid read write permissions",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermission();
}
}
});
}
}
}
break;
}
}

private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
new AlertDialog.Builder(MainActivity.this)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", null)
.create()
.show();
}
}

0 comments on commit 2b4db6d

Please sign in to comment.