content: /*
   * Copyright (C) 2016 Facishare Technology Co., Ltd. All Rights Reserved.
   */
  package com.termux.floatball.permission.rom;
  
  import android.annotation.TargetApi;
  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.os.Build;
  import android.util.Log;
  import android.widget.Toast;
  
  import com.termux.floatball.R;
  
  import java.lang.reflect.Method;
  
  public class HuaweiUtils {
      private static final String TAG = "HuaweiUtils";
  
      /**
       * Check Huawei floating window permissions
       */
      public static boolean checkFloatWindowPermission(Context context) {
          final int version = Build.VERSION.SDK_INT;
          if (version >= 19) {
              return checkOp(context, 24); //OP_SYSTEM_ALERT_WINDOW = 24;
          }
          return true;
      }
  
      /**
       * Go to Huawei's permission application page
       */
      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");//Huawei Permission Management
  //   ComponentName comp = new ComponentName("com.huawei.systemmanager",
  //      "com.huawei.permissionmanager.ui.SingleAppActivity");//Huawei permission management, jump to the permission management location of the specified app requires Huawei interface permission, unresolved
              ComponentName comp = new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.addviewmonitor.AddViewMonitorActivity");//Floating window management page
              intent.setComponent(comp);
              if (RomUtils.getEmuiVersion() == 3.1) {
                  //emui 3.1's way
                  context.startActivity(intent);
              } else {
                  //emui 3.0's way'
                  comp = new ComponentName("com.huawei.systemmanager", "com.huawei.notificationmanager.ui.NotificationManagmentActivity");//Floating window management page
                  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");//Huawei Permission Management
              ComponentName comp = new ComponentName("com.huawei.systemmanager",
                  "com.huawei.permissionmanager.ui.MainActivity");//Huawei permission management, jump to the permission management location of the specified app requires Huawei interface permission, unresolved
  //      ComponentName comp = new ComponentName("com.huawei.systemmanager","com.huawei.systemmanager.addviewmonitor.AddViewMonitorActivity");//Floating window management page
              intent.setComponent(comp);
              context.startActivity(intent);
              Log.e(TAG, Log.getStackTraceString(e));
          } catch (ActivityNotFoundException e) {
              /**
               * The version of mobile manager is lower 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");//Huawei Permission Managementandroid4.4
  //   ComponentName comp = new ComponentName("com.android.settings","com.android.settings.permission.single_app_activity");//Here you can jump to the permission management page corresponding to the specified app, but relevant permissions are required and unresolved
              intent.setComponent(comp);
              context.startActivity(intent);
              e.printStackTrace();
              Log.e(TAG, Log.getStackTraceString(e));
          } catch (Exception e) {
              //exception
              Toast.makeText(context, context.getString(R.string.apply_permission), Toast.LENGTH_LONG).show();
              Log.e(TAG, Log.getStackTraceString(e));
          }
      }
  
      @TargetApi(Build.VERSION_CODES.KITKAT)
      private static boolean checkOp(Context context, int op) {
          final int version = Build.VERSION.SDK_INT;
          if (version >= 19) {
              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));
              }
          } else {
              Log.e(TAG, "Below API 19 cannot invoke!");
          }
          return false;
      }
  }
  
  
  