replace activity by fragment

This commit is contained in:
contrudar 2016-10-04 23:08:25 +03:00
parent 1aa70452fe
commit 2ebb34f41f
4 changed files with 223 additions and 160 deletions

View File

@ -17,5 +17,6 @@ android {
}
dependencies {
provided "com.android.support:appcompat-v7:24.2.1"
compile project(':libraries:Android-Pdf-Viewer-Library:gestureimageview')
}

View File

@ -6,16 +6,20 @@ import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.RectF;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
@ -42,13 +46,16 @@ import net.sf.andpdf.pdfviewer.gui.FullScrollView;
import net.sf.andpdf.refs.HardReference;
import java.io.IOException;
import java.util.Locale;
import static android.content.Context.INPUT_METHOD_SERVICE;
/**
* 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 {
public class PdfViewerFragment extends Fragment {
private static final int STARTPAGE = 1;
private static final float STARTZOOM = 1.0f;
@ -75,7 +82,7 @@ public class PdfViewerActivity extends Activity {
private final static int MENU_BACK = 6;
private final static int MENU_CLEANUP = 7;
private final static int DIALOG_PAGENUM = 1;
public static final String DIALOG_FRAGMENT_TAG_MARK = "DIALOG_FRAGMENT";
private GraphView mOldGraphView;
private GraphView mGraphView;
@ -90,6 +97,90 @@ public class PdfViewerActivity extends Activity {
private Thread backgroundThread;
private Handler uiHandler;
private boolean passwordNeeded;
private String password;
@Nullable
@Override
public View onCreateView(final LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable final Bundle savedInstanceState) {
Log.i(TAG, "onCreate");
uiHandler = new Handler();
restoreInstance();
progress = ProgressDialog.show(getActivity(), "Loading", "Loading PDF Page", true, true);
if (mOldGraphView != null) {
mGraphView = new GraphView(getActivity());
mGraphView.mBi = mOldGraphView.mBi;
mOldGraphView = null;
mGraphView.pdfView.setImageBitmap(mGraphView.mBi);
mGraphView.updateTexts();
return mGraphView;
} else {
mGraphView = new GraphView(getActivity());
if (savedInstanceState != null) {
PDFImage.sShowImages = savedInstanceState.getBoolean(PdfViewerFragment.EXTRA_SHOWIMAGES, PdfViewerFragment.DEFAULTSHOWIMAGES);
PDFPaint.s_doAntiAlias = savedInstanceState.getBoolean(PdfViewerFragment.EXTRA_ANTIALIAS, PdfViewerFragment.DEFAULTANTIALIAS);
PDFFont.sUseFontSubstitution = savedInstanceState.getBoolean(PdfViewerFragment.EXTRA_USEFONTSUBSTITUTION,
PdfViewerFragment.DEFAULTUSEFONTSUBSTITUTION);
}
HardReference.sKeepCaches = true;
mPage = STARTPAGE;
mZoom = STARTZOOM;
return setContent(password);
}
}
@Override
public void onViewCreated(final View view, @Nullable final Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (view == null) {
getFragmentManager().popBackStack();
return;
}
if (!passwordNeeded) {
startRenderThread(mPage, mZoom);
} else {
hideProgressBar();
final EditText etPW = (EditText) view.findViewById(getPdfPasswordEditField());
Button btOK = (Button) view.findViewById(getPdfPasswordOkButton());
Button btExit = (Button) view.findViewById(getPdfPasswordExitButton());
btOK.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
password = etPW.getText().toString();
getFragmentManager()
.beginTransaction()
.detach(PdfViewerFragment.this)
.attach(PdfViewerFragment.this)
.commit();
hideSoftInput();
}
});
btExit.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
hideSoftInput();
getFragmentManager().popBackStack();
}
});
}
}
@Nullable
private View setContent(@Nullable final String password) {
try {
openFile(byteArray, password);
passwordNeeded = false;
return mGraphView;
} catch (PDFAuthenticationFailureException e) {
passwordNeeded = true;
return LayoutInflater.from(getActivity()).inflate(R.layout.pdf_file_password, null);
} catch (Exception ex) {
Log.e(TAG, "an unexpected exception occurred");
}
return null;
}
/**
* restore member variables from previously saved instance
*
@ -99,8 +190,8 @@ public class PdfViewerActivity extends Activity {
private boolean restoreInstance() {
mOldGraphView = null;
Log.e(TAG, "restoreInstance");
if (getLastNonConfigurationInstance() == null) return false;
PdfViewerActivity inst = (PdfViewerActivity) getLastNonConfigurationInstance();
if (getActivity().getLastNonConfigurationInstance() == null) return false;
PdfViewerFragment inst = (PdfViewerFragment) getActivity().getLastNonConfigurationInstance();
if (inst != this) {
Log.e(TAG, "restoring Instance");
mOldGraphView = inst.mGraphView;
@ -113,67 +204,6 @@ public class PdfViewerActivity extends Activity {
return true;
}
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "onCreate");
uiHandler = new Handler();
restoreInstance();
if (mOldGraphView != null) {
mGraphView = new GraphView(this);
mGraphView.mBi = mOldGraphView.mBi;
mOldGraphView = null;
mGraphView.pdfView.setImageBitmap(mGraphView.mBi);
mGraphView.updateTexts();
setContentView(mGraphView);
} else {
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);
}
}
private void setContent(String password) {
try {
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");
}
}
private synchronized void startRenderThread(final int page, final float zoom) {
if (backgroundThread != null) return;
backgroundThread = new Thread(new Runnable() {
@ -205,17 +235,18 @@ public class PdfViewerActivity extends Activity {
}, 1000);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
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;
if (HardReference.sKeepCaches) {
menu.add(Menu.NONE, MENU_CLEANUP, Menu.NONE, "Clear Caches");
}
}
/**
@ -246,7 +277,7 @@ public class PdfViewerActivity extends Activity {
break;
}
case MENU_BACK: {
finish();
getFragmentManager().popBackStack();
break;
}
case MENU_CLEANUP: {
@ -303,7 +334,7 @@ public class PdfViewerActivity extends Activity {
mPage += 1;
mGraphView.bZoomOut.setEnabled(true);
mGraphView.bZoomIn.setEnabled(true);
progress = ProgressDialog.show(PdfViewerActivity.this, "Loading", "Loading PDF Page " + mPage, true, true);
progress = ProgressDialog.show(getActivity(), "Loading", "Loading PDF Page " + mPage, true, true);
startRenderThread(mPage, mZoom);
}
}
@ -315,7 +346,7 @@ public class PdfViewerActivity extends Activity {
mPage -= 1;
mGraphView.bZoomOut.setEnabled(true);
mGraphView.bZoomIn.setEnabled(true);
progress = ProgressDialog.show(PdfViewerActivity.this, "Loading", "Loading PDF Page " + mPage, true, true);
progress = ProgressDialog.show(getActivity(), "Loading", "Loading PDF Page " + mPage, true, true);
startRenderThread(mPage, mZoom);
}
}
@ -323,60 +354,87 @@ public class PdfViewerActivity extends Activity {
private void gotoPage() {
if (mPdfFile != null) {
showDialog(DIALOG_PAGENUM);
final Bundle bundle = new Bundle();
showDialogFragment(new GoToPageDialogFragment(), bundle);
}
}
@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() {
/**
* Hides device keyboard that is showing over {@link Activity}.
* Do NOT use it if keyboard is over {@link android.app.Dialog} - it won't work as they have different {@link Activity#getWindow()}.
*/
public void hideSoftInput() {
if (getActivity().getCurrentFocus() == null) {
return;
}
final InputMethodManager inputManager = (InputMethodManager) getActivity().getSystemService(INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
getActivity().getWindow().getDecorView().requestFocus();
}
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;
public class GoToPageDialogFragment extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {
LayoutInflater factory = LayoutInflater.from(getActivity());
final View pagenumView = factory.inflate(R.layout.dialog_pagenumber, null);
final EditText edPagenum = (EditText) pagenumView.findViewById(R.id.pagenum_edit);
edPagenum.setText(String.format(Locale.getDefault(), "%d", 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) getActivity().getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edPagenum.getWindowToken(), 0);
}
});
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);
}
return true;
}
});
return new AlertDialog.Builder(getActivity())
//.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) {
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
if ((pageNum != mPage) && (pageNum >= 1) && (pageNum <= mPdfFile.getNumPages())) {
mPage = pageNum;
mGraphView.bZoomOut.setEnabled(true);
mGraphView.bZoomIn.setEnabled(true);
progress = ProgressDialog.show(getActivity(), "Loading", "Loading PDF Page " + mPage, true, true);
startRenderThread(mPage, mZoom);
}
})
.create();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
})
.create();
}
return null;
}
public void showDialogFragment(@NonNull final DialogFragment dialogFragment, @Nullable final Bundle bundle) {
if (getFragmentManager().getFragments() != null) {
for (final Fragment fragment : getFragmentManager().getFragments()) {
// to fix bug with opening fragment several times on fast clicking
if (fragment != null && fragment.getClass() == dialogFragment.getClass()) {
return;
}
}
}
if (bundle != null) {
dialogFragment.setArguments(bundle);
}
dialogFragment.show(getFragmentManager(), dialogFragment.getClass().getName() + ";" + DIALOG_FRAGMENT_TAG_MARK);
}
private class GraphView extends FullScrollView {
public Bitmap mBi;
@ -400,10 +458,6 @@ public class PdfViewerActivity extends Activity {
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;
@ -540,7 +594,7 @@ public class PdfViewerActivity extends Activity {
}
private void showPage(int page, float zoom) throws Exception {
private void showPage(int page, float zoom) {
try {
// free memory from previous page
@ -558,11 +612,16 @@ public class PdfViewerActivity extends Activity {
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);
}
hideProgressBar();
}
private void hideProgressBar() {
if (progress != null) {
progress.dismiss();
}
}
/**
@ -586,7 +645,7 @@ public class PdfViewerActivity extends Activity {
mPdfFile = new PDFFile(bb, new PDFPassword(password));
}
} else {
Toast.makeText(this, "The error occurred", Toast.LENGTH_LONG).show();
Toast.makeText(getActivity(), "The error occurred", Toast.LENGTH_LONG).show();
}
}
@ -612,14 +671,6 @@ public class PdfViewerActivity extends Activity {
return R.drawable.zoom_out;
}
private int getPdfPasswordLayoutResource() {
return R.layout.pdf_file_password;
}
private int getPdfPageNumberResource() {
return R.layout.dialog_pagenumber;
}
private int getPdfPasswordEditField() {
return R.id.etPassword;
}
@ -632,8 +683,4 @@ public class PdfViewerActivity extends Activity {
return R.id.btExit;
}
private int getPdfPageNumberEditField() {
return R.id.pagenum_edit;
}
}

View File

@ -20,32 +20,47 @@
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- filename -->
<!-- filename -->
<TextView
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_weight="0"
android:paddingBottom="0dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Password protected PDF-File:"/>
<EditText
<EditText
android:id="@+id/etPassword"
android:layout_width="fill_parent"
android:layout_height="60px"
android:text=""
android:layout_weight="10"
android:hint="please enter password for PDF">
<requestFocus />
android:layout_height="60dp"
android:layout_weight="10"
android:hint="please enter password for PDF"
android:inputType="textPassword">
<requestFocus/>
</EditText>
<!-- Buttons 'OK' + 'Exit' -->
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:orientation="horizontal" >
<Button android:id="@+id/btExit" android:layout_width="120px" android:layout_height="40px" android:text="Exit"></Button>
<Button android:id="@+id/btOK" android:layout_width="120px" android:layout_height="40px" android:text="OK"></Button>
</LinearLayout>
<!-- Buttons 'OK' + 'Exit' -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="@+id/btExit"
android:layout_width="120dp"
android:layout_height="40dp"
android:text="Exit"/>
<Button
android:id="@+id/btOK"
android:layout_width="120dp"
android:layout_height="40dp"
android:text="OK"/>
</LinearLayout>
</LinearLayout>

View File

@ -1,2 +1,2 @@
include ':PdfView', ':pdfviewsample'
include ':gestureimageview'
include ':gestureimageview'