Skip to content

Commit

Permalink
Merge pull request #378 from onaio/Fix-permission-prompt
Browse files Browse the repository at this point in the history
Fix permission prompt
  • Loading branch information
Lentumunai-Mark authored Oct 15, 2024
2 parents d80eaf3 + 5747f27 commit e5e4489
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 6 deletions.
6 changes: 3 additions & 3 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ buildscript {
apply plugin: 'realm-android'
apply plugin: 'maven-publish'

version '0.10.6-SNAPSHOT'
version '0.10.7-SNAPSHOT'

project.version = this.version

Expand Down Expand Up @@ -120,9 +120,9 @@ dependencies { configuration ->
implementation mapboxAnnotationPlugin

// Comment the line below when creating releases - The line is for development of the library & utils
//implementation (project(":utils")) {
implementation (project(":utils")) {
// Uncomment the line below when creating releases
implementation('io.ona.kujaku:utils:0.10.6-SNAPSHOT') {
//implementation('io.ona.kujaku:utils:0.10.7-SNAPSHOT') {
transitive = true;
exclude group: 'com.mapbox.mapboxsdk', module: 'mapbox-android-sdk'
exclude group: 'com.android.support', module: 'support-v4'
Expand Down
14 changes: 12 additions & 2 deletions library/src/main/java/io/ona/kujaku/helpers/PermissionsHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,33 @@
import com.karumi.dexter.Dexter;
import com.karumi.dexter.listener.multi.MultiplePermissionsListener;

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

import io.ona.kujaku.utils.KujakuMultiplePermissionListener;
import timber.log.Timber;

public class PermissionsHelper {

public static void checkPermissions(String TAG, Context context) {
if (context instanceof Activity) {
List<String> permissions = new ArrayList<>();
permissions.add(Manifest.permission.ACCESS_FINE_LOCATION);

if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.S) {
permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
final Activity activity = (Activity) context;

MultiplePermissionsListener dialogMultiplePermissionListener = new KujakuMultiplePermissionListener(activity);

Dexter.withActivity(activity)
.withPermissions(Manifest.permission.ACCESS_FINE_LOCATION, (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) ? Manifest.permission.MANAGE_EXTERNAL_STORAGE : Manifest.permission.WRITE_EXTERNAL_STORAGE)
.withPermissions(permissions)
.withListener(dialogMultiplePermissionListener)
.check();

} else {
Log.wtf(TAG, "KujakuMapView was not started in an activity!! This is very bad or it is being used in tests. We are going to ignore the permissions check! Good luck");
Timber.tag(TAG).wtf("KujakuMapView was not started in an activity!! This is very bad or it is being used in tests. We are going to ignore the permissions check! Good luck");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package io.ona.kujaku.helpers;

import static org.junit.Assert.assertFalse;

import android.app.Activity;
import android.content.Context;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import com.karumi.dexter.MultiplePermissionsReport;
import io.ona.kujaku.utils.KujakuMultiplePermissionListener;

@RunWith(RobolectricTestRunner.class)
public class PermissionsHelperTest {

@Mock
Context mockContext;

@Before
public void setUp() {
MockitoAnnotations.openMocks(this);
// Use a real context
mockContext = Robolectric.setupActivity(Activity.class).getApplicationContext();
}

@Test
public void testOnPermissionsCheckedWhenAnyPermissionPermanentlyDenied() {
MultiplePermissionsReport report = Mockito.mock(MultiplePermissionsReport.class);
Mockito.when(report.isAnyPermissionPermanentlyDenied()).thenReturn(true);
Mockito.when(report.areAllPermissionsGranted()).thenReturn(false);
KujakuMultiplePermissionListener listener = new KujakuMultiplePermissionListener(mockContext);
listener.onPermissionsChecked(report);

// Check that the dialog was created with the expected properties
Mockito.verify(report).isAnyPermissionPermanentlyDenied();
}

@Test
public void testOnPermissionsCheckedWhenAnyPermissionNotPermanentlyDenied() {
MultiplePermissionsReport report = Mockito.mock(MultiplePermissionsReport.class);
Mockito.when(report.isAnyPermissionPermanentlyDenied()).thenReturn(false);
Mockito.when(report.areAllPermissionsGranted()).thenReturn(false);
KujakuMultiplePermissionListener listener = new KujakuMultiplePermissionListener(mockContext);
listener.onPermissionsChecked(report);

boolean result = report.isAnyPermissionPermanentlyDenied();

// Use the result in your assertions or further logic
assertFalse(result);
}
}
2 changes: 1 addition & 1 deletion utils/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
apply plugin: 'maven-publish'
apply plugin: 'jacoco'

version '0.10.6-SNAPSHOT'
version '0.10.7-SNAPSHOT'
project.version = this.version


Expand Down

0 comments on commit e5e4489

Please sign in to comment.