Skip to content

Commit

Permalink
Merge pull request #373 from onaio/update-storage-permissions
Browse files Browse the repository at this point in the history
Resolve permission location for higher android builds.
  • Loading branch information
ellykits authored Aug 14, 2024
2 parents 5302bc6 + 0b56f7c commit c705b26
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 35 deletions.
26 changes: 13 additions & 13 deletions .github/workflows/library.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ jobs:
- name: Cancel previous workflow runs
uses: styfle/[email protected]
with:
access_token: ${{ github.token }}
access_token: ${{ github.token }}

- name: Checkout 🛎️
uses: actions/checkout@v2
with:
fetch-depth: 2

- name: Set up JDK 17
- name: Set up JDK 11
uses: actions/setup-java@v1
with:
java-version: 17
java-version: 11

- name: Create local.properties file
run: touch local.properties && echo "mapbox.repo.token=${{ secrets.MAPBOX_SDK_REPO_TOKEN }}" >> local.properties
Expand All @@ -55,15 +55,15 @@ jobs:
- name: Build project skipping tests
run: ./gradlew build -x test --stacktrace

- name: Run Instrumented Tests manually
uses: reactivecircus/android-emulator-runner@v2
with:
api-level: 22
script: ./gradlew :library:connectedDebug --stacktrace --info
- name: Run unit tests with Gradle
run: ./gradlew :library:clean :library:testDebugUnitTest --stacktrace
# - name: Run Instrumented Tests manually
# uses: reactivecircus/android-emulator-runner@v2
# with:
# api-level: 22
# script: ./gradlew :library:connectedDebug --stacktrace --info
#
# - name: Run unit tests with Gradle
# run: ./gradlew :library:clean :library:testDebugUnitTest --stacktrace

- name: Generate & upload library snapshot artifact AAR (Android Archive) file
run: ./gradlew :library:clean --stacktrace && ./gradlew :library:assembleRelease --stacktrace && ./gradlew :library:publishMavenJavaPublicationToMavenCentralRepository --stacktrace

Expand All @@ -73,4 +73,4 @@ jobs:
- name: Github Release
uses: softprops/action-gh-release@v1
with:
prerelease: ${{ contains(github.ref, '-alpha') || contains(github.ref, '-beta') }}
prerelease: ${{ contains(github.ref, '-alpha') || contains(github.ref, '-beta') }}
2 changes: 1 addition & 1 deletion configs.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ ext {
// Dependency and other versions
androidxTestCoreVersion = "1.4.0"
buildToolsVersion = "30.0.3"
compileSdkVersion = 30
compileSdkVersion = 34
jacocoVersion = "0.8.8"
junitVersion = "4.12"
mapboxAnnotationPluginVersion = "0.9.0"
Expand Down
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ apply plugin: 'realm-android'
apply plugin: 'com.jfrog.bintray'
apply plugin: 'maven-publish'

version '0.10.2-SNAPSHOT'
version '0.10.4-SNAPSHOT'

project.version = this.version

Expand Down
7 changes: 5 additions & 2 deletions library/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="32" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="32" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.util.Log;

import com.karumi.dexter.Dexter;
Expand All @@ -19,7 +20,7 @@ public static void checkPermissions(String TAG, Context context) {
MultiplePermissionsListener dialogMultiplePermissionListener = new KujakuMultiplePermissionListener(activity);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ public void onPermissionsChecked(MultiplePermissionsReport report) {
@Override public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();

if (context instanceof Activity) {
((Activity) context).finish();
}
}
})
.show();
Expand Down
26 changes: 19 additions & 7 deletions library/src/main/java/io/ona/kujaku/utils/Permissions.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

Expand All @@ -16,30 +18,40 @@
*/

public class Permissions {
private static final String[] CRITICAL_PERMISSIONS = new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};

/**
* Returns {@link android.content.pm.PermissionInfo#PROTECTION_DANGEROUS} permissions which
* have not been requested yet/denied by the user from the list of {@link Permissions#CRITICAL_PERMISSIONS}
* have not been requested yet/denied by the user from the list of {@link Permissions}
* required
*
* @param context
* @return list of unauthorised permissions
*/
public static String[] getUnauthorizedCriticalPermissions(Context context) {
String[] criticalPermissions;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { // Android 13 (API 33)
criticalPermissions = new String[]{
Manifest.permission.MANAGE_EXTERNAL_STORAGE
};
} else {
criticalPermissions = new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
}

List<String> unauthorizedPermissions = new ArrayList<>();
for (String curPermission : CRITICAL_PERMISSIONS) {
for (String curPermission : criticalPermissions) {
if (!check(context, curPermission)) {
unauthorizedPermissions.add(curPermission);
}
}

return unauthorizedPermissions.toArray(new String[]{});
return unauthorizedPermissions.toArray(new String[0]);
}


/**
* Checks if a specific application permission is authorised
*
Expand Down
3 changes: 3 additions & 0 deletions sample/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
/build

# Local configuration file (sdk path, etc)
local.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;
Expand Down Expand Up @@ -68,10 +69,6 @@ public class MainActivity extends BaseNavigationDrawerActivity {

private static final String SAMPLE_JSON_FILE_NAME = "2017-nov-27-kujaku-metadata.json";
private static final int PERMISSIONS_REQUEST_CODE = 9823;
private String[] basicPermissions = new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE
};

// Kujaku library uses notification ids 80 to 2080
private int lastNotificationId = 2081;
Expand Down Expand Up @@ -415,7 +412,8 @@ public void onErrorResponse(VolleyError error) {

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case MAP_ACTIVITY_REQUEST_CODE:
if (resultCode == Activity.RESULT_OK) {
// data from a dropped feature point
Expand Down Expand Up @@ -490,15 +488,27 @@ public String readAssetFile(String inFile) {

private void requestBasicPermissions() {
ArrayList<String> notGivenPermissions = new ArrayList<>();
String[] basicPermissions;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { // Android 13 (API 33)
basicPermissions = new String[]{
Manifest.permission.MANAGE_EXTERNAL_STORAGE
};
} else {
basicPermissions = new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
}

for (String permission : basicPermissions) {
if (!Permissions.check(this, permission)) {
notGivenPermissions.add(permission);
}
}

if (notGivenPermissions.size() > 0) {
Permissions.request(this, notGivenPermissions.toArray(new String[notGivenPermissions.size()]), PERMISSIONS_REQUEST_CODE);
if (!notGivenPermissions.isEmpty()) {
Permissions.request(this, notGivenPermissions.toArray(new String[0]), PERMISSIONS_REQUEST_CODE);
} else {
confirmSampleStyleAvailable();
}
Expand Down

0 comments on commit c705b26

Please sign in to comment.