@Override
 public void onConfigurationChanged(Configuration newConfig) {
   super.onConfigurationChanged(newConfig);
   // We need to relayout the window as dialog width may be
   // different in landscape vs portrait which affect the min
   // window height needed to show all content. We have to
   // re-add the window to force it to be resized if needed.
   View decor = getWindow().getDecorView();
   getWindowManager().removeViewImmediate(decor);
   getWindowManager().addView(decor, decor.getLayoutParams());
   if (mViewHandler instanceof GrantPermissionsDefaultViewHandler) {
     ((GrantPermissionsDefaultViewHandler) mViewHandler).onConfigurationChanged();
   }
 }
  @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();
    }
  }
 @Override
 public void finish() {
   setResultIfNeeded(RESULT_CANCELED);
   super.finish();
 }
 @Override
 protected void onRestoreInstanceState(Bundle savedInstanceState) {
   super.onRestoreInstanceState(savedInstanceState);
   mViewHandler.loadInstanceState(savedInstanceState);
 }
 @Override
 protected void onSaveInstanceState(Bundle outState) {
   super.onSaveInstanceState(outState);
   mViewHandler.saveInstanceState(outState);
 }