Android Open Source - Swipelight_Flashlight Flashlight Widget Receiver






From Project

Back to project page Swipelight_Flashlight.

License

The source code is released under:

Apache License

If you think the Android project Swipelight_Flashlight listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.collinguarino.flashlight;
//from   ww w  .ja v  a  2  s.  c  o m
import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.hardware.Camera;
import android.widget.RemoteViews;
import android.widget.Toast;

public class FlashlightWidgetReceiver extends BroadcastReceiver {

    private static boolean isLightOn = false;
    private static Camera camera;

    @Override
    public void onReceive(Context context, Intent intent) {
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.homescreen_widget_layout);

        if (isLightOn) {
            views.setImageViewResource(R.id.imageView, R.drawable.swipelighticon); // off
        } else {
            views.setImageViewResource(R.id.imageView, R.drawable.swipelightinverted); // on
        }

        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        appWidgetManager.updateAppWidget(new ComponentName(context, HomeScreenWidgetProvider.class),
                views);

        if (isLightOn) {
            if (camera != null) {
                camera.stopPreview();
                camera.release();
                camera = null;
                isLightOn = false;
            }

        } else {

            camera = Camera.open();

            if(camera == null) {
                Toast.makeText(context, "No camera detected", Toast.LENGTH_SHORT).show();
            } else {
                // Set the torch flash mode
                Camera.Parameters param = camera.getParameters();
                param.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                try {
                    camera.setParameters(param);
                    camera.startPreview();
                    isLightOn = true;
                } catch (Exception e) {
                    Toast.makeText(context,"No LED detected", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

}




Java Source Code List

com.collinguarino.flashlight.FlashlightWidgetReceiver.java
com.collinguarino.flashlight.HomeScreenWidgetProvider.java
com.collinguarino.flashlight.LockFlashlightWidgetReceiver.java
com.collinguarino.flashlight.LockScreenWidgetProvider.java
com.collinguarino.flashlight.Main.java
com.collinguarino.flashlight.SwipelightDashClock.java
com.collinguarino.flashlight.WidgetProvider.java