Permission: Permission Management

更新时间:
2024-07-24
下载文档

Permission: Permission Management

App front end can obtain and check related permissions. If the deactive state App has permission to change, it cannot be obtained through the permission event.

The EdgerOS permission object contains the following members (different versions of EdgerOS may be different):

Edger system permissions

  • ainn {Boolean} Whether AI Neural Network Computing is allowed.
  • alarm {Boolean} Is there permission to add alarms.
  • share {Boolean} Whether this app allows to share of information with other apps.
  • notify {Boolean} Whether this app allows to push messages.
  • advnwc {Boolean} Whether this app allows advanced network control.
  • network {Boolean} Whether this app allows network communication.
  • display {Boolean} Whether this app allows the use of display output.
  • rtsp {Boolean} Whether this app allows RTSP network, such as Webcam, Network microphone.
  • lora {Boolean} Whether this app allows to send or receive data via LoRaWAN network.
  • coap {Boolean} Whether this app allows CoAP IoT network protocol.
  • wallpaper {Boolean} Whether this app allows wallpaper settings.
  • account {Boolean} Whether to allow applications to get the user and group lists.
  • printer {Boolean} Whether this app allows to use printer.
  • auxstorage {Boolean} Whether this app allows to use auxiliary storage.
  • vpn {Boolean} Whether this app allows to create and manage VPN networks (temporarily closed).
  • mqtt {Object} MQTT Client sub object.
    • publish {Boolean} Whether to allow applications to publish data using the MQTT protocol.
    • subscribe {Boolean} Whether to allow applications to subscribe to messages using the MQTT protocol.
  • mediacenter {Object} Media Center access permission.
    • readable {Boolean} Can read media center content.
    • writable {Boolean} Can write media center content.
    • removable {Boolean} Can remove media center content.

Terminal device permissions

  • devices {Array} List of allowed device IDs.

Terminal native permissions

  • phone {Object} Mobile phone features.
    • camera {Boolean} Mobile phone camera.
    • contacts {Boolean} Mobile phone contacts.
    • microphone {Boolean} Mobile phone microphone and midi input.
    • geolocation {Boolean} Mobile phone geolocation.
    • media {Boolean} Mobile phone album.
    • file {Boolean} Mobile phone file.
    • browser {Boolean} Whether to allow applications to open the browser.

Vehicle permissions

  • vehicle {Object} Vehicle features.
    • media {Boolean} Audio-visual entertainment system.
    • geolocation {Boolean} Vehicle geolocation.
    • diagnostic {Boolean} Vehicle diagnostic information.
    • cockpit {Boolean} Cockpit controls, such as air conditioning, car windows.
    • drive {Boolean} Driving and autonomous driving related functions.

Developers can determine whether these permissions work in the EdgerOS mobile App environment through the following code:

edger.env().then(data => {
  if (data.env === 'edgerapp') {
    // We work in EdgerOS mobile App environment.
  }
}).catch(error => {
  console.error(error);
});

Functions

edger.permission.request(option)

  • option {Object} List of permissions to be obtained.
  • Returns: {Promise} Promise object.

Requesting EdgerOS administor to activate the specified permissions for the current App.

option must contain the following members:

  • type {String} Permission types: permissions, devices, phone or vehicle. Currently, the vehicle type is not supported.
  • code {Array} List of requested permission codes.

The corresponding table of permission request type and code is as follows:

typecode
permissionsainn, alarm, share, notify, advnwc, network, display, rtsp, lora, coap, wallpaper, account, printer, auxstorage, vpn, mqtt.publish, mqtt.subscribe, mediacenter.readable, mediacenter.writable, mediacenter.removable
devicesList of allowed device IDs
phonecamera, microphone, geolocation , browser, media, file, contacts
vehiclemedia, geolocation, diagnostic, cockpit, drive

The code array is an array of strings, for example:

  • ['ainn'] Requesting AI computing support.
  • ['network', 'notify', 'mediacenter.readable'] Requesting three permissions: network communication, message notification and media center read.

This function can only pop up the permission reminder dialog box, and it cannot guarantee that the EdgerOS user grants permission.

