Skip to content

Commit

Permalink
sample added and fixed some bugs #2
Browse files Browse the repository at this point in the history
  • Loading branch information
NoNews committed Sep 18, 2017
1 parent 838dfde commit a9d7c2c
Show file tree
Hide file tree
Showing 23 changed files with 296 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;

import java.util.ArrayList;
import java.util.List;

/**
* Date: 30.07.2017
* Time: 18:39
Expand All @@ -25,11 +28,11 @@ public class PermissionHelper {
private OnPermissionNewerAskAgainListener newerAskAgainListener;

public PermissionHelper(Activity activity) {
permissions = new String[1];
this.activity = activity;
}

public PermissionHelper check(String permission) {
this.permissions = new String[1];
this.permissions[0] = permission;
return this;
}
Expand All @@ -55,26 +58,50 @@ public PermissionHelper onNewerAskAgain(OnPermissionNewerAskAgainListener listen
return this;
}


public void run() {
if (isNeedAskPermission()) {
if (isListenersCorrect()) {
if (isNeedToAskPermissions()) {
checkPermissions();
} else {
successListener.onSuccess();
}
} else {
throw new RuntimeException("OnPermissionSuccessListener or OnPermissionFailureListener or OnPermissionNewerAskAgainListener not implemented. Use methods: onSuccess, onFailure and onNewerAskAgain");
}
}

if (successListener != null && failureListener != null && newerAskAgainListener != null) {
activity.requestPermissions(permissions, PERMISSION_REQUEST_CODE);
} else
throw new RuntimeException("OnPermissionSuccessListener or OnPermissionFailureListener not implemented. Use methods: onSuccess and onFailure");
@RequiresApi(api = Build.VERSION_CODES.M)
private void checkPermissions() {
final String[] permissionsForRequest = getPermissionsForRequest();
if (permissionsForRequest.length > 0) {
activity.requestPermissions(permissionsForRequest, PERMISSION_REQUEST_CODE);
} else {
successListener.onSuccess();
}
}

private boolean isNeedAskPermission() {

/**
* Check listeners for null
*/
private boolean isListenersCorrect() {
return successListener != null && failureListener != null && newerAskAgainListener != null;
}


/**
* We need to ask permission only if API >=23
*/
private boolean isNeedToAskPermissions() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
}

public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == PERMISSION_REQUEST_CODE) {
for (String permission : permissions) {
if (isPermissionNotGranted(permission)) {
if (isNeedAskPermission()) {

if (isNeedToAskPermissions()) {
if (isPermissionNotGranted(permission)) {
if (isNewerAskAgain(permission)) {
newerAskAgainListener.onNewerAskAgain();
} else {
Expand Down Expand Up @@ -111,6 +138,17 @@ public void unsubscribe() {
}
}

private String[] getPermissionsForRequest() {
List<String> permissionsForRequest = new ArrayList<>();
for (String permission : permissions) {
if (isPermissionNotGranted(permission)) {
permissionsForRequest.add(permission);
}
}
return permissionsForRequest.toArray(new String[permissionsForRequest.size()]);
}


public interface OnPermissionSuccessListener {
void onSuccess();
}
Expand Down
1 change: 1 addition & 0 deletions sample/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
35 changes: 35 additions & 0 deletions sample/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 26
buildToolsVersion "26.0.1"

defaultConfig {
applicationId "ru.alexbykov.permissionssample"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.+'
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'
}
25 changes: 25 additions & 0 deletions sample/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in C:\Users\reg\AppData\Local\Android\Sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ru.alexbykov.permissionssample;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
* Instrumentation test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() throws Exception {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();

assertEquals("ru.alexbykov.permissionssample", appContext.getPackageName());
}
}
24 changes: 24 additions & 0 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ru.alexbykov.permissionssample">

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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package ru.alexbykov.permissionssample;

import android.Manifest;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import ru.alexbykov.nopermission.PermissionHelper;

public class MainActivity extends AppCompatActivity {

private static final int LAYOUT = R.layout.activity_main;
private PermissionHelper permissionHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(LAYOUT);
setupPermissionHelper();
setupUX();
}

private void setupUX() {
findViewById(R.id.btnAskPermission).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
askLocationPermission();
}
});
}

private void askLocationPermission() {
permissionHelper.check(Manifest.permission.READ_CONTACTS, Manifest.permission.ACCESS_COARSE_LOCATION)
.onSuccess(new PermissionHelper.OnPermissionSuccessListener() {
@Override
public void onSuccess() {
((TextView) findViewById(R.id.tvResult)).setText("Location success");
}
})
.onFailure(new PermissionHelper.OnPermissionFailureListener() {
@Override
public void onFailure() {
((TextView) findViewById(R.id.tvResult)).setText("Location failure");
}
})
.onNewerAskAgain(new PermissionHelper.OnPermissionNewerAskAgainListener() {
@Override
public void onNewerAskAgain() {
((TextView) findViewById(R.id.tvResult)).setText("Location newer ask again");
}
})
.run();
}

private void setupPermissionHelper() {
permissionHelper = new PermissionHelper(this);
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
permissionHelper.onRequestPermissionsResult(requestCode, permissions, grantResults);
}

@Override
protected void onDestroy() {
permissionHelper.unsubscribe();
super.onDestroy();
}
}
27 changes: 27 additions & 0 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="ru.alexbykov.permissionssample.MainActivity">

<Button
android:text="@string/btn_ask_location_permission"
android:id="@+id/btnAskPermission"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>


<TextView
android:id="@+id/tvResult"
android:layout_marginTop="16dp"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btnAskPermission"/>

</RelativeLayout>
Binary file added sample/src/main/res/mipmap-hdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sample/src/main/res/mipmap-mdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sample/src/main/res/mipmap-xhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sample/src/main/res/mipmap-xxhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions sample/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>
4 changes: 4 additions & 0 deletions sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<resources>
<string name="app_name">PermissionsSample</string>
<string name="btn_ask_location_permission">Ask location permission</string>
</resources>
11 changes: 11 additions & 0 deletions sample/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>

</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ru.alexbykov.permissionssample;

import org.junit.Test;

import static org.junit.Assert.*;

/**
* Example local unit test, which will execute on the development machine (host).
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() throws Exception {
assertEquals(4, 2 + 2);
}
}
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include ':library'
include ':library', ':sample'

0 comments on commit a9d7c2c

Please sign in to comment.