Index: android_webview/java/src/org/chromium/android_webview/AwMetricsServiceClient.java |
diff --git a/android_webview/java/src/org/chromium/android_webview/AwMetricsServiceClient.java b/android_webview/java/src/org/chromium/android_webview/AwMetricsServiceClient.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..88d7ff1d30503b1f6c72943ae512dab582845e8d |
--- /dev/null |
+++ b/android_webview/java/src/org/chromium/android_webview/AwMetricsServiceClient.java |
@@ -0,0 +1,60 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+package org.chromium.android_webview; |
+ |
+import android.content.Context; |
+import android.webkit.ValueCallback; |
+ |
+import org.chromium.base.Log; |
+import org.chromium.base.annotations.JNINamespace; |
+ |
+import java.lang.reflect.InvocationTargetException; |
+ |
+/** |
+ * Java twin of the homonymous C++ class. The Java side is only responsible for |
+ * switching metrics on and off. Since the setting is a platform feature, it |
+ * must be obtained through PlatformServiceBridge. |
+ */ |
+@JNINamespace("android_webview") |
+public class AwMetricsServiceClient { |
+ private static final String TAG = "AwMetricsServiceCli-"; |
sgurun-gerrit only
2015/12/11 00:06:45
"AwMetricsServiceClient"
paulmiller
2015/12/18 00:15:49
I'd prefer that too, but there's a length limit.
|
+ private static final String GMAIL = "com.google.android.gm"; |
sgurun-gerrit only
2015/12/11 00:06:46
let's move this logic to the bridge
paulmiller
2015/12/18 00:15:49
Are you sure? The Gmail detection is specific to M
|
+ private static final String PLATFORM_SERVICE_BRIDGE = |
+ "com.android.webview.chromium.PlatformServiceBridgeGoogle"; |
+ |
+ private PlatformServiceBridge mPlatformServiceBridge; |
+ |
+ public AwMetricsServiceClient(Context applicationContext) { |
+ mPlatformServiceBridge = getPlatformServiceBridge(applicationContext); |
+ |
+ mPlatformServiceBridge.setMetricsSettingListener(new ValueCallback<Boolean>() { |
+ public void onReceiveValue(Boolean enabled) { |
+ nativeSetMetricsEnabled(enabled.booleanValue()); |
+ } |
+ }); |
+ } |
+ |
+ private static PlatformServiceBridge getPlatformServiceBridge(Context applicationContext) { |
+ // If this is gmail, try to get the Google service bridge. |
+ if (GMAIL.equals(applicationContext.getPackageName())) { |
sgurun-gerrit only
2015/12/11 00:06:46
same, move this to the bridge.
|
+ try { |
+ Class<?> cls = Class.forName(PLATFORM_SERVICE_BRIDGE); |
+ return (PlatformServiceBridge) cls.getDeclaredConstructor(Context.class) |
+ .newInstance(applicationContext); |
+ } catch (InvocationTargetException e) { |
+ Log.e(TAG, "Failed to get " + PLATFORM_SERVICE_BRIDGE + " (invocation):", |
sgurun-gerrit only
2015/12/11 00:06:46
not an error. This is actually a reasonable state
paulmiller
2015/12/18 00:15:50
I think that'll actually be a ClassNotFoundExcepti
|
+ e.getCause()); |
+ } catch (Exception e) { |
sgurun-gerrit only
2015/12/11 00:06:45
I don't think you can put a blanket exception here
paulmiller
2015/12/18 00:15:49
fixed
|
+ Log.e(TAG, "Failed to get " + PLATFORM_SERVICE_BRIDGE + ":", e); |
sgurun-gerrit only
2015/12/11 00:06:46
rather than repeating the Log.e, use a boolean to
paulmiller
2015/12/18 00:15:49
But I want to log InvocationTargetException differ
|
+ } |
+ } |
+ |
+ // If this is not gmail or getting the preferred service |
+ // bridge failed, get the generic service bridge. |
sgurun-gerrit only
2015/12/11 00:06:46
remove the gmail comment.
paulmiller
2015/12/18 00:15:49
fixed
|
+ return new PlatformServiceBridge(); |
+ } |
+ |
+ public static native void nativeSetMetricsEnabled(boolean enabled); |
+} |