content: package androidx.appcompat.widget;
  
  import android.app.Activity;
  import android.content.Context;
  import android.content.ContextWrapper;
  import android.content.res.Resources;
  import android.graphics.Rect;
  import android.util.DisplayMetrics;
  import android.util.Log;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.view.WindowManager;
  import android.widget.TextView;
  import androidx.appcompat.R;
  import androidx.core.view.PointerIconCompat;
  
  class TooltipPopup {
      private static final String TAG = "TooltipPopup";
      private final View mContentView;
      private final Context mContext;
      private final WindowManager.LayoutParams mLayoutParams;
      private final TextView mMessageView;
      private final int[] mTmpAnchorPos = new int[2];
      private final int[] mTmpAppPos = new int[2];
      private final Rect mTmpDisplayFrame = new Rect();
  
      TooltipPopup(Context context) {
          WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
          this.mLayoutParams = layoutParams;
          this.mContext = context;
          View inflate = LayoutInflater.from(context).inflate(R.layout.abc_tooltip, (ViewGroup) null);
          this.mContentView = inflate;
          this.mMessageView = (TextView) inflate.findViewById(R.id.message);
          layoutParams.setTitle(getClass().getSimpleName());
          layoutParams.packageName = context.getPackageName();
          layoutParams.type = PointerIconCompat.TYPE_HAND;
          layoutParams.width = -2;
          layoutParams.height = -2;
          layoutParams.format = -3;
          layoutParams.windowAnimations = R.style.Animation_AppCompat_Tooltip;
          layoutParams.flags = 24;
      }
  
      private void computePosition(View anchorView, int anchorX, int anchorY, boolean fromTouch, WindowManager.LayoutParams outParams) {
          int i;
          int i2;
          WindowManager.LayoutParams layoutParams = outParams;
          layoutParams.token = anchorView.getApplicationWindowToken();
          int dimensionPixelOffset = this.mContext.getResources().getDimensionPixelOffset(R.dimen.tooltip_precise_anchor_threshold);
          int width = anchorView.getWidth() >= dimensionPixelOffset ? anchorX : anchorView.getWidth() / 2;
          if (anchorView.getHeight() >= dimensionPixelOffset) {
              int dimensionPixelOffset2 = this.mContext.getResources().getDimensionPixelOffset(R.dimen.tooltip_precise_anchor_extra_offset);
              i = anchorY + dimensionPixelOffset2;
              i2 = anchorY - dimensionPixelOffset2;
          } else {
              i = anchorView.getHeight();
              i2 = 0;
          }
          layoutParams.gravity = 49;
          int dimensionPixelOffset3 = this.mContext.getResources().getDimensionPixelOffset(fromTouch ? R.dimen.tooltip_y_offset_touch : R.dimen.tooltip_y_offset_non_touch);
          View appRootView = getAppRootView(anchorView);
          if (appRootView == null) {
              Log.e(TAG, "Cannot find app view");
              return;
          }
          appRootView.getWindowVisibleDisplayFrame(this.mTmpDisplayFrame);
          if (this.mTmpDisplayFrame.left < 0 && this.mTmpDisplayFrame.top < 0) {
              Resources resources = this.mContext.getResources();
              int identifier = resources.getIdentifier("status_bar_height", "dimen", "android");
              int dimensionPixelSize = identifier != 0 ? resources.getDimensionPixelSize(identifier) : 0;
              DisplayMetrics displayMetrics = resources.getDisplayMetrics();
              this.mTmpDisplayFrame.set(0, dimensionPixelSize, displayMetrics.widthPixels, displayMetrics.heightPixels);
          }
          appRootView.getLocationOnScreen(this.mTmpAppPos);
          anchorView.getLocationOnScreen(this.mTmpAnchorPos);
          int[] iArr = this.mTmpAnchorPos;
          int i3 = iArr[0];
          int[] iArr2 = this.mTmpAppPos;
          int i4 = i3 - iArr2[0];
          iArr[0] = i4;
          iArr[1] = iArr[1] - iArr2[1];
          layoutParams.x = (i4 + width) - (appRootView.getWidth() / 2);
          int makeMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, 0);
          this.mContentView.measure(makeMeasureSpec, makeMeasureSpec);
          int measuredHeight = this.mContentView.getMeasuredHeight();
          int i5 = this.mTmpAnchorPos[1];
          int i6 = ((i5 + i2) - dimensionPixelOffset3) - measuredHeight;
          int i7 = i5 + i + dimensionPixelOffset3;
          if (fromTouch) {
              if (i6 >= 0) {
                  layoutParams.y = i6;
              } else {
                  layoutParams.y = i7;
              }
          } else if (i7 + measuredHeight <= this.mTmpDisplayFrame.height()) {
              layoutParams.y = i7;
          } else {
              layoutParams.y = i6;
          }
      }
  
      private static View getAppRootView(View anchorView) {
          View rootView = anchorView.getRootView();
          ViewGroup.LayoutParams layoutParams = rootView.getLayoutParams();
          if ((layoutParams instanceof WindowManager.LayoutParams) && ((WindowManager.LayoutParams) layoutParams).type == 2) {
              return rootView;
          }
          for (Context context = anchorView.getContext(); context instanceof ContextWrapper; context = ((ContextWrapper) context).getBaseContext()) {
              if (context instanceof Activity) {
                  return ((Activity) context).getWindow().getDecorView();
              }
          }
          return rootView;
      }
  
      /* access modifiers changed from: package-private */
      public void hide() {
          if (isShowing()) {
              ((WindowManager) this.mContext.getSystemService("window")).removeView(this.mContentView);
          }
      }
  
      /* access modifiers changed from: package-private */
      public boolean isShowing() {
          return this.mContentView.getParent() != null;
      }
  
      /* access modifiers changed from: package-private */
      public void show(View anchorView, int anchorX, int anchorY, boolean fromTouch, CharSequence tooltipText) {
          if (isShowing()) {
              hide();
          }
          this.mMessageView.setText(tooltipText);
          computePosition(anchorView, anchorX, anchorY, fromTouch, this.mLayoutParams);
          ((WindowManager) this.mContext.getSystemService("window")).addView(this.mContentView, this.mLayoutParams);
      }
  }
  