Skip to content
This repository has been archived by the owner on Jul 19, 2024. It is now read-only.

Commit

Permalink
Project Oxford App Mimicker Alarm Release - January 28th 2016 Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Team Mimicker committed Feb 4, 2016
1 parent 92c2630 commit 588a4a5
Show file tree
Hide file tree
Showing 29 changed files with 368 additions and 50 deletions.
5 changes: 3 additions & 2 deletions Mimicker/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ android {
applicationId "com.microsoft.mimickeralarm"
minSdkVersion 16
targetSdkVersion 22
versionCode 2
versionCode 6
versionName "1.0"
}

Expand All @@ -31,6 +31,7 @@ android {
}

debug.setRoot('build-types/debug')
debug.res.srcDirs = ['src/debug/res']
release.setRoot('build-types/release')
}

Expand Down Expand Up @@ -114,4 +115,4 @@ task fortify << {
}

println "Scan completed!"
}
}
6 changes: 6 additions & 0 deletions Mimicker/app/src/debug/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#FF0000</color>
<color name="colorPrimaryDark">#FF0000</color>
<color name="colorAccent">#FF0000</color>
</resources>
4 changes: 4 additions & 0 deletions Mimicker/app/src/debug/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<resources>
<string name="app_name">Mimicker Alarm Debug</string>
<string name="app_short_name">Mimicker Debug</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import android.graphics.YuvImage;
import android.hardware.Camera;
import android.os.AsyncTask;
import android.os.Handler;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
Expand All @@ -63,15 +64,15 @@
* image captured by the camera
*
* To use this, pass in
* a ImageCallback to process the image returned,
* a CapturedImageCallbackAsync to process the image returned,
* an aspect ratio to use. The class will find the camera setting that best fits this aspect ratio,
* a camera facing (Front or Back)
*
* Public methods:
* initPreview (initialize the camera and the preview surface),
* start (call initPreview before),
* stop,
* onCapture (set the ImageCallback),
* onCapture (set the CapturedImageCallbackAsync),
* onFocus (focus the camera at a certain x, y position)
*/
@SuppressWarnings("deprecation")
Expand All @@ -84,21 +85,60 @@ public class CameraPreview implements SurfaceHolder.Callback {
private int mCameraFacing;
private int mCameraRotation;
private double mCameraAspectRatio;
private ImageCallback mCallbackOnCaptured;
private Boolean mIsFlashSupported; // we only want torch mode. Boolean type so we can cache the result
private boolean mFlashState;
private FlashStateCallback mFlashStateCallback;
private CapturedImageCallbackAsync mCapturedCapturedImageCallbackAsync;
private CameraInitializedCallback mCameraInitializedCallback;

private Camera.PreviewCallback mCaptureCallback = new Camera.PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera camera) {
camera.stopPreview();
if (mIsFlashSupported) {
// Delay turning off flash for 0.5s to allow camera to capture image
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
changeFlashState(false);
if (mFlashStateCallback != null) {
mFlashStateCallback.execute(false);
}
}
}, 500);
}
new processCaptureImage().execute(data, camera);
}
};

