Giter Site home page Giter Site logo

cordova-env's Introduction

cordova-plugin-env

Download Activity Travis CI Snyk
npm Build Status Known Vulnerabilities

A small Cordova plugin that exposes Android's Environment object.

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() {
    console.log(navigator.Env);
}

Installation

From the Command line:

cordova plugin add cordova-plugin-env

Config.xml for PhoneGap Build:

<gap:plugin name="cordova-plugin-env" source="npm" />

These commands will install the plugin from npm. You can find this plugin up on npm here, or by searching for ecosystem:cordova in the npm registry like this.

Supported Platform

  • Android

Env

The Env object provides a way to access the directories -- and some helper methods -- exposed by the Android Environment object.

Methods

Currently this plugin provides the following methods:

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

isExternalStorageEmulated

Parameters:

  • 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.isExternalStorageEmulated(
        function (result) {
            if (result) {
                console.log("isExternalStorageEmulated returns: " + result);
            }
        },
        function (error) {
            console.log("isExternalStorageEmulated error: " + error);
        }
    );
} else {
    console.log("Plugin error: Env plugin not found (is it installed?)");
}

isExternalStorageRemovable

Parameters:

  • 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.isExternalStorageRemovable(
        function (result) {
            if (result) {
                console.log("isExternalStorageRemovable returns: " + result);
            }
        },
        function (error) {
            console.log("isExternalStorageRemovable error: " + error);
        }
    );
} else {
    console.log("Plugin error: Env plugin not found (is it installed?)");
}

isExternalStorageManager

Parameters:

  • successCallback: Callback that returns "true" if the app has All Files Access.
  • errorCallback: Callback that executes if an error occurs during the call.

Example

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

getExternalStorageState

Parameters:

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:

  • directory: (string) Special directory to look up (see "Directories" below).
  • successCallback: Callback that returns the string name of the specified directory on this device.
  • errorCallback: Callback that executes if an error occurs during the call.

Directories

Following are valid string values for the directory parameter above:

String value Android directory
"Alarms" DIRECTORY_ALARMS
"DCIM" DIRECTORY_DCIM
"Documents" DIRECTORY_DOCUMENTS
"Downloads" DIRECTORY_DOWNLOADS
"Movies" DIRECTORY_MOVIES
"Music" DIRECTORY_MUSIC
"Notifications" DIRECTORY_NOTIFICATIONS
"Pictures" DIRECTORY_PICTURES
"Podcasts" DIRECTORY_PODCASTS
"Ringtones" DIRECTORY_RINGTONES

Example

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

getExternalStoragePublicDirectory

DEPRECATION WARNING This method has been deprecated on Android API 29 and later.

Parameters:

  • directory: (string) Directory to look up. This needs to be one of the strings returned by the getDirectory() call above.
  • successCallback: Callback that returns the full path string of the specified directory on this device.
  • errorCallback: Callback that executes if an error occurs during the call.

Example

if (navigator.Env) {
    console.log("Env object in navigator");
    // attempt to get Environment.DIRECTORY_DOWNLOADS
    navigator.Env.getDirectory("Downloads", 
        function (dirName) {
            if (dirName) {
                console.log("getDirectory(Downloads) returns: " + dirName);
                // success -- now try to get the full path for the shared Downloads directory
                navigator.Env.getExternalStoragePublicDirectory(dirName,
                    function(fullPath) {
                        console.log("getExternalStoragePublicDirectory(Downloads) returns: " + fullPath);
                    }, function (error) {
                        console.log("getExternalStoragePublicDirectory error: " + error);
                    }
                );
            }
        },
        function (error) {
            console.log("getDirectory error: " + error);
        }
    );
} else {
    console.log("Plugin error: Env plugin not found (is it installed?)");
}

cordova-env's People

Contributors

eb1 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

cordova-env's Issues

Full pah of Download directory

Is your feature request related to a problem? Please describe.
I want to have the full path of Downloads directory

Describe the solution you'd like
a full path of a directory

Describe alternatives you've considered
I tried this as described in the docs:

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

The problem is that path returns only Download and not the full path.
I'm doing cordova.file.externalRootDirectory + path but not sure if this is correct

Additional context

cordova info Output from my cordova info
$ cordova info
6.0.0
6.0.0

Cordova Packages:

    cli: 10.0.0
        common: 4.0.2
        create: 3.0.0
        lib: 10.0.0
            common: 4.0.2
            fetch: 3.0.0
            serve: 4.0.0

Project Installed Platforms:

    android: 9.0.0
    browser: null

Project Installed Plugins:

    cordova-pdf-generator: 2.1.1
    cordova-plugin-app-version: 0.1.12
    cordova-plugin-camera: 5.0.1
    cordova-plugin-compat: 1.2.0
    cordova-plugin-device: 2.0.3
    cordova-plugin-email-composer: 0.8.15
    cordova-plugin-env: 1.0
    cordova-plugin-file: 6.0.1
    cordova-plugin-geolocation: 2.4.3
    cordova-plugin-inappbrowser: 4.0.0
    cordova-plugin-is-debug: 1.0.0
    cordova-plugin-mobile-ocr: 3.1.2
    cordova-plugin-network-information: 2.0.2
    cordova-plugin-screen-orientation: 3.0.2
    cordova-plugin-simple-image-resizer: 0.1.1
    cordova-plugin-splashscreen: 6.0.0
    cordova-plugin-statusbar: 2.4.3
    cordova-plugin-whitelist: 1.3.4
    es6-promise-plugin: 4.2.2

Environment:

    OS: Ubuntu 18.04.5 LTS (linux 5.4.0-60-generic) x64
    Node: v14.15.4
    npm: 6.14.11

android Environment:

    android:
*************************************************************************
The "android" command is deprecated.
For manual SDK, AVD, and project management, please use Android Studio.
For command-line tools, use tools/bin/sdkmanager and tools/bin/avdmanager
*************************************************************************
Running /home/joao/Android/Sdk/tools/bin/avdmanager list target

Available Android targets:==============] 100% Fetch remote repository...       
----------
id: 1 or "android-24"
     Name: Android API 24
     Type: Platform
     API level: 24
     Revision: 2
----------
id: 2 or "android-25"
     Name: Android API 25
     Type: Platform
     API level: 25
     Revision: 3
----------
id: 3 or "android-26"
     Name: Android API 26
     Type: Platform
     API level: 26
     Revision: 2
----------
id: 4 or "android-27"
     Name: Android API 27
     Type: Platform
     API level: 27
     Revision: 3
----------
id: 5 or "android-28"
     Name: Android API 28
     Type: Platform
     API level: 28
     Revision: 6
----------
id: 6 or "android-29"
     Name: Android API 29
     Type: Platform
     API level: 29
     Revision: 4
----------
id: 7 or "android-R"
     Name: Android API 29, R preview (Preview)
     Type: Platform
     API level: R
     Revision: 4
----------
id: 8 or "android-30"
     Name: Android API 30
     Type: Platform
     API level: 30
     Revision: 3


