|
@@ -1,6 +1,5 @@
|
|
|
package me.yoqi.flashlight;
|
|
|
|
|
|
-
|
|
|
import java.io.IOException;
|
|
|
import java.util.List;
|
|
|
|
|
@@ -20,7 +19,7 @@ import android.widget.Button;
|
|
|
import android.widget.RemoteViews;
|
|
|
import me.yoqi.flashlight.utils.DroidLED;
|
|
|
|
|
|
-public class MainActivity extends Activity implements SurfaceHolder.Callback {
|
|
|
+public class MainActivity extends Activity implements SurfaceHolder.Callback {
|
|
|
|
|
|
int count = 0;
|
|
|
static Camera mCamera;
|
|
@@ -42,7 +41,7 @@ public class MainActivity extends Activity implements SurfaceHolder.Callback {
|
|
|
setWidgetTo(R.drawable.light_on);
|
|
|
}
|
|
|
|
|
|
- if (getmCamera() != null) {
|
|
|
+ if (mCamera != null) {
|
|
|
try {
|
|
|
flashOffApp();
|
|
|
} catch (Exception e) {
|
|
@@ -57,7 +56,7 @@ public class MainActivity extends Activity implements SurfaceHolder.Callback {
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
- setmCameraActivity(Camera.open());
|
|
|
+ mCamera = Camera.open();
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
@@ -85,7 +84,8 @@ public class MainActivity extends Activity implements SurfaceHolder.Callback {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /* 返回键关闭
|
|
|
+ /*
|
|
|
+ * 返回键关闭
|
|
|
*/
|
|
|
@Override
|
|
|
public void onBackPressed() {
|
|
@@ -93,9 +93,6 @@ public class MainActivity extends Activity implements SurfaceHolder.Callback {
|
|
|
processOffClick();
|
|
|
}
|
|
|
|
|
|
- // When the app resumes turn the LED off. This is a design decision. I think
|
|
|
- // the user will likely want to open the app when it is on only to turn it
|
|
|
- // off.
|
|
|
@Override
|
|
|
protected void onResume() {
|
|
|
super.onResume();
|
|
@@ -108,7 +105,7 @@ public class MainActivity extends Activity implements SurfaceHolder.Callback {
|
|
|
|
|
|
}
|
|
|
|
|
|
- if (getmCamera() != null) {
|
|
|
+ if (mCamera != null) {
|
|
|
|
|
|
try {
|
|
|
flashOffApp();
|
|
@@ -133,25 +130,25 @@ public class MainActivity extends Activity implements SurfaceHolder.Callback {
|
|
|
|
|
|
}
|
|
|
|
|
|
- // Any time the widget or the button in the app is pressed to turn the LED
|
|
|
- // on we process this off click. On method that is deprecated is needed for
|
|
|
- // earlier than Android 3.0 devices.
|
|
|
+ /**
|
|
|
+ * 开
|
|
|
+ */
|
|
|
@SuppressWarnings("deprecation")
|
|
|
private void processOnClick() {
|
|
|
|
|
|
flashlight_button.setBackgroundResource(R.drawable.light_off);
|
|
|
setWidgetTo(R.drawable.light_off);
|
|
|
|
|
|
- if (getmCamera() == null) {
|
|
|
+ if (mCamera == null) {
|
|
|
try {
|
|
|
mHolder.addCallback(this);
|
|
|
- if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
|
|
|
+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {//<11
|
|
|
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
|
|
|
}
|
|
|
- setmCameraActivity(Camera.open());
|
|
|
+ mCamera = Camera.open();
|
|
|
try {
|
|
|
if (mHolder != null) {
|
|
|
- getmCamera().setPreviewDisplay(mHolder);
|
|
|
+ mCamera.setPreviewDisplay(mHolder);
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
@@ -161,35 +158,55 @@ public class MainActivity extends Activity implements SurfaceHolder.Callback {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if (getmCamera() != null) {
|
|
|
+ if (mCamera != null) {
|
|
|
flashOnApp();
|
|
|
}
|
|
|
|
|
|
if (FlashlightWidgetReceiver.getmCameraWidget() != null) {
|
|
|
flashOnWidget();
|
|
|
}
|
|
|
-
|
|
|
turnMotorolaOn();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 关
|
|
|
+ */
|
|
|
+ private void processOffClick() {
|
|
|
+
|
|
|
+ flashlight_button.setBackgroundResource(R.drawable.light_on);
|
|
|
+ setWidgetTo(R.drawable.light_on);
|
|
|
+
|
|
|
+ if (mCamera != null) {
|
|
|
+ count = 0;
|
|
|
+ flashOffApp();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (FlashlightWidgetReceiver.getmCameraWidget() != null) {
|
|
|
+ count = 0;
|
|
|
+ flashOffWidget();
|
|
|
+ }
|
|
|
+
|
|
|
+ turnMotorolaOff();
|
|
|
|
|
|
}
|
|
|
|
|
|
private void flashOnApp() {
|
|
|
- setParams(getmCamera().getParameters());
|
|
|
+ this.params = mCamera.getParameters();
|
|
|
|
|
|
- List<String> flashModes = getParams().getSupportedFlashModes();
|
|
|
+ List<String> flashModes = params.getSupportedFlashModes();
|
|
|
|
|
|
if (flashModes == null) {
|
|
|
return;
|
|
|
} else {
|
|
|
if (count == 0) {
|
|
|
- getParams().setFlashMode(Parameters.FLASH_MODE_OFF);
|
|
|
- getmCamera().setParameters(getParams());
|
|
|
+ params.setFlashMode(Parameters.FLASH_MODE_OFF);
|
|
|
+ mCamera.setParameters(params);
|
|
|
preview = (SurfaceView) findViewById(R.id.preview);
|
|
|
mHolder = preview.getHolder();
|
|
|
mHolder.addCallback(this);
|
|
|
|
|
|
try {
|
|
|
- getmCamera().startPreview();
|
|
|
+ mCamera.startPreview();
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
|
|
@@ -198,21 +215,20 @@ public class MainActivity extends Activity implements SurfaceHolder.Callback {
|
|
|
MyApplication.setIsFlashOn(true);
|
|
|
}
|
|
|
|
|
|
- String flashMode = getParams().getFlashMode();
|
|
|
+ String flashMode = params.getFlashMode();
|
|
|
|
|
|
if (!Parameters.FLASH_MODE_TORCH.equals(flashMode)) {
|
|
|
|
|
|
if (flashModes.contains(Parameters.FLASH_MODE_TORCH)) {
|
|
|
- getParams().setFlashMode(Parameters.FLASH_MODE_TORCH);
|
|
|
- getmCamera().setParameters(getParams());
|
|
|
+ params.setFlashMode(Parameters.FLASH_MODE_TORCH);
|
|
|
+ mCamera.setParameters(params);
|
|
|
} else {
|
|
|
- getParams().setFlashMode(Parameters.FLASH_MODE_ON);
|
|
|
+ params.setFlashMode(Parameters.FLASH_MODE_ON);
|
|
|
|
|
|
- getmCamera().setParameters(getParams());
|
|
|
+ mCamera.setParameters(params);
|
|
|
try {
|
|
|
- getmCamera().autoFocus(new AutoFocusCallback() {
|
|
|
- public void onAutoFocus(boolean success,
|
|
|
- Camera camera) {
|
|
|
+ mCamera.autoFocus(new AutoFocusCallback() {
|
|
|
+ public void onAutoFocus(boolean success, Camera camera) {
|
|
|
count = 1;
|
|
|
}
|
|
|
});
|
|
@@ -228,20 +244,16 @@ public class MainActivity extends Activity implements SurfaceHolder.Callback {
|
|
|
|
|
|
// Turns the LED on when the widget is pressed.
|
|
|
private void flashOnWidget() {
|
|
|
- FlashlightWidgetReceiver.setParamsWidget(FlashlightWidgetReceiver
|
|
|
- .getmCameraWidget().getParameters());
|
|
|
+ FlashlightWidgetReceiver.setParamsWidget(FlashlightWidgetReceiver.getmCameraWidget().getParameters());
|
|
|
|
|
|
- List<String> flashModes = FlashlightWidgetReceiver.getParamsWidget()
|
|
|
- .getSupportedFlashModes();
|
|
|
+ List<String> flashModes = FlashlightWidgetReceiver.getParamsWidget().getSupportedFlashModes();
|
|
|
|
|
|
if (flashModes == null) {
|
|
|
return;
|
|
|
} else {
|
|
|
if (count == 0) {
|
|
|
- FlashlightWidgetReceiver.getParamsWidget().setFlashMode(
|
|
|
- Parameters.FLASH_MODE_OFF);
|
|
|
- FlashlightWidgetReceiver.getmCameraWidget().setParameters(
|
|
|
- FlashlightWidgetReceiver.getParamsWidget());
|
|
|
+ FlashlightWidgetReceiver.getParamsWidget().setFlashMode(Parameters.FLASH_MODE_OFF);
|
|
|
+ FlashlightWidgetReceiver.getmCameraWidget().setParameters(FlashlightWidgetReceiver.getParamsWidget());
|
|
|
|
|
|
try {
|
|
|
FlashlightWidgetReceiver.getmCameraWidget().startPreview();
|
|
@@ -253,30 +265,25 @@ public class MainActivity extends Activity implements SurfaceHolder.Callback {
|
|
|
MyApplication.setIsFlashOn(true);
|
|
|
}
|
|
|
|
|
|
- String flashMode = FlashlightWidgetReceiver.getParamsWidget()
|
|
|
- .getFlashMode();
|
|
|
+ String flashMode = FlashlightWidgetReceiver.getParamsWidget().getFlashMode();
|
|
|
|
|
|
if (!Parameters.FLASH_MODE_TORCH.equals(flashMode)) {
|
|
|
|
|
|
if (flashModes.contains(Parameters.FLASH_MODE_TORCH)) {
|
|
|
- FlashlightWidgetReceiver.getParamsWidget().setFlashMode(
|
|
|
- Parameters.FLASH_MODE_TORCH);
|
|
|
- FlashlightWidgetReceiver.getmCameraWidget().setParameters(
|
|
|
- FlashlightWidgetReceiver.getParamsWidget());
|
|
|
+ FlashlightWidgetReceiver.getParamsWidget().setFlashMode(Parameters.FLASH_MODE_TORCH);
|
|
|
+ FlashlightWidgetReceiver.getmCameraWidget()
|
|
|
+ .setParameters(FlashlightWidgetReceiver.getParamsWidget());
|
|
|
} else {
|
|
|
- FlashlightWidgetReceiver.getParamsWidget().setFlashMode(
|
|
|
- Parameters.FLASH_MODE_ON);
|
|
|
+ FlashlightWidgetReceiver.getParamsWidget().setFlashMode(Parameters.FLASH_MODE_ON);
|
|
|
|
|
|
- FlashlightWidgetReceiver.getmCameraWidget().setParameters(
|
|
|
- FlashlightWidgetReceiver.getParamsWidget());
|
|
|
+ FlashlightWidgetReceiver.getmCameraWidget()
|
|
|
+ .setParameters(FlashlightWidgetReceiver.getParamsWidget());
|
|
|
try {
|
|
|
- FlashlightWidgetReceiver.getmCameraWidget().autoFocus(
|
|
|
- new AutoFocusCallback() {
|
|
|
- public void onAutoFocus(boolean success,
|
|
|
- Camera camera) {
|
|
|
- count = 1;
|
|
|
- }
|
|
|
- });
|
|
|
+ FlashlightWidgetReceiver.getmCameraWidget().autoFocus(new AutoFocusCallback() {
|
|
|
+ public void onAutoFocus(boolean success, Camera camera) {
|
|
|
+ count = 1;
|
|
|
+ }
|
|
|
+ });
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
@@ -298,32 +305,11 @@ public class MainActivity extends Activity implements SurfaceHolder.Callback {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // Any time the widget or the button in the app is pressed to turn the LED
|
|
|
- // off we process this off click.
|
|
|
- private void processOffClick() {
|
|
|
-
|
|
|
- flashlight_button.setBackgroundResource(R.drawable.light_on);
|
|
|
- setWidgetTo(R.drawable.light_on);
|
|
|
-
|
|
|
- if (getmCamera() != null) {
|
|
|
- count = 0;
|
|
|
- flashOffApp();
|
|
|
- }
|
|
|
-
|
|
|
- if (FlashlightWidgetReceiver.getmCameraWidget() != null) {
|
|
|
- count = 0;
|
|
|
- flashOffWidget();
|
|
|
- }
|
|
|
-
|
|
|
- turnMotorolaOff();
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
// Turns the LED off when the button on the app is pressed.
|
|
|
private void flashOffApp() {
|
|
|
- getmCamera().stopPreview();
|
|
|
- getmCamera().release();
|
|
|
- setmCameraActivity(null);
|
|
|
+ mCamera.stopPreview();
|
|
|
+ mCamera.release();
|
|
|
+ mCamera = null;
|
|
|
MyApplication.setIsFlashOn(false);
|
|
|
}
|
|
|
|
|
@@ -351,14 +337,10 @@ public class MainActivity extends Activity implements SurfaceHolder.Callback {
|
|
|
// setWidgetTo changes the widget view to either off or on.
|
|
|
private void setWidgetTo(int drawable) {
|
|
|
Context context = this;
|
|
|
- AppWidgetManager appWidgetManager = AppWidgetManager
|
|
|
- .getInstance(context);
|
|
|
- RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
|
|
|
- R.layout.widget_layout);
|
|
|
- ComponentName thisWidget = new ComponentName(context,
|
|
|
- FlashlightWidgetProvider.class);
|
|
|
- remoteViews.setImageViewResource(R.id.flashlight_widget_imageview,
|
|
|
- drawable);
|
|
|
+ AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
|
|
|
+ RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
|
|
|
+ ComponentName thisWidget = new ComponentName(context, FlashlightWidgetProvider.class);
|
|
|
+ remoteViews.setImageViewResource(R.id.flashlight_widget_imageview, drawable);
|
|
|
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
|
|
|
}
|
|
|
|
|
@@ -366,9 +348,9 @@ public class MainActivity extends Activity implements SurfaceHolder.Callback {
|
|
|
public void surfaceCreated(SurfaceHolder holder) {
|
|
|
mHolder = holder;
|
|
|
mHolder.addCallback(this);
|
|
|
- if (getmCamera() != null) {
|
|
|
+ if (mCamera != null) {
|
|
|
try {
|
|
|
- getmCamera().setPreviewDisplay(mHolder);
|
|
|
+ mCamera.setPreviewDisplay(mHolder);
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
@@ -376,14 +358,13 @@ public class MainActivity extends Activity implements SurfaceHolder.Callback {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void surfaceChanged(SurfaceHolder holder, int format, int width,
|
|
|
- int height) {
|
|
|
+ public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
|
|
|
mHolder = holder;
|
|
|
mHolder.addCallback(this);
|
|
|
|
|
|
- if (getmCamera() != null) {
|
|
|
+ if (mCamera != null) {
|
|
|
try {
|
|
|
- getmCamera().setPreviewDisplay(mHolder);
|
|
|
+ mCamera.setPreviewDisplay(mHolder);
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
@@ -395,21 +376,4 @@ public class MainActivity extends Activity implements SurfaceHolder.Callback {
|
|
|
public void surfaceDestroyed(SurfaceHolder holder) {
|
|
|
mHolder = null;
|
|
|
}
|
|
|
-
|
|
|
- public static Camera getmCamera() {
|
|
|
- return mCamera;
|
|
|
- }
|
|
|
-
|
|
|
- public static void setmCameraActivity(Camera mCameraActivity) {
|
|
|
- MainActivity.mCamera = mCameraActivity;
|
|
|
- }
|
|
|
-
|
|
|
- // Getters and setters for params.
|
|
|
- public Parameters getParams() {
|
|
|
- return params;
|
|
|
- }
|
|
|
-
|
|
|
- public void setParams(Parameters params) {
|
|
|
- this.params = params;
|
|
|
- }
|
|
|
}
|