This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[android] check external database's read/write permissions asynchrono…
…usly
- Loading branch information
1 parent
5e747ea
commit 22c2c88
Showing
2 changed files
with
147 additions
and
13 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
121 changes: 121 additions & 0 deletions
121
platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils/FileUtils.java
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 |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package com.mapbox.mapboxsdk.utils; | ||
|
||
import android.os.AsyncTask; | ||
|
||
import java.io.File; | ||
import java.lang.ref.WeakReference; | ||
|
||
public class FileUtils { | ||
|
||
/** | ||
* Task checking whether app's process can read a file. | ||
*/ | ||
public static class CheckFileReadPermissionTask extends AsyncTask<File, Void, Boolean> { | ||
private final WeakReference<OnCheckFileReadPermissionListener> listenerWeakReference; | ||
|
||
public CheckFileReadPermissionTask(OnCheckFileReadPermissionListener listener) { | ||
this.listenerWeakReference = new WeakReference<>(listener); | ||
} | ||
|
||
@Override | ||
protected Boolean doInBackground(File... files) { | ||
try { | ||
return files[0].canRead(); | ||
} catch (Exception ex) { | ||
return false; | ||
} | ||
} | ||
|
||
@Override | ||
protected void onCancelled() { | ||
OnCheckFileReadPermissionListener listener = listenerWeakReference.get(); | ||
if (listener != null) { | ||
listener.onError(); | ||
} | ||
} | ||
|
||
@Override | ||
protected void onPostExecute(Boolean result) { | ||
OnCheckFileReadPermissionListener listener = listenerWeakReference.get(); | ||
if (listener != null) { | ||
if (result) { | ||
listener.onReadPermissionGranted(); | ||
} else { | ||
listener.onError(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Interface definition for a callback invoked when checking file's read permissions. | ||
*/ | ||
public interface OnCheckFileReadPermissionListener { | ||
|
||
/** | ||
* Invoked when app's process has a permission to read a file. | ||
*/ | ||
void onReadPermissionGranted(); | ||
|
||
/** | ||
* Invoked when app's process doesn't have a permission to read a file or an error occurs. | ||
*/ | ||
void onError(); | ||
} | ||
|
||
/** | ||
* Task checking whether app's process can write to a file. | ||
*/ | ||
public static class CheckFileWritePermissionTask extends AsyncTask<File, Void, Boolean> { | ||
private final WeakReference<OnCheckFileWritePermissionListener> listenerWeakReference; | ||
|
||
public CheckFileWritePermissionTask(OnCheckFileWritePermissionListener listener) { | ||
this.listenerWeakReference = new WeakReference<>(listener); | ||
} | ||
|
||
@Override | ||
protected Boolean doInBackground(File... files) { | ||
try { | ||
return files[0].canWrite(); | ||
} catch (Exception ex) { | ||
return false; | ||
} | ||
} | ||
|
||
@Override | ||
protected void onCancelled() { | ||
OnCheckFileWritePermissionListener listener = listenerWeakReference.get(); | ||
if (listener != null) { | ||
listener.onError(); | ||
} | ||
} | ||
|
||
@Override | ||
protected void onPostExecute(Boolean result) { | ||
OnCheckFileWritePermissionListener listener = listenerWeakReference.get(); | ||
if (listener != null) { | ||
if (result) { | ||
listener.onWritePermissionGranted(); | ||
} else { | ||
listener.onError(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Interface definition for a callback invoked when checking file's write permissions. | ||
*/ | ||
public interface OnCheckFileWritePermissionListener { | ||
|
||
/** | ||
* Invoked when app's process has a permission to write to a file. | ||
*/ | ||
void onWritePermissionGranted(); | ||
|
||
/** | ||
* Invoked when app's process doesn't have a permission to write to a file or an error occurs. | ||
*/ | ||
void onError(); | ||
} | ||
} |