Skip to content

Instantly share code, notes, and snippets.

@vorobeij
Created October 1, 2017 10:48
Show Gist options
  • Select an option

  • Save vorobeij/967b4e09ad21dde6de9274529cbf2390 to your computer and use it in GitHub Desktop.

Select an option

Save vorobeij/967b4e09ad21dde6de9274529cbf2390 to your computer and use it in GitHub Desktop.
a sheme of using permissions on android
package com.jack.sparrow.mylauncher.model;
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import com.jack.sparrow.mylauncher.utils.Settings;
import com.jack.sparrow.mylauncher.utils.Tags;
import java.util.ArrayList;
import java.util.List;
public class PermissionsManager {
private static PermissionsManager instance;
private Context context;
private PermissionsManager() {
}
private PermissionsManager(Context context) {
this.context = context;
}
public static PermissionsManager getInstance(Context context) {
if (instance == null) {
instance = new PermissionsManager(context);
}
return instance;
}
public List<String> findUnAskedPermissions(List<String> wanted) {
List<String> result = new ArrayList<>();
for (String p : wanted) {
if (!hasPermission(p) && shouldWeAskForPermission(p)) result.add(p);
}
return result;
}
/**
* defines if permission is granted
*
* @param permission example:Manifest.permission.READ_CONTACTS
* @return
*/
public boolean hasPermission(String permission) {
if (isPermissionModelHigherLollipop()) {
return ((ContextCompat.checkSelfPermission(context,
permission)) == PackageManager.PERMISSION_GRANTED);
}
return true;
}
private boolean shouldWeAskForPermission(String permission) {
// todo check if this permission is in shared preferences
return true;
}
/**
* checks if the permission model is as in Marshmallow or not
*
* @return
*/
public boolean isPermissionModelHigherLollipop() {
return (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1);
}
/**
* this method is called from activity
* @param activity
*/
private void exampleOfUsingPermission(Activity activity) {
final String permission = Manifest.permission.READ_CONTACTS;
// at this point of code we need to use a feature
// make it as one function and call feature()
if (hasPermission(permission)) {
// we can use feature because have permissions
feature();
} else {
if (shouldWeAskForPermission(permission)) {
// display user that permission is denied
// maybe at custom toast as in chrome when close tab
} else {
requestPermission(permission, RequestCodes.FEATURE_1, activity);
markPermissionAsAsked(permission);
}
}
}
/**
* its feature that needs a permission
*/
public void feature() {
}
/**
* check if permission is granted
*
* @param permission example: Manifest.permission.READ_CONTACTS
* @param code int constant
* @return true if permission already granted and false if attempting to grant it
*/
private boolean requestPermission(String permission, int code, Activity activity) {
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(activity,
permission) != PackageManager.PERMISSION_GRANTED) {
if (Settings.DEBUG) Log.d(Tags.tag, "permission " + permission + " is granted");
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(activity,
Manifest.permission.READ_CONTACTS)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(activity, new String[]{permission}, code);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
return true;
} else {
if (Settings.DEBUG) Log.d(Tags.tag, "permission " + permission + " is not granted");
return false;
}
}
private void markPermissionAsAsked(String permission) {
// todo sharedPreferences.edit().putBoolean(permission, false).apply();
}
public class RequestCodes {
public static final int FEATURE_1 = 100;
}
}
public class Launcher extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.launcher);
ButterKnife.bind(this);
permissionsManager = PermissionsManager.getInstance(this);
permissionsManager.exampleOfUsingPermission(this);
}
@Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_BIND_DEVICE_ADMIN: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
permissionsManager.feature();
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
//display that permission is denied and feature cant be used
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment