Skip to content

Commit

Permalink
Merge pull request #700 from moberwasserlechner/feature/android_unify…
Browse files Browse the repository at this point in the history
…_logs

Android: Unify logs
  • Loading branch information
mlynch authored Aug 7, 2018
2 parents d8abec4 + 15b7f2a commit debe27a
Show file tree
Hide file tree
Showing 31 changed files with 170 additions and 182 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@


public class AndroidProtocolHandler {
private static final String TAG = "AndroidProtocolHandler";

private Context context;

Expand Down Expand Up @@ -62,19 +61,12 @@ public InputStream openResource(Uri uri) {
if (valueType == TypedValue.TYPE_STRING) {
return context.getResources().openRawResource(fieldId);
} else {
Log.e(TAG, "Asset not of type string: " + uri);
return null;
Log.e(LogUtils.getCoreTag(), "Asset not of type string: " + uri);
}
} catch (ClassNotFoundException e) {
Log.e(TAG, "Unable to open resource URL: " + uri, e);
return null;
} catch (NoSuchFieldException e) {
Log.e(TAG, "Unable to open resource URL: " + uri, e);
return null;
} catch (IllegalAccessException e) {
Log.e(TAG, "Unable to open resource URL: " + uri, e);
return null;
} catch (ClassNotFoundException | IllegalAccessException | NoSuchFieldException e) {
Log.e(LogUtils.getCoreTag(), "Unable to open resource URL: " + uri, e);
}
return null;
}

