-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from LolHens/feature/android11
Android 11 Support
- Loading branch information
Showing
17 changed files
with
167 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,82 @@ | ||
package de.lolhens.resticui | ||
|
||
import android.app.Activity | ||
import android.Manifest.permission.READ_EXTERNAL_STORAGE | ||
import android.Manifest.permission.WRITE_EXTERNAL_STORAGE | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.content.pm.PackageManager | ||
import android.net.Uri | ||
import android.os.Build | ||
import android.os.Build.VERSION.SDK_INT | ||
import android.os.Environment | ||
import android.provider.Settings | ||
import androidx.activity.ComponentActivity | ||
import androidx.activity.result.contract.ActivityResultContracts | ||
import androidx.core.app.ActivityCompat | ||
import androidx.core.content.ContextCompat | ||
import java.util.* | ||
import java.util.concurrent.CompletableFuture | ||
import java.util.concurrent.ConcurrentLinkedQueue | ||
|
||
|
||
object Permissions { | ||
fun granted(context: Context, permission: String): Boolean = | ||
ContextCompat.checkSelfPermission( | ||
context, | ||
permission | ||
) == PackageManager.PERMISSION_GRANTED | ||
|
||
private var callbacks: Map<String, CompletableFuture<Boolean>> = emptyMap() | ||
|
||
fun onRequestPermissionsResult(permissions: Array<out String>, grantResults: IntArray) { | ||
val permission = permissions[0] | ||
val granted = | ||
grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED | ||
|
||
val callback = callbacks.get(permission) | ||
if (callback != null) { | ||
synchronized(this) { | ||
callbacks = callbacks.minus(permission) | ||
// https://stackoverflow.com/questions/62782648/android-11-scoped-storage-permissions | ||
fun hasStoragePermission(context: Context, write: Boolean): Boolean = | ||
if (SDK_INT >= Build.VERSION_CODES.R) { | ||
Environment.isExternalStorageManager() | ||
} else { | ||
ContextCompat.checkSelfPermission( | ||
context, | ||
READ_EXTERNAL_STORAGE | ||
) == PackageManager.PERMISSION_GRANTED && | ||
(!write || ContextCompat.checkSelfPermission( | ||
context, | ||
WRITE_EXTERNAL_STORAGE | ||
) == PackageManager.PERMISSION_GRANTED) | ||
} | ||
|
||
callback.complete(granted) | ||
} | ||
} | ||
private const val PERMISSION_REQUEST_CODE = 2290 | ||
|
||
fun request( | ||
activity: Activity, | ||
permission: String | ||
): CompletableFuture<Boolean> = | ||
if (granted(activity, permission)) { | ||
CompletableFuture.completedFuture(true) | ||
} else { | ||
// Requesting the permission | ||
val future = CompletableFuture<Boolean>() | ||
private var legacyCallbacks: Queue<CompletableFuture<Unit>> = ConcurrentLinkedQueue() | ||
|
||
synchronized(this) { | ||
callbacks = callbacks.plus(Pair(permission, future)) | ||
fun requestStoragePermission(activity: ComponentActivity, write: Boolean): CompletableFuture<Boolean> { | ||
val future = CompletableFuture<Unit>() | ||
|
||
if (SDK_INT >= Build.VERSION_CODES.R) { | ||
val resultLauncher = activity.registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { | ||
future.complete(Unit) | ||
} | ||
|
||
ActivityCompat.requestPermissions(activity, arrayOf(permission), 0) | ||
try { | ||
val intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION) | ||
intent.addCategory("android.intent.category.DEFAULT") | ||
intent.data = Uri.parse(String.format("package:%s", activity.packageName)) | ||
resultLauncher.launch(intent) | ||
} catch (e: Exception) { | ||
val intent = Intent() | ||
intent.action = Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION | ||
resultLauncher.launch(intent) | ||
} | ||
} else { | ||
//below android 11 | ||
legacyCallbacks.offer(future) | ||
|
||
future | ||
ActivityCompat.requestPermissions( | ||
activity, | ||
arrayOf(WRITE_EXTERNAL_STORAGE), | ||
PERMISSION_REQUEST_CODE | ||
) | ||
} | ||
|
||
return future.thenApply { | ||
hasStoragePermission(activity, write) | ||
} | ||
} | ||
|
||
fun onRequestPermissionsResult(requestCode: Int) { | ||
when (requestCode) { | ||
PERMISSION_REQUEST_CODE -> | ||
legacyCallbacks.poll()?.complete(Unit) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.