Fix crash when viewing or editing contacts without account

QuickContact and editor would crash when opening contacts with an
account_name and account_type from an invalid account or an account
without a contacts sync adapter.

Test: manually verify that a contact with a bogus account can be opened
in QuickContact and editor.

Bug 36782218

Change-Id: I943581be036dff07838ca630c0b771b370f9e2c7
diff --git a/src/com/android/contacts/model/AccountTypeManager.java b/src/com/android/contacts/model/AccountTypeManager.java
index 280d859..b442e4f 100644
--- a/src/com/android/contacts/model/AccountTypeManager.java
+++ b/src/com/android/contacts/model/AccountTypeManager.java
@@ -580,9 +580,9 @@
         if (account == null) {
             return null;
         }
-        final AccountType type = mTypeProvider.getTypeForAccount(account);
+        AccountType type = mTypeProvider.getTypeForAccount(account);
         if (type == null) {
-            return null;
+            type = mFallbackAccountType;
         }
         return type.wrapAccount(mContext, account);
     }
@@ -703,7 +703,8 @@
      */
     @Override
     public AccountType getAccountType(AccountTypeWithDataSet accountTypeWithDataSet) {
-        return mTypeProvider.getType(
+        final AccountType type = mTypeProvider.getType(
                 accountTypeWithDataSet.accountType, accountTypeWithDataSet.dataSet);
+        return type != null ? type : mFallbackAccountType;
     }
 }
diff --git a/src/com/android/contacts/model/account/FallbackAccountType.java b/src/com/android/contacts/model/account/FallbackAccountType.java
index 907fce4..288a917 100644
--- a/src/com/android/contacts/model/account/FallbackAccountType.java
+++ b/src/com/android/contacts/model/account/FallbackAccountType.java
@@ -93,4 +93,18 @@
     public boolean areContactsWritable() {
         return true;
     }
+
+
+    /**
+     * {@inheritDoc}
+     *
+     * <p>This is overriden because the base class validates that the account.type matches
+     * {@link #accountType} but for the fallback case we want to be more permissive</p>
+     */
+    @Override
+    public AccountInfo wrapAccount(Context context, AccountWithDataSet account) {
+        return new AccountInfo(
+                new AccountDisplayInfo(account, account.name,
+                        getDisplayLabel(context), getDisplayIcon(context), false), this);
+    }
 }