content: /*
   * Copyright (C) 2016 Facishare Technology Co., Ltd. All Rights Reserved.
   */
  package com.termux.floatball.permission;
  
  import android.app.AlertDialog;
  import android.app.Dialog;
  import android.content.Context;
  import android.content.Intent;
  import android.graphics.PixelFormat;
  import android.graphics.Point;
  import android.net.Uri;
  import android.os.Build;
  import android.provider.Settings;
  import android.util.Log;
  import android.view.Gravity;
  import android.view.WindowManager;
  
  import com.termux.floatball.R;
  import com.termux.floatball.permission.rom.HuaweiUtils;
  import com.termux.floatball.permission.rom.MeizuUtils;
  import com.termux.floatball.permission.rom.MiuiUtils;
  import com.termux.floatball.permission.rom.QikuUtils;
  import com.termux.floatball.permission.rom.RomUtils;
  
  import java.lang.reflect.Field;
  import java.lang.reflect.Method;
  
  /**
   * Description:
   *
   * @author zhaozp
   * @since 2016-10-17
   */
  
  public class FloatPermissionManager {
      private static final String TAG = "FloatPermissionManager";
  
  
      private boolean isWindowDismiss = true;
      private WindowManager windowManager = null;
      private WindowManager.LayoutParams mParams = null;
      private Dialog dialog;
  
  
      public void applyOrShowFloatWindow(Context context) {
          if (checkPermission(context)) {
              showWindow(context);
          } else {
              applyPermission(context);
          }
      }
  
      public boolean checkPermission(Context context) {
          //After version 6.0, Google added management of floating window permissions, so the method is unified.
          if (Build.VERSION.SDK_INT < 23) {
              if (RomUtils.checkIsMiuiRom()) {
                  return miuiPermissionCheck(context);
              } else if (RomUtils.checkIsMeizuRom()) {
                  return meizuPermissionCheck(context);
              } else if (RomUtils.checkIsHuaweiRom()) {
                  return huaweiPermissionCheck(context);
              } else if (RomUtils.checkIs360Rom()) {
                  return qikuPermissionCheck(context);
              }
          }
          return commonROMPermissionCheck(context);
      }
  
      private boolean huaweiPermissionCheck(Context context) {
          return HuaweiUtils.checkFloatWindowPermission(context);
      }
  
      private boolean miuiPermissionCheck(Context context) {
          return MiuiUtils.checkFloatWindowPermission(context);
      }
  
      private boolean meizuPermissionCheck(Context context) {
          return MeizuUtils.checkFloatWindowPermission(context);
      }
  
      private boolean qikuPermissionCheck(Context context) {
          return QikuUtils.checkFloatWindowPermission(context);
      }
  
      private boolean commonROMPermissionCheck(Context context) {
          //I recently discovered that this method doesn't work well with the Meizu 6.0 system. you are the only weirdo. There is no other way. You have to adapt it separately.
          if (RomUtils.checkIsMeizuRom()) {
              return meizuPermissionCheck(context);
          } else {
              Boolean result = true;
              if (Build.VERSION.SDK_INT >= 23) {
                  try {
                      Class clazz = Settings.class;
                      Method canDrawOverlays = clazz.getDeclaredMethod("canDrawOverlays", Context.class);
                      result = (Boolean) canDrawOverlays.invoke(null, context);
                  } catch (Exception e) {
                      Log.e(TAG, Log.getStackTraceString(e));
                  }
              }
              return result;
          }
      }
  
      public void applyPermission(Context context) {
          if (Build.VERSION.SDK_INT < 23) {
              if (RomUtils.checkIsMiuiRom()) {
                  miuiROMPermissionApply(context);
              } else if (RomUtils.checkIsMeizuRom()) {
                  meizuROMPermissionApply(context);
              } else if (RomUtils.checkIsHuaweiRom()) {
                  huaweiROMPermissionApply(context);
              } else if (RomUtils.checkIs360Rom()) {
                  ROM360PermissionApply(context);
              }
          }
          commonROMPermissionApply(context);
      }
  
      private void ROM360PermissionApply(final Context context) {
          showConfirmDialog(context, new OnConfirmResult() {
              @Override
              public void confirmResult(boolean confirm) {
                  if (confirm) {
                      QikuUtils.applyPermission(context);
                  } else {
                      Log.e(TAG, "ROM:360, user manually refuse OVERLAY_PERMISSION");
                  }
              }
          });
      }
  
      private void huaweiROMPermissionApply(final Context context) {
          showConfirmDialog(context, new OnConfirmResult() {
              @Override
              public void confirmResult(boolean confirm) {
                  if (confirm) {
                      HuaweiUtils.applyPermission(context);
                  } else {
                      Log.e(TAG, "ROM:huawei, user manually refuse OVERLAY_PERMISSION");
                  }
              }
          });
      }
  
      private void meizuROMPermissionApply(final Context context) {
          showConfirmDialog(context, new OnConfirmResult() {
              @Override
              public void confirmResult(boolean confirm) {
                  if (confirm) {
                      MeizuUtils.applyPermission(context);
                  } else {
                      Log.e(TAG, "ROM:meizu, user manually refuse OVERLAY_PERMISSION");
                  }
              }
          });
      }
  
      private void miuiROMPermissionApply(final Context context) {
          showConfirmDialog(context, new OnConfirmResult() {
              @Override
              public void confirmResult(boolean confirm) {
                  if (confirm) {
                      MiuiUtils.applyMiuiPermission(context);
                  } else {
                      Log.e(TAG, "ROM:miui, user manually refuse OVERLAY_PERMISSION");
                  }
              }
          });
      }
  
      /**
       * Universal ROM permission application
       */
      private void commonROMPermissionApply(final Context context) {
          //The same is true here, the Meizu system needs to be adapted separately
          if (RomUtils.checkIsMeizuRom()) {
              meizuROMPermissionApply(context);
          } else {
              if (Build.VERSION.SDK_INT >= 23) {
                  showConfirmDialog(context, new OnConfirmResult() {
                      @Override
                      public void confirmResult(boolean confirm) {
                          if (confirm) {
                              try {
                                  Class clazz = Settings.class;
                                  Field field = clazz.getDeclaredField("ACTION_MANAGE_OVERLAY_PERMISSION");
  
                                  Intent intent = new Intent(field.get(null).toString());
                                  intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                  intent.setData(Uri.parse("package:" + context.getPackageName()));
                                  context.startActivity(intent);
                              } catch (Exception e) {
                                  Log.e(TAG, Log.getStackTraceString(e));
                              }
                          } else {
                              Log.d(TAG, "user manually refuse OVERLAY_PERMISSION");
                          }
                      }
                  });
              }
          }
      }
  
      private void showConfirmDialog(Context context, OnConfirmResult result) {
          showConfirmDialog(context, context.getString(R.string.no_display_over_app_permission), result);
      }
  
      private void showConfirmDialog(Context context, String message, final OnConfirmResult result) {
          if (dialog != null && dialog.isShowing()) {
              dialog.dismiss();
          }
  
          dialog = new AlertDialog.Builder(context).setCancelable(true).setTitle("")
              .setMessage(message)
              .setPositiveButton(context.getString(R.string.open_permission),
                  (dialog, which) -> {
                      result.confirmResult(true);
                      dialog.dismiss();
                  }).setNegativeButton(context.getString(R.string.not_open_permission),
                  (dialog, which) -> {
                      result.confirmResult(false);
                      dialog.dismiss();
                  }).create();
          dialog.show();
      }
  
      private interface OnConfirmResult {
          void confirmResult(boolean confirm);
      }
  
      private void showWindow(Context context) {
          if (!isWindowDismiss) {
              Log.e(TAG, "view is already added here");
              return;
          }
  
          isWindowDismiss = false;
          if (windowManager == null) {
              windowManager = (WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
          }
  
          Point size = new Point();
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
              windowManager.getDefaultDisplay().getSize(size);
          }
          int screenWidth = size.x;
          int screenHeight = size.y;
  
          mParams = new WindowManager.LayoutParams();
          mParams.packageName = context.getPackageName();
          mParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
          mParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
          mParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
              | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
              | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
          mParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
          mParams.format = PixelFormat.RGBA_8888;
          mParams.gravity = Gravity.LEFT | Gravity.TOP;
          mParams.x = screenWidth - dp2px(context, 100);
          mParams.y = screenHeight - dp2px(context, 171);
  
  
  //        ImageView imageView = new ImageView(mContext);
  //        imageView.setImageResource(R.mDrawable.app_icon);
      }
  
  
      private int dp2px(Context context, float dp) {
          final float scale = context.getResources().getDisplayMetrics().density;
          return (int) (dp * scale + 0.5f);
      }
  }
  