Created
September 14, 2017 00:49
-
-
Save ElyDantas/4c5aa45d06ff74e3f57972fc9bc513e8 to your computer and use it in GitHub Desktop.
Example of code to turn on/off a android flashlight in a particular sequence and speed
This file contains hidden or 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
package esmaltec.etalk; | |
import android.content.pm.PackageManager; | |
import android.hardware.Camera; | |
import android.hardware.camera2.CameraAccessException; | |
import android.hardware.camera2.CameraCharacteristics; | |
import android.hardware.camera2.CameraManager; | |
import android.os.Build; | |
import android.os.Bundle; | |
import android.os.Handler; | |
import android.support.v4.app.Fragment; | |
import android.support.v4.content.ContextCompat; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.EditText; | |
import android.widget.SeekBar; | |
import android.widget.TextView; | |
import android.widget.ToggleButton; | |
import java.util.Random; | |
import java.util.Timer; | |
import java.util.TimerTask; | |
import static android.content.Context.CAMERA_SERVICE; | |
import static java.lang.Integer.MIN_VALUE; | |
/** | |
* A placeholder fragment containing a simple view. | |
*/ | |
public class MainActivityFragment extends Fragment { | |
private View view; | |
private SeekBar seekBar; | |
private View flashView; | |
private ToggleButton toggleButtonFlashHardware; | |
private ToggleButton toggleButtonFlashView; | |
private EditText textViewSeekBarValue; | |
private Camera mCamera; | |
private Camera.Parameters mParams; | |
private CameraManager mManager; | |
private EditText editTextCodigo; | |
private Thread mThread; | |
public MainActivityFragment() { | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
view = inflater.inflate(R.layout.fragment_main, container, false); | |
textViewSeekBarValue = (EditText) view.findViewById(R.id.textViewSeekBarValue); | |
editTextCodigo = (EditText) view.findViewById(R.id.editTextCodigo); | |
seekBar = (SeekBar) view.findViewById(R.id.seekBar); | |
flashView = (View) view.findViewById(R.id.flashView); | |
toggleButtonFlashHardware = (ToggleButton) view.findViewById(R.id.toggleButtonFlashHardware); | |
toggleButtonFlashView = (ToggleButton) view.findViewById(R.id.toggleButtonFlashView); | |
if (seekBar.getProgress() < MIN_VALUE) { | |
seekBar.setProgress(1); | |
} | |
seekBar.setProgress(100); | |
textViewSeekBarValue.setText("100"); | |
seekBar.setMax(1000); | |
turnOffFlashView(); | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | |
mManager = (CameraManager) getActivity().getSystemService(CAMERA_SERVICE); | |
} else { | |
mCamera = Camera.open(); | |
mParams = mCamera.getParameters(); | |
} | |
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { | |
@Override | |
public void onProgressChanged(SeekBar seekBar, int i, boolean b) { | |
textViewSeekBarValue.setText(String.valueOf(seekBar.getProgress())); | |
} | |
@Override | |
public void onStartTrackingTouch(SeekBar seekBar) { | |
} | |
@Override | |
public void onStopTrackingTouch(SeekBar seekBar) { | |
} | |
}); | |
toggleButtonFlashView.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
if (toggleButtonFlashView.isChecked()) { | |
mThread = new Thread(new Runnable() { | |
@Override | |
public void run() { | |
try { | |
Thread.sleep(3000); | |
for (int i = 0; i < editTextCodigo.getText().length(); i++) { | |
if (editTextCodigo.getText().charAt(i) == '0') { | |
getActivity().runOnUiThread(new Runnable() { | |
@Override | |
public void run() { | |
turnOffFlashView(); | |
} | |
}); | |
} else { | |
getActivity().runOnUiThread(new Runnable() { | |
@Override | |
public void run() { | |
turnOnFlashView(); | |
} | |
}); | |
} | |
if (i == editTextCodigo.getText().length()-1) { | |
getActivity().runOnUiThread(new Runnable() { | |
@Override | |
public void run() { | |
toggleButtonFlashView.setChecked(false); | |
turnOffFlashView(); | |
} | |
}); | |
return; | |
} | |
Thread.sleep(Integer.valueOf(textViewSeekBarValue.getText().toString())); | |
} | |
} catch (Exception e) { | |
e.getLocalizedMessage(); | |
} | |
} | |
}); | |
mThread.start(); | |
} else { | |
mThread.interrupt(); | |
turnOffFlashView(); | |
} | |
} | |
}); | |
toggleButtonFlashHardware.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
if (toggleButtonFlashHardware.isChecked()) { | |
long blinkDelay = Integer.valueOf(textViewSeekBarValue.getText().toString()); //Delay in ms | |
for (int i = 0; i < editTextCodigo.getText().length(); i++) { | |
if (editTextCodigo.getText().charAt(i) == '0') { | |
turnOffCameraFlash(); | |
} else { | |
turnOnCameraFlash(); | |
} | |
try { | |
Thread.sleep(blinkDelay); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
if (i == editTextCodigo.getText().length() - 1) | |
toggleButtonFlashHardware.setChecked(false); | |
if (!toggleButtonFlashHardware.isChecked()) { | |
break; | |
} | |
} | |
} else { | |
turnOffCameraFlash(); | |
} | |
} | |
}); | |
return view; | |
} | |
private void turnOnFlashView() { | |
flashView.setBackgroundColor(ContextCompat.getColor(getContext(), android.R.color.white)); | |
} | |
private void turnOffFlashView() { | |
flashView.setBackgroundColor(ContextCompat.getColor(getContext(), android.R.color.black)); | |
} | |
private void turnOnCameraFlash() { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | |
try { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | |
mManager.setTorchMode(getCameraInstanceV2(), true); | |
} | |
} catch (CameraAccessException e) { | |
e.printStackTrace(); | |
} | |
} else { | |
mParams.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH); | |
mCamera.setParameters(mParams); | |
mCamera.startPreview(); | |
} | |
} | |
private void turnOffCameraFlash() { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | |
try { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | |
mManager.setTorchMode(getCameraInstanceV2(), false); | |
} | |
} catch (CameraAccessException e) { | |
e.printStackTrace(); | |
} | |
} else { | |
mParams.setFlashMode(Camera.Parameters.FLASH_MODE_OFF); | |
mCamera.setParameters(mParams); | |
mCamera.stopPreview(); | |
} | |
} | |
/** | |
* Check if this device has a camera | |
*/ | |
private boolean checkCameraFlashHardware() { | |
if (getActivity().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) { | |
// this device has a camera flash | |
return true; | |
} else { | |
// no camera flash on this device | |
return false; | |
} | |
} | |
/** | |
* A safe way to get an instance of the android.hardware.camera2 object. | |
*/ | |
public String getCameraInstanceV2() { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | |
try { | |
for (String cameraId : mManager.getCameraIdList()) { | |
CameraCharacteristics chars = mManager.getCameraCharacteristics(cameraId); | |
if (chars.get(CameraCharacteristics.FLASH_INFO_AVAILABLE)) { | |
// Retorna a primeira camera com flash disponivel | |
return cameraId; | |
} | |
} | |
} catch (CameraAccessException e) { | |
e.printStackTrace(); | |
} | |
} | |
return "-1"; | |
} | |
/** | |
* A safe way to get an instance of the android.hardware.camera object. | |
*/ | |
public static Camera getCameraInstanceV1() { | |
Camera c = null; | |
try { | |
c = Camera.open(); // attempt to get a Camera instance | |
} catch (Exception e) { | |
// Camera is not available (in use or does not exist) | |
} | |
return c; // returns null if camera is unavailable | |
} | |
@Override | |
public void onPause() { | |
super.onPause(); | |
releaseCamera(); // release the camera immediately on pause event | |
} | |
private void releaseCamera() { | |
if (mCamera != null) { | |
mCamera.release(); // release the camera for other applications | |
mCamera = null; | |
} | |
} | |
} |
This file contains hidden or 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
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
> | |
<View | |
android:id="@+id/flashView" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:background="@color/colorAccent" | |
android:layout_above="@+id/textViewSeekBarValue" /> | |
<EditText | |
android:id="@+id/textViewSeekBarValue" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="0.0" | |
android:inputType="numberDecimal" | |
android:layout_margin="10dp" | |
android:textAlignment="center" | |
android:layout_above="@+id/seekBar" | |
/> | |
<SeekBar | |
android:id="@+id/seekBar" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_margin="30dp" | |
android:layout_above="@+id/editTextCodigo" | |
/> | |
<EditText | |
android:id="@+id/editTextCodigo" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_margin="10dp" | |
android:text="01010101010101010101010101010100110011001100110011000111000114000111000111000111000111000111000111000000111111000000111111000000111111000000111111" | |
android:layout_above="@+id/linearLayoutBottom" | |
/> | |
<LinearLayout | |
android:id="@+id/linearLayoutBottom" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginTop="10dp" | |
android:layout_marginBottom="40dp" | |
android:layout_alignParentBottom="true" | |
android:gravity="center" | |
android:orientation="horizontal"> | |
<ToggleButton | |
android:id="@+id/toggleButtonFlashView" | |
android:layout_width="match_parent" | |
android:layout_height="80dp" | |
android:textOn="ON" | |
android:textOff="OFF" | |
android:text="Tela" | |
/> | |
</LinearLayout> | |
</RelativeLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment