Skip to content

Commit

Permalink
Reset stored server path on new binary
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile committed Sep 5, 2018
1 parent 1bfea4f commit 9d09b67
Showing 1 changed file with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.graphics.Bitmap;
import android.util.Log;
import android.webkit.WebResourceRequest;
Expand All @@ -27,6 +28,9 @@ public class IonicWebViewEngine extends SystemWebViewEngine {

private WebViewLocalServer localServer;
private String CDV_LOCAL_SERVER;
private static final String LAST_BINARY_VERSION_CODE = "lastBinaryVersionCode";
private static final String LAST_BINARY_VERSION_NAME = "lastBinaryVersionName";


/** Used when created via reflection. */
public IonicWebViewEngine(Context context, CordovaPreferences preferences) {
Expand Down Expand Up @@ -62,11 +66,37 @@ public void init(CordovaWebView parentWebView, CordovaInterface cordova, final C
super.init(parentWebView, cordova, client, resourceApi, pluginManager, nativeToJsMessageQueue);
SharedPreferences prefs = cordova.getActivity().getApplicationContext().getSharedPreferences(IonicWebView.WEBVIEW_PREFS_NAME, Activity.MODE_PRIVATE);
String path = prefs.getString(IonicWebView.CDV_SERVER_PATH, null);
if (path != null) {
if (!isNewBinary() && path != null && !path.isEmpty()) {
setServerBasePath(path);
}
}

private boolean isNewBinary() {
String versionCode = "";
String versionName = "";
SharedPreferences prefs = cordova.getActivity().getApplicationContext().getSharedPreferences(IonicWebView.WEBVIEW_PREFS_NAME, Activity.MODE_PRIVATE);
String lastVersionCode = prefs.getString(LAST_BINARY_VERSION_CODE, null);
String lastVersionName = prefs.getString(LAST_BINARY_VERSION_NAME, null);

try {
PackageInfo pInfo = this.cordova.getActivity().getPackageManager().getPackageInfo(this.cordova.getActivity().getPackageName(), 0);
versionCode = Integer.toString(pInfo.versionCode);
versionName = pInfo.versionName;
} catch(Exception ex) {
Log.e(TAG, "Unable to get package info", ex);
}

if (!versionCode.equals(lastVersionCode) || !versionName.equals(lastVersionName)) {
SharedPreferences.Editor editor = prefs.edit();
editor.putString(LAST_BINARY_VERSION_CODE, versionCode);
editor.putString(LAST_BINARY_VERSION_NAME, versionName);
editor.putString(IonicWebView.CDV_SERVER_PATH, "");
editor.apply();
return true;
}
return false;
}

private class ServerClient extends SystemWebViewClient
{
private ConfigXmlParser parser;
Expand Down

0 comments on commit 9d09b67

Please sign in to comment.