Java tutorial
/* * Copyright (c) 2016. Vv <envyfan@qq.com><http://www.v-sounds.com/> * * This file is part of AndroidReview (Android??) * * AndroidReview is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * AndroidReview is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with AndroidReview. If not, see <http://www.gnu.org/licenses/>. */ package com.vv.androidreview.ui.activites; import android.app.Activity; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import android.os.Bundle; import android.provider.Settings; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.ActivityCompat; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import com.vv.androidreview.R; import com.vv.androidreview.utils.PermissionsChecker; /** * ???? * <p/> * Created by wangchenlong on 16/1/26. */ public class PermissionsActivity extends AppCompatActivity { public static final int PERMISSIONS_GRANTED = 0; // ??? public static final int PERMISSIONS_DENIED = 1; // ??? private static final int PERMISSION_REQUEST_CODE = 0; // ????? private static final String EXTRA_PERMISSIONS = "extra_permission"; // ??? private static final String PACKAGE_URL_SCHEME = "package:"; // private PermissionsChecker mChecker; // ?? private boolean isRequireCheck; // ????, ???? // ?????? public static void startActivityForResult(Activity activity, int requestCode, String... permissions) { Intent intent = new Intent(activity, PermissionsActivity.class); intent.putExtra(EXTRA_PERMISSIONS, permissions); ActivityCompat.startActivityForResult(activity, intent, requestCode, null); } @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getIntent() == null || !getIntent().hasExtra(EXTRA_PERMISSIONS)) { throw new RuntimeException("PermissionsActivity???startActivityForResult?!"); } View view = new View(this); view.setBackgroundColor(getResources().getColor(R.color.theme_color_level2)); ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); view.setLayoutParams(lp); setContentView(view); mChecker = new PermissionsChecker(this); isRequireCheck = true; } @Override protected void onResume() { super.onResume(); if (isRequireCheck) { String[] permissions = getPermissions(); if (mChecker.lacksPermissions(permissions)) { requestPermissions(permissions); // ?? } else { allPermissionsGranted(); // ??? } } else { isRequireCheck = true; } } // ??? private String[] getPermissions() { return getIntent().getStringArrayExtra(EXTRA_PERMISSIONS); } // ?? private void requestPermissions(String... permissions) { ActivityCompat.requestPermissions(this, permissions, PERMISSION_REQUEST_CODE); } // ???? private void allPermissionsGranted() { setResult(PERMISSIONS_GRANTED); finish(); } /** * ???, * ?, . * ??, ??Dialog. * * @param requestCode ? * @param permissions ?? * @param grantResults */ @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == PERMISSION_REQUEST_CODE && hasAllPermissionsGranted(grantResults)) { isRequireCheck = true; allPermissionsGranted(); } else { isRequireCheck = false; showMissingPermissionDialog(); } } // ??? private boolean hasAllPermissionsGranted(@NonNull int[] grantResults) { for (int grantResult : grantResults) { if (grantResult == PackageManager.PERMISSION_DENIED) { return false; } } return true; } // ???? private void showMissingPermissionDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(PermissionsActivity.this); builder.setTitle(R.string.help); builder.setMessage(R.string.string_help_text); // ?, builder.setNegativeButton(R.string.quit, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { setResult(PERMISSIONS_DENIED); finish(); } }); builder.setPositiveButton(R.string.settings, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { startAppSettings(); } }); builder.setCancelable(false); builder.show(); } // ? private void startAppSettings() { Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); intent.setData(Uri.parse(PACKAGE_URL_SCHEME + getPackageName())); startActivity(intent); } }