Skip to content

Commit

Permalink
Changed permission classes privacy
Browse files Browse the repository at this point in the history
  • Loading branch information
NastiaGusev committed Jul 25, 2021
1 parent 6282763 commit d6ffae3
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 12 deletions.
6 changes: 6 additions & 0 deletions .idea/vcs.xml

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

Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@

import androidx.activity.result.ActivityResultLauncher;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class MyPermissions {

private Context context;
public Utils utils;
public CameraPermission cameraPermission;
public ContactsPermission contactsPermission;
public LocationPermission locationPermission;
public MicrophonePermission microphonePermission;
private CameraPermission cameraPermission;
private ContactsPermission contactsPermission;
private LocationPermission locationPermission;
private MicrophonePermission microphonePermission;

public Utils utils;
public String[] permissions;

public ActivityResultLauncher<String[]> multiplePermissionsLauncher;
public ActivityResultLauncher<Intent> requestManualPermissionLauncher;

Expand Down Expand Up @@ -67,19 +69,46 @@ private String generateDenyMessage(String[] permissions) {
*/
public void launchRequestPermissions(String[] perm) {
this.permissions = perm;
checkLocationPermissions();
if (permissions.length > 0) {
multiplePermissionsLauncher.launch(permissions);
}
}

/**
* Check if the location permissions are requested and add the necessary permissions for the request
*/
private void checkLocationPermissions() {
List<String> temp = Arrays.asList(this.permissions);
List<String> newPermissions = new ArrayList<>();
Iterator<String> iter = temp.iterator();
boolean addLocationPerm = false;

while (iter.hasNext()) {
String perm = iter.next();
if (perm.equals(Manifest.permission.ACCESS_FINE_LOCATION) || perm.equals(Manifest.permission.ACCESS_COARSE_LOCATION)
|| perm.equals(Manifest.permission.ACCESS_BACKGROUND_LOCATION)) {
addLocationPerm = true;
}else {
newPermissions.add(perm);
}
}

if (addLocationPerm) {
newPermissions.addAll(locationPermission.requestPermissionLocation());
this.permissions = newPermissions.toArray(new String[0]);
}
}


/**
* Launch request for settings dialog
*
* @param manualPermissions String array of permissions to request manually
*/
public void launchManualSettingsDialog(List<String> manualPermissions) {
if (manualPermissions.size() > 0) {
String[] permissions = manualPermissions.toArray(new String[0]);
utils.openPermissionSettingDialog(generateManualMessage(permissions), generateDenyMessage(permissions));
public void launchManualSettingsDialog(String[] manualPermissions) {
if (manualPermissions.length > 0) {
utils.openPermissionSettingDialog(generateManualMessage(manualPermissions), generateDenyMessage(manualPermissions));
}
}

Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ android {
}

dependencies {

implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
implementation project(path: ':PermissionLibrary')
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.permissionlibraryexample">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_CONTACTS" />

<application
android:allowBackup="true"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,70 @@
package com.example.permissionlibraryexample;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;

import com.example.permissionlibrary.MyPermissions;

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

public class MainActivity extends AppCompatActivity {

MyPermissions myPermissions;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

myPermissions = new MyPermissions(this, multiplePermissionLauncher, manuallyPermissionLauncher);
myPermissions.launchRequestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.READ_CONTACTS});
}

/**
* Launch request for a list of permissions
*/
public ActivityResultLauncher<String[]> multiplePermissionLauncher = registerForActivityResult
(new ActivityResultContracts.RequestMultiplePermissions(), (Map<String, Boolean> isGranted) -> {
//Iterate over the permissions - and check if they are granted
List<String> grantedPermissions = new ArrayList<>();
List<String> manualPermissions = new ArrayList<>();
for (Map.Entry<String, Boolean> permissionEntry : isGranted.entrySet()) {
if (permissionEntry.getValue()) {
grantedPermissions.add(permissionEntry.getKey());
} else {
//Check if the user denied asking again - if not ask again for permission
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, permissionEntry.getKey())) {
myPermissions.requestPermission(permissionEntry.getKey());
} else {
//If the user chose not to ask again, we add the permission to list of manual permissions
manualPermissions.add(permissionEntry.getKey());
}
}
}
myPermissions.printGrantedPermissions(grantedPermissions);
myPermissions.launchManualSettingsDialog(manualPermissions.toArray(new String[0]));
});

/**
* Launch request for settings dialog for requesting manual permission
*/
public ActivityResultLauncher<Intent> manuallyPermissionLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
// //After we return from the settings page - check each permission if it was granted
myPermissions.checkPermissions();
}
});
}

0 comments on commit d6ffae3

Please sign in to comment.