Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added error handling for corrupted pdf files. #134

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ local.properties
*.swp

# gradle
.gradle
.gradle
build
.DS_Store
2 changes: 1 addition & 1 deletion android-pdfview-sample/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
android:theme="@style/AppTheme"
android:label="@string/app_name" >
<activity
android:name="com.joanzapata.PDFViewActivity_"
android:name="com.joanzapata.PDFViewActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
2 changes: 1 addition & 1 deletion android-pdfview-sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description = 'android-pdfview-sample'

android {
compileSdkVersion 19
buildToolsVersion "19.0.3"
buildToolsVersion "19.1.0"

defaultConfig {
minSdkVersion 8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,68 +18,61 @@
*/
package com.joanzapata;

import android.content.Intent;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.googlecode.androidannotations.annotations.*;

import com.joanzapata.pdfview.PDFView;
import com.joanzapata.pdfview.listener.OnErrorOccurredListener;
import com.joanzapata.pdfview.listener.OnLoadCompleteListener;
import com.joanzapata.pdfview.listener.OnPageChangeListener;
import com.joanzapata.pdfview.sample.R;

import static java.lang.String.format;

@EActivity(R.layout.activity_main)
@OptionsMenu(R.menu.actionbar)
public class PDFViewActivity extends SherlockActivity implements OnPageChangeListener {
public class PDFViewActivity extends Activity implements OnPageChangeListener, OnLoadCompleteListener,
OnErrorOccurredListener {

public static final String SAMPLE_FILE = "sample.pdf";

public static final String ABOUT_FILE = "about.pdf";

@ViewById
PDFView pdfView;

@NonConfigurationInstance
String pdfName = SAMPLE_FILE;

@NonConfigurationInstance
Integer pageNumber = 1;

@AfterViews
void afterViews() {
display(pdfName, false);
}

@OptionsItem
public void about() {
if (!displaying(ABOUT_FILE))
display(ABOUT_FILE, true);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

private void display(String assetFileName, boolean jumpToFirstPage) {
if (jumpToFirstPage) pageNumber = 1;
setTitle(pdfName = assetFileName);
pdfView = (PDFView)findViewById(R.id.pdfView);

pdfView.fromAsset(assetFileName)
pdfView.fromAsset(SAMPLE_FILE)
.defaultPage(pageNumber)
.onPageChange(this)
.onErrorOccured(this)
.load();
}

PDFView pdfView;


String pdfName = SAMPLE_FILE;


Integer pageNumber = 1;

@Override
public void onPageChanged(int page, int pageCount) {
pageNumber = page;
setTitle(format("%s %s / %s", pdfName, page, pageCount));
}

@Override
public void onBackPressed() {
if (ABOUT_FILE.equals(pdfName)) {
display(SAMPLE_FILE, true);
} else {
super.onBackPressed();
}
public void loadComplete(int nbPages) {
Log.d("AS ", "f");
}

@Override
public void errorOccured() {
Log.d("error ", "A");
}

private boolean displaying(String fileName) {
Expand Down
2 changes: 1 addition & 1 deletion android-pdfview/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description = 'android-pdfview'

android {
compileSdkVersion 19
buildToolsVersion '19.0.3'
buildToolsVersion '19.1.0'

sourceSets {
main {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.vudroid.core.DecodeServiceBase;
import org.vudroid.pdfdroid.codec.PdfContext;

class DecodingAsyncTask extends AsyncTask<Void, Void, Void> {
class DecodingAsyncTask extends AsyncTask<Void, Void, Boolean> {

/** The decode service used for decoding the PDF */
private DecodeService decodeService;
Expand All @@ -46,15 +46,24 @@ public DecodingAsyncTask(Uri uri, PDFView pdfView) {
}

@Override
protected Void doInBackground(Void... params) {
protected Boolean doInBackground(Void... params) {
decodeService = new DecodeServiceBase(new PdfContext());
decodeService.setContentResolver(pdfView.getContext().getContentResolver());
decodeService.open(uri);
return null;

try {
decodeService.open(uri);
} catch (Exception e) {
return true;
}

return false;
}

protected void onPostExecute(Void result) {
if (!cancelled) {
protected void onPostExecute(Boolean isErrorOccured) {

if (isErrorOccured) {
pdfView.errorOccurred();
} else if (!isErrorOccured && !cancelled) {
pdfView.loadComplete(decodeService);
}
}
Expand Down
21 changes: 21 additions & 0 deletions android-pdfview/src/main/java/com/joanzapata/pdfview/PDFView.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import android.view.SurfaceView;
import com.joanzapata.pdfview.exception.FileNotFoundException;
import com.joanzapata.pdfview.listener.OnDrawListener;
import com.joanzapata.pdfview.listener.OnErrorOccurredListener;
import com.joanzapata.pdfview.listener.OnLoadCompleteListener;
import com.joanzapata.pdfview.listener.OnPageChangeListener;
import com.joanzapata.pdfview.model.PagePart;
Expand Down Expand Up @@ -151,6 +152,9 @@ public class PDFView extends SurfaceView {
/** Call back object to call when the page has changed */
private OnPageChangeListener onPageChangeListener;

/** Call back object to call when error occurs when opening PDF */
private OnErrorOccurredListener onErrorOccurredListener;

/** Call back object to call when the above layer is to drawn */
private OnDrawListener onDrawListener;

Expand Down Expand Up @@ -291,6 +295,10 @@ private void setOnPageChangeListener(OnPageChangeListener onPageChangeListener)
this.onPageChangeListener = onPageChangeListener;
}

private void setOnErrorOccuredListener(OnErrorOccurredListener onErrorOccurredListener) {
this.onErrorOccurredListener = onErrorOccurredListener;
}

private void setOnDrawListener(OnDrawListener onDrawListener) {
this.onDrawListener = onDrawListener;
}
Expand Down Expand Up @@ -657,6 +665,11 @@ public void loadComplete(DecodeService decodeService) {
}
}

/** Called when error occurs while loading PDF */
public void errorOccurred() {
onErrorOccurredListener.errorOccured();
}

/**
* Called when a rendering task is over and
* a PagePart has been freshly created.
Expand Down Expand Up @@ -995,6 +1008,8 @@ public class Configurator {

private OnPageChangeListener onPageChangeListener;

private OnErrorOccurredListener onErrorOccurredListener;

private int defaultPage = 1;

private boolean showMinimap = false;
Expand Down Expand Up @@ -1039,6 +1054,11 @@ public Configurator onPageChange(OnPageChangeListener onPageChangeListener) {
return this;
}

public Configurator onErrorOccured(OnErrorOccurredListener onErrorOccurredListener) {
this.onErrorOccurredListener = onErrorOccurredListener;
return this;
}

public Configurator defaultPage(int defaultPage) {
this.defaultPage = defaultPage;
return this;
Expand All @@ -1064,6 +1084,7 @@ public void load() {
PDFView.this.recycle();
PDFView.this.setOnDrawListener(onDrawListener);
PDFView.this.setOnPageChangeListener(onPageChangeListener);
PDFView.this.setOnErrorOccuredListener(onErrorOccurredListener);
PDFView.this.enableSwipe(enableSwipe);
PDFView.this.enableDoubletap(enableDoubletap);
PDFView.this.setDefaultPage(defaultPage);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.joanzapata.pdfview.listener;

/**
* Created by Milan on 2015-07-15.
*/
public interface OnErrorOccurredListener {

void errorOccured();
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.+'
classpath 'com.android.tools.build:gradle:1.2.3'
}
}

Expand Down
23 changes: 2 additions & 21 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,25 +1,6 @@
#
# Copyright 2014 Joan Zapata
#
# This file is part of Android-pdfview.
#
# Android-pdfview is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Android-pdfview is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Android-pdfview. If not, see <http://www.gnu.org/licenses/>.
#

#Fri May 02 16:31:46 BST 2014
#Wed Jul 15 08:45:14 EDT 2015
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=http\://services.gradle.org/distributions/gradle-1.11-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip