Skip to content

Commit

Permalink
feat(Android): Refactoring configuration (#3778)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Imhoff <[email protected]>
  • Loading branch information
carlpoole and imhoffd authored Dec 15, 2020
1 parent 3746404 commit 9820a30
Show file tree
Hide file tree
Showing 19 changed files with 1,048 additions and 139 deletions.
2 changes: 1 addition & 1 deletion android/capacitor/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ dependencies {
androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
implementation "org.apache.cordova:framework:$cordovaAndroidVersion"
testImplementation 'org.json:json:20140107'
testImplementation 'org.mockito:mockito-inline:2.25.1'
testImplementation 'org.mockito:mockito-inline:3.6.28'
}

34 changes: 15 additions & 19 deletions android/capacitor/src/main/java/com/getcapacitor/Bridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import org.apache.cordova.PluginEntry;
import org.apache.cordova.PluginManager;
import org.json.JSONException;
import org.json.JSONObject;

/**
* The Bridge class is the main engine of Capacitor. It manages
Expand Down Expand Up @@ -137,7 +136,7 @@ public Bridge(
MockCordovaInterfaceImpl cordovaInterface,
PluginManager pluginManager,
CordovaPreferences preferences,
JSONObject config
CapConfig config
) {
this.app = new App();
this.context = context;
Expand All @@ -151,7 +150,7 @@ public Bridge(
handlerThread.start();
taskHandler = new Handler(handlerThread.getLooper());

this.config = new CapConfig(getActivity().getAssets(), config);
this.config = config != null ? config : CapConfig.fromFile(getActivity());
Logger.init(this.config);

// Initialize web view and message handler for it
Expand All @@ -174,7 +173,7 @@ public App getApp() {

private void loadWebView() {
appUrlConfig = this.getServerUrl();
String[] appAllowNavigationConfig = this.config.getArray("server.allowNavigation");
String[] appAllowNavigationConfig = this.config.getAllowNavigation();

ArrayList<String> authorities = new ArrayList<>();
if (appAllowNavigationConfig != null) {
Expand Down Expand Up @@ -204,7 +203,7 @@ private void loadWebView() {
}
}

final boolean html5mode = this.config.getBoolean("server.html5mode", true);
final boolean html5mode = this.config.isHTML5Mode();

// Start the local web server
localServer = new WebViewLocalServer(context, this, getJSInjector(), authorities, html5mode);
Expand Down Expand Up @@ -345,23 +344,23 @@ public Uri getIntentUri() {
* @return
*/
public String getScheme() {
return this.config.getString("server.androidScheme", CAPACITOR_HTTP_SCHEME);
return this.config.getAndroidScheme();
}

/**
* Get host name that is used to serve content
* @return
*/
public String getHost() {
return this.config.getString("server.hostname", "localhost");
return this.config.getHostname();
}

/**
* Get the server url that is used to serve content
* @return
*/
public String getServerUrl() {
return this.config.getString("server.url");
return this.config.getServerUrl();
}

public CapConfig getConfig() {
Expand All @@ -384,34 +383,31 @@ private void initWebView() {
settings.setAppCacheEnabled(true);
settings.setMediaPlaybackRequiresUserGesture(false);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
if (this.config.getBoolean("android.allowMixedContent", false)) {
if (this.config.isMixedContentAllowed()) {
settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}

String appendUserAgent = this.config.getString("android.appendUserAgent", this.config.getString("appendUserAgent", null));
String appendUserAgent = this.config.getAppendedUserAgentString();
if (appendUserAgent != null) {
String defaultUserAgent = settings.getUserAgentString();
settings.setUserAgentString(defaultUserAgent + " " + appendUserAgent);
}
String overrideUserAgent = this.config.getString("android.overrideUserAgent", this.config.getString("overrideUserAgent", null));
String overrideUserAgent = this.config.getOverriddenUserAgentString();
if (overrideUserAgent != null) {
settings.setUserAgentString(overrideUserAgent);
}

String backgroundColor = this.config.getString("android.backgroundColor", this.config.getString("backgroundColor", null));
String backgroundColor = this.config.getBackgroundColor();
try {
if (backgroundColor != null) {
webView.setBackgroundColor(WebColor.parseColor(backgroundColor));
}
} catch (IllegalArgumentException ex) {
Logger.debug("WebView background color not applied");
}
boolean defaultDebuggable = false;
if (isDevMode()) {
defaultDebuggable = true;
}

webView.requestFocusFromTouch();
WebView.setWebContentsDebuggingEnabled(this.config.getBoolean("android.webContentsDebuggingEnabled", defaultDebuggable));
WebView.setWebContentsDebuggingEnabled(this.config.isWebContentsDebuggingEnabled());
}

/**
Expand Down Expand Up @@ -1119,7 +1115,7 @@ public void setWebViewClient(BridgeWebViewClient client) {
public static class Builder {

private Bundle instanceState = null;
private JSONObject config = new JSONObject();
private CapConfig config = null;
private List<Class<? extends Plugin>> plugins = new ArrayList<>();
private AppCompatActivity activity = null;
private Context context = null;
Expand All @@ -1137,7 +1133,7 @@ public Builder setInstanceState(Bundle instanceState) {
return this;
}

public Builder setConfig(JSONObject config) {
public Builder setConfig(CapConfig config) {
this.config = config;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
import com.getcapacitor.android.R;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONObject;

public class BridgeActivity extends AppCompatActivity {

protected Bridge bridge;
protected boolean keepRunning = true;
private JSONObject config;
private CapConfig config;

private int activityDepth = 0;
private List<Class<? extends Plugin>> initialPlugins = new ArrayList<>();
private final Bridge.Builder bridgeBuilder = new Bridge.Builder();
Expand All @@ -25,22 +25,29 @@ protected void onCreate(Bundle savedInstanceState) {
}

/**
* Initializes the Capacitor Bridge with the Activity.
* @deprecated It is preferred not to call this method. If it is not called, the bridge is
* initialized automatically. If you need to add additional plugins during initialization,
* use {@link #registerPlugin(Class)} or {@link #registerPlugins(List)}.
*
* @param plugins A list of plugins to initialize with Capacitor
*/
@Deprecated
protected void init(Bundle savedInstanceState, List<Class<? extends Plugin>> plugins) {
this.init(savedInstanceState, plugins, null);
}

/**
* Initializes the Capacitor Bridge with the Activity.
* @deprecated It is preferred not to call this method. If it is not called, the bridge is
* initialized automatically. If you need to add additional plugins during initialization,
* use {@link #registerPlugin(Class)} or {@link #registerPlugins(List)}.
*
* @param plugins A list of plugins to initialize with Capacitor
* @param config An instance of a Capacitor Configuration to use. If null, will load from file
*/
@Deprecated
protected void init(Bundle savedInstanceState, List<Class<? extends Plugin>> plugins, JSONObject config) {
protected void init(Bundle savedInstanceState, List<Class<? extends Plugin>> plugins, CapConfig config) {
this.initialPlugins = plugins;
this.config = config;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class BridgeFragment extends Fragment {
protected boolean keepRunning = true;

private final List<Class<? extends Plugin>> initialPlugins = new ArrayList<>();
private CapConfig config = null;

public BridgeFragment() {
// Required empty public constructor
Expand All @@ -50,6 +51,10 @@ public void addPlugin(Class<? extends Plugin> plugin) {
this.initialPlugins.add(plugin);
}

public void setConfig(CapConfig config) {
this.config = config;
}

/**
* Load the WebView and create the Bridge
*/
Expand All @@ -68,6 +73,7 @@ protected void load(Bundle savedInstanceState) {
.setActivity((AppCompatActivity) getActivity())
.setInstanceState(savedInstanceState)
.setPlugins(initialPlugins)
.setConfig(config)
.create();

if (startDir != null) {
Expand Down
Loading

0 comments on commit 9820a30

Please sign in to comment.