Professional Documents
Culture Documents
pdfviewer;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import net.sf.andpdf.nio.ByteBuffer;
import com.model.PdfAnno;
import com.model.PdfReport;
import com.pdfviewer.FullScrollView;
import net.sf.andpdf.refs.HardReference;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFImage;
import com.sun.pdfview.PDFPage;
import com.sun.pdfview.PDFPaint;
import com.sun.pdfview.decrypt.PDFAuthenticationFailureException;
import com.sun.pdfview.decrypt.PDFPassword;
import com.test.R;
import com.test.StageListActivity;
import com.utils.FileUtils;
/**
* U:\Android\android-sdk-windows-1.5_r1\tools\adb push
u:\Android\simple_T.pdf
* /data/test.pdf
*
* @author ferenc.hechler
*/
public class PdfViewerActivity extends Activity {
@Override
public Object onRetainNonConfigurationInstance() {
// return a reference to the current instance
Log.e(TAG, "onRetainNonConfigurationInstance");
return this;
}
/**
* restore member variables from previously saved instance
*
* @see onRetainNonConfigurationInstance
* @return true if instance to restore from was found
*/
private boolean restoreInstance() {
mOldGraphView = null;
Log.e(TAG, "restoreInstance");
if (getLastNonConfigurationInstance() == null)
return false;
PdfViewerActivity inst = (PdfViewerActivity)
getLastNonConfigurationInstance();
if (inst != this) {
Log.e(TAG, "restoring Instance");
mOldGraphView = inst.mGraphView;
mPage = inst.mPage;
mPdfFile = inst.mPdfFile;
mPdfPage = inst.mPdfPage;
mTmpFile = inst.mTmpFile;
mZoom = inst.mZoom;
pdffilename = inst.pdffilename;
backgroundThread = inst.backgroundThread;
// mGraphView.invalidate();
}
return true;
}
// boolean showImages =
//
getIntent().getBooleanExtra(PdfFileSelectActivity.EXTRA_SHOWIMAGES,
// PdfFileSelectActivity.DEFAULTSHOWIMAGES);
PDFImage.sShowImages = true;
// boolean antiAlias =
//
getIntent().getBooleanExtra(PdfFileSelectActivity.EXTRA_ANTIALIAS,
// PdfFileSelectActivity.DEFAULTANTIALIAS);
PDFPaint.s_doAntiAlias = true;
// boolean keepCaches =
//
getIntent().getBooleanExtra(PdfFileSelectActivity.EXTRA_KEEPCACHES,
// PdfFileSelectActivity.DEFAULTKEEPCACHES);
HardReference.sKeepCaches = false;
if (intent != null) {
if
("android.intent.action.VIEW".equals(intent.getAction())) {
pdffilename =
storeUriContentToFile(intent.getData());
} else {
pdffilename =
getIntent().getStringExtra(
StageListActivity.EXTRA_PDFFILENAME);
}
}
if (pdffilename == null)
pdffilename = "no file selected";
mPage = STARTPAGE;
mZoom = STARTZOOM;
setContent(null);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(Menu.NONE, MEN_PREV_PAGE, Menu.NONE, "Previous
Page");
menu.add(Menu.NONE, MEN_NEXT_PAGE, Menu.NONE, "Next Page");
menu.add(Menu.NONE, MEN_GOTO_PAGE, Menu.NONE, "Goto Page");
menu.add(Menu.NONE, MEN_ZOOM_OUT, Menu.NONE, "Zoom Out");
menu.add(Menu.NONE, MEN_ZOOM_IN, Menu.NONE, "Zoom In");
menu.add(Menu.NONE, MEN_BACK, Menu.NONE, "Back");
menu.add(Menu.NONE, MEN_SAVE, Menu.NONE, "Save");
if (HardReference.sKeepCaches)
menu.add(Menu.NONE, MEN_CLEANUP, Menu.NONE, "Clear
Caches");
return true;
}
/**
* Called when a menu item is selected.
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case MEN_NEXT_PAGE: {
nextPage();
break;
}
case MEN_PREV_PAGE: {
prevPage();
break;
}
case MEN_GOTO_PAGE: {
gotoPage();
break;
}
case MEN_ZOOM_IN: {
zoomIn();
break;
}
case MEN_ZOOM_OUT: {
zoomOut();
break;
}
case MEN_BACK: {
finish();
break;
}
case MEN_CLEANUP: {
HardReference.cleanup();
break;
}
case MEN_SAVE: {
save();
}
}
return true;
}
Toast.makeText(getApplicationContext(),
"UPLOAD SUCCESS!", Toast.LENGTH_SHORT)
.show();
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_PAGENUM:
LayoutInflater factory = LayoutInflater.from(this);
final View pagenumView = factory.inflate(
R.layout.dialog_pagenumber, null);
final EditText edPagenum = (EditText) pagenumView
.findViewById(R.id.pagenum_edit);
edPagenum.setText(Integer.toString(mPage));
return new
AlertDialog.Builder(this).setIcon(R.drawable.icon)
.setTitle("Jump to
page").setView(pagenumView)
.setPositiveButton("OK",
new
DialogInterface.OnClickListener() {
public void
onClick(DialogInterface dialog,
int whichButton) {
String
strPagenum = edPagenum.getText()
.
toString();
int
pageNum = mPage;
try {
pageNum = Integer.parseInt(strPagenum);
} catch
(NumberFormatException ignore) {
}
if
((pageNum != mPage)
.getNumPages())) {
mPage = pageNum;
startRenderThread(mPage, mZoom);
}
}
}).setNegativeButton("Ca
ncel",
new
DialogInterface.OnClickListener() {
public void
onClick(DialogInterface dialog,
int whichButton) {
}
}).create();
case DIALOG_ANNOTATION:
final EditText comment=new
EditText(getApplicationContext());
comment.setWidth(250);
comment.setHeight(150);
return new AlertDialog.Builder(this).setTitle("Nouvelle
Annotation")
.setView(comment)
.setPositiveButton("OK",
new
DialogInterface.OnClickListener() {
public void
onClick(DialogInterface dialog,
int whichButton) {
float
x=mGraphView.getXPercentage();
float
y=mGraphView.getYPercentage();
String
content=comment.getText().toString();
PdfAnno
anno=new PdfAnno();
anno.setX(x);
anno.setY(y);
anno.setContent(content);
anno.setPage(mPage);
annotations.add(anno);
int whichButton) {
}
}).create();
}
return null;
}
// layout params
LinearLayout.LayoutParams lpWrap1 = new
LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 1);
LinearLayout.LayoutParams lpWrap10 = new
LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 10);
// vertical layout
LinearLayout vl = new LinearLayout(context);
vl.setLayoutParams(lpWrap10);
vl.setOrientation(LinearLayout.VERTICAL);
/*
* mLine1 = "PDF Viewer initializing"; mLine1View = new
* TextView(context);
mLine1View.setLayoutParams(lpWrap1);
* mLine1View.setText(mLine1);
mLine1View.setTextColor(Color.BLACK);
* vl.addView(mLine1View);
*
* mLine2 = "unknown number of pages"; mLine2View = new
* TextView(context);
mLine2View.setLayoutParams(lpWrap1);
* mLine2View.setText(mLine2);
mLine2View.setTextColor(Color.BLACK);
* vl.addView(mLine2View);
*
* mLine3 = "unknown timestamps"; mLine3View = new
* TextView(context);
mLine3View.setLayoutParams(lpWrap1);
* mLine3View.setText(mLine3);
mLine3View.setTextColor(Color.BLACK);
* vl.addView(mLine3View);
*/
addNavButtons(vl);
// remember page button for updates
mBtPage2 = mBtPage;
// addNavButtons(vl);
setLayoutParams(new
LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT, 100));
setBackgroundColor(Color.LTGRAY);
setHorizontalScrollBarEnabled(true);
setHorizontalFadingEdgeEnabled(true);
setVerticalScrollBarEnabled(true);
setVerticalFadingEdgeEnabled(true);
addView(vl);
addSpace(vg, 6, 6);
LinearLayout.LayoutParams lpChild1 = new
LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 1);
LinearLayout.LayoutParams lpWrap10 = new
LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 10);
// zoom in button
Button bZoomIn = new Button(context);
bZoomIn.setLayoutParams(lpChild1);
bZoomIn.setText("+");
bZoomIn.setWidth(40);
bZoomIn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
zoomIn();
}
});
hl.addView(bZoomIn);
addSpace(hl, 6, 6);
// prev button
Button bPrev = new Button(context);
bPrev.setLayoutParams(lpChild1);
bPrev.setText("<");
bPrev.setWidth(40);
bPrev.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
prevPage();
}
});
hl.addView(bPrev);
// page button
mBtPage = new Button(context);
mBtPage.setLayoutParams(lpChild1);
String maxPage = ((mPdfFile == null) ? "?" : Integer
.toString(mPdfFile.getNumPages()));
mBtPage.setText(mPage + "/" + maxPage);
mBtPage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
gotoPage();
}
});
hl.addView(mBtPage);
// next button
Button bNext = new Button(context);
bNext.setLayoutParams(lpChild1);
bNext.setText(">");
bNext.setWidth(40);
bNext.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
nextPage();
}
});
hl.addView(bNext);
// exit button
Button bExit = new Button(context);
bExit.setLayoutParams(lpChild1);
bExit.setText("Back");
bExit.setWidth(60);
bExit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
hl.addView(bExit);
vg.addView(hl);
addSpace(vg, 6, 6);
}
paint.setStrokeWidth(3);
paint.setTextSize(50);
paint.setColor(Color.BLACK);
String pdfname=pdffilename.split("/")[2];
can.drawText("Bienvenue! PDF "+pdfname+" will
show here later ", 50, 400, paint);
}
}
mBtPage.setText(mPdfPage.getPageNumber() + "/"
+
mPdfFile.getNumPages());
if (mBtPage2 != null)
mBtPage2.setText(mPdfPage.getPageNumber() + "/"
+
mPdfFile.getNumPages());
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
mCurDown = action == MotionEvent.ACTION_DOWN
|| action == MotionEvent.ACTION_MOVE;
mCurX=event.getX();
mCurY=event.getY();
showDialog(DIALOG_ANNOTATION);
}
/*
int N = event.getHistorySize();
for (int i = 0; i < N; i++) {
Log.i("TouchPaint", "Intermediate pointer #" +
i);
drawPoint(event.getHistoricalX(i),
event.getHistoricalY(i),
event.getHistoricalPressure(i),
event
.getHistoricalSi
ze(i));
}
drawPoint(event.getX(), event.getY(),
event.getPressure(), event
.getSize());
*/
return true;
}
/**
* <p>
* Open a specific pdf file. Creates a DocumentInfo from the file, and
opens
* that.
* </p>
*
* <p>
* <b>Note:</b> Mapping the file locks the file until the PDFFile is
closed.
* </p>
*
* @param file
* the file to open
* @throws IOException
*/
public void openFile(File file, String password) throws IOException {
// first open the file for random access
RandomAccessFile raf = new RandomAccessFile(file, "r");
mGraphView.showText("Anzahl Seiten:" +
mPdfFile.getNumPages());
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mTmpFile != null) {
mTmpFile.delete();
mTmpFile = null;
}
}