Skip to content
This repository was archived by the owner on Feb 4, 2024. It is now read-only.

Added the possibility to jump to a specific page #177

Open
wants to merge 2 commits 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
52 changes: 52 additions & 0 deletions app/src/main/java/com/gsnathan/pdfviewer/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import android.app.ActivityManager;
import android.app.Dialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
Expand All @@ -39,13 +40,17 @@
import android.preference.PreferenceManager;
import android.print.PrintManager;
import android.provider.OpenableColumns;
import android.text.InputType;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.WindowManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.result.ActivityResultLauncher;
Expand Down Expand Up @@ -446,6 +451,50 @@ void navToSettings() {
settingsLauncher.launch(new Intent(this, SettingsActivity.class));
}

private void goToPage(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getResources().getString(R.string.go_to_page));

EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
input.setGravity(Gravity.CENTER);

builder.setView(input);

builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
jumpToPage(Integer.parseInt(input.getText().toString()));
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});

builder.setOnKeyListener((dialog, keyCode, event) -> {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
dialog.cancel();
jumpToPage(Integer.parseInt(input.getText().toString()));
return true;
}
return false;
});
builder.show();
}

private void jumpToPage(int page){
if(viewBinding.pdfView.getPageCount() == 0){
return;
}
pageNumber = page - 1;
viewBinding.pdfView.jumpTo(pageNumber);
viewBinding.pdfView.performPageSnap();
setCurrentPage(pageNumber, viewBinding.pdfView.getPageCount());
}

private void setCurrentPage(int page, int pageCount) {
pageNumber = page;
setTitle(String.format("%s %s / %s", pdfFileName + " ", page + 1, pageCount));
Expand Down Expand Up @@ -521,6 +570,9 @@ public boolean onOptionsItemSelected(MenuItem item) {
case R.id.settings:
navToSettings();
return true;
case R.id.go_to_page:
goToPage();
return true;
default:
return super.onOptionsItemSelected(item);
}
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/gsnathan/pdfviewer/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ static void showLog(AppCompatActivity context) {
new WhatsNewItem("Full screen mode", "A new button has been added to the bottom bar to read PDFs in full screen!", R.drawable.star_icon),
new WhatsNewItem("Keep the screen on while reading", "You can enable this feature in Settings.", R.drawable.star_icon),
new WhatsNewItem("Sharing improvements and fixes", "Including better support for third-party share dialogs.", R.drawable.star_icon),
new WhatsNewItem("Bugs", "A bunch of bug fixes and robustness improvements.", R.drawable.star_icon)
new WhatsNewItem("Bugs", "A bunch of bug fixes and robustness improvements.", R.drawable.star_icon),
new WhatsNewItem("Jump to page", "You can now jump to a specific page", R.drawable.star_icon)
);
log.setTitleColor(Color.BLACK);
log.setTitleText(context.getResources().getString(R.string.appChangelog));
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/menu/menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/go_to_page"
android:orderInCategory="1"
android:title="@string/go_to_page"
app:showAsAction="never" />

<item
android:id="@+id/action_about"
android:orderInCategory="3"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-it/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<string name="toast_ssl_error">Connessione sicura fallita.</string>
<string name="saved_to_download">Il file è stato salvato nella cartella dei download.</string>
<string name="save_to_download_failed">Non è stato possibile salvare il file nella cartella dei download.</string>
<string name="go_to_page">Vai a pagina</string>
<string name="action_about">Informazioni su…</string>
<string name="version">Versione</string>
<string name="intro">Ripeti l\'introduzione</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-ru-rRU/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<string name="fling">Перелистывание страниц</string>

<!-- About -->
<string name="go_to_page">Перейти на страницу</string>
<string name="action_about">О приложении</string>
<string name="app_info">Информация о приложении</string>
<string name="intro">Повтор введения</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<string name="toast_ssl_error">Secure connection failed.</string>
<string name="saved_to_download">File saved to Download folder.</string>
<string name="save_to_download_failed">Couldn\'t save file to Download folder.</string>
<string name="go_to_page">Go to page</string>
<string name="action_about">About</string>
<string name="version">Version</string>
<string name="intro">Replay Intro</string>
Expand Down