OSDN Git Service

Follow-up on preventing QC from hiding soft keyboard.
[android-x86/packages-apps-Contacts.git] / src / com / android / contacts / ui / QuickContactWindow.java
index baf42ac..6d4ff9b 100644 (file)
@@ -17,6 +17,7 @@
 package com.android.contacts.ui;
 
 import com.android.contacts.Collapser;
+import com.android.contacts.ContactPresenceIconUtil;
 import com.android.contacts.ContactsUtils;
 import com.android.contacts.R;
 import com.android.contacts.model.ContactsSource;
@@ -33,7 +34,6 @@ import android.content.ActivityNotFoundException;
 import android.content.ContentUris;
 import android.content.ContentValues;
 import android.content.Context;
-import android.content.EntityIterator;
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.content.pm.ApplicationInfo;
@@ -58,6 +58,7 @@ import android.provider.ContactsContract.CommonDataKinds.Photo;
 import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
 import android.provider.ContactsContract.CommonDataKinds.Website;
 import android.text.TextUtils;
+import android.util.AttributeSet;
 import android.util.Log;
 import android.view.ContextThemeWrapper;
 import android.view.Gravity;
@@ -84,6 +85,7 @@ import android.widget.CompoundButton;
 import android.widget.HorizontalScrollView;
 import android.widget.ImageView;
 import android.widget.ListView;
+import android.widget.RelativeLayout;
 import android.widget.TextView;
 import android.widget.Toast;
 
@@ -113,6 +115,32 @@ public class QuickContactWindow implements Window.Callback,
         public void onDismiss(QuickContactWindow dialog);
     }
 
+    /**
+     * Custom layout the sole purpose of which is to intercept the BACK key and
+     * close QC even when the soft keyboard is open.
+     */
+    public static class RootLayout extends RelativeLayout {
+
+        QuickContactWindow mQuickContactWindow;
+
+        public RootLayout(Context context, AttributeSet attrs) {
+            super(context, attrs);
+        }
+
+        /**
+         * Intercepts the BACK key event and dismisses QuickContact window.
+         */
+        @Override
+        public boolean dispatchKeyEventPreIme(KeyEvent event) {
+            if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
+                mQuickContactWindow.onBackPressed();
+                return true;
+            } else {
+                return super.dispatchKeyEventPreIme(event);
+            }
+        }
+    }
+
     private final Context mContext;
     private final LayoutInflater mInflater;
     private final WindowManager mWindowManager;
@@ -147,6 +175,7 @@ public class QuickContactWindow implements Window.Callback,
     private ImageView mArrowDown;
 
     private int mMode;
+    private RootLayout mRootView;
     private View mHeader;
     private HorizontalScrollView mTrackScroll;
     private ViewGroup mTrack;
@@ -235,9 +264,16 @@ public class QuickContactWindow implements Window.Callback,
         mWindow = PolicyManager.makeNewWindow(mContext);
         mWindow.setCallback(this);
         mWindow.setWindowManager(mWindowManager, null, null);
+        mWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED);
 
         mWindow.setContentView(R.layout.quickcontact);
 
+        mRootView = (RootLayout)mWindow.findViewById(R.id.root);
+        mRootView.mQuickContactWindow = this;
+        mRootView.setFocusable(true);
+        mRootView.setFocusableInTouchMode(true);
+        mRootView.setDescendantFocusability(RootLayout.FOCUS_AFTER_DESCENDANTS);
+
         mArrowUp = (ImageView)mWindow.findViewById(R.id.arrow_up);
         mArrowDown = (ImageView)mWindow.findViewById(R.id.arrow_down);
 
@@ -323,6 +359,18 @@ public class QuickContactWindow implements Window.Callback,
             android.os.Debug.startMethodTracing(TRACE_TAG);
         }
 
+        // Validate incoming parameters
+        final boolean validMode = (mode == QuickContact.MODE_SMALL
+                || mode == QuickContact.MODE_MEDIUM || mode == QuickContact.MODE_LARGE);
+        if (!validMode) {
+            throw new IllegalArgumentException("Invalid mode, expecting MODE_LARGE, "
+                    + "MODE_MEDIUM, or MODE_SMALL");
+        }
+
+        if (anchor == null) {
+            throw new IllegalArgumentException("Missing anchor rectangle");
+        }
+
         // Prepare header view for requested mode
         mLookupUri = lookupUri;
         mAnchor = new Rect(anchor);
@@ -340,6 +388,10 @@ public class QuickContactWindow implements Window.Callback,
 
         resetTrack();
 
+        // We need to have a focused view inside the QuickContact window so
+        // that the BACK key event can be intercepted
+        mRootView.requestFocus();
+
         mHasValidSocial = false;
         mDismissed = false;
         mQuerying = true;
@@ -605,30 +657,6 @@ public class QuickContactWindow implements Window.Callback,
     }
 
     /**
-     * Find the presence icon for showing in summary header.
-     */
-    private Drawable getPresenceIcon(int status) {
-        int resId = -1;
-        switch (status) {
-            case StatusUpdates.AVAILABLE:
-                resId = android.R.drawable.presence_online;
-                break;
-            case StatusUpdates.IDLE:
-            case StatusUpdates.AWAY:
-                resId = android.R.drawable.presence_away;
-                break;
-            case StatusUpdates.DO_NOT_DISTURB:
-                resId = android.R.drawable.presence_busy;
-                break;
-        }
-        if (resId != -1) {
-            return mContext.getResources().getDrawable(resId);
-        } else {
-            return null;
-        }
-    }
-
-    /**
      * Find the QuickContact-specific presence icon for showing in chiclets.
      */
     private Drawable getTrackPresenceIcon(int status) {
@@ -1203,7 +1231,7 @@ public class QuickContactWindow implements Window.Callback,
             // Read contact information from last data row
             final String name = cursor.getString(DataQuery.DISPLAY_NAME);
             final int presence = cursor.getInt(DataQuery.CONTACT_PRESENCE);
-            final Drawable statusIcon = getPresenceIcon(presence);
+            final Drawable statusIcon = ContactPresenceIconUtil.getPresenceIcon(mContext, presence);
 
             setHeaderText(R.id.name, name);
             setHeaderImage(R.id.presence, statusIcon);
@@ -1516,7 +1544,7 @@ public class QuickContactWindow implements Window.Callback,
      * window, which usually means we should dismiss.
      */
     protected void detectEventOutside(MotionEvent event) {
-        if (event.getAction() == MotionEvent.ACTION_DOWN) {
+        if (event.getAction() == MotionEvent.ACTION_DOWN && mDecor != null) {
             // Only try detecting outside events on down-press
             mDecor.getHitRect(mRect);
             mRect.top = mRect.top + mShadowTouch;