Snap for 4402249 from 366471c38c19e75c1475f8e2a6e69571558e21c4 to oc-mr1-release

Change-Id: If768bf464b82e09f66ef15177ad766ac8baf8053
diff --git a/Common/src/com/googlecode/android_scripting/jsonrpc/JsonBuilder.java b/Common/src/com/googlecode/android_scripting/jsonrpc/JsonBuilder.java
index 9499498..29dc554 100644
--- a/Common/src/com/googlecode/android_scripting/jsonrpc/JsonBuilder.java
+++ b/Common/src/com/googlecode/android_scripting/jsonrpc/JsonBuilder.java
@@ -45,6 +45,7 @@
 import com.googlecode.android_scripting.facade.telephony.TelephonyConstants;
 import com.googlecode.android_scripting.facade.telephony.TelephonyUtils;
 
+import android.annotation.NonNull;
 import android.bluetooth.BluetoothDevice;
 import android.bluetooth.BluetoothGattCharacteristic;
 import android.bluetooth.BluetoothGattDescriptor;
@@ -102,6 +103,8 @@
 import android.util.DisplayMetrics;
 import android.util.SparseArray;
 
+import static com.googlecode.android_scripting.ConvertUtils.toNonNullString;
+
 public class JsonBuilder {
 
     @SuppressWarnings("unchecked")
@@ -828,15 +831,20 @@
         return url;
     }
 
-    private static JSONObject buildPhoneAccount(PhoneAccount data)
+    /**
+     * Builds a json representation of a {@link PhoneAccount}.
+     * @param data The PhoneAccount convert to JSON.
+     * @return A JSONObject representation of a {@link PhoneAccount}.
+     * @throws JSONException
+     */
+    private static JSONObject buildPhoneAccount(@NonNull PhoneAccount data)
             throws JSONException {
         JSONObject acct = new JSONObject();
-        acct.put("Address", data.getAddress().toSafeString());
-        acct.put("SubscriptionAddress", data.getSubscriptionAddress()
-                .toSafeString());
-        acct.put("Label", ((data.getLabel() != null) ? data.getLabel().toString() : ""));
-        acct.put("ShortDescription", ((data.getShortDescription() != null) ? data
-                .getShortDescription().toString() : ""));
+        acct.put("Address", toNonNullString(data.getAddress(), Uri::toSafeString));
+        acct.put("SubscriptionAddress", toNonNullString(data.getSubscriptionAddress(),
+                Uri::toSafeString));
+        acct.put("Label", toNonNullString(data.getLabel()));
+        acct.put("ShortDescription", toNonNullString(data.getShortDescription()));
         return acct;
     }
 
diff --git a/Utils/src/com/googlecode/android_scripting/ConvertUtils.java b/Utils/src/com/googlecode/android_scripting/ConvertUtils.java
index fc7288a..b18d830 100644
--- a/Utils/src/com/googlecode/android_scripting/ConvertUtils.java
+++ b/Utils/src/com/googlecode/android_scripting/ConvertUtils.java
@@ -16,6 +16,10 @@
 
 package com.googlecode.android_scripting;
 
+import android.annotation.NonNull;
+
+import com.google.common.base.Strings;
+
 public class ConvertUtils {
     /**
      * Converts a String of comma separated bytes to a byte array
@@ -61,4 +65,42 @@
         return ret;
     }
 
+    /**
+     * An interface for
+     * @param <T1> The type of the caller.
+     * @param <T2> The returned type of the call.
+     */
+    public interface IFunction<T1, T2> {
+        T2 call(T1 t1);
+    }
+
+    /**
+     * Calls a given function on an object, and returns a NonNull String of the return value.
+     * @param obj The object to get the string data from.
+     * @param function The function or method to call.
+     * @param <T1> The type of the object.
+     * @param <T2> The type of the function return type.
+     * @return A string guaranteed not to be null.
+     */
+    public static <T1, T2> String toNonNullString(T1 obj, @NonNull IFunction<T1, T2> function) {
+        if (obj == null) {
+            return "";
+        } else {
+            return toNonNullString(function.call(obj));
+        }
+    }
+
+    /**
+     * Returns toString() or an empty string if {@code obj} or result is null.
+     * @param obj The object to call toString() on.
+     * @param <T> The type of the object.
+     * @return A string guaranteed not to be null.
+     */
+    public static <T> String toNonNullString(T obj) {
+        if (obj == null) {
+            return "";
+        } else {
+            return Strings.nullToEmpty(obj.toString());
+        }
+    }
 }