Skip to content

Wake lock toggle #512

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

Open
wants to merge 3 commits into
base: main
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
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
android:targetSandboxVersion="2">
<original-package android:name="org.grapheneos.pdfviewer" />

<uses-permission android:name="android.permission.WAKE_LOCK" />

<application android:name=".App"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
Expand Down
104 changes: 100 additions & 4 deletions app/src/main/java/app/grapheneos/pdfviewer/PdfViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
Copy link
Preview

Copilot AI Jun 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The import 'android.view.WindowManager' is unused. Consider removing it to clean up imports.

Suggested change
import android.view.WindowManager;

Copilot uses AI. Check for mistakes.

import android.webkit.CookieManager;
import android.webkit.JavascriptInterface;
import android.webkit.RenderProcessGoneDetail;
Expand Down Expand Up @@ -65,6 +67,8 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
private static final String STATE_ZOOM_RATIO = "zoomRatio";
private static final String STATE_DOCUMENT_ORIENTATION_DEGREES = "documentOrientationDegrees";
private static final String STATE_ENCRYPTED_DOCUMENT_PASSWORD = "encrypted_document_password";
private static final String STATE_VERTICAL_SCROLL_MODE = "vertical_scroll_mode";
private static final String STATE_KEEP_SCREEN_ON = "keep_screen_on";
private static final String KEY_PROPERTIES = "properties";
private static final int MIN_WEBVIEW_RELEASE = 133;

Expand Down Expand Up @@ -130,6 +134,9 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
private String mEncryptedDocumentPassword;
private List<CharSequence> mDocumentProperties;
private InputStream mInputStream;
private boolean isVerticalScrollMode = false;
private boolean mKeepScreenOn = false;
private PowerManager.WakeLock mWakeLock;

private PdfviewerBinding binding;
private TextView mTextView;
Expand Down Expand Up @@ -265,6 +272,22 @@ public void onLoaded() {
public String getPassword() {
return mEncryptedDocumentPassword != null ? mEncryptedDocumentPassword : "";
}

@JavascriptInterface
public boolean isVerticalScrollMode() {
return isVerticalScrollMode;
}

@JavascriptInterface
public void onPageChanged(final int pageNumber) {
if (isVerticalScrollMode) {
mPage = pageNumber;
runOnUiThread(() -> {
invalidateOptionsMenu();
showPageNumber();
});
}
}
}

private void showWebViewCrashed() {
Expand All @@ -285,6 +308,11 @@ protected void onCreate(Bundle savedInstanceState) {
setSupportActionBar(binding.toolbar);
viewModel = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication())).get(PdfViewModel.class);

// Initialize wake lock
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
mWakeLock = powerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "PDFViewer:KeepScreenOn");
mWakeLock.setReferenceCounted(false);

