Skip to content

Commit

Permalink
Merge pull request #7 from kayvannj/v110
Browse files Browse the repository at this point in the history
V110
  • Loading branch information
kayvannj authored Oct 7, 2016
2 parents 342f30e + 2d2ad15 commit 4c1e8f4
Show file tree
Hide file tree
Showing 11 changed files with 209 additions and 58 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
.DS_Store
/build
/captures
/.idea
kayvanlibs.properties
1 change: 0 additions & 1 deletion .idea/.name

This file was deleted.

1 change: 1 addition & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 15 additions & 5 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 29 additions & 2 deletions PermissionUtils/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ apply plugin: 'com.android.library'
apply plugin: 'com.jfrog.bintray'
apply plugin: 'com.github.dcendents.android-maven'

group = 'com.github.kayvannj' // Change this to match your package name
version = '1.0.1' // Change this to match your version number
group = 'com.github.kayvannj'
version = '1.0.3'

android {
compileSdkVersion 23
Expand Down Expand Up @@ -51,3 +51,30 @@ artifacts {
task findConventions << {
println project.getConvention()
}

Properties props = new Properties()
File propsFile = file('kayvanlibs.properties')
props.load(propsFile.newDataInputStream())

def siteUrl = 'https://github.com/kayvannj/PermissionUtil'
def gitUrl = 'https://github.com/kayvannj/PermissionUtil.git'
def bintray_user = 'kayvannj'
def bintray_api_key= props.get("bintray_api_key")

bintray {
user = bintray_user
key = bintray_api_key

configurations = ['archives'] //When uploading configuration files
pkg {
repo = 'maven'
name = group
desc = 'Android 6.0 permissions made simple'
websiteUrl = siteUrl
issueTrackerUrl = 'https://github.com/kayvannj/PermissionUtil/issues'
vcsUrl = gitUrl
licenses = ['Apache-2.0']
labels = ['aar', 'android']
publicDownloadNumbers = true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
Expand All @@ -25,45 +26,78 @@
*/
public class PermissionUtil {

static private AppCompatActivity mAppCompatActivity;

public static PermissionObject with(AppCompatActivity activity) {
mAppCompatActivity = activity;
return new PermissionObject();
return new PermissionObject(activity);
}

public static PermissionObject with(Fragment fragment) {
return new PermissionObject(fragment);
}

public static class PermissionObject {

private AppCompatActivity mActivity;
private Fragment mFragment;

PermissionObject(AppCompatActivity activity) {
mActivity = activity;
}

PermissionObject(Fragment fragment) {
mFragment = fragment;
}

public boolean has(String permissionName) {
int permissionCheck;
if (mActivity != null) {
permissionCheck = ContextCompat.checkSelfPermission(mActivity, permissionName);
} else {
permissionCheck = ContextCompat.checkSelfPermission(mFragment.getContext(), permissionName);
}

return permissionCheck == PackageManager.PERMISSION_GRANTED;
}

public PermissionRequestObject request(String permissionName) {
return new PermissionRequestObject(new String[]{permissionName});
if (mActivity != null) {
return new PermissionRequestObject(mActivity, new String[]{permissionName});
} else {
return new PermissionRequestObject(mFragment, new String[]{permissionName});
}
}

public PermissionRequestObject request(String... permissionNames) {
return new PermissionRequestObject(permissionNames);
return new PermissionRequestObject(mActivity, permissionNames);
}
}

static public class PermissionRequestObject {

private static final String TAG = PermissionObject.class.getSimpleName();

private AppCompatActivity mActivity;
private Func mDenyFunc;
private Fragment mFragment;
private Func mGrantFunc;
private String[] mPermissionNames;
private ArrayList<SinglePermission> mPermissionsWeDontHave;
private Func3 mRationalFunc;
private int mRequestCode;
private Func mGrantFunc;
private Func mDenyFunc;
private Func2 mResultFunc;
private Func3 mRationalFunc;
private String[] mPermissionNames;

public PermissionRequestObject(String[] permissionNames) {
public PermissionRequestObject(AppCompatActivity activity, String[] permissionNames) {
mActivity = activity;
mPermissionNames = permissionNames;
}

public PermissionRequestObject(Fragment fragment, String[] permissionNames) {
mFragment = fragment;
mPermissionNames = permissionNames;
}

/**
* Execute the permission request with the given Request Code
*
* @param reqCode
* a unique request code in your activity
* @param reqCode a unique request code in your activity
*/
public PermissionRequestObject ask(int reqCode) {
mRequestCode = reqCode;
Expand All @@ -75,10 +109,16 @@ public PermissionRequestObject ask(int reqCode) {

if (needToAsk()) {
Log.i(TAG, "Asking for permission");
ActivityCompat.requestPermissions(mAppCompatActivity, mPermissionNames, reqCode);
if (mActivity != null) {
ActivityCompat.requestPermissions(mActivity, mPermissionNames, reqCode);
} else {
mFragment.requestPermissions(mPermissionNames, reqCode);
}
} else {
Log.i(TAG, "No need to ask for permission");
if (mGrantFunc != null) mGrantFunc.call();
if (mGrantFunc != null) {
mGrantFunc.call();
}
}
return this;
}
Expand All @@ -87,11 +127,23 @@ private boolean needToAsk() {
ArrayList<SinglePermission> neededPermissions = new ArrayList<>(mPermissionsWeDontHave);
for (int i = 0; i < mPermissionsWeDontHave.size(); i++) {
SinglePermission perm = mPermissionsWeDontHave.get(i);
int checkRes = ContextCompat.checkSelfPermission(mAppCompatActivity, perm.getPermissionName());
int checkRes;
if (mActivity != null) {
checkRes = ContextCompat.checkSelfPermission(mActivity, perm.getPermissionName());
} else {
checkRes = ContextCompat.checkSelfPermission(mFragment.getContext(), perm.getPermissionName());
}
if (checkRes == PackageManager.PERMISSION_GRANTED) {
neededPermissions.remove(perm);
} else {
if (ActivityCompat.shouldShowRequestPermissionRationale(mAppCompatActivity, perm.getPermissionName())) {
boolean shouldShowRequestPermissionRationale;
if (mActivity != null) {
shouldShowRequestPermissionRationale =
ActivityCompat.shouldShowRequestPermissionRationale(mActivity, perm.getPermissionName());
} else {
shouldShowRequestPermissionRationale = mFragment.shouldShowRequestPermissionRationale(perm.getPermissionName());
}
if (shouldShowRequestPermissionRationale) {
perm.setRationalNeeded(true);
}
}
Expand Down Expand Up @@ -151,9 +203,8 @@ public PermissionRequestObject onResult(Func2 resultFunc) {
* </pre>
*/
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
Log.i(TAG, String.format("ReqCode: %d, ResCode: %d, PermissionName: %s", requestCode, grantResults[0], permissions[0]));

if (mRequestCode == requestCode) {
Log.i(TAG, String.format("ReqCode: %d, ResCode: %d, PermissionName: %s", requestCode, grantResults[0], permissions[0]));
if (mResultFunc != null) {
Log.i(TAG, "Calling Results Func");
mResultFunc.call(requestCode, permissions, grantResults);
Expand All @@ -162,16 +213,15 @@ public void onRequestPermissionsResult(int requestCode, String permissions[], in

for (int i = 0; i < permissions.length; i++) {
if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
if (mPermissionsWeDontHave.get(i).isRationalNeeded()) {
if (mRationalFunc != null) {
Log.i(TAG, "Calling Rational Func");
mRationalFunc.call(mPermissionsWeDontHave.get(i).getPermissionName());
}
}
if (mDenyFunc != null) {
if (mPermissionsWeDontHave.get(i).isRationalNeeded() && mRationalFunc != null) {
Log.i(TAG, "Calling Rational Func");
mRationalFunc.call(mPermissionsWeDontHave.get(i).getPermissionName());
} else if (mDenyFunc != null) {
Log.i(TAG, "Calling Deny Func");
mDenyFunc.call();
} else Log.e(TAG, "NUll DENY FUNCTIONS");
} else {
Log.e(TAG, "NUll DENY FUNCTIONS");
}

// terminate if there is at least one deny
return;
Expand All @@ -182,7 +232,9 @@ public void onRequestPermissionsResult(int requestCode, String permissions[], in
if (mGrantFunc != null) {
Log.i(TAG, "Calling Grant Func");
mGrantFunc.call();
} else Log.e(TAG, "NUll GRANT FUNCTIONS");
} else {
Log.e(TAG, "NUll GRANT FUNCTIONS");
}
}
}
}
Expand Down
17 changes: 13 additions & 4 deletions app/src/main/java/com/kayvan/permissionutil/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CODE_CONTACTS = 1;
private static final int REQUEST_CODE_STORAGE = 2;
private static final int REQUEST_CODE_BOTH = 3;
public static final String WRITE_EXTERNAL_STORAGE = Manifest.permission.WRITE_EXTERNAL_STORAGE;
public static final String WRITE_CONTACTS = Manifest.permission.WRITE_CONTACTS;
@Bind(R.id.status) TextView mStatus;
private PermissionUtil.PermissionRequestObject mStoragePermissionRequest;
private PermissionUtil.PermissionRequestObject mContactsPermissionRequest;
Expand All @@ -29,9 +31,17 @@ public class MainActivity extends AppCompatActivity {
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
@OnClick(R.id.storage_check) public void onCheckStoragePermissionClick(){
boolean hasStoragePermission = PermissionUtil.with(this).has(WRITE_EXTERNAL_STORAGE);
updateStatus(hasStoragePermission?"Has Storage permission":"Doesn't have Storage permission");
}
@OnClick(R.id.contacts_check) public void onCheckContactsPermissionClick(){
boolean hasContactsPermission = PermissionUtil.with(this).has(WRITE_CONTACTS);
updateStatus(hasContactsPermission?"Has Contacts permission":"Doesn't have Contacts permission");
}

@OnClick(R.id.storage) public void onAskForStoragePermissionClick() {
mStoragePermissionRequest = PermissionUtil.with(this).request(Manifest.permission.WRITE_EXTERNAL_STORAGE).onAllGranted(
mStoragePermissionRequest = PermissionUtil.with(this).request(WRITE_EXTERNAL_STORAGE).onAllGranted(
new Func() {
@Override protected void call() {
doOnPermissionGranted("Storage");
Expand All @@ -47,7 +57,7 @@ public class MainActivity extends AppCompatActivity {

@OnClick(R.id.both) public void onAskBothPermissionsClick() {
mBothPermissionRequest =
PermissionUtil.with(this).request(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.WRITE_CONTACTS).onResult(
PermissionUtil.with(this).request(WRITE_EXTERNAL_STORAGE, WRITE_CONTACTS).onResult(
new Func2() {
@Override protected void call(int requestCode, String[] permissions, int[] grantResults) {
for (int i = 0; i < permissions.length; i++) {
Expand All @@ -60,8 +70,7 @@ public class MainActivity extends AppCompatActivity {
}

@OnClick(R.id.contacts) public void onAskForContactsPermissionClick() {
mContactsPermissionRequest = PermissionUtil.with(this).request(
Manifest.permission.WRITE_CONTACTS);
mContactsPermissionRequest = PermissionUtil.with(this).request(WRITE_CONTACTS);
mContactsPermissionRequest.onAllGranted(
new Func() {
@Override protected void call() {
Expand Down
Loading

0 comments on commit 4c1e8f4

Please sign in to comment.