package com.rubenvandeven.emotionhero; import android.Manifest; import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.content.pm.PackageManager; import android.media.AudioAttributes; import android.media.AudioManager; import android.media.SoundPool; import android.os.Build; import android.support.v4.app.ActivityCompat; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.os.Handler; import android.util.Log; import android.view.MotionEvent; import android.view.SurfaceView; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; import android.widget.Button; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; import com.affectiva.android.affdex.sdk.Frame; import com.affectiva.android.affdex.sdk.detector.CameraDetector; import com.affectiva.android.affdex.sdk.detector.Detector; import com.affectiva.android.affdex.sdk.detector.Face; import java.util.HashMap; import java.util.List; /** * An example full-screen activity that shows and hides the system UI (i.e. * status bar and navigation/system bar) with user interaction. */ public class GamingActivity extends AppCompatActivity implements Detector.ImageListener, CameraDetector.CameraEventListener, Detector.FaceListener { final static String LOG_TAG = "EmotionHero"; final int PERMISSIONS_REQUEST_CAMERA = 1; private TextView mContentView; private CameraDetector detector; SurfaceView cameraPreview; int previewWidth = 0; int previewHeight = 0; Scenario currentScenario; ScenarioView scenarioView; boolean has_camera_permission = false; Button restartButton; public SoundPool sound; public HashMap soundIds = new HashMap<>(); final static int SOUND_SCORE = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); getSupportActionBar().hide(); setContentView(R.layout.activity_gaming); mContentView = (TextView) findViewById(R.id.fullscreen_content); RelativeLayout videoLayout = (RelativeLayout) findViewById(R.id.video_layout); restartButton = (Button) findViewById(R.id.restartButton); restartButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { finish(); startActivity(getIntent()); } }); // Set up the user interaction to manually show or hide the system UI. mContentView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // toggle(); } }); //We create a custom SurfaceView that resizes itself to match the aspect ratio of the incoming camera frames cameraPreview = new SurfaceView(this) { @Override public void onMeasure(int widthSpec, int heightSpec) { int measureWidth = MeasureSpec.getSize(widthSpec); int measureHeight = MeasureSpec.getSize(heightSpec); int width; int height; if (previewHeight == 0 || previewWidth == 0) { width = measureWidth; height = measureHeight; } else { float viewAspectRatio = (float)measureWidth/measureHeight; float cameraPreviewAspectRatio = (float) previewWidth/previewHeight; if (cameraPreviewAspectRatio > viewAspectRatio) { width = measureWidth; height =(int) (measureWidth / cameraPreviewAspectRatio); } else { width = (int) (measureHeight * cameraPreviewAspectRatio); height = measureHeight; } } setMeasuredDimension(width,height); // setMeasuredDimension(1,1); // this DOES increase performance.... } }; RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); // RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(1,1); params.addRule(RelativeLayout.CENTER_IN_PARENT,RelativeLayout.TRUE); cameraPreview.setLayoutParams(params); videoLayout.addView(cameraPreview,0); currentScenario = new ScenarioAnger(this); scenarioView = new ScenarioView(this, currentScenario); RelativeLayout.LayoutParams scenarioViewParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); videoLayout.addView(scenarioView, 1, scenarioViewParams); createSoundPool(); // instantiate SoundPool in sound soundIds.put(SOUND_SCORE, sound.load(this, R.raw.score2, 1)); } @Override protected void onResume() { super.onResume(); startDetector(); } @Override protected void onPause() { super.onPause(); stopDetector(); } void startDetector() { if (detector == null || !detector.isRunning()) { // check permission String permission = "android.permission.CAMERA"; int res = getApplicationContext().checkCallingOrSelfPermission(permission); if (res == PackageManager.PERMISSION_GRANTED) { Log.e(LOG_TAG, "HAS PERM"); has_camera_permission = true; } else { Log.e(LOG_TAG, "NO PERM"); ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, PERMISSIONS_REQUEST_CAMERA); } if(has_camera_permission) { if(detector == null) { // SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surfaceView); detector = new CameraDetector(this, CameraDetector.CameraType.CAMERA_FRONT, cameraPreview, 1, Detector.FaceDetectorMode.LARGE_FACES); detector.setLicensePath("emotionhero_dev.license"); detector.setMaxProcessRate(10); detector.setDetectAllEmotions(true); detector.setDetectAllAppearances(false); detector.setDetectAllEmojis(false); detector.setDetectAllExpressions(false); detector.setMaxProcessRate(20); detector.setImageListener(this); detector.setOnCameraEventListener(this); detector.setFaceListener(this); } detector.start(); setText("STARTING..."); Log.d(LOG_TAG, Boolean.toString(detector.isRunning())); } } } void stopDetector() { if (detector != null && detector.isRunning()) { detector.stop(); } } @Override /** * Detector callback gives the faces found so we can match their scores to the given scenario. */ public void onImageResults(List list, Frame frame, float timestamp) { // frame.getOriginalBitmapFrame() // Log.e(LOG_TAG, "RESULT! faces: " + Integer.toString(list.size()) + " t: " + Float.toString(timestamp) + "s" ); // if(!currentScenario.isWithinTime(timestamp)) if(currentScenario.isFinished()) { setText(String.format("LEVEL ENDED\nScore: %.2f", currentScenario.getTotalScore())); stopDetector(); restartButton.setVisibility(View.VISIBLE); return; } if (list == null) return; if (list.size() == 0) { // mContentView.setText("NO FACE FOUND"); // this happens in onFaceDetectionStopped } else { hideText(); // hide textView as we want as few elements as possible. Face face = list.get(0); currentScenario.setCurrentAttributeScoresForFace(face); scenarioView.setCurrentAttributeScoresForFace(face); // String paramString = ""; // paramString += "Anger " + String.format("%02.2f", face.emotions.getAnger()) + "%\n"; // paramString += "Contempt " + String.format("%02.2f", face.emotions.getContempt()) + "%\n"; // paramString += "Disgust " + String.format("%02.2f", face.emotions.getDisgust()) + "%\n"; // paramString += "Fear " + String.format("%02.2f", face.emotions.getFear()) + "%\n"; // paramString += "Joy " + String.format("%02.2f", face.emotions.getJoy()) + "%\n"; // paramString += "Sadness " + String.format("%02.2f", face.emotions.getSadness()) + "%\n"; // paramString += "Surprise " + String.format("%02.2f", face.emotions.getSurprise()) + "%\n"; // // paramText.setText(paramString); } } @Override /** * For CameraDetector.CameraEventListener * Used to scale video preview output */ public void onCameraSizeSelected(int width, int height, Frame.ROTATE rotate) { if (rotate == Frame.ROTATE.BY_90_CCW || rotate == Frame.ROTATE.BY_90_CW) { previewWidth = height; previewHeight = width; } else { previewHeight = height; previewWidth = width; } cameraPreview.requestLayout(); } @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case PERMISSIONS_REQUEST_CAMERA: { // If request is cancelled, the result arrays are empty. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { has_camera_permission = true; startDetector(); } else { has_camera_permission = false; Toast errorMsg = Toast.makeText(this, R.string.camera_required, Toast.LENGTH_LONG); errorMsg.show(); } return; } // other 'case' lines to check for other // permissions this app might request } } @Override public void onFaceDetectionStarted() { setText("START!"); currentScenario.start(); } @Override public void onFaceDetectionStopped() { setText("No face found..."); currentScenario.pause(); } public void setText(String text) { mContentView.setVisibility(View.VISIBLE); mContentView.setText(text); } public void hideText() { mContentView.setVisibility(View.GONE); } // http://stackoverflow.com/a/27552576 protected void createSoundPool() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { createNewSoundPool(); } else { createOldSoundPool(); } } // http://stackoverflow.com/a/27552576 @TargetApi(Build.VERSION_CODES.LOLLIPOP) protected void createNewSoundPool(){ AudioAttributes attributes = new AudioAttributes.Builder() .setUsage(AudioAttributes.USAGE_GAME) .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) .build(); sound = new SoundPool.Builder() .setAudioAttributes(attributes) .build(); } // http://stackoverflow.com/a/27552576 @SuppressWarnings("deprecation") protected void createOldSoundPool(){ sound = new SoundPool(5, AudioManager.STREAM_MUSIC,0); } }