Use view binding instead of findViewById
Provides null safety and type safety
This commit is contained in:
parent
5f5e1682a0
commit
fdf2cf8c27
|
@ -50,6 +50,10 @@ android {
|
||||||
signingConfig = signingConfigs.getByName("release")
|
signingConfig = signingConfigs.getByName("release")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
buildFeatures {
|
||||||
|
viewBinding = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
compileOptions {
|
compileOptions {
|
||||||
|
|
|
@ -30,6 +30,7 @@ import androidx.loader.content.Loader;
|
||||||
|
|
||||||
import com.google.android.material.snackbar.Snackbar;
|
import com.google.android.material.snackbar.Snackbar;
|
||||||
|
|
||||||
|
import org.grapheneos.pdfviewer.databinding.WebviewBinding;
|
||||||
import org.grapheneos.pdfviewer.fragment.DocumentPropertiesFragment;
|
import org.grapheneos.pdfviewer.fragment.DocumentPropertiesFragment;
|
||||||
import org.grapheneos.pdfviewer.fragment.JumpToPageFragment;
|
import org.grapheneos.pdfviewer.fragment.JumpToPageFragment;
|
||||||
import org.grapheneos.pdfviewer.loader.DocumentPropertiesLoader;
|
import org.grapheneos.pdfviewer.loader.DocumentPropertiesLoader;
|
||||||
|
@ -106,7 +107,7 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
|
||||||
private List<CharSequence> mDocumentProperties;
|
private List<CharSequence> mDocumentProperties;
|
||||||
private InputStream mInputStream;
|
private InputStream mInputStream;
|
||||||
|
|
||||||
private WebView mWebView;
|
private WebviewBinding binding;
|
||||||
private TextView mTextView;
|
private TextView mTextView;
|
||||||
private Toast mToast;
|
private Toast mToast;
|
||||||
private Snackbar snackbar;
|
private Snackbar snackbar;
|
||||||
|
@ -154,23 +155,22 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
|
||||||
@SuppressLint({"SetJavaScriptEnabled", "ClickableViewAccessibility"})
|
@SuppressLint({"SetJavaScriptEnabled", "ClickableViewAccessibility"})
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
binding = WebviewBinding.inflate(getLayoutInflater());
|
||||||
|
setContentView(binding.getRoot());
|
||||||
|
|
||||||
setContentView(R.layout.webview);
|
binding.webview.setBackgroundColor(Color.TRANSPARENT);
|
||||||
|
|
||||||
mWebView = findViewById(R.id.webview);
|
|
||||||
mWebView.setBackgroundColor(Color.TRANSPARENT);
|
|
||||||
|
|
||||||
if (BuildConfig.DEBUG) {
|
if (BuildConfig.DEBUG) {
|
||||||
WebView.setWebContentsDebuggingEnabled(true);
|
WebView.setWebContentsDebuggingEnabled(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
mWebView.setOnApplyWindowInsetsListener((view, insets) -> {
|
binding.webview.setOnApplyWindowInsetsListener((view, insets) -> {
|
||||||
windowInsetTop = insets.getSystemWindowInsetTop();
|
windowInsetTop = insets.getSystemWindowInsetTop();
|
||||||
mWebView.evaluateJavascript("updateInset()", null);
|
binding.webview.evaluateJavascript("updateInset()", null);
|
||||||
return insets;
|
return insets;
|
||||||
});
|
});
|
||||||
|
|
||||||
final WebSettings settings = mWebView.getSettings();
|
final WebSettings settings = binding.webview.getSettings();
|
||||||
settings.setAllowContentAccess(false);
|
settings.setAllowContentAccess(false);
|
||||||
settings.setAllowFileAccess(false);
|
settings.setAllowFileAccess(false);
|
||||||
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
|
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
|
||||||
|
@ -178,9 +178,9 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
|
||||||
|
|
||||||
CookieManager.getInstance().setAcceptCookie(false);
|
CookieManager.getInstance().setAcceptCookie(false);
|
||||||
|
|
||||||
mWebView.addJavascriptInterface(new Channel(), "channel");
|
binding.webview.addJavascriptInterface(new Channel(), "channel");
|
||||||
|
|
||||||
mWebView.setWebViewClient(new WebViewClient() {
|
binding.webview.setWebViewClient(new WebViewClient() {
|
||||||
private WebResourceResponse fromAsset(final String mime, final String path) {
|
private WebResourceResponse fromAsset(final String mime, final String path) {
|
||||||
try {
|
try {
|
||||||
InputStream inputStream = getAssets().open(path.substring(1));
|
InputStream inputStream = getAssets().open(path.substring(1));
|
||||||
|
@ -241,12 +241,12 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
GestureHelper.attach(PdfViewer.this, mWebView,
|
GestureHelper.attach(PdfViewer.this, binding.webview,
|
||||||
new GestureHelper.GestureListener() {
|
new GestureHelper.GestureListener() {
|
||||||
@Override
|
@Override
|
||||||
public boolean onTapUp() {
|
public boolean onTapUp() {
|
||||||
if (mUri != null) {
|
if (mUri != null) {
|
||||||
mWebView.evaluateJavascript("isTextSelected()", selection -> {
|
binding.webview.evaluateJavascript("isTextSelected()", selection -> {
|
||||||
if (!Boolean.parseBoolean(selection)) {
|
if (!Boolean.parseBoolean(selection)) {
|
||||||
if ((getWindow().getDecorView().getSystemUiVisibility() &
|
if ((getWindow().getDecorView().getSystemUiVisibility() &
|
||||||
View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
|
View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
|
||||||
|
@ -288,7 +288,7 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
|
||||||
// loader manager impl so that the result will be delivered.
|
// loader manager impl so that the result will be delivered.
|
||||||
LoaderManager.getInstance(this);
|
LoaderManager.getInstance(this);
|
||||||
|
|
||||||
snackbar = Snackbar.make(mWebView, "", Snackbar.LENGTH_LONG);
|
snackbar = Snackbar.make(binding.webview, "", Snackbar.LENGTH_LONG);
|
||||||
|
|
||||||
final Intent intent = getIntent();
|
final Intent intent = getIntent();
|
||||||
if (Intent.ACTION_VIEW.equals(intent.getAction())) {
|
if (Intent.ACTION_VIEW.equals(intent.getAction())) {
|
||||||
|
@ -346,11 +346,11 @@ public class PdfViewer extends AppCompatActivity implements LoaderManager.Loader
|
||||||
}
|
}
|
||||||
|
|
||||||
showSystemUi();
|
showSystemUi();
|
||||||
mWebView.loadUrl("https://localhost/viewer.html");
|
binding.webview.loadUrl("https://localhost/viewer.html");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void renderPage(final int zoom) {
|
private void renderPage(final int zoom) {
|
||||||
mWebView.evaluateJavascript("onRenderPage(" + zoom + ")", null);
|
binding.webview.evaluateJavascript("onRenderPage(" + zoom + ")", null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void documentOrientationChanged(final int orientationDegreesOffset) {
|
private void documentOrientationChanged(final int orientationDegreesOffset) {
|
||||||
|
|
Loading…
Reference in New Issue