Skip to content

Commit

Permalink
Merge pull request #158 from voghDev/request-permissions
Browse files Browse the repository at this point in the history
Request permissions
  • Loading branch information
voghDev authored Jun 12, 2020
2 parents c782665 + a0d6cf2 commit 80fc233
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
62 changes: 51 additions & 11 deletions sample/src/main/java/es/voghdev/pdfviewpager/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@
*/
package es.voghdev.pdfviewpager;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.core.app.ActivityCompat;

import es.voghdev.pdfviewpager.library.PDFViewPager;
import es.voghdev.pdfviewpager.library.adapter.BasePDFPagerAdapter;
import es.voghdev.pdfviewpager.library.adapter.PDFPagerAdapter;
Expand All @@ -28,13 +33,15 @@ public class MainActivity extends BaseSampleActivity {
PDFViewPager pdfViewPager;
BasePDFPagerAdapter adapter;

final int REQUEST_CODE = 1;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(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);
Expand All @@ -44,10 +51,7 @@ public void onCreate(Bundle savedInstanceState) {
protected void onDestroy() {
super.onDestroy();

if (adapter != null) {
adapter.close();
adapter = null;
}
adapter.close();
}

@Override
Expand All @@ -61,23 +65,59 @@ 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) {
AssetOnSDActivity.open(this);
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) {
AssetOnXMLActivity.open(this);
requestPermissionsThenOpen(AssetOnXMLActivity.class);
} else if (id == R.id.action_sample8) {
ZoomablePDFActivityPhotoView.open(this);
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);
}

protected boolean hasExternalStoragePermissions() {
boolean hasReadPermission = ActivityCompat.checkSelfPermission(
this,
Manifest.permission.READ_EXTERNAL_STORAGE
) == PackageManager.PERMISSION_GRANTED;

boolean hasWritePermission = ActivityCompat.checkSelfPermission(
this,
Manifest.permission.READ_EXTERNAL_STORAGE
) == PackageManager.PERMISSION_GRANTED;

return hasReadPermission && hasWritePermission;
}

protected void requestPermissionsThenOpen(Class activityClass) {
if (hasExternalStoragePermissions()) {
launchActivity(activityClass);
} else {
requestExternalStoragePermissions();
}
}

protected void requestExternalStoragePermissions() {
String[] permissions = {
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE
};

ActivityCompat.requestPermissions(this, permissions, REQUEST_CODE);
}

protected void launchActivity(Class activityClass) {
Intent i = new Intent(this, activityClass);
startActivity(i);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -64,16 +63,7 @@ private PdfScale getPdfScale() {
protected void onDestroy() {
super.onDestroy();

BasePDFPagerAdapter adapter = (BasePDFPagerAdapter) pdfViewPager.getAdapter();
if (adapter != null) {
adapter.close();
adapter = null;
}
}

public static void open(Context context) {
Intent i = new Intent(context, PDFWithScaleActivity.class);
context.startActivity(i);
((BasePDFPagerAdapter) pdfViewPager.getAdapter()).close();
}

public int getScreenWidth(Context ctx) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ protected void onDestroy() {
BasePDFPagerAdapter adapter = (BasePDFPagerAdapter) pdfViewPager.getAdapter();
if (adapter != null) {
adapter.close();
adapter = null;
}
}

public static void open(Context context) {
Intent i = new Intent(context, ZoomablePDFActivityPhotoView.class);
context.startActivity(i);
}
}

0 comments on commit 80fc233

Please sign in to comment.