Skip to content
rolinger edited this page Nov 17, 2016 · 11 revisions

Welcome to the DeviceInformationPlugin wiki!

For Android 6 and greater this plugin will need TWO permissions, and the two permissions must be secured in order or it will fail.

In this example, we are using cordova-plugin-diagnostic to check for permissions and, if necessary, ask for permissions:

Define your promises:

function getReadPhoneAuth(){
  var q = $q.defer();
  cordova.plugins.diagnostic.getPermissionAuthorizationStatus(function(status) {
    if (status == "NOT_REQUESTED") {
      cordova.plugins.diagnostic.requestRuntimePermission(function(status){
        q.resolve(status) ;
      },function(err) {console.log("ReadPhone ask Permission error: "+err) ;
      },cordova.plugins.diagnostic.permission.READ_PHONE_STATE) ;
    } else {
      q.resolve(status) ;
    }
  },function(err) {console.log("ReadPhone check permission error: "+err) ;
  },cordova.plugins.diagnostic.permission.READ_PHONE_STATE) ;
  return q.promise ;
}
function getContactsAuth(){
  var q = $q.defer();
  cordova.plugins.diagnostic.isContactsAuthorized(function(status) {
    if (status == false) {
      cordova.plugins.diagnostic.requestContactsAuthorization(function(status){
        q.resolve(status) ;
      }) ;
    } else {
      q.resolve(status) ;
    }
  }) ;
  return q.promise ;
}

// Call the Promises - IN THIS ORDER

getReadPhoneAuth().then(function(status) {
 if (status == "GRANTED") {
  var devInfo = cordova.require("cordova/plugin/DeviceInformation") ;
  devInfo.get(function(result) {
     // with only securing READ_PHONE_STATE, this plugin will only return the following SIM Card info:
     result == {"deviceID": "12345678909876",
                "phoneNo": "18005551212",
                "netCountry": "us",
                "netName": "T-Mobile",
                "simNo": "98765432101234567",
                "simCountry": "us",
                "simName": "TM.ERROR"}
  },function(err) { console.log(err) }) ;
} else {
  console.log("status Could be DENIED or DENIED_ALWAYS")
}
return getContactsAuth()
}).then(function(status) {
 if (status == "GRANTED") {
  var devInfo = cordova.require("cordova/plugin/DeviceInformation") ;
  devInfo.get(function(result) {
     // with securing isContactsAuthorized, this plugin will both above info AND Accounts info:
     result == {"account0Name": "user0",
      "account0Type": "com.sec.android.app.sns3.twitter",
      "account1Name": "Office",
      "account1Type": "com.microsoft.office",
      "account2Name": "user2@blah.com",
      "account2Type": "com.android.email",
      "account3Name": "user3@foo.com",
      "account3Type": "com.android.email",
      "account4Name": "user4@foobar.com",
      "account4Type": "com.android.email",
      "account5Name": "user5@hotmail.com:Outlook",
      "account5Type": "com.microsoft.office.outlook.USER_ACCOUNT",
      "account6Name": "googleUser@gmail.com",  <-- FIRST com.google account IS the phones main/playstore account
      "account6Type": "com.google",
      "account7Name": "anotherAccount@gmail.com",
      "account7Type": "com.google",
      "account8Name": "thisUser@gmail.com",
      "account8Type": "com.google",
      "account9Name": "twitterUser",
      "account9Type": "com.twitter.android.auth.login",
      "deviceID": "12345678909876",
      "phoneNo": "18005551212",
      "netCountry": "us",
      "netName": "T-Mobile",
      "simNo": "98765432101234567",
      "simCountry": "us",
      "simName": "TM.ERROR"}
     },function(err) { console.log(err)}) ;
 } else {
  console.log("status Could be DENIED or DENIED_ALWAYS")
 }
}) ;

If you call getContactsAuth first, it will fail. Apparently cordova/plugin/DeviceInformation uses READ_PHONE_STATE first...whether you are using that info or not is irrelevant. If you only want CONTACTS info, you will still need to get READ_PHONE_STATE first, otherwise devInfo.get(function(result) will error out.

To demonstrate how the plugin can work in Android 6+ I broke it out above to easily follow. However, you can merge the two permissions first and then call the plugin later and grab all the info at once. But you risk the user denying READ_PHONE_STATE, and even though they do permit GET_ACCOUNTS, causing devInfo.get() to error out.

This plugin was designed pre Android 6 thus all permissions were GRANTED by default. But Post Android 6 "dangerous" permissions are required to get user approval. Both of the permissions required by DeviceInformation are considered "dangerous". But how the DeviceInformation plugin is designed (dependent on READ_PHONE_STATE GRANTED first otherwise fail the whole thing) is no longer ideal - especially if you are only looking for the CONTACTS section.

In the end, its better to completely separate these two functions out, to do so, use the following outline:

cordova-plugin-diagostic
   -> isContactsAuthorized
      -> NO, then get permission: requestContactsAuthorization
   -> If GRANTED, cordova-plugin-contacts to retrieve contacts
.then()
cordova-plugin-diagnostic
   -> getPermissionAuthorizationStatus
      -> NO, then get permission: requestRuntimePermission
   -> If GRANTED, cordova-plugin-sim to retrieve SIM Card OR DeviceInformation (SIM returns way more info)
Clone this wiki locally