liuyuqi-dellpc 5 years ago
parent
commit
7b5fc0781f

+ 3 - 3
src/me/yoqi/flashlight/FlashlightWidgetReceiver.java

@@ -36,7 +36,7 @@ public class FlashlightWidgetReceiver extends BroadcastReceiver {
 
 			}
 
-			if (MainActivity.getmCameraActivity() != null) {
+			if (MainActivity.getmCamera() != null) {
 
 				flashOffApp();
 				MainActivity.flashlight_button.setBackgroundResource(R.drawable.light_on);
@@ -102,8 +102,8 @@ public class FlashlightWidgetReceiver extends BroadcastReceiver {
 
 	// Turns the LED off when the button on the app is pressed.
 	private void flashOffApp() {
-		MainActivity.getmCameraActivity().stopPreview();
-		MainActivity.getmCameraActivity().release();
+		MainActivity.getmCamera().stopPreview();
+		MainActivity.getmCamera().release();
 		MainActivity.setmCameraActivity(null);
 		MyApplication.setIsFlashOn(true);
 		count = 0;

+ 23 - 25
src/me/yoqi/flashlight/MainActivity.java

@@ -13,17 +13,17 @@ import android.hardware.Camera.AutoFocusCallback;
 import android.hardware.Camera.Parameters;
 import android.os.Build;
 import android.os.Bundle;
-import android.support.v7.app.AppCompatActivity;
 import android.view.SurfaceHolder;
 import android.view.SurfaceView;
 import android.view.View;
 import android.widget.Button;
 import android.widget.RemoteViews;
+import me.yoqi.flashlight.utils.DroidLED;
 
 public class MainActivity extends Activity  implements SurfaceHolder.Callback {
 
 	int count = 0;
-	static Camera mCameraActivity;
+	static Camera mCamera;
 	static Button flashlight_button;
 	Parameters params;
 	SurfaceView preview;
@@ -42,8 +42,7 @@ public class MainActivity extends Activity  implements SurfaceHolder.Callback {
 				setWidgetTo(R.drawable.light_on);
 			}
 
-			if (getmCameraActivity() != null) {
-
+			if (getmCamera() != null) {
 				try {
 					flashOffApp();
 				} catch (Exception e) {
@@ -109,7 +108,7 @@ public class MainActivity extends Activity  implements SurfaceHolder.Callback {
 
 			}
 
-			if (getmCameraActivity() != null) {
+			if (getmCamera() != null) {
 
 				try {
 					flashOffApp();
@@ -143,7 +142,7 @@ public class MainActivity extends Activity  implements SurfaceHolder.Callback {
 		flashlight_button.setBackgroundResource(R.drawable.light_off);
 		setWidgetTo(R.drawable.light_off);
 
-		if (getmCameraActivity() == null) {
+		if (getmCamera() == null) {
 			try {
 				mHolder.addCallback(this);
 				if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
@@ -152,7 +151,7 @@ public class MainActivity extends Activity  implements SurfaceHolder.Callback {
 				setmCameraActivity(Camera.open());
 				try {
 					if (mHolder != null) {
-						getmCameraActivity().setPreviewDisplay(mHolder);
+						getmCamera().setPreviewDisplay(mHolder);
 					}
 				} catch (IOException e) {
 					e.printStackTrace();
@@ -162,7 +161,7 @@ public class MainActivity extends Activity  implements SurfaceHolder.Callback {
 			}
 		}
 
-		if (getmCameraActivity() != null) {
+		if (getmCamera() != null) {
 			flashOnApp();
 		}
 
@@ -175,7 +174,7 @@ public class MainActivity extends Activity  implements SurfaceHolder.Callback {
 	}
 
 	private void flashOnApp() {
-		setParams(getmCameraActivity().getParameters());
+		setParams(getmCamera().getParameters());
 
 		List<String> flashModes = getParams().getSupportedFlashModes();
 
@@ -184,13 +183,13 @@ public class MainActivity extends Activity  implements SurfaceHolder.Callback {
 		} else {
 			if (count == 0) {
 				getParams().setFlashMode(Parameters.FLASH_MODE_OFF);
-				getmCameraActivity().setParameters(getParams());
+				getmCamera().setParameters(getParams());
 				preview = (SurfaceView) findViewById(R.id.preview);
 				mHolder = preview.getHolder();
 				mHolder.addCallback(this);
 
 				try {
-					getmCameraActivity().startPreview();
+					getmCamera().startPreview();
 				} catch (Exception e) {
 					e.printStackTrace();
 
@@ -205,13 +204,13 @@ public class MainActivity extends Activity  implements SurfaceHolder.Callback {
 
 				if (flashModes.contains(Parameters.FLASH_MODE_TORCH)) {
 					getParams().setFlashMode(Parameters.FLASH_MODE_TORCH);
-					getmCameraActivity().setParameters(getParams());
+					getmCamera().setParameters(getParams());
 				} else {
 					getParams().setFlashMode(Parameters.FLASH_MODE_ON);
 
-					getmCameraActivity().setParameters(getParams());
+					getmCamera().setParameters(getParams());
 					try {
-						getmCameraActivity().autoFocus(new AutoFocusCallback() {
+						getmCamera().autoFocus(new AutoFocusCallback() {
 							public void onAutoFocus(boolean success,
 									Camera camera) {
 								count = 1;
@@ -306,7 +305,7 @@ public class MainActivity extends Activity  implements SurfaceHolder.Callback {
 		flashlight_button.setBackgroundResource(R.drawable.light_on);
 		setWidgetTo(R.drawable.light_on);
 
-		if (getmCameraActivity() != null) {
+		if (getmCamera() != null) {
 			count = 0;
 			flashOffApp();
 		}
@@ -322,8 +321,8 @@ public class MainActivity extends Activity  implements SurfaceHolder.Callback {
 
 	// Turns the LED off when the button on the app is pressed.
 	private void flashOffApp() {
-		getmCameraActivity().stopPreview();
-		getmCameraActivity().release();
+		getmCamera().stopPreview();
+		getmCamera().release();
 		setmCameraActivity(null);
 		MyApplication.setIsFlashOn(false);
 	}
@@ -367,9 +366,9 @@ public class MainActivity extends Activity  implements SurfaceHolder.Callback {
 	public void surfaceCreated(SurfaceHolder holder) {
 		mHolder = holder;
 		mHolder.addCallback(this);
-		if (getmCameraActivity() != null) {
+		if (getmCamera() != null) {
 			try {
-				getmCameraActivity().setPreviewDisplay(mHolder);
+				getmCamera().setPreviewDisplay(mHolder);
 			} catch (IOException e) {
 				e.printStackTrace();
 			}
@@ -382,9 +381,9 @@ public class MainActivity extends Activity  implements SurfaceHolder.Callback {
 		mHolder = holder;
 		mHolder.addCallback(this);
 
-		if (getmCameraActivity() != null) {
+		if (getmCamera() != null) {
 			try {
-				getmCameraActivity().setPreviewDisplay(mHolder);
+				getmCamera().setPreviewDisplay(mHolder);
 			} catch (IOException e) {
 				e.printStackTrace();
 			}
@@ -397,13 +396,12 @@ public class MainActivity extends Activity  implements SurfaceHolder.Callback {
 		mHolder = null;
 	}
 
-	// Getters and setters for mCameraActivity.
-	public static Camera getmCameraActivity() {
-		return mCameraActivity;
+	public static Camera getmCamera() {
+		return mCamera;
 	}
 
 	public static void setmCameraActivity(Camera mCameraActivity) {
-		MainActivity.mCameraActivity = mCameraActivity;
+		MainActivity.mCamera = mCameraActivity;
 	}
 
 	// Getters and setters for params.

+ 11 - 3
src/me/yoqi/flashlight/DroidLED.java → src/me/yoqi/flashlight/utils/DroidLED.java

@@ -1,15 +1,23 @@
-package me.yoqi.flashlight;
+package me.yoqi.flashlight.utils;
 
 import java.lang.reflect.Method;
 
 import android.os.IBinder;
 
-class DroidLED {
+/**
+ * 闪光灯工具
+ * @author liuyuqi
+ *
+ */
+public class DroidLED {
 	
 	private Object svc = null;
-	private Method getFlashlightEnabled = null;
+	private Method getFlashlightEnabled = null; //系统flash闪光灯get方法
 	private Method setFlashlightEnabled = null;
 
+	/** 构造函数
+	 * @throws Exception
+	 */
 	@SuppressWarnings({ "unchecked", "rawtypes" })
 	public DroidLED() throws Exception {
 		try {