viewModel.getOutline().observe(this, requested -> {
if (requested instanceof PdfViewModel.OutlineStatus.Requested) {
viewModel.setLoadingOutline();
Expand Down Expand Up @@ -502,8 +530,13 @@ public void onZoomEnd() {
mZoomRatio = savedInstanceState.getFloat(STATE_ZOOM_RATIO);
mDocumentOrientationDegrees = savedInstanceState.getInt(STATE_DOCUMENT_ORIENTATION_DEGREES);
mEncryptedDocumentPassword = savedInstanceState.getString(STATE_ENCRYPTED_DOCUMENT_PASSWORD);
isVerticalScrollMode = savedInstanceState.getBoolean(STATE_VERTICAL_SCROLL_MODE, false);
mKeepScreenOn = savedInstanceState.getBoolean(STATE_KEEP_SCREEN_ON, false);
}

// Apply wake lock state
updateWakeLock();

binding.webviewAlertReload.setOnClickListener(v -> {
webViewCrashed = false;
recreate();
Expand All @@ -530,6 +563,12 @@ private void purgeWebView() {
@Override
protected void onDestroy() {
super.onDestroy();

// Release wake lock if held
if (mWakeLock != null && mWakeLock.isHeld()) {
mWakeLock.release();
}

purgeWebView();
maybeCloseInputStream();
}
Expand Down Expand Up @@ -570,6 +609,9 @@ private void setToolbarTitleWithDocumentName() {
protected void onResume() {
super.onResume();

// Reapply wake lock state when returning to activity
updateWakeLock();

if (!webViewCrashed) {
// The user could have left the activity to update the WebView
invalidateOptionsMenu();
Expand All @@ -585,6 +627,16 @@ protected void onResume() {
}
}

@Override
protected void onPause() {
super.onPause();

// Release wake lock when activity is not visible to save battery
if (mWakeLock != null && mWakeLock.isHeld()) {
mWakeLock.release();
}
}

private int getWebViewRelease() {
PackageInfo webViewPackage = WebView.getCurrentWebViewPackage();
String webViewVersionName = webViewPackage.versionName;
Expand Down Expand Up @@ -675,12 +727,40 @@ private static void enableDisableMenuItem(MenuItem item, boolean enable) {
public void onJumpToPageInDocument(final int selected_page) {
if (selected_page >= 1 && selected_page <= mNumPages && mPage != selected_page) {
mPage = selected_page;
renderPage(0);
if (isVerticalScrollMode) {
binding.webview.evaluateJavascript("scrollToPageInDocument(" + selected_page + ")", null);
} else {
renderPage(0);
}
showPageNumber();
invalidateOptionsMenu();
}
}

private void toggleVerticalScrollMode() {
isVerticalScrollMode = !isVerticalScrollMode;
binding.webview.evaluateJavascript("setVerticalScrollMode(" + isVerticalScrollMode + ")", null);
invalidateOptionsMenu();
}

private void toggleKeepScreenOn() {
mKeepScreenOn = !mKeepScreenOn;
updateWakeLock();
invalidateOptionsMenu();
}

private void updateWakeLock() {
if (mKeepScreenOn) {
if (!mWakeLock.isHeld()) {
mWakeLock.acquire();
}
} else {
if (mWakeLock.isHeld()) {
mWakeLock.release();
}
}
}

private void showSystemUi() {
ViewKt.showSystemUi(binding.getRoot(), getWindow());
getSupportActionBar().show();
Expand All @@ -700,6 +780,8 @@ public void onSaveInstanceState(@NonNull Bundle savedInstanceState) {
savedInstanceState.putFloat(STATE_ZOOM_RATIO, mZoomRatio);
savedInstanceState.putInt(STATE_DOCUMENT_ORIENTATION_DEGREES, mDocumentOrientationDegrees);
savedInstanceState.putString(STATE_ENCRYPTED_DOCUMENT_PASSWORD, mEncryptedDocumentPassword);
savedInstanceState.putBoolean(STATE_VERTICAL_SCROLL_MODE, isVerticalScrollMode);
savedInstanceState.putBoolean(STATE_KEEP_SCREEN_ON, mKeepScreenOn);
}

private void showPageNumber() {
Expand Down Expand Up @@ -731,7 +813,7 @@ public boolean onPrepareOptionsMenu(@NonNull Menu menu) {
R.id.action_next, R.id.action_previous, R.id.action_first, R.id.action_last,
R.id.action_rotate_clockwise, R.id.action_rotate_counterclockwise,
R.id.action_view_document_properties, R.id.action_share, R.id.action_save_as,
R.id.action_outline));
R.id.action_outline, R.id.action_toggle_vertical_scroll, R.id.action_keep_screen_on));
if (BuildConfig.DEBUG) {
ids.add(R.id.debug_action_toggle_text_layer_visibility);
ids.add(R.id.debug_action_crash_webview);
Expand All @@ -757,14 +839,22 @@ public boolean onPrepareOptionsMenu(@NonNull Menu menu) {
enableDisableMenuItem(menu.findItem(R.id.action_open),
!webViewCrashed && getWebViewRelease() >= MIN_WEBVIEW_RELEASE);
enableDisableMenuItem(menu.findItem(R.id.action_share), mUri != null);
enableDisableMenuItem(menu.findItem(R.id.action_next), mPage < mNumPages);
enableDisableMenuItem(menu.findItem(R.id.action_previous), mPage > 1);

// In vertical scroll mode, disable page navigation buttons
enableDisableMenuItem(menu.findItem(R.id.action_next), !isVerticalScrollMode && mPage < mNumPages);
enableDisableMenuItem(menu.findItem(R.id.action_previous), !isVerticalScrollMode && mPage > 1);
enableDisableMenuItem(menu.findItem(R.id.action_first), !isVerticalScrollMode);
enableDisableMenuItem(menu.findItem(R.id.action_last), !isVerticalScrollMode);

enableDisableMenuItem(menu.findItem(R.id.action_save_as), mUri != null);
enableDisableMenuItem(menu.findItem(R.id.action_view_document_properties),
mDocumentProperties != null);

menu.findItem(R.id.action_outline).setVisible(viewModel.hasOutline());

// Set checked state for toggleable menu items
menu.findItem(R.id.action_keep_screen_on).setChecked(mKeepScreenOn);

if (webViewCrashed) {
for (final int id : ids) {
enableDisableMenuItem(menu.findItem(id), false);
Expand Down Expand Up @@ -822,6 +912,12 @@ public boolean onOptionsItemSelected(MenuItem item) {
return true;
} else if (itemId == R.id.action_save_as) {
saveDocument();
} else if (itemId == R.id.action_toggle_vertical_scroll) {
toggleVerticalScrollMode();
return true;
} else if (itemId == R.id.action_keep_screen_on) {
toggleKeepScreenOn();
return true;
} else if (itemId == R.id.debug_action_toggle_text_layer_visibility) {
binding.webview.evaluateJavascript("toggleTextLayerVisibility()", null);
return true;
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_screen_lock_portrait_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorOnSurface">
<path
android:fillColor="@android:color/white"
android:pathData="M6,2c-1.1,0 -2,0.9 -2,2v16c0,1.1 0.9,2 2,2h12c1.1,0 2,-0.9 2,-2L20,4c0,-1.1 -0.9,-2 -2,-2L6,2zM18,20L6,20L6,4h12v16zM9,19h6v-1L9,18v1zM15,7L9,7v10h6L15,7z"/>
</vector>
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_view_agenda_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorOnSurface">
<path
android:fillColor="@android:color/white"
android:pathData="M20,13H3c-0.55,0 -1,0.45 -1,1v6c0,0.55 0.45,1 1,1h17c0.55,0 1,-0.45 1,-1v-6c0,-0.55 -0.45,-1 -1,-1zM20,3H3c-0.55,0 -1,0.45 -1,1v6c0,0.55 0.45,1 1,1h17c0.55,0 1,-0.45 1,-1V4c0,-0.55 -0.45,-1 -1,-1z"/>
</vector>
13 changes: 13 additions & 0 deletions app/src/main/res/menu/pdf_viewer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@
android:title="@string/action_jump_to_page"
app:showAsAction="ifRoom" />

<item
android:id="@+id/action_toggle_vertical_scroll"
android:icon="@drawable/ic_view_agenda_24dp"
android:title="@string/action_toggle_vertical_scroll"
app:showAsAction="ifRoom" />

<item
android:id="@+id/action_rotate_clockwise"
android:icon="@drawable/ic_rotate_right_24dp"
Expand Down Expand Up @@ -77,4 +83,11 @@
android:title="@string/action_view_document_properties"
app:showAsAction="never" />

<item
android:id="@+id/action_keep_screen_on"
android:icon="@drawable/ic_screen_lock_portrait_24dp"
android:title="@string/action_keep_screen_on"
android:checkable="true"
app:showAsAction="never" />

</menu>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
<string name="action_outline">Outline</string>
<string name="action_view_document_properties">Properties</string>
<string name="action_close">Close</string>
<string name="action_toggle_vertical_scroll">Vertical scroll</string>
<string name="action_keep_screen_on">Keep screen on</string>

<string name="outline_child_button_description">View nested outline entries</string>
<string name="outline_not_available">No outline available</string>
Expand Down
Loading