Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add $util.getPermissions to action type #247

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 deletions app/src/main/java/com/jasonette/seed/Action/JasonUtilAction.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
package com.jasonette.seed.Action;

import android.Manifest;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.provider.ContactsContract;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.DialogFragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AlertDialog;
import android.text.InputType;
Expand All @@ -35,6 +40,7 @@
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
Expand Down Expand Up @@ -92,6 +98,38 @@ public void run() {
Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString());
}
}
public void getPermissions(final JSONObject action, final JSONObject data, final JSONObject event, final Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int doPerm = 0;
String allPerm[] = new String[0];
try {
JSONObject options = action.getJSONObject("options");
if (options.has("permissions")) {
String[] commaSeparatedArr = options.getString("permissions").split("\\s*,\\s*");
ArrayList<String> listItems = new ArrayList<>(Arrays.asList(commaSeparatedArr));
allPerm = new String[listItems.size()];
for (int i = 0; i < listItems.size(); i++) {
String newPer = new String("android.permission.") + listItems.get(i).toUpperCase();
if (ContextCompat.checkSelfPermission(context, newPer) != PackageManager.PERMISSION_GRANTED) {
allPerm[doPerm] = newPer;
doPerm++;
}
}
}
} catch (Exception e) {
Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString());
}
if (doPerm >= 1) {
JasonHelper.next("success", action, new JSONObject(), event, context);
ActivityCompat.requestPermissions((JasonViewActivity) context, allPerm, 0);
} else {
JasonHelper.next("error", action, new JSONObject(), event, context);
}
} else {
JasonHelper.next("error", action, new JSONObject(), event, context);
}

}
public void alert(final JSONObject action, final JSONObject data, final JSONObject event, final Context context){
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
Expand Down Expand Up @@ -146,7 +184,7 @@ public void run() {


}
builder.setPositiveButton("OK",
builder.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
Expand All @@ -167,7 +205,7 @@ public void onClick(DialogInterface dialog,
}
}
});
builder.setNeutralButton("CANCEL",
builder.setNeutralButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
Expand Down Expand Up @@ -227,7 +265,7 @@ public void onClick(DialogInterface dialog, int val) {
}
}
});
builder.setNeutralButton("CANCEL",
builder.setNeutralButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int val) {
}
Expand Down
11 changes: 8 additions & 3 deletions app/src/main/java/com/jasonette/seed/Core/JasonViewActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Parcelable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.LocalBroadcastManager;
Expand Down Expand Up @@ -66,6 +67,7 @@
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -1588,6 +1590,9 @@ public void reload ( final JSONObject action, JSONObject data, JSONObject event,
public void flush ( final JSONObject action, JSONObject data, JSONObject event, Context context){
// there's no default caching on Android. So don't do anything for now
try {
for(File tempFile : context.getExternalFilesDir(Environment.DIRECTORY_PICTURES).listFiles()) {
tempFile.delete();
}
JasonHelper.next("success", action, new JSONObject(), event, context);
} catch (Exception e) {
Log.d("Warning", e.getStackTrace()[0].getMethodName() + " : " + e.toString());
Expand Down Expand Up @@ -1912,7 +1917,7 @@ public boolean onTouch(View v, MotionEvent event) {

@Override
public void onGlobalLayout() {
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
rootLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
else {
Expand Down Expand Up @@ -1947,7 +1952,7 @@ public void onGlobalLayout() {
rootLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
rootLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
rootLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
Expand Down Expand Up @@ -2936,7 +2941,7 @@ public void removeListViewOnItemTouchListeners() {
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
cameraManager.startVision(JasonViewActivity.this);
//cameraManager.startVision(JasonViewActivity.this);
} else {
Log.d("Warning", "Waiting for permission approval");
}
Expand Down