content: /*
   * Copyright (C) 2016 Facishare Technology Co., Ltd. All Rights Reserved.
   */
  package com.android.permission;
  
  import android.app.AlertDialog;
  import android.app.Dialog;
  import android.content.Context;
  import android.content.DialogInterface;
  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.android.floatwindowpermission.R;
  import com.android.permission.rom.HuaweiUtils;
  import com.android.permission.rom.MeizuUtils;
  import com.android.permission.rom.MiuiUtils;
  import com.android.permission.rom.OppoUtils;
  import com.android.permission.rom.QikuUtils;
  import com.android.permission.rom.RomUtils;
  import com.android.permission.rom.VivoUtils;
  
  import java.lang.reflect.Field;
  import java.lang.reflect.Method;
  
  /**
   * Description:
   *
   * @author zhaozp
   * @since 2016-10-17
   */
  
  public class FloatWindowManager {
      private static final String TAG = "FloatWindowManager";
  
      private static volatile FloatWindowManager instance;
      private Dialog dialog;
  
      public static FloatWindowManager getInstance() {
          if (instance == null) {
              synchronized (FloatWindowManager.class) {
                  if (instance == null) {
                      instance = new FloatWindowManager();
                  }
              }
          }
          return instance;
      }
  
      public boolean checkFloatPermission(Context context) {
          Log.i(TAG, "Start check permission");
          if (checkPermission(context)) {
              return true;
          } else {
              applyPermission(context);
              return false;
          }
      }
  
      public boolean checkPermission(Context context) {
          //6.0 版本之后由于 google 增加了对悬浮窗权限的管理，所以方式就统一了
          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);
              } else if (RomUtils.isOppoSystem()) {
                  return oppoROMPermissionCheck(context);
              } else if (RomUtils.isVivoSystem()) {
                  return vivoPermissionCheck(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 oppoROMPermissionCheck(Context context) {
          return OppoUtils.checkFloatWindowPermission(context);
      }
  
      private boolean vivoPermissionCheck(Context context) {
          return VivoUtils.checkFloatWindowPermission(context);
      }
  
      private boolean commonROMPermissionCheck(Context context) {
          //最新发现魅族6.0的系统这种方式不好用，天杀的，只有你是奇葩，没办法，单独适配一下
          if (RomUtils.checkIsMeizuRom()) {
              return meizuPermissionCheck(context);
          // VIVO的i管家比较厉害
          } else if (RomUtils.isVivoSystem()) {
              return vivoPermissionCheck(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);
              } else if (RomUtils.isOppoSystem()) {
                  oppoROMPermissionApply(context);
              }
          } else {
              commonROMPermissionApply(context);
          }
      }
  
      /**
       * 直接去申请权限
       * @param context
       */
      public void applyPermissionDirect(Context context) {
  
          if (Build.VERSION.SDK_INT < 23) {
              if (RomUtils.checkIsMiuiRom()) {
                  MiuiUtils.applyMiuiPermission(context);
              } else if (RomUtils.checkIsHuaweiRom()) {
                  HuaweiUtils.applyPermission(context);
              } else if (RomUtils.checkIs360Rom()) {
                  QikuUtils.applyPermission(context);
              } else if (RomUtils.isOppoSystem()) {
                  OppoUtils.applyOppoPermission(context);
              } else if (RomUtils.isVivoSystem()) {
                  VivoUtils.applyPermission(context);
              }
          } else {
              // 其他的再试一次
              if (RomUtils.checkIsMeizuRom()) {
                  MeizuUtils.applyPermission(context);
              } else if (RomUtils.isVivoSystem()) {
                  VivoUtils.applyPermission(context);
              } else {
                  try {
                      commonROMPermissionApplyInternal(context);
                  } catch (Exception e) {
                      Log.e(TAG, "Throw exception " + e.getMessage(), e);
                  }
              }
          }
      }
  
      private void ROM360PermissionApply(final Context context) {
          Log.i(TAG, "Request 360 permission");
          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) {
          Log.i(TAG, "Request HAWEI permission");
          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) {
          Log.i(TAG, "Request Flyme permission");
          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) {
          Log.i(TAG, "Request MIUI permission");
          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");
                  }
              }
          });
      }
  
      private void oppoROMPermissionApply(final Context context) {
          showConfirmDialog(context, new OnConfirmResult() {
              @Override
              public void confirmResult(boolean confirm) {
                  if (confirm) {
                      OppoUtils.applyOppoPermission(context);
                  } else {
                      Log.e(TAG, "ROM:miui, user manually refuse OVERLAY_PERMISSION");
                  }
              }
          });
      }
  
      /**
       * 通用 rom 权限申请
       */
      private void commonROMPermissionApply(final Context context) {
          //这里也一样，魅族系统需要单独适配
          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 {
                                  commonROMPermissionApplyInternal(context);
                              } catch (Exception e) {
                                  Log.e(TAG, Log.getStackTraceString(e));
                              }
                          } else {
                              Log.d(TAG, "user manually refuse OVERLAY_PERMISSION");
                              //需要做统计效果
                          }
                      }
                  });
              }
          }
      }
  
      public static void commonROMPermissionApplyInternal(Context context) throws NoSuchFieldException, IllegalAccessException {
          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);
      }
  
      private void showConfirmDialog(Context context, OnConfirmResult result) {
          showConfirmDialog(context, "您的手机没有授予悬浮窗权限，请开启后再试", 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(R.string.float__go_to_grant,
                          new DialogInterface.OnClickListener() {
                              @Override
                              public void onClick(DialogInterface dialog, int which) {
                                  result.confirmResult(true);
                                  dialog.dismiss();
                              }
                          }).setNegativeButton(R.string.float__no_grant_now,
                          new DialogInterface.OnClickListener() {
  
                              @Override
                              public void onClick(DialogInterface dialog, int which) {
                                  result.confirmResult(false);
                                  dialog.dismiss();
                              }
                          }).create();
          dialog.show();
      }
  
      private interface OnConfirmResult {
          void confirmResult(boolean confirm);
      }
  }
  