|
|
@@ -1,22 +1,39 @@
|
|
|
package com.knacki.mimou.activity;
|
|
|
|
|
|
+import android.Manifest;
|
|
|
+import android.content.DialogInterface;
|
|
|
import android.content.Intent;
|
|
|
+import android.content.pm.PackageManager;
|
|
|
import android.net.Uri;
|
|
|
import android.os.Build;
|
|
|
import android.os.Bundle;
|
|
|
+import android.os.Handler;
|
|
|
+import android.support.annotation.NonNull;
|
|
|
+import android.support.v4.app.ActivityCompat;
|
|
|
+import android.support.v4.content.ContextCompat;
|
|
|
+import android.support.v7.app.AlertDialog;
|
|
|
import android.support.v7.app.AppCompatActivity;
|
|
|
+import android.util.Base64;
|
|
|
+import android.webkit.ValueCallback;
|
|
|
import android.webkit.WebChromeClient;
|
|
|
import android.webkit.WebView;
|
|
|
import android.webkit.WebViewClient;
|
|
|
+import android.widget.Toast;
|
|
|
|
|
|
import com.knacki.mimou.BuildConfig;
|
|
|
-import com.knacki.mimou.api.CredentialHolder;
|
|
|
+import com.knacki.mimou.preference.CredentialHolder;
|
|
|
import com.knacki.mimou.JsInterface;
|
|
|
import com.knacki.mimou.R;
|
|
|
+import com.knacki.mimou.preference.UserSettings;
|
|
|
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+import java.security.Permissions;
|
|
|
+import java.util.concurrent.Callable;
|
|
|
+import java.util.logging.Level;
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
|
public class MainActivity extends AppCompatActivity {
|
|
|
+ public final static int SMS_PERMISSION_CALLBACK = 42;
|
|
|
protected WebView web;
|
|
|
protected JsInterface interfaceMimouDroid;
|
|
|
private static Logger log = Logger.getLogger(MainActivity.class.getName());
|
|
|
@@ -56,7 +73,7 @@ public class MainActivity extends AppCompatActivity {
|
|
|
web.loadUrl(CredentialHolder.isLocal(this) ? (getString(R.string.mimouUrl) + (getString(R.string.localUrl))) : getString(R.string.mimouUrl));
|
|
|
}
|
|
|
|
|
|
- protected void showLogin() {
|
|
|
+ public void showLogin() {
|
|
|
startActivity(new Intent(MainActivity.this, LoginActivity.class));
|
|
|
}
|
|
|
|
|
|
@@ -69,6 +86,74 @@ public class MainActivity extends AppCompatActivity {
|
|
|
interfaceMimouDroid.setActivity(this);
|
|
|
}
|
|
|
|
|
|
+ public void injectJavascript(String js) {
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT || false) {
|
|
|
+ web.evaluateJavascript(js, new ValueCallback<String>() {
|
|
|
+ @Override
|
|
|
+ public void onReceiveValue(String s) {
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ try {
|
|
|
+ web.loadUrl("data:text/html;charset=utf-8;base64," + Base64.encodeToString(("<script>" +js +"</script>").getBytes("UTF-8"), Base64.DEFAULT));
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ log.log(Level.SEVERE, e.getMessage(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private JsInterface.TypedCallback<Boolean> currentCallback = null;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
|
|
+ boolean allGranted = true;
|
|
|
+ for (int grantResult: grantResults) {
|
|
|
+ if (grantResult != PackageManager.PERMISSION_GRANTED) {
|
|
|
+ allGranted = false;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (currentCallback != null) {
|
|
|
+ currentCallback.onResult(allGranted);
|
|
|
+ currentCallback = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void promptAndroidPermission() {
|
|
|
+ ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.READ_SMS, Manifest.permission.SEND_SMS}, SMS_PERMISSION_CALLBACK);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void promptSmsPermission(final JsInterface.TypedCallback<Boolean> callback) {
|
|
|
+ new AlertDialog.Builder(this)
|
|
|
+ .setMessage(getString(R.string.useSmsPermission))
|
|
|
+ .setPositiveButton(getString(R.string.yes), new DialogInterface.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(DialogInterface dialogInterface, int i) {
|
|
|
+ currentCallback = callback;
|
|
|
+ promptAndroidPermission();
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .setNegativeButton(getString(R.string.no), new DialogInterface.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(DialogInterface dialogInterface, int i) {
|
|
|
+ callback.onResult(false);
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .show();
|
|
|
+ }
|
|
|
+
|
|
|
+ public void requestSmsPermission(JsInterface.TypedCallback<Boolean> callback) {
|
|
|
+ Boolean userPermission = UserSettings.readSms(this);
|
|
|
+ if (userPermission == null) {
|
|
|
+ promptSmsPermission(callback);
|
|
|
+ } else if (userPermission == false) {
|
|
|
+ callback.onResult(false);
|
|
|
+ } else {
|
|
|
+ currentCallback = callback;
|
|
|
+ promptAndroidPermission();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public void onBackPressed() {
|
|
|
moveTaskToBack(true);
|