Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/nisrulz/lantern

🏮[Android Library] Handling device flash as torch for Android.
https://github.com/nisrulz/lantern

android android-library display flashlight screen torch

Last synced: 3 months ago
JSON representation

🏮[Android Library] Handling device flash as torch for Android.

Awesome Lists containing this project

README

        



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
```gradle
implementation "com.github.nisrulz:lantern:{latest version}"
```
where `{latest version}` corresponds to published version in [ ![Download](https://api.bintray.com/packages/nisrulz/maven/com.github.nisrulz%3Alantern/images/download.svg) ](https://bintray.com/nisrulz/maven/com.github.nisrulz%3Alantern/_latestVersion)

# 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

```xml




```

1. Init code.

```java
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.
}
}
}
```
1. 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.

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

+ Turn On

```java
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

```java
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](https://github.com/nisrulz/AndroidCodeStyle/tree/nishant-config), 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](https://github.com/nisrulz/lantern/issues) first, before filing an issue.
6. Have fun!

## License
Licensed under the Apache License, Version 2.0, [click here for the full license](/LICENSE.txt).

## Author & support
This project was created by [Nishant Srivastava](https://github.com/nisrulz/nisrulz.github.io#nishant-srivastava) but hopefully developed and maintained by many others. See the [the list of contributors here](https://github.com/nisrulz/lantern/graphs/contributors).

> If you appreciate my work, consider buying me a cup of :coffee: to keep me recharged :metal:
> + [PayPal](https://www.paypal.me/nisrulz/5usd)
> + Bitcoin Address: 13PjuJcfVW2Ad81fawqwLtku4bZLv1AxCL
>
> Donation to the project is always welcome which helps to maintain and keep [my open source projects](https://github.com/nisrulz/) up to date!