mirror of
https://github.com/SimpleMobileTools/Simple-Flashlight.git
synced 2025-01-19 04:01:42 +01:00
adding a widget
This commit is contained in:
parent
901a2c6861
commit
a4683d5417
@ -28,5 +28,18 @@
|
||||
<category android:name="android.intent.category.LAUNCHER"/>
|
||||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
<receiver
|
||||
android:name=".MyWidgetProvider"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:label="Simple Flashlight">
|
||||
<intent-filter>
|
||||
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
|
||||
</intent-filter>
|
||||
|
||||
<meta-data
|
||||
android:name="android.appwidget.provider"
|
||||
android:resource="@xml/widget_info"/>
|
||||
</receiver>
|
||||
</application>
|
||||
</manifest>
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
7
app/src/main/res/layout/widget.xml
Normal file
7
app/src/main/res/layout/widget.xml
Normal file
@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<ImageView
|
||||
android:id="@+id/toggle_btn"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:src="@mipmap/flashlight_big_off"/>
|
@ -1,6 +0,0 @@
|
||||
<resources>
|
||||
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
|
||||
(such as screen margins) for screens with more than 820dp of available width. This
|
||||
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
|
||||
<dimen name="activity_horizontal_margin">64dp</dimen>
|
||||
</resources>
|
@ -1,3 +1,4 @@
|
||||
<resources>
|
||||
<string name="app_name">Simple Flashlight</string>
|
||||
<string name="camera_error">Obtaining the camera failed</string>
|
||||
</resources>
|
||||
|
6
app/src/main/res/xml/widget_info.xml
Normal file
6
app/src/main/res/xml/widget_info.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:initialLayout="@layout/widget"
|
||||
android:minHeight="40dp"
|
||||
android:minWidth="40dp">
|
||||
</appwidget-provider>
|
Loading…
Reference in New Issue
Block a user