Skip to content

Commit

Permalink
- Optimize code
Browse files Browse the repository at this point in the history
- Renamed AndroidRewrite.java to Global.java
- Fixed infinite loadFile() calls
- Moved torchToggle() and changeBackground() Global
  • Loading branch information
Nachiketa Vadera committed Sep 14, 2018
1 parent b924d3a commit 072a0dc
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 107 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraManager;
import android.os.Build;
import android.os.IBinder;
import android.os.Vibrator;

Expand All @@ -20,10 +17,7 @@ public class FlashlightShakeToggle extends Service implements SensorEventListene
public static final int MIN_TIME_BETWEEN_SHAKES = 1000;
private long lastShakeTime = 0;
private boolean isFlashlightOn = false;

public FlashlightShakeToggle() {

}
private Float shakeThreshold;

@Override
public void onCreate() {
Expand All @@ -34,6 +28,14 @@ public void onCreate() {
Sensor accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorManager.registerListener((SensorEventListener) this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}

try {
shakeThreshold = Float.parseFloat(Global.loadFile(getApplicationContext(), "settings.txt"));
} catch (Exception ex) {
Global.saveFile(getApplicationContext(), "settings.txt", String.valueOf(10.2f));
shakeThreshold = 10.2f;
ex.getMessage();
}
}

@Override
Expand All @@ -43,16 +45,11 @@ public IBinder onBind(Intent intent) {

@Override
public void onSensorChanged(SensorEvent event) {
Float shakeThreshold;
try {
shakeThreshold = Float.parseFloat(AndroidReadWrite.loadFile(getApplicationContext(), "settings.txt"));
} catch (Exception ex) {
AndroidReadWrite.saveFile(getApplicationContext(), "settings.txt", 10.2f + "");
shakeThreshold = 10.2f;
ex.getMessage();
}

Global global = new Global();

if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

long curTime = System.currentTimeMillis();
if ((curTime - lastShakeTime) > MIN_TIME_BETWEEN_SHAKES) {

Expand All @@ -67,38 +64,13 @@ public void onSensorChanged(SensorEvent event) {
if (acceleration > shakeThreshold) {
lastShakeTime = curTime;
if (!isFlashlightOn) {
torchToggle("on");
isFlashlightOn = true;
} else {
torchToggle("off");
isFlashlightOn = false;
}
}
}
}
}

private void torchToggle(String command) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
CameraManager camManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
String cameraId = null; // Usually back camera is at 0 position.
try {
if (camManager != null) {
cameraId = camManager.getCameraIdList()[0];
}
if (camManager != null) {
if (command.equals("on")) {
camManager.setTorchMode(cameraId, true); // Turn ON
vibrator.vibrate(500);
isFlashlightOn = true;
isFlashlightOn = global.torchToggle("on", this);
global.changeBackground(true);
} else {
camManager.setTorchMode(cameraId, false); // Turn OFF
vibrator.vibrate(500);
isFlashlightOn = false;
isFlashlightOn = global.torchToggle("off", this);
global.changeBackground(false);
}
}
} catch (CameraAccessException e) {
e.getMessage();
}
}
}
Expand Down
100 changes: 100 additions & 0 deletions app/src/main/java/android/nachiketa/flashlight/Global.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package android.nachiketa.flashlight;

import android.content.Context;
import android.graphics.Color;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraManager;
import android.os.Build;
import android.os.Vibrator;
import android.util.Log;
import android.widget.RelativeLayout;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Objects;

