Skip to content

Commit

Permalink
Cordova-Android should use org.apache.cordova.LOG for logging
Browse files Browse the repository at this point in the history
* Read LogLevel preference from config.xml
* Replace Log with LOG
* Add addition warning method to LOG
  • Loading branch information
macdonst committed Aug 22, 2016
1 parent 8fbb6d7 commit 4a0a7bc
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 95 deletions.
12 changes: 5 additions & 7 deletions framework/src/org/apache/cordova/CallbackContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ Licensed to the Apache Software Foundation (ASF) under one

import org.json.JSONArray;

import android.util.Log;

import org.apache.cordova.CordovaWebView;
import org.apache.cordova.PluginResult;
import org.json.JSONObject;
Expand All @@ -38,23 +36,23 @@ public CallbackContext(String callbackId, CordovaWebView webView) {
this.callbackId = callbackId;
this.webView = webView;
}

public boolean isFinished() {
return finished;
}

public boolean isChangingThreads() {
return changingThreads > 0;
}

public String getCallbackId() {
return callbackId;
}

public void sendPluginResult(PluginResult pluginResult) {
synchronized (this) {
if (finished) {
Log.w(LOG_TAG, "Attempted to send a second callback for ID: " + callbackId + "\nResult was: " + pluginResult.getMessage());
LOG.w(LOG_TAG, "Attempted to send a second callback for ID: " + callbackId + "\nResult was: " + pluginResult.getMessage());
return;
} else {
finished = !pluginResult.getKeepCallback();
Expand Down Expand Up @@ -98,7 +96,7 @@ public void success(JSONArray message) {
public void success(byte[] message) {
sendPluginResult(new PluginResult(PluginResult.Status.OK, message));
}

/**
* Helper for success callbacks that just returns the Status.OK by default
*
Expand Down
3 changes: 1 addition & 2 deletions framework/src/org/apache/cordova/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ Licensed to the Apache Software Foundation (ASF) under one
import java.util.List;

import android.app.Activity;
import android.util.Log;

@Deprecated // Use Whitelist, CordovaPrefences, etc. directly.
public class Config {
Expand Down Expand Up @@ -61,7 +60,7 @@ public static String getErrorUrl() {
public static List<PluginEntry> getPluginEntries() {
return parser.getPluginEntries();
}

public static CordovaPreferences getPreferences() {
return parser.getPreferences();
}
Expand Down
17 changes: 10 additions & 7 deletions framework/src/org/apache/cordova/CordovaActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ Licensed to the Apache Software Foundation (ASF) under one
import android.media.AudioManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
Expand All @@ -50,7 +49,7 @@ Licensed to the Apache Software Foundation (ASF) under one
* html file that contains the application.
*
* As an example:
*
*
* <pre>
* package org.apache.cordova.examples;
*
Expand All @@ -67,8 +66,8 @@ Licensed to the Apache Software Foundation (ASF) under one
* }
* }
* </pre>
*
* Cordova xml configuration: Cordova uses a configuration file at
*
* Cordova xml configuration: Cordova uses a configuration file at
* res/xml/config.xml to specify its settings. See "The config.xml File"
* guide in cordova-docs at http://cordova.apache.org/docs for the documentation
* for the configuration. The use of the set*Property() methods is
Expand Down Expand Up @@ -104,17 +103,21 @@ public class CordovaActivity extends Activity {
*/
@Override
public void onCreate(Bundle savedInstanceState) {
// need to activate preferences before super.onCreate to avoid "requestFeature() must be called before adding content" exception
loadConfig();

String logLevel = preferences.getString("loglevel", "ERROR");
LOG.setLogLevel(logLevel);

LOG.i(TAG, "Apache Cordova native platform version " + CordovaWebView.CORDOVA_VERSION + " is starting");
LOG.d(TAG, "CordovaActivity.onCreate()");

// need to activate preferences before super.onCreate to avoid "requestFeature() must be called before adding content" exception
loadConfig();
if (!preferences.getBoolean("ShowTitle", false)) {
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
}

if (preferences.getBoolean("SetFullscreen", false)) {
Log.d(TAG, "The SetFullscreen configuration is deprecated in favor of Fullscreen, and will be removed in a future version.");
LOG.d(TAG, "The SetFullscreen configuration is deprecated in favor of Fullscreen, and will be removed in a future version.");
preferences.set("Fullscreen", true);
}
if (preferences.getBoolean("Fullscreen", false)) {
Expand Down
16 changes: 7 additions & 9 deletions framework/src/org/apache/cordova/CordovaBridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ Licensed to the Apache Software Foundation (ASF) under one
import org.json.JSONArray;
import org.json.JSONException;

import android.util.Log;

/**
* Contains APIs that the JS can call. All functions in here should also have
* an equivalent entry in CordovaChromeClient.java, and be added to
Expand Down Expand Up @@ -87,15 +85,15 @@ public String jsRetrieveJsMessages(int bridgeSecret, boolean fromOnlineEvent) th
private boolean verifySecret(String action, int bridgeSecret) throws IllegalAccessException {
if (!jsMessageQueue.isBridgeEnabled()) {
if (bridgeSecret == -1) {
Log.d(LOG_TAG, action + " call made before bridge was enabled.");
LOG.d(LOG_TAG, action + " call made before bridge was enabled.");
} else {
Log.d(LOG_TAG, "Ignoring " + action + " from previous page load.");
LOG.d(LOG_TAG, "Ignoring " + action + " from previous page load.");
}
return false;
}
// Bridge secret wrong and bridge not due to it being from the previous page.
if (expectedBridgeSecret < 0 || bridgeSecret != expectedBridgeSecret) {
Log.e(LOG_TAG, "Bridge access attempt with wrong secret token, possibly from malicious code. Disabling exec() bridge!");
LOG.e(LOG_TAG, "Bridge access attempt with wrong secret token, possibly from malicious code. Disabling exec() bridge!");
clearBridgeSecret();
throw new IllegalAccessException();
}
Expand All @@ -120,7 +118,7 @@ int generateBridgeSecret() {

public void reset() {
jsMessageQueue.reset();
clearBridgeSecret();
clearBridgeSecret();
}

public String promptOnJsPrompt(String origin, String message, String defaultValue) {
Expand All @@ -141,7 +139,7 @@ public String promptOnJsPrompt(String origin, String message, String defaultValu
}
return "";
}
// Sets the native->JS bridge mode.
// Sets the native->JS bridge mode.
else if (defaultValue != null && defaultValue.startsWith("gap_bridge_mode:")) {
try {
int bridgeSecret = Integer.parseInt(defaultValue.substring(16));
Expand All @@ -153,7 +151,7 @@ else if (defaultValue != null && defaultValue.startsWith("gap_bridge_mode:")) {
}
return "";
}
// Polling for JavaScript messages
// Polling for JavaScript messages
else if (defaultValue != null && defaultValue.startsWith("gap_poll:")) {
int bridgeSecret = Integer.parseInt(defaultValue.substring(9));
try {
Expand All @@ -175,7 +173,7 @@ else if (defaultValue != null && defaultValue.startsWith("gap_init:")) {
int secret = generateBridgeSecret();
return ""+secret;
} else {
Log.e(LOG_TAG, "gap_init called from restricted origin: " + origin);
LOG.e(LOG_TAG, "gap_init called from restricted origin: " + origin);
}
return "";
}
Expand Down
5 changes: 2 additions & 3 deletions framework/src/org/apache/cordova/CordovaInterfaceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ Licensed to the Apache Software Foundation (ASF) under one
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.util.Pair;

import org.json.JSONException;
Expand Down Expand Up @@ -147,13 +146,13 @@ public boolean onActivityResult(int requestCode, int resultCode, Intent intent)
activityResultCallback = null;

if (callback != null) {
Log.d(TAG, "Sending activity result to plugin");
LOG.d(TAG, "Sending activity result to plugin");
initCallbackService = null;
savedResult = null;
callback.onActivityResult(requestCode, resultCode, intent);
return true;
}
Log.w(TAG, "Got an activity result, but no plugin was registered to receive it" + (savedResult != null ? " yet!" : "."));
LOG.w(TAG, "Got an activity result, but no plugin was registered to receive it" + (savedResult != null ? " yet!" : "."));
return false;
}

Expand Down
5 changes: 2 additions & 3 deletions framework/src/org/apache/cordova/CordovaWebViewImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ Licensed to the Apache Software Foundation (ASF) under one
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
Expand Down Expand Up @@ -245,7 +244,7 @@ public void showWebPage(String url, boolean openExternal, boolean clearHistory,
@Deprecated
public void showCustomView(View view, WebChromeClient.CustomViewCallback callback) {
// This code is adapted from the original Android Browser code, licensed under the Apache License, Version 2.0
Log.d(TAG, "showing Custom View");
LOG.d(TAG, "showing Custom View");
// if a view already exists then immediately terminate the new one
if (mCustomView != null) {
callback.onCustomViewHidden();
Expand Down Expand Up @@ -276,7 +275,7 @@ public void showCustomView(View view, WebChromeClient.CustomViewCallback callbac
public void hideCustomView() {
// This code is adapted from the original Android Browser code, licensed under the Apache License, Version 2.0
if (mCustomView == null) return;
Log.d(TAG, "Hiding Custom View");
LOG.d(TAG, "Hiding Custom View");

// Hide the custom view.
mCustomView.setVisibility(View.GONE);
Expand Down
10 changes: 10 additions & 0 deletions framework/src/org/apache/cordova/LOG.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,16 @@ public static void i(String tag, String s, Throwable e) {
if (LOG.INFO >= LOGLEVEL) Log.i(tag, s, e);
}

/**
* Warning log message.
*
* @param tag
* @param e
*/
public static void w(String tag, Throwable e) {
if (LOG.WARN >= LOGLEVEL) Log.w(tag, e);
}

/**
* Warning log message.
*
Expand Down
Loading

0 comments on commit 4a0a7bc

Please # to comment.