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

fix(cordova): Read DisableDeploy preference before setting start path #1724

Merged
merged 1 commit into from
Jul 1, 2019
Merged
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
19 changes: 14 additions & 5 deletions android/capacitor/src/main/java/com/getcapacitor/Bridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import com.getcapacitor.util.HostMask;

import org.apache.cordova.CordovaInterfaceImpl;
import org.apache.cordova.CordovaPreferences;
import org.apache.cordova.PluginManager;
import org.json.JSONException;
import org.json.JSONObject;
Expand Down Expand Up @@ -102,6 +103,7 @@ public class Bridge {
// A reference to the main WebView for the app
private final WebView webView;
public final CordovaInterfaceImpl cordovaInterface;
private CordovaPreferences preferences;

// Our MessageHandler for sending and receiving data to the WebView
private final MessageHandler msgHandler;
Expand Down Expand Up @@ -134,11 +136,12 @@ public class Bridge {
* @param context
* @param webView
*/
public Bridge(Activity context, WebView webView, List<Class<? extends Plugin>> initialPlugins, CordovaInterfaceImpl cordovaInterface, PluginManager pluginManager) {
public Bridge(Activity context, WebView webView, List<Class<? extends Plugin>> initialPlugins, CordovaInterfaceImpl cordovaInterface, PluginManager pluginManager, CordovaPreferences preferences) {
this.context = context;
this.webView = webView;
this.initialPlugins = initialPlugins;
this.cordovaInterface = cordovaInterface;
this.preferences = preferences;

// Start our plugin execution threads and handlers
handlerThread.start();
Expand Down Expand Up @@ -230,15 +233,21 @@ private boolean launchIntent(Uri url) {
}
});

SharedPreferences prefs = getContext().getSharedPreferences(com.getcapacitor.plugin.WebView.WEBVIEW_PREFS_NAME, Activity.MODE_PRIVATE);
String path = prefs.getString(com.getcapacitor.plugin.WebView.CAP_SERVER_PATH, null);
if (path != null && !path.isEmpty() && new File(path).exists()) {
setServerBasePath(path);
if (!isDeployDisabled()) {
SharedPreferences prefs = getContext().getSharedPreferences(com.getcapacitor.plugin.WebView.WEBVIEW_PREFS_NAME, Activity.MODE_PRIVATE);
String path = prefs.getString(com.getcapacitor.plugin.WebView.CAP_SERVER_PATH, null);
if (path != null && !path.isEmpty() && new File(path).exists()) {
setServerBasePath(path);
}
}
// Get to work
webView.loadUrl(appUrl);
}

public boolean isDeployDisabled() {
return preferences.getBoolean("DisableDeploy", false);
}


public void handleAppUrlLoadError(Exception ex) {
if (ex instanceof SocketTimeoutException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected void load(Bundle savedInstanceState) {

pluginManager = mockWebView.getPluginManager();
cordovaInterface.onCordovaInit(pluginManager);
bridge = new Bridge(this, webView, initialPlugins, cordovaInterface, pluginManager);
bridge = new Bridge(this, webView, initialPlugins, cordovaInterface, pluginManager, preferences);

Splash.showOnLaunch(this);

Expand Down
14 changes: 6 additions & 8 deletions ios/Capacitor/Capacitor/CAPBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -307,14 +307,12 @@ enum BridgeError: Error {
}

func registerCordovaPlugins() {
let cordovaParser = CDVConfigParser.init();
let configUrl = Bundle.main.url(forResource: "config", withExtension: "xml")
let configParser = XMLParser(contentsOf: configUrl!)!;
configParser.delegate = cordovaParser
configParser.parse()
cordovaPluginManager = CDVPluginManager.init(parser: cordovaParser, viewController: self.viewController, webView: self.getWebView())
if cordovaParser.startupPluginNames.count > 0 {
for pluginName in cordovaParser.startupPluginNames {
guard let bridgeVC = self.viewController as? CAPBridgeViewController else {
return
}
cordovaPluginManager = CDVPluginManager.init(parser: bridgeVC.cordovaParser, viewController: self.viewController, webView: self.getWebView())
if bridgeVC.cordovaParser.startupPluginNames.count > 0 {
for pluginName in bridgeVC.cordovaParser.startupPluginNames {
_ = cordovaPluginManager?.getCommandInstance(pluginName as? String)
}
}
Expand Down
29 changes: 21 additions & 8 deletions ios/Capacitor/Capacitor/CAPBridgeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import UIKit
import WebKit
import Cordova

public class CAPBridgeViewController: UIViewController, CAPBridgeDelegate, WKScriptMessageHandler, WKUIDelegate, WKNavigationDelegate {

Expand All @@ -17,7 +18,7 @@ public class CAPBridgeViewController: UIViewController, CAPBridgeDelegate, WKScr
public var bridgedViewController: UIViewController? {
return self
}

public let cordovaParser = CDVConfigParser.init();
private var hostname: String?
private var allowNavigationConfig: [String]?
private var basePath: String = ""
Expand All @@ -34,6 +35,10 @@ public class CAPBridgeViewController: UIViewController, CAPBridgeDelegate, WKScr
private var handler: CAPAssetHandler?

override public func loadView() {
let configUrl = Bundle.main.url(forResource: "config", withExtension: "xml")
let configParser = XMLParser(contentsOf: configUrl!)!;
configParser.delegate = cordovaParser
configParser.parse()
guard let startPath = self.getStartPath() else {
return
}
Expand Down Expand Up @@ -84,18 +89,26 @@ public class CAPBridgeViewController: UIViewController, CAPBridgeDelegate, WKScr
return nil
}

let defaults = UserDefaults.standard
let persistedPath = defaults.string(forKey: "serverBasePath")
if (persistedPath != nil && !persistedPath!.isEmpty) {
let libPath = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true)[0]
let cordovaDataDirectory = (libPath as NSString).appendingPathComponent("NoCloud")
let snapshots = (cordovaDataDirectory as NSString).appendingPathComponent("ionic_built_snapshots")
startPath = (snapshots as NSString).appendingPathComponent((persistedPath! as NSString).lastPathComponent)
if !isDeployDisabled() {
let defaults = UserDefaults.standard
let persistedPath = defaults.string(forKey: "serverBasePath")
if (persistedPath != nil && !persistedPath!.isEmpty) {
let libPath = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true)[0]
let cordovaDataDirectory = (libPath as NSString).appendingPathComponent("NoCloud")
let snapshots = (cordovaDataDirectory as NSString).appendingPathComponent("ionic_built_snapshots")
startPath = (snapshots as NSString).appendingPathComponent((persistedPath! as NSString).lastPathComponent)
}
}

self.basePath = startPath
return startPath
}

func isDeployDisabled() -> Bool {
let val = cordovaParser.settings.object(forKey: "DisableDeploy".lowercased()) as? NSString
return val?.boolValue ?? false
}

override public func viewDidLoad() {
super.viewDidLoad()
self.becomeFirstResponder()
Expand Down