public class Global {

// TODO : SOLVE BACKGROUND COLOR ERROR - NULL POINTER EXCEPTION (TRY TO PASS ACTIVITY AS ARGUMENT)


private RelativeLayout layout;

public void setRelativeLayout(RelativeLayout relativeLayout) {
layout = relativeLayout;
}

public void changeBackground(boolean isTorchOn) {
if (isTorchOn) {
Objects.requireNonNull(layout).setBackgroundColor(Color.WHITE);
} else {
Objects.requireNonNull(layout).setBackgroundColor(Color.BLACK);
}
}

public boolean torchToggle(String command, Context context) {

Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
CameraManager camManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
String cameraId = null; // Usually back camera is at 0 position.
try {
if (camManager != null) {
cameraId = camManager.getCameraIdList()[0];
}
if (camManager != null) {
if (command.equals("on")) {
camManager.setTorchMode(cameraId, true); // Turn ON
Objects.requireNonNull(vibrator).vibrate(500);
changeBackground(true);
return true;

} else {
camManager.setTorchMode(cameraId, false); // Turn OFF
Objects.requireNonNull(vibrator).vibrate(500);
changeBackground(false);
return false;
}
}
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
return false;
}

public static boolean saveFile(Context context, String fileName, String data) {
try {
FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
Writer out = new OutputStreamWriter(fos);
out.write(data);
out.close();
Log.i("saveFile", "File saved successfully");
return true;
} catch (IOException ioEx) {
Log.e("saveFile", ioEx.getMessage());
return false;
}
}

public static String loadFile(Context context, String fileName) {
try {
FileInputStream fileInputStream = context.openFileInput(fileName);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
String line = bufferedReader.readLine();
bufferedReader.close();
Log.i("loadFile", "File read successfully");
return line;
} catch (IOException ioEx) {
Log.e("loadFile", ioEx.getMessage());
return null;
}
}

}
26 changes: 11 additions & 15 deletions app/src/main/java/android/nachiketa/flashlight/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraManager;
import android.os.Build;
Expand All @@ -13,21 +12,27 @@
import android.widget.Button;
import android.widget.RelativeLayout;

import java.util.Objects;

public class MainActivity extends AppCompatActivity {

// TODO : Optimize code

RelativeLayout relativeLayout = null;
Global global;
boolean isTorchOn = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

startService(new Intent(this, FlashlightShakeToggle.class));
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relLayMain);

global = new Global();

relativeLayout = (RelativeLayout) findViewById(R.id.relLayMain);
global.setRelativeLayout(Objects.requireNonNull(relativeLayout));

startService(new Intent(this, FlashlightShakeToggle.class));
}

@Override
Expand Down Expand Up @@ -60,11 +65,11 @@ private void torchToggle(String command) {
if (command.equals("on")) {
camManager.setTorchMode(cameraId, true); // Turn ON
isTorchOn = true;
changeBackground();
global.changeBackground(true);
} else {
camManager.setTorchMode(cameraId, false); // Turn OFF
isTorchOn = false;
changeBackground();
global.changeBackground(false);
}
}
} catch (CameraAccessException e) {
Expand All @@ -73,15 +78,6 @@ private void torchToggle(String command) {
}
}

private void changeBackground() {
if (isTorchOn) {
relativeLayout.setBackgroundColor(Color.WHITE);
} else {
relativeLayout.setBackgroundColor(Color.BLACK);
}
}


@Override
protected void onDestroy() {
startService(new Intent(this, FlashlightShakeToggle.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import com.bumptech.glide.Glide;

import java.util.Objects;

public class SettingsActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {

SeekBar seekBar = null;
Expand All @@ -21,7 +23,7 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);

String progress = AndroidReadWrite.loadFile(getApplicationContext(), "settings.txt");
String progress = Global.loadFile(getApplicationContext(), "settings.txt");

seekBar = (SeekBar) findViewById(R.id.skbThreshold);
seekBar.setProgress((int) Float.parseFloat(Objects.requireNonNull(progress).replace("f","")));
Expand All @@ -37,7 +39,7 @@ protected void onCreate(Bundle savedInstanceState) {
}

public void saveSettings(View view) {
if (AndroidReadWrite.saveFile(getApplicationContext(), "settings.txt", seekBar.getProgress() + "")) {
if (Global.saveFile(getApplicationContext(), "settings.txt", seekBar.getProgress() + "")) {
Log.i("Flashlight_saveSettings", "File Saved Successfully");
startActivity(new Intent(this, MainActivity.class));
finish();
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.3'
classpath 'com.android.tools.build:gradle:3.1.4'


// NOTE: Do not place your application dependencies here; they belong
Expand Down

0 comments on commit 072a0dc

Please sign in to comment.