OSDN Git Service

Use a PopupWindow to show tooltips
authorManu Cornet <manucornet@google.com>
Thu, 1 Jun 2017 21:44:13 +0000 (14:44 -0700)
committerManu Cornet <manucornet@google.com>
Fri, 2 Jun 2017 20:04:13 +0000 (20:04 +0000)
This allows tooltips to work even in a context where they don't belong
in any activity (and therefore no window token to use). It also
simplifies a tiny bit the logic of how to get the view to show up.

Test: Checked tooltip behavior in and outside an app
Bug: 62065980
Change-Id: I6c02009c4fdd6d4bc4fa2cf8019955360506f0ee
(cherry picked from commit 77e539775b52da55a8b23f1a9765d471ee782013)

core/java/com/android/internal/view/TooltipPopup.java

index ebbbdbb..d834e63 100644 (file)
@@ -25,6 +25,7 @@ import android.view.LayoutInflater;
 import android.view.View;
 import android.view.WindowManager;
 import android.view.WindowManagerGlobal;
+import android.widget.PopupWindow;
 import android.widget.TextView;
 
 public class TooltipPopup {
@@ -32,6 +33,7 @@ public class TooltipPopup {
 
     private final Context mContext;
 
+    private final PopupWindow mPopupWindow;
     private final View mContentView;
     private final TextView mMessageView;
 
@@ -43,6 +45,8 @@ public class TooltipPopup {
     public TooltipPopup(Context context) {
         mContext = context;
 
+        mPopupWindow = new PopupWindow(context);
+        mPopupWindow.setBackgroundDrawable(null);
         mContentView = LayoutInflater.from(mContext).inflate(
                 com.android.internal.R.layout.tooltip, null);
         mMessageView = (TextView) mContentView.findViewById(
@@ -70,17 +74,16 @@ public class TooltipPopup {
 
         computePosition(anchorView, anchorX, anchorY, fromTouch, mLayoutParams);
 
-        WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
-        wm.addView(mContentView, mLayoutParams);
+        mPopupWindow.setContentView(mContentView);
+        mPopupWindow.showAtLocation(
+            anchorView, mLayoutParams.gravity, mLayoutParams.x, mLayoutParams.y);
     }
 
     public void hide() {
         if (!isShowing()) {
             return;
         }
-
-        WindowManager wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
-        wm.removeView(mContentView);
+        mPopupWindow.dismiss();
     }
 
     public View getContentView() {
@@ -88,7 +91,7 @@ public class TooltipPopup {
     }
 
     public boolean isShowing() {
-        return mContentView.getParent() != null;
+        return mPopupWindow.isShowing();
     }
 
     public void updateContent(CharSequence tooltipText) {