Skip to content

Commit

Permalink
feat(android): hideLogs feature (#2839)
Browse files Browse the repository at this point in the history
  • Loading branch information
imhoffd authored May 4, 2020
1 parent 471ed0c commit d60757a
Show file tree
Hide file tree
Showing 38 changed files with 244 additions and 206 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import android.content.Context;
import android.content.res.AssetManager;
import android.net.Uri;
import android.util.Log;
import android.util.TypedValue;

import java.io.File;
Expand Down Expand Up @@ -51,10 +50,10 @@ public InputStream openResource(Uri uri) {
if (valueType == TypedValue.TYPE_STRING) {
return context.getResources().openRawResource(fieldId);
} else {
Log.e(LogUtils.getCoreTag(), "Asset not of type string: " + uri);
Logger.error("Asset not of type string: " + uri);
}
} catch (ClassNotFoundException | IllegalAccessException | NoSuchFieldException e) {
Log.e(LogUtils.getCoreTag(), "Unable to open resource URL: " + uri, e);
Logger.error("Unable to open resource URL: " + uri, e);
}
return null;
}
Expand Down Expand Up @@ -86,7 +85,7 @@ public InputStream openContentUrl(Uri uri) throws IOException {
try {
stream = context.getContentResolver().openInputStream(Uri.parse(realPath));
} catch (SecurityException e) {
Log.e(LogUtils.getCoreTag(), "Unable to open content URL: " + uri, e);
Logger.error("Unable to open content URL: " + uri, e);
}
return stream;
}
Expand Down
58 changes: 20 additions & 38 deletions android/capacitor/src/main/java/com/getcapacitor/Bridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.Log;
import android.webkit.ValueCallback;
import android.webkit.WebSettings;
import android.webkit.WebView;
Expand Down Expand Up @@ -75,7 +74,6 @@
*/
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";
Expand Down Expand Up @@ -209,7 +207,7 @@ private void loadWebView() {
localServer = new WebViewLocalServer(context, this, getJSInjector(), authorities, html5mode);
localServer.hostAssets(DEFAULT_WEB_ASSET_DIR);

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

webView.setWebChromeClient(new BridgeWebChromeClient(this));
webView.setWebViewClient(this.webViewClient);
Expand Down Expand Up @@ -251,7 +249,7 @@ private boolean isNewBinary() {
versionCode = Integer.toString(pInfo.versionCode);
versionName = pInfo.versionName;
} catch(Exception ex) {
Log.e(LOG_TAG, "Unable to get package info", ex);
Logger.error("Unable to get package info", ex);
}

if (!versionCode.equals(lastVersionCode) || !versionName.equals(lastVersionName)) {
Expand All @@ -275,7 +273,7 @@ public void handleAppUrlLoadError(Exception ex) {
if (BuildConfig.DEBUG) {
Toast.show(getContext(), "Unable to load app. Are you sure the server is running at " + appUrl + "?");
}
Log.e(LOG_TAG, "Unable to load app. Ensure the server is running at " + appUrl + ", or modify the " +
Logger.error("Unable to load app. Ensure the server is running at " + appUrl + ", or modify the " +
"appUrl setting in capacitor.config.json (make sure to npx cap copy after to commit changes).", ex);
}
}
Expand Down Expand Up @@ -323,22 +321,6 @@ public String getScheme() {
return Config.getString("server.androidScheme", CAPACITOR_HTTP_SCHEME);
}

/*
public void registerPlugins() {
Log.d(LOG_TAG, "Finding plugins");
try {
Enumeration<URL> roots = getClass().getClassLoader().getResources("");
while (roots.hasMoreElements()) {
URL url = roots.nextElement();
Log.d(LOG_TAG, "CLASSAPTH ROOT: " + url.getPath());
//File root = new File(url.getPath());
}
} catch(Exception ex) {
Log.e(LOG_TAG, "Unable to query for plugin classes", ex);
}
}
*/

public void reset() {
savedCalls = new HashMap<>();
}
Expand Down Expand Up @@ -376,7 +358,7 @@ private void initWebView() {
webView.setBackgroundColor(Color.parseColor(backgroundColor));
}
} catch (IllegalArgumentException ex) {
Log.d(LogUtils.getCoreTag(), "WebView background color not applied");
Logger.debug("WebView background color not applied");
}
boolean defaultDebuggable = false;
if (isDevMode()) {
Expand Down Expand Up @@ -437,7 +419,7 @@ public void registerPlugin(Class<? extends Plugin> pluginClass) {
NativePlugin pluginAnnotation = pluginClass.getAnnotation(NativePlugin.class);

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

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

Log.d(LOG_TAG, "Registering plugin: " + pluginId);
Logger.debug("Registering plugin: " + pluginId);

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

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

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

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

Expand All @@ -523,9 +505,9 @@ public void run() {
saveCall(call);
}
} catch(PluginLoadException | InvalidPluginMethodException ex) {
Log.e(LOG_TAG, "Unable to execute plugin method", ex);
Logger.error("Unable to execute plugin method", ex);
} catch (Exception ex) {
Log.e(LOG_TAG, "Serious error executing plugin", ex);
Logger.error("Serious error executing plugin", ex);
throw new RuntimeException(ex);
}
}
Expand All @@ -534,7 +516,7 @@ public void run() {
taskHandler.post(currentThreadTask);

} catch (Exception ex) {
Log.e("callPluginMethod", "error : " + ex);
Logger.error(Logger.tags("callPluginMethod"), "error : " + ex, null);
call.errorCallback(ex.toString());
}
}
Expand Down Expand Up @@ -647,7 +629,7 @@ private JSInjector getJSInjector() {

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

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

Expand All @@ -692,7 +674,7 @@ public void restoreInstanceState(Bundle savedInstanceState) {
}

public void saveInstanceState(Bundle outState) {
Log.d(LOG_TAG, "Saving instance state!");
Logger.debug("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 @@ -710,7 +692,7 @@ public void saveInstanceState(Bundle outState) {
}

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

pluginCallForLastActivity = call;

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

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

if (plugin == null || plugin.getInstance() == null) {
Log.d(LOG_TAG, "Unable to find a Capacitor plugin to handle requestCode, trying Cordova plugins " + requestCode);
Logger.debug("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 @@ -4,7 +4,6 @@
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;

import androidx.appcompat.app.AppCompatActivity;
Expand Down Expand Up @@ -62,7 +61,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(LogUtils.getCoreTag(), "Starting BridgeActivity");
Logger.debug("Starting BridgeActivity");

webView = findViewById(R.id.webview);

Expand Down Expand Up @@ -122,14 +121,14 @@ public void onStart() {
this.bridge.onStart();
mockWebView.handleStart();

Log.d(LogUtils.getCoreTag(), "App started");
Logger.debug("App started");
}

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

@Override
Expand All @@ -142,7 +141,7 @@ public void onResume() {

mockWebView.handleResume(this.keepRunning);

Log.d(LogUtils.getCoreTag(), "App resumed");
Logger.debug("App resumed");
}

@Override
Expand All @@ -155,7 +154,7 @@ public void onPause() {
this.mockWebView.handlePause(keepRunning);
}

Log.d(LogUtils.getCoreTag(), "App paused");
Logger.debug("App paused");
}

@Override
Expand All @@ -173,7 +172,7 @@ public void onStop() {
mockWebView.handleStop();
}

Log.d(LogUtils.getCoreTag(), "App stopped");
Logger.debug("App stopped");
}

@Override
Expand All @@ -183,7 +182,7 @@ public void onDestroy() {
if (this.mockWebView != null) {
mockWebView.handleDestroy();
}
Log.d(LogUtils.getCoreTag(), "App destroyed");
Logger.debug("App destroyed");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import android.net.Uri;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -84,7 +83,7 @@ protected void init(Bundle savedInstanceState) {
* Load the WebView and create the Bridge
*/
protected void load(Bundle savedInstanceState) {
Log.d(LogUtils.getCoreTag(), "Starting BridgeActivity");
Logger.debug("Starting BridgeActivity");

Bundle args = getArguments();
String startDir = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;
import android.webkit.ConsoleMessage;
import android.webkit.GeolocationPermissions;
import android.webkit.JsPromptResult;
Expand Down Expand Up @@ -181,7 +180,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(LogUtils.getCoreTag(), "onGeolocationPermissionsShowPrompt: DOING IT HERE FOR ORIGIN: " + origin);
Logger.debug("onGeolocationPermissionsShowPrompt: DOING IT HERE FOR ORIGIN: " + origin);

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

Expand All @@ -211,7 +210,7 @@ public void onRequestPermissionResult(int requestCode, String[] permissions, int
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
showMediaCaptureOrFilePicker(filePathCallback, fileChooserParams, captureVideo);
} else {
Log.w(LogUtils.getCoreTag("FileChooser"), "Camera permission not granted");
Logger.warn(Logger.tags("FileChooser"), "Camera permission not granted");
filePathCallback.onReceiveValue(null);
}
}
Expand Down Expand Up @@ -244,7 +243,7 @@ private void showMediaCaptureOrFilePicker(ValueCallback<Uri[]> filePathCallback,
shown = showImageCapturePicker(filePathCallback);
}
if (!shown) {
Log.w(LogUtils.getCoreTag("FileChooser"), "Media capture intent could not be launched. Falling back to default file picker.");
Logger.warn(Logger.tags("FileChooser"), "Media capture intent could not be launched. Falling back to default file picker.");
showFilePicker(filePathCallback, fileChooserParams);
}
}
Expand All @@ -259,7 +258,7 @@ private boolean showImageCapturePicker(final ValueCallback<Uri[]> filePathCallba
try {
imageFileUri = CameraUtils.createImageFileUri(bridge.getActivity(), bridge.getContext().getPackageName());
} catch (Exception ex) {
Log.e(LogUtils.getCoreTag(), "Unable to create temporary media capture file: " + ex.getMessage());
Logger.error("Unable to create temporary media capture file: " + ex.getMessage());
return false;
}
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageFileUri);
Expand Down Expand Up @@ -350,18 +349,18 @@ private String[] getValidTypes(String[] currentTypes) {

@Override
public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
String tag = "Capacitor/Console";
String tag = Logger.tags("Console");
if (consoleMessage.message() != null && isValidMsg(consoleMessage.message())) {
String msg = String.format("File: %s - Line %d - Msg: %s" , consoleMessage.sourceId() , consoleMessage.lineNumber(), consoleMessage.message());
String level = consoleMessage.messageLevel().name();
if ("ERROR".equalsIgnoreCase(level)) {
Log.e(tag, msg);
Logger.error(tag, msg, null);
} else if ("WARNING".equalsIgnoreCase(level)) {
Log.w(tag, msg);
Logger.warn(tag, msg);
} else if ("TIP".equalsIgnoreCase(level)) {
Log.d(tag, msg);
Logger.debug(tag, msg);
} else {
Log.i(tag, msg);
Logger.info(tag, msg);
}
}
return true;
Expand Down
Loading

0 comments on commit d60757a

Please sign in to comment.