diff options
author | cimamoglu@chromium.org <cimamoglu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-11-20 16:52:21 +0000 |
---|---|---|
committer | cimamoglu@chromium.org <cimamoglu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-11-20 16:52:21 +0000 |
commit | 90fba6c7ff57877ca5047eff24dc1ae08b9819e5 (patch) | |
tree | e1b1ead0eb86af78e06b72c1084554ab74e70d76 /chrome/android | |
parent | c665ede20f558f4528543ea452c2e7c777062b28 (diff) | |
download | chromium_src-90fba6c7ff57877ca5047eff24dc1ae08b9819e5.zip chromium_src-90fba6c7ff57877ca5047eff24dc1ae08b9819e5.tar.gz chromium_src-90fba6c7ff57877ca5047eff24dc1ae08b9819e5.tar.bz2 |
Refactor Android printing code to make it more testable.
* Move printing logic from Tab to TabBase (i.e. to upstream), and
also in the relevant files tab_android.*, TabPrinter.java.
* Remove obsolete Android printing feature detection code.
* Move PrintingControllerFactory logic into PrintingControllerImpl.
* Create a new PrintingControllerFactory, so the clients have a
ligher weight creation process (5-6 lines to 1).
* Instead of depending on Context to create a PrintManager, depend
on an interface, namely PrintManagerDelegate.
* Remove setErrorText (move the logic inside factory).
* Remove the hardcoded default file name (use Printable#getTitle)
instead.
BUG=315229
Review URL: https://codereview.chromium.org/63483007
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@236256 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/android')
3 files changed, 98 insertions, 1 deletions
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/TabBase.java b/chrome/android/java/src/org/chromium/chrome/browser/TabBase.java index 75cfa08..741f1ee 100644 --- a/chrome/android/java/src/org/chromium/chrome/browser/TabBase.java +++ b/chrome/android/java/src/org/chromium/chrome/browser/TabBase.java @@ -309,7 +309,6 @@ public abstract class TabBase implements NavigationClient { } /** - * * @return The infobar container. */ public final InfoBarContainer getInfoBarContainer() { @@ -323,6 +322,16 @@ public abstract class TabBase implements NavigationClient { protected abstract AutoLoginProcessor createAutoLoginProcessor(); /** + * Prints the current page. + * + * @return Whether the printing process is started successfully. + **/ + public boolean print() { + assert mNativeTabAndroid != 0; + return nativePrint(mNativeTabAndroid); + } + + /** * Reloads the current page content if it is a {@link ContentView}. */ public void reload() { @@ -771,4 +780,5 @@ public abstract class TabBase implements NavigationClient { private native int nativeGetSecurityLevel(int nativeTabAndroid); private native void nativeSetActiveNavigationEntryTitleForUrl(int nativeTabAndroid, String url, String title); + private native boolean nativePrint(int nativeTabAndroid); } diff --git a/chrome/android/java/src/org/chromium/chrome/browser/printing/PrintingControllerFactory.java b/chrome/android/java/src/org/chromium/chrome/browser/printing/PrintingControllerFactory.java new file mode 100644 index 0000000..600975b --- /dev/null +++ b/chrome/android/java/src/org/chromium/chrome/browser/printing/PrintingControllerFactory.java @@ -0,0 +1,35 @@ +// Copyright 2013 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.printing; + +import android.app.Activity; +import android.content.Context; +import android.print.PrintManager; + +import org.chromium.chrome.R; +import org.chromium.printing.PrintManagerDelegateImpl; +import org.chromium.printing.PrintingController; +import org.chromium.printing.PrintingControllerImpl; + +/** + * Creates a {@link PrintingControllerImpl}. + * + * Also, sets the default title of {@link TabPrinter}. + */ +public class PrintingControllerFactory { + public static PrintingController create(Activity activity) { + if (PrintingControllerImpl.isPrintingSupported()) { + String defaultJobTitle = activity.getResources().getString(R.string.menu_print); + TabPrinter.setDefaultTitle(defaultJobTitle); + + PrintManager printManager = + (PrintManager) activity.getSystemService(Context.PRINT_SERVICE); + String errorText = activity.getResources().getString(R.string.error_printing_failed); + return PrintingControllerImpl.create( + new PrintManagerDelegateImpl(printManager), errorText); + } + return null; + } +} diff --git a/chrome/android/java/src/org/chromium/chrome/browser/printing/TabPrinter.java b/chrome/android/java/src/org/chromium/chrome/browser/printing/TabPrinter.java new file mode 100644 index 0000000..a522308 --- /dev/null +++ b/chrome/android/java/src/org/chromium/chrome/browser/printing/TabPrinter.java @@ -0,0 +1,52 @@ +// Copyright 2013 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.printing; + +import android.text.TextUtils; + +import org.chromium.chrome.browser.TabBase; +import org.chromium.printing.Printable; + +import java.lang.ref.WeakReference; + +/** + * Wraps printing related functionality of a {@link TabBase} object. + * + * This class doesn't have any lifetime expectations with regards to Tab, since we keep a weak + * reference. + */ +public class TabPrinter implements Printable { + private static String sDefaultTitle; + + private final WeakReference<TabBase> mTab; + + public TabPrinter(TabBase tab) { + mTab = new WeakReference<TabBase>(tab); + } + + public static void setDefaultTitle(String defaultTitle) { + sDefaultTitle = defaultTitle; + } + + @Override + public boolean print() { + TabBase tab = mTab.get(); + return tab != null && tab.print(); + } + + @Override + public String getTitle() { + TabBase tab = mTab.get(); + if (tab == null) return sDefaultTitle; + + String title = tab.getTitle(); + if (!TextUtils.isEmpty(title)) return title; + + String url = tab.getUrl(); + if (!TextUtils.isEmpty(url)) return url; + + return sDefaultTitle; + } +} |