MayI


Source link: https://github.com/ThanosFisherman/MayI

MayI

MayI is yet another library that simplifies the process of requesting permissions at runtime for devices that run Android Marshmallow and above.

As of Androids Marshmallow and above a new functionality has been added that lets users grant or deny permissions while an app is running instead of granting them all together when installing it. This approach gives the user more control over applications but requires developers to add lots of code to support it.

This library aims to reduce boilerplate code needed to request permissions at runtime by featuring a simple chainable API designed the way I want it.

Screenshot

Usage

Single Permission

To request a single permission using this library, you just need to call Mayi with a valid Activity and use withPermission method:

public MainActivity extends AppCompatActivity  {
  @Override
public void onCreate()
{

super.onCreate();

Mayi.withActivity(MainActivity.this)

 .withPermission(Manifest.permission.READ_CONTACTS)

 .onResult(permission -> permissionResultSingle(permission))

 .onRationale((permission, token) -> permissionRationaleSingle(permission,token))

 .check()  
}
 
}

permissionResultSingle and permissionRationaleSingle could be custom-defined methods of your own that would deal accordingly in each situation. For Example:

 private void permissionResultSingle(PermissionBean permission)  {

Toast.makeText(MainActivity.this, "PERMISSION RESULT " + permission.toString(), Toast.LENGTH_LONG).show();
  
}

 private void permissionRationaleSingle(PermissionBean bean, PermissionToken token)  {

Toast.makeText(MainActivity.this, "Should show rationale for " + bean.getSimpleName() + " permission", Toast.LENGTH_LONG).show();

token.skipPermissionRequest();

  
}

Multiple Permissions

Similarly to request multiple permissions at once, you just need to call Mayi with a valid Activity but this time use withPermissions method to specify more than one permissions. Furthermore the lambda expressions from the example above could be replaced with method references like so:

public MainActivity extends AppCompatActivity  {
  @Override
public void onCreate()
{

super.onCreate();

Mayi.withActivity(this)

 .withPermissions(Manifest.permission.READ_CONTACTS, Manifest.permission.ACCESS_FINE_LOCATION)

 .onRationale(this::permissionRationaleMulti)

 .onResult(this::permissionResultMulti)

 .check()  
}
 
}

Again possible custom-defined methods for the above example could be something like:

private void permissionResultMulti(PermissionBean[] permissions) {

  Toast.makeText(MainActivity.this, "MULTI PERMISSION RESULT " + Arrays.deepToString(permissions), Toast.LENGTH_LONG).show();
 
}
  private void permissionRationaleMulti(PermissionBean[] permissions, PermissionToken token) {

  Toast.makeText(MainActivity.this, "Rationales for Multiple Permissions " + Arrays.deepToString(permissions), Toast.LENGTH_LONG).show();

  token.continuePermissionRequest();
 
}

Error handling

If you think there is going to be an error in your Mayi integration, just call a onErrorListener:

 Mayi.withActivity(this)
  .withPermissions(Manifest.permission.READ_CONTACTS, Manifest.permission.ACCESS_FINE_LOCATION)
  .onRationale(this::permissionRationaleMulti)
  .onResult(this::permissionResultMulti)
  .onErrorListener(this::inCaseOfError)
  .check());

private void inCaseOfError(Exception e) {

  Toast.makeText(MainActivity.this, "ERROR " + e.toString(), Toast.LENGTH_SHORT).show();
 
}

The library will then notify you when something unexpected happens.

Add it to your project

Add the following to your app module build.gradle file

dependencies {

 compile 'com.thanosfisherman.mayi:mayi:1.4' 
}

Library Flow

  • The first time this library runs, system permission promt will appear asking for the user to either deny or allow the permission. Next onResult() method will be called that includes the result of the user's choice.
  • If user denied the permission the first time (but didn't check "don't ask again" option) then onRationale will be the first method to be called next time this library runs. Inside onRationale method you now have 3 options.
    • Call token.continuePermissionRequest() method which shows again system dialog prompt and then calls onResult() that includes the user's choice.
    • Call token.skipPermissionRequest() method which will skip showing system dialog prompt and immediately call onResult() that includes the user's choice.
    • Call none of the 2 above thus terminating the flow after onRationale finishes its execution.
  • If user denied the permission by checking "don't ask again" then onResult() will be called that includes the result of the user's choice.

below is a flow chart that visualizes the library's flow described above.

Contributing?

Feel free to add/correct/fix something to this library, I will be glad to improve it with your help.

License

Copyright 2017 Thanos Psaridis  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

Easy SharedPreference Engine foR ANDROid.

This is a simple Android utils library to write any type of data into cache files and then read them later, using Gson to serialize and deserialize these data.

An Android Library for the type-safe creation of Share Intents.

Library for Emoji View like Hangouts, Emoji TextView and Emoji EditText.

VisibilityPercentageLayout is a layout (extends RelativeLayout) with a custom listener which gives the visibility percentage of the layout every time it is changed.Pixel visibility listener is also available.

AndroidAppTranslate translates an Android application resource, easy and fast!

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