From 14774ee222104a509eb52670da7cefc5594a025e Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 13 Jun 2016 17:45:53 +0200 Subject: [PATCH] check storage permission at every activity --- .../com/simplemobiletools/gallery/Utils.java | 7 ++++ .../gallery/activities/MainActivity.java | 5 ++- .../gallery/activities/MediaActivity.java | 32 ++++--------------- .../gallery/activities/ViewPagerActivity.java | 13 ++++++++ 4 files changed, 29 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/com/simplemobiletools/gallery/Utils.java b/app/src/main/java/com/simplemobiletools/gallery/Utils.java index 9a87b8572..f76a6e8a4 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/Utils.java +++ b/app/src/main/java/com/simplemobiletools/gallery/Utils.java @@ -1,7 +1,10 @@ package com.simplemobiletools.gallery; +import android.Manifest; import android.content.Context; +import android.content.pm.PackageManager; import android.content.res.Resources; +import android.support.v4.content.ContextCompat; import android.widget.Toast; public class Utils { @@ -21,4 +24,8 @@ public class Utils { return 0; } + + public static boolean hasStoragePermission(Context cxt) { + return ContextCompat.checkSelfPermission(cxt, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED; + } } diff --git a/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java b/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java index b03c392ab..6e668d417 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java @@ -14,7 +14,6 @@ import android.provider.MediaStore; import android.support.design.widget.CoordinatorLayout; import android.support.design.widget.Snackbar; import android.support.v4.app.ActivityCompat; -import android.support.v4.content.ContextCompat; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.util.SparseBooleanArray; @@ -30,10 +29,10 @@ import android.widget.GridView; import android.widget.Toast; import com.simplemobiletools.gallery.Constants; -import com.simplemobiletools.gallery.models.Directory; import com.simplemobiletools.gallery.R; import com.simplemobiletools.gallery.Utils; import com.simplemobiletools.gallery.adapters.DirectoryAdapter; +import com.simplemobiletools.gallery.models.Directory; import java.io.File; import java.util.ArrayList; @@ -101,7 +100,7 @@ public class MainActivity extends AppCompatActivity } private void tryloadGallery() { - if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { + if (Utils.hasStoragePermission(getApplicationContext())) { initializeGallery(); } else { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION); diff --git a/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java b/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java index e83685d41..e21177809 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/MediaActivity.java @@ -1,8 +1,6 @@ package com.simplemobiletools.gallery.activities; -import android.Manifest; import android.content.Intent; -import android.content.pm.PackageManager; import android.content.res.Resources; import android.database.Cursor; import android.graphics.Color; @@ -13,8 +11,6 @@ import android.os.Parcelable; import android.provider.MediaStore; import android.support.design.widget.CoordinatorLayout; import android.support.design.widget.Snackbar; -import android.support.v4.app.ActivityCompat; -import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.util.SparseBooleanArray; import android.view.ActionMode; @@ -25,13 +21,12 @@ import android.view.MotionEvent; import android.view.View; import android.widget.AdapterView; import android.widget.GridView; -import android.widget.Toast; import com.simplemobiletools.gallery.Constants; -import com.simplemobiletools.gallery.models.Medium; import com.simplemobiletools.gallery.R; import com.simplemobiletools.gallery.Utils; import com.simplemobiletools.gallery.adapters.MediaAdapter; +import com.simplemobiletools.gallery.models.Medium; import java.io.File; import java.util.ArrayList; @@ -46,7 +41,6 @@ public class MediaActivity extends AppCompatActivity GridView.OnTouchListener { @BindView(R.id.media_grid) GridView gridView; - private static final int STORAGE_PERMISSION = 1; private List media; private int selectedItemsCnt; private String path; @@ -79,23 +73,10 @@ public class MediaActivity extends AppCompatActivity } private void tryloadGallery() { - if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { + if (Utils.hasStoragePermission(getApplicationContext())) { initializeGallery(); } else { - ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION); - } - } - - @Override - public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { - super.onRequestPermissionsResult(requestCode, permissions, grantResults); - - if (requestCode == STORAGE_PERMISSION) { - if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { - initializeGallery(); - } else { - Toast.makeText(this, getResources().getString(R.string.no_permissions), Toast.LENGTH_SHORT).show(); - } + finish(); } } @@ -207,7 +188,7 @@ public class MediaActivity extends AppCompatActivity } private void deleteFiles() { - if (toBeDeleted.isEmpty()) + if (toBeDeleted == null || toBeDeleted.isEmpty()) return; if (snackbar != null) { @@ -254,10 +235,11 @@ public class MediaActivity extends AppCompatActivity @Override public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) { - if (checked) + if (checked) { selectedItemsCnt++; - else + } else { selectedItemsCnt--; + } if (selectedItemsCnt > 0) mode.setTitle(String.valueOf(selectedItemsCnt)); diff --git a/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java b/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java index 3adac5d24..b7bae3a5f 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java @@ -57,6 +57,11 @@ public class ViewPagerActivity extends AppCompatActivity setContentView(R.layout.activity_medium); ButterKnife.bind(this); + if (!Utils.hasStoragePermission(getApplicationContext())) { + finish(); + return; + } + pos = 0; isFullScreen = true; actionbar = getSupportActionBar(); @@ -83,6 +88,14 @@ public class ViewPagerActivity extends AppCompatActivity updateActionbarTitle(); } + @Override + protected void onResume() { + super.onResume(); + if (!Utils.hasStoragePermission(getApplicationContext())) { + finish(); + } + } + @OnClick(R.id.undo_delete) public void undoDeletion() { isUndoShown = false;