Merge "Add print samples to the ApiDemos app." into klp-dev

This commit is contained in:
Svetoslav Ganov
2013-10-30 21:04:48 +00:00
committed by Android (Google) Code Review
12 changed files with 703 additions and 2 deletions

View File

@@ -12,6 +12,8 @@ LOCAL_SRC_FILES += \
LOCAL_JAVA_LIBRARIES := telephony-common mms-common
LOCAL_STATIC_JAVA_LIBRARIES = android-support-v4
LOCAL_PACKAGE_NAME := ApiDemos
LOCAL_SDK_VERSION := current

View File

@@ -996,6 +996,33 @@
</intent-filter>
</activity>
<activity android:name=".app.PrintBitmap"
android:label="@string/print_bitmap"
android:enabled="@bool/atLeastKitKat">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" />
</intent-filter>
</activity>
<activity android:name=".app.PrintHtmlFromScreen"
android:label="@string/print_html_from_screen"
android:enabled="@bool/atLeastKitKat">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" />
</intent-filter>
</activity>
<activity android:name=".app.PrintHtmlOffScreen"
android:label="@string/print_html_off_screen"
android:enabled="@bool/atLeastKitKat">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" />
</intent-filter>
</activity>
<activity android:name=".app.PrintCustomContent"
android:label="@string/print_custom_content"
android:enabled="@bool/atLeastKitKat">

View File

@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2013 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@raw/android_logo"
android:contentDescription="@string/android_logo">
</ImageView>

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2013 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/web_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="16dip">
</WebView>

View File

@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2013 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textIsSelectable="false"
android:text="@string/print_html_off_screen_msg">
</TextView>

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

View File

