From a4683d54170455af28fa22e1fc663b4d3e20c8b0 Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 6 Jan 2016 20:32:02 +0100 Subject: [PATCH] adding a widget --- app/src/main/AndroidManifest.xml | 13 +++ .../simplemobiletools/com/MainActivity.java | 13 ++- .../simplemobiletools/com/MyCamera.java | 2 + .../simplemobiletools/com/MyCameraImpl.java | 34 +++++--- .../com/MyWidgetProvider.java | 81 +++++++++++++++++++ app/src/main/res/layout/widget.xml | 7 ++ app/src/main/res/values-w820dp/dimens.xml | 6 -- app/src/main/res/values/strings.xml | 1 + app/src/main/res/xml/widget_info.xml | 6 ++ 9 files changed, 144 insertions(+), 19 deletions(-) create mode 100644 app/src/main/java/flashlight/simplemobiletools/com/MyWidgetProvider.java create mode 100644 app/src/main/res/layout/widget.xml delete mode 100644 app/src/main/res/values-w820dp/dimens.xml create mode 100644 app/src/main/res/xml/widget_info.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index ac04c22..7b17cd5 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -28,5 +28,18 @@ + + + + + + + + diff --git a/app/src/main/java/flashlight/simplemobiletools/com/MainActivity.java b/app/src/main/java/flashlight/simplemobiletools/com/MainActivity.java index 5d50096..9f4d524 100644 --- a/app/src/main/java/flashlight/simplemobiletools/com/MainActivity.java +++ b/app/src/main/java/flashlight/simplemobiletools/com/MainActivity.java @@ -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(); } } diff --git a/app/src/main/java/flashlight/simplemobiletools/com/MyCamera.java b/app/src/main/java/flashlight/simplemobiletools/com/MyCamera.java index 5ec4152..c3a1d2f 100644 --- a/app/src/main/java/flashlight/simplemobiletools/com/MyCamera.java +++ b/app/src/main/java/flashlight/simplemobiletools/com/MyCamera.java @@ -4,4 +4,6 @@ public interface MyCamera { void enableFlashlight(); void disableFlashlight(); + + void cameraUnavailable(); } diff --git a/app/src/main/java/flashlight/simplemobiletools/com/MyCameraImpl.java b/app/src/main/java/flashlight/simplemobiletools/com/MyCameraImpl.java index b559a24..7690a57 100644 --- a/app/src/main/java/flashlight/simplemobiletools/com/MyCameraImpl.java +++ b/app/src/main/java/flashlight/simplemobiletools/com/MyCameraImpl.java @@ -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(); } diff --git a/app/src/main/java/flashlight/simplemobiletools/com/MyWidgetProvider.java b/app/src/main/java/flashlight/simplemobiletools/com/MyWidgetProvider.java new file mode 100644 index 0000000..b00fb10 --- /dev/null +++ b/app/src/main/java/flashlight/simplemobiletools/com/MyWidgetProvider.java @@ -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(); + } +} diff --git a/app/src/main/res/layout/widget.xml b/app/src/main/res/layout/widget.xml new file mode 100644 index 0000000..699e58c --- /dev/null +++ b/app/src/main/res/layout/widget.xml @@ -0,0 +1,7 @@ + + diff --git a/app/src/main/res/values-w820dp/dimens.xml b/app/src/main/res/values-w820dp/dimens.xml deleted file mode 100644 index 63fc816..0000000 --- a/app/src/main/res/values-w820dp/dimens.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - 64dp - diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5931cdf..92eb4d9 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,3 +1,4 @@ Simple Flashlight + Obtaining the camera failed diff --git a/app/src/main/res/xml/widget_info.xml b/app/src/main/res/xml/widget_info.xml new file mode 100644 index 0000000..99f38f8 --- /dev/null +++ b/app/src/main/res/xml/widget_info.xml @@ -0,0 +1,6 @@ + + +