From a03949aeb1eb8c0d2d4bd3dd1d9e58bbb80b7f4e Mon Sep 17 00:00:00 2001 From: Manu Cornet Date: Thu, 1 Jun 2017 14:44:13 -0700 Subject: [PATCH] Use a PopupWindow to show tooltips 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) --- .../com/android/internal/view/TooltipPopup.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/core/java/com/android/internal/view/TooltipPopup.java b/core/java/com/android/internal/view/TooltipPopup.java index ebbbdbb0d6a6d..d834e6383acde 100644 --- a/core/java/com/android/internal/view/TooltipPopup.java +++ b/core/java/com/android/internal/view/TooltipPopup.java @@ -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) {