1/2 Allow the keyboard to resize the compact editor layout

So we're reverting the fix submitted in ag/680795 and
preventing the landscape layout from getting messed up
by basing the photo resizing off of the display dimensions.

The reason is that adjustResize provides a better UX and
is necessary to fix the bugs related to showing and hiding
the type downdowns propertly.

Bug 20526470

Change-Id: Ia6755336cb8d1fa78142b754e5d40a45b4c50d44
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 536d543..d7ee9c8 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -341,7 +341,7 @@
             android:name=".activities.CompactContactEditorActivity"
             android:label="@string/launcherActivityLabel"
             android:theme="@style/EditorActivityTheme"
-            android:windowSoftInputMode="stateHidden|adjustPan">
+            android:windowSoftInputMode="stateHidden|adjustResize">
 
             <intent-filter android:label="@string/editContactDescription">
                 <action android:name="android.intent.action.EDIT" />
diff --git a/src/com/android/contacts/editor/CompactPhotoEditorView.java b/src/com/android/contacts/editor/CompactPhotoEditorView.java
index 7483af6..0b1cbe8 100644
--- a/src/com/android/contacts/editor/CompactPhotoEditorView.java
+++ b/src/com/android/contacts/editor/CompactPhotoEditorView.java
@@ -31,7 +31,9 @@
 import com.android.contacts.util.SchedulingUtils;
 import com.android.contacts.widget.QuickContactImageView;
 
+import android.app.Activity;
 import android.content.Context;
+import android.content.res.TypedArray;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.graphics.drawable.GradientDrawable;
@@ -40,6 +42,7 @@
 import android.provider.ContactsContract.CommonDataKinds.Photo;
 import android.provider.ContactsContract.DisplayPhoto;
 import android.util.AttributeSet;
+import android.util.DisplayMetrics;
 import android.util.Log;
 import android.util.TypedValue;
 import android.view.View;
@@ -61,6 +64,9 @@
     private final float mPortraitPhotoRatio;
     private final boolean mIsTwoPanel;
 
+    private final int mActionBarHeight;
+    private final int mStatusBarHeight;
+
     private ValuesDelta mValuesDelta;
     private boolean mReadOnly;
     private boolean mIsPhotoSet;
@@ -78,6 +84,15 @@
         mLandscapePhotoRatio = getTypedFloat(R.dimen.quickcontact_landscape_photo_ratio);
         mPortraitPhotoRatio = getTypedFloat(R.dimen.editor_portrait_photo_ratio);
         mIsTwoPanel = getResources().getBoolean(R.bool.quickcontact_two_panel);
+
+        final TypedArray styledAttributes = getContext().getTheme().obtainStyledAttributes(
+                new int[] { android.R.attr.actionBarSize });
+        mActionBarHeight = (int) styledAttributes.getDimension(0, 0);
+        styledAttributes.recycle();
+
+        final int resourceId = getResources().getIdentifier(
+                "status_bar_height", "dimen", "android");
+        mStatusBarHeight = resourceId > 0 ? getResources().getDimensionPixelSize(resourceId) : 0;
     }
 
     private float getTypedFloat(int resourceId) {
@@ -144,15 +159,13 @@
             setDefaultPhotoTint();
         }
 
-        // Adjust the photo dimensions following the same logic as in
-        // MultiShrinkScroll.initialize
+        // Adjust the photo dimensions following the same logic as MultiShrinkScroll.initialize
         SchedulingUtils.doOnPreDraw(this, /* drawNextFrame =*/ false, new Runnable() {
             @Override
             public void run() {
                 final int photoHeight, photoWidth;
                 if (mIsTwoPanel) {
-                    // Make the photo slightly more narrow than it is tall
-                    photoHeight = getHeight();
+                    photoHeight = getContentViewHeight();
                     photoWidth = (int) (photoHeight * mLandscapePhotoRatio);
                 } else {
                     // Make the photo slightly shorter that it is wide
@@ -167,6 +180,17 @@
         });
     }
 
+    // We're calculating the height the hard way because using the height of the content view
+    // (found using android.view.Window.ID_ANDROID_CONTENT) with the soft keyboard up when
+    // going from portrait to landscape mode results in a very small height value.
+    // See b/20526470
+    private int getContentViewHeight() {
+        final Activity activity = (Activity) getContext();
+        final DisplayMetrics displayMetrics = new DisplayMetrics();
+        activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
+        return displayMetrics.heightPixels - mActionBarHeight - mStatusBarHeight;
+    }
+
     /**
      * Set the {@link PhotoHandler} to forward clicks (i.e. requests to edit the photo) to.
      */