Android Open Source - Simplest-Flashlight-App Flashlight Widget






From Project

Back to project page Simplest-Flashlight-App.

License

The source code is released under:

Apache License

If you think the Android project Simplest-Flashlight-App 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.andrewq.simpleflashlight;
//from   w  w  w  . j  a  va 2 s  .  co m
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.widget.RemoteViews;

public class FlashlightWidget extends AppWidgetProvider {

  final static String ACTION_SWITCH = "com.andrewq.simpleflashlight.SWITCH";
  final static String LOG_TAG = "test";
  static boolean isEnabled = false;
  static Camera mCamera;

  @Override
  public void onUpdate(Context context, AppWidgetManager appWidgetManager,
      int[] appWidgetIds) {
    super.onUpdate(context, appWidgetManager, appWidgetIds);

    for (int id : appWidgetIds) {
      updateWidget(context, appWidgetManager, id);
    }
  }

  private void updateWidget(Context context,
      AppWidgetManager appWidgetManager, int id) {

    RemoteViews widgetView = new RemoteViews(context.getPackageName(),
        R.layout.widget);
    if (isEnabled) {
      widgetView.setImageViewResource(R.id.ibSwitcher,
          R.drawable.green_light);
    } else {
      widgetView.setImageViewResource(R.id.ibSwitcher,
          R.drawable.red_light);
    }

    Intent intent = new Intent(context, FlashlightWidget.class);
    intent.setAction(ACTION_SWITCH);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, id);
    PendingIntent pIntent = PendingIntent.getBroadcast(context, id, intent,
        0);
    widgetView.setOnClickPendingIntent(R.id.ibSwitcher, pIntent);
    appWidgetManager.updateAppWidget(id, widgetView);
  }

  @Override
  public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);

    if (intent.getAction().equalsIgnoreCase(ACTION_SWITCH)) {
      isEnabled = !isEnabled;
      int id = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
          AppWidgetManager.INVALID_APPWIDGET_ID);
      updateWidget(context, AppWidgetManager.getInstance(context), id);
      switchFlash();
    }
  }

  private void switchFlash() {
    if (isEnabled) {
      if (mCamera == null) {
        mCamera = Camera.open();
      }
      Parameters params = mCamera.getParameters();
      params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
      mCamera.setParameters(params);
      mCamera.startPreview();
    } else if (mCamera != null) {
      mCamera.stopPreview();
      mCamera.release();
      mCamera = null;
    }
  }

}




Java Source Code List

com.andrewq.simpleflashlight.FlashlightWidget.java
com.andrewq.simpleflashlight.MainActivity.java
com.andrewq.simpleflashlight.OnSwipeTouchListener.java
com.andrewq.simpleflashlight.Settings.java