@@ -0,0 +1,333 @@
<!DOCTYPE html>
<html>
<body>
<table>
<caption>500cc/MotoGP champions</caption>
<thead><tr>
<th scope="col">Season</td>
<th scope="col">Rider</td>
<th scope="col">Constructor</td>
</tr></thead><tbody>
<tr>
<td>2012</td>
<td>Jorge Lorenzo</td>
<td>Yamaha</td>
</tr>
<tr>
<td>2011</td>
<td>Casey Stoner</td>
<td>Honda</td>
</tr>
<tr>
<td>2010</td>
<td>Jorge Lorenzo</td>
<td>Yamaha</td>
</tr>
<tr>
<td>2009</td>
<td>Valentino Rossi</td>
<td>Yamaha</td>
</tr>
<tr>
<td>2008</td>
<td>Valentino Rossi</td>
<td>Yamaha</td>
</tr>
<tr>
<td>2007</td>
<td>Casey Stoner</td>
<td>Ducati</td>
</tr>
<tr>
<td>2006</td>
<td>Nicky Hayden</td>
<td>Honda</td>
</tr>
<tr>
<td>2005</td>
<td>Valentino Rossi</td>
<td>Yamaha</td>
</tr>
<tr>
<td>2004</td>
<td>Valentino Rossi</td>
<td>Yamaha</td>
</tr>
<tr>
<td>2003</td>
<td>Valentino Rossi</td>
<td>Honda</td>
</tr>
<tr>
<td>2002</td>
<td>Valentino Rossi</td>
<td>Honda</td>
</tr>
<tr>
<td>2001</td>
<td>Valentino Rossi</td>
<td>Honda</td>
</tr>
<tr>
<td>2000</td>
<td>Kenny Roberts, Jr.</td>
<td>Suzuki</td>
</tr>
<tr>
<td>1999</td>
<td><EFBFBD>lex Crivill<6C></td>
<td>Honda</td>
</tr>
<tr>
<td>1998</td>
<td>Michael Doohan</td>
<td>Honda</td>
</tr>
<tr>
<td>1997</td>
<td>Michael Doohan</td>
<td>Honda</td>
</tr>
<tr>
<td>1996</td>
<td>Michael Doohan</td>
<td>Honda</td>
</tr>
<tr>
<td>1995</td>
<td>Michael Doohan</td>
<td>Honda</td>
</tr>
<tr>
<td>1994</td>
<td>Michael Doohan</td>
<td>Honda</td>
</tr>
<tr>
<td>1993</td>
<td>Kevin Schwantz</td>
<td>Suzuki</td>
</tr>
<tr>
<td>1992</td>
<td>Wayne Rainey</td>
<td>Yamaha</td>
</tr>
<tr>
<td>1991</td>
<td>Wayne Rainey</td>
<td>Yamaha</td>
</tr>
<tr>
<td>1990</td>
<td>Wayne Rainey</td>
<td>Yamaha</td>
</tr>
<tr>
<td>1989</td>
<td>Eddie Lawson</td>
<td>Honda</td>
</tr>
<tr>
<td>1988</td>
<td>Eddie Lawson</td>
<td>Yamaha</td>
</tr>
<tr>
<td>1987</td>
<td>Wayne Gardner</td>
<td>Honda</td>
</tr>
<tr>
<td>1986</td>
<td>Eddie Lawson</td>
<td>Yamaha</td>
</tr>
<tr>
<td>1985</td>
<td>Freddie Spencer</td>
<td>Honda</td>
</tr>
<tr>
<td>1984</td>
<td>Eddie Lawson</td>
<td>Yamaha</td>
</tr>
<tr>
<td>1983</td>
<td>Freddie Spencer</td>
<td>Honda</td>
</tr>
<tr>
<td>1982</td>
<td>Franco Uncini</td>
<td>Suzuki</td>
</tr>
<tr>
<td>1981</td>
<td>Marco Lucchinelli</td>
<td>Suzuki</td>
</tr>
<tr>
<td>1980</td>
<td>Kenny Roberts</td>
<td>Yamaha</td>
</tr>
<tr>
<td>1979</td>
<td>Kenny Roberts</td>
<td>Yamaha</td>
</tr>
<tr>
<td>1978</td>
<td>Kenny Roberts</td>
<td>Yamaha</td>
</tr>
<tr>
<td>1977</td>
<td>Barry Sheene</td>
<td>Suzuki</td>
</tr>
<tr>
<td>1976</td>
<td>Barry Sheene</td>
<td>Suzuki</td>
</tr>
<tr>
<td>1975</td>
<td>Giacomo Agostini</td>
<td>Yamaha</td>
</tr>
<tr>
<td>1974</td>
<td>Phil Read</td>
<td>MV Agusta</td>
</tr>
<tr>
<td>1973</td>
<td>Phil Read</td>
<td>MV Agusta</td>
</tr>
<tr>
<td>1972</td>
<td>Giacomo Agostini</td>
<td>MV Agusta</td>
</tr>
<tr>
<td>1971</td>
<td>Giacomo Agostini</td>
<td>MV Agusta</td>
</tr>
<tr>
<td>1970</td>
<td>Giacomo Agostini</td>
<td>MV Agusta</td>
</tr>
<tr>
<td>1969</td>
<td>Giacomo Agostini</td>
<td>MV Agusta</td>
</tr>
<tr>
<td>1968</td>
<td>Giacomo Agostini</td>
<td>MV Agusta</td>
</tr>
<tr>
<td>1967</td>
<td>Giacomo Agostini</td>
<td>MV Agusta</td>
</tr>
<tr>
<td>1966</td>
<td>Giacomo Agostini</td>
<td>MV Agusta</td>
</tr>
<tr>
<td>1965</td>
<td>Mike Hailwood</td>
<td>MV Agusta</td>
</tr>
<tr>
<td>1964</td>
<td>Mike Hailwood</td>
<td>MV Agusta</td>
</tr>
<tr>
<td>1963</td>
<td>Mike Hailwood</td>
<td>MV Agusta</td>
</tr>
<tr>
<td>1962</td>
<td>Mike Hailwood</td>
<td>MV Agusta</td>
</tr>
<tr>
<td>1961</td>
<td>Gary Hocking</td>
<td>MV Agusta</td>
</tr>
<tr>
<td>1960</td>
<td>John Surtees</td>
<td>MV Agusta</td>
</tr>
<tr>
<td>1959</td>
<td>John Surtees</td>
<td>MV Agusta</td>
</tr>
<tr>
<td>1958</td>
<td>John Surtees</td>
<td>MV Agusta</td>
</tr>
<tr>
<td>1957</td>
<td>Libero Liberati</td>
<td>Gilera</td>
</tr>
<tr>
<td>1956</td>
<td>John Surtees</td>
<td>MV Agusta</td>
</tr>
<tr>
<td>1955</td>
<td>Geoff Duke</td>
<td>Gilera</td>
</tr>
<tr>
<td>1954</td>
<td>Geoff Duke</td>
<td>Gilera</td>
</tr>
<tr>
<td>1953</td>
<td>Geoff Duke</td>
<td>Gilera</td>
</tr>
<tr>
<td>1952</td>
<td>Umberto Masetti</td>
<td>Gilera</td>
</tr>
<tr>
<td>1951</td>
<td>Geoff Duke</td>
<td>Norton</td>
</tr>
<tr>
<td>1950</td>
<td>Umberto Masetti</td>
<td>Gilera</td>
</tr>
<tr>
<td>1949</td>
<td>Leslie Graham</td>
<td>AJS</td>
</tr>
</tbody><tfoot></tfoot></table>
</body>
</html>

View File