public CameraPreview(SurfaceView surfaceView, double aspectRatio, int facing) {
public interface CapturedImageCallbackAsync {
void execute(Bitmap bitmap);
}

public interface CameraInitializedCallback {
void execute(boolean success);
}

public interface FlashStateCallback {
void execute(boolean state);
}

private OnCameraPreviewException mOnException;

public CameraPreview(SurfaceView surfaceView,
OnCameraPreviewException onException,
CameraInitializedCallback onCameraInitialized,
double aspectRatio, int facing) {
mCameraAspectRatio = aspectRatio;
mCameraFacing = facing;
mPreviewView = surfaceView;
mCameraRotation = 0;
mIsFlashSupported = null;
mFlashState = false;
final SurfaceHolder surfaceHolder = mPreviewView.getHolder();
surfaceHolder.addCallback(this);
mOnException = onException;
mCameraInitializedCallback = onCameraInitialized;
}

@Override
Expand All @@ -112,32 +152,41 @@ public void surfaceChanged(SurfaceHolder holder, int format, int width, int heig
@Override
public void surfaceDestroyed(SurfaceHolder holder) {}

public void initPreview() {
private void initPreview(){
if (mCamera == null) {
mCamera = getNewCamera();
}
try {
mCamera.setPreviewDisplay(mPreviewView.getHolder());
if (mCamera != null) {
mCamera.setPreviewDisplay(mPreviewView.getHolder());
}
} catch (IOException e) {
Logger.trackException(e);
}
}

public void stop() {
if (mCamera != null) {
changeFlashState(false);
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}
public void start() {
public void start() throws MimicException{
if (mCamera != null) {
mCamera.startPreview();
}
else{
MimicException e = new MimicException("failed to open camera");
Logger.trackException(e);
throw e;
}
}

public void onCapture(ImageCallback callback) {
mCallbackOnCaptured = callback;
public void onCapture(CapturedImageCallbackAsync callback, FlashStateCallback flashCallback) {
mCapturedCapturedImageCallbackAsync = callback;
mFlashStateCallback = flashCallback;
if (mCamera != null) {
mCamera.setOneShotPreviewCallback(mCaptureCallback);
}
Expand All @@ -150,23 +199,101 @@ public void onFocus(int x, int y) {
rotateMatrix.mapRect(focusRectF);
Rect focusRect = new Rect();
focusRectF.round(focusRect);
if (mCamera == null){
return;
}
try {
Camera.Parameters parameters = mCamera.getParameters();
if (parameters == null)
{
return;
}
if (parameters.getMaxNumFocusAreas() > 0) {
List<Camera.Area> focusAreas = new ArrayList<>();
focusAreas.add(new Camera.Area(focusRect, 1000));
parameters.setFocusAreas(focusAreas);

mCamera.cancelAutoFocus();
mCamera.setParameters(parameters);
mCamera.autoFocus(new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean success, Camera camera) {
}
});
}
} catch (Exception e) {
Logger.trackException(e);
}
}

public boolean isFlashSupported() {
if (mIsFlashSupported != null) {
return mIsFlashSupported;
}
// use mIsFlashSupported to cache;
mIsFlashSupported = false;

// Currently let's limit flash to back camera only
if (mCameraFacing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
return false;
}

if (mCamera == null) {
return false;
}

Camera.Parameters parameters = mCamera.getParameters();
if (parameters == null) {
return false;
}
List<String> flashModes = parameters.getSupportedFlashModes();
if (flashModes != null && !flashModes.isEmpty()){
if (flashModes.contains(Camera.Parameters.FLASH_MODE_TORCH)) {
mIsFlashSupported = true;
}
}

return mIsFlashSupported;
}

public boolean changeFlashState(boolean turnOn) {
if (mFlashState == turnOn) {
return true;
}

if (mCamera == null) {
return false;
}

if (!isFlashSupported()) {
return false;
}

Camera.Parameters parameters = mCamera.getParameters();
if (parameters.getMaxNumFocusAreas() > 0) {
List<Camera.Area> focusAreas = new ArrayList<>();
focusAreas.add(new Camera.Area(focusRect, 1000));
parameters.setFocusAreas(focusAreas);
if (parameters == null) {
return false;
}

try {
mCamera.cancelAutoFocus();
if (turnOn) {
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
}
else {
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
}
mCamera.setParameters(parameters);
mCamera.autoFocus(new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean success, Camera camera) {}
});
} catch (Exception e) {
}
catch (Exception e) {
Logger.trackException(e);
return false;
}

mFlashState = turnOn;
return true;
}

public boolean getFlashState() {
return mFlashState;
}

private Camera getNewCamera() {
Expand Down Expand Up @@ -237,22 +364,41 @@ private Camera getNewCamera() {
return cam;
}

public interface ImageCallback {
void run(Bitmap bitmap);
}

private class OpenCameraTask extends AsyncTask<Object, String, Boolean> {
@Override
protected Boolean doInBackground(Object... params) {
if (mCamera != null) {
try {
initPreview();
} catch (Exception ex) {
Log.e(LOGTAG, "err starting camera preview", ex);
Logger.trackException(ex);
if(mCamera == null) {
initPreview();
}

boolean success = false;
try {
start();
success = true;
}
catch (MimicException ex) {
Logger.trackException(ex);
}
catch (Exception ex) {
Log.e(LOGTAG, "err starting camera preview", ex);
Logger.trackException(ex);
}
return success;
}

@Override
protected void onPostExecute(Boolean success) {
super.onPostExecute(success);
if (!success) {
if (mOnException != null) {
mOnException.execute();
}
}
else {
if (mCameraInitializedCallback != null) {
mCameraInitializedCallback.execute(true);
}
}
return null;
}
}

Expand Down Expand Up @@ -285,12 +431,16 @@ protected Boolean doInBackground(Object... params) {
transform.postRotate(mCameraRotation);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), transform, true);

if (mCallbackOnCaptured != null) {
mCallbackOnCaptured.run(bitmap);
if (mCapturedCapturedImageCallbackAsync != null) {
mCapturedCapturedImageCallbackAsync.execute(bitmap);
}
}
return null;
}
}

public interface OnCameraPreviewException{
void execute();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ public interface IMimicImplementation {
void onCountDownTimerExpired();
void onSucceeded();
void onFailed();
void onInternalError();
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public interface IMimicMediator {
void onMimicSuccess(String successMessage);
void onMimicFailureWithRetry(String failureMessage);
void onMimicFailure(String failureMessage);
void onMimicInternalError();

void registerStateBanner(MimicStateBanner mimicStateBanner);
void registerCountDownTimer(CountDownTimerView countDownTimerView, int timeout);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.microsoft.mimickeralarm.mimics;

public class MimicException extends Exception {
public MimicException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,20 @@ private static boolean isNetworkAvailable(Activity caller) {
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

public static Fragment getNoNetworkMimic(Activity caller) {
Fragment fragment = null;
try {
fragment = MimicNoNetworkFragment.class.newInstance();
} catch (Exception e) {
Log.e(TAG, "Couldn't create fragment:", e);
Logger.trackException(e);
}
return fragment;
}

public interface MimicResultListener {
void onMimicSuccess(String shareable);

void onMimicFailure();
void onMimicError();
}
}
Loading

0 comments on commit 588a4a5

Please sign in to comment.