Back to project page sana.
The source code is released under:
Copyright (c) 2010, Moca All rights reserved. The source code for Moca is licensed under the BSD license as follows: Redistribution and use in source and binary forms, with or without modification, ...
If you think the Android project sana listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package org.moca.procedure; //from w w w .j av a2 s .c o m import java.io.IOException; import java.util.ArrayList; import org.moca.ImagePreviewDialog; import org.moca.ScalingImageAdapter; import org.moca.activity.ProcedureRunner; import org.moca.db.MocaDB; import org.moca.db.MocaDB.ImageSQLFormat; import org.w3c.dom.Node; import android.app.Activity; import android.content.ContentUris; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.util.Log; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.Button; import android.widget.Gallery; import android.widget.GridView; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemLongClickListener; import android.widget.LinearLayout.LayoutParams; /** * PictureElement is a ProcedureElement that allows a user to take photos. It displays * thumbnails of images that have been taken, allows a user to bring up the camera to take * new ones, and allows a user to click on a thumbnail to bring up a dialog allowing for * closer image review and examination. */ public class PictureElement extends ProcedureElement implements OnClickListener, OnItemClickListener, OnItemLongClickListener { public static final String PARAMS_NAME = "keys"; private ScalingImageAdapter imageAdapter; private static final int THUMBNAIL_SCALE_FACTOR = 5; private Button cameraButton; private GridView imageGrid; private ImagePreviewDialog imageReview; private Intent imageCaptureIntent; @Override public ElementType getType() { return ElementType.PICTURE; } /** * Do not call this method! It's called /if/ PictureElement becomes activated for display. */ @Override protected View createView(Context c) { imageGrid = new GridView(c); String procedureId = getProcedure().getInstanceUri().getPathSegments().get(1); String whereStr = ImageSQLFormat.SAVED_PROCEDURE_ID + " = ? AND " + ImageSQLFormat.ELEMENT_ID + " = ? AND " + ImageSQLFormat.FILE_VALID + " = ?"; Cursor cursor = c.getContentResolver().query( MocaDB.ImageSQLFormat.CONTENT_URI, new String[] { ImageSQLFormat._ID }, whereStr, new String[] { procedureId, id, "1" }, null); // HAXMODE -- if we don't do this we leak the Cursor if (c instanceof Activity) { ((Activity)c).startManagingCursor(cursor); } imageAdapter = new ScalingImageAdapter(c, cursor, THUMBNAIL_SCALE_FACTOR); imageGrid.setAdapter(imageAdapter); imageGrid.setNumColumns(3); imageGrid.setVerticalSpacing(5); imageGrid.setPadding(5, 0, 0, 0); imageGrid.setOnItemClickListener(this); imageGrid.setOnItemLongClickListener(this); //imageGrid.setTranscriptMode(imageGrid.TRANSCRIPT_MODE_ALWAYS_SCROLL); cameraButton = new Button(c); cameraButton.setText("Add Picture"); cameraButton.setOnClickListener(this); imageReview = new ImagePreviewDialog(c); LinearLayout picContainer = new LinearLayout(c); picContainer.setOrientation(LinearLayout.VERTICAL); if(question == null) { question = "Attach a picture:"; } //Set question TextView tv = new TextView(c); tv.setText(question); tv.setGravity(Gravity.CENTER); tv.setTextAppearance(c, android.R.style.TextAppearance_Medium); //Add to layout picContainer.addView(tv, new LinearLayout.LayoutParams(-1,-1,0.1f)); //picContainer.addView(imageView, new LinearLayout.LayoutParams(-1,-1,0.1f)); //Add button picContainer.addView(cameraButton, new LinearLayout.LayoutParams(-1,-1,0.1f)); picContainer.addView(imageGrid, new LinearLayout.LayoutParams(-1, 210)); //LayoutParams(-1,-1,0.8f)); picContainer.setWeightSum(1.0f); return picContainer; } // Sends an intent to ProcedureRunner with the image // parameters. The native camera app is then called from // ProcedureRunner. public void onClick(View v) { if (v == cameraButton) { String procedureId = getProcedure().getInstanceUri().getPathSegments().get(1); //which procedure its part of String[] params = {procedureId, id}; imageCaptureIntent = new Intent(getContext(), ProcedureRunner.class); imageCaptureIntent.putExtra(PARAMS_NAME, params) .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP) .putExtra("intentKey", 0); ((Activity) getContext()).startActivity(imageCaptureIntent); } } public void onItemClick(AdapterView<?> parent, View v, int position, long id) { Log.i(TAG, "onItemClick pos:" + position + " id:" + id); imageAdapter.toggleSelection(id); //v.postInvalidate(); v.invalidate(); //SelectableImageView view = (SelectableImageView) v; //view.toggleMultiSelected(); //view.invalidate(); //iadapter.notifyDataSetChanged(); //parent.invalidate(); } /* (non-Javadoc) * @see android.widget.AdapterView.OnItemLongClickListener#onItemLongClick(android.widget.AdapterView, android.view.View, int, long) */ /** * If a thumbnail is clicked, open up an image preview dialog. */ public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) { Log.i(TAG, "" + position); long imageId = imageAdapter.getItemId(position); Uri imageUri = ContentUris.withAppendedId(ImageSQLFormat.CONTENT_URI, imageId); try { // hack for in-emulator demo, commented out here, but use to load a // static image // imageReview.showImage(getContext().getContentResolver().openInputStream(Uri.parse("android.resource://org.moca/" // + R.drawable.incision2))); imageReview.showImage(imageUri); imageReview.show(); } catch (IOException e) { Log.e(TAG, "Can't open the image file for uri " + imageUri); } return false; } public void setAnswer(String answer) { if(!isViewActive()) { this.answer = answer; } else { // TODO : Fix this so that it works! We have the id # of the picture, and we need to reset 'selected' to match this such that // iadapter.getItemId(selected) == the answer we have here. String[] ids = answer.split(","); for(String id : ids) { imageAdapter.setSelected(Long.parseLong(id), true); } } } public String getAnswer() { // old stuff from single selection if(!isViewActive()) return answer; // Log.i(TAG, "Putting answer " + selected); // return iadapter.getItemId(selected) + ""; ArrayList<String> answerz = new ArrayList<String>(); /*for(Entry<Long,SelectableImageView> e : iadapter.getViewMap().entrySet()) { Log.i(TAG, "getAnswer -- item " + e.getKey()); if(e.getValue().isMultiSelected()) { answerz.add(Long.toString(e.getKey())); Log.i(TAG, "getAnswer -- item selected" + e.getKey()); } }*/ for (int i = 0; i < imageAdapter.getCount(); i++) { Long id = imageAdapter.getItemId(i); Log.i(TAG, "Considering element " + id + " for selection."); if (imageAdapter.isSelected(id)) { Log.i(TAG, "Element " + id + " is selected."); answerz.add(Long.toString(id)); } } if (answerz.size() > 0) { StringBuilder csv = new StringBuilder(answerz.get(0)); for (int i=1; i<answerz.size(); i++) { csv.append(","); csv.append(answerz.get(i)); } Log.i(TAG, "getAnswers is returning " + csv.toString()); return csv.toString(); } else { Log.i(TAG, "getAnswers is returning blank"); return ""; } } /** * Make question and response into an XML string for storing or transmission. */ public void buildXML(StringBuilder sb) { sb.append("<Element type=\"" + getType().name() + "\" id=\"" + id); sb.append("\" question=\"" + question); sb.append("\" answer=\"" + getAnswer()); sb.append("\" concept=\"" + getConcept()); sb.append("\"/>\n"); } private PictureElement(String id, String question, String answer, String concept, String figure, String audio) { super(id, question, answer, concept, figure, audio); } /** * Create a PictureElement from an XML procedure definition. */ public static PictureElement fromXML(String id, String question, String answer, String concept, String figure, String audio, Node node) { return new PictureElement(id, question, answer, concept, figure, audio); } }