@@ -875,8 +875,14 @@
<!-- app/print print examples strings -->
<!-- ================================= -->
<string name="print_bitmap">App/Print/Print Bitmap</string>
<string name="print_html_from_screen">App/Print/Print HTML from screen</string>
<string name="print_html_off_screen">App/Print/Print HTML off screen</string>
<string name="print_custom_content">App/Print/Print Custom Layout</string>
<string name="print">Print</string>
<string name="print_html_off_screen_msg">From the overflow menu you can print some
off screen content.</string>
<string name="android_logo">Android logo</string>
<!-- ============================ -->
<!-- graphics examples strings -->

View File

@@ -0,0 +1,82 @@
package com.example.android.apis.app;
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.print.PrintManager;
import android.support.v4.print.PrintHelper;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.widget.ImageView;
import com.example.android.apis.R;
/**
* This class demonstrates how to implement bitmap printing.
* <p>
* This activity shows an image and offers a print option in the overflow
* menu. When the user chooses to print a helper class from the support
* library is used to print the image.
* </p>
*
* @see PrintManager
* @see WebView
*/
public class PrintBitmap extends Activity {
private ImageView mImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.print_bitmap);
mImageView = (ImageView) findViewById(R.id.image);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.print_custom_content, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.menu_print) {
print();
return true;
}
return super.onOptionsItemSelected(item);
}
private void print() {
// Get the print manager.
PrintHelper printHelper = new PrintHelper(this);
// Set the desired scale mode.
printHelper.setScaleMode(PrintHelper.SCALE_MODE_FIT);
// Get the bitmap for the ImageView's drawable.
Bitmap bitmap = ((BitmapDrawable) mImageView.getDrawable()).getBitmap();
// Print the bitmap.
printHelper.printBitmap("Print Bitmap", bitmap);
}
}

View File

@@ -376,9 +376,9 @@ public class PrintCustomContent extends ListActivity {
// Add the height but if the view crosses the page
// boundary we will put it to the next one.
pageContentHeight += view.getHeight();
pageContentHeight += view.getMeasuredHeight();
if (currentPage < 0 || pageContentHeight > mRenderPageHeight) {
pageContentHeight = view.getHeight();
pageContentHeight = view.getMeasuredHeight();
currentPage++;
// Done with the current page - finish it.
if (page != null) {

View File

@@ -0,0 +1,93 @@
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.apis.app;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.print.PrintManager;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.example.android.apis.R;
/**
* This class demonstrates how to implement HTML content printing
* from a {@link WebView} which is shown on the screen.
* <p>
* This activity shows a simple HTML content in a {@link WebView}
* and allows the user to print that content via an action in the
* action bar. The shown {@link WebView} is doing the printing.
* </p>
*
* @see PrintManager
* @see WebView
*/
public class PrintHtmlFromScreen extends Activity {
private WebView mWebView;
private boolean mDataLoaded;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.print_html_from_screen);
mWebView = (WebView) findViewById(R.id.web_view);
// Important: Only enable the print option after the page is loaded.
mWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
// Data loaded, so now we want to show the print option.
mDataLoaded = true;
invalidateOptionsMenu();
}
});
// Load an HTML page.
mWebView.loadUrl("file:///android_res/raw/motogp_stats.html");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
if (mDataLoaded) {
getMenuInflater().inflate(R.menu.print_custom_content, menu);
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.menu_print) {
print();
return true;
}
return super.onOptionsItemSelected(item);
}
private void print() {
// Get the print manager.
PrintManager printManager = (PrintManager) getSystemService(
Context.PRINT_SERVICE);
// Pass in the ViewView's document adapter.
printManager.print("MotoGP stats", mWebView.createPrintDocumentAdapter(), null);
}
}

View File

@@ -0,0 +1,89 @@
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.apis.app;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.print.PrintManager;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.example.android.apis.R;
/**
* This class demonstrates how to implement HTML content printing
* from a {@link WebView} which is not shown on the screen.
* <p>
* This activity shows a text prompt and when the user chooses the
* print option from the overflow menu an HTML page with content that
* is not on the screen is printed via an off-screen {@link WebView}.
* </p>
*
* @see PrintManager
* @see WebView
*/
public class PrintHtmlOffScreen extends Activity {
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.print_html_off_screen);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.print_custom_content, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.menu_print) {
print();
return true;
}
return super.onOptionsItemSelected(item);
}
private void print() {
// Create a WebView and hold on to it as the printing will start when
// load completes and we do not want the WbeView to be garbage collected.
mWebView = new WebView(this);
// Important: Only after the page is loaded we will do the print.
mWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
// Get the print manager.
PrintManager printManager = (PrintManager) getSystemService(
Context.PRINT_SERVICE);
// Pass in the ViewView's document adapter.
printManager.print("MotoGP stats", mWebView.createPrintDocumentAdapter(),
null);
}
});
// Load an HTML page.
mWebView.loadUrl("file:///android_res/raw/motogp_stats.html");
}
}