Add changes lost in I467a8b4fa5cc610c3e1627f30631a55ecd6c91c1

This allows contacts to be created in a null device account without showing the
account chooser. It also shows the correct title in the action bar when viewing
device accounts.

Change-Id: I3ee096d1ed70d544fd719e930dcb49953c162eb3
diff --git a/src/com/android/contacts/activities/CompactContactEditorActivity.java b/src/com/android/contacts/activities/CompactContactEditorActivity.java
index f8c9c5c..3778400 100644
--- a/src/com/android/contacts/activities/CompactContactEditorActivity.java
+++ b/src/com/android/contacts/activities/CompactContactEditorActivity.java
@@ -64,6 +64,15 @@
     // 3 used for ContactDeletionInteraction.RESULT_CODE_DELETED
     public static final int RESULT_CODE_EDITED = 4;
 
+    /**
+     * The contact will be saved to the device local account when this is set for an insert. This
+     * is necessary because {@link android.accounts.Account} cannot be created with null values
+     * for the name and type and an Account is needed for
+     * {@link android.provider.ContactsContract.Intents.Insert#EXTRA_ACCOUNT}
+     */
+    public static final String EXTRA_SAVE_TO_DEVICE_FLAG =
+            "com.android.contacts.SAVE_TO_DEVICE_FLAG";
+
     private static final String TAG_COMPACT_EDITOR = "compact_editor";
     private static final String TAG_PHOTO_SELECTION = "photo_selector";
 
diff --git a/src/com/android/contacts/common/util/AccountFilterUtil.java b/src/com/android/contacts/common/util/AccountFilterUtil.java
index c69cf2a..9db0e26 100644
--- a/src/com/android/contacts/common/util/AccountFilterUtil.java
+++ b/src/com/android/contacts/common/util/AccountFilterUtil.java
@@ -32,6 +32,7 @@
 import android.widget.Toast;
 
 import com.android.contacts.R;
+import com.android.contacts.activities.CompactContactEditorActivity;
 import com.android.contacts.common.list.AccountFilterActivity;
 import com.android.contacts.common.list.ContactListFilter;
 import com.android.contacts.common.list.ContactListFilterController;
@@ -191,10 +192,15 @@
         // If we are in account view, we pass the account explicitly in order to
         // create contact in the account. This will prevent the default account dialog
         // from being displayed.
-        if (!isAllContactsFilter(filter) && !isDeviceContactsFilter(filter)) {
+        if (!isAllContactsFilter(filter) && filter.accountName != null
+                && filter.accountType != null) {
             final Account account = new Account(filter.accountName, filter.accountType);
             intent.putExtra(Intents.Insert.EXTRA_ACCOUNT, account);
             intent.putExtra(Intents.Insert.EXTRA_DATA_SET, filter.dataSet);
+        } else if (isDeviceContactsFilter(filter)) {
+            // It's OK to add this even though it's an implicit intent. If a different app
+            // receives the intent it should just ignore the flag.
+            intent.putExtra(CompactContactEditorActivity.EXTRA_SAVE_TO_DEVICE_FLAG, true);
         }
 
         try {
diff --git a/src/com/android/contacts/editor/CompactContactEditorFragment.java b/src/com/android/contacts/editor/CompactContactEditorFragment.java
index c07ebe2..838bafd 100644
--- a/src/com/android/contacts/editor/CompactContactEditorFragment.java
+++ b/src/com/android/contacts/editor/CompactContactEditorFragment.java
@@ -602,6 +602,9 @@
                 mHasNewContact = true;
                 if (mAccountWithDataSet != null) {
                     createContact(mAccountWithDataSet);
+                } else if (mIntentExtras != null && mIntentExtras.getBoolean(
+                        CompactContactEditorActivity.EXTRA_SAVE_TO_DEVICE_FLAG, false)) {
+                    createContact(null);
                 } else {
                     // No Account specified. Let the user choose
                     // Load Accounts async so that we can present them
diff --git a/src/com/android/contacts/list/DefaultContactBrowseListFragment.java b/src/com/android/contacts/list/DefaultContactBrowseListFragment.java
index 4169ae6..424c55d 100644
--- a/src/com/android/contacts/list/DefaultContactBrowseListFragment.java
+++ b/src/com/android/contacts/list/DefaultContactBrowseListFragment.java
@@ -68,6 +68,8 @@
 import com.android.contacts.common.logging.Logger;
 import com.android.contacts.common.logging.ScreenEvent;
 import com.android.contacts.common.model.AccountTypeManager;
+import com.android.contacts.common.model.account.AccountDisplayInfo;
+import com.android.contacts.common.model.account.AccountDisplayInfoFactory;
 import com.android.contacts.common.model.account.AccountWithDataSet;
 import com.android.contacts.common.model.account.GoogleAccountType;
 import com.android.contacts.common.util.AccountFilterUtil;
@@ -632,10 +634,14 @@
     }
 
     private String getActionBarTitleForAccount(ContactListFilter filter) {
-        if (GoogleAccountType.ACCOUNT_TYPE.equals(filter.accountType)) {
+        final AccountDisplayInfoFactory factory = AccountDisplayInfoFactory
+                .forAllAccounts(getActivity());
+        final AccountDisplayInfo account = factory.getAccountDisplayInfoFor(filter);
+        if (account.hasGoogleAccountType()) {
             return getString(R.string.title_from_google);
         }
-        return getString(R.string.title_from_other_accounts, filter.accountName);
+        return account.withFormattedName(getActivity(), R.string.title_from_other_accounts)
+                .getNameLabel().toString();
     }
 
     public void setSwipeRefreshLayoutEnabledOrNot(ContactListFilter filter) {