Giter Site home page Giter Site logo

nachiketavadera / flashlight Goto Github PK

View Code? Open in Web Editor NEW
13.0 1.0 20.0 1.1 MB

An android application that makes using LED as a torch easier.

License: Apache License 2.0

Java 100.00%
android java android-application flashlight-application torch sensor shake-detection vibration toggle free

flashlight's Introduction

Flashlight

Project Logo

Open Source Love PRs Welcome License

Summary

The base of this project is LED provided along with the Camera in a typical Android smart phone. After seeing that Play Store has a number of apps that ask for unnecessary permissions, I decided to create an app that toggles the LED without asking for any type of permission. This app also implements a service that detects when the device is shook (used to toggle the flashlight).

As of June 24th of 2018, Flashlight is licensed under Apache License version 2.0.

Downloads

Latest apk: Flashlight.apk

Source Code (zip): Flashlight.zip

Source Code (tar.gz): Flashlight.tar.gz

Contributors

Code

The majority of the code is written in Java and is simple. For detecting shake motion: Declare global variables:

    SensorManager sensorManager = null;
    public static final float SHAKE_THRESHOLD = 10.25f; // How hard should user shake to invoke the service
    public static final int MIN_TIME_BETWEEN_SHAKES = 1000;
    private long lastShakeTime = 0;
    private boolean isFlashlightOn = false;

Initialize sensorManager:

    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    if (sensorManager != null) {
        Sensor accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sensorManager.registerListener((SensorEventListener) this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
    }

Make class implement SensorEventListener interface and override it's onSensorChanged method:

      @Override
    public void onSensorChanged(SensorEvent event) {
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            long curTime = System.currentTimeMillis();
            if ((curTime - lastShakeTime) > MIN_TIME_BETWEEN_SHAKES) {

                float x = event.values[0];
                float y = event.values[1];
                float z = event.values[2];

                double acceleration = Math.sqrt(Math.pow(x, 2) +
                        Math.pow(y, 2) +
                        Math.pow(z, 2)) - SensorManager.GRAVITY_EARTH;

                if (acceleration > SHAKE_THRESHOLD) {
                    lastShakeTime = curTime;
                    if (!isFlashlightOn) {
                        torchToggle("on");
                        isFlashlightOn = true;
                    }
                    else {
                        torchToggle("off");
                        isFlashlightOn = false;
                    }
                }
            }
        }

Create a function torchToggle(String command):

    private void torchToggle(String command) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            CameraManager camManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
            String cameraId = null; // Usually back camera is at 0 position.
            try {
                if (camManager != null) {
                    cameraId = camManager.getCameraIdList()[0];
                }
                if (camManager != null) {
                    if (command.equals("on")) {
                        camManager.setTorchMode(cameraId, true);   // Turn ON
                        isFlashlightOn = true;
                    } else {
                        camManager.setTorchMode(cameraId, false);  // Turn OFF
                        isFlashlightOn = false;
                    }
                }
            } catch (CameraAccessException e) {
                e.getMessage();
            }
        }
    }

Contributing

Any help, including feedback, is highly appriciated. I have just started out with Android and Iโ€™m relatively new to app development.

  1. Fork it!
  2. Create your feature branch: git checkout -b new-branch
  3. Commit your changes: git commit -am 'Make a valuable addition'
  4. Push to the branch: git push origin new-feature
  5. Submit a pull request :D

Next Step

Once staretd, an app can never be completely finished.

  1. Revamp the project and make it better.

flashlight's People

Contributors

dee-y avatar kant avatar rlf89 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

flashlight's Issues

Logo proposal

Hello, i am a graphic designer and i'd like to contribute a logo to your project, it's free of course.

Null Pointer Exception onSensorChanged

Describe the bug
Upon vibrating the phone app is crashing with the following exception!

Exception

FATAL EXCEPTION: main
Process: android.nachiketa.flashlight, PID: 13636
java.lang.NullPointerException
	at java.util.Objects.requireNonNull(Objects.java:203)
	at android.nachiketa.flashlight.Global.changeBackground(Global.java:34)
	at android.nachiketa.flashlight.Global.torchToggle(Global.java:55)
	at android.nachiketa.flashlight.FlashlightShakeToggle.onSensorChanged(FlashlightShakeToggle.java:67)
	at android.hardware.SystemSensorManager$SensorEventQueue.dispatchSensorEvent(SystemSensorManager.java:857)
	at android.os.MessageQueue.nativePollOnce(Native Method)
	at android.os.MessageQueue.next(MessageQueue.java:336)
	at android.os.Looper.loop(Looper.java:181)
	at android.app.ActivityThread.main(ActivityThread.java:7562)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)

Device details
Android version: 10
Device vendor: Xaomi
Device name: Redmi note 9s

add more gestures & details on power consumption

I haven't found a single word on how much extra energy that service would consume if left working in background.

As for 'more gestures' feature request, actually, this doesn't relate to the flashlight. I'd like to build a similar background service that would listen to 'triple wrist-twist' gestures clockwise and counter clock wise when the phone is locked to perform two different BLE requests, but I am not quite sure how to recognize such gestures (if possible at all)

add blinking light in order to look more feature

add this with UI in order to add blinking light . I am beginner so I don't know about mobile developement
public void toggle(View view)
{
Button button = (Button) view;
// mine coding for disco light
if(button.getText().equals("Switch On") )
{
int i=1000;
while (i>3)
{
if(i%80!=0)
{
//button.setText(R.string.switch_on_text);
torchToggle("off");
}
if(i%80==0)
{
// button.setText(R.string.switch_off_text);
torchToggle("on");
}
if (i==10000)
{
button.setText(R.string.switch_off_text);
torchToggle("off");
break;
}
i++;
}
}
// mine coding

}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.