Project Setting Files:

    config.xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<widget id="com.form.parking.violation" version="2.6.4" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
  <name>Denúncia Estacionamento</name>
  <description>
        Envio de queixa de estacionamento ilegal a autoridade competente
    </description>
  <author email="[email protected]" href="https://www.joaopimentel.com/">
        João Pimentel Ferreira
    </author>
  <content src="index.html"/>
  <icon height="512" src="res/icon/universal/icon4_512x512_playstore.png" width="512"/>
  <icon density="xhdpi" height="196" src="res/icon/universal/icon4_XHDPI_196x196_320dpi.png" width="196"/>
  <icon density="xxxhdpi" height="192" src="res/icon/universal/icon4_XXXHDPI_192x192_640dpi.png" width="192"/>
  <icon density="xxhdpi" height="144" src="res/icon/universal/icon4_XXHDPI_144x144_480dpi.png" width="144"/>
  <icon density="hdpi" height="72" src="res/icon/universal/icon4_HDPI_72x72_240dpi.png" width="72"/>
  <icon density="mdpi" height="48" src="res/icon/universal/icon4_MDPI_48x48_160dpi.png" width="48"/>
  <access origin="*"/>
  <allow-navigation href="*"/>
  <allow-intent href="http://*/*"/>
  <allow-intent href="https://*/*"/>
  <preference name="windows-target-version" value="10.0"/>
  <preference name="windows-phone-target-version" value="10.0"/>
  <preference name="iosExtraFilesystems" value="library,library-nosync,documents,documents-nosync,cache,bundle"/>
  <preference name="AndroidExtraFilesystems" value="files,files-external,documents,sdcard,cache,cache-external,assets"/>
  <preference name="StatusBarOverlaysWebView" value="false"/>
  <preference name="StatusBarBackgroundColor" value="#FFFFFF"/>
  <preference name="StatusBarStyle" value="blacktranslucent"/>
  <hook src="hooks/importNpmPackages.js" type="before_prepare"/>
  <hook src="hooks/copyCredentials.js" type="before_prepare"/>
  <hook src="hooks/convertHbsToHtml.js" type="after_prepare"/>
  <hook src="hooks/minifyFiles.js" type="after_prepare"/>
  <platform name="android">
    <preference name="android-minSdkVersion" value="22"/>
    <preference name="android-targetSdkVersion" value="29"/>
    <allow-intent href="market:*"/>
    <icon height="512" src="res/icon/android/512.png" width="512"/>
    <icon density="xhdpi" height="192" src="res/icon/android/192.png" width="192"/>
    <icon density="xxxhdpi" height="192" src="res/icon/android/192.png" width="192"/>
    <icon density="xxhdpi" height="144" src="res/icon/android/144.png" width="144"/>
    <icon density="hdpi" height="72" src="res/icon/android/72.png" width="72"/>
    <icon density="mdpi" height="48" src="res/icon/android/48.png" width="48"/>
    <splash density="hdpi" src="res/screen/android/screen-hdpi-portrait.png"/>
    <splash density="port-hdpi" src="res/screen/android/screen-hdpi-portrait.png"/>
    <splash density="ldpi" src="res/screen/android/screen-ldpi-portrait.png"/>
    <splash density="port-ldpi" src="res/screen/android/screen-ldpi-portrait.png"/>
    <splash density="mdpi" src="res/screen/android/screen-mdpi-portrait.png"/>
    <splash density="port-mdpi" src="res/screen/android/screen-mdpi-portrait.png"/>
    <splash density="xhdpi" src="res/screen/android/screen-xhdpi-portrait.png"/>
    <splash density="port-xhdpi" src="res/screen/android/screen-xhdpi-portrait.png"/>
  </platform>
  <platform name="ios">
    <allow-intent href="itms:*"/>
    <allow-intent href="itms-apps:*"/>
  </platform>
</widget>

    package.json:
--- Start of Cordova JSON Snippet ---
{
  "plugins": {
    "cordova-plugin-geolocation": {},
    "cordova-plugin-email-composer": {},
    "cordova-plugin-file": {},
    "cordova-plugin-statusbar": {},
    "cordova-plugin-screen-orientation": {},
    "cordova-plugin-device": {},
    "cordova-plugin-whitelist": {},
    "cordova-plugin-network-information": {},
    "cordova-plugin-compat": {},
    "cordova-pdf-generator": {},
    "cordova-plugin-splashscreen": {},
    "cordova-plugin-inappbrowser": {},
    "cordova-plugin-is-debug": {},
    "cordova-plugin-app-version": {},
    "cordova-plugin-camera": {
      "ANDROID_SUPPORT_V4_VERSION": "27.+"
    },
    "cordova-plugin-mobile-ocr": {},
    "cordova-plugin-simple-image-resizer": {
      "ANDROID_EXIFINTERFACES_VERSION": "27.+"
    },
    "cordova-plugin-env": {}
  },
  "platforms": [
    "browser",
    "android"
  ]
}
--- End of Cordova JSON Snippet ---

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.