From 77e98d5a42d55d4239ef1d05314c894a8b17c6bf Mon Sep 17 00:00:00 2001 From: Olmo Gallegos Date: Tue, 28 Apr 2020 12:51:13 +0200 Subject: [PATCH 1/2] Refactor intent navigation calls --- .../pdfviewpager/AssetOnSDActivity.java | 5 --- .../pdfviewpager/AssetOnXMLActivity.java | 5 --- .../pdfviewpager/InvalidPdfActivity.java | 5 --- .../es/voghdev/pdfviewpager/MainActivity.java | 38 ++++++++++--------- .../pdfviewpager/PDFWithScaleActivity.java | 5 --- .../pdfviewpager/RemotePDFActivity.java | 5 --- .../ZoomablePDFActivityPhotoView.java | 5 --- 7 files changed, 20 insertions(+), 48 deletions(-) diff --git a/sample/src/main/java/es/voghdev/pdfviewpager/AssetOnSDActivity.java b/sample/src/main/java/es/voghdev/pdfviewpager/AssetOnSDActivity.java index addea29..d1b8517 100644 --- a/sample/src/main/java/es/voghdev/pdfviewpager/AssetOnSDActivity.java +++ b/sample/src/main/java/es/voghdev/pdfviewpager/AssetOnSDActivity.java @@ -79,9 +79,4 @@ protected void onDestroy() { ((BasePDFPagerAdapter) pdfViewPager.getAdapter()).close(); } } - - public static void open(Context context) { - Intent i = new Intent(context, AssetOnSDActivity.class); - context.startActivity(i); - } } \ No newline at end of file diff --git a/sample/src/main/java/es/voghdev/pdfviewpager/AssetOnXMLActivity.java b/sample/src/main/java/es/voghdev/pdfviewpager/AssetOnXMLActivity.java index c99c7ee..cdbd031 100644 --- a/sample/src/main/java/es/voghdev/pdfviewpager/AssetOnXMLActivity.java +++ b/sample/src/main/java/es/voghdev/pdfviewpager/AssetOnXMLActivity.java @@ -40,9 +40,4 @@ protected void onDestroy() { ((BasePDFPagerAdapter) pdfViewPager.getAdapter()).close(); } - - public static void open(Context context) { - Intent i = new Intent(context, AssetOnXMLActivity.class); - context.startActivity(i); - } } \ No newline at end of file diff --git a/sample/src/main/java/es/voghdev/pdfviewpager/InvalidPdfActivity.java b/sample/src/main/java/es/voghdev/pdfviewpager/InvalidPdfActivity.java index 7611ba2..83a8e22 100644 --- a/sample/src/main/java/es/voghdev/pdfviewpager/InvalidPdfActivity.java +++ b/sample/src/main/java/es/voghdev/pdfviewpager/InvalidPdfActivity.java @@ -48,9 +48,4 @@ protected void onDestroy() { adapter.close(); } - - public static void open(Context context) { - Intent i = new Intent(context, InvalidPdfActivity.class); - context.startActivity(i); - } } diff --git a/sample/src/main/java/es/voghdev/pdfviewpager/MainActivity.java b/sample/src/main/java/es/voghdev/pdfviewpager/MainActivity.java index fc4a9d9..237c6cc 100644 --- a/sample/src/main/java/es/voghdev/pdfviewpager/MainActivity.java +++ b/sample/src/main/java/es/voghdev/pdfviewpager/MainActivity.java @@ -16,6 +16,7 @@ package es.voghdev.pdfviewpager; import android.Manifest; +import android.content.Intent; import android.content.pm.PackageManager; import android.os.Bundle; import android.view.Menu; @@ -67,33 +68,21 @@ public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_sample2) { - RemotePDFActivity.open(this); + launchActivity(RemotePDFActivity.class); return false; } else if (id == R.id.action_sample3) { - if (hasExternalStoragePermissions()) { - AssetOnSDActivity.open(this); - } else { - requestExternalStoragePermissions(); - } + requestPermissionsThenOpen(AssetOnSDActivity.class); return false; } else if (id == R.id.action_sample4) { Toast.makeText(this, R.string.dummy_msg, Toast.LENGTH_LONG).show(); } else if (id == R.id.action_sample5) { - if (hasExternalStoragePermissions()) { - AssetOnXMLActivity.open(this); - } else { - requestExternalStoragePermissions(); - } + requestPermissionsThenOpen(AssetOnXMLActivity.class); } else if (id == R.id.action_sample8) { - if (hasExternalStoragePermissions()) { - ZoomablePDFActivityPhotoView.open(this); - } else { - requestExternalStoragePermissions(); - } + requestPermissionsThenOpen(ZoomablePDFActivityPhotoView.class); } else if (id == R.id.action_sample9) { - PDFWithScaleActivity.open(this); + launchActivity(PDFWithScaleActivity.class); } else if (id == R.id.action_sample10) { - InvalidPdfActivity.open(this); + launchActivity(InvalidPdfActivity.class); } return super.onOptionsItemSelected(item); @@ -113,6 +102,14 @@ protected boolean hasExternalStoragePermissions() { return hasReadPermission && hasWritePermission; } + protected void requestPermissionsThenOpen(Class activityClass) { + if (hasExternalStoragePermissions()) { + launchActivity(activityClass); + } else { + requestExternalStoragePermissions(); + } + } + protected void requestExternalStoragePermissions() { String[] permissions = { Manifest.permission.WRITE_EXTERNAL_STORAGE, @@ -121,4 +118,9 @@ protected void requestExternalStoragePermissions() { ActivityCompat.requestPermissions(this, permissions, REQUEST_CODE); } + + protected void launchActivity(Class activityClass) { + Intent i = new Intent(this, activityClass); + startActivity(i); + } } diff --git a/sample/src/main/java/es/voghdev/pdfviewpager/PDFWithScaleActivity.java b/sample/src/main/java/es/voghdev/pdfviewpager/PDFWithScaleActivity.java index 1cc42d0..9cf1f8a 100644 --- a/sample/src/main/java/es/voghdev/pdfviewpager/PDFWithScaleActivity.java +++ b/sample/src/main/java/es/voghdev/pdfviewpager/PDFWithScaleActivity.java @@ -71,11 +71,6 @@ protected void onDestroy() { } } - public static void open(Context context) { - Intent i = new Intent(context, PDFWithScaleActivity.class); - context.startActivity(i); - } - public int getScreenWidth(Context ctx) { int w = 0; if (ctx instanceof Activity) { diff --git a/sample/src/main/java/es/voghdev/pdfviewpager/RemotePDFActivity.java b/sample/src/main/java/es/voghdev/pdfviewpager/RemotePDFActivity.java index 1aad4b8..a20b6e4 100644 --- a/sample/src/main/java/es/voghdev/pdfviewpager/RemotePDFActivity.java +++ b/sample/src/main/java/es/voghdev/pdfviewpager/RemotePDFActivity.java @@ -74,11 +74,6 @@ protected String getUrlFromEditText() { return etPdfUrl.getText().toString().trim(); } - public static void open(Context context) { - Intent i = new Intent(context, RemotePDFActivity.class); - context.startActivity(i); - } - public void showDownloadButton() { btnDownload.setVisibility(View.VISIBLE); } diff --git a/sample/src/main/java/es/voghdev/pdfviewpager/ZoomablePDFActivityPhotoView.java b/sample/src/main/java/es/voghdev/pdfviewpager/ZoomablePDFActivityPhotoView.java index 4caab99..456c7ae 100644 --- a/sample/src/main/java/es/voghdev/pdfviewpager/ZoomablePDFActivityPhotoView.java +++ b/sample/src/main/java/es/voghdev/pdfviewpager/ZoomablePDFActivityPhotoView.java @@ -44,9 +44,4 @@ protected void onDestroy() { adapter = null; } } - - public static void open(Context context) { - Intent i = new Intent(context, ZoomablePDFActivityPhotoView.class); - context.startActivity(i); - } } From a0d6cf250f3a3cc5c733b52f93b02d48d8c132be Mon Sep 17 00:00:00 2001 From: Olmo Gallegos Date: Tue, 28 Apr 2020 12:55:08 +0200 Subject: [PATCH 2/2] Minor refactors --- .../java/es/voghdev/pdfviewpager/AssetOnXMLActivity.java | 2 +- .../main/java/es/voghdev/pdfviewpager/MainActivity.java | 7 ++----- .../java/es/voghdev/pdfviewpager/PDFWithScaleActivity.java | 7 +------ .../voghdev/pdfviewpager/ZoomablePDFActivityPhotoView.java | 1 - 4 files changed, 4 insertions(+), 13 deletions(-) diff --git a/sample/src/main/java/es/voghdev/pdfviewpager/AssetOnXMLActivity.java b/sample/src/main/java/es/voghdev/pdfviewpager/AssetOnXMLActivity.java index cdbd031..b54425a 100644 --- a/sample/src/main/java/es/voghdev/pdfviewpager/AssetOnXMLActivity.java +++ b/sample/src/main/java/es/voghdev/pdfviewpager/AssetOnXMLActivity.java @@ -31,7 +31,7 @@ public void onCreate(Bundle savedInstanceState) { setTitle(R.string.asset_on_xml); setContentView(R.layout.activity_asset_on_xml); - pdfViewPager = (PDFViewPager) findViewById(R.id.pdfViewPager); + pdfViewPager = findViewById(R.id.pdfViewPager); } @Override diff --git a/sample/src/main/java/es/voghdev/pdfviewpager/MainActivity.java b/sample/src/main/java/es/voghdev/pdfviewpager/MainActivity.java index 237c6cc..5135b10 100644 --- a/sample/src/main/java/es/voghdev/pdfviewpager/MainActivity.java +++ b/sample/src/main/java/es/voghdev/pdfviewpager/MainActivity.java @@ -41,7 +41,7 @@ public void onCreate(Bundle savedInstanceState) { setTitle(R.string.std_example); setContentView(R.layout.activity_main); - pdfViewPager = (PDFViewPager) findViewById(R.id.pdfViewPager); + pdfViewPager = findViewById(R.id.pdfViewPager); adapter = new PDFPagerAdapter(this, "sample.pdf"); pdfViewPager.setAdapter(adapter); @@ -51,10 +51,7 @@ public void onCreate(Bundle savedInstanceState) { protected void onDestroy() { super.onDestroy(); - if (adapter != null) { - adapter.close(); - adapter = null; - } + adapter.close(); } @Override diff --git a/sample/src/main/java/es/voghdev/pdfviewpager/PDFWithScaleActivity.java b/sample/src/main/java/es/voghdev/pdfviewpager/PDFWithScaleActivity.java index 9cf1f8a..a1854d3 100644 --- a/sample/src/main/java/es/voghdev/pdfviewpager/PDFWithScaleActivity.java +++ b/sample/src/main/java/es/voghdev/pdfviewpager/PDFWithScaleActivity.java @@ -17,7 +17,6 @@ import android.app.Activity; import android.content.Context; -import android.content.Intent; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.View; @@ -64,11 +63,7 @@ private PdfScale getPdfScale() { protected void onDestroy() { super.onDestroy(); - BasePDFPagerAdapter adapter = (BasePDFPagerAdapter) pdfViewPager.getAdapter(); - if (adapter != null) { - adapter.close(); - adapter = null; - } + ((BasePDFPagerAdapter) pdfViewPager.getAdapter()).close(); } public int getScreenWidth(Context ctx) { diff --git a/sample/src/main/java/es/voghdev/pdfviewpager/ZoomablePDFActivityPhotoView.java b/sample/src/main/java/es/voghdev/pdfviewpager/ZoomablePDFActivityPhotoView.java index 456c7ae..5a4e162 100644 --- a/sample/src/main/java/es/voghdev/pdfviewpager/ZoomablePDFActivityPhotoView.java +++ b/sample/src/main/java/es/voghdev/pdfviewpager/ZoomablePDFActivityPhotoView.java @@ -41,7 +41,6 @@ protected void onDestroy() { BasePDFPagerAdapter adapter = (BasePDFPagerAdapter) pdfViewPager.getAdapter(); if (adapter != null) { adapter.close(); - adapter = null; } } }