content: /*
   * Copyright (C) 2016 Facishare Technology Co., Ltd. All Rights Reserved.
   */
  package io.agora.api.example.common.floatwindow.rom;
  
  import android.app.AppOpsManager;
  import android.content.ActivityNotFoundException;
  import android.content.ComponentName;
  import android.content.Context;
  import android.content.Intent;
  import android.os.Binder;
  import android.util.Log;
  import android.widget.Toast;
  
  import java.lang.reflect.Method;
  
  /**
   * The type Huawei utils.
   */
  public final class HuaweiUtils {
      private static final String TAG = "HuaweiUtils";
  
      private HuaweiUtils() {
  
      }
  
      /**
       * 检测 Huawei 悬浮窗权限
       *
       * @param context the context
       * @return the boolean
       */
      public static boolean checkFloatWindowPermission(Context context) {
          return checkOp(context, 24); //OP_SYSTEM_ALERT_WINDOW = 24;
      }
  
      /**
       * 去华为权限申请页面
       *
       * @param context the context
       */
      public static void applyPermission(Context context) {
          try {
              Intent intent = new Intent();
              intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  //   ComponentName comp = new ComponentName("com.huawei.systemmanager","com.huawei.permissionmanager.ui.MainActivity");//华为权限管理
  //   ComponentName comp = new ComponentName("com.huawei.systemmanager",
  //      "com.huawei.permissionmanager.ui.SingleAppActivity");//华为权限管理，跳转到指定app的权限管理位置需要华为接口权限，未解决
              ComponentName comp = new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.addviewmonitor.AddViewMonitorActivity"); //悬浮窗管理页面
              intent.setComponent(comp);
              final double versionDiff = 3.1;
              if (RomUtils.getEmuiVersion() == versionDiff) {
                  //emui 3.1 的适配
                  context.startActivity(intent);
              } else {
                  //emui 3.0 的适配
                  comp = new ComponentName("com.huawei.systemmanager", "com.huawei.notificationmanager.ui.NotificationManagmentActivity"); //悬浮窗管理页面
                  intent.setComponent(comp);
                  context.startActivity(intent);
              }
          } catch (SecurityException e) {
              Intent intent = new Intent();
              intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  //   ComponentName comp = new ComponentName("com.huawei.systemmanager","com.huawei.permissionmanager.ui.MainActivity");//华为权限管理
              ComponentName comp = new ComponentName("com.huawei.systemmanager",
                      "com.huawei.permissionmanager.ui.MainActivity"); //华为权限管理，跳转到本app的权限管理页面,这个需要华为接口权限，未解决
  //      ComponentName comp = new ComponentName("com.huawei.systemmanager","com.huawei.systemmanager.addviewmonitor.AddViewMonitorActivity");//悬浮窗管理页面
              intent.setComponent(comp);
              context.startActivity(intent);
              Log.e(TAG, Log.getStackTraceString(e));
          } catch (ActivityNotFoundException e) {
              /*
               * 手机管家版本较低 HUAWEI SC-UL10
               */
  //   Toast.makeText(MainActivity.this, "act找不到", Toast.LENGTH_LONG).show();
              Intent intent = new Intent();
              intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
              ComponentName comp = new ComponentName("com.Android.settings", "com.android.settings.permission.TabItem"); //权限管理页面 android4.4
  //   ComponentName comp = new ComponentName("com.android.settings","com.android.settings.permission.single_app_activity"); //此处可跳转到指定app对应的权限管理页面，但是需要相关权限，未解决
              intent.setComponent(comp);
              context.startActivity(intent);
              e.printStackTrace();
              Log.e(TAG, Log.getStackTraceString(e));
          } catch (Exception e) {
              //抛出异常时提示信息
              Toast.makeText(context, "进入设置页面失败，请手动设置", Toast.LENGTH_LONG).show();
              Log.e(TAG, Log.getStackTraceString(e));
          }
      }
  
      private static boolean checkOp(Context context, int op) {
          AppOpsManager manager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
          try {
              Class clazz = AppOpsManager.class;
              Method method = clazz.getDeclaredMethod("checkOp", int.class, int.class, String.class);
              return AppOpsManager.MODE_ALLOWED == (int) method.invoke(manager, op, Binder.getCallingUid(), context.getPackageName());
          } catch (Exception e) {
              Log.e(TAG, Log.getStackTraceString(e));
          }
          return false;
      }
  }
  
  
  