private static int getFieldId(Context context, String assetType, String assetName)
Expand Down
58 changes: 22 additions & 36 deletions android/capacitor/src/main/java/com/getcapacitor/Bridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.ValueCallback;
import android.webkit.WebResourceRequest;
Expand Down Expand Up @@ -70,14 +69,14 @@
* BridgeActivity.java</a>
*/
public class Bridge {

private static final String LOG_TAG = LogUtils.getCoreTag();
private static final String PREFS_NAME = "CapacitorSettings";
private static final String BUNDLE_LAST_PLUGIN_ID_KEY = "capacitorLastActivityPluginId";
private static final String BUNDLE_LAST_PLUGIN_CALL_METHOD_NAME_KEY = "capacitorLastActivityPluginMethod";
private static final String BUNDLE_PLUGIN_CALL_OPTIONS_SAVED_KEY = "capacitorLastPluginCallOptions";
private static final String BUNDLE_PLUGIN_CALL_BUNDLE_KEY = "capacitorLastPluginCallBundle";

public static final String TAG = "Capacitor";

// The name of the directory we use to look for index.html and the rest of our web assets
public static final String DEFAULT_WEB_ASSET_DIR = "public";

Expand Down Expand Up @@ -178,7 +177,7 @@ private void loadWebView() {

final String appUrl = appUrlConfig == null ? url.replace("%3A", ":") : appUrlConfig;

log("Loading app at " + appUrl);
Log.d(LOG_TAG, "Loading app at " + appUrl);

webView.setWebChromeClient(new BridgeWebChromeClient(this));
webView.setWebViewClient(new WebViewClient() {
Expand Down Expand Up @@ -242,7 +241,7 @@ public int getRandomPort(){
public void handleAppUrlLoadError(Exception ex) {
if (ex instanceof SocketTimeoutException) {
Toast.show(getContext(), "Unable to load app. Are you sure the server is running at " + localServer.getAuthority() + "?");
Log.e(TAG, "Unable to load app. Ensure the server is running at " + localServer.getAuthority() + ", or modify the " +
Log.e(LOG_TAG, "Unable to load app. Ensure the server is running at " + localServer.getAuthority() + ", or modify the " +
"appUrl setting in capacitor.config.json (make sure to npx cap copy after to commit changes).", ex);
}
}
Expand Down Expand Up @@ -284,16 +283,16 @@ public Uri getIntentUri() {

/*
public void registerPlugins() {
Log.d(TAG, "Finding plugins");
Log.d(LOG_TAG, "Finding plugins");
try {
Enumeration<URL> roots = getClass().getClassLoader().getResources("");
while (roots.hasMoreElements()) {
URL url = roots.nextElement();
Log.d(TAG, "CLASSAPTH ROOT: " + url.getPath());
Log.d(LOG_TAG, "CLASSAPTH ROOT: " + url.getPath());
//File root = new File(url.getPath());
}
} catch(Exception ex) {
Log.e(TAG, "Unable to query for plugin classes", ex);
Log.e(LOG_TAG, "Unable to query for plugin classes", ex);
}
}
*/
Expand Down Expand Up @@ -368,7 +367,7 @@ public void registerPlugin(Class<? extends Plugin> pluginClass) {
NativePlugin pluginAnnotation = pluginClass.getAnnotation(NativePlugin.class);

if (pluginAnnotation == null) {
Log.e(Bridge.TAG, "NativePlugin doesn't have the @NativePlugin annotation. Please add it");
Log.e(LOG_TAG, "NativePlugin doesn't have the @NativePlugin annotation. Please add it");
return;
}

Expand All @@ -379,16 +378,16 @@ public void registerPlugin(Class<? extends Plugin> pluginClass) {
pluginId = pluginAnnotation.name();
}

Log.d(Bridge.TAG, "Registering plugin: " + pluginId);
Log.d(LOG_TAG, "Registering plugin: " + pluginId);

try {
this.plugins.put(pluginId, new PluginHandle(this, pluginClass));
} catch (InvalidPluginException ex) {
Log.e(TAG, "NativePlugin " + pluginClass.getName() +
Log.e(LOG_TAG, "NativePlugin " + pluginClass.getName() +
" is invalid. Ensure the @NativePlugin annotation exists on the plugin class and" +
" the class extends Plugin");
} catch (PluginLoadException ex) {
Log.e(TAG, "NativePlugin " + pluginClass.getName() + " failed to load", ex);
Log.e(LOG_TAG, "NativePlugin " + pluginClass.getName() + " failed to load", ex);
}
}

Expand Down Expand Up @@ -435,12 +434,12 @@ public void callPluginMethod(String pluginId, final String methodName, final Plu
final PluginHandle plugin = this.getPlugin(pluginId);

if (plugin == null) {
Log.e(Bridge.TAG, "unable to find plugin : " + pluginId);
Log.e(LOG_TAG, "unable to find plugin : " + pluginId);
call.errorCallback("unable to find plugin : " + pluginId);
return;
}

Log.d(Bridge.TAG, "callback: " + call.getCallbackId() +
Log.v(LOG_TAG, "callback: " + call.getCallbackId() +
", pluginId: " + plugin.getId() +
", methodName: " + methodName + ", methodData: " + call.getData().toString());

Expand All @@ -454,9 +453,9 @@ public void run() {
saveCall(call);
}
} catch(PluginLoadException | InvalidPluginMethodException | PluginInvocationException ex) {
Log.e(Bridge.TAG, "Unable to execute plugin method", ex);
Log.e(LOG_TAG, "Unable to execute plugin method", ex);
} catch(Exception ex) {
Log.e(Bridge.TAG, "Serious error executing plugin", ex);
Log.e(LOG_TAG, "Serious error executing plugin", ex);
}
}
};
Expand All @@ -469,19 +468,6 @@ public void run() {
}
}

public void error(String... args) {
Log.e(TAG, "⚡️ " + TextUtils.join(" ", args));
}

public void fatalError(String... args) {
Log.e(TAG, "⚡️ FATAL ERROR ⚡️");
error(args);
}

public void log(String... args) {
Log.d(TAG, TextUtils.join(" ", args));
}

/**
* Evaluate JavaScript in the web view. This method
* executes on the main thread automatically.
Expand Down Expand Up @@ -589,7 +575,7 @@ private JSInjector getJSInjector() {

return new JSInjector(globalJS, coreJS, pluginJS, cordovaJS, cordovaPluginsJS, cordovaPluginsFileJS);
} catch(JSExportException ex) {
Log.e(TAG, "Unable to export Capacitor JS. App will not function!", ex);
Log.e(LOG_TAG, "Unable to export Capacitor JS. App will not function!", ex);
}
return null;
}
Expand Down Expand Up @@ -620,7 +606,7 @@ protected void restoreInstanceState(Bundle savedInstanceState) {
lastPluginId, PluginCall.CALLBACK_ID_DANGLING, lastPluginCallMethod, options);

} catch (JSONException ex) {
Log.e(TAG, "Unable to restore plugin call, unable to parse persisted JSON object", ex);
Log.e(LOG_TAG, "Unable to restore plugin call, unable to parse persisted JSON object", ex);
}
}

Expand All @@ -634,7 +620,7 @@ protected void restoreInstanceState(Bundle savedInstanceState) {
}

public void saveInstanceState(Bundle outState) {
Log.d(TAG, "Saving instance state!");
Log.d(LOG_TAG, "Saving instance state!");

// If there was a last PluginCall for a started activity, we need to
// persist it so we can load it again in case our app gets terminated
Expand All @@ -652,7 +638,7 @@ public void saveInstanceState(Bundle outState) {
}

public void startActivityForPluginWithResult(PluginCall call, Intent intent, int requestCode) {
Log.d(TAG, "Starting activity for result");
Log.d(LOG_TAG, "Starting activity for result");

pluginCallForLastActivity = call;

Expand All @@ -671,11 +657,11 @@ public void onRequestPermissionsResult(int requestCode, String[] permissions, in
PluginHandle plugin = getPluginWithRequestCode(requestCode);

if (plugin == null) {
Log.d(Bridge.TAG, "Unable to find a Capacitor plugin to handle requestCode, try with Cordova plugins " + requestCode);
Log.d(LOG_TAG, "Unable to find a Capacitor plugin to handle requestCode, try with Cordova plugins " + requestCode);
try {
cordovaInterface.onRequestPermissionResult(requestCode, permissions, grantResults);
} catch (JSONException e) {
Log.d(Bridge.TAG, "Error on Cordova plugin permissions request " + e.getMessage());
Log.d(LOG_TAG, "Error on Cordova plugin permissions request " + e.getMessage());
}
return;
}
Expand All @@ -694,7 +680,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
PluginHandle plugin = getPluginWithRequestCode(requestCode);

if (plugin == null || plugin.getInstance() == null) {
Log.d(Bridge.TAG, "Unable to find a Capacitor plugin to handle requestCode, trying Cordova plugins " + requestCode);
Log.d(LOG_TAG, "Unable to find a Capacitor plugin to handle requestCode, trying Cordova plugins " + requestCode);
cordovaInterface.onActivityResult(requestCode, resultCode, data);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.webkit.WebView;

import com.getcapacitor.android.R;
import com.getcapacitor.cordova.MockCordovaWebViewImpl;
import com.getcapacitor.plugin.App;

import org.apache.cordova.ConfigXmlParser;
import org.apache.cordova.CordovaInterfaceImpl;
import org.apache.cordova.CordovaPreferences;
Expand Down Expand Up @@ -59,7 +57,7 @@ protected void init(Bundle savedInstanceState, List<Class<? extends Plugin>> plu
* Load the WebView and create the Bridge
*/
protected void load(Bundle savedInstanceState) {
Log.d(Bridge.TAG, "Starting BridgeActivity");
Log.d(LogUtils.getCoreTag(), "Starting BridgeActivity");

webView = findViewById(R.id.webview);
cordovaInterface = new CordovaInterfaceImpl(this);
Expand Down Expand Up @@ -115,14 +113,14 @@ public void onStart() {

this.bridge.onStart();

Log.d(Bridge.TAG, "App started");
Log.d(LogUtils.getCoreTag(), "App started");
}

@Override
public void onRestart() {
super.onRestart();
this.bridge.onRestart();
Log.d(Bridge.TAG, "App restarted");
Log.d(LogUtils.getCoreTag(), "App restarted");
}

@Override
Expand All @@ -133,7 +131,7 @@ public void onResume() {

this.bridge.onResume();

Log.d(Bridge.TAG, "App resumed");
Log.d(LogUtils.getCoreTag(), "App resumed");
}

@Override
Expand All @@ -142,7 +140,7 @@ public void onPause() {

this.bridge.onPause();

Log.d(Bridge.TAG, "App paused");
Log.d(LogUtils.getCoreTag(), "App paused");
}

@Override
Expand All @@ -156,13 +154,13 @@ public void onStop() {

this.bridge.onStop();

Log.d(Bridge.TAG, "App stopped");
Log.d(LogUtils.getCoreTag(), "App stopped");
}

@Override
public void onDestroy() {
super.onDestroy();
Log.d(Bridge.TAG, "App destroyed");
Log.d(LogUtils.getCoreTag(), "App destroyed");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public void onResult(boolean value, boolean didCancel, String inputValue) {
@Override
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
super.onGeolocationPermissionsShowPrompt(origin, callback);
Log.d(Bridge.TAG, "onGeolocationPermissionsShowPrompt: DOING IT HERE FOR ORIGIN: " + origin);
Log.d(LogUtils.getCoreTag(), "onGeolocationPermissionsShowPrompt: DOING IT HERE FOR ORIGIN: " + origin);

// Set that we want geolocation perms for this origin
callback.invoke(origin, true, false);
Expand All @@ -129,7 +129,7 @@ public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermiss
if (!geo.hasRequiredPermissions()) {
geo.pluginRequestAllPermissions();
} else {
Log.d(bridge.TAG, "onGeolocationPermissionsShowPrompt: has required permis");
Log.d(LogUtils.getCoreTag(), "onGeolocationPermissionsShowPrompt: has required permis");
}
}

Expand Down
5 changes: 3 additions & 2 deletions android/capacitor/src/main/java/com/getcapacitor/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* Management interface for accessing values in capacitor.config.json
*/
public class Config {

private JSONObject config = new JSONObject();

private static Config instance;
Expand Down Expand Up @@ -46,9 +47,9 @@ private void loadConfig(Activity activity) {
String jsonString = b.toString();
this.config = new JSONObject(jsonString);
} catch (IOException ex) {
Log.e(Bridge.TAG, "Unable to load capacitor.config.json. Run npx cap copy first", ex);
Log.e(LogUtils.getCoreTag(), "Unable to load capacitor.config.json. Run npx cap copy first", ex);
} catch (JSONException ex) {
Log.e(Bridge.TAG, "Unable to parse capacitor.config.json. Make sure it's valid json", ex);
Log.e(LogUtils.getCoreTag(), "Unable to parse capacitor.config.json. Make sure it's valid json", ex);
} finally {
if (reader != null) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static String getCordovaJS(Context context) {
try {
fileContent = getJS(context, "public/cordova.js");
} catch(IOException ex) {
Log.e(Bridge.TAG, "Unable to read public/cordova.js file, Cordova plugins will not work");
Log.e(LogUtils.getCoreTag(), "Unable to read public/cordova.js file, Cordova plugins will not work");
}
return fileContent;
}
Expand All @@ -58,7 +58,7 @@ public static String getCordovaPluginsFileJS(Context context) {
try {
fileContent = getJS(context, "public/cordova_plugins.js");
} catch(IOException ex) {
Log.e(Bridge.TAG, "Unable to read public/cordova_plugins.js file, Cordova plugins will not work");
Log.e(LogUtils.getCoreTag(), "Unable to read public/cordova_plugins.js file, Cordova plugins will not work");
}
return fileContent;
}
Expand Down Expand Up @@ -112,7 +112,7 @@ public static String getFilesContent(Context context, String path) {
return getJS(context, path);
}
} catch(IOException ex) {
Log.e(Bridge.TAG, "Unable to read file at path "+path);
Log.e(LogUtils.getCoreTag(), "Unable to read file at path " + path);
}
return builder.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public InputStream getInjectedStream(InputStream responseStream) {
InputStream jsInputStream = new ByteArrayInputStream(js.getBytes(StandardCharsets.UTF_8.name()));
return new SequenceInputStream(jsInputStream, responseStream);
} catch(UnsupportedEncodingException ex) {
Log.e(Bridge.TAG, "Unable to get encoding! Serious internal error, please file an issue", ex);
Log.e(LogUtils.getCoreTag(), "Unable to get encoding! Serious internal error, please file an issue", ex);
}
return null;
}
Expand Down
Loading

0 comments on commit debe27a

Please sign in to comment.