private void logRequestedPermissionGroups() {
    if (mRequestGrantPermissionGroups.isEmpty()) {
      return;
    }

    final int groupCount = mRequestGrantPermissionGroups.size();
    List<AppPermissionGroup> groups = new ArrayList<>(groupCount);
    for (GroupState groupState : mRequestGrantPermissionGroups.values()) {
      groups.add(groupState.mGroup);
    }

    SafetyNetLogger.logPermissionsRequested(mAppPermissions.getPackageInfo(), groups);
  }
  private boolean showNextPermissionGroupGrantRequest() {
    final int groupCount = mRequestGrantPermissionGroups.size();

    int currentIndex = 0;
    for (GroupState groupState : mRequestGrantPermissionGroups.values()) {
      if (groupState.mState == GroupState.STATE_UNKNOWN) {
        CharSequence appLabel = mAppPermissions.getAppLabel();
        SpannableString message =
            new SpannableString(
                getString(
                    R.string.permission_warning_template,
                    appLabel,
                    groupState.mGroup.getDescription()));
        // Set the permission message as the title so it can be announced.
        setTitle(message);
        // Color the app name.
        int appLabelStart = message.toString().indexOf(appLabel.toString(), 0);
        int appLabelLength = appLabel.length();
        int color = getColor(R.color.grant_permissions_app_color);
        message.setSpan(
            new ForegroundColorSpan(color), appLabelStart, appLabelStart + appLabelLength, 0);

        // Set the new grant view
        // TODO: Use a real message for the action. We need group action APIs
        Resources resources;
        try {
          resources =
              getPackageManager().getResourcesForApplication(groupState.mGroup.getIconPkg());
        } catch (NameNotFoundException e) {
          // Fallback to system.
          resources = Resources.getSystem();
        }
        int icon = groupState.mGroup.getIconResId();

        mViewHandler.updateUi(
            groupState.mGroup.getName(),
            groupCount,
            currentIndex,
            Icon.createWithResource(resources, icon),
            message,
            groupState.mGroup.isUserSet());
        return true;
      }

      currentIndex++;
    }

    return false;
  }
  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setFinishOnTouchOutside(false);

    setTitle(R.string.permission_request_title);

    if (DeviceUtils.isTelevision(this)) {
      mViewHandler = new GrantPermissionsTvViewHandler(this).setResultListener(this);
    } else if (DeviceUtils.isWear(this)) {
      mViewHandler = new GrantPermissionsWatchViewHandler(this).setResultListener(this);
    } else {
      mViewHandler = new GrantPermissionsDefaultViewHandler(this).setResultListener(this);
    }

    mRequestedPermissions =
        getIntent().getStringArrayExtra(PackageManager.EXTRA_REQUEST_PERMISSIONS_NAMES);
    if (mRequestedPermissions == null) {
      mRequestedPermissions = new String[0];
    }

    final int requestedPermCount = mRequestedPermissions.length;
    mGrantResults = new int[requestedPermCount];

    if (requestedPermCount == 0) {
      setResultAndFinish();
      return;
    }

    PackageInfo callingPackageInfo = getCallingPackageInfo();

    DevicePolicyManager devicePolicyManager = getSystemService(DevicePolicyManager.class);
    final int permissionPolicy = devicePolicyManager.getPermissionPolicy(null);

    // If calling package is null we default to deny all.
    updateDefaultResults(callingPackageInfo, permissionPolicy);

    if (callingPackageInfo == null) {
      setResultAndFinish();
      return;
    }

    mAppPermissions =
        new AppPermissions(
            this,
            callingPackageInfo,
            null,
            false,
            new Runnable() {
              @Override
              public void run() {
                setResultAndFinish();
              }
            });

    for (AppPermissionGroup group : mAppPermissions.getPermissionGroups()) {
      boolean groupHasRequestedPermission = false;
      for (String requestedPermission : mRequestedPermissions) {
        if (group.hasPermission(requestedPermission)) {
          groupHasRequestedPermission = true;
          break;
        }
      }
      if (!groupHasRequestedPermission) {
        continue;
      }
      // We allow the user to choose only non-fixed permissions. A permission
      // is fixed either by device policy or the user denying with prejudice.
      if (!group.isUserFixed() && !group.isPolicyFixed()) {
        switch (permissionPolicy) {
          case DevicePolicyManager.PERMISSION_POLICY_AUTO_GRANT:
            {
              if (!group.areRuntimePermissionsGranted()) {
                group.grantRuntimePermissions(false);
              }
              group.setPolicyFixed();
            }
            break;

          case DevicePolicyManager.PERMISSION_POLICY_AUTO_DENY:
            {
              if (group.areRuntimePermissionsGranted()) {
                group.revokeRuntimePermissions(false);
              }
              group.setPolicyFixed();
            }
            break;

          default:
            {
              if (!group.areRuntimePermissionsGranted()) {
                mRequestGrantPermissionGroups.put(group.getName(), new GroupState(group));
              } else {
                group.grantRuntimePermissions(false);
                updateGrantResults(group);
              }
            }
            break;
        }
      } else {
        // if the permission is fixed, ensure that we return the right request result
        updateGrantResults(group);
      }
    }

    setContentView(mViewHandler.createView());

    Window window = getWindow();
    WindowManager.LayoutParams layoutParams = window.getAttributes();
    mViewHandler.updateWindowAttributes(layoutParams);
    window.setAttributes(layoutParams);

    if (!showNextPermissionGroupGrantRequest()) {
      setResultAndFinish();
    }
  }