forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
cimamoglu@chromium.org
committed
Nov 20, 2013
1 parent
c665ede
commit 90fba6c
Showing
12 changed files
with
245 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
chrome/android/java/src/org/chromium/chrome/browser/printing/PrintingControllerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
chrome/android/java/src/org/chromium/chrome/browser/printing/TabPrinter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
printing/android/java/src/org/chromium/printing/PrintManagerDelegate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// 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.printing; | ||
|
||
import android.print.PrintAttributes; | ||
import android.print.PrintDocumentAdapter; | ||
|
||
/** | ||
* Defines an interface for the Android system printing service, for easier testing. | ||
* We can't simply extend from {@link android.print.PrintManager}, since it's a final class. | ||
*/ | ||
public interface PrintManagerDelegate { | ||
|
||
/** | ||
* Same as {@link android.print.PrintManager#print}, except this doesn't return a | ||
* {@link android.print.PrintJob} since the clients don't need it. | ||
*/ | ||
void print(String printJobName, | ||
PrintDocumentAdapter documentAdapter, | ||
PrintAttributes attributes); | ||
} |
27 changes: 27 additions & 0 deletions
27
printing/android/java/src/org/chromium/printing/PrintManagerDelegateImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// 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.printing; | ||
|
||
import android.print.PrintAttributes; | ||
import android.print.PrintDocumentAdapter; | ||
import android.print.PrintManager; | ||
|
||
/** | ||
* An implementation of {@link PrintManagerDelegate} using the Android framework print manager. | ||
*/ | ||
public class PrintManagerDelegateImpl implements PrintManagerDelegate { | ||
private final PrintManager mPrintManager; | ||
|
||
public PrintManagerDelegateImpl(PrintManager printManager) { | ||
mPrintManager = printManager; | ||
} | ||
|
||
@Override | ||
public void print(String printJobName, PrintDocumentAdapter documentAdapter, | ||
PrintAttributes attributes) { | ||
mPrintManager.print(printJobName, documentAdapter, attributes); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 0 additions & 33 deletions
33
printing/android/java/src/org/chromium/printing/PrintingControllerFactory.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.