Bug 1198338 - Get the IMEI r=snorp
authorFabrice Desré <fabrice@mozilla.com>
Fri, 25 Sep 2015 09:49:11 -0700
changeset 264655 876b48d14ee058053c1c3fdf0c7ba963f1595ff8
parent 264654 9b0daa2a1a313cb116b00049d998a21faaa23215
child 264656 77d1c5f861a49a3c135d5d78f9384c217946092c
push id65707
push usercbook@mozilla.com
push dateMon, 28 Sep 2015 12:18:34 +0000
treeherdermozilla-inbound@2c0e60a8f736 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssnorp
bugs1198338
milestone44.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1198338 - Get the IMEI r=snorp
mobile/android/b2gdroid/app/src/main/AndroidManifest.xml
mobile/android/b2gdroid/app/src/main/java/org/mozilla/b2gdroid/SettingsMapper.java
mobile/android/b2gdroid/components/MessagesBridge.jsm
--- a/mobile/android/b2gdroid/app/src/main/AndroidManifest.xml
+++ b/mobile/android/b2gdroid/app/src/main/AndroidManifest.xml
@@ -46,16 +46,17 @@
     <!-- App requires OpenGL ES 2.0 -->
     <uses-feature android:glEsVersion="0x00020000" android:required="true" />
 
     <!-- Needed to disable the default lockscreen -->
     <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
 
     <uses-permission android:name="android.permission.WRITE_SETTINGS" />
     <uses-permission android:name="android.permission.SET_WALLPAPER" />
+    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
 
     <application android:label="@string/b2g"
                  android:icon="@drawable/b2g"
                  android:logo="@drawable/b2g"
                  android:hardwareAccelerated="true"
                  android:debuggable="true">
 
         <meta-data android:name="com.sec.android.support.multiwindow" android:value="true"/>
--- a/mobile/android/b2gdroid/app/src/main/java/org/mozilla/b2gdroid/SettingsMapper.java
+++ b/mobile/android/b2gdroid/app/src/main/java/org/mozilla/b2gdroid/SettingsMapper.java
@@ -13,16 +13,17 @@ import org.json.JSONObject;
 import android.app.WallpaperManager;
 import android.content.Context;
 import android.database.ContentObserver;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.net.Uri;
 import android.os.Handler;
 import android.provider.Settings.System;
+import android.telephony.TelephonyManager;
 import android.util.Base64;
 import android.util.Log;
 
 import org.mozilla.gecko.EventDispatcher;
 import org.mozilla.gecko.GeckoAppShell;
 import org.mozilla.gecko.GeckoEvent;
 import org.mozilla.gecko.util.GeckoEventListener;
 
@@ -159,17 +160,18 @@ class SettingsMapper extends ContentObse
         void onAndroidChange(Uri uri) { }
     }
 
     SettingsMapper(Context context, Handler handler) {
         super(handler);
         mContext = context;
         EventDispatcher.getInstance()
                        .registerGeckoThreadListener(this,
-                                                    "Settings:Change");
+                                                    "Settings:Change",
+                                                    "Android:GetIMEI");
 
         mContext.getContentResolver()
                 .registerContentObserver(System.CONTENT_URI,
                                          true,
                                          this);
 
         mGeckoSettings = new Hashtable<String, BaseMapping>();
         mAndroidSettings = new Hashtable<String, BaseMapping>();
@@ -189,38 +191,52 @@ class SettingsMapper extends ContentObse
         for (int i = 0; i < props.length; i++) {
             mAndroidSettings.put(props[i], mapping);
         }
     }
 
     void destroy() {
         EventDispatcher.getInstance()
                        .unregisterGeckoThreadListener(this,
-                                                      "Settings:Change");
+                                                      "Settings:Change",
+                                                      "Android:GetIMEI");
         mGeckoSettings.clear();
         mGeckoSettings = null;
         mAndroidSettings.clear();
         mAndroidSettings = null;
         mContext.getContentResolver().unregisterContentObserver(this);
     }
 
     public void handleMessage(String event, JSONObject message) {
         Log.w(LOGTAG, "Received " + event);
 
-        try {
-            String setting = message.getString("setting");
-            BaseMapping mapping = mGeckoSettings.get(setting);
-            if (mapping != null) {
-                Log.d(LOGTAG, "Changing gecko setting " + setting);
-                mapping.onGeckoChange(setting, message);
-            } else {
-                Log.d(LOGTAG, "No gecko mapping registered for " + setting);
+        if ("Settings:Change".equals(event)) {
+            try {
+                String setting = message.getString("setting");
+                BaseMapping mapping = mGeckoSettings.get(setting);
+                if (mapping != null) {
+                    Log.d(LOGTAG, "Changing gecko setting " + setting);
+                    mapping.onGeckoChange(setting, message);
+                } else {
+                    Log.d(LOGTAG, "No gecko mapping registered for " + setting);
+                }
+            } catch(Exception ex) {
+                Log.d(LOGTAG, "Error getting setting name", ex);
             }
-        } catch(Exception ex) {
-            Log.d(LOGTAG, "Error getting setting name", ex);
+        } else if ("Android:GetIMEI".equals(event)) {
+            TelephonyManager telManager =
+            (TelephonyManager)mContext.getSystemService(Context.TELEPHONY_SERVICE);
+            JSONObject ret = new JSONObject();
+            Log.d(LOGTAG, "Getting IMEI: " + telManager.getDeviceId());
+            try {
+                ret.put("imei", telManager.getDeviceId());
+            } catch(Exception jsonEx) {
+                Log.wtf(LOGTAG, "Error getting IMEI", jsonEx);
+            }
+            EventDispatcher.sendResponse(message, ret);
         }
     }
 
     // ContentObserver, see
     // http://developer.android.com/reference/android/database/ContentObserver.html
     @Override
     public boolean deliverSelfNotifications() {
         return false;
--- a/mobile/android/b2gdroid/components/MessagesBridge.jsm
+++ b/mobile/android/b2gdroid/components/MessagesBridge.jsm
@@ -32,16 +32,24 @@ function getWindow() {
 let _blockedSettings = new Set();
 
 this.MessagesBridge = {
   init: function() {
     Services.obs.addObserver(this.onAndroidMessage, "Android:Launcher", false);
     Services.obs.addObserver(this.onAndroidSetting, "Android:Setting", false);
     Services.obs.addObserver(this.onSettingChange, "mozsettings-changed", false);
     Services.obs.addObserver(this, "xpcom-shutdown", false);
+
+    // Send a request to get the device's IMEI.
+    Messaging.sendRequestForResult({ type: "Android:GetIMEI" })
+    .then(aData => {
+      debug("Got IMEI: " + aData.imei);
+      let lock = settings.createLock();
+      lock.set("deviceinfo.imei", aData.imei, null);
+    });
   },
 
   onAndroidMessage: function(aSubject, aTopic, aData) {
     let data = JSON.parse(aData);
     debug(`Got Android:Launcher message ${data.action}`);
 
     let window = getWindow();
     switch (data.action) {