Example

// Apply for edger system permission
edger.permission.request({
  code: ['network', 'notify', 'mediacenter.readable'],
  type: 'permissions'
}).then((data) => {
  // data.success is 'true' means pops up successfully
}).catch(error => {
  console.error(error);
});

// Apply for terminal native permission
edger.permission.request({
  code: ['camera', `microphone`, 'geolocation'],
  type: 'phone'
}).then((data) => {
  // data.success is 'true' means pops up successfully
}).catch(error => {
  console.error(error);
});

edger.permission.fetch([params])

  • params {Array} List of type permissions to be fetched. Optional.
  • Returns: {Promise} Promise object. When params is null or undefined, will return all permission results, otherwise will return depend on params.

params is an array, each element must contain the following members:

  • type {String} Permission types: permissions, devices, phone or vehicle. Currently, the vehicle type is not supported.
  • code {Array} List of requested permission codes.

The corresponding table of permission request type and code is as follows:

typecode
permissionsainn, alarm, share, notify, advnwc, network, display, rtsp, lora, coap, wallpaper, account, printer, auxstorage, vpn, mqtt.publish, mqtt.subscribe, mediacenter.readable, mediacenter.writable, mediacenter.removable
devicesList of allowed device IDs.
phonecamera, microphone, geolocation , browser, media, file, contacts
vehiclemedia, geolocation, diagnostic, cockpit, drive

The code array is an array of strings, for example:

  • ['ainn'] Requesting AI computing support.
  • ['network', 'notify', 'mediacenter.readable'] Requesting three permissions: network communication, message notification and media center read.

Fetch current App permission table.

Example

const params = [
  { type: 'devices', code: [] },
  { type: 'phone', code: ['geolocation', 'media'] },
  { type: 'permissions', code: ['mediacenter.readable', 'mqtt.publish', 'share'] }
]
edger.permission.fetch(params).then((data) => {
  // data contains a complete permission table
  if (data.share) {
    console.log('We have share permission');
  }
  if (data.mediacenter.readable) {
    console.log('We have mediacenter.readable permission');
  }
  if (data.devices.includes('xxxx')) {
    console.log('We have device xxx permission');
  }
}).catch(error => {
  console.error(error);
});

Events

The unified event listener provided by Web-SDK:

const listener = (payload) => {
  // Event handling...
}

// add listener
edger.addEventListener('some-event', listener);

// or 
// onAction() is an alias of addEventListener().
edger.onAction('some-event', listener);

// remove listener
edger.removeEventListener('some-event', listener);

// remove all listeners
edger.removeAllListeners();

permission

When permission changes, EdgerOS will send this event, and developers need to listen to this event to get the latest permission status.

Example

const listener = (payload) => {
  // This payload is same with edger.permission.fetch()
  if (payload.share) {
    console.log('We have share permission');
  }
}

edger.addEventListener('permission', listener);

Example

Developers can request permissions in the following ways:

const weWantPerms = ['network', 'notify'];
const weHavePerms = false;

// Try to apply for permission
async function request() {
  const remind = [];
  const perm = await edger.permission.fetch();
  for (let code of weWantPerms) {
    if (!perm[code]) {
      remind.push(code);
    }
  }

  // Some of the permissions we need do not exist
  if (remind.length) {
    const ret = await edger.permission.request({
      code: weWantPerms,
      type: 'permissions'
    });
    if (!ret.success) {
      // Here you can remind users to set related permissions.
      // If the user agrees, you can share the message to the setting for permission update
      if (/* Waiting for user consent remind */) {
        edger.app.open({
          id: 'com.acoinfo.setting'
        }, {
          type: 'app-permission', msg: {}
        }).then(() => {}).catch(error => {
          console.error(error);
        });
      }
    }
  } else {
    weHavePerms = true;
  }
}

// Permission update
const listener = (payload) => {
  for (let code of weWantPerms) {
    if (!payload[code]) {
      weHavePerms = false;
      return;
    }
  }
  weHavePerms = true;
}
edger.addEventListener('permission', listener);
文档内容是否对您有所帮助?
有帮助
没帮助