From d80d705fce7100fef0ea3ebe5175f71f7ec41b4c Mon Sep 17 00:00:00 2001 From: Mariano Ruiz Date: Thu, 11 Mar 2021 09:35:19 -0300 Subject: [PATCH 1/8] Logging improvements - Remove redundant DEBUG check performed by MedicLog.* methods - Better and more homogeneous logging messages - Add caller reference to Medic.log --- .../webapp/mobile/AppUrlIntentActivity.java | 3 +-- .../webapp/mobile/AppUrlVerifier.java | 3 +-- .../mobile/FreeSpaceWarningActivity.java | 3 +-- .../medicmobile/webapp/mobile/LockScreen.java | 3 +-- .../medicmobile/webapp/mobile/MedicLog.java | 26 ++++++------------- .../webapp/mobile/SettingsDialogActivity.java | 12 ++++----- .../webapp/mobile/SettingsStore.java | 5 ++-- .../webapp/mobile/SimprintsSupport.java | 4 +-- .../medicmobile/webapp/mobile/SmsSender.java | 14 +++++----- .../webapp/mobile/StartupActivity.java | 3 +-- .../mobile/EmbeddedBrowserActivity.java | 9 ++++--- .../webapp/mobile/PhotoGrabber.java | 5 +++- .../webapp/mobile/XWalkMigration.java | 1 - .../mobile/EmbeddedBrowserActivity.java | 11 ++++---- .../webapp/mobile/PhotoGrabber.java | 5 +++- 15 files changed, 49 insertions(+), 58 deletions(-) diff --git a/src/main/java/org/medicmobile/webapp/mobile/AppUrlIntentActivity.java b/src/main/java/org/medicmobile/webapp/mobile/AppUrlIntentActivity.java index 35c2a46f..f92a4639 100644 --- a/src/main/java/org/medicmobile/webapp/mobile/AppUrlIntentActivity.java +++ b/src/main/java/org/medicmobile/webapp/mobile/AppUrlIntentActivity.java @@ -5,7 +5,6 @@ import android.os.Bundle; import android.net.Uri; -import static org.medicmobile.webapp.mobile.BuildConfig.DEBUG; import static org.medicmobile.webapp.mobile.MedicLog.trace; public class AppUrlIntentActivity extends Activity { @@ -14,7 +13,7 @@ public class AppUrlIntentActivity extends Activity { Intent appLinkIntent = getIntent(); Uri appLinkData = appLinkIntent.getData(); - if(DEBUG) trace(this, "TOKEN LOGIN=%s", appLinkData.toString()); + trace(this, "onCreate() :: Token Login: %s", appLinkData.toString()); startActivity(new Intent(Intent.ACTION_VIEW, appLinkData, this, EmbeddedBrowserActivity.class)); finish(); diff --git a/src/main/java/org/medicmobile/webapp/mobile/AppUrlVerifier.java b/src/main/java/org/medicmobile/webapp/mobile/AppUrlVerifier.java index 324378b7..b6bca336 100644 --- a/src/main/java/org/medicmobile/webapp/mobile/AppUrlVerifier.java +++ b/src/main/java/org/medicmobile/webapp/mobile/AppUrlVerifier.java @@ -5,7 +5,6 @@ import org.json.JSONException; import org.json.JSONObject; -import static org.medicmobile.webapp.mobile.BuildConfig.DEBUG; import static org.medicmobile.webapp.mobile.BuildConfig.DISABLE_APP_URL_VALIDATION; import static org.medicmobile.webapp.mobile.MedicLog.trace; import static org.medicmobile.webapp.mobile.R.string.errAppUrl_apiNotReady; @@ -37,7 +36,7 @@ public AppUrlVerification verify(String appUrl) { return AppUrlVerification.failure(appUrl, errAppUrl_appNotFound); } catch(IOException ex) { - if(DEBUG) trace(ex, "Exception caught trying to verify url: %s", redactUrl(appUrl)); + trace(ex, "Exception caught trying to verify url: %s", redactUrl(appUrl)); return AppUrlVerification.failure(appUrl, errAppUrl_serverNotFound); } diff --git a/src/main/java/org/medicmobile/webapp/mobile/FreeSpaceWarningActivity.java b/src/main/java/org/medicmobile/webapp/mobile/FreeSpaceWarningActivity.java index e5ed72e8..bf3ed00d 100644 --- a/src/main/java/org/medicmobile/webapp/mobile/FreeSpaceWarningActivity.java +++ b/src/main/java/org/medicmobile/webapp/mobile/FreeSpaceWarningActivity.java @@ -4,7 +4,6 @@ import android.view.View; import android.widget.TextView; -import static org.medicmobile.webapp.mobile.BuildConfig.DEBUG; import static org.medicmobile.webapp.mobile.MedicLog.trace; import static org.medicmobile.webapp.mobile.Utils.startAppActivityChain; @@ -14,7 +13,7 @@ public class FreeSpaceWarningActivity extends LockableActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - if(DEBUG) trace(this, "Starting..."); + trace(this, "onCreate()"); setContentView(R.layout.free_space_warning); diff --git a/src/main/java/org/medicmobile/webapp/mobile/LockScreen.java b/src/main/java/org/medicmobile/webapp/mobile/LockScreen.java index 1530a532..2cf19d08 100644 --- a/src/main/java/org/medicmobile/webapp/mobile/LockScreen.java +++ b/src/main/java/org/medicmobile/webapp/mobile/LockScreen.java @@ -11,7 +11,6 @@ import android.widget.TextView; import android.widget.Toast; -import static org.medicmobile.webapp.mobile.BuildConfig.DEBUG; import static org.medicmobile.webapp.mobile.MedicLog.trace; import static org.medicmobile.webapp.mobile.MedicLog.warn; @@ -42,7 +41,7 @@ enum Task { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - if(DEBUG) trace(this, "Starting..."); + trace(this, "onCreate()"); task = Task.valueOf(getIntent().getStringExtra(X_TASK)); diff --git a/src/main/java/org/medicmobile/webapp/mobile/MedicLog.java b/src/main/java/org/medicmobile/webapp/mobile/MedicLog.java index ccb57b65..1d922dae 100644 --- a/src/main/java/org/medicmobile/webapp/mobile/MedicLog.java +++ b/src/main/java/org/medicmobile/webapp/mobile/MedicLog.java @@ -13,26 +13,20 @@ private MedicLog() {} public static void trace(Object caller, String message, Object... extras) { if(!DEBUG) return; - d(LOG_TAG, messageWithCaller(caller, message, extras)); } - public static void log(String message, Object... extras) { - message = format(message, extras); - - i(LOG_TAG, message); - } - public static void trace(Exception ex, String message, Object... extras) { - message = format(message, extras); + if(!DEBUG) return; + d(LOG_TAG, format(message, extras), ex); + } - d(LOG_TAG, message, ex); + public static void log(Object caller, String message, Object... extras) { + i(LOG_TAG, messageWithCaller(caller, message, extras)); } public static void log(Exception ex, String message, Object... extras) { - message = format(message, extras); - - i(LOG_TAG, message, ex); + i(LOG_TAG, format(message, extras), ex); } public static void warn(Object caller, String message, Object... extras) { @@ -40,15 +34,11 @@ public static void warn(Object caller, String message, Object... extras) { } public static void warn(Exception ex, String message, Object... extras) { - message = format(message, extras); - - w(LOG_TAG, message, ex); + w(LOG_TAG, format(message, extras), ex); } public static void error(Exception ex, String message, Object... extras) { - message = format(message, extras); - - e(LOG_TAG, message, ex); + e(LOG_TAG, format(message, extras), ex); } private static String messageWithCaller(Object caller, String message, Object... extras) { diff --git a/src/main/java/org/medicmobile/webapp/mobile/SettingsDialogActivity.java b/src/main/java/org/medicmobile/webapp/mobile/SettingsDialogActivity.java index affd41f2..cd20e557 100644 --- a/src/main/java/org/medicmobile/webapp/mobile/SettingsDialogActivity.java +++ b/src/main/java/org/medicmobile/webapp/mobile/SettingsDialogActivity.java @@ -59,7 +59,7 @@ protected void onPostExecute(AppUrlVerification result) { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - if(DEBUG) trace(this, "Starting..."); + trace(this, "onCreate()"); this.settings = SettingsStore.in(this); this.serverRepo = new ServerRepo(this); @@ -100,7 +100,7 @@ private void displayCustomServerForm() { //> EVENT HANDLERS public void verifyAndSave(View view) { - if(DEBUG) trace(this, "verifyAndSave"); + trace(this, "verifyAndSave()"); submitButton().setEnabled(false); cancelButton().setEnabled(false); @@ -111,7 +111,7 @@ public void verifyAndSave(View view) { } public void cancelSettingsEdit(View view) { - if(DEBUG) trace(this, "cancelSettingsEdit"); + trace(this, "cancelSettingsEdit()"); backToWebview(); } @@ -141,12 +141,12 @@ private void saveSettings(WebappSettings s) { settings.updateWith(s); this.backToWebview(); } catch(IllegalSettingsException ex) { - if(DEBUG) trace(ex, "Tried to save illegal setting."); + trace(ex, "Tried to save illegal setting."); for(IllegalSetting error : ex.errors) { showError(error); } } catch(SettingsException ex) { - if(DEBUG) trace(ex, "Problem savung settings."); + trace(ex, "Problem saving settings."); submitButton().setError(ex.getMessage()); } } @@ -217,7 +217,7 @@ class ServerMetadata { } ServerMetadata(String name, String url) { - if(DEBUG) trace(this, "constructor :: name:%s, url:%s", name, redactUrl(url)); + trace(this, "ServerMetadata() :: name: %s, url: %s", name, redactUrl(url)); this.name = name; this.url = url; } diff --git a/src/main/java/org/medicmobile/webapp/mobile/SettingsStore.java b/src/main/java/org/medicmobile/webapp/mobile/SettingsStore.java index 41ac9ea3..1e355ab8 100644 --- a/src/main/java/org/medicmobile/webapp/mobile/SettingsStore.java +++ b/src/main/java/org/medicmobile/webapp/mobile/SettingsStore.java @@ -7,7 +7,6 @@ import static org.medicmobile.webapp.mobile.BuildConfig.DEBUG; import static org.medicmobile.webapp.mobile.SimpleJsonClient2.redactUrl; -import static org.medicmobile.webapp.mobile.MedicLog.log; import static org.medicmobile.webapp.mobile.MedicLog.trace; @SuppressWarnings("PMD.ShortMethodName") @@ -72,7 +71,7 @@ void setUserDeniedGeolocation() throws SettingsException { } static SettingsStore in(Context ctx) { - if(DEBUG) log("Loading settings for context %s...", ctx); + trace(SettingsStore.class, "Loading settings for context %s...", ctx); SharedPreferences prefs = ctx.getSharedPreferences( SettingsStore.class.getName(), @@ -137,7 +136,7 @@ class WebappSettings { public final String appUrl; public WebappSettings(String appUrl) { - if(DEBUG) trace(this, "WebappSettings() appUrl=%s", redactUrl(appUrl)); + trace(this, "WebappSettings() :: appUrl: %s", redactUrl(appUrl)); this.appUrl = appUrl; } diff --git a/src/main/java/org/medicmobile/webapp/mobile/SimprintsSupport.java b/src/main/java/org/medicmobile/webapp/mobile/SimprintsSupport.java index 0382d9c5..be79efef 100644 --- a/src/main/java/org/medicmobile/webapp/mobile/SimprintsSupport.java +++ b/src/main/java/org/medicmobile/webapp/mobile/SimprintsSupport.java @@ -77,7 +77,7 @@ String process(int requestCode, Intent i) { } } - log("Simprints ident returned IDs: " + result + "; requestId=" + requestId); + log(this, "Simprints ident returned IDs: " + result + "; requestId=" + requestId); return jsResponse("identify", requestId, result); } catch(JSONException ex) { @@ -91,7 +91,7 @@ String process(int requestCode, Intent i) { if(i == null || !i.hasExtra(SIMPRINTS_REGISTRATION)) return "console.log('No registration data returned from simprints app.')"; Registration registration = i.getParcelableExtra(SIMPRINTS_REGISTRATION); String id = registration.getGuid(); - log("Simprints registration returned ID: " + id + "; requestId=" + requestCode); + log(this, "Simprints registration returned ID: " + id + "; requestId=" + requestCode); return jsResponse("register", requestId, json("id", id)); } catch(JSONException ex) { warn(ex, "Problem serialising simprints registration result."); diff --git a/src/main/java/org/medicmobile/webapp/mobile/SmsSender.java b/src/main/java/org/medicmobile/webapp/mobile/SmsSender.java index 12e1fc99..495c613a 100644 --- a/src/main/java/org/medicmobile/webapp/mobile/SmsSender.java +++ b/src/main/java/org/medicmobile/webapp/mobile/SmsSender.java @@ -38,7 +38,7 @@ class SmsSender { parent.registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context ctx, Intent intent) { - log("BroadcastReceiver.onReceive() :: %s", intent.getAction()); + log(this, "onReceive() :: %s", intent.getAction()); try { switch(intent.getAction()) { @@ -158,11 +158,11 @@ class DeliveryReportHandler { //> PUBLIC API public void handle(Intent intent) { - log("Received delivery report for message %s.", describe(intent)); + log(this, "Received delivery report for message: %s", describe(intent)); int status = createFromPdu(intent).getStatus(); - log("Delivery status: 0x" + toHexString(status)); + log(this, "Delivery status: 0x" + toHexString(status)); if((status & GSM_STATUS_MASK) == status) { handleGsmDelivery(intent, status); @@ -235,17 +235,17 @@ private void handleGsmDelivery(Intent intent, int status) { } else throw new IllegalStateException("Unexpected status (> 0x7F) : 0x" + toHexString(status)); reportStatus(intent, "FAILED", fDetail); - log("Delivering message to %s failed (cause: %s)", describe(intent), fDetail); + log(this, "Delivering message to %s failed (cause: %s)", describe(intent), fDetail); } private void handleCdmaDelivery() { - log("Delivery reports not yet supported on CDMA devices."); + log(this, "Delivery reports not yet supported on CDMA devices."); } } class SendingReportHandler { void handle(Intent intent, int resultCode) { - log("Received sending report for message %s.", describe(intent)); + log(this, "Received sending report for message: %s", describe(intent)); if(resultCode == RESULT_OK) { reportStatus(intent, "SENT"); @@ -268,7 +268,7 @@ void handle(Intent intent, int resultCode) { failureReason = "unknown; resultCode=" + resultCode; } reportStatus(intent, "FAILED", failureReason); - log("Sending message %s failed. Cause: %s", describe(intent), failureReason); + log(this, "Sending message %s failed. Cause: %s", describe(intent), failureReason); } } diff --git a/src/main/java/org/medicmobile/webapp/mobile/StartupActivity.java b/src/main/java/org/medicmobile/webapp/mobile/StartupActivity.java index 3bb793b5..a36d280f 100644 --- a/src/main/java/org/medicmobile/webapp/mobile/StartupActivity.java +++ b/src/main/java/org/medicmobile/webapp/mobile/StartupActivity.java @@ -4,7 +4,6 @@ import android.content.Intent; import android.os.Bundle; -import static org.medicmobile.webapp.mobile.BuildConfig.DEBUG; import static org.medicmobile.webapp.mobile.MedicLog.trace; import static org.medicmobile.webapp.mobile.Utils.createUseragentFrom; import static org.medicmobile.webapp.mobile.Utils.startAppActivityChain; @@ -12,7 +11,7 @@ public class StartupActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - if(DEBUG) trace(this, "Starting..."); + trace(this, "onCreate()"); configureAndStartNextActivity(); diff --git a/src/webview/java/org/medicmobile/webapp/mobile/EmbeddedBrowserActivity.java b/src/webview/java/org/medicmobile/webapp/mobile/EmbeddedBrowserActivity.java index fec1754b..93225c88 100644 --- a/src/webview/java/org/medicmobile/webapp/mobile/EmbeddedBrowserActivity.java +++ b/src/webview/java/org/medicmobile/webapp/mobile/EmbeddedBrowserActivity.java @@ -301,7 +301,7 @@ private String getUrlToLoad(Uri url) { private void browseTo(Uri url) { String urlToLoad = getUrlToLoad(url); - if(DEBUG) trace(this, "Pointing browser to %s", redactUrl(urlToLoad)); + trace(this, "Pointing browser to: %s", redactUrl(urlToLoad)); container.loadUrl(urlToLoad, null); } @@ -323,7 +323,7 @@ private void setUpUiClient(WebView container) { return true; } @Override public boolean onShowFileChooser(WebView webView, ValueCallback filePathCallback, WebChromeClient.FileChooserParams fileChooserParams){ - if(DEBUG) trace(this, "onShowFileChooser() :: %s,%s,%s", webView, filePathCallback, fileChooserParams); + trace(this, "onShowFileChooser() :: webView: %s,filePathCallback: %s,fileChooserParams: %s", webView, filePathCallback, fileChooserParams); boolean capture = fileChooserParams.isCaptureEnabled(); trace(this, "onShowFileChooser() capture :: %s", capture); @@ -417,8 +417,9 @@ public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request @Override public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { String failingUrl = request.getUrl().toString(); - if(DEBUG) trace(this, "onReceivedLoadError() :: %s,%s,%s", failingUrl, error.getErrorCode(), error.getDescription()); - if(!getRootUrl().equals(failingUrl)) { + trace(this, "onReceivedLoadError() :: url: %s, error code: %s, description: %s", + failingUrl, error.getErrorCode(), error.getDescription()); + if (!getRootUrl().equals(failingUrl)) { super.onReceivedError(view, request, error); } else { evaluateJavascript(String.format( diff --git a/src/webview/java/org/medicmobile/webapp/mobile/PhotoGrabber.java b/src/webview/java/org/medicmobile/webapp/mobile/PhotoGrabber.java index 8a8425db..34d4b893 100644 --- a/src/webview/java/org/medicmobile/webapp/mobile/PhotoGrabber.java +++ b/src/webview/java/org/medicmobile/webapp/mobile/PhotoGrabber.java @@ -206,7 +206,10 @@ private Bitmap downsize(Bitmap bm) { private File tempFile() throws IOException { File imageCacheDir = new File(a.getCacheDir(), "medic-form-photos"); boolean mkdirSuccess = imageCacheDir.mkdirs(); - if(!mkdirSuccess) log("tempFile() :: imageCacheDir.mkdirs() failed. This may cause problems with taking photos."); + if(!mkdirSuccess) { + log(this, "tempFile() :: imageCacheDir.mkdirs() failed. " + + "This may cause problems with taking photos."); + } return File.createTempFile("photo", ".jpg", imageCacheDir); } } diff --git a/src/webview/java/org/medicmobile/webapp/mobile/XWalkMigration.java b/src/webview/java/org/medicmobile/webapp/mobile/XWalkMigration.java index 982fad5b..cdbc272f 100644 --- a/src/webview/java/org/medicmobile/webapp/mobile/XWalkMigration.java +++ b/src/webview/java/org/medicmobile/webapp/mobile/XWalkMigration.java @@ -4,7 +4,6 @@ import android.app.Activity; import android.content.Context; import android.os.Build; - import java.io.File; import static org.medicmobile.webapp.mobile.MedicLog.trace; diff --git a/src/xwalk/java/org/medicmobile/webapp/mobile/EmbeddedBrowserActivity.java b/src/xwalk/java/org/medicmobile/webapp/mobile/EmbeddedBrowserActivity.java index 457a1869..d6cd72a6 100644 --- a/src/xwalk/java/org/medicmobile/webapp/mobile/EmbeddedBrowserActivity.java +++ b/src/xwalk/java/org/medicmobile/webapp/mobile/EmbeddedBrowserActivity.java @@ -32,7 +32,6 @@ import org.xwalk.core.XWalkWebResourceResponse; import static java.lang.Boolean.parseBoolean; -import static org.medicmobile.webapp.mobile.BuildConfig.DEBUG; import static org.medicmobile.webapp.mobile.BuildConfig.DISABLE_APP_URL_VALIDATION; import static org.medicmobile.webapp.mobile.MedicLog.error; import static org.medicmobile.webapp.mobile.MedicLog.log; @@ -312,7 +311,7 @@ private String getUrlToLoad(Uri url) { private void browseTo(Uri url) { String urlToLoad = getUrlToLoad(url); - if(DEBUG) trace(this, "Pointing browser to %s", redactUrl(urlToLoad)); + trace(this, "Pointing browser to: %s", redactUrl(urlToLoad)); container.load(urlToLoad, null); } @@ -336,7 +335,8 @@ private void setUpUiClient(XWalkView container) { } */ @Override public void openFileChooser(XWalkView view, ValueCallback callback, String acceptType, String shouldCapture) { - if(DEBUG) trace(this, "openFileChooser() :: %s,%s,%s,%s", view, callback, acceptType, shouldCapture); + trace(this, "openFileChooser() :: view: %s, callback: %s, acceptType: %s, shouldCapture: %s", + view, callback, acceptType, shouldCapture); boolean capture = parseBoolean(shouldCapture); @@ -450,10 +450,11 @@ private void enableUrlHandlers(XWalkView container) { @Override public void onReceivedLoadError(XWalkView view, int errorCode, String description, String failingUrl) { if(errorCode == XWalkResourceClient.ERROR_OK) return; - log("EmbeddedBrowserActivity.onReceivedLoadError() :: [%s] %s :: %s", errorCode, failingUrl, description); + log(this, "onReceivedLoadError() :: [%s] %s :: %s", + errorCode, failingUrl, description); if(!getRootUrl().equals(failingUrl)) { - log("EmbeddedBrowserActivity.onReceivedLoadError() :: ignoring for non-root URL"); + log(this, "onReceivedLoadError() :: ignoring for non-root URL"); } evaluateJavascript(String.format( diff --git a/src/xwalk/java/org/medicmobile/webapp/mobile/PhotoGrabber.java b/src/xwalk/java/org/medicmobile/webapp/mobile/PhotoGrabber.java index ea805bcb..bf6aae02 100644 --- a/src/xwalk/java/org/medicmobile/webapp/mobile/PhotoGrabber.java +++ b/src/xwalk/java/org/medicmobile/webapp/mobile/PhotoGrabber.java @@ -197,7 +197,10 @@ private Bitmap downsize(Bitmap bm) { private File tempFile() throws IOException { File imageCacheDir = new File(a.getCacheDir(), "medic-form-photos"); boolean mkdirSuccess = imageCacheDir.mkdirs(); - if(!mkdirSuccess) log("tempFile() :: imageCacheDir.mkdirs() failed. This may cause problems with taking photos."); + if(!mkdirSuccess) { + log(this, "tempFile() :: imageCacheDir.mkdirs() failed. " + + "This may cause problems with taking photos."); + } return File.createTempFile("photo", ".jpg", imageCacheDir); } } From 754e95be64c7270ea49c91d01edd601773df37a7 Mon Sep 17 00:00:00 2001 From: Mariano Ruiz Date: Thu, 11 Mar 2021 10:33:56 -0300 Subject: [PATCH 2/8] Upgrade com.android.tools.build:gradle to 4.1.2 Upgrade due security concerns highlighted by Android Studio, though there is no release notes with the issues fixed in the library --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 0da22bb9..1ace054e 100644 --- a/build.gradle +++ b/build.gradle @@ -8,7 +8,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:4.1.1' + classpath 'com.android.tools.build:gradle:4.1.2' classpath 'com.noveogroup.android:check:1.2.5' } } From a81bfa372e787a047e6c92c89ef16789ad0c1918 Mon Sep 17 00:00:00 2001 From: Mariano Ruiz Date: Thu, 11 Mar 2021 11:55:17 -0300 Subject: [PATCH 3/8] =?UTF-8?q?After=20XWalk=20=E2=86=92=20Webview=20migra?= =?UTF-8?q?tion=20restart=20app=20to=20avoid=20login=20page?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mobile/EmbeddedBrowserActivity.java | 36 ++++++++++++++++++- .../webapp/mobile/XWalkMigration.java | 32 ++++++++--------- 2 files changed, 49 insertions(+), 19 deletions(-) diff --git a/src/webview/java/org/medicmobile/webapp/mobile/EmbeddedBrowserActivity.java b/src/webview/java/org/medicmobile/webapp/mobile/EmbeddedBrowserActivity.java index 93225c88..70b53f3a 100644 --- a/src/webview/java/org/medicmobile/webapp/mobile/EmbeddedBrowserActivity.java +++ b/src/webview/java/org/medicmobile/webapp/mobile/EmbeddedBrowserActivity.java @@ -15,6 +15,7 @@ import android.view.View; import android.view.Window; import android.webkit.ConsoleMessage; +import android.webkit.CookieManager; import android.webkit.GeolocationPermissions; import android.webkit.ValueCallback; import android.webkit.WebChromeClient; @@ -32,6 +33,7 @@ import static org.medicmobile.webapp.mobile.BuildConfig.DEBUG; import static org.medicmobile.webapp.mobile.BuildConfig.DISABLE_APP_URL_VALIDATION; import static org.medicmobile.webapp.mobile.MedicLog.error; +import static org.medicmobile.webapp.mobile.MedicLog.log; import static org.medicmobile.webapp.mobile.MedicLog.trace; import static org.medicmobile.webapp.mobile.MedicLog.warn; import static org.medicmobile.webapp.mobile.SimpleJsonClient2.redactUrl; @@ -78,6 +80,8 @@ public void onReceiveValue(String result) { private PhotoGrabber photoGrabber; private SmsSender smsSender; + private boolean isMigrationRunning = false; + //> ACTIVITY LIFECYCLE METHODS @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -131,7 +135,17 @@ public void onReceiveValue(String result) { @Override protected void onStart() { - new XWalkMigration(this).run(); + trace(this, "onStart() :: Checking Crosswalk migration ..."); + XWalkMigration xWalkMigration = new XWalkMigration(this); + if (xWalkMigration.hasToMigrate()) { + log(this, "onStart() :: Running Crosswalk migration ..."); + //TODO display waiting... + isMigrationRunning = true; + xWalkMigration.run(); + } else { + trace(this, "onStart() :: Crosswalk installation not found - skipping migration"); + } + trace(this, "onStart() :: Checking Crosswalk migration done."); super.onStart(); } @@ -436,6 +450,26 @@ public void onReceivedError(WebView view, WebResourceRequest request, WebResourc "}", error.getErrorCode(), error.getDescription()), false); } } + + @Override + public void onPageFinished(WebView view, String url) { + trace(this, "onPageFinished() :: url: %s, isMigrationRunning: %s", url, isMigrationRunning); + if (isMigrationRunning && url.contains("/login")) { + isMigrationRunning = false; + CookieManager cookieManager = CookieManager.getInstance(); + String cookie = cookieManager.getCookie(appUrl); + if (cookie == null) { + log(this, "onPageFinished() :: Migration process in progress, and " + + "cookies were not loaded, restarting ..."); + Context context = view.getContext(); + Intent intent = new Intent(context, StartupActivity.class); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); + context.startActivity(intent); + Runtime.getRuntime().exit(0); + } + trace(this, "onPageFinished() :: Cookies loaded, skipping restart"); + } + } }); } diff --git a/src/webview/java/org/medicmobile/webapp/mobile/XWalkMigration.java b/src/webview/java/org/medicmobile/webapp/mobile/XWalkMigration.java index cdbc272f..6491b1a6 100644 --- a/src/webview/java/org/medicmobile/webapp/mobile/XWalkMigration.java +++ b/src/webview/java/org/medicmobile/webapp/mobile/XWalkMigration.java @@ -14,8 +14,6 @@ public class XWalkMigration { public static final String TAG = "Migration"; - private static boolean hasRun = false; - private static String xWalkPath = "app_xwalkcore/Default"; // Root dir for system webview data used by Android 4.4+ @@ -52,22 +50,17 @@ public XWalkMigration(Activity a) { activity = a; context = a.getApplicationContext(); isModernAndroid = Build.VERSION.SDK_INT >= 19; - trace(this, "Set up Crosswalk migration, %s, %s, %s", hasRun, activity, context); } - public void run() { - trace(this, "Running Crosswalk migration"); - + /** + * Check whether the migration needs to be done. + */ + public boolean hasToMigrate() { boolean found = lookForXwalk(context.getFilesDir()); if (!found) { - lookForXwalk(context.getExternalFilesDir(null)); - } - - if (found) { - migrateData(); - } else { - trace(this, "Crosswalk directory not found - skipping migration"); + found = lookForXwalk(context.getExternalFilesDir(null)); } + return found; // if Crosswalk directory found => need to migrate } private boolean lookForXwalk(File filesPath) { @@ -82,7 +75,10 @@ private boolean lookForXwalk(File filesPath) { return found; } - private void migrateData() { + /** + * Migrate the data from XWalk to Webview + */ + public void run() { xWalkRoot = constructFilePaths(appRoot, xWalkPath); webviewRoot = constructFilePaths(appRoot, getWebviewPath()); @@ -100,7 +96,7 @@ private void migrateData() { for (String dirName : modernAndroidStorage) { if (testFileExists(xWalkRoot, dirName)) { moveDirFromXWalkToWebView(dirName); - trace(this, "Moved " + dirName + " from XWalk to System Webview"); + trace(this, "Moved %s from XWalk to System Webview", dirName); hasMigratedData = true; } } @@ -142,17 +138,17 @@ private String getWebviewLocalStoragePath() { } private void restartCordova() { - trace(this, "restarting EmbeddedBrowserActivity"); + trace(this, "Restarting EmbeddedBrowserActivity"); activity.recreate(); } private boolean testFileExists(File root, String name) { boolean status = false; - if (!name.equals("")) { + if (!name.isEmpty()) { File newPath = constructFilePaths(root.toString(), name); status = newPath.exists(); - trace(this, "exists '" + newPath.getAbsolutePath() + ": " + status); + trace(this, "testFileExists() :: '%s': %s", newPath.getAbsolutePath(), status); } return status; } From 9f31159e0db4b53b30c0af763e8ab8c037a9df43 Mon Sep 17 00:00:00 2001 From: Mariano Ruiz Date: Fri, 12 Mar 2021 13:51:36 -0300 Subject: [PATCH 4/8] Use onPageStarted event instead onPageFinished to avoid seeing the login page even if it is a fraction of seconds --- .../mobile/EmbeddedBrowserActivity.java | 11 ++--- .../webapp/mobile/XWalkMigration.java | 45 +++++++------------ 2 files changed, 23 insertions(+), 33 deletions(-) diff --git a/src/webview/java/org/medicmobile/webapp/mobile/EmbeddedBrowserActivity.java b/src/webview/java/org/medicmobile/webapp/mobile/EmbeddedBrowserActivity.java index 70b53f3a..858f86f5 100644 --- a/src/webview/java/org/medicmobile/webapp/mobile/EmbeddedBrowserActivity.java +++ b/src/webview/java/org/medicmobile/webapp/mobile/EmbeddedBrowserActivity.java @@ -5,6 +5,7 @@ import android.content.Context; import android.content.Intent; import android.app.ActivityManager; +import android.graphics.Bitmap; import android.net.Uri; import android.net.ConnectivityManager; import android.os.Bundle; @@ -136,7 +137,7 @@ public void onReceiveValue(String result) { @Override protected void onStart() { trace(this, "onStart() :: Checking Crosswalk migration ..."); - XWalkMigration xWalkMigration = new XWalkMigration(this); + XWalkMigration xWalkMigration = new XWalkMigration(this.getApplicationContext()); if (xWalkMigration.hasToMigrate()) { log(this, "onStart() :: Running Crosswalk migration ..."); //TODO display waiting... @@ -452,14 +453,14 @@ public void onReceivedError(WebView view, WebResourceRequest request, WebResourc } @Override - public void onPageFinished(WebView view, String url) { - trace(this, "onPageFinished() :: url: %s, isMigrationRunning: %s", url, isMigrationRunning); + public void onPageStarted(WebView view, String url, Bitmap favicon) { + trace(this, "onPageStarted() :: url: %s, isMigrationRunning: %s", url, isMigrationRunning); if (isMigrationRunning && url.contains("/login")) { isMigrationRunning = false; CookieManager cookieManager = CookieManager.getInstance(); String cookie = cookieManager.getCookie(appUrl); if (cookie == null) { - log(this, "onPageFinished() :: Migration process in progress, and " + + log(this, "onPageStarted() :: Migration process in progress, and " + "cookies were not loaded, restarting ..."); Context context = view.getContext(); Intent intent = new Intent(context, StartupActivity.class); @@ -467,7 +468,7 @@ public void onPageFinished(WebView view, String url) { context.startActivity(intent); Runtime.getRuntime().exit(0); } - trace(this, "onPageFinished() :: Cookies loaded, skipping restart"); + trace(this, "onPageStarted() :: Cookies loaded, skipping restart"); } } }); diff --git a/src/webview/java/org/medicmobile/webapp/mobile/XWalkMigration.java b/src/webview/java/org/medicmobile/webapp/mobile/XWalkMigration.java index 6491b1a6..6a42b3bc 100644 --- a/src/webview/java/org/medicmobile/webapp/mobile/XWalkMigration.java +++ b/src/webview/java/org/medicmobile/webapp/mobile/XWalkMigration.java @@ -1,7 +1,6 @@ package org.medicmobile.webapp.mobile; import android.annotation.SuppressLint; -import android.app.Activity; import android.content.Context; import android.os.Build; import java.io.File; @@ -14,22 +13,22 @@ public class XWalkMigration { public static final String TAG = "Migration"; - private static String xWalkPath = "app_xwalkcore/Default"; + private static final String xWalkPath = "app_xwalkcore/Default"; // Root dir for system webview data used by Android 4.4+ - private static String modernWebviewDir = "app_webview"; + private static final String modernWebviewDir = "app_webview"; // Root dir for system webview data used by Android 4.3 and below - private static String oldWebviewDir = "app_database"; + private static final String oldWebviewDir = "app_database"; // Directory name for local storage files used by Android 4.4+ and XWalk - private static String modernLocalStorageDir = "Local Storage"; + private static final String modernLocalStorageDir = "Local Storage"; // Directory name for local storage files used by Android 4.3 and below - private static String oldLocalStorageDir = "localstorage"; + private static final String oldLocalStorageDir = "localstorage"; // Storage directory names used by Android 4.4+ and XWalk - private static String[] modernAndroidStorage = { + private static final String[] modernAndroidStorage = { "Cache", "Cookies", "Cookies-journal", @@ -37,30 +36,26 @@ public class XWalkMigration { "databases" }; - private Activity activity; - private Context context; + @SuppressLint("ObsoleteSdkInt") + private static final boolean isModernAndroid = Build.VERSION.SDK_INT >= 19; - private boolean isModernAndroid; + private boolean xWalkFound; private File appRoot; private File xWalkRoot; private File webviewRoot; - @SuppressLint("ObsoleteSdkInt") - public XWalkMigration(Activity a) { - activity = a; - context = a.getApplicationContext(); - isModernAndroid = Build.VERSION.SDK_INT >= 19; + public XWalkMigration(Context context) { + xWalkFound = lookForXwalk(context.getFilesDir()); + if (!xWalkFound) { + xWalkFound = lookForXwalk(context.getExternalFilesDir(null)); + } } /** * Check whether the migration needs to be done. */ public boolean hasToMigrate() { - boolean found = lookForXwalk(context.getFilesDir()); - if (!found) { - found = lookForXwalk(context.getExternalFilesDir(null)); - } - return found; // if Crosswalk directory found => need to migrate + return xWalkFound; // if Crosswalk directory found => need to migrate } private boolean lookForXwalk(File filesPath) { @@ -79,8 +74,9 @@ private boolean lookForXwalk(File filesPath) { * Migrate the data from XWalk to Webview */ public void run() { - xWalkRoot = constructFilePaths(appRoot, xWalkPath); + if (!xWalkFound) return; + xWalkRoot = constructFilePaths(appRoot, xWalkPath); webviewRoot = constructFilePaths(appRoot, getWebviewPath()); boolean hasMigratedData = false; @@ -104,7 +100,6 @@ public void run() { if (hasMigratedData) { deleteRecursive(xWalkRoot); - restartCordova(); } } @@ -137,12 +132,6 @@ private String getWebviewLocalStoragePath() { } } - private void restartCordova() { - trace(this, "Restarting EmbeddedBrowserActivity"); - activity.recreate(); - } - - private boolean testFileExists(File root, String name) { boolean status = false; if (!name.isEmpty()) { From 7c72508e8c72a8f627034774e3fd51daff1b7512 Mon Sep 17 00:00:00 2001 From: Mariano Ruiz Date: Fri, 12 Mar 2021 15:24:51 -0300 Subject: [PATCH 5/8] Add splash screen with spinner while the migration is happening --- src/main/AndroidManifest.xml | 3 ++ src/main/res/layout/upgrading.xml | 39 +++++++++++++++++++ src/main/res/values-es/strings.xml | 4 ++ src/main/res/values-fr/strings.xml | 4 ++ src/main/res/values/strings.xml | 4 ++ .../mobile/EmbeddedBrowserActivity.java | 2 +- .../webapp/mobile/UpgradingActivity.java | 13 +++++++ 7 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 src/main/res/layout/upgrading.xml create mode 100644 src/webview/java/org/medicmobile/webapp/mobile/UpgradingActivity.java diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index 002ab083..66f14805 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -39,6 +39,9 @@ + + + + + + + + + + diff --git a/src/main/res/values-es/strings.xml b/src/main/res/values-es/strings.xml index 957a2a0b..ff57531d 100644 --- a/src/main/res/values-es/strings.xml +++ b/src/main/res/values-es/strings.xml @@ -5,4 +5,8 @@ Activar No gracias Ícono de mi ubicación + + Cargando… + Actualizando la app + Estás actualizando a la última versión. Por favor aguarda mientras tus datos son migrados. diff --git a/src/main/res/values-fr/strings.xml b/src/main/res/values-fr/strings.xml index 5a671981..e025bd28 100644 --- a/src/main/res/values-fr/strings.xml +++ b/src/main/res/values-fr/strings.xml @@ -5,4 +5,8 @@ Activer Non merci Icône de mon emplacement + + Chargement… + Mettre à jour votre application + Vous passez à la dernière version. Veuillez patienter pendant la migration de vos données. diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index 279b7003..870639ae 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -51,4 +51,8 @@ Turn on No thanks My location icon + + Loading… + Upgrading your app + You\'re upgrading to the latest version. Please wait while your data is being migrated. diff --git a/src/webview/java/org/medicmobile/webapp/mobile/EmbeddedBrowserActivity.java b/src/webview/java/org/medicmobile/webapp/mobile/EmbeddedBrowserActivity.java index 858f86f5..3d82b72a 100644 --- a/src/webview/java/org/medicmobile/webapp/mobile/EmbeddedBrowserActivity.java +++ b/src/webview/java/org/medicmobile/webapp/mobile/EmbeddedBrowserActivity.java @@ -140,8 +140,8 @@ protected void onStart() { XWalkMigration xWalkMigration = new XWalkMigration(this.getApplicationContext()); if (xWalkMigration.hasToMigrate()) { log(this, "onStart() :: Running Crosswalk migration ..."); - //TODO display waiting... isMigrationRunning = true; + startActivity(new Intent(this, UpgradingActivity.class)); xWalkMigration.run(); } else { trace(this, "onStart() :: Crosswalk installation not found - skipping migration"); diff --git a/src/webview/java/org/medicmobile/webapp/mobile/UpgradingActivity.java b/src/webview/java/org/medicmobile/webapp/mobile/UpgradingActivity.java new file mode 100644 index 00000000..030baa24 --- /dev/null +++ b/src/webview/java/org/medicmobile/webapp/mobile/UpgradingActivity.java @@ -0,0 +1,13 @@ +package org.medicmobile.webapp.mobile; + +import android.os.Bundle; +import android.view.Window; + +public class UpgradingActivity extends LockableActivity { + + @Override public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + this.requestWindowFeature(Window.FEATURE_NO_TITLE); + setContentView(R.layout.upgrading); + } +} From 3e49c59bea3d97463df9c04263b590af395c782f Mon Sep 17 00:00:00 2001 From: Mariano Ruiz Date: Mon, 15 Mar 2021 13:58:18 -0300 Subject: [PATCH 6/8] Set `if(DEBUG)` again in the cases were there is a string computation in the arguments --- .../org/medicmobile/webapp/mobile/AppUrlIntentActivity.java | 3 ++- src/main/java/org/medicmobile/webapp/mobile/SettingsStore.java | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/medicmobile/webapp/mobile/AppUrlIntentActivity.java b/src/main/java/org/medicmobile/webapp/mobile/AppUrlIntentActivity.java index f92a4639..da3b6748 100644 --- a/src/main/java/org/medicmobile/webapp/mobile/AppUrlIntentActivity.java +++ b/src/main/java/org/medicmobile/webapp/mobile/AppUrlIntentActivity.java @@ -5,6 +5,7 @@ import android.os.Bundle; import android.net.Uri; +import static org.medicmobile.webapp.mobile.BuildConfig.DEBUG; import static org.medicmobile.webapp.mobile.MedicLog.trace; public class AppUrlIntentActivity extends Activity { @@ -13,7 +14,7 @@ public class AppUrlIntentActivity extends Activity { Intent appLinkIntent = getIntent(); Uri appLinkData = appLinkIntent.getData(); - trace(this, "onCreate() :: Token Login: %s", appLinkData.toString()); + if(DEBUG) trace(this, "onCreate() :: Token Login: %s", appLinkData.toString()); startActivity(new Intent(Intent.ACTION_VIEW, appLinkData, this, EmbeddedBrowserActivity.class)); finish(); diff --git a/src/main/java/org/medicmobile/webapp/mobile/SettingsStore.java b/src/main/java/org/medicmobile/webapp/mobile/SettingsStore.java index 1e355ab8..2dfd873c 100644 --- a/src/main/java/org/medicmobile/webapp/mobile/SettingsStore.java +++ b/src/main/java/org/medicmobile/webapp/mobile/SettingsStore.java @@ -136,7 +136,7 @@ class WebappSettings { public final String appUrl; public WebappSettings(String appUrl) { - trace(this, "WebappSettings() :: appUrl: %s", redactUrl(appUrl)); + if(DEBUG) trace(this, "WebappSettings() :: appUrl: %s", redactUrl(appUrl)); this.appUrl = appUrl; } From e378c7a0ad475922a13555ff25319fc131c180f7 Mon Sep 17 00:00:00 2001 From: Mariano Ruiz Date: Mon, 15 Mar 2021 14:00:17 -0300 Subject: [PATCH 7/8] Refactor code and add comments of why the app is restarted in `onPageStarted()` --- .../org/medicmobile/webapp/mobile/Utils.java | 7 +++++++ .../webapp/mobile/EmbeddedBrowserActivity.java | 17 ++++++++++------- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/medicmobile/webapp/mobile/Utils.java b/src/main/java/org/medicmobile/webapp/mobile/Utils.java index 83018809..7c1b1435 100644 --- a/src/main/java/org/medicmobile/webapp/mobile/Utils.java +++ b/src/main/java/org/medicmobile/webapp/mobile/Utils.java @@ -70,4 +70,11 @@ static String createUseragentFrom(String current) { return String.format("%s %s/%s", current, APPLICATION_ID, VERSION_NAME); } + + static void restartApp(Context context) { + Intent intent = new Intent(context, StartupActivity.class); + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); + context.startActivity(intent); + Runtime.getRuntime().exit(0); + } } diff --git a/src/webview/java/org/medicmobile/webapp/mobile/EmbeddedBrowserActivity.java b/src/webview/java/org/medicmobile/webapp/mobile/EmbeddedBrowserActivity.java index 3d82b72a..d30d6e11 100644 --- a/src/webview/java/org/medicmobile/webapp/mobile/EmbeddedBrowserActivity.java +++ b/src/webview/java/org/medicmobile/webapp/mobile/EmbeddedBrowserActivity.java @@ -40,6 +40,7 @@ import static org.medicmobile.webapp.mobile.SimpleJsonClient2.redactUrl; import static org.medicmobile.webapp.mobile.Utils.createUseragentFrom; import static org.medicmobile.webapp.mobile.Utils.isUrlRelated; +import static org.medicmobile.webapp.mobile.Utils.restartApp; @SuppressWarnings({ "PMD.GodClass", "PMD.TooManyMethods" }) public class EmbeddedBrowserActivity extends LockableActivity { @@ -452,8 +453,14 @@ public void onReceivedError(WebView view, WebResourceRequest request, WebResourc } } - @Override - public void onPageStarted(WebView view, String url, Bitmap favicon) { + // Check how the migration process is going if it was started. + // Because most of the cases after the XWalk -> Webview migration process ends + // the cookies are not available for unknowns reasons, making the webapp to + // redirect the user to the login page instead of the main page. + // If these conditions are met: migration running + /login page + no cookies, + // the app is restarted to refresh the Webview and prevent the user to + // login again. + @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { trace(this, "onPageStarted() :: url: %s, isMigrationRunning: %s", url, isMigrationRunning); if (isMigrationRunning && url.contains("/login")) { isMigrationRunning = false; @@ -462,11 +469,7 @@ public void onPageStarted(WebView view, String url, Bitmap favicon) { if (cookie == null) { log(this, "onPageStarted() :: Migration process in progress, and " + "cookies were not loaded, restarting ..."); - Context context = view.getContext(); - Intent intent = new Intent(context, StartupActivity.class); - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); - context.startActivity(intent); - Runtime.getRuntime().exit(0); + restartApp(view.getContext()); } trace(this, "onPageStarted() :: Cookies loaded, skipping restart"); } From 9958c9708ce5ea5d6bcbe74e5cb32973fe78357c Mon Sep 17 00:00:00 2001 From: Mariano Ruiz Date: Mon, 15 Mar 2021 17:35:13 -0300 Subject: [PATCH 8/8] Fix french translations Co-authored-by: Marc Abbyad --- src/main/res/values-fr/strings.xml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/res/values-fr/strings.xml b/src/main/res/values-fr/strings.xml index e025bd28..b27b3279 100644 --- a/src/main/res/values-fr/strings.xml +++ b/src/main/res/values-fr/strings.xml @@ -6,7 +6,7 @@ Non merci Icône de mon emplacement - Chargement… - Mettre à jour votre application - Vous passez à la dernière version. Veuillez patienter pendant la migration de vos données. + Chargement en cours… + Mise à jour de votre application + Vous passez à la nouvelle version. Veuillez patienter pendant la preparation de vos données.