AllowMe


Source link: https://github.com/aitorvs/allowme

DEPRECATED: I found a much better and elegant solution that I am now using instead.

AllowMe

AllowMe simple library to handle Android M permissions easily.

Gradle Dependency

Repository

repositories {

  maven {
 url "https://jitpack.io" 
}
 
}

Dependency

dependencies {

// ... other dependencies here

compile 'com.github.aitorvs:allowme:0.3.2' 
}

--

Basics

The library simplifies the task of requesting permissions for Android M. Just one call and one callback are enough.

Usage

  1. Extend your activity from AllowMeActivity (which extends AppCompatActivity)
public class MyPermissionsActivity extends AllowMeActivity {

 //... more code here
  
}
  1. Request the permission and register the callback for handling. All in one call

  //... more code here

if (!AllowMe.isPermissionGranted(permission)) {

 new AllowMe.Builder()

.setPermissions(permission)

.setRationale(rationale)

.setPrimingMessage(primingMessage)

.setRationaleThemeId(themeId)

.setCallback(new AllowMeCallback() {

 @Override

 public void onPermissionResult(int requestCode, PermissionResultSet result) {

  if (result.isGranted(permission)) {

//... permission is granted, handle here

  
}

 
}

}
).request(requestCode);

}
 else {

 //... handle permission already granted

}
  • permission is the permission you need to request
  • rationale is optional and it is the string message to show when the user denies the permission for the first time
  • primingMessage is optional and sets the permission priming message dialog that is showed prior to request the permission. In some cases where the dialog requesting the permission appears without any further notice and without enough context, permission priming will help you to put the user in context.
  • themeId is optional and allows to style the rationale alert dialog
  • requestCode is an integer to identify the request

The library makes sure that onPermissionResult is only called when the requestCode matches the user input request code. Anyway, the requestCode is also returned in the callback params so that it can also be checked against the original request code.

And that's all it takes.

It is also possible to request the permissions inside fragments, just extend the parent activity from AllowMeActivity and steps above remain the same.

Priming vs rationale

The permission rationale dialog appears once the user has denied the permission perviously. It is a way to explain the user why we need the permission. But sometimes the first permission request appears with little or no context and, it is then when priming is important. Priming message dialog, when defined, will appear in the first permission request helping to put the user in context as to why the permission is required.

If the user decides not to go ahead after reading the priming message, the permission will not be requested again and the user will need to go to device settings to grant it.

Use annotated methods instead of callbacks

It is possible to, instead of using callback methods, use annotations to define the method to be called when the permission request is performed.


public class MyActivity extends AllowMeActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

 super.onCreate(savedInstanceState);

  //... more code here

  int requestCode = 69;

 if (!AllowMe.isPermissionGranted(Manifest.permission.READ_CONTACTS)) {

  new AllowMe.Builder()

 .setPermissions(Manifest.permission.READ_CONTACTS)

 .setRationale(rationale)

 .setPrimingMessage(primingMessage)

 .setRationaleThemeId(themeId)

 .requestPermissionForResult(MyActivity.this, requestCode);

 
}
 else {

  //... handle permission already granted

 
}

}

  @OnPermissionResult(requestedPermissions = {
Manifest.permission.READ_CONTACTS
}
)

void permissionRequestHandler(int requestCode, PermissionResultSet result) {

 //... handle result

}

  
}

The annotation OnPermissionResult receives the list of requested permissions in requestedPermissions param.

To use the annotated method, a new requestPermissionForResult call should be used. This call receives two params:

  • Object with class the annotated method belongs to
  • int with the permission request code

The library will then call the annotated method to handle the permission request result.

Developed By

Aitor Viana Sanchez - [email protected]>

License

Apache 2.0

Copyright 2015 aitorvs, Inc.  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. 

Resources

Yet another MVP library. Super simple, but with enough functionality.

A view to help developers and designers view the View's font size, color, and border.

Only by one line code,and all TextView、Button、ImageView and others support view will show text border、color attr in top of View like gif demo.

A vertical slider view for switching to the next fragment page.

Annotate methods to use as listeners for a sensor.

With this library you can implement RecyclerView.Adapter in a few lines.

Library for data transmission via sound waves, to facilitate the implementation of communication technology for sound waves by the programmers. This library prototype specifically makes the transformation and transmission of certain information into sound waves in the application that it is implemented and also makes the receipt of the same information transmitted.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes