Skip to content

Commit

Permalink
Add isExternalStorageManager method
Browse files Browse the repository at this point in the history
  • Loading branch information
eb1 committed Jan 20, 2021
1 parent 0e1a2cd commit fd00162
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 24 deletions.
68 changes: 47 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
|:-:|:-:|:-:|
| [![npm](https://img.shields.io/npm/dm/cordova-plugin-env.svg)](https://www.npmjs.com/package/cordova-plugin-env) | [![Build Status](https://travis-ci.org/adapt-it/cordova-env.svg?branch=master)](https://travis-ci.org/adapt-it/cordova-env) | [![Known Vulnerabilities](https://snyk.io/test/github/adapt-it/cordova-env/badge.svg)](https://snyk.io/test/github/adapt-it/cordova-env) |

A small Cordova plugin that exposes Android's [Environment object](https://developer.android.com/reference/android/os/Environment) directories and some methods (not complete).
A small Cordova plugin that exposes Android's [Environment](https://developer.android.com/reference/android/os/Environment) object.

This plugin defines a global `Environment` object, which provides access to the common directories available on Android's Environment object. The `Environment` object is available from the `navigator` object after the `deviceready` event fires.
This plugin defines a global `Environment` object, which provides access to the common directories and helper methods available on Android's Environment object. The `Environment` object is available from the `navigator` object after the `deviceready` event fires.

document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
Expand All @@ -51,83 +51,84 @@ These commands will install the plugin from npm. You can find this plugin up on

# Env

The `Env` object provides a way to access the directories exposed by the Environment object.
The `Env` object provides a way to access the directories -- and some helper methods -- exposed by the Android [Environment](https://developer.android.com/reference/android/os/Environment) object.

## Methods

Currently this plugin provides Four methods:
Currently this plugin provides the following methods:

- getExternalStorageState
- isExternalStorageEmulated
- isExternalStorageRemovable
- isExternalStorageManager
- getExternalStorageState
- getDirectory
- getExternalStoragePublicDirectory (deprecated in Android API 29, but still available)

### getExternalStorageState
### isExternalStorageEmulated

**Parameters:**

- **successCallback**: Callback that returns the string value of the External Storage State. See https://developer.android.com/reference/android/os/Environment.html#getExternalStorageState() for possible result values.
- **successCallback**: Callback that returns "true" if the external storage is emulated.
- **errorCallback:** Callback that executes if an error occurs during the call.

### Example

if (navigator.Env) {
console.log("Env object in navigator");
navigator.Env.getExternalStorageState(
function (state) {
if (state) {
console.log("External storage state: " + state);
navigator.Env.isExternalStorageEmulated(
function (result) {
if (result) {
console.log("isExternalStorageEmulated returns: " + result);
}
},
function (error) {
console.log("getExternalStorageState error: " + error);
console.log("isExternalStorageEmulated error: " + error);
}
);
} else {
console.log("Plugin error: Env plugin not found (is it installed?)");
}

### isExternalStorageEmulated
### isExternalStorageRemovable

**Parameters:**

- **successCallback**: Callback that returns "true" if the external storage is emulated.
- **successCallback**: Callback that returns "true" if the external storage is removable.
- **errorCallback:** Callback that executes if an error occurs during the call.

### Example

if (navigator.Env) {
console.log("Env object in navigator");
navigator.Env.isExternalStorageEmulated(
navigator.Env.isExternalStorageRemovable(
function (result) {
if (result) {
console.log("isExternalStorageEmulated returns: " + result);
console.log("isExternalStorageRemovable returns: " + result);
}
},
function (error) {
console.log("isExternalStorageEmulated error: " + error);
console.log("isExternalStorageRemovable error: " + error);
}
);
} else {
console.log("Plugin error: Env plugin not found (is it installed?)");
}

### isExternalStorageRemovable
### isExternalStorageManager

**Parameters:**

- **successCallback**: Callback that returns "true" if the external storage is removable.
- **successCallback**: Callback that returns "true" if the app has [All Files Access](https://developer.android.com/reference/android/provider/Settings?hl=en#ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION).
- **errorCallback:** Callback that executes if an error occurs during the call.

### Example

if (navigator.Env) {
console.log("Env object in navigator");
navigator.Env.isExternalStorageRemovable(
navigator.Env.isExternalStorageManager(
function (result) {
if (result) {
console.log("isExternalStorageRemovable returns: " + result);
console.log("isExternalStorageManager returns: " + result);
}
},
function (error) {
Expand All @@ -138,6 +139,31 @@ Currently this plugin provides Four methods:
console.log("Plugin error: Env plugin not found (is it installed?)");
}

### getExternalStorageState

**Parameters:**

- **successCallback**: Callback that returns the string value of the External Storage State. See https://developer.android.com/reference/android/os/Environment.html#getExternalStorageState() for possible result values.
- **errorCallback:** Callback that executes if an error occurs during the call.

### Example

if (navigator.Env) {
console.log("Env object in navigator");
navigator.Env.getExternalStorageState(
function (state) {
if (state) {
console.log("External storage state: " + state);
}
},
function (error) {
console.log("getExternalStorageState error: " + error);
}
);
} else {
console.log("Plugin error: Env plugin not found (is it installed?)");
}

### getDirectory

**Parameters:**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cordova-plugin-env",
"version": "1.1.0",
"version": "1.2.0",
"description": "Cordova Environment plugin",
"keywords": [
"cordova",
Expand Down
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version='1.0' encoding='utf-8'?>
<plugin id="cordova-plugin-env" version="1.1.0" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:rim="http://www.blackberry.com/ns/widgets" xmlns:android="http://schemas.android.com/apk/res/android">
<plugin id="cordova-plugin-env" version="1.2.0" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:rim="http://www.blackberry.com/ns/widgets" xmlns:android="http://schemas.android.com/apk/res/android">
<name>cordova-plugin-env</name>
<description>Cordova Environment plugin</description>
<author>Erik Brommers</author>
Expand Down
13 changes: 13 additions & 0 deletions src/android/Env.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public class Env extends CordovaPlugin {
public static final String EXTERNALSTORAGEEMULATED = "isExternalStorageEmulated";
public static final String EXTERNALSTORAGEREMOVABLE = "isExternalStorageRemovable";
public static final String GETEXTERNALSTORAGEPUBLICDIRECTORY = "getExternalStoragePublicDirectory";
public static final String EXTERNALSTORAGEMANAGER = "isExternalStorageManager";

public static final String GETDIRECTORY = "getDirectory";
public static final String DIRECTORY_ALARMS = "Alarms";
public static final String DIRECTORY_DCIM = "DCIM";
Expand Down Expand Up @@ -106,6 +108,17 @@ public void run() {
}
);
return true;
} else if (action.equals(EXTERNALSTORAGEMANAGER)) {
cordova.getThreadPool().execute(
new Runnable() {
public void run() {
final String results = String.valueOf(Environment.isExternalStorageManager());
System.out.println("results: " + results.toString());
callbackContext.success(results);
}
}
);
return true;
} else {
return false;
}
Expand Down
16 changes: 15 additions & 1 deletion www/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,21 @@ var Env = {
isExternalStorageRemovable: function (successCB, errorCB) {
exec(successCB, errorCB, "Env", "isExternalStorageRemovable", []);
},

/**
* Returns whether the calling app has All Files Access on the primary shared / external storage media.
*
* @param {function} successCB
* @param {function} errorCB
*
* @return Object.value {String}: "true" if the calling app has All Files Access.
*
* Example
* Env.isExternalStorageManager(function(isRStorageManager) {console.log(isStorageManager);},
* function(error) {console.log(error);});
*/
isExternalStorageManager: function (successCB, errorCB) {
exec(successCB, errorCB, "Env", "isExternalStorageManager", []);
},
/**
* Returns the full path to the specified special directory. Note that this API
* is deprecated in Android API 29.
Expand Down

0 comments on commit fd00162

Please sign in to comment.