Download File
Download Project
Settings
Line Wrap
Themes
default
ambiance
bespin
dracula
eclipse
material
mbo
mdn-like
neat
solarized dark
ttcn
zenburn
MainActivity.java
/* * Copyright Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package pub.devrel.easypermissions.sample; import android.Manifest; import android.content.Intent; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Toast; import java.util.List; import pub.devrel.easypermissions.AfterPermissionGranted; import pub.devrel.easypermissions.AppSettingsDialog; import pub.devrel.easypermissions.EasyPermissions; public class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks { private static final String TAG = "MainActivity"; private static final String[] LOCATION_AND_CONTACTS = {Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.READ_CONTACTS}; private static final int RC_CAMERA_PERM = 123; private static final int RC_LOCATION_CONTACTS_PERM = 124; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Button click listener that will request one permission. findViewById(R.id.button_camera).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { cameraTask(); } }); // Button click listener that will request two permissions. findViewById(R.id.button_location_and_contacts).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { locationAndContactsTask(); } }); } private boolean hasCameraPermission() { return EasyPermissions.hasPermissions(this, Manifest.permission.CAMERA); } private boolean hasLocationAndContactsPermissions() { return EasyPermissions.hasPermissions(this, LOCATION_AND_CONTACTS); } private boolean hasSmsPermission() { return EasyPermissions.hasPermissions(this, Manifest.permission.READ_SMS); } @AfterPermissionGranted(RC_CAMERA_PERM) public void cameraTask() { if (hasCameraPermission()) { // Have permission, do the thing! Toast.makeText(this, "TODO: Camera things", Toast.LENGTH_LONG).show(); } else { // Ask for one permission EasyPermissions.requestPermissions( this, getString(R.string.rationale_camera), RC_CAMERA_PERM, Manifest.permission.CAMERA); } } @AfterPermissionGranted(RC_LOCATION_CONTACTS_PERM) public void locationAndContactsTask() { if (hasLocationAndContactsPermissions()) { // Have permissions, do the thing! Toast.makeText(this, "TODO: Location and Contacts things", Toast.LENGTH_LONG).show(); } else { // Ask for both permissions EasyPermissions.requestPermissions( this, getString(R.string.rationale_location_contacts), RC_LOCATION_CONTACTS_PERM, LOCATION_AND_CONTACTS); } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); // EasyPermissions handles the request result. EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this); } @Override public void onPermissionsGranted(int requestCode, List
perms) { Log.d(TAG, "onPermissionsGranted:" + requestCode + ":" + perms.size()); } @Override public void onPermissionsDenied(int requestCode, List
perms) { Log.d(TAG, "onPermissionsDenied:" + requestCode + ":" + perms.size()); // (Optional) Check whether the user denied any permissions and checked "NEVER ASK AGAIN." // This will display a dialog directing them to enable the permission in app settings. if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) { new AppSettingsDialog.Builder(this).build().show(); } } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == AppSettingsDialog.DEFAULT_SETTINGS_REQ_CODE) { String yes = getString(R.string.yes); String no = getString(R.string.no); // Do something after user returned from app settings screen, like showing a Toast. Toast.makeText( this, getString(R.string.returned_from_app_settings_to_activity, hasCameraPermission() ? yes : no, hasLocationAndContactsPermissions() ? yes : no, hasSmsPermission() ? yes : no), Toast.LENGTH_LONG) .show(); } } }