Merge "Modify action bar for contact editor"
diff --git a/res/layout-sw580dp/item_kind_section.xml b/res/layout-sw580dp/item_kind_section.xml
index 31538bb..5ff2da2 100644
--- a/res/layout-sw580dp/item_kind_section.xml
+++ b/res/layout-sw580dp/item_kind_section.xml
@@ -24,6 +24,7 @@
     android:orientation="vertical">
 
     <include
+        android:id="@+id/kind_title_layout"
         layout="@layout/edit_kind_title" />
 
     <LinearLayout
diff --git a/res/layout/item_kind_section.xml b/res/layout/item_kind_section.xml
index edc9d37..0b6017c 100644
--- a/res/layout/item_kind_section.xml
+++ b/res/layout/item_kind_section.xml
@@ -24,6 +24,7 @@
     android:orientation="vertical">
 
     <include
+        android:id="@+id/kind_title_layout"
         layout="@layout/edit_kind_title" />
 
     <LinearLayout
diff --git a/res/layout/organization_editor_view_switcher.xml b/res/layout/organization_editor_view_switcher.xml
new file mode 100644
index 0000000..c6e16ae
--- /dev/null
+++ b/res/layout/organization_editor_view_switcher.xml
@@ -0,0 +1,45 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2011 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.
+-->
+
+<!--
+  Layout of "organization" field in contact editor which either shows the "add
+  organization" button or a container (which should be populated with the actual
+  list of edit text fields).
+-->
+
+<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:paddingLeft="@dimen/editor_field_left_padding"
+    android:paddingRight="@dimen/editor_field_right_padding"
+    android:background="?android:attr/selectableItemBackground">
+
+    <TextView
+        android:id="@+id/add_organization_button"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:paddingLeft="@dimen/editor_field_left_padding"
+        android:textAppearance="?android:attr/textAppearanceMedium"
+        android:text="@string/organizationLabelsGroup"
+        android:duplicateParentState="true"/>
+
+    <FrameLayout
+        android:id="@+id/container"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:visibility="gone"/>
+
+</FrameLayout>
\ No newline at end of file
diff --git a/src/com/android/contacts/editor/ContactEditorFragment.java b/src/com/android/contacts/editor/ContactEditorFragment.java
index 9f1bbd2..1ec590c 100644
--- a/src/com/android/contacts/editor/ContactEditorFragment.java
+++ b/src/com/android/contacts/editor/ContactEditorFragment.java
@@ -542,10 +542,8 @@
         // ensureKind will not add it, so it is safe to add e.g. Event)
         EntityModifier.ensureKindExists(insert, newAccountType, Phone.CONTENT_ITEM_TYPE);
         EntityModifier.ensureKindExists(insert, newAccountType, Email.CONTENT_ITEM_TYPE);
-        EntityModifier.ensureKindExists(insert, newAccountType, Note.CONTENT_ITEM_TYPE);
         EntityModifier.ensureKindExists(insert, newAccountType, Organization.CONTENT_ITEM_TYPE);
         EntityModifier.ensureKindExists(insert, newAccountType, Event.CONTENT_ITEM_TYPE);
-        EntityModifier.ensureKindExists(insert, newAccountType, Website.CONTENT_ITEM_TYPE);
         EntityModifier.ensureKindExists(insert, newAccountType, StructuredPostal.CONTENT_ITEM_TYPE);
 
         if (mState == null) {
diff --git a/src/com/android/contacts/editor/KindSectionView.java b/src/com/android/contacts/editor/KindSectionView.java
index bd2a9a6..02aa73f 100644
--- a/src/com/android/contacts/editor/KindSectionView.java
+++ b/src/com/android/contacts/editor/KindSectionView.java
@@ -155,6 +155,10 @@
         return mTitleString;
     }
 
+    public void setTitleVisible(boolean visible) {
+        findViewById(R.id.kind_title_layout).setVisibility(visible ? View.VISIBLE : View.GONE);
+    }
+
     /**
      * Build editors for all current {@link #mState} rows.
      */
@@ -278,6 +282,19 @@
         return getEmptyEditors().size() > 0;
     }
 
+    /**
+     * Returns true if all editors are empty.
+     */
+    public boolean isEmpty() {
+        for (int i = 0; i < mEditors.getChildCount(); i++) {
+            View view = mEditors.getChildAt(i);
+            if (!((Editor) view).isEmpty()) {
+                return false;
+            }
+        }
+        return true;
+    }
+
     public void addItem() {
         ValuesDelta values = null;
         // If this is a list, we can freely add. If not, only allow adding the first.
diff --git a/src/com/android/contacts/editor/RawContactEditorView.java b/src/com/android/contacts/editor/RawContactEditorView.java
index 881f590..499b86f 100644
--- a/src/com/android/contacts/editor/RawContactEditorView.java
+++ b/src/com/android/contacts/editor/RawContactEditorView.java
@@ -20,17 +20,16 @@
 import com.android.contacts.R;
 import com.android.contacts.model.AccountType;
 import com.android.contacts.model.AccountType.EditType;
-import com.android.contacts.model.AccountTypeManager;
 import com.android.contacts.model.DataKind;
 import com.android.contacts.model.EntityDelta;
 import com.android.contacts.model.EntityDelta.ValuesDelta;
 import com.android.contacts.model.EntityModifier;
 
-import android.accounts.Account;
 import android.content.Context;
 import android.content.Entity;
 import android.database.Cursor;
 import android.provider.ContactsContract.CommonDataKinds.GroupMembership;
+import android.provider.ContactsContract.CommonDataKinds.Organization;
 import android.provider.ContactsContract.CommonDataKinds.Photo;
 import android.provider.ContactsContract.CommonDataKinds.StructuredName;
 import android.provider.ContactsContract.Contacts;
@@ -164,6 +163,7 @@
      */
     @Override
     public void setState(EntityDelta state, AccountType type, ViewIdGenerator vig) {
+
         mState = state;
 
         // Remove any existing sections
@@ -174,8 +174,9 @@
 
         setId(vig.getId(state, null, null, ViewIdGenerator.NO_VIEW_INDEX));
 
-        // Make sure we have StructuredName
+        // Make sure we have a StructuredName and Organization
         EntityModifier.ensureKindExists(state, type, StructuredName.CONTENT_ITEM_TYPE);
+        EntityModifier.ensureKindExists(state, type, Organization.CONTENT_ITEM_TYPE);
 
         ValuesDelta values = state.getValues();
         mRawContactId = values.getAsLong(RawContacts._ID);
@@ -238,6 +239,42 @@
                 if (mGroupMembershipView != null) {
                     mGroupMembershipView.setState(state);
                 }
+            } else if (Organization.CONTENT_ITEM_TYPE.equals(mimeType)) {
+                // Create the organization section
+                final KindSectionView section = (KindSectionView) mInflater.inflate(
+                        R.layout.item_kind_section, mFields, false);
+                section.setTitleVisible(false);
+                section.setEnabled(isEnabled());
+                section.setState(kind, state, false, vig);
+
+                // If there is organization info for the contact already, display it
+                if (!section.isEmpty()) {
+                    mFields.addView(section);
+                } else {
+                    // Otherwise provide the user with an "add organization" button that shows the
+                    // EditText fields only when clicked
+                    final View organizationView = mInflater.inflate(
+                            R.layout.organization_editor_view_switcher, mFields, false);
+                    final View addOrganizationButton = organizationView.findViewById(
+                            R.id.add_organization_button);
+                    final ViewGroup organizationSectionViewContainer =
+                            (ViewGroup) organizationView.findViewById(R.id.container);
+
+                    organizationSectionViewContainer.addView(section);
+
+                    // Setup the click listener for the "add organization" button
+                    addOrganizationButton.setOnClickListener(new OnClickListener() {
+                        @Override
+                        public void onClick(View v) {
+                            // Once the user expands the organization field, the user cannot
+                            // collapse them again.
+                            addOrganizationButton.setVisibility(View.GONE);
+                            organizationSectionViewContainer.setVisibility(View.VISIBLE);
+                        }
+                    });
+
+                    mFields.addView(organizationView);
+                }
             } else {
                 // Otherwise use generic section-based editors
                 if (kind.fieldList == null) continue;
@@ -362,8 +399,13 @@
             View child = mFields.getChildAt(i);
             if (child instanceof KindSectionView) {
                 final KindSectionView sectionView = (KindSectionView) child;
-                // not a list and already exists? ignore
+                // If the section is already visible (has 1 or more editors), then don't offer the
+                // option to add this type of field in the popup menu
+                if (sectionView.getEditorCount() > 0) {
+                    continue;
+                }
                 DataKind kind = sectionView.getKind();
+                // not a list and already exists? ignore
                 if (!kind.isList && sectionView.getEditorCount() != 0) {
                     continue;
                 }