mirror of
https://github.com/SimpleMobileTools/Simple-Flashlight.git
synced 2025-06-05 21:59:19 +02:00
some refactoring, no functionality change
This commit is contained in:
@ -20,7 +20,7 @@
|
||||
android:supportsRtl="true"
|
||||
android:theme="@style/AppTheme">
|
||||
<activity
|
||||
android:name=".MainActivity"
|
||||
android:name=".activities.MainActivity"
|
||||
android:screenOrientation="portrait">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN"/>
|
||||
@ -30,12 +30,12 @@
|
||||
</activity>
|
||||
|
||||
<activity
|
||||
android:name=".AboutActivity"
|
||||
android:name=".activities.AboutActivity"
|
||||
android:label="@string/about"
|
||||
android:screenOrientation="portrait"/>
|
||||
|
||||
<activity
|
||||
android:name=".LicenseActivity"
|
||||
android:name=".activities.LicenseActivity"
|
||||
android:label="@string/third_party_licences"
|
||||
android:screenOrientation="portrait"/>
|
||||
|
||||
|
@ -9,6 +9,7 @@ import android.util.Log;
|
||||
|
||||
public class MarshmallowCamera {
|
||||
private static final String TAG = MyCameraImpl.class.getSimpleName();
|
||||
|
||||
private MyCamera mCallback;
|
||||
private Context mContext;
|
||||
|
||||
|
@ -6,26 +6,27 @@ import android.util.Log;
|
||||
|
||||
public class MyCameraImpl {
|
||||
private static final String TAG = MyCameraImpl.class.getSimpleName();
|
||||
private Camera camera;
|
||||
private Camera.Parameters params;
|
||||
private boolean isFlashlightOn;
|
||||
private MyCamera callback;
|
||||
private Context context;
|
||||
private boolean isMarshmallow;
|
||||
private MarshmallowCamera marshmallowCamera;
|
||||
private static Camera mCamera;
|
||||
private static Camera.Parameters mParams;
|
||||
private static MyCamera mCallback;
|
||||
private static Context mContext;
|
||||
private static MarshmallowCamera mMarshmallowCamera;
|
||||
|
||||
private static boolean mIsFlashlightOn;
|
||||
private static boolean mIsMarshmallow;
|
||||
|
||||
public MyCameraImpl(MyCamera camera, Context cxt) {
|
||||
callback = camera;
|
||||
context = cxt;
|
||||
isMarshmallow = isMarshmallow();
|
||||
mCallback = camera;
|
||||
mContext = cxt;
|
||||
mIsMarshmallow = isMarshmallow();
|
||||
handleCameraSetup();
|
||||
}
|
||||
|
||||
public void toggleFlashlight() {
|
||||
handleCameraSetup();
|
||||
isFlashlightOn = !isFlashlightOn;
|
||||
mIsFlashlightOn = !mIsFlashlightOn;
|
||||
|
||||
if (isFlashlightOn) {
|
||||
if (mIsFlashlightOn) {
|
||||
enableFlashlight();
|
||||
} else {
|
||||
disableFlashlight();
|
||||
@ -33,7 +34,7 @@ public class MyCameraImpl {
|
||||
}
|
||||
|
||||
public void handleCameraSetup() {
|
||||
if (isMarshmallow) {
|
||||
if (mIsMarshmallow) {
|
||||
setupMarshmallowCamera();
|
||||
} else {
|
||||
setupCamera();
|
||||
@ -41,65 +42,67 @@ public class MyCameraImpl {
|
||||
}
|
||||
|
||||
private void setupMarshmallowCamera() {
|
||||
if (marshmallowCamera == null) {
|
||||
marshmallowCamera = new MarshmallowCamera(callback, context);
|
||||
if (mMarshmallowCamera == null) {
|
||||
mMarshmallowCamera = new MarshmallowCamera(mCallback, mContext);
|
||||
}
|
||||
}
|
||||
|
||||
private void setupCamera() {
|
||||
if (isMarshmallow)
|
||||
if (mIsMarshmallow)
|
||||
return;
|
||||
|
||||
if (camera == null) {
|
||||
if (mCamera == null) {
|
||||
try {
|
||||
camera = Camera.open();
|
||||
params = camera.getParameters();
|
||||
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
|
||||
camera.setParameters(params);
|
||||
mCamera = Camera.open();
|
||||
mParams = mCamera.getParameters();
|
||||
mParams.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
|
||||
mCamera.setParameters(mParams);
|
||||
|
||||
if (isFlashlightOn)
|
||||
if (mIsFlashlightOn)
|
||||
enableFlashlight();
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "setup camera " + e.getMessage());
|
||||
callback.cameraUnavailable();
|
||||
Log.e(TAG, "setup mCamera " + e.getMessage());
|
||||
mCallback.cameraUnavailable();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void enableFlashlight() {
|
||||
if (isMarshmallow) {
|
||||
if (mIsMarshmallow) {
|
||||
toggleMarshmallowFlashlight(true);
|
||||
} else {
|
||||
if (camera == null || params == null)
|
||||
if (mCamera == null || mParams == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
|
||||
camera.setParameters(params);
|
||||
mParams.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
|
||||
mCamera.setParameters(mParams);
|
||||
}
|
||||
callback.enableFlashlight();
|
||||
mCallback.enableFlashlight();
|
||||
}
|
||||
|
||||
private void disableFlashlight() {
|
||||
if (isMarshmallow()) {
|
||||
toggleMarshmallowFlashlight(false);
|
||||
} else {
|
||||
if (camera == null || params == null)
|
||||
if (mCamera == null || mParams == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
|
||||
camera.setParameters(params);
|
||||
mParams.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
|
||||
mCamera.setParameters(mParams);
|
||||
}
|
||||
callback.disableFlashlight();
|
||||
mCallback.disableFlashlight();
|
||||
}
|
||||
|
||||
private void toggleMarshmallowFlashlight(boolean enable) {
|
||||
marshmallowCamera.toggleMarshmallowFlashlight(enable);
|
||||
mMarshmallowCamera.toggleMarshmallowFlashlight(enable);
|
||||
}
|
||||
|
||||
public void releaseCamera() {
|
||||
if (camera != null) {
|
||||
camera.release();
|
||||
camera = null;
|
||||
if (mCamera != null) {
|
||||
mCamera.release();
|
||||
mCamera = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -11,67 +11,66 @@ import android.graphics.Bitmap;
|
||||
import android.widget.RemoteViews;
|
||||
|
||||
public class MyWidgetProvider extends AppWidgetProvider implements MyCamera {
|
||||
private static MyCameraImpl cameraImpl;
|
||||
private static RemoteViews remoteViews;
|
||||
private static Context cxt;
|
||||
private static int[] widgetIds;
|
||||
private static AppWidgetManager widgetManager;
|
||||
private static final String TOGGLE = "toggle";
|
||||
private static Bitmap coloredBmp;
|
||||
private static MyCameraImpl mCameraImpl;
|
||||
private static RemoteViews mRemoteViews;
|
||||
private static AppWidgetManager mWidgetManager;
|
||||
private static Bitmap mColoredBmp;
|
||||
|
||||
private static int[] mWidgetIds;
|
||||
|
||||
@Override
|
||||
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
|
||||
initVariables(context);
|
||||
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
|
||||
appWidgetManager.updateAppWidget(appWidgetIds, mRemoteViews);
|
||||
}
|
||||
|
||||
private void initVariables(Context context) {
|
||||
cxt = context;
|
||||
final ComponentName component = new ComponentName(cxt, MyWidgetProvider.class);
|
||||
widgetManager = AppWidgetManager.getInstance(context);
|
||||
widgetIds = widgetManager.getAppWidgetIds(component);
|
||||
final ComponentName component = new ComponentName(context, MyWidgetProvider.class);
|
||||
mWidgetManager = AppWidgetManager.getInstance(context);
|
||||
mWidgetIds = mWidgetManager.getAppWidgetIds(component);
|
||||
|
||||
final Intent intent = new Intent(cxt, MyWidgetProvider.class);
|
||||
final Intent intent = new Intent(context, MyWidgetProvider.class);
|
||||
intent.setAction(TOGGLE);
|
||||
|
||||
final PendingIntent pendingIntent = PendingIntent.getBroadcast(cxt, 0, intent, 0);
|
||||
remoteViews = new RemoteViews(cxt.getPackageName(), R.layout.widget);
|
||||
remoteViews.setOnClickPendingIntent(R.id.toggle_btn, pendingIntent);
|
||||
cameraImpl = new MyCameraImpl(this, cxt);
|
||||
final PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
|
||||
mRemoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
|
||||
mRemoteViews.setOnClickPendingIntent(R.id.toggle_btn, pendingIntent);
|
||||
mCameraImpl = new MyCameraImpl(this, context);
|
||||
|
||||
final Resources res = cxt.getResources();
|
||||
final Resources res = context.getResources();
|
||||
final int appColor = res.getColor(R.color.colorPrimary);
|
||||
coloredBmp = Utils.getColoredIcon(cxt.getResources(), appColor, R.mipmap.flashlight_small);
|
||||
mColoredBmp = Utils.getColoredIcon(context.getResources(), appColor, R.mipmap.flashlight_small);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
final String action = intent.getAction();
|
||||
if (action.equals(TOGGLE)) {
|
||||
if (cameraImpl == null) {
|
||||
if (mCameraImpl == null) {
|
||||
initVariables(context);
|
||||
}
|
||||
|
||||
cameraImpl.toggleFlashlight();
|
||||
mCameraImpl.toggleFlashlight();
|
||||
} else
|
||||
super.onReceive(context, intent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enableFlashlight() {
|
||||
remoteViews.setImageViewBitmap(R.id.toggle_btn, coloredBmp);
|
||||
for (int widgetId : widgetIds) {
|
||||
widgetManager.updateAppWidget(widgetId, remoteViews);
|
||||
mRemoteViews.setImageViewBitmap(R.id.toggle_btn, mColoredBmp);
|
||||
for (int widgetId : mWidgetIds) {
|
||||
mWidgetManager.updateAppWidget(widgetId, mRemoteViews);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disableFlashlight() {
|
||||
remoteViews.setImageViewResource(R.id.toggle_btn, R.mipmap.flashlight_small);
|
||||
for (int widgetId : widgetIds) {
|
||||
widgetManager.updateAppWidget(widgetId, remoteViews);
|
||||
mRemoteViews.setImageViewResource(R.id.toggle_btn, R.mipmap.flashlight_small);
|
||||
for (int widgetId : mWidgetIds) {
|
||||
mWidgetManager.updateAppWidget(widgetId, mRemoteViews);
|
||||
}
|
||||
cameraImpl.releaseCamera();
|
||||
mCameraImpl.releaseCamera();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -85,10 +84,10 @@ public class MyWidgetProvider extends AppWidgetProvider implements MyCamera {
|
||||
}
|
||||
|
||||
private void releaseCamera(Context context) {
|
||||
if (cameraImpl == null)
|
||||
if (mCameraImpl == null)
|
||||
initVariables(context);
|
||||
|
||||
disableFlashlight();
|
||||
cameraImpl.releaseCamera();
|
||||
mCameraImpl.releaseCamera();
|
||||
}
|
||||
}
|
||||
|
@ -19,6 +19,7 @@ public class Utils {
|
||||
final Paint paint = new Paint();
|
||||
final ColorFilter filter = new PorterDuffColorFilter(newTextColor, PorterDuff.Mode.SRC_IN);
|
||||
paint.setColorFilter(filter);
|
||||
|
||||
final Canvas canvas = new Canvas(bmp);
|
||||
canvas.drawBitmap(bmp, 0, 0, paint);
|
||||
return bmp;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.simplemobiletools.flashlight;
|
||||
package com.simplemobiletools.flashlight.activities;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.content.res.Resources;
|
||||
@ -8,6 +8,9 @@ import android.text.Html;
|
||||
import android.text.method.LinkMovementMethod;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.simplemobiletools.flashlight.BuildConfig;
|
||||
import com.simplemobiletools.flashlight.R;
|
||||
|
||||
import java.util.Calendar;
|
||||
|
||||
import butterknife.BindView;
|
||||
@ -15,17 +18,18 @@ import butterknife.ButterKnife;
|
||||
import butterknife.OnClick;
|
||||
|
||||
public class AboutActivity extends AppCompatActivity {
|
||||
@BindView(R.id.about_copyright) TextView copyright;
|
||||
@BindView(R.id.about_version) TextView version;
|
||||
@BindView(R.id.about_email) TextView emailTV;
|
||||
private Resources res;
|
||||
@BindView(R.id.about_copyright) TextView mCopyright;
|
||||
@BindView(R.id.about_version) TextView mVersion;
|
||||
@BindView(R.id.about_email) TextView mEmailTV;
|
||||
|
||||
private static Resources mRes;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_about);
|
||||
ButterKnife.bind(this);
|
||||
res = getResources();
|
||||
mRes = getResources();
|
||||
|
||||
setupEmail();
|
||||
setupVersion();
|
||||
@ -33,23 +37,23 @@ public class AboutActivity extends AppCompatActivity {
|
||||
}
|
||||
|
||||
private void setupEmail() {
|
||||
final String email = res.getString(R.string.email);
|
||||
final String appName = res.getString(R.string.app_name);
|
||||
final String email = mRes.getString(R.string.email);
|
||||
final String appName = mRes.getString(R.string.app_name);
|
||||
final String href = "<a href=\"mailto:" + email + "?subject=" + appName + "\">" + email + "</a>";
|
||||
emailTV.setText(Html.fromHtml(href));
|
||||
emailTV.setMovementMethod(LinkMovementMethod.getInstance());
|
||||
mEmailTV.setText(Html.fromHtml(href));
|
||||
mEmailTV.setMovementMethod(LinkMovementMethod.getInstance());
|
||||
}
|
||||
|
||||
private void setupVersion() {
|
||||
final String versionName = BuildConfig.VERSION_NAME;
|
||||
final String versionText = String.format(res.getString(R.string.version), versionName);
|
||||
version.setText(versionText);
|
||||
final String versionText = String.format(mRes.getString(R.string.version), versionName);
|
||||
mVersion.setText(versionText);
|
||||
}
|
||||
|
||||
private void setupCopyright() {
|
||||
final int year = Calendar.getInstance().get(Calendar.YEAR);
|
||||
final String copyrightText = String.format(res.getString(R.string.copyright), year);
|
||||
copyright.setText(copyrightText);
|
||||
final String copyrightText = String.format(mRes.getString(R.string.copyright), year);
|
||||
mCopyright.setText(copyrightText);
|
||||
}
|
||||
|
||||
@OnClick(R.id.about_license)
|
@ -1,14 +1,17 @@
|
||||
package com.simplemobiletools.flashlight;
|
||||
package com.simplemobiletools.flashlight.activities;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
|
||||
import com.simplemobiletools.flashlight.R;
|
||||
|
||||
import butterknife.ButterKnife;
|
||||
import butterknife.OnClick;
|
||||
|
||||
public class LicenseActivity extends AppCompatActivity {
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
@ -1,22 +1,27 @@
|
||||
package com.simplemobiletools.flashlight;
|
||||
package com.simplemobiletools.flashlight.activities;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.graphics.PorterDuff;
|
||||
import android.os.Bundle;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.view.WindowManager;
|
||||
import android.widget.ImageView;
|
||||
|
||||
import com.simplemobiletools.flashlight.MyCamera;
|
||||
import com.simplemobiletools.flashlight.MyCameraImpl;
|
||||
import com.simplemobiletools.flashlight.R;
|
||||
import com.simplemobiletools.flashlight.Utils;
|
||||
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
import butterknife.OnClick;
|
||||
|
||||
public class MainActivity extends AppCompatActivity implements MyCamera {
|
||||
@BindView(R.id.toggle_btn) ImageView toggleBtn;
|
||||
private MyCameraImpl cameraImpl;
|
||||
@BindView(R.id.toggle_btn) ImageView mToggleBtn;
|
||||
|
||||
private static MyCameraImpl mCameraImpl;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@ -29,8 +34,7 @@ public class MainActivity extends AppCompatActivity implements MyCamera {
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
MenuInflater inflater = getMenuInflater();
|
||||
inflater.inflate(R.menu.menu, menu);
|
||||
getMenuInflater().inflate(R.menu.menu, menu);
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -47,50 +51,50 @@ public class MainActivity extends AppCompatActivity implements MyCamera {
|
||||
}
|
||||
|
||||
private void setupCameraImpl() {
|
||||
cameraImpl = new MyCameraImpl(this, this);
|
||||
cameraImpl.toggleFlashlight();
|
||||
mCameraImpl = new MyCameraImpl(this, this);
|
||||
mCameraImpl.toggleFlashlight();
|
||||
}
|
||||
|
||||
@OnClick(R.id.toggle_btn)
|
||||
public void toggleFlashlight() {
|
||||
cameraImpl.toggleFlashlight();
|
||||
mCameraImpl.toggleFlashlight();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStart() {
|
||||
super.onStart();
|
||||
cameraImpl.handleCameraSetup();
|
||||
mCameraImpl.handleCameraSetup();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
cameraImpl.handleCameraSetup();
|
||||
mCameraImpl.handleCameraSetup();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
cameraImpl.releaseCamera();
|
||||
mCameraImpl.releaseCamera();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStop() {
|
||||
super.onStop();
|
||||
cameraImpl.releaseCamera();
|
||||
mCameraImpl.releaseCamera();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enableFlashlight() {
|
||||
final int appColor = getResources().getColor(R.color.colorPrimary);
|
||||
toggleBtn.setImageResource(R.mipmap.flashlight_big);
|
||||
toggleBtn.getDrawable().mutate().setColorFilter(appColor, PorterDuff.Mode.SRC_IN);
|
||||
mToggleBtn.setImageResource(R.mipmap.flashlight_big);
|
||||
mToggleBtn.getDrawable().mutate().setColorFilter(appColor, PorterDuff.Mode.SRC_IN);
|
||||
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disableFlashlight() {
|
||||
toggleBtn.setImageResource(R.mipmap.flashlight_big);
|
||||
mToggleBtn.setImageResource(R.mipmap.flashlight_big);
|
||||
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
|
||||
}
|
||||
|
Reference in New Issue
Block a user