Lantern

Additional

Language
Java
Version
2.0.0 (Mar 17, 2018)
Created
May 28, 2017
Updated
Mar 17, 2018
Owner
Nishant Srivastava (nisrulz)
Contributor
Nishant Srivastava (nisrulz)
1
Activity
Badge
Generate
Download
Source code
APK file

Advertisement

Lantern 

Android library handling flashlight for camera and camera2 api. Added support for handling display/screen light.

Built with ❤︎ by Nishant Srivastava and contributors


Including in your project

Lantern is available in the Jcenter, so getting it as simple as adding it as a dependency

implementation "com.github.nisrulz:lantern:{latest version}"

where {latest version} corresponds to published version in

Usage

Lantern uses a fluent api. You can enable/disable feature by calling the right method on the Lantern object. Use what you need!

  1. Declare permissions in your app's AndroidManifest.xml file

    <!-- Permissions : Allows access to flashlight -->
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.FLASHLIGHT"/>
    
    <!-- Permissions : Allows access to change system settings for handling screen states -->
    <uses-permission android:name="android.permission.WRITE_SETTINGS"/>
  2. Init code.

    private Lantern lantern = new Lantern(this)
                                // Check and request for system permission, used for handling screen states
                                .checkAndRequestSystemPermission()
                                // OPTIONAL: Setup Lantern to observe the lifecycle of the activity/fragment, handles auto-calling cleanup() method
                                .observeLifecycle(this);
    
    // Init Lantern's torch feature by calling `initTorch()`, which also check if camera permission is granted + camera feature exists
    // In case permission is not granted, request for the permission and retry by calling `initTorch()` method
    // NOTE: In case camera feature/hardware does not exist, `initTorch()` will return `false` and Lantern will not have
    // torch functionality but only screen based features
    if (!lantern.initTorch()) {
        // Request camera permission if it is not granted
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, REQUEST_CODE);
    }
    
    
    // Handle the runtime permission
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
            @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    
        if (requestCode == REQUEST_CODE) {
            // Retry initializing the Lantern's torch feature
            if (!lantern.initTorch()) {
                // Camera Permission Denied! Do something.
            }
        }
    }
  3. Cleanup

    If you are not observing the lifecyle of the activity/fragment via observeLifecycle(this) method, then you need to call cleanup() method in onDestroy() of the Activity.

    @Override
    protected void onDestroy() {
        lantern.cleanup();
        super.onDestroy();
    }
  4. Manage states via

    • Turn On

      lantern
          // Enable always on display
          .alwaysOnDisplay(true)
          // Set screen to full bright
          .fullBrightDisplay(true)
          // Or set screen to Auto Bright
          .autoBright(true)
          // Enable torch via flash
          .enableTorchMode(true)
          // Enable pulsating torch
          .pulse(true)
          // Set the delay for between each pulse
          .withDelay(1, TimeUnit.SECONDS);
    • Turn Off

      lantern
          // Disable always on display
          .alwaysOnDisplay(false)
          // Unset full bright screen state
          .fullBrightDisplay(false)
          // Or unset screen from Auto Bright
          .autoBright(false)
          // Disable torch via flash
          .enableTorchMode(false)
          // Disable pulsating torch
          .pulse(false);

Pull Requests

I welcome and encourage all pull requests. It usually will take me within 24-48 hours to respond to any issue or request. Here are some basic rules to follow to ensure timely addition of your request:

  1. Match coding style (braces, spacing, etc.) This is best achieved using CMD+Option+L (Reformat code) on Mac (not sure for Windows) with Android Studio defaults. This project uses a modified version of Grandcentrix's code style, so please use the same when editing this project.
  2. If its a feature, bugfix, or anything please only change code to what you specify.
  3. Please keep PR titles easy to read and descriptive of changes, this will make them easier to merge :)
  4. Pull requests must be made against develop branch. Any other branch (unless specified by the maintainers) will get rejected.
  5. Check for existing issues first, before filing an issue.
  6. Have fun!

License

Licensed under the Apache License, Version 2.0, click here for the full license.

Author & support

This project was created by Nishant Srivastava but hopefully developed and maintained by many others. See the the list of contributors here.

If you appreciate my work, consider buying me a cup of ☕️ to keep me recharged 🤘

  • PayPal
  • Bitcoin Address: 13PjuJcfVW2Ad81fawqwLtku4bZLv1AxCL

Donation to the project is always welcome which helps to maintain and keep my open source projects up to date!