Skip to content

Commit

Permalink
Never ask again callback now not required, and some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
NoNews committed Oct 11, 2017
1 parent 5b96990 commit 7bafd35
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 46 deletions.
6 changes: 4 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.1'
classpath 'com.novoda:bintray-release:0.3.4'
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.novoda:bintray-release:0.5.0'


// NOTE: Do not place your application dependencies here; they belong
Expand All @@ -17,6 +18,7 @@ buildscript {
allprojects {
repositories {
jcenter()
google()
}
}

Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Sun Jul 30 18:37:38 MSK 2017
#Wed Oct 11 10:27:50 MSK 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
4 changes: 2 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ apply plugin: 'com.novoda.bintray-release'

android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
buildToolsVersion '26.0.2'

defaultConfig {
minSdkVersion 15
Expand Down Expand Up @@ -42,7 +42,7 @@ dependencies {
publish {
groupId = 'ru.alexbykov'
artifactId = 'nopermission'
publishVersion = '1.0.8'
publishVersion = '1.1.0'
desc = 'Permission library for Android'
licences = ['Apache-2.0']
website = 'https://github.com/NoNews/NoPermission'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void run() {
successListener.run();
}
} else {
throw new RuntimeException("OnPermissionSuccessListener or OnPermissionFailureListener or OnPermissionNewerAskAgainListener not implemented. Use methods: onSuccess, onFailure and onNewerAskAgain");
throw new RuntimeException("OnPermissionSuccessListener or OnPermissionFailureListener not installed. Please, use onSuccess and onFailure methods");
}
}

Expand All @@ -121,7 +121,7 @@ private void checkPermissions() {
* Check listeners for null
*/
private boolean isListenersCorrect() {
return successListener != null && failureListener != null && neverAskAgainListener != null;
return successListener != null && failureListener != null;
}


Expand All @@ -148,32 +148,40 @@ private String[] getPermissionsForRequest() {


/**
* if permission not granted, check newerAskAgain, else call failure
* if permission not granted, check neverAskAgain, else call failure
* if permission grander, call success
*
* @param grantResults Permissions, which granted
* @param permissions Permissions, which you asked
* @param requestCode requestCode of out request
*/
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == PERMISSION_REQUEST_CODE) {
if (requestCode == PERMISSION_REQUEST_CODE && isNeedToAskPermissions()) {
for (String permission : permissions) {
if (isNeedToAskPermissions()) {
if (isPermissionNotGranted(permission)) {
if (isNeverAskAgain(permission)) {
neverAskAgainListener.run();
} else {
failureListener.run();
}
return;
if (isPermissionNotGranted(permission)) {
if (isNeverAskAgain(permission)) {
runNeverAskAgain();
} else {
failureListener.run();
}
return;
}
}
}
successListener.run();
}


/**
* run never ask again callback
*/
private void runNeverAskAgain() {
if (neverAskAgainListener != null) {
neverAskAgainListener.run();
}
}


/**
* @param permission for check
* @return true if permission granted and false if permission not granted
Expand All @@ -196,12 +204,8 @@ private boolean isNeverAskAgain(String permission) {
*/
public void unsubscribe() {
activity = null;
if (failureListener != null) {
failureListener = null;
}
if (successListener != null) {
successListener = null;
}
failureListener = null;
successListener = null;
if (neverAskAgainListener != null) {
neverAskAgainListener = null;
}
Expand Down
4 changes: 2 additions & 2 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.application'

android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
buildToolsVersion '26.0.2'

defaultConfig {
applicationId "ru.alexbykov.permissionssample"
Expand Down Expand Up @@ -31,5 +31,5 @@ dependencies {
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile project(':library')
// compile 'ru.alexbykov:nopermission:1.0.7'
// compile 'ru.alexbykov:nopermission:1.1.0'
}
1 change: 1 addition & 0 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package="ru.alexbykov.permissionssample">

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>

<application
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import ru.alexbykov.nopermission.PermissionHelper;

public class MainActivity extends AppCompatActivity {


private final String TAG = "PermissionResult: ";
private static final int LAYOUT = R.layout.activity_main;
private PermissionHelper permissionHelper;

Expand All @@ -33,26 +36,26 @@ public void onClick(View view) {
}

private void askLocationPermission() {
permissionHelper.check(Manifest.permission.READ_CONTACTS, Manifest.permission.ACCESS_COARSE_LOCATION)
.onSuccess(new Runnable() {
@Override
public void run() {
((TextView) findViewById(R.id.tvResult)).setText("Location success");
}
})
.onFailure(new Runnable() {
@Override
public void run() {
((TextView) findViewById(R.id.tvResult)).setText("Location failure");
}
})
.onNeverAskAgain(new Runnable() {
@Override
public void run() {
((TextView) findViewById(R.id.tvResult)).setText("Location newer ask again");
}
})
.run();
permissionHelper.check(Manifest.permission.ACCESS_COARSE_LOCATION).onSuccess(new Runnable() {
@Override
public void run() {
Log.d(TAG, "LocationSuccess");
((TextView) findViewById(R.id.tvResult)).setText(R.string.result_success);
}
}).onFailure(new Runnable() {
@Override
public void run() {
Log.d(TAG, "LocationFailure");
((TextView) findViewById(R.id.tvResult)).setText(R.string.result_failure);
}
}).onNeverAskAgain(new Runnable() {
@Override
public void run() {
Log.d(TAG, "LocationNeverAskAgain");
((TextView) findViewById(R.id.tvResult)).setText(R.string.result_never_ask_again);
}
}).run();

}

private void setupPermissionHelper() {
Expand Down
3 changes: 3 additions & 0 deletions sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<resources>
<string name="app_name">PermissionsSample</string>
<string name="btn_ask_location_permission">Ask location permission</string>
<string name="result_success">Location success</string>
<string name="result_failure">Locration failure</string>
<string name="result_never_ask_again">Location never ask again</string>
</resources>

0 comments on commit 7bafd35

Please sign in to comment.