resolved conflicts for merge of 7b5df2ab to master

This method has been removed from master since we're now using API
PhoneUtils from Telephony instead of this one.

Change-Id: I3532078089b519a48df25e70c0a6f2584d45e3ea
diff --git a/src/com/android/contacts/common/preference/ContactsPreferences.java b/src/com/android/contacts/common/preference/ContactsPreferences.java
index 311d007..2fce78c 100644
--- a/src/com/android/contacts/common/preference/ContactsPreferences.java
+++ b/src/com/android/contacts/common/preference/ContactsPreferences.java
@@ -26,8 +26,10 @@
 import android.provider.ContactsContract;
 import android.provider.Settings;
 import android.provider.Settings.SettingNotFoundException;
+import android.text.TextUtils;
 
 import com.android.contacts.common.R;
+import com.android.contacts.common.model.account.AccountWithDataSet;
 
 /**
  * Manages user preferences for contacts.
@@ -64,15 +66,22 @@
     private final Context mContext;
     private int mSortOrder = -1;
     private int mDisplayOrder = -1;
+    private String mDefaultAccount = null;
     private ChangeListener mListener = null;
     private Handler mHandler;
     private final SharedPreferences mPreferences;
+    private String mDefaultAccountKey;
+    private String mDefaultAccountSavedKey;
 
     public ContactsPreferences(Context context) {
         mContext = context;
         mHandler = new Handler();
         mPreferences = mContext.getSharedPreferences(context.getPackageName(),
                 Context.MODE_PRIVATE);
+        mDefaultAccountKey = mContext.getResources().getString(
+                R.string.contact_editor_default_account_key);
+        mDefaultAccountSavedKey = mContext.getResources().getString(
+                R.string.contact_editor_anything_saved_key);
         maybeMigrateSystemSettings();
     }
 
@@ -134,6 +143,37 @@
         editor.commit();
     }
 
+    public boolean isDefaultAccountUserChangeable() {
+        return mContext.getResources().getBoolean(R.bool.config_default_account_user_changeable);
+    }
+
+    public String getDefaultAccount() {
+        if (!isDefaultAccountUserChangeable()) {
+            return mDefaultAccount;
+        }
+        if (TextUtils.isEmpty(mDefaultAccount)) {
+            final String accountString = mPreferences.getString(mDefaultAccountKey, mDefaultAccount);
+            if (!TextUtils.isEmpty(accountString)) {
+                final AccountWithDataSet accountWithDataSet = AccountWithDataSet.unstringify(
+                        accountString);
+                mDefaultAccount = accountWithDataSet.name;
+            }
+        }
+        return mDefaultAccount;
+    }
+
+    public void setDefaultAccount(AccountWithDataSet accountWithDataSet) {
+        mDefaultAccount = accountWithDataSet == null ? null : accountWithDataSet.name;
+        final Editor editor = mPreferences.edit();
+        if (TextUtils.isEmpty(mDefaultAccount)) {
+            editor.remove(mDefaultAccountKey);
+        } else {
+            editor.putString(mDefaultAccountKey, accountWithDataSet.stringify());
+        }
+        editor.putBoolean(mDefaultAccountSavedKey, true);
+        editor.commit();
+    }
+
     public void registerChangeListener(ChangeListener listener) {
         if (mListener != null) unregisterChangeListener();
 
diff --git a/src/com/android/contacts/common/preference/DefaultAccountPreference.java b/src/com/android/contacts/common/preference/DefaultAccountPreference.java
new file mode 100644
index 0000000..6fb7cc4
--- /dev/null
+++ b/src/com/android/contacts/common/preference/DefaultAccountPreference.java
@@ -0,0 +1,97 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package com.android.contacts.common.preference;
+
+import android.app.AlertDialog;
+import android.content.Context;
+import android.preference.ListPreference;
+import android.util.AttributeSet;
+
+import com.android.contacts.common.model.AccountTypeManager;
+import com.android.contacts.common.model.account.AccountType;
+import com.android.contacts.common.model.account.AccountTypeWithDataSet;
+import com.android.contacts.common.model.account.AccountWithDataSet;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+public class DefaultAccountPreference extends ListPreference {
+    private ContactsPreferences mPreferences;
+    private Map<String, AccountWithDataSet> mAccountMap;
+
+    public DefaultAccountPreference(Context context) {
+        super(context);
+        prepare();
+    }
+
+    public DefaultAccountPreference(Context context, AttributeSet attrs) {
+        super(context, attrs);
+        prepare();
+    }
+
+    private void prepare() {
+        mPreferences = new ContactsPreferences(getContext());
+        mAccountMap = new HashMap<>();
+        final AccountTypeManager accountTypeManager = AccountTypeManager.getInstance(getContext());
+        List<AccountWithDataSet> accounts = accountTypeManager.getAccounts(true);
+        for (AccountWithDataSet account : accounts) {
+            mAccountMap.put(account.name, account);
+        }
+        final Set<String> accountNames = mAccountMap.keySet();
+        final String[] accountNamesArray = accountNames.toArray(new String[accountNames.size()]);
+        setEntries(accountNamesArray);
+        setEntryValues(accountNamesArray);
+        final String defaultAccount = String.valueOf(mPreferences.getDefaultAccount());
+        if (accountNames.contains(defaultAccount)) {
+            setValue(String.valueOf(mPreferences.getDefaultAccount()));
+        } else {
+            setValue(null);
+        }
+    }
+
+    @Override
+    protected boolean shouldPersist() {
+        return false;   // This preference takes care of its own storage
+    }
+
+    @Override
+    public CharSequence getSummary() {
+        return mPreferences.getDefaultAccount();
+    }
+
+    @Override
+    protected boolean persistString(String value) {
+        if (value == null && mPreferences.getDefaultAccount() == null) {
+            return true;
+        }
+        if (value == null || mPreferences.getDefaultAccount() == null
+                || !value.equals(mPreferences.getDefaultAccount())) {
+            mPreferences.setDefaultAccount(mAccountMap.get(value));
+            notifyChanged();
+        }
+        return true;
+    }
+
+    @Override
+    // UX recommendation is not to show cancel button on such lists.
+    protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
+        super.onPrepareDialogBuilder(builder);
+        builder.setNegativeButton(null, null);
+    }
+}
diff --git a/src/com/android/contacts/common/util/PhoneNumberHelper.java b/src/com/android/contacts/common/util/PhoneNumberHelper.java
index 11f2153..794b6dd 100644
--- a/src/com/android/contacts/common/util/PhoneNumberHelper.java
+++ b/src/com/android/contacts/common/util/PhoneNumberHelper.java
@@ -54,92 +54,6 @@
     }
 
     /**
-     * Formats the phone number only if the given number hasn't been formatted.
-     * <p>
-     * The number which has only dailable character is treated as not being
-     * formatted.
-     *
-     * @param phoneNumber the number to be formatted.
-     * @param phoneNumberE164 The E164 format number whose country code is used if the given
-     * phoneNumber doesn't have the country code.
-     * @param defaultCountryIso The ISO 3166-1 two letters country code whose convention will
-     * be used if the phoneNumberE164 is null or invalid, or if phoneNumber contains IDD.
-     * @return The formatted number if the given number has been formatted, otherwise, return the
-     * given number.
-     *
-     * TODO: Remove if PhoneNumberUtils.formatNumber(String phoneNumber, String phoneNumberE164,
-     * String defaultCountryIso) is made public.
-     */
-    public static String formatNumber(
-            String phoneNumber, String phoneNumberE164, String defaultCountryIso) {
-        int len = phoneNumber.length();
-        for (int i = 0; i < len; i++) {
-            if (!PhoneNumberUtils.isDialable(phoneNumber.charAt(i))) {
-                return phoneNumber;
-            }
-        }
-        PhoneNumberUtil util = PhoneNumberUtil.getInstance();
-        // Get the country code from phoneNumberE164
-        if (phoneNumberE164 != null && phoneNumberE164.length() >= 2
-                && phoneNumberE164.charAt(0) == '+') {
-            try {
-                // The number to be parsed is in E164 format, so the default region used doesn't
-                // matter.
-                PhoneNumber pn = util.parse(phoneNumberE164, "ZZ");
-                String regionCode = util.getRegionCodeForNumber(pn);
-                if (!TextUtils.isEmpty(regionCode) &&
-                        // This makes sure phoneNumber doesn't contain an IDD
-                        normalizeNumber(phoneNumber).indexOf(phoneNumberE164.substring(1)) <= 0) {
-                    defaultCountryIso = regionCode;
-                }
-            } catch (NumberParseException e) {
-                Log.w(LOG_TAG, "The number could not be parsed in E164 format!");
-            }
-        }
-
-        String result = formatNumber(phoneNumber, defaultCountryIso);
-        return result == null ? phoneNumber : result;
-    }
-
-    /**
-     * Format a phone number.
-     * <p>
-     * If the given number doesn't have the country code, the phone will be
-     * formatted to the default country's convention.
-     *
-     * @param phoneNumber The number to be formatted.
-     * @param defaultCountryIso The ISO 3166-1 two letters country code whose convention will
-     * be used if the given number doesn't have the country code.
-     * @return The formatted number, or null if the given number is not valid.
-     *
-     * TODO: Remove if PhoneNumberUtils.formatNumber(String phoneNumber, String defaultCountryIso)
-     * is made public.
-     */
-    public static String formatNumber(String phoneNumber, String defaultCountryIso) {
-        // Do not attempt to format numbers that start with a hash or star symbol.
-        if (phoneNumber.startsWith("#") || phoneNumber.startsWith("*")) {
-            return phoneNumber;
-        }
-
-        final PhoneNumberUtil util = PhoneNumberUtil.getInstance();
-        String result = null;
-        try {
-            PhoneNumber pn = util.parseAndKeepRawInput(phoneNumber, defaultCountryIso);
-            if (KOREA_ISO_COUNTRY_CODE.equals(defaultCountryIso) &&
-                    (pn.getCountryCode() == util.getCountryCodeForRegion(KOREA_ISO_COUNTRY_CODE))) {
-                // Format local Korean phone numbers with country code to corresponding national
-                // format which would replace the leading +82 with 0.
-                result = util.format(pn, PhoneNumberUtil.PhoneNumberFormat.NATIONAL);
-            } else {
-                result = util.formatInOriginalFormat(pn, defaultCountryIso);
-            }
-        } catch (NumberParseException e) {
-            Log.w(LOG_TAG, "Number could not be parsed with the given country code!");
-        }
-        return result;
-    }
-
-    /**
      * Normalize a phone number by removing the characters other than digits. If
      * the given number has keypad letters, the letters will be converted to
      * digits first.
diff --git a/src/com/android/contacts/commonbind/analytics/AnalyticsUtil.java b/src/com/android/contacts/commonbind/analytics/AnalyticsUtil.java
index 87ca7be..84420b6 100644
--- a/src/com/android/contacts/commonbind/analytics/AnalyticsUtil.java
+++ b/src/com/android/contacts/commonbind/analytics/AnalyticsUtil.java
@@ -1,3 +1,17 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
+ * or implied. See the License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
 package com.android.contacts.commonbind.analytics;
 
 import android.app.Activity;