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.os.Looper; import android.support.annotation.NonNull; import android.support.v4.app.ActivityCompat; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.webkit.WebChromeClient; import android.webkit.WebView; import android.webkit.WebViewClient; import com.knacki.mimou.BuildConfig; import com.knacki.mimou.bridge.JavascriptFunction; import com.knacki.mimou.preference.CredentialHolder; import com.knacki.mimou.bridge.JsInterface; import com.knacki.mimou.R; import com.knacki.mimou.preference.UserSettings; import java.util.List; import java.util.logging.Logger; public class MainActivity extends AppCompatActivity { public final static int SMS_PERMISSION_CALLBACK = 42; protected WebView web; protected JsInterface interfaceMimouDroid; public static boolean needReload = true; private static Logger log = Logger.getLogger(MainActivity.class.getName()); private JsInterface.TypedCallback currentCallback = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); interfaceMimouDroid = new JsInterface(); web = (WebView) findViewById(R.id.webview); web.setWebChromeClient(new WebChromeClient()); web.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { Uri uri = Uri.parse(url); log.severe("Loading url " +url); if (uri.getHost().equals(getString(R.string.mimouHost))) { List pathSegments = uri.getPathSegments(); if (pathSegments.size() > 0) { if (pathSegments.get(0).equals("login") && !url.contains("?phoneAccess=")) { CredentialHolder.reset(MainActivity.this); showLogin(); return true; } } return super.shouldOverrideUrlLoading(view, url); } startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url))); return true; } }); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { web.setWebContentsDebuggingEnabled(BuildConfig.DEBUG); } web.getSettings().setJavaScriptEnabled(true); web.addJavascriptInterface(interfaceMimouDroid, "__native"); needReload = true; Uri intentUri = getIntent().getData(); if (intentUri != null) { String securityToken = intentUri.getQueryParameter("token"); if (securityToken != null) { CredentialHolder.setSecurityToken(this, securityToken); } } reload(); } public void reload() { if (needReload && web != null) { if (CredentialHolder.hasCredential(this)) { final String url = getString(R.string.mimouUrl) + (CredentialHolder.isLocal(this) ? getString(R.string.localUrl) : (getString(R.string.loginUrl) + "/android?phoneAccess=" +CredentialHolder.getToken(this))); log.fine("Loading URL: " +url); web.loadUrl(url); } else { showLogin(); } } needReload = false; } public void showLogin() { startActivity(new Intent(MainActivity.this, LoginActivity.class)); } @Override protected void onResume() { super.onResume(); if (!CredentialHolder.hasCredential(this)) showLogin(); else reload(); interfaceMimouDroid.setActivity(this); } @Override protected void onDestroy() { super.onDestroy(); if (web != null) { web.removeAllViews(); web.clearHistory(); web.clearCache(true); web.destroyDrawingCache(); web.pauseTimers(); web.destroy(); web = null; } } private void runJavascript(final String js) { (new Handler(Looper.getMainLooper())).post(new Runnable() { @Override public void run() { if (web != null) web.loadUrl("javascript:" +js); } }); } public void runJavascript(JavascriptFunction fnc) { runJavascript(fnc.toString()); } @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 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) { CredentialHolder.setSMSPermission(MainActivity.this, false); currentCallback = callback; promptAndroidPermission(); } }) .setNegativeButton(getString(R.string.no), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { CredentialHolder.setSMSPermission(MainActivity.this, false); callback.onResult(false); } }) .setCancelable(false) .show(); } public void requestSmsPermission(JsInterface.TypedCallback callback) { Boolean userPermission = CredentialHolder.hasSMSPermission(this); if (userPermission == null) { promptSmsPermission(callback); } else if (userPermission == false) { callback.onResult(false); } else { currentCallback = callback; promptAndroidPermission(); } } @Override public void onBackPressed() { if (UserSettings.backShowMenu(this)) runJavascript(new JavascriptFunction("onNativeBackPressed")); else moveTaskToBack(true); } }