Skip to content
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

add ProgressBar with new Toolbar to show loading #64

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -34,8 +34,8 @@ android {

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.0'
}

def props = new Properties()
1 change: 1 addition & 0 deletions app/src/main/assets/viewer.js
Original file line number Diff line number Diff line change
@@ -59,6 +59,7 @@ function display(newCanvas, zoom) {
if (!zoom) {
scrollTo(0, 0);
}
channel.hideProgressBar();
}

function renderPage(pageNumber, zoom, prerender, prerenderTrigger=0) {
72 changes: 61 additions & 11 deletions app/src/main/java/org/grapheneos/pdfviewer/PdfViewer.java
Original file line number Diff line number Diff line change
@@ -20,24 +20,28 @@
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.Loader;

import com.google.android.material.snackbar.Snackbar;

import org.grapheneos.pdfviewer.fragment.DocumentPropertiesFragment;
import org.grapheneos.pdfviewer.fragment.JumpToPageFragment;
import org.grapheneos.pdfviewer.loader.DocumentPropertiesLoader;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;

import org.grapheneos.pdfviewer.fragment.DocumentPropertiesFragment;
import org.grapheneos.pdfviewer.fragment.JumpToPageFragment;
import org.grapheneos.pdfviewer.loader.DocumentPropertiesLoader;

public class PdfViewer extends AppCompatActivity implements LoaderManager.LoaderCallbacks<List<CharSequence>> {
public static final String TAG = "PdfViewer";

@@ -85,6 +89,8 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
private static final int STATE_END = 2;
private static final int PADDING = 10;

private boolean mIsProgressBarVisible;

private Uri mUri;
public int mPage;
public int mNumPages;
@@ -95,6 +101,9 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
private List<CharSequence> mDocumentProperties;
private InputStream mInputStream;

private CoordinatorLayout mCoordinatorLayout;
private Toolbar mToolbar;
private ProgressBar mProgressBar;
private WebView mWebView;
private TextView mTextView;
private Toast mToast;
@@ -127,6 +136,16 @@ public void setNumPages(int numPages) {
runOnUiThread(PdfViewer.this::invalidateOptionsMenu);
}

@JavascriptInterface
public void hideProgressBar() {
if (mIsProgressBarVisible) {
mIsProgressBarVisible = false;
runOnUiThread(() -> {
mProgressBar.setVisibility(View.INVISIBLE);
});
}
}

@JavascriptInterface
public void setDocumentProperties(final String properties) {
if (mDocumentProperties != null) {
@@ -154,10 +173,22 @@ protected void onCreate(Bundle savedInstanceState) {

setContentView(R.layout.webview);

mCoordinatorLayout = findViewById(R.id.appCoordinatorLayout);

mIsProgressBarVisible = false;
mProgressBar = findViewById(R.id.progressBar);

mToolbar = findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
mToolbar.inflateMenu(R.menu.pdf_viewer);
mToolbar.setTitle(R.string.app_name);

mWebView = findViewById(R.id.webview);

mWebView.setOnApplyWindowInsetsListener((view, insets) -> {
windowInsetTop = insets.getSystemWindowInsetTop();
// The ActionBar is no longer a part of the system, so we have to
// add in its height for the inset.
windowInsetTop = insets.getSystemWindowInsetTop() + mToolbar.getMinimumHeight();
mWebView.evaluateJavascript("updateInset()", null);
return insets;
});
@@ -234,7 +265,7 @@ public void onPageFinished(WebView view, String url) {
}
});

showSystemUi();
showSystemUiAndActionBar();

GestureHelper.attach(PdfViewer.this, mWebView,
new GestureHelper.GestureListener() {
@@ -245,9 +276,9 @@ public boolean onTapUp() {
if (!Boolean.valueOf(selection)) {
if ((getWindow().getDecorView().getSystemUiVisibility() &
View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
hideSystemUi();
hideSystemUiAndActionBar();
} else {
showSystemUi();
showSystemUiAndActionBar();
}
}
});
@@ -283,7 +314,7 @@ public void onZoomEnd() {
// loader manager impl so that the result will be delivered.
LoaderManager.getInstance(this);

snackbar = Snackbar.make(mWebView, "", Snackbar.LENGTH_LONG);
snackbar = Snackbar.make(mCoordinatorLayout, "", Snackbar.LENGTH_LONG);

final Intent intent = getIntent();
if (Intent.ACTION_VIEW.equals(intent.getAction())) {
@@ -328,12 +359,20 @@ public void onLoaderReset(Loader<List<CharSequence>> loader) {
mDocumentProperties = null;
}

private void showProgressBar(final int zoom) {
if (zoom == 0) {
mIsProgressBarVisible = true;
mProgressBar.setVisibility(View.VISIBLE);
}
}

private void loadPdf() {
try {
if (mInputStream != null) {
mInputStream.close();
}
mInputStream = getContentResolver().openInputStream(mUri);
showProgressBar(0);
} catch (IOException e) {
snackbar.setText(R.string.io_error).show();
return;
@@ -342,6 +381,7 @@ private void loadPdf() {
}

private void renderPage(final int zoom) {
showProgressBar(zoom);
mWebView.evaluateJavascript("onRenderPage(" + zoom + ")", null);
}

@@ -399,21 +439,31 @@ public void onJumpToPageInDocument(final int selected_page) {
}
}

private void showSystemUi() {
private void showSystemUiAndActionBar() {
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);

final ActionBar actionBar = getSupportActionBar();
if (actionBar != null && !actionBar.isShowing()) {
actionBar.show();
}
}

private void hideSystemUi() {
private void hideSystemUiAndActionBar() {
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_FULLSCREEN |
View.SYSTEM_UI_FLAG_IMMERSIVE);

final ActionBar actionBar = getSupportActionBar();
if (actionBar != null && actionBar.isShowing()) {
actionBar.hide();
}
}

@Override
61 changes: 57 additions & 4 deletions app/src/main/res/layout/webview.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/appCoordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="visible" />

<LinearLayout
android:id="@+id/toolbarProgressBarHolder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:fitsSystemWindows="true"
android:orientation="vertical">

<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:background="?attr/colorPrimary"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme">

<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
android:theme="@style/AppTheme"
app:menu="@menu/pdf_viewer" />

</com.google.android.material.appbar.AppBarLayout>

<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="16dp"
android:layout_marginTop="-6dp"
android:layout_marginBottom="-6dp"
android:background="?attr/colorPrimary"
android:fitsSystemWindows="false"
android:indeterminate="true"
android:theme="@style/AppTheme"
android:visibility="gone"
tools:visibility="visible" />
</LinearLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>
3 changes: 1 addition & 2 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
</style>

</resources>