content: package androidkun.com.versionupdatelibrary.Activity;
  
  import android.Manifest;
  import android.app.Activity;
  import android.app.AlertDialog;
  import android.content.DialogInterface;
  import android.content.Intent;
  import android.content.pm.PackageManager;
  import android.net.Uri;
  import android.os.Build;
  import android.os.Bundle;
  import android.provider.Settings;
  import android.support.annotation.RequiresApi;
  import android.support.v4.app.ActivityCompat;
  import android.support.v4.content.ContextCompat;
  import android.view.KeyEvent;
  import android.widget.Toast;
  
  
  import java.util.ArrayList;
  import java.util.List;
  
  import androidkun.com.versionupdatelibrary.R;
  import androidkun.com.versionupdatelibrary.entity.VersionUpdateConfig;
  
  /***
   * 透明的Activity 用于运行权限检测
   */
  public class TranslucentActivity extends Activity implements
          ActivityCompat.OnRequestPermissionsResultCallback {
      /**
       * 需要进行检测的权限数组
       */
      protected String[] needPermissions = {
              Manifest.permission.WRITE_EXTERNAL_STORAGE,
              Manifest.permission.READ_EXTERNAL_STORAGE
      };
  
      private static final int PERMISSON_REQUESTCODE = 0;
  
      //未知来源app安装权限
      private static final int InstallPermission = 10022;
      /**
       * 判断是否需要检测，防止不停的弹框
       */
      private boolean isNeedCheck = true;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_translucent);
      }
  
      @Override
      protected void onResume() {
          super.onResume();
          if (isNeedCheck) {
              checkPermissions(needPermissions);
          }
      }
  
      /**
       * @since 2.5.0
       */
      private void checkPermissions(String... permissions) {
          List<String> needRequestPermissonList = findDeniedPermissions(permissions);
          if (null != needRequestPermissonList
                  && needRequestPermissonList.size() > 0) {
              ActivityCompat.requestPermissions(this,
                      needRequestPermissonList.toArray(
                              new String[needRequestPermissonList.size()]),
                      PERMISSON_REQUESTCODE);
          } else {
              //权限已授权  检查未知来源apk安装权限
              checkInstallProcess();
  //            VersionUpdateConfig.getInstance().passCheck();
  //            finish();
          }
      }
  
      /**
       * 获取权限集中需要申请权限的列表
       *
       * @param permissions
       * @return
       * @since 2.5.0
       */
      private List<String> findDeniedPermissions(String[] permissions) {
          List<String> needRequestPermissonList = new ArrayList<String>();
          for (String perm : permissions) {
              if (ContextCompat.checkSelfPermission(this,
                      perm) != PackageManager.PERMISSION_GRANTED
                      || ActivityCompat.shouldShowRequestPermissionRationale(
                      this, perm)) {
                  needRequestPermissonList.add(perm);
              }
          }
          return needRequestPermissonList;
      }
  
      /**
       * 检测是否说有的权限都已经授权
       *
       * @param grantResults
       * @return
       * @since 2.5.0
       */
      private boolean verifyPermissions(int[] grantResults) {
          for (int result : grantResults) {
              if (result != PackageManager.PERMISSION_GRANTED) {
                  return false;
              }
          }
          return true;
      }
  
      @Override
      public void onRequestPermissionsResult(int requestCode,
                                             String[] permissions, int[] paramArrayOfInt) {
          if (requestCode == PERMISSON_REQUESTCODE) {
              if (!verifyPermissions(paramArrayOfInt)) {
                  showMissingPermissionDialog();
                  isNeedCheck = false;
              }else{
                  checkInstallProcess();
              }
          }
      }
  
      /**
       * 显示提示信息
       *
       * @since 2.5.0
       */
      private void showMissingPermissionDialog() {
          AlertDialog.Builder builder = new AlertDialog.Builder(this);
          builder.setTitle(R.string.notifyTitle);
          builder.setMessage(R.string.notifyMsg);
  
          // 拒绝, 退出应用
          builder.setNegativeButton(R.string.cancel,
                  new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int which) {
                          finish();
                      }
                  });
  
          builder.setPositiveButton(R.string.setting,
                  new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int which) {
                          startAppSettings();
                      }
                  });
  
          builder.setCancelable(false);
  
          builder.show();
      }
  
      /**
       * 启动应用的设置
       *
       * @since 2.5.0
       */
      private void startAppSettings() {
          Intent intent = new Intent(
                  Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
          intent.setData(Uri.parse("package:" + getPackageName()));
          startActivity(intent);
          isNeedCheck = true;
      }
  
  
      //检查是否有8.0未知来源安装权限
      private void checkInstallProcess() {
          boolean haveInstallPermission;
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
              //先获取是否有安装未知来源应用的权限
              haveInstallPermission = getPackageManager().canRequestPackageInstalls();
              if (!haveInstallPermission) {//没有权限
                  AlertDialog.Builder alert = new AlertDialog.Builder(this);
                  alert.setTitle("更新提示").setMessage("安装应用需要打开未知来源权限，请去设置中开启权限").setPositiveButton("确定",
                          new DialogInterface.OnClickListener() {
  
                              @Override
                              public void onClick(DialogInterface dialog, int which) {
                                  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                                      startInstallPermissionSettingActivity();
                                  }
                              }
                          }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int which) {
                          Toast.makeText(TranslucentActivity.this, "没有权限,已停止更新", Toast.LENGTH_LONG).show();
                          if(VersionUpdateConfig.getInstance().getStrongUpdate()==1){
                              //直接退出app
                              android.os.Process.killProcess(android.os.Process.myPid());// 杀死该应用进程
                              System.exit(0);
                          }else {
                              finish();
                          }
                      }
                  });
                  alert.create();
                  alert.show();
                  return;
              }
          }
          //权限已授权 开始下载
          VersionUpdateConfig.getInstance().passCheck();
          finish();
      }
  
      @RequiresApi(api = Build.VERSION_CODES.O)
      private void startInstallPermissionSettingActivity() {
          Uri packageURI = Uri.parse("package:" + getPackageName());
          //注意这个是8.0新API
          Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, packageURI);
          startActivityForResult(intent, InstallPermission);
      }
  
      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
          if(requestCode==InstallPermission){//未知来源app安装权限
              if(resultCode == RESULT_OK){
                  //获取未知来源apk安装权限
                  VersionUpdateConfig.getInstance().passCheck();
                  finish();
              }else{
                  Toast.makeText(TranslucentActivity.this, "没有权限,已停止更新", Toast.LENGTH_LONG).show();
                  if(VersionUpdateConfig.getInstance().getStrongUpdate()==1){
                      //直接退出app
                      finish();
                      System.exit(0);
                  }else {
                      finish();
                  }
              }
          }
      }
  
      @Override
      public boolean onKeyDown(int keyCode, KeyEvent event) {
          if (keyCode == KeyEvent.KEYCODE_BACK) {
              this.finish();
              return true;
          }
          return super.onKeyDown(keyCode, event);
      }
  }
  