Commit c126466b authored by Josejulio Martínez Magaña's avatar Josejulio Martínez Magaña

Merge branch 'take-picture-refactoring' into 'develop'

Take picture refactoring

See merge request !2
parents 0f35f231 6c47e90f
......@@ -79,7 +79,7 @@ public class EarCaptureActivity extends Activity {
*/
getImage = (Button)this.findViewById(R.id.extra_image_value);
getImage = (Button)this.findViewById(R.id.take_picture);
getImage.setOnClickListener(new OnClickListener() {
private Toast loginInCommCareToast = null;
......
......@@ -2,59 +2,155 @@ package com.aluxoft.earrecognition.intents;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import com.aluxoft.earrecognition.activities.SelectFollowupActivity;
import com.aluxoft.earrecognition.EarIdentifier;
import com.aluxoft.earrecognition.R;
import com.aluxoft.earrecognition.common.EarIdList;
import com.aluxoft.earrecognition.loader.EarDataLoaderCommcare;
import com.aluxoft.earrecognition.utils.FileCache;
import com.aluxoft.earrecognition.utils.ImageUtils;
import com.google.gson.Gson;
import org.apache.commons.io.FileUtils;
import java.util.concurrent.Callable;
import bolts.Continuation;
import bolts.Task;
import java.io.File;
import java.io.IOException;
/**
* Callout to transfer features to CommCare
*/
public class IntentTransferingFeatures extends Activity {
public static final int KEY_REQUEST_IMAGE = 1;
public static final int KEY_SIFT_ACTIVITY_LOADER = 2;
File location;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ProgressDialog loading = ProgressDialog.show(IntentTransferingFeatures.this,
"Transfering", "Please wait while transfering the features...");
loading.setCancelable(false);
loading.setProgressStyle(ProgressDialog.STYLE_SPINNER);
Task.callInBackground(new Callable<Object>() {
@Override
public Object call() throws Exception {
synchronized (this) {
wait(1000);
FileCache imageCache = FileCache.getFile(this.getApplicationContext(), FileCache.FileCacheType.Image);
FileCache featuresCache = FileCache.getFile(this.getApplicationContext(), FileCache.FileCacheType.Features);
if (imageCache.isValid() && featuresCache.isValid()) {
setContentView(R.layout.activity_callout_with_features);
} else {
setContentView(R.layout.activity_callout);
}
if (this.findViewById(R.id.current_picture) != null) {
final Button button = (Button) this.findViewById(R.id.current_picture);
ImageView image = (ImageView) this.findViewById(R.id.image);
image.setImageDrawable(Drawable.createFromPath(imageCache.getAbsolutePath()));
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
Intent returningIntent = new Intent(getIntent());
returningIntent.putExtra(
"odk_intent_data",
FileUtils.readFileToString(
FileCache
.getFile(IntentTransferingFeatures.this.getApplicationContext(),
FileCache.FileCacheType.Features)
)
);
IntentTransferingFeatures.this.setResult(Activity.RESULT_OK, returningIntent);
finish();
} catch (Exception e) {
Toast.makeText(IntentTransferingFeatures.this, "An error occurred when loading the features.", Toast.LENGTH_LONG);
button.setEnabled(false);
}
}
});
}
this.findViewById(R.id.take_picture).setOnClickListener(new View.OnClickListener() {
private Toast loginInCommCareToast = null;
public void onClick(View v) {
if (!EarDataLoaderCommcare.checkConnection(IntentTransferingFeatures.this)) {
if (loginInCommCareToast == null || loginInCommCareToast.getView().getWindowVisibility() != View.VISIBLE) {
loginInCommCareToast = Toast.makeText(IntentTransferingFeatures.this,
"Please login in CommCare application to proceed.",
Toast.LENGTH_LONG);
loginInCommCareToast.show();
}
return;
}
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
try {
location = ImageUtils.createTempImageFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return;
}
// if this gets modified, the onActivityResult in
// FormEntyActivity will also need to be updated.
i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(location));
try {
startActivityForResult(i, KEY_REQUEST_IMAGE);
} catch (ActivityNotFoundException e) {
Toast.makeText(IntentTransferingFeatures.this, "No Camera", Toast.LENGTH_SHORT).show();
}
return null;
}
}).continueWith(new Continuation<Object, Object>() {
@Override
public Object then(Task<Object> task) throws Exception {
Intent returningIntent = new Intent(getIntent());
returningIntent.putExtra(
"odk_intent_data",
FileUtils.readFileToString(
FileCache
.getFile(IntentTransferingFeatures.this.getApplicationContext(),
FileCache.FileCacheType.Features)
)
);
IntentTransferingFeatures.this.setResult(Activity.RESULT_OK, returningIntent);
finish();
return null;
});
if(this.getLastNonConfigurationInstance() != null) {
location = ((IntentTransferingFeatures)this.getLastNonConfigurationInstance()).location;
}
if(savedInstanceState != null) {
location = new File(savedInstanceState.getString("location"));
}
}
/* (non-Javadoc)
* @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == KEY_REQUEST_IMAGE) {
//Go grab the image and set it's location
if(location == null || !location.exists()) {
location = null;
} else {
String path = location.getAbsolutePath();
EarIdentifier identifier = new EarIdentifier(null);
Intent intent = new Intent("com.auriclon.activity_sift");
intent.putExtra("image_path", path);
startActivityForResult(intent, KEY_SIFT_ACTIVITY_LOADER);
}
}, Task.UI_THREAD_EXECUTOR);
} else if (requestCode == KEY_SIFT_ACTIVITY_LOADER) {
String list = data.getStringExtra("list");
EarIdList earIdList = new Gson().fromJson(list, EarIdList.class);
Intent returningIntent = new Intent(getIntent());
returningIntent.putExtra(
"odk_intent_data",
earIdList.getCurrentFeature().serializeFeatures()
);
IntentTransferingFeatures.this.setResult(Activity.RESULT_OK, returningIntent);
finish();
}
}
}
......@@ -5,14 +5,13 @@
android:orientation="vertical" >
<Button
android:id="@+id/extra_image_value"
android:id="@+id/take_picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@mipmap/ic_ear"
android:text="Identify ear"
android:layout_gravity="center_horizontal"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:shadowColor="#000000" />
android:layout_centerHorizontal="true" />
</RelativeLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:orientation="horizontal"
android:background="@android:drawable/bottom_bar"
android:paddingLeft="4.0dip"
android:paddingTop="5.0dip"
android:paddingRight="4.0dip"
android:paddingBottom="1.0dip"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:id="@+id/linearLayout">
<Button
android:id="@+id/take_picture"
android:drawableTop="@mipmap/ic_ear"
android:text="Take ear picture"
android:layout_width="0.0dip" android:layout_height="fill_parent"
android:layout_weight="1.0"
/>
<Button
android:id="@+id/current_picture"
android:text="Use current picture"
android:layout_width="0.0dip" android:layout_height="fill_parent"
android:layout_weight="1.0" />
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image"
android:layout_below="@+id/linearLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment