content: package com.termux.floatball;
  
  import android.app.Activity;
  import android.content.Context;
  import android.content.res.Configuration;
  import android.graphics.Point;
  import android.view.View;
  import android.view.WindowManager;
  
  import com.termux.floatball.menu.FloatMenu;
  import com.termux.floatball.menu.FloatMenuCfg;
  import com.termux.floatball.menu.MenuItem;
  import com.termux.floatball.widget.FloatBall;
  import com.termux.floatball.widget.FloatBallCfg;
  import com.termux.floatball.widget.StatusBarView;
  
  import java.util.ArrayList;
  import java.util.List;
  
  
  public class FloatBallManager {
      public int mScreenWidth, mScreenHeight;
  
      private IFloatBallPermission mPermission;
      private OnFloatBallClickListener mFloatBallClickListener;
      private final WindowManager mWindowManager;
      private final Context mContext;
      private final FloatBall floatBall;
      private final FloatMenu floatMenu;
      private final StatusBarView statusBarView;
      public int floatBallX, floatBallY;
      private boolean isShowing = false;
      private List<MenuItem> menuItems = new ArrayList<>();
      private boolean mFloatBallOverOtherApp = false;
  
      public boolean isFloatBallOverOtherApp() {
          return mFloatBallOverOtherApp;
      }
      public FloatBall getFloatBall(){
          return this.floatBall;
      }
  
      public void setFloatBallOverOtherApp(boolean floatBallOverOtherApp) {
          this.mFloatBallOverOtherApp = floatBallOverOtherApp;
      }
  
      public FloatBallManager(Context application, FloatBallCfg ballCfg) {
          this(application, ballCfg, null);
      }
  
      public FloatBallManager(Context application, FloatBallCfg ballCfg, FloatMenuCfg menuCfg) {
          mContext = application;
          FloatBallUtil.inSingleActivity = false;
          mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
          computeScreenSize();
          floatBall = new FloatBall(mContext, this, ballCfg);
          floatMenu = new FloatMenu(mContext, this, menuCfg);
          statusBarView = new StatusBarView(mContext, this);
      }
  
      public void buildMenu() {
          inflateMenuItem();
      }
  
      /**
       * add menu item
       *
       * @param item
       */
      public FloatBallManager addMenuItem(MenuItem item) {
          menuItems.add(item);
          return this;
      }
  
      public int getMenuItemSize() {
          return menuItems != null ? menuItems.size() : 0;
      }
  
      /**
       * set menu item
       *
       * @param items
       */
      public FloatBallManager setMenu(List<MenuItem> items) {
          menuItems = items;
          return this;
      }
  
      private void inflateMenuItem() {
          floatMenu.removeAllItemViews();
          for (MenuItem item : menuItems) {
              floatMenu.addItem(item);
          }
      }
  
      public int getBallSize() {
          return floatBall.getSize();
      }
  
      public void computeScreenSize() {
          Point point = new Point();
          mWindowManager.getDefaultDisplay().getSize(point);
          mScreenWidth = point.x;
          mScreenHeight = point.y;
      }
  
      public int getStatusBarHeight() {
          return statusBarView.getStatusBarHeight();
      }
  
      public void onStatusBarHeightChange() {
          floatBall.onLayoutChange();
      }
  
      public void show() {
          if (mFloatBallOverOtherApp) {
              if (mPermission == null) {
                  return;
              }
              if (!mPermission.hasFloatBallPermission(mContext)) {
                  mPermission.onRequestFloatBallPermission();
                  return;
              }
          }
          if (isShowing) return;
          isShowing = true;
          floatBall.setVisibility(View.VISIBLE);
          statusBarView.attachToWindow(mWindowManager);
          floatBall.attachToWindow(mWindowManager);
          floatMenu.detachFromWindow(mWindowManager);
      }
  
      public void closeMenu() {
          floatMenu.closeMenu();
      }
  
      public void reset() {
          floatBall.setVisibility(View.VISIBLE);
          floatBall.postSleepRunnable();
          floatMenu.detachFromWindow(mWindowManager);
      }
  
      public void onFloatBallClick() {
          if (menuItems != null && !menuItems.isEmpty()) {
              floatMenu.attachToWindow(mWindowManager);
              if (mFloatBallClickListener != null) {
                  mFloatBallClickListener.onFloatBallClick();
              }
          } else {
              if (mFloatBallClickListener != null) {
                  mFloatBallClickListener.onFloatBallClick();
              }
          }
      }
  
      public void hide() {
          if (!isShowing) return;
          isShowing = false;
          floatBall.detachFromWindow(mWindowManager);
          floatMenu.detachFromWindow(mWindowManager);
          statusBarView.detachFromWindow(mWindowManager);
      }
  
      public void onConfigurationChanged(Configuration newConfig) {
          computeScreenSize();
          reset();
      }
  
      public void setPermission(IFloatBallPermission iPermission) {
          this.mPermission = iPermission;
      }
  
      public void setOnFloatBallClickListener(OnFloatBallClickListener listener) {
          mFloatBallClickListener = listener;
      }
  
      public interface OnFloatBallClickListener {
          void onFloatBallClick();
      }
  
      public interface IFloatBallPermission {
          /**
           * request the permission of floatball,just use {@link #requestFloatBallPermission(Activity)},
           * or use your custom method.
           *
           * @return return true if requested the permission
           * @see #requestFloatBallPermission(Activity)
           */
          boolean onRequestFloatBallPermission();
  
          /**
           * detect whether allow  using floatball here or not.
           *
           * @return
           */
          boolean hasFloatBallPermission(Context context);
  
          /**
           * request floatball permission
           */
          void requestFloatBallPermission(Activity activity);
      }
  }
  