diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..242d039 --- /dev/null +++ b/.gitignore @@ -0,0 +1,34 @@ +# Built application files +*.apk +*.ap_ + +# Files for the Dalvik VM +*.dex + +# Java class files +*.class + +# Generated files +bin/ +gen/ + +# Gradle files +.gradle/ +build/ + +# Local configuration file (sdk path, etc) +local.properties + +# Log Files +*.log + +# Android Studio Navigation editor temp files +.navigation/ + +# Android Studio captures folder +captures/ + +.gradle +.idea +.DS_Store +*.iml \ No newline at end of file diff --git a/Android-Pdf-Viewer-Library.iml b/Android-Pdf-Viewer-Library.iml index e6b0607..ebceb9f 100644 --- a/Android-Pdf-Viewer-Library.iml +++ b/Android-Pdf-Viewer-Library.iml @@ -1,5 +1,5 @@ - + diff --git a/PdfView/PdfView.iml b/PdfView/PdfView.iml index 3587982..7ef96b0 100644 --- a/PdfView/PdfView.iml +++ b/PdfView/PdfView.iml @@ -1,9 +1,9 @@ - + - @@ -48,6 +48,7 @@ + @@ -55,6 +56,7 @@ + @@ -62,6 +64,7 @@ + @@ -69,6 +72,7 @@ + @@ -76,16 +80,19 @@ + + - + + diff --git a/PdfView/build.gradle b/PdfView/build.gradle index fef3f0b..42c3c52 100644 --- a/PdfView/build.gradle +++ b/PdfView/build.gradle @@ -17,5 +17,5 @@ android { } dependencies { - compile project(':gestureimageview') + compile project(':libraries:Android-Pdf-Viewer-Library:gestureimageview') } \ No newline at end of file diff --git a/PdfView/src/main/java/net/sf/andpdf/pdfviewer/PdfViewerActivity.java b/PdfView/src/main/java/net/sf/andpdf/pdfviewer/PdfViewerActivity.java index 951c217..7492a16 100644 --- a/PdfView/src/main/java/net/sf/andpdf/pdfviewer/PdfViewerActivity.java +++ b/PdfView/src/main/java/net/sf/andpdf/pdfviewer/PdfViewerActivity.java @@ -10,9 +10,7 @@ import android.content.Intent; import android.graphics.Bitmap; import android.graphics.Color; import android.graphics.RectF; -import android.net.Uri; import android.os.Bundle; -import android.os.Environment; import android.os.Handler; import android.util.Log; import android.view.KeyEvent; @@ -29,6 +27,7 @@ import android.widget.ImageButton; 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; @@ -40,832 +39,601 @@ import com.sun.pdfview.font.PDFFont; import net.sf.andpdf.nio.ByteBuffer; import net.sf.andpdf.pdfviewer.gui.FullScrollView; -import net.sf.andpdf.pdfviewer.gui.PdfView; import net.sf.andpdf.refs.HardReference; -import java.io.File; -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; /** * U:\Android\android-sdk-windows-1.5_r1\tools\adb push u:\Android\simple_T.pdf /data/test.pdf * * @author ferenc.hechler */ -public abstract class PdfViewerActivity extends Activity { +public class PdfViewerActivity extends Activity { - private static final int STARTPAGE = 1; - private static final float STARTZOOM = 1.0f; + private static final int STARTPAGE = 1; + private static final float STARTZOOM = 1.0f; - private static final float MIN_ZOOM = 0.25f; - private static final float MAX_ZOOM = 3.0f; - private static final float ZOOM_INCREMENT = 1.5f; + private static final float MIN_ZOOM = 0.25f; + private static final float MAX_ZOOM = 3.0f; + private static final float ZOOM_INCREMENT = 1.5f; - private static final String TAG = "PDFVIEWER"; + private static final String TAG = "PDFVIEWER"; - public static final String EXTRA_PDFFILENAME = "net.sf.andpdf.extra.PDFFILENAME"; - public static final String EXTRA_SHOWIMAGES = "net.sf.andpdf.extra.SHOWIMAGES"; - public static final String EXTRA_ANTIALIAS = "net.sf.andpdf.extra.ANTIALIAS"; - public static final String EXTRA_USEFONTSUBSTITUTION = "net.sf.andpdf.extra.USEFONTSUBSTITUTION"; - public static final String EXTRA_KEEPCACHES = "net.sf.andpdf.extra.KEEPCACHES"; + public static final String EXTRA_SHOWIMAGES = "net.sf.andpdf.extra.SHOWIMAGES"; + public static final String EXTRA_ANTIALIAS = "net.sf.andpdf.extra.ANTIALIAS"; + public static final String EXTRA_USEFONTSUBSTITUTION = "net.sf.andpdf.extra.USEFONTSUBSTITUTION"; - public static final boolean DEFAULTSHOWIMAGES = true; - public static final boolean DEFAULTANTIALIAS = true; - public static final boolean DEFAULTUSEFONTSUBSTITUTION = false; - public static final boolean DEFAULTKEEPCACHES = false; + public static final boolean DEFAULTSHOWIMAGES = true; + public static final boolean DEFAULTANTIALIAS = true; + public static final boolean DEFAULTUSEFONTSUBSTITUTION = false; - private final static int MENU_NEXT_PAGE = 1; - private final static int MENU_PREV_PAGE = 2; - private final static int MENU_GOTO_PAGE = 3; - private final static int MENU_ZOOM_IN = 4; - private final static int MENU_ZOOM_OUT = 5; - private final static int MENU_BACK = 6; - private final static int MENU_CLEANUP = 7; + private final static int MENU_NEXT_PAGE = 1; + private final static int MENU_PREV_PAGE = 2; + private final static int MENU_GOTO_PAGE = 3; + private final static int MENU_ZOOM_IN = 4; + private final static int MENU_ZOOM_OUT = 5; + private final static int MENU_BACK = 6; + private final static int MENU_CLEANUP = 7; - private final static int DIALOG_PAGENUM = 1; + private final static int DIALOG_PAGENUM = 1; - private GraphView mOldGraphView; - private GraphView mGraphView; - private String pdffilename; - private PDFFile mPdfFile; - private int mPage; - private float mZoom; - private File mTmpFile; - private ProgressDialog progress; + private GraphView mOldGraphView; + private GraphView mGraphView; + private PDFFile mPdfFile; + public static byte[] byteArray; + private int mPage; + private float mZoom; + private ProgressDialog progress; - /*private View navigationPanel; - private Handler closeNavigationHandler; - private Thread closeNavigationThread;*/ + private PDFPage mPdfPage; - private PDFPage mPdfPage; + private Thread backgroundThread; + private Handler uiHandler; - private Thread backgroundThread; - private Handler uiHandler; - - @Override public Object onRetainNonConfigurationInstance() { - // return a reference to the current instance - Log.e(TAG, "onRetainNonConfigurationInstance"); - return this; - } - - /** - * restore member variables from previously saved instance - * - * @return true if instance to restore from was found - * @see - */ - 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(); + /** + * restore member variables from previously saved instance + * + * @return true if instance to restore from was found + * @see + */ + 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; + mZoom = inst.mZoom; + backgroundThread = inst.backgroundThread; + } + return true; } - return true; - } - public abstract String getFileName(); + /** + * Called when the activity is first created. + */ + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + Log.i(TAG, "onCreate"); + uiHandler = new Handler(); + restoreInstance(); - /** - * Called when the activity is first created. - */ - @Override public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - pdffilename = getFileName(); - Log.i(TAG, "onCreate"); - uiHandler = new Handler(); - restoreInstance(); - if (mOldGraphView != null) { - mGraphView = new GraphView(this); - mGraphView.mBi = mOldGraphView.mBi; - mOldGraphView = null; - mGraphView.mImageView.setImageBitmap(mGraphView.mBi); - // mGraphView.updateTexts(); - setContentView(mGraphView); - } else { - mGraphView = new GraphView(this); - Intent intent = getIntent(); - Log.i(TAG, "" + intent); - - boolean showImages = - getIntent().getBooleanExtra(PdfViewerActivity.EXTRA_SHOWIMAGES, PdfViewerActivity.DEFAULTSHOWIMAGES); - PDFImage.sShowImages = showImages; - boolean antiAlias = - getIntent().getBooleanExtra(PdfViewerActivity.EXTRA_ANTIALIAS, PdfViewerActivity.DEFAULTANTIALIAS); - PDFPaint.s_doAntiAlias = antiAlias; - boolean useFontSubstitution = getIntent().getBooleanExtra(PdfViewerActivity.EXTRA_USEFONTSUBSTITUTION, - PdfViewerActivity.DEFAULTUSEFONTSUBSTITUTION); - PDFFont.sUseFontSubstitution = useFontSubstitution; - boolean keepCaches = - getIntent().getBooleanExtra(PdfViewerActivity.EXTRA_KEEPCACHES, PdfViewerActivity.DEFAULTKEEPCACHES); - HardReference.sKeepCaches = true; - - if (intent != null && pdffilename == null) { - if ("android.intent.action.VIEW".equals(intent.getAction())) { - pdffilename = storeUriContentToFile(intent.getData()); + if (mOldGraphView != null) { + mGraphView = new GraphView(this); + mGraphView.mBi = mOldGraphView.mBi; + mOldGraphView = null; + mGraphView.pdfView.setImageBitmap(mGraphView.mBi); + mGraphView.updateTexts(); + setContentView(mGraphView); } else { - pdffilename = getIntent().getStringExtra(PdfViewerActivity.EXTRA_PDFFILENAME); + mGraphView = new GraphView(this); + Intent intent = getIntent(); + Log.i(TAG, "" + intent); + + PDFImage.sShowImages = getIntent().getBooleanExtra(PdfViewerActivity.EXTRA_SHOWIMAGES, PdfViewerActivity.DEFAULTSHOWIMAGES); + PDFPaint.s_doAntiAlias = getIntent().getBooleanExtra(PdfViewerActivity.EXTRA_ANTIALIAS, PdfViewerActivity.DEFAULTANTIALIAS); + PDFFont.sUseFontSubstitution = getIntent().getBooleanExtra(PdfViewerActivity.EXTRA_USEFONTSUBSTITUTION, + PdfViewerActivity.DEFAULTUSEFONTSUBSTITUTION); + HardReference.sKeepCaches = true; + + mPage = STARTPAGE; + mZoom = STARTZOOM; + + setContent(null); } - } - - if (pdffilename == null) pdffilename = "no file selected"; - - mPage = STARTPAGE; - mZoom = STARTZOOM; - - setContent(null); } - } - private void setContent(String password) { - try { - parsePDF(pdffilename, password); - pdfView.setmPdfFile(mPdfFile); - setContentView(mGraphView); - startRenderThread(mPage, mZoom); - } catch (PDFAuthenticationFailureException e) { - setContentView(getPdfPasswordLayoutResource()); - final EditText etPW = (EditText) findViewById(getPdfPasswordEditField()); - Button btOK = (Button) findViewById(getPdfPasswordOkButton()); - Button btExit = (Button) findViewById(getPdfPasswordExitButton()); - btOK.setOnClickListener(new OnClickListener() { - public void onClick(View v) { - String pw = etPW.getText().toString(); - setContent(pw); - } - }); - btExit.setOnClickListener(new OnClickListener() { - public void onClick(View v) { - finish(); - } - }); - } - } - - private synchronized void startRenderThread(final int page, final float zoom) { - if (backgroundThread != null) return; - // mGraphView.showText("reading page " + page + ", zoom:" + zoom); - //progress = ProgressDialog.show(PdfViewerActivity.this, "Loading", "Loading PDF Page"); - backgroundThread = new Thread(new Runnable() { - public void run() { + private void setContent(String password) { try { - if (mPdfFile != null) { - showPage(page, zoom); - } - } catch (Exception e) { - Log.e(TAG, e.getMessage(), e); + openFile(byteArray, password); + setContentView(mGraphView); + startRenderThread(mPage, mZoom); + } catch (PDFAuthenticationFailureException e) { + setContentView(getPdfPasswordLayoutResource()); + final EditText etPW = (EditText) findViewById(getPdfPasswordEditField()); + Button btOK = (Button) findViewById(getPdfPasswordOkButton()); + Button btExit = (Button) findViewById(getPdfPasswordExitButton()); + btOK.setOnClickListener(new OnClickListener() { + public void onClick(View v) { + String pw = etPW.getText().toString(); + setContent(pw); + } + }); + btExit.setOnClickListener(new OnClickListener() { + public void onClick(View v) { + finish(); + } + }); + } catch (Exception ex) { + Log.e(TAG, "an unexpected exception occurred"); } - backgroundThread = null; - } - }); - updateImageStatus(); - backgroundThread.start(); - } - - private void updateImageStatus() { - // Log.i(TAG, "updateImageStatus: " + (System.currentTimeMillis()&0xffff)); - if (backgroundThread == null) { - // mGraphView.updateUi(); - - /*if (progress != null) - progress.dismiss();*/ - return; } - // mGraphView.updateUi(); - mGraphView.postDelayed(new Runnable() { - public void run() { - updateImageStatus(); - /*if (progress != null) - progress.dismiss();*/ - } - }, 1000); - } - - @Override public boolean onCreateOptionsMenu(Menu menu) { - super.onCreateOptionsMenu(menu); - menu.add(Menu.NONE, MENU_PREV_PAGE, Menu.NONE, "Previous Page").setIcon(getPreviousPageImageResource()); - menu.add(Menu.NONE, MENU_NEXT_PAGE, Menu.NONE, "Next Page").setIcon(getNextPageImageResource()); - menu.add(Menu.NONE, MENU_GOTO_PAGE, Menu.NONE, "Goto Page"); - menu.add(Menu.NONE, MENU_ZOOM_OUT, Menu.NONE, "Zoom Out").setIcon(getZoomOutImageResource()); - menu.add(Menu.NONE, MENU_ZOOM_IN, Menu.NONE, "Zoom In").setIcon(getZoomInImageResource()); - if (HardReference.sKeepCaches) menu.add(Menu.NONE, MENU_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 MENU_NEXT_PAGE: { - nextPage(); - break; - } - case MENU_PREV_PAGE: { - prevPage(); - break; - } - case MENU_GOTO_PAGE: { - gotoPage(); - break; - } - case MENU_ZOOM_IN: { - zoomIn(); - break; - } - case MENU_ZOOM_OUT: { - zoomOut(); - break; - } - case MENU_BACK: { - finish(); - break; - } - case MENU_CLEANUP: { - HardReference.cleanup(); - break; - } - } - return true; - } - - private void zoomIn() { - if (mPdfFile != null) { - if (mZoom < MAX_ZOOM) { - mZoom *= ZOOM_INCREMENT; - if (mZoom > MAX_ZOOM) mZoom = MAX_ZOOM; - - if (mZoom >= MAX_ZOOM) { - Log.d(TAG, "Disabling zoom in button"); - mGraphView.bZoomIn.setEnabled(false); - } else { - mGraphView.bZoomIn.setEnabled(true); - } - - mGraphView.bZoomOut.setEnabled(true); - - //progress = ProgressDialog.show(PdfViewerActivity.this, "Rendering", "Rendering PDF Page"); - startRenderThread(mPage, mZoom); - } - } - } - - private void zoomOut() { - if (mPdfFile != null) { - if (mZoom > MIN_ZOOM) { - mZoom /= ZOOM_INCREMENT; - if (mZoom < MIN_ZOOM) mZoom = MIN_ZOOM; - - if (mZoom <= MIN_ZOOM) { - Log.d(TAG, "Disabling zoom out button"); - mGraphView.bZoomOut.setEnabled(false); - } else { - mGraphView.bZoomOut.setEnabled(true); - } - - mGraphView.bZoomIn.setEnabled(true); - - //progress = ProgressDialog.show(PdfViewerActivity.this, "Rendering", "Rendering PDF Page"); - startRenderThread(mPage, mZoom); - } - } - } - - private void nextPage() { - if (mPdfFile != null) { - if (mPage < mPdfFile.getNumPages()) { - mPage += 1; - mGraphView.bZoomOut.setEnabled(true); - mGraphView.bZoomIn.setEnabled(true); - progress = ProgressDialog.show(PdfViewerActivity.this, "Loading", "Loading PDF Page " + mPage, true, true); - startRenderThread(mPage, mZoom); - } - } - } - - private void prevPage() { - if (mPdfFile != null) { - if (mPage > 1) { - mPage -= 1; - mGraphView.bZoomOut.setEnabled(true); - mGraphView.bZoomIn.setEnabled(true); - progress = ProgressDialog.show(PdfViewerActivity.this, "Loading", "Loading PDF Page " + mPage, true, true); - startRenderThread(mPage, mZoom); - } - } - } - - private void gotoPage() { - if (mPdfFile != null) { - showDialog(DIALOG_PAGENUM); - } - } - - @Override protected Dialog onCreateDialog(int id) { - switch (id) { - case DIALOG_PAGENUM: - LayoutInflater factory = LayoutInflater.from(this); - final View pagenumView = factory.inflate(getPdfPageNumberResource(), null); - final EditText edPagenum = (EditText) pagenumView.findViewById(getPdfPageNumberEditField()); - edPagenum.setText(Integer.toString(mPage)); - edPagenum.setOnEditorActionListener(new TextView.OnEditorActionListener() { - - public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { - if (event == null || (event.getAction() == 1)) { - // Hide the keyboard - InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); - imm.hideSoftInputFromWindow(edPagenum.getWindowToken(), 0); - } - return true; - } - }); - 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; + private synchronized void startRenderThread(final int page, final float zoom) { + if (backgroundThread != null) return; + backgroundThread = new Thread(new Runnable() { + public void run() { try { - pageNum = Integer.parseInt(strPagenum); - } catch (NumberFormatException ignore) { + if (mPdfFile != null) { + showPage(page, zoom); + } + } catch (Exception e) { + Log.e(TAG, e.getMessage(), e); } - if ((pageNum != mPage) && (pageNum >= 1) && (pageNum <= mPdfFile.getNumPages())) { - mPage = pageNum; - mGraphView.bZoomOut.setEnabled(true); - mGraphView.bZoomIn.setEnabled(true); - progress = - ProgressDialog.show(PdfViewerActivity.this, "Loading", "Loading PDF Page " + mPage, true, true); - startRenderThread(mPage, mZoom); + backgroundThread = null; + } + }); + updateImageStatus(); + backgroundThread.start(); + } + + private void updateImageStatus() { + if (backgroundThread == null) { + mGraphView.updateUi(); + return; + } + mGraphView.updateUi(); + mGraphView.postDelayed(new Runnable() { + public void run() { + updateImageStatus(); + } + }, 1000); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + super.onCreateOptionsMenu(menu); + menu.add(Menu.NONE, MENU_PREV_PAGE, Menu.NONE, "Previous Page").setIcon(getPreviousPageImageResource()); + menu.add(Menu.NONE, MENU_NEXT_PAGE, Menu.NONE, "Next Page").setIcon(getNextPageImageResource()); + menu.add(Menu.NONE, MENU_GOTO_PAGE, Menu.NONE, "Goto Page"); + menu.add(Menu.NONE, MENU_ZOOM_OUT, Menu.NONE, "Zoom Out").setIcon(getZoomOutImageResource()); + menu.add(Menu.NONE, MENU_ZOOM_IN, Menu.NONE, "Zoom In").setIcon(getZoomInImageResource()); + if (HardReference.sKeepCaches) menu.add(Menu.NONE, MENU_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 MENU_NEXT_PAGE: { + nextPage(); + break; + } + case MENU_PREV_PAGE: { + prevPage(); + break; + } + case MENU_GOTO_PAGE: { + gotoPage(); + break; + } + case MENU_ZOOM_IN: { + zoomIn(); + break; + } + case MENU_ZOOM_OUT: { + zoomOut(); + break; + } + case MENU_BACK: { + finish(); + break; + } + case MENU_CLEANUP: { + HardReference.cleanup(); + break; + } + } + return true; + } + + private void zoomIn() { + if (mPdfFile != null) { + if (mZoom < MAX_ZOOM) { + mZoom *= ZOOM_INCREMENT; + if (mZoom > MAX_ZOOM) mZoom = MAX_ZOOM; + + if (mZoom >= MAX_ZOOM) { + Log.d(TAG, "Disabling zoom in button"); + mGraphView.bZoomIn.setEnabled(false); + } else { + mGraphView.bZoomIn.setEnabled(true); } - } - }) - .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int whichButton) { - } - }) - .create(); - } - return null; - } - //TODO - PdfView pdfView; + mGraphView.bZoomOut.setEnabled(true); - private class GraphView extends FullScrollView { - //private String mText; - //private long fileMillis; - //private long pageParseMillis; - //private long pageRenderMillis; - private Bitmap mBi; - //private String mLine1; - //private String mLine2; - //private String mLine3; - private ImageView mImageView; - //private TextView mLine1View; - //private TextView mLine2View; - //private TextView mLine3View; - private Button mBtPage; - private Button mBtPage2; - - ImageButton bZoomOut; - ImageButton bZoomIn; - - public GraphView(Context context) { - super(context); - - //setContentView(R.layout.graphics_view); - // layout params - LinearLayout.LayoutParams lpWrap1 = - new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); - LinearLayout.LayoutParams lpWrap10 = - new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); - - LinearLayout.LayoutParams matchLp = - new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); - // vertical layout - LinearLayout vl = new LinearLayout(context); - vl.setLayoutParams(lpWrap10); - vl.setOrientation(LinearLayout.VERTICAL); - - if (mOldGraphView == null) { - progress = ProgressDialog.show(PdfViewerActivity.this, "Loading", "Loading PDF Page", true, true); - } - //TODO - pdfView = new PdfView(PdfViewerActivity.this); - - addNavButtons(vl); - // remember page button for updates - mBtPage2 = mBtPage; - - mImageView = new ImageView(context); - setPageBitmap(null); - updateImage(); - mImageView.setLayoutParams(lpWrap1); - // mImageView.setPadding(5, 5, 5, 5); - vl.addView(mImageView); - vl.addView(pdfView); - //addView(pdfView); - pdfView.setLayoutParams(matchLp); - - /*mImageView = (ImageView) findViewById(R.id.pdf_image); - if (mImageView == null) { - Log.i(TAG, "mImageView is null!!!!!!"); - } - setPageBitmap(null); - updateImage();*/ - - /* - navigationPanel = new ViewStub(PdfViewerActivity.this, R.layout.navigation_overlay); - final ImageButton previous = (ImageButton)navigationPanel.findViewById(R.id.navigation_previous); - previous.setBackgroundDrawable(null); - previous.setOnClickListener(new OnClickListener() { - - @Override - public void onClick(View v) { - prevPage(); - } - }); - - final ImageButton next = (ImageButton)navigationPanel.findViewById(R.id.navigation_next); - next.setBackgroundDrawable(null); - previous.setOnClickListener(new OnClickListener() { - - @Override - public void onClick(View v) { - nextPage(); - } - }); - - //stub.setLayoutParams(Layou) - vl.addView(navigationPanel); - - vl.setOnTouchListener(new OnTouchListener() { - - @Override - public boolean onTouch(View v, MotionEvent event) { - if (navigationPanel.getVisibility() != View.VISIBLE) { - navigationPanel.startAnimation(AnimationUtils.loadAnimation(PdfViewerActivity.this, - R.anim.slide_in)); - navigationPanel.setVisibility(View.VISIBLE); - } - - return false; - } - }); - */ - - //addNavButtons(vl); - - setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); - setBackgroundColor(Color.LTGRAY); - setHorizontalScrollBarEnabled(true); - setHorizontalFadingEdgeEnabled(true); - setVerticalScrollBarEnabled(true); - setVerticalFadingEdgeEnabled(true); - addView(vl); - } - - private void addNavButtons(ViewGroup vg) { - - // addSpace(vg, 6, 6); - - LinearLayout.LayoutParams lpChild1 = - new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); - LinearLayout.LayoutParams lpWrap10 = - new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); - - Context context = vg.getContext(); - LinearLayout hl = new LinearLayout(context); - hl.setLayoutParams(lpWrap10); - hl.setOrientation(LinearLayout.HORIZONTAL); - - // zoom out button - bZoomOut = new ImageButton(context); - bZoomOut.setBackgroundDrawable(null); - bZoomOut.setLayoutParams(lpChild1); - //bZoomOut.setText("-"); - //bZoomOut.setWidth(40); - bZoomOut.setImageResource(getZoomOutImageResource()); - bZoomOut.setOnClickListener(new OnClickListener() { - public void onClick(View v) { - zoomOut(); + startRenderThread(mPage, mZoom); + } } - }); - hl.addView(bZoomOut); + } - // zoom in button - bZoomIn = new ImageButton(context); - bZoomIn.setBackgroundDrawable(null); - bZoomIn.setLayoutParams(lpChild1); - //bZoomIn.setText("+"); - //bZoomIn.setWidth(40); - bZoomIn.setImageResource(getZoomInImageResource()); - bZoomIn.setOnClickListener(new OnClickListener() { - public void onClick(View v) { - zoomIn(); + private void zoomOut() { + if (mPdfFile != null) { + if (mZoom > MIN_ZOOM) { + mZoom /= ZOOM_INCREMENT; + if (mZoom < MIN_ZOOM) mZoom = MIN_ZOOM; + + if (mZoom <= MIN_ZOOM) { + Log.d(TAG, "Disabling zoom out button"); + mGraphView.bZoomOut.setEnabled(false); + } else { + mGraphView.bZoomOut.setEnabled(true); + } + + mGraphView.bZoomIn.setEnabled(true); + + startRenderThread(mPage, mZoom); + } } - }); - hl.addView(bZoomIn); + } - // addSpace(hl, 6, 6); - - // prev button - ImageButton bPrev = new ImageButton(context); - bPrev.setBackgroundDrawable(null); - bPrev.setLayoutParams(lpChild1); - //bPrev.setText("<"); - //bPrev.setWidth(40); - bPrev.setImageResource(getPreviousPageImageResource()); - bPrev.setOnClickListener(new OnClickListener() { - public void onClick(View v) { - prevPage(); + private void nextPage() { + if (mPdfFile != null) { + if (mPage < mPdfFile.getNumPages()) { + mPage += 1; + mGraphView.bZoomOut.setEnabled(true); + mGraphView.bZoomIn.setEnabled(true); + progress = ProgressDialog.show(PdfViewerActivity.this, "Loading", "Loading PDF Page " + mPage, true, true); + startRenderThread(mPage, mZoom); + } } - }); - hl.addView(bPrev); + } - // page button - mBtPage = new Button(context); - mBtPage.setLayoutParams(lpChild1); - String maxPage = ((mPdfFile == null) ? "0" : Integer.toString(mPdfFile.getNumPages())); - mBtPage.setText(mPage + "/" + maxPage); - mBtPage.setOnClickListener(new OnClickListener() { - public void onClick(View v) { - gotoPage(); + private void prevPage() { + if (mPdfFile != null) { + if (mPage > 1) { + mPage -= 1; + mGraphView.bZoomOut.setEnabled(true); + mGraphView.bZoomIn.setEnabled(true); + progress = ProgressDialog.show(PdfViewerActivity.this, "Loading", "Loading PDF Page " + mPage, true, true); + startRenderThread(mPage, mZoom); + } } - }); - hl.addView(mBtPage); + } - // next button - ImageButton bNext = new ImageButton(context); - bNext.setBackgroundDrawable(null); - bNext.setLayoutParams(lpChild1); - //bNext.setText(">"); - //bNext.setWidth(40); - bNext.setImageResource(getNextPageImageResource()); - bNext.setOnClickListener(new OnClickListener() { - public void onClick(View v) { - nextPage(); + private void gotoPage() { + if (mPdfFile != null) { + showDialog(DIALOG_PAGENUM); } - }); - hl.addView(bNext); - - // addSpace(hl, 20, 20); - - // 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); } - private void addSpace(ViewGroup vg, int width, int height) { - TextView tvSpacer = new TextView(vg.getContext()); - tvSpacer.setLayoutParams(new LinearLayout.LayoutParams(width, height, 1)); - tvSpacer.setText(""); - // tvSpacer.setWidth(width); - // tvSpacer.setHeight(height); - vg.addView(tvSpacer); - } + @Override + protected Dialog onCreateDialog(int id) { + switch (id) { + case DIALOG_PAGENUM: + LayoutInflater factory = LayoutInflater.from(this); + final View pagenumView = factory.inflate(getPdfPageNumberResource(), null); + final EditText edPagenum = (EditText) pagenumView.findViewById(getPdfPageNumberEditField()); + edPagenum.setText(Integer.toString(mPage)); + edPagenum.setOnEditorActionListener(new TextView.OnEditorActionListener() { - // private void updateUi() { - // uiHandler.post(new Runnable() { - // public void run() { - // updateTexts(); - // } - // }); - // } - - private void updateImage() { - uiHandler.post(new Runnable() { - public void run() { - mImageView.setImageBitmap(mBi); - - /*if (progress != null) - progress.dismiss();*/ + public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { + if (event == null || (event.getAction() == 1)) { + // Hide the keyboard + InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); + imm.hideSoftInputFromWindow(edPagenum.getWindowToken(), 0); + } + return true; + } + }); + 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) && (pageNum >= 1) && (pageNum <= mPdfFile.getNumPages())) { + mPage = pageNum; + mGraphView.bZoomOut.setEnabled(true); + mGraphView.bZoomIn.setEnabled(true); + progress = + ProgressDialog.show(PdfViewerActivity.this, "Loading", "Loading PDF Page " + mPage, true, true); + startRenderThread(mPage, mZoom); + } + } + }) + .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int whichButton) { + } + }) + .create(); } - }); + return null; } - private void setPageBitmap(Bitmap bi) { - if (bi != null) { - mBi = bi; - } else { - /* - mBi = Bitmap.createBitmap(100, 100, Config.RGB_565); - Canvas can = new Canvas(mBi); - can.drawColor(Color.RED); - Paint paint = new Paint(); - paint.setColor(Color.BLUE); - can.drawCircle(50, 50, 50, paint); + private class GraphView extends FullScrollView { + public Bitmap mBi; + public ImageView pdfView; + public Button mBtPage; + private Button mBtPage2; + + ImageButton bZoomOut; + ImageButton bZoomIn; + + public GraphView(Context context) { + super(context); + +// LinearLayout.LayoutParams matchLp = +// new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); + + 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); + + LinearLayout vl = new LinearLayout(context); + vl.setLayoutParams(lpWrap10); + vl.setOrientation(LinearLayout.VERTICAL); + + if (mOldGraphView == null) { + progress = ProgressDialog.show(PdfViewerActivity.this, "Loading", "Loading PDF Page", true, true); + } + + addNavButtons(vl); + // remember page button for updates + mBtPage2 = mBtPage; + + pdfView = new ImageView(context); + setPageBitmap(null); + updateImage(); + pdfView.setLayoutParams(lpWrap1); + pdfView.setPadding(5, 5, 5, 5); + vl.addView(pdfView); + + setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 100)); + setBackgroundColor(Color.LTGRAY); + setHorizontalScrollBarEnabled(true); + setHorizontalFadingEdgeEnabled(true); + setVerticalScrollBarEnabled(true); + setVerticalFadingEdgeEnabled(true); + addView(vl); + } + + private void addNavButtons(ViewGroup vg) { + + 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); + + Context context = vg.getContext(); + LinearLayout hl = new LinearLayout(context); + hl.setLayoutParams(lpWrap10); + hl.setOrientation(LinearLayout.HORIZONTAL); + + // zoom out button + bZoomOut = new ImageButton(context); + bZoomOut.setBackgroundDrawable(null); + bZoomOut.setLayoutParams(lpChild1); + bZoomOut.setImageResource(getZoomOutImageResource()); + bZoomOut.setOnClickListener(new OnClickListener() { + public void onClick(View v) { + zoomOut(); + } + }); + hl.addView(bZoomOut); + + // zoom in button + bZoomIn = new ImageButton(context); + bZoomIn.setBackgroundDrawable(null); + bZoomIn.setLayoutParams(lpChild1); + bZoomIn.setImageResource(getZoomInImageResource()); + bZoomIn.setOnClickListener(new OnClickListener() { + public void onClick(View v) { + zoomIn(); + } + }); + hl.addView(bZoomIn); + + // prev button + ImageButton bPrev = new ImageButton(context); + bPrev.setBackgroundDrawable(null); + bPrev.setLayoutParams(lpChild1); + //bPrev.setText("<"); + //bPrev.setWidth(40); + bPrev.setImageResource(getPreviousPageImageResource()); + bPrev.setOnClickListener(new OnClickListener() { + public void onClick(View v) { + prevPage(); + } + }); + hl.addView(bPrev); + + // page button + mBtPage = new Button(context); + mBtPage.setLayoutParams(lpChild1); + + mBtPage.setOnClickListener(new OnClickListener() { + public void onClick(View v) { + gotoPage(); + } + }); + hl.addView(mBtPage); + + // next button + ImageButton bNext = new ImageButton(context); + bNext.setBackgroundDrawable(null); + bNext.setLayoutParams(lpChild1); + bNext.setImageResource(getNextPageImageResource()); + bNext.setOnClickListener(new OnClickListener() { + public void onClick(View v) { + nextPage(); + } + }); + hl.addView(bNext); + + vg.addView(hl); + + addSpace(vg, 6, 6); + } + + private void addSpace(ViewGroup vg, int width, int height) { + TextView tvSpacer = new TextView(vg.getContext()); + tvSpacer.setLayoutParams(new LinearLayout.LayoutParams(width, height, 1)); + tvSpacer.setText(null); + vg.addView(tvSpacer); + } + + private void updateImage() { + uiHandler.post(new Runnable() { + public void run() { + pdfView.setImageBitmap(mBi); + } + }); + } + + private void updateUi() { + uiHandler.post(new Runnable() { + public void run() { + updateTexts(); + } + }); + } + + private void setPageBitmap(Bitmap bi) { + if (bi != null) { + mBi = bi; + } + } + + protected void updateTexts() { + if (mPdfPage != null) { + if (mBtPage != null) + mBtPage.setText(mPdfPage.getPageNumber() + "/" + mPdfFile.getNumPages()); + if (mBtPage2 != null) + mBtPage2.setText(mPdfPage.getPageNumber() + "/" + mPdfFile.getNumPages()); + } + } - paint.setStrokeWidth(0); - paint.setColor(Color.BLACK); - can.drawText("Bitmap", 10, 50, paint); - */ - } } - // protected void updateTexts() { - // - // if (mPdfPage != null) { - // if (mBtPage != null) - // mBtPage.setText(mPdfPage.getPageNumber() + "/" + mPdfFile.getNumPages()); - // if (mBtPage2 != null) - // mBtPage2.setText(mPdfPage.getPageNumber() + "/" + mPdfFile.getNumPages()); - // } - // } - } + private void showPage(int page, float zoom) throws Exception { + try { - private void showPage(int page, float zoom) throws Exception { - pdfView.showPage(page, zoom); - //long startTime = System.currentTimeMillis(); - //long middleTime = startTime; - try { + // free memory from previous page + mGraphView.setPageBitmap(null); + mGraphView.updateImage(); - // Only load the page if it's a different page (i.e. not just changing the zoom level) - if (mPdfPage == null || mPdfPage.getPageNumber() != page) { - mPdfPage = mPdfFile.getPage(page, true); - } - //int num = mPdfPage.getPageNumber(); - //int maxNum = mPdfFile.getNumPages(); - float width = mPdfPage.getWidth(); - float height = mPdfPage.getHeight(); - //String pageInfo= new File(pdffilename).getName() + " - " + num +"/"+maxNum+ ": " + width + "x" + height; - //mGraphView.showText(pageInfo); - //Log.i(TAG, pageInfo); - RectF clip = null; - //middleTime = System.currentTimeMillis(); - Bitmap bi = mPdfPage.getImage((int) (width * zoom), (int) (height * zoom), clip, true, true); - mGraphView.setPageBitmap(bi); - mGraphView.updateImage(); + // Only load the page if it's a different page (i.e. not just changing the zoom level) + if (mPdfPage == null || mPdfPage.getPageNumber() != page) { + mPdfPage = mPdfFile.getPage(page, true); + } - if (progress != null) progress.dismiss(); - } catch (Throwable e) { - Log.e(TAG, e.getMessage(), e); - // mGraphView.showText("Exception: " + e.getMessage()); + float width = mPdfPage.getWidth(); + float height = mPdfPage.getHeight(); + RectF clip = null; + Bitmap bi = mPdfPage.getImage((int) (width * zoom), (int) (height * zoom), clip, true, true); + mGraphView.setPageBitmap(bi); + mGraphView.updateImage(); + + if (progress != null) progress.dismiss(); + } catch (Throwable e) { + Log.e(TAG, e.getMessage(), e); + } } - //long stopTime = System.currentTimeMillis(); - //mGraphView.pageParseMillis = middleTime-startTime; - //mGraphView.pageRenderMillis = stopTime-middleTime; - } - private void parsePDF(String filename, String password) throws PDFAuthenticationFailureException { - //long startTime = System.currentTimeMillis(); - try { - File f = new File(filename); - long len = f.length(); - if (len == 0) { - // mGraphView.showText("file '" + filename + "' not found"); - } else { - // mGraphView.showText("file '" + filename + "' has " + len + " bytes"); - openFile(f, password); - } - } catch (PDFAuthenticationFailureException e) { - throw e; - } catch (Throwable e) { - e.printStackTrace(); - // mGraphView.showText("Exception: " + e.getMessage()); + /** + *

