diff options
author | Trevor Johns <trevorjohns@google.com> | 2014-11-12 11:39:30 -0800 |
---|---|---|
committer | Trevor Johns <trevorjohns@google.com> | 2014-11-12 11:39:30 -0800 |
commit | 527a4f30a69aaa54fc9654539f0b6d839e37af3c (patch) | |
tree | 38f816a286e672e8f00caed6a0344c3c18632a78 /samples/browseable/PdfRendererBasic | |
parent | fcd28181a15ebe95724fe096a8b5198c964b0d7f (diff) | |
download | android_development-527a4f30a69aaa54fc9654539f0b6d839e37af3c.tar.gz android_development-527a4f30a69aaa54fc9654539f0b6d839e37af3c.tar.bz2 android_development-527a4f30a69aaa54fc9654539f0b6d839e37af3c.zip |
Update browseable samples for lmp-docs
Synced to commit df5e5013422b81b4fd05c0ac9fd964b13624847a. Includes
new samples for Android Auto.
Change-Id: I3fec46e2a6b3f196682a92f1afd91eb682dc2dc1
Diffstat (limited to 'samples/browseable/PdfRendererBasic')
25 files changed, 686 insertions, 0 deletions
diff --git a/samples/browseable/PdfRendererBasic/AndroidManifest.xml b/samples/browseable/PdfRendererBasic/AndroidManifest.xml new file mode 100644 index 000000000..22870679a --- /dev/null +++ b/samples/browseable/PdfRendererBasic/AndroidManifest.xml @@ -0,0 +1,38 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Copyright 2014 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. +--> + +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="com.example.android.pdfrendererbasic" + android:versionCode="1" + android:versionName="1.0"> + + <application android:allowBackup="true" + android:label="@string/app_name" + android:icon="@drawable/ic_launcher" + android:theme="@style/AppTheme"> + + <activity android:name=".MainActivity" + android:label="@string/app_name"> + <intent-filter> + <action android:name="android.intent.action.MAIN" /> + <category android:name="android.intent.category.LAUNCHER" /> + </intent-filter> + </activity> + </application> + + +</manifest> diff --git a/samples/browseable/PdfRendererBasic/_index.jd b/samples/browseable/PdfRendererBasic/_index.jd new file mode 100644 index 000000000..59c5a5309 --- /dev/null +++ b/samples/browseable/PdfRendererBasic/_index.jd @@ -0,0 +1,9 @@ +page.tags="PdfRendererBasic" +sample.group=UI +@jd:body + +<p> + + This sample demonstrates how to use PdfRenderer to display PDF documents on the screen. + + </p> diff --git a/samples/browseable/PdfRendererBasic/res/drawable-hdpi/ic_action_info.png b/samples/browseable/PdfRendererBasic/res/drawable-hdpi/ic_action_info.png Binary files differnew file mode 100644 index 000000000..32bd1aabc --- /dev/null +++ b/samples/browseable/PdfRendererBasic/res/drawable-hdpi/ic_action_info.png diff --git a/samples/browseable/PdfRendererBasic/res/drawable-hdpi/ic_launcher.png b/samples/browseable/PdfRendererBasic/res/drawable-hdpi/ic_launcher.png Binary files differnew file mode 100644 index 000000000..0c9b69468 --- /dev/null +++ b/samples/browseable/PdfRendererBasic/res/drawable-hdpi/ic_launcher.png diff --git a/samples/browseable/PdfRendererBasic/res/drawable-hdpi/tile.9.png b/samples/browseable/PdfRendererBasic/res/drawable-hdpi/tile.9.png Binary files differnew file mode 100644 index 000000000..135862883 --- /dev/null +++ b/samples/browseable/PdfRendererBasic/res/drawable-hdpi/tile.9.png diff --git a/samples/browseable/PdfRendererBasic/res/drawable-mdpi/ic_action_info.png b/samples/browseable/PdfRendererBasic/res/drawable-mdpi/ic_action_info.png Binary files differnew file mode 100644 index 000000000..8efbbf8b3 --- /dev/null +++ b/samples/browseable/PdfRendererBasic/res/drawable-mdpi/ic_action_info.png diff --git a/samples/browseable/PdfRendererBasic/res/drawable-mdpi/ic_launcher.png b/samples/browseable/PdfRendererBasic/res/drawable-mdpi/ic_launcher.png Binary files differnew file mode 100644 index 000000000..1a6c3d0d8 --- /dev/null +++ b/samples/browseable/PdfRendererBasic/res/drawable-mdpi/ic_launcher.png diff --git a/samples/browseable/PdfRendererBasic/res/drawable-xhdpi/ic_action_info.png b/samples/browseable/PdfRendererBasic/res/drawable-xhdpi/ic_action_info.png Binary files differnew file mode 100644 index 000000000..ba143ea7a --- /dev/null +++ b/samples/browseable/PdfRendererBasic/res/drawable-xhdpi/ic_action_info.png diff --git a/samples/browseable/PdfRendererBasic/res/drawable-xhdpi/ic_launcher.png b/samples/browseable/PdfRendererBasic/res/drawable-xhdpi/ic_launcher.png Binary files differnew file mode 100644 index 000000000..2081f403b --- /dev/null +++ b/samples/browseable/PdfRendererBasic/res/drawable-xhdpi/ic_launcher.png diff --git a/samples/browseable/PdfRendererBasic/res/drawable-xxhdpi/ic_action_info.png b/samples/browseable/PdfRendererBasic/res/drawable-xxhdpi/ic_action_info.png Binary files differnew file mode 100644 index 000000000..394eb7e53 --- /dev/null +++ b/samples/browseable/PdfRendererBasic/res/drawable-xxhdpi/ic_action_info.png diff --git a/samples/browseable/PdfRendererBasic/res/drawable-xxhdpi/ic_launcher.png b/samples/browseable/PdfRendererBasic/res/drawable-xxhdpi/ic_launcher.png Binary files differnew file mode 100644 index 000000000..b506de4d5 --- /dev/null +++ b/samples/browseable/PdfRendererBasic/res/drawable-xxhdpi/ic_launcher.png diff --git a/samples/browseable/PdfRendererBasic/res/layout/activity_main.xml b/samples/browseable/PdfRendererBasic/res/layout/activity_main.xml new file mode 100755 index 000000000..be1aa49d9 --- /dev/null +++ b/samples/browseable/PdfRendererBasic/res/layout/activity_main.xml @@ -0,0 +1,36 @@ +<!-- + Copyright 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. + --> + +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:orientation="vertical"> + + <LinearLayout style="@style/Widget.SampleMessageTile" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:orientation="vertical"> + + <TextView style="@style/Widget.SampleMessage" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_marginLeft="@dimen/horizontal_page_margin" + android:layout_marginRight="@dimen/horizontal_page_margin" + android:layout_marginTop="@dimen/vertical_page_margin" + android:layout_marginBottom="@dimen/vertical_page_margin" + android:text="@string/intro_message" /> + </LinearLayout> +</LinearLayout> diff --git a/samples/browseable/PdfRendererBasic/res/layout/activity_main_real.xml b/samples/browseable/PdfRendererBasic/res/layout/activity_main_real.xml new file mode 100644 index 000000000..dcd52beaf --- /dev/null +++ b/samples/browseable/PdfRendererBasic/res/layout/activity_main_real.xml @@ -0,0 +1,23 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + Copyright (C) 2014 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. +--> +<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:tools="http://schemas.android.com/tools" + android:id="@+id/container" + android:layout_width="match_parent" + android:layout_height="match_parent" + tools:context=".MainActivity" + tools:ignore="MergeRootFrame" /> diff --git a/samples/browseable/PdfRendererBasic/res/layout/fragment_pdf_renderer_basic.xml b/samples/browseable/PdfRendererBasic/res/layout/fragment_pdf_renderer_basic.xml new file mode 100644 index 000000000..4a7799b7b --- /dev/null +++ b/samples/browseable/PdfRendererBasic/res/layout/fragment_pdf_renderer_basic.xml @@ -0,0 +1,56 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + Copyright (C) 2014 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. +--> +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:tools="http://schemas.android.com/tools" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:orientation="vertical" + tools:context=".MainActivity$PlaceholderFragment"> + + <ImageView + android:id="@+id/image" + android:layout_width="match_parent" + android:layout_height="0dp" + android:layout_weight="1" + android:background="@android:color/white" + android:scaleType="fitCenter" /> + + <LinearLayout + style="?android:attr/buttonBarStyle" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:measureWithLargestChild="true" + android:orientation="horizontal"> + + <Button + android:id="@+id/previous" + style="?android:attr/buttonBarButtonStyle" + android:layout_width="0dp" + android:layout_height="wrap_content" + android:layout_weight="1" + android:text="@string/previous" /> + + <Button + android:id="@+id/next" + style="?android:attr/buttonBarButtonStyle" + android:layout_width="0dp" + android:layout_height="wrap_content" + android:layout_weight="1" + android:text="@string/next" /> + </LinearLayout> + +</LinearLayout> diff --git a/samples/browseable/PdfRendererBasic/res/menu/main.xml b/samples/browseable/PdfRendererBasic/res/menu/main.xml new file mode 100644 index 000000000..fa452e108 --- /dev/null +++ b/samples/browseable/PdfRendererBasic/res/menu/main.xml @@ -0,0 +1,25 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + Copyright (C) 2014 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. +--> +<menu xmlns:android="http://schemas.android.com/apk/res/android"> + + <item + android:id="@+id/action_info" + android:icon="@drawable/ic_action_info" + android:showAsAction="always" + android:title="@string/info" /> + +</menu>
\ No newline at end of file diff --git a/samples/browseable/PdfRendererBasic/res/values-sw600dp/template-dimens.xml b/samples/browseable/PdfRendererBasic/res/values-sw600dp/template-dimens.xml new file mode 100644 index 000000000..22074a2bd --- /dev/null +++ b/samples/browseable/PdfRendererBasic/res/values-sw600dp/template-dimens.xml @@ -0,0 +1,24 @@ +<!-- + Copyright 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. + --> + +<resources> + + <!-- Semantic definitions --> + + <dimen name="horizontal_page_margin">@dimen/margin_huge</dimen> + <dimen name="vertical_page_margin">@dimen/margin_medium</dimen> + +</resources> diff --git a/samples/browseable/PdfRendererBasic/res/values-sw600dp/template-styles.xml b/samples/browseable/PdfRendererBasic/res/values-sw600dp/template-styles.xml new file mode 100644 index 000000000..03d197418 --- /dev/null +++ b/samples/browseable/PdfRendererBasic/res/values-sw600dp/template-styles.xml @@ -0,0 +1,25 @@ +<!-- + Copyright 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. + --> + +<resources> + + <style name="Widget.SampleMessage"> + <item name="android:textAppearance">?android:textAppearanceLarge</item> + <item name="android:lineSpacingMultiplier">1.2</item> + <item name="android:shadowDy">-6.5</item> + </style> + +</resources> diff --git a/samples/browseable/PdfRendererBasic/res/values-v11/template-styles.xml b/samples/browseable/PdfRendererBasic/res/values-v11/template-styles.xml new file mode 100644 index 000000000..8c1ea66f2 --- /dev/null +++ b/samples/browseable/PdfRendererBasic/res/values-v11/template-styles.xml @@ -0,0 +1,22 @@ +<!-- + Copyright 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. + --> + +<resources> + + <!-- Activity themes --> + <style name="Theme.Base" parent="android:Theme.Holo.Light" /> + +</resources> diff --git a/samples/browseable/PdfRendererBasic/res/values-v21/template-styles.xml b/samples/browseable/PdfRendererBasic/res/values-v21/template-styles.xml new file mode 100644 index 000000000..134fcd9d3 --- /dev/null +++ b/samples/browseable/PdfRendererBasic/res/values-v21/template-styles.xml @@ -0,0 +1,22 @@ +<!-- + Copyright 2014 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. + --> + +<resources> + + <!-- Activity themes --> + <style name="Theme.Base" parent="android:Theme.Material.Light" /> + +</resources> diff --git a/samples/browseable/PdfRendererBasic/res/values/base-strings.xml b/samples/browseable/PdfRendererBasic/res/values/base-strings.xml new file mode 100644 index 000000000..d97b3c6a8 --- /dev/null +++ b/samples/browseable/PdfRendererBasic/res/values/base-strings.xml @@ -0,0 +1,28 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Copyright 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. +--> +<resources> + <string name="app_name">PdfRendererBasic</string> + <string name="intro_message"> + <![CDATA[ + + + This sample demonstrates how to use PdfRenderer to display PDF documents on the screen. + + + ]]> + </string> +</resources> diff --git a/samples/browseable/PdfRendererBasic/res/values/strings.xml b/samples/browseable/PdfRendererBasic/res/values/strings.xml new file mode 100644 index 000000000..e0182fd39 --- /dev/null +++ b/samples/browseable/PdfRendererBasic/res/values/strings.xml @@ -0,0 +1,24 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + Copyright (C) 2014 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. +--> +<resources> + + <string name="app_name_with_index">PdfRendererBasic (%1$d/%2$d)</string> + <string name="info">Info</string> + <string name="previous">Previous</string> + <string name="next">Next</string> + +</resources> diff --git a/samples/browseable/PdfRendererBasic/res/values/template-dimens.xml b/samples/browseable/PdfRendererBasic/res/values/template-dimens.xml new file mode 100644 index 000000000..39e710b5c --- /dev/null +++ b/samples/browseable/PdfRendererBasic/res/values/template-dimens.xml @@ -0,0 +1,32 @@ +<!-- + Copyright 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. + --> + +<resources> + + <!-- Define standard dimensions to comply with Holo-style grids and rhythm. --> + + <dimen name="margin_tiny">4dp</dimen> + <dimen name="margin_small">8dp</dimen> + <dimen name="margin_medium">16dp</dimen> + <dimen name="margin_large">32dp</dimen> + <dimen name="margin_huge">64dp</dimen> + + <!-- Semantic definitions --> + + <dimen name="horizontal_page_margin">@dimen/margin_medium</dimen> + <dimen name="vertical_page_margin">@dimen/margin_medium</dimen> + +</resources> diff --git a/samples/browseable/PdfRendererBasic/res/values/template-styles.xml b/samples/browseable/PdfRendererBasic/res/values/template-styles.xml new file mode 100644 index 000000000..6e7d593dd --- /dev/null +++ b/samples/browseable/PdfRendererBasic/res/values/template-styles.xml @@ -0,0 +1,42 @@ +<!-- + Copyright 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. + --> + +<resources> + + <!-- Activity themes --> + + <style name="Theme.Base" parent="android:Theme.Light" /> + + <style name="Theme.Sample" parent="Theme.Base" /> + + <style name="AppTheme" parent="Theme.Sample" /> + <!-- Widget styling --> + + <style name="Widget" /> + + <style name="Widget.SampleMessage"> + <item name="android:textAppearance">?android:textAppearanceMedium</item> + <item name="android:lineSpacingMultiplier">1.1</item> + </style> + + <style name="Widget.SampleMessageTile"> + <item name="android:background">@drawable/tile</item> + <item name="android:shadowColor">#7F000000</item> + <item name="android:shadowDy">-3.5</item> + <item name="android:shadowRadius">2</item> + </style> + +</resources> diff --git a/samples/browseable/PdfRendererBasic/src/com.example.android.pdfrendererbasic/MainActivity.java b/samples/browseable/PdfRendererBasic/src/com.example.android.pdfrendererbasic/MainActivity.java new file mode 100644 index 000000000..6b7e8b454 --- /dev/null +++ b/samples/browseable/PdfRendererBasic/src/com.example.android.pdfrendererbasic/MainActivity.java @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2014 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.pdfrendererbasic; + +import android.app.Activity; +import android.app.AlertDialog; +import android.os.Bundle; +import android.view.Menu; +import android.view.MenuItem; + +public class MainActivity extends Activity { + + public static final String FRAGMENT_PDF_RENDERER_BASIC = "pdf_renderer_basic"; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main_real); + if (savedInstanceState == null) { + getFragmentManager().beginTransaction() + .add(R.id.container, new PdfRendererBasicFragment(), + FRAGMENT_PDF_RENDERER_BASIC) + .commit(); + } + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.main, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case R.id.action_info: + new AlertDialog.Builder(this) + .setMessage(R.string.intro_message) + .setPositiveButton(android.R.string.ok, null) + .show(); + return true; + } + return super.onOptionsItemSelected(item); + } +} diff --git a/samples/browseable/PdfRendererBasic/src/com.example.android.pdfrendererbasic/PdfRendererBasicFragment.java b/samples/browseable/PdfRendererBasic/src/com.example.android.pdfrendererbasic/PdfRendererBasicFragment.java new file mode 100644 index 000000000..e413c6599 --- /dev/null +++ b/samples/browseable/PdfRendererBasic/src/com.example.android.pdfrendererbasic/PdfRendererBasicFragment.java @@ -0,0 +1,221 @@ +/* + * Copyright (C) 2014 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.pdfrendererbasic; + +import android.app.Activity; +import android.app.Fragment; +import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.pdf.PdfRenderer; +import android.os.Bundle; +import android.os.ParcelFileDescriptor; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; +import android.widget.ImageView; +import android.widget.Toast; + +import java.io.IOException; + +/** + * This fragment has a big {@ImageView} that shows PDF pages, and 2 {@link android.widget.Button}s to move between + * pages. We use a {@link android.graphics.pdf.PdfRenderer} to render PDF pages as {@link android.graphics.Bitmap}s. + */ +public class PdfRendererBasicFragment extends Fragment implements View.OnClickListener { + + /** + * Key string for saving the state of current page index. + */ + private static final String STATE_CURRENT_PAGE_INDEX = "current_page_index"; + + /** + * File descriptor of the PDF. + */ + private ParcelFileDescriptor mFileDescriptor; + + /** + * {@link android.graphics.pdf.PdfRenderer} to render the PDF. + */ + private PdfRenderer mPdfRenderer; + + /** + * Page that is currently shown on the screen. + */ + private PdfRenderer.Page mCurrentPage; + + /** + * {@link android.widget.ImageView} that shows a PDF page as a {@link android.graphics.Bitmap} + */ + private ImageView mImageView; + + /** + * {@link android.widget.Button} to move to the previous page. + */ + private Button mButtonPrevious; + + /** + * {@link android.widget.Button} to move to the next page. + */ + private Button mButtonNext; + + public PdfRendererBasicFragment() { + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + return inflater.inflate(R.layout.fragment_pdf_renderer_basic, container, false); + } + + @Override + public void onViewCreated(View view, Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + // Retain view references. + mImageView = (ImageView) view.findViewById(R.id.image); + mButtonPrevious = (Button) view.findViewById(R.id.previous); + mButtonNext = (Button) view.findViewById(R.id.next); + // Bind events. + mButtonPrevious.setOnClickListener(this); + mButtonNext.setOnClickListener(this); + // Show the first page by default. + int index = 0; + // If there is a savedInstanceState (screen orientations, etc.), we restore the page index. + if (null != savedInstanceState) { + index = savedInstanceState.getInt(STATE_CURRENT_PAGE_INDEX, 0); + } + showPage(index); + } + + @Override + public void onAttach(Activity activity) { + super.onAttach(activity); + try { + openRenderer(activity); + } catch (IOException e) { + e.printStackTrace(); + Toast.makeText(activity, "Error! " + e.getMessage(), Toast.LENGTH_SHORT).show(); + activity.finish(); + } + } + + @Override + public void onDetach() { + try { + closeRenderer(); + } catch (IOException e) { + e.printStackTrace(); + } + super.onDetach(); + } + + @Override + public void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + if (null != mCurrentPage) { + outState.putInt(STATE_CURRENT_PAGE_INDEX, mCurrentPage.getIndex()); + } + } + + /** + * Sets up a {@link android.graphics.pdf.PdfRenderer} and related resources. + */ + private void openRenderer(Context context) throws IOException { + // In this sample, we read a PDF from the assets directory. + mFileDescriptor = context.getAssets().openFd("sample.pdf").getParcelFileDescriptor(); + // This is the PdfRenderer we use to render the PDF. + mPdfRenderer = new PdfRenderer(mFileDescriptor); + } + + /** + * Closes the {@link android.graphics.pdf.PdfRenderer} and related resources. + * + * @throws java.io.IOException When the PDF file cannot be closed. + */ + private void closeRenderer() throws IOException { + if (null != mCurrentPage) { + mCurrentPage.close(); + } + mPdfRenderer.close(); + mFileDescriptor.close(); + } + + /** + * Shows the specified page of PDF to the screen. + * + * @param index The page index. + */ + private void showPage(int index) { + if (mPdfRenderer.getPageCount() <= index) { + return; + } + // Make sure to close the current page before opening another one. + if (null != mCurrentPage) { + mCurrentPage.close(); + } + // Use `openPage` to open a specific page in PDF. + mCurrentPage = mPdfRenderer.openPage(index); + // Important: the destination bitmap must be ARGB (not RGB). + Bitmap bitmap = Bitmap.createBitmap(mCurrentPage.getWidth(), mCurrentPage.getHeight(), + Bitmap.Config.ARGB_8888); + // Here, we render the page onto the Bitmap. + // To render a portion of the page, use the second and third parameter. Pass nulls to get + // the default result. + // Pass either RENDER_MODE_FOR_DISPLAY or RENDER_MODE_FOR_PRINT for the last parameter. + mCurrentPage.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY); + // We are ready to show the Bitmap to user. + mImageView.setImageBitmap(bitmap); + updateUi(); + } + + /** + * Updates the state of 2 control buttons in response to the current page index. + */ + private void updateUi() { + int index = mCurrentPage.getIndex(); + int pageCount = mPdfRenderer.getPageCount(); + mButtonPrevious.setEnabled(0 != index); + mButtonNext.setEnabled(index + 1 < pageCount); + getActivity().setTitle(getString(R.string.app_name_with_index, index + 1, pageCount)); + } + + /** + * Gets the number of pages in the PDF. This method is marked as public for testing. + * + * @return The number of pages. + */ + public int getPageCount() { + return mPdfRenderer.getPageCount(); + } + + @Override + public void onClick(View view) { + switch (view.getId()) { + case R.id.previous: { + // Move to the previous page + showPage(mCurrentPage.getIndex() - 1); + break; + } + case R.id.next: { + // Move to the next page + showPage(mCurrentPage.getIndex() + 1); + break; + } + } + } + +} |