Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/dom_distiller/DomDistillerUIUtils.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/dom_distiller/DomDistillerUIUtils.java b/chrome/android/java/src/org/chromium/chrome/browser/dom_distiller/DomDistillerUIUtils.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d775a305f0973464b56f0584a060364a778f14d8 |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/dom_distiller/DomDistillerUIUtils.java |
| @@ -0,0 +1,96 @@ |
| +// Copyright 2014 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.chrome.browser.dom_distiller; |
| + |
| +import android.app.Activity; |
| +import android.support.v7.app.AlertDialog; |
| + |
| +import org.chromium.base.ThreadUtils; |
| +import org.chromium.base.annotations.CalledByNative; |
| +import org.chromium.base.annotations.JNINamespace; |
| +import org.chromium.base.metrics.RecordUserAction; |
| +import org.chromium.chrome.R; |
| +import org.chromium.chrome.browser.ChromeApplication; |
| +import org.chromium.chrome.browser.feedback.FeedbackCollector; |
| +import org.chromium.chrome.browser.feedback.FeedbackReporter; |
| +import org.chromium.chrome.browser.profiles.Profile; |
| +import org.chromium.content.browser.ContentViewCore; |
| +import org.chromium.content_public.browser.WebContents; |
| +import org.chromium.ui.base.WindowAndroid; |
| + |
| +/** |
| + * Java implementation of dom_distiller::android::DistillerUIHandleAndroid. |
| + */ |
| +@JNINamespace("dom_distiller::android") |
| +public final class DomDistillerUIUtils { |
| + private static final String DISTILLATION_QUALITY_KEY = "Distillation quality"; |
| + private static final String DISTILLATION_QUALITY_GOOD = "good"; |
| + private static final String DISTILLATION_QUALITY_BAD = "bad"; |
| + |
| + private static FeedbackReporter sFeedbackReporter; |
| + |
| + /** |
| + * A static method for native code to open the external feedback form UI. |
| + * @param webContents The WebContents containing the distilled content. |
| + * @param url The URL to report feedback for. |
| + * @param good True if the feedback is good and false if not. |
| + */ |
| + @CalledByNative |
| + public static void reportFeedbackWithWebContents( |
| + WebContents webContents, String url, final boolean good) { |
| + ThreadUtils.assertOnUiThread(); |
| + // TODO(mdjones): It would be better to get he WebContents from the manager so that the |
| + // native code does not need to depend on RenderFrame. |
| + Activity activity = getActivityFromWebContents(webContents); |
| + if (activity == null) return; |
| + |
| + if (sFeedbackReporter == null) { |
| + ChromeApplication application = (ChromeApplication) activity.getApplication(); |
| + sFeedbackReporter = application.createFeedbackReporter(); |
| + } |
| + FeedbackCollector.create(activity, Profile.getLastUsedProfile(), url, |
| + new FeedbackCollector.FeedbackResult() { |
| + @Override |
| + public void onResult(FeedbackCollector collector) { |
| + String quality = |
| + good ? DISTILLATION_QUALITY_GOOD : DISTILLATION_QUALITY_BAD; |
| + collector.add(DISTILLATION_QUALITY_KEY, quality); |
| + sFeedbackReporter.reportFeedback(collector); |
| + } |
| + }); |
| + } |
| + |
| + /** |
| + * A static method for native code to call to open the distiller UI settings. |
| + * @param webContents The WebContents containing the distilled content. |
| + */ |
| + @CalledByNative |
| + public static void openSettings(WebContents webContents) { |
| + Activity activity = getActivityFromWebContents(webContents); |
| + if (webContents != null && activity != null) { |
| + RecordUserAction.record("DomDistiller_DistilledPagePrefsOpened"); |
| + AlertDialog.Builder builder = |
| + new AlertDialog.Builder(activity, R.style.AlertDialogTheme); |
| + builder.setView(DistilledPagePrefsView.create(activity)); |
| + builder.show(); |
| + } |
| + } |
| + |
| + /** |
| + * @param webContents The WebContents to get the Activity from. |
| + * @return The Activity associated with the WebContents. |
| + */ |
| + private static Activity getActivityFromWebContents(WebContents webContents) { |
| + ContentViewCore contentView = ContentViewCore.fromWebContents(webContents); |
|
nyquist
2015/10/14 17:48:22
Does fromWebContents support taking in null as an
mdjones
2015/10/14 20:55:42
It does, but it makes a JNI call to check it; I ad
|
| + if (contentView == null) return null; |
| + |
| + WindowAndroid window = contentView.getWindowAndroid(); |
| + if (window == null) return null; |
| + |
| + return window.getActivity().get(); |
| + } |
| + |
| + private DomDistillerUIUtils() {} |
| +} |