Open a specific pdf file. Creates a DocumentInfo from the file, + * and opens that.

+ *

+ *

Note: Mapping the file locks the file until the PDFFile + * is closed.

+ * + * @param byteArray the file to open + * @throws IOException + */ + public void openFile(final byte[] byteArray, String password) throws IOException { + if (byteArray != null) { + // now memory-map a byte-buffer + ByteBuffer bb = ByteBuffer.NEW(byteArray); + // create a PDFFile from the data + if (password == null) { + mPdfFile = new PDFFile(bb); + } else { + mPdfFile = new PDFFile(bb, new PDFPassword(password)); + } + } else { + Toast.makeText(this, "The error occurred", Toast.LENGTH_LONG).show(); + } } - //long stopTime = System.currentTimeMillis(); - //mGraphView.fileMillis = stopTime-startTime; - } - /** - *

Open a specific pdf file. Creates a DocumentInfo from the file, - * and opens that.

- *

- *

Note: Mapping the file locks the file until the PDFFile - * is closed.

- * - * @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"); - - // extract a file channel - FileChannel channel = raf.getChannel(); - - // now memory-map a byte-buffer - ByteBuffer bb = ByteBuffer.NEW(channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size())); - // create a PDFFile from the data - if (password == null) { - mPdfFile = new PDFFile(bb); - } else { - mPdfFile = new PDFFile(bb, new PDFPassword(password)); + @Override + public void onDestroy() { + super.onDestroy(); + byteArray = null; } - // mGraphView.showText("Anzahl Seiten:" + mPdfFile.getNumPages()); - } - - - /*private byte[] readBytes(File srcFile) throws IOException { - long fileLength = srcFile.length(); - int len = (int)fileLength; - byte[] result = new byte[len]; - FileInputStream fis = new FileInputStream(srcFile); - int pos = 0; - int cnt = fis.read(result, pos, len-pos); - while (cnt > 0) { - pos += cnt; - cnt = fis.read(result, pos, len-pos); - } - return result; - }*/ - private String storeUriContentToFile(Uri uri) { - String result = null; - try { - if (mTmpFile == null) { - File root = Environment.getExternalStorageDirectory(); - if (root == null) throw new Exception("external storage dir not found"); - mTmpFile = new File(root, "AndroidPdfViewer/AndroidPdfViewer_temp.pdf"); - mTmpFile.getParentFile().mkdirs(); - mTmpFile.delete(); - } else { - mTmpFile.delete(); - } - InputStream is = getContentResolver().openInputStream(uri); - OutputStream os = new FileOutputStream(mTmpFile); - byte[] buf = new byte[1024]; - int cnt = is.read(buf); - while (cnt > 0) { - os.write(buf, 0, cnt); - cnt = is.read(buf); - } - os.close(); - is.close(); - result = mTmpFile.getCanonicalPath(); - mTmpFile.deleteOnExit(); - } catch (Exception e) { - Log.e(TAG, e.getMessage(), e); + private int getPreviousPageImageResource() { + return R.drawable.left_arrow; } - return result; - } - @Override protected void onDestroy() { - super.onDestroy(); - if (mTmpFile != null) { - mTmpFile.delete(); - mTmpFile = null; + private int getNextPageImageResource() { + return R.drawable.right_arrow; } - } - /*private void postHideNavigation() { - // Start a time to hide the panel after 3 seconds - closeNavigationHandler.removeCallbacks(closeNavigationThread); - closeNavigationHandler.postDelayed(closeNavigationThread, 3000); - }*/ + private int getZoomInImageResource() { + return R.drawable.zoom_in; + } - public abstract int getPreviousPageImageResource(); // R.drawable.left_arrow + private int getZoomOutImageResource() { + return R.drawable.zoom_out; + } - public abstract int getNextPageImageResource(); // R.drawable.right_arrow + private int getPdfPasswordLayoutResource() { + return R.layout.pdf_file_password; + } - public abstract int getZoomInImageResource(); // R.drawable.zoom_int + private int getPdfPageNumberResource() { + return R.layout.dialog_pagenumber; + } - public abstract int getZoomOutImageResource(); // R.drawable.zoom_out + private int getPdfPasswordEditField() { + return R.id.etPassword; + } - public abstract int getPdfPasswordLayoutResource(); // R.layout.pdf_file_password + private int getPdfPasswordOkButton() { + return R.id.btOK; + } - public abstract int getPdfPageNumberResource(); // R.layout.dialog_pagenumber + private int getPdfPasswordExitButton() { + return R.id.btExit; + } - public abstract int getPdfPasswordEditField(); // R.id.etPassword + private int getPdfPageNumberEditField() { + return R.id.pagenum_edit; + } - public abstract int getPdfPasswordOkButton(); // R.id.btOK - - public abstract int getPdfPasswordExitButton(); // R.id.btExit - - public abstract int getPdfPageNumberEditField(); // R.id.pagenum_edit } \ No newline at end of file diff --git a/PdfView/src/main/java/net/sf/andpdf/pdfviewer/gui/PdfView.java b/PdfView/src/main/java/net/sf/andpdf/pdfviewer/gui/PdfView.java deleted file mode 100644 index 9df31eb..0000000 --- a/PdfView/src/main/java/net/sf/andpdf/pdfviewer/gui/PdfView.java +++ /dev/null @@ -1,293 +0,0 @@ -package net.sf.andpdf.pdfviewer.gui; - -import android.content.Context; -import android.graphics.Bitmap; -import android.graphics.Color; -import android.graphics.RectF; -import android.os.Handler; -import android.util.AttributeSet; -import android.util.DisplayMetrics; -import android.util.Log; -import android.view.View; -import android.view.ViewGroup; -import android.view.WindowManager; -import android.widget.Toast; -import com.polites.android.Direction; -import com.polites.android.GestureImageView; -import com.polites.android.GestureImageViewTouchListener; -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 java.io.File; -import java.io.IOException; -import java.io.RandomAccessFile; -import java.nio.channels.FileChannel; -import net.sf.andpdf.nio.ByteBuffer; -import net.sf.andpdf.refs.HardReference; - -public class PdfView extends FullScrollView { - - private static final int STARTPAGE = 1; - private static final float STARTZOOM = 1.0f; - - private static final float MIN_ZOOM = 0.25f; - private static final float MAX_ZOOM = 3.0f; - private static final float ZOOM_INCREMENT = 1.5f; - - private Bitmap mBi; - public GestureImageView mImageView; - private Handler uiHandler; - private PDFFile mPdfFile; - private PDFPage mPdfPage; - private Thread backgroundThread; - private int mPage; - private float mZoom; - - private boolean drawing = false; - - public PdfView(Context context) { - this(context, null); - } - - public PdfView(Context context, AttributeSet attrs) { - this(context, attrs, android.R.attr.scrollViewStyle); - } - - public PdfView(Context context, AttributeSet attrs, int defStyle) { - super(context, attrs, defStyle); - PDFImage.sShowImages = true; - PDFPaint.s_doAntiAlias = true; - HardReference.sKeepCaches = true; - uiHandler = new Handler(); - LayoutParams matchLp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); - mImageView = new GestureImageView(context); - mImageView.setLayoutParams(matchLp); - addView(mImageView); - setLayoutParams(matchLp); - setBackgroundColor(Color.LTGRAY); - setHorizontalScrollBarEnabled(true); - setHorizontalFadingEdgeEnabled(true); - setVerticalScrollBarEnabled(true); - setVerticalFadingEdgeEnabled(true); - mZoom = STARTZOOM; - mImageView.setOnClickListener(new OnClickListener() { - @Override public void onClick(View v) { - nextPage(); - } - }); - mImageView.setOnReachBoundListener(new GestureImageViewTouchListener.OnReachBoundListener() { - @Override public void onReach(View view, int direction) { - if (drawing) { - return; - } - switch (direction) { - case Direction.LEFT: - prevPage(); - break; - case Direction.RIGHT: - nextPage(); - break; - case Direction.TOP: - break; - case Direction.BOTTOM: - break; - } - } - }); - } - - public PDFFile getmPdfFile() { - return mPdfFile; - } - - public void setmPdfFile(PDFFile mPdfFile) { - this.mPdfFile = mPdfFile; - } - - private int getDeviceWidth() { - DisplayMetrics metric = new DisplayMetrics(); - WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE); - wm.getDefaultDisplay().getMetrics(metric); - return metric.widthPixels; // 屏幕宽度(像素) - } - - private int getDeviceHeight() { - DisplayMetrics metric = new DisplayMetrics(); - WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE); - wm.getDefaultDisplay().getMetrics(metric); - return metric.heightPixels; // 屏幕高度(像素) - } - - public void showPage(int page, float zoom) throws Exception { - try { - // free memory from previous page - updateImage(); - // Only load the page if it's a different page (i.e. not just changing the zoom level) - if (mPdfPage == null || mPdfPage.getPageNumber() != page) { - mPdfPage = mPdfFile.getPage(page, true); - } - float width = mPdfPage.getWidth(); - float height = mPdfPage.getHeight(); - if (getLayoutParams().height == ViewGroup.LayoutParams.MATCH_PARENT) { - height *= getDeviceWidth() / width; - } - if (getLayoutParams().width == LayoutParams.MATCH_PARENT) { - width = getDeviceWidth(); - } - RectF clip = null; - final Bitmap bi = mPdfPage.getImage((int) (width * zoom), (int) (height * zoom), clip, true, true); - setPageBitmap(bi); - updateImage(); - } catch (Throwable e) { - Log.e(TAG, e.getMessage(), e); - } - } - - private void updateImage() { - uiHandler.post(new Runnable() { - public void run() { - mImageView.reset(); - mImageView.setImageBitmap(mBi); - } - }); - } - - private void setPageBitmap(Bitmap bi) { - if (bi != null) { - mBi = bi; - } - } - - private void zoomIn() { - if (mPdfFile != null) { - if (mZoom < MAX_ZOOM) { - mZoom *= ZOOM_INCREMENT; - if (mZoom > MAX_ZOOM) mZoom = MAX_ZOOM; - startRenderThread(mPage, mZoom); - } - } - } - - private void zoomOut() { - if (mPdfFile != null) { - if (mZoom > MIN_ZOOM) { - mZoom /= ZOOM_INCREMENT; - if (mZoom < MIN_ZOOM) mZoom = MIN_ZOOM; - startRenderThread(mPage, mZoom); - } - } - } - - private void nextPage() { - if (mPdfFile != null) { - if (mPage < mPdfFile.getNumPages()) { - mPage += 1; - startRenderThread(mPage, mZoom); - } - } - } - - private void prevPage() { - if (mPdfFile != null) { - if (mPage > 1) { - mPage -= 1; - startRenderThread(mPage, mZoom); - } - } - } - - private void gotoPage() { - if (mPdfFile != null) { - // showDialog(DIALOG_PAGENUM); - } - } - - public synchronized void startRenderThread(final int page, final float zoom) { - if (backgroundThread != null) return; - backgroundThread = new Thread(new Runnable() { - public void run() { - drawing = true; - try { - if (mPdfFile != null) { - showPage(page, zoom); - } - } catch (Exception e) { - Log.e(TAG, e.getMessage(), e); - } - drawing = false; - backgroundThread = null; - } - }); - updateImageStatus(); - backgroundThread.start(); - } - - private void updateImageStatus() { - if (backgroundThread == null) { - return; - } - postDelayed(new Runnable() { - public void run() { - updateImageStatus(); - } - }, 1000); - } - - public void parsePDF(File f, String password) throws PDFAuthenticationFailureException { - try { - long len = f.length(); - if (len == 0) { - toastMessage("file '" + f.getName() + "' not found"); - } else { - toastMessage("file '" + f.getName() + "' has " + len + " bytes"); - openFile(f, password); - } - } catch (PDFAuthenticationFailureException e) { - throw e; - } catch (Throwable e) { - e.printStackTrace(); - toastMessage("Exception: " + e.getMessage()); - } - } - - public void parsePDF(String filename, String password) throws PDFAuthenticationFailureException { - try { - File f = new File(filename); - long len = f.length(); - if (len == 0) { - toastMessage("file '" + filename + "' not found"); - } else { - toastMessage("file '" + filename + "' has " + len + " bytes"); - openFile(f, password); - } - } catch (PDFAuthenticationFailureException e) { - throw e; - } catch (Throwable e) { - e.printStackTrace(); - toastMessage("Exception: " + e.getMessage()); - } - } - - public void openFile(File file, String password) throws IOException { - // first open the file for random access - RandomAccessFile raf = new RandomAccessFile(file, "r"); - // extract a file channel - FileChannel channel = raf.getChannel(); - // now memory-map a byte-buffer - ByteBuffer bb = ByteBuffer.NEW(channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size())); - // create a PDFFile from the data - if (password == null) { - mPdfFile = new PDFFile(bb); - } else { - mPdfFile = new PDFFile(bb, new PDFPassword(password)); - } - toastMessage("Anzahl Seiten:" + mPdfFile.getNumPages()); - } - - public void toastMessage(String msg) { - Toast.makeText(getContext(), msg, Toast.LENGTH_LONG).show(); - } -} diff --git a/PdfView/src/main/res/layout/graphics_view.xml b/PdfView/src/main/res/layout/graphics_view.xml index c12cd06..ec20711 100644 --- a/PdfView/src/main/res/layout/graphics_view.xml +++ b/PdfView/src/main/res/layout/graphics_view.xml @@ -1,14 +1,15 @@ - - - + xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="fill_parent" + android:layout_height="fill_parent" + android:orientation="vertical"> + + + diff --git a/build.gradle b/build.gradle index 739ceb5..4e6acd8 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,6 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:2.0.0' } } diff --git a/gestureimageview/gestureimageview.iml b/gestureimageview/gestureimageview.iml index 748fd66..fc66be5 100644 --- a/gestureimageview/gestureimageview.iml +++ b/gestureimageview/gestureimageview.iml @@ -1,9 +1,9 @@ - + - @@ -48,6 +48,7 @@ + @@ -55,6 +56,7 @@ + @@ -62,6 +64,7 @@ + @@ -69,6 +72,7 @@ + @@ -76,16 +80,19 @@ + + +