### Requesting multiple Android permissions with ActivityCompat Source: https://github.com/anthonycr/grant/blob/master/README.md This example shows how to initiate a request for one or more Android permissions using `ActivityCompat.requestPermissions`. It highlights the necessity of providing a unique request code to identify the origin of the request in the subsequent callback, and an array of permissions to be requested. ```Java // You need a request code so you know where you made the request from private static final int WRITE_EXTERNAL_STORAGE_TASK_CODE = 1; . . . Activity activity = this; String[] permissionsToRequest = new String[] { Manifest.Permisions.WRITE_EXTERNAL_STORAGE }; ActivityCompat.requestPermissions(activity, String[] permissionsToRequest, WRITE_EXTERNAL_STORAGE_TASK_CODE); ``` -------------------------------- ### Request Specific Android Permissions with Callback Source: https://github.com/anthonycr/grant/blob/master/README.md This example shows how to request specific Android permissions (e.g., `WRITE_EXTERNAL_STORAGE`) using the `PermissionsManager`. It wraps the permission-dependent logic in a `PermissionsResultAction` callback, executing `onGranted()` if all permissions are granted or `onDenied()` if any are denied, simplifying permission handling. ```Java PermissionsManager.getInstance().requestPermissionsIfNecessaryForResult(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, new PermissionsResultAction() { @Override public void onGranted() { writeToStorage(); } @Override public void onDenied(String permission) { Toast.makeText(MainActivity.this, "Sorry, we need the Storage Permission to do that", Toast.LENGTH_SHORT).show(); } }); ``` -------------------------------- ### Core Classes of Android Permissions Library Source: https://github.com/anthonycr/grant/blob/master/README.md Overview of the main classes provided by the 'anthonycr/grant' permissions management library, including their purpose and key functionalities. ```APIDOC PermissionsManager: Description: This is a singleton class that handles all the permissions requests, logic, and callbacks. PermissionsResultAction: Description: This is a callback that you use to execute code when the permissions you have requested are either granted or denied. Methods: onGranted(): Called when all requested permissions are granted. onDenied(permission: String): Called when a permission is denied. permission: The specific permission string that was denied. ``` -------------------------------- ### Request All Manifest Permissions Globally Source: https://github.com/anthonycr/grant/blob/master/README.md This snippet demonstrates how to request all permissions declared in the application's manifest upfront using `PermissionsManager`. While not generally recommended for user experience, it provides a way to prompt for all necessary permissions at once, with callbacks for granted or denied states. ```Java PermissionsManager.getInstance().requestAllManifestPermissionsIfNecessary(this, new PermissionsResultAction() { @Override public void onGranted() { // Proceed with initialization } @Override public void onDenied(String permission) { // Notify the user that you need all of the permissions } }); ``` -------------------------------- ### Apache License Version 2.0 Source: https://github.com/anthonycr/grant/blob/master/README.md This snippet contains the full text of the Apache License, Version 2.0, governing the use and distribution of the AnthonyCR/Grant project. It specifies the terms under which the software can be used, modified, and distributed, including limitations on liability and warranty disclaimers. ```text Copyright 2015 Anthony Restaino 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. ``` -------------------------------- ### Handling Android permission request results in onRequestPermissionsResult Source: https://github.com/anthonycr/grant/blob/master/README.md This snippet illustrates the complexity involved in processing the results of permission requests within the `onRequestPermissionsResult` callback. Developers must check array lengths, verify specific permission grants, and match the `requestCode` to the original request to execute the correct application logic, leading to convoluted code. ```Java @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { // check array lengths // check that the storage permission was granted // check that requestCode == WRITE_EXTERNAL_STORAGE_REQUEST_CODE because // maybe the request was made from somewhere else and you shouldn't execute // your code in that case. // blah blah execute your code or maybe other code, who knows. } ``` -------------------------------- ### Checking a single Android permission using ActivityCompat Source: https://github.com/anthonycr/grant/blob/master/README.md This snippet demonstrates how to check if a specific permission, such as `WRITE_EXTERNAL_STORAGE`, has been granted to an Android application. It uses `ActivityCompat.checkSelfPermission` and requires conditional logic to determine whether to proceed with code execution or request the permission. ```Java Activity activity = this; String permission = Manifest.Permisions.WRITE_EXTERNAL_STORAGE; if(ActivityCompat.checkSelfPermission(activity, permission) == PackageManager.PERMISSION_GRANTED) { // Proceed with your code execution } else { // Uhhh I guess we have to ask for permission } ``` -------------------------------- ### Handle Activity Permissions Result Callback in Android Source: https://github.com/anthonycr/grant/blob/master/README.md This snippet demonstrates how to override the `onRequestPermissionsResult` method in an Android Activity to notify the `PermissionsManager` of permission changes. This is crucial for the library to correctly process permission grants or denials for API 23 and above. ```Java @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { PermissionsManager.getInstance().notifyPermissionsChange(permissions, grantResults); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.