Recent Updates RSS Toggle Comment Threads | Keyboard Shortcuts

  • Kristian 5:47 pm on October 15, 2011 Permalink
    Tags: android,   

    Get Android deviceId, simple enough…? 

    Turns out it is not as simple as I thought to get a unique device id to identify an Android device in my app. TelephoneManager.getDeviceId() only works if the device has telephone support (of course), ANDROID_ID is apparently buggy on some pre 2.2 devices (some manufacturers always returning the same value, see

    This blogpost on the Android Developers blog describes the issues in more detail:

    I ended up writing a simple factory class to handle this, falling back to a generated id that is stored in SharedPreferences if all above fails:

    public class DeviceIdFactory {
        private static String deviceId;
        private static final String DEVICE_ID = "prefDeviceId";
         * Get unique device id. 
         * First try Settings.Secure.ANDROID_ID. Due to a bug some manufacturers will return the same
         * id for Settings.Secure.ANDROID_ID. 
         * Fallback on TelephonyManager.getDeviceId(), which should return unique device id if telephony is 
         * supported on device.
         * Last fallback generate unique id for this installation and store as SharedPreference.
         * @see
         * @see
         * @param context
         * @return unique id for this device (might change on factory resets, device upgrade etc)
        public static String getDeviceId(Context context) {
            if (deviceId == null) {
                synchronized (DeviceIdFactory.class) {
                    if (deviceId == null) {
                        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
                        deviceId = prefs.getString(DEVICE_ID, null);
                        if (deviceId == null) {
                            String deviceId = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID); 
                            try {
                                if ("9774d56d682e549c".equals(deviceId)) {
                                    String tmId = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
                                    deviceId = tmId == null ? UUID.randomUUID().toString() : UUID.nameUUIDFromBytes(tmId.getBytes("utf8")).toString();
                                } else {
                                    deviceId = UUID.nameUUIDFromBytes(deviceId.getBytes("utf8")).toString();
                            } catch (UnsupportedEncodingException e) {
                                throw new RuntimeException(e);
                            prefs.edit().putString(DEVICE_ID, deviceId).commit();
            return deviceId;

    Note: requires READ_PHONE_STATE permission

  • Kristian 11:15 pm on October 8, 2011 Permalink
    Tags: java   

    The Java Life Rap Music Video –

  • Kristian 11:14 pm on August 15, 2011 Permalink
    Tags: , javascript   

    I have a list of lists that I need to flatten to a single list that contains a string representation of each object.

    * Flatten a list of lists into a list of string representations of each object
    * @param {Object[][]} list A list of lists
    * @param {Function(Object)} apply Returns a string representation of each element 
    *                             in list
    * @returns {String[]} A list of string representations of each element in list,  
    *                             duplicates excluded
    function flatten(list, apply) {
       var result = [];
       var obj = {};
       for(var i = 0; i < list.length; i++) {
           for(var j = 0; j < list[i].length; j++) {
               obj[apply(list[i][j])] = 0;
       for(var e in obj) {
       return result;

    I can call this function with:

    var listOfLists= [["Some", "object"], ["Another", "list"]];
    var flat = flatten(listOfLists, function(o){
            return o;

    The purpose of the apply function is to be able to convert the objects in the list to a string, for example picking out an id that represents each object.

  • Kristian 9:21 am on July 13, 2011 Permalink
    Tags: book   

    Software is one of the few professions that allows its practitioners to come to work and dream, solve puzzles, surf the Internet, and then collect a handsome paycheck at the end of the day.

    Making it Big in Software: Get the Job. Work the Org. Become Great – Sam Lightstone
  • Kristian 4:27 pm on July 11, 2011 Permalink
    Tags: code-review, google   

    At Google, no code, for any product, for any project, gets checked in until it gets a positive review.
  • Kristian 12:55 pm on July 11, 2011 Permalink

    Testing SyntaxHighlighter plugin 

    class Test {
      void test() {
        System.out.println("Hello world");
  • Kristian 4:46 pm on July 3, 2011 Permalink
    Tags: life   

    How much time have I wasted in my days waiting while downloading/installing/configuring the latest new cool language/framwork/tool/plugin…

compose new post
next post/next comment
previous post/previous comment
show/hide comments
go to top
go to login
show/hide help
shift + esc