check storage permission at every activity

This commit is contained in:
tibbi 2016-06-13 17:45:53 +02:00
parent 51981e4f06
commit 14774ee222
4 changed files with 29 additions and 28 deletions

View file

@ -1,7 +1,10 @@
package com.simplemobiletools.gallery; package com.simplemobiletools.gallery;
import android.Manifest;
import android.content.Context; import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Resources; import android.content.res.Resources;
import android.support.v4.content.ContextCompat;
import android.widget.Toast; import android.widget.Toast;
public class Utils { public class Utils {
@ -21,4 +24,8 @@ public class Utils {
return 0; return 0;
} }
public static boolean hasStoragePermission(Context cxt) {
return ContextCompat.checkSelfPermission(cxt, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
}
} }

View file

@ -14,7 +14,6 @@ import android.provider.MediaStore;
import android.support.design.widget.CoordinatorLayout; import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar; import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat; import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog; import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.util.SparseBooleanArray; import android.util.SparseBooleanArray;
@ -30,10 +29,10 @@ import android.widget.GridView;
import android.widget.Toast; import android.widget.Toast;
import com.simplemobiletools.gallery.Constants; import com.simplemobiletools.gallery.Constants;
import com.simplemobiletools.gallery.models.Directory;
import com.simplemobiletools.gallery.R; import com.simplemobiletools.gallery.R;
import com.simplemobiletools.gallery.Utils; import com.simplemobiletools.gallery.Utils;
import com.simplemobiletools.gallery.adapters.DirectoryAdapter; import com.simplemobiletools.gallery.adapters.DirectoryAdapter;
import com.simplemobiletools.gallery.models.Directory;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
@ -101,7 +100,7 @@ public class MainActivity extends AppCompatActivity
} }
private void tryloadGallery() { private void tryloadGallery() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { if (Utils.hasStoragePermission(getApplicationContext())) {
initializeGallery(); initializeGallery();
} else { } else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION); ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION);

View file

@ -1,8 +1,6 @@
package com.simplemobiletools.gallery.activities; package com.simplemobiletools.gallery.activities;
import android.Manifest;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Resources; import android.content.res.Resources;
import android.database.Cursor; import android.database.Cursor;
import android.graphics.Color; import android.graphics.Color;
@ -13,8 +11,6 @@ import android.os.Parcelable;
import android.provider.MediaStore; import android.provider.MediaStore;
import android.support.design.widget.CoordinatorLayout; import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar; 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.support.v7.app.AppCompatActivity;
import android.util.SparseBooleanArray; import android.util.SparseBooleanArray;
import android.view.ActionMode; import android.view.ActionMode;
@ -25,13 +21,12 @@ import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.GridView; import android.widget.GridView;
import android.widget.Toast;
import com.simplemobiletools.gallery.Constants; import com.simplemobiletools.gallery.Constants;
import com.simplemobiletools.gallery.models.Medium;
import com.simplemobiletools.gallery.R; import com.simplemobiletools.gallery.R;
import com.simplemobiletools.gallery.Utils; import com.simplemobiletools.gallery.Utils;
import com.simplemobiletools.gallery.adapters.MediaAdapter; import com.simplemobiletools.gallery.adapters.MediaAdapter;
import com.simplemobiletools.gallery.models.Medium;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
@ -46,7 +41,6 @@ public class MediaActivity extends AppCompatActivity
GridView.OnTouchListener { GridView.OnTouchListener {
@BindView(R.id.media_grid) GridView gridView; @BindView(R.id.media_grid) GridView gridView;
private static final int STORAGE_PERMISSION = 1;
private List<Medium> media; private List<Medium> media;
private int selectedItemsCnt; private int selectedItemsCnt;
private String path; private String path;
@ -79,23 +73,10 @@ public class MediaActivity extends AppCompatActivity
} }
private void tryloadGallery() { private void tryloadGallery() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { if (Utils.hasStoragePermission(getApplicationContext())) {
initializeGallery(); initializeGallery();
} else { } else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION); finish();
}
}
@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();
}
} }
} }
@ -207,7 +188,7 @@ public class MediaActivity extends AppCompatActivity
} }
private void deleteFiles() { private void deleteFiles() {
if (toBeDeleted.isEmpty()) if (toBeDeleted == null || toBeDeleted.isEmpty())
return; return;
if (snackbar != null) { if (snackbar != null) {
@ -254,10 +235,11 @@ public class MediaActivity extends AppCompatActivity
@Override @Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) { public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
if (checked) if (checked) {
selectedItemsCnt++; selectedItemsCnt++;
else } else {
selectedItemsCnt--; selectedItemsCnt--;
}
if (selectedItemsCnt > 0) if (selectedItemsCnt > 0)
mode.setTitle(String.valueOf(selectedItemsCnt)); mode.setTitle(String.valueOf(selectedItemsCnt));

View file

@ -57,6 +57,11 @@ public class ViewPagerActivity extends AppCompatActivity
setContentView(R.layout.activity_medium); setContentView(R.layout.activity_medium);
ButterKnife.bind(this); ButterKnife.bind(this);
if (!Utils.hasStoragePermission(getApplicationContext())) {
finish();
return;
}
pos = 0; pos = 0;
isFullScreen = true; isFullScreen = true;
actionbar = getSupportActionBar(); actionbar = getSupportActionBar();
@ -83,6 +88,14 @@ public class ViewPagerActivity extends AppCompatActivity
updateActionbarTitle(); updateActionbarTitle();
} }
@Override
protected void onResume() {
super.onResume();
if (!Utils.hasStoragePermission(getApplicationContext())) {
finish();
}
}
@OnClick(R.id.undo_delete) @OnClick(R.id.undo_delete)
public void undoDeletion() { public void undoDeletion() {
isUndoShown = false; isUndoShown = false;