-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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
Showing
9 changed files
with
139 additions
and
107 deletions.
There are no files selected for viewing
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
44 changes: 0 additions & 44 deletions
44
app/src/main/java/android/nachiketa/flashlight/AndroidReadWrite.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
app/src/main/java/android/nachiketa/flashlight/Global.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters