mirror of
https://github.com/SimpleMobileTools/Simple-Flashlight.git
synced 2025-06-05 21:59:19 +02:00
adding a widget
This commit is contained in:
@@ -4,6 +4,7 @@ import android.os.Bundle;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.view.View;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.Toast;
|
||||
|
||||
public class MainActivity extends AppCompatActivity implements MyCamera {
|
||||
private ImageView toggleBtn;
|
||||
@@ -20,7 +21,6 @@ public class MainActivity extends AppCompatActivity implements MyCamera {
|
||||
|
||||
private void setupCameraImpl() {
|
||||
cameraImpl = new MyCameraImpl(this);
|
||||
cameraImpl.setupCamera();
|
||||
cameraImpl.toggleFlashlight();
|
||||
}
|
||||
|
||||
@@ -60,11 +60,18 @@ public class MainActivity extends AppCompatActivity implements MyCamera {
|
||||
|
||||
@Override
|
||||
public void enableFlashlight() {
|
||||
toggleBtn.setBackground(getResources().getDrawable(R.mipmap.flashlight_big_on));
|
||||
toggleBtn.setImageResource(R.mipmap.flashlight_big_on);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disableFlashlight() {
|
||||
toggleBtn.setBackground(getResources().getDrawable(R.mipmap.flashlight_big_off));
|
||||
toggleBtn.setImageResource(R.mipmap.flashlight_big_off);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cameraUnavailable() {
|
||||
final String errorMsg = getResources().getString(R.string.camera_error);
|
||||
Toast.makeText(this, errorMsg, Toast.LENGTH_SHORT).show();
|
||||
disableFlashlight();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,4 +4,6 @@ public interface MyCamera {
|
||||
void enableFlashlight();
|
||||
|
||||
void disableFlashlight();
|
||||
|
||||
void cameraUnavailable();
|
||||
}
|
||||
|
||||
@@ -1,16 +1,20 @@
|
||||
package flashlight.simplemobiletools.com;
|
||||
|
||||
import android.hardware.Camera;
|
||||
|
||||
public class MyCameraImpl {
|
||||
private android.hardware.Camera camera;
|
||||
private android.hardware.Camera.Parameters params;
|
||||
private Camera camera;
|
||||
private Camera.Parameters params;
|
||||
private boolean isFlashlightOn;
|
||||
private MyCamera callback;
|
||||
|
||||
public MyCameraImpl(MyCamera camera) {
|
||||
callback = camera;
|
||||
setupCamera();
|
||||
}
|
||||
|
||||
public void toggleFlashlight() {
|
||||
setupCamera();
|
||||
isFlashlightOn = !isFlashlightOn;
|
||||
|
||||
if (isFlashlightOn) {
|
||||
@@ -22,24 +26,34 @@ public class MyCameraImpl {
|
||||
|
||||
public void setupCamera() {
|
||||
if (camera == null) {
|
||||
camera = android.hardware.Camera.open();
|
||||
params = camera.getParameters();
|
||||
params.setFlashMode(android.hardware.Camera.Parameters.FLASH_MODE_OFF);
|
||||
camera.setParameters(params);
|
||||
try {
|
||||
camera = Camera.open();
|
||||
params = camera.getParameters();
|
||||
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
|
||||
camera.setParameters(params);
|
||||
|
||||
if (isFlashlightOn)
|
||||
enableFlashlight();
|
||||
if (isFlashlightOn)
|
||||
enableFlashlight();
|
||||
} catch (Exception e) {
|
||||
callback.cameraUnavailable();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void enableFlashlight() {
|
||||
params.setFlashMode(android.hardware.Camera.Parameters.FLASH_MODE_TORCH);
|
||||
if (camera == null || params == null)
|
||||
return;
|
||||
|
||||
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
|
||||
camera.setParameters(params);
|
||||
callback.enableFlashlight();
|
||||
}
|
||||
|
||||
private void disableFlashlight() {
|
||||
params.setFlashMode(android.hardware.Camera.Parameters.FLASH_MODE_OFF);
|
||||
if (camera == null || params == null)
|
||||
return;
|
||||
|
||||
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
|
||||
camera.setParameters(params);
|
||||
callback.disableFlashlight();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
package flashlight.simplemobiletools.com;
|
||||
|
||||
import android.app.PendingIntent;
|
||||
import android.appwidget.AppWidgetManager;
|
||||
import android.appwidget.AppWidgetProvider;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.widget.RemoteViews;
|
||||
|
||||
public class MyWidgetProvider extends AppWidgetProvider implements MyCamera {
|
||||
private static MyCameraImpl cameraImpl;
|
||||
private static RemoteViews remoteViews;
|
||||
private static int[] widgetIds;
|
||||
private static AppWidgetManager widgetManager;
|
||||
private static final String TOGGLE = "toggle";
|
||||
|
||||
@Override
|
||||
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
|
||||
initVariables(context);
|
||||
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
|
||||
}
|
||||
|
||||
private void initVariables(Context context) {
|
||||
final ComponentName component = new ComponentName(context, MyWidgetProvider.class);
|
||||
widgetManager = AppWidgetManager.getInstance(context);
|
||||
widgetIds = widgetManager.getAppWidgetIds(component);
|
||||
|
||||
final Intent intent = new Intent(context, MyWidgetProvider.class);
|
||||
intent.setAction(TOGGLE);
|
||||
|
||||
final PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
|
||||
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
|
||||
remoteViews.setOnClickPendingIntent(R.id.toggle_btn, pendingIntent);
|
||||
cameraImpl = new MyCameraImpl(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
final String action = intent.getAction();
|
||||
if (action.equals(TOGGLE)) {
|
||||
if (cameraImpl == null) {
|
||||
initVariables(context);
|
||||
}
|
||||
|
||||
cameraImpl.toggleFlashlight();
|
||||
} else
|
||||
super.onReceive(context, intent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enableFlashlight() {
|
||||
remoteViews.setImageViewResource(R.id.toggle_btn, R.mipmap.flashlight_big_on);
|
||||
widgetManager.updateAppWidget(widgetIds, remoteViews);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disableFlashlight() {
|
||||
remoteViews.setImageViewResource(R.id.toggle_btn, R.mipmap.flashlight_big_off);
|
||||
widgetManager.updateAppWidget(widgetIds, remoteViews);
|
||||
cameraImpl.releaseCamera();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cameraUnavailable() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDeleted(Context context, int[] appWidgetIds) {
|
||||
super.onDeleted(context, appWidgetIds);
|
||||
releaseCamera(context);
|
||||
}
|
||||
|
||||
private void releaseCamera(Context context) {
|
||||
if (cameraImpl == null)
|
||||
initVariables(context);
|
||||
|
||||
disableFlashlight();
|
||||
cameraImpl.releaseCamera();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user