package com.esri.activity;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.esri.android.map.ags.ArcGISFeatureLayer;
import com.esri.android.map.ags.ArcGISFeatureLayer.MODE;
import com.esri.core.geometry.Point;
import com.esri.core.map.CallbackListener;
import com.esri.core.map.FeatureEditResult;
import com.esri.core.map.FeatureSet;
import com.esri.core.map.Graphic;
import com.esri.core.tasks.ags.query.Query;
import com.esri.core.tasks.ags.query.QueryTask;
import com.esri.geometry.utils.GeometryUtils;
import com.esri.modle.Enterprise;
import com.esri.modle.LawEnforcement;
import com.esri.sql.MySQLiteHelper;
import com.esri.utils.FeatureUtils;
import com.esri.utils.FileOpreateUtils;
public class LawEnforcementActivity extends Activity {
public static final int NONE = 0;
public static final int PHOTOHRAPH = 1;//
public static final int PHOTOZOOM = 2; //
public static final int PHOTORESOULT = 3;//
public static final int CODE_VIDEO = 4;//
public static final String IMAGE_UNSPECIFIED = "image/*";
LawEnforcement law = null;
String filePhotoName = "";
String fileVideoName = "";
Intent intent = null;
Spinner law_spinner = null;
EditText edit_Enterprisename;
EditText enterpris_remarksEdit;
ImageView imageView = null;
boolean isform = false;
ArcGISFeatureLayer featureLayer = null;
private MySQLiteHelper msh = null;//
ProgressDialog prog;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.law_enforcement);
featureLayer = new ArcGISFeatureLayer(FeatureUtils.LAWENFORCEMENT_URL, MODE.SELECTION);
law_spinner = (Spinner)findViewById(R.id.law_spinner);
edit_Enterprisename = (EditText)findViewById(R.id.edit_Enterprisename);
enterpris_remarksEdit = (EditText)findViewById(R.id.enterpris_remarksEdit);
imageView = (ImageView)findViewById(R.id.imgView);
setValues();
setEdites();
msh = new MySQLiteHelper(this,"YDZF");
}
public void onLocation(View v){
law.setEnterpris_illegal_type(((TextView)law_spinner.getSelectedView()).getText().toString());
law.setEnterpris_Name(edit_Enterprisename.getText().toString());
law.setRemarks(enterpris_remarksEdit.getText().toString());
law.setFilePhotoName(filePhotoName);
law.setFileVideoName(fileVideoName);
Bundle bd = new Bundle();
bd.putSerializable("lawenforcement", law);
intent.putExtras(bd);
setResult(1003,intent);
finish();
}
private void setValues(){
ArrayAdapter<CharSequence> spinnerAdapter = ArrayAdapter.createFromResource(this, R.array.violatetypes, android.R.layout.simple_list_item_1);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
law_spinner.setAdapter(spinnerAdapter);
law_spinner.setPromptId(R.string.enterpriseviolatetypes);
law_spinner.setOnItemSelectedListener(spinnerListener);
intent = getIntent();
Bundle bd = intent.getExtras();
law = (LawEnforcement)bd.getSerializable("lawenforcement");
if(intent.getSerializableExtra("geo") != null){
Point geo = (Point)intent.getSerializableExtra("geo");
law.setGeo(geo);
}
// spinner
law_spinner.setSelection(getSelectCode(law.getEnterpris_illegal_type(),spinnerAdapter));
edit_Enterprisename.setText(law.getEnterpris_Name());
enterpris_remarksEdit.setText(law.getRemarks());
filePhotoName = law.getFilePhotoName();
if(filePhotoName != null && !filePhotoName.equals("")){
Bitmap img = BitmapFactory.decodeFile(filePhotoName);
imageView.setImageBitmap(img);
imageView.setLayoutParams(new LayoutParams(100, 100));
}
}
private int getSelectCode(String name,ArrayAdapter<CharSequence> temp){
int postion=0;
if(temp == null){
return 0;
}
for(int i=0;i<temp.getCount();i++){
if(name.equalsIgnoreCase(temp.getItem(i).toString())){
postion = i;
break;
}
}
return postion;
}
private AdapterView.OnItemSelectedListener spinnerListener =new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View v,
int arg2, long arg3) {
// TODO Auto-generated method stub
TextView tv = (TextView)v;
tv.setTextColor(getResources().getColor(R.color.black));
tv.setTextSize(12);
tv.setGravity(Gravity.CENTER_HORIZONTAL);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
};
public void doTakePhoto(View v){
filePhotoName = Environment.getExternalStorageDirectory ().getAbsolutePath()+"/MyMediaStore/";
File out = new File(filePhotoName);
if(!out.exists()){
out.mkdirs();
}
filePhotoName = filePhotoName+new Date().getTime()+".jpg";
out = new File(filePhotoName);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(out));
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
startActivityForResult(intent, PHOTOHRAPH);
}
public void doTakeVideo(View v) throws IOException{
fileVideoName = Environment.getExternalStorageDirectory ().getAbsolutePath()+"/MyMediaStore/";
Intent videoCaptureIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
// File out = new File(fileVideoName);
// if(!out.exists()){
// out.mkdirs();
// }
// out = new File(fileVideoName, new Date().getTime()+".mp4");
// if(!out.exists()){
// out.createNewFile();
// }
// videoCaptureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(out));
videoCaptureIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
videoCaptureIntent.putExtra(MediaStore.EXTRA_DURATION_LIMIT,"2000");
startActivityForResult(videoCaptureIntent, CODE_VIDEO);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == NONE)
return;
//
if (requestCode == PHOTOHRAPH) {
//
// File picture = new File(filePhotoName);
// startPhotoZoom(Uri.fromFile(picture));
Bitmap img = BitmapFactory.decodeFile(filePhotoName);
imageView.setImageBitmap(img);
imageView.setLayoutParams(new LayoutParams(100, 100));
}
if (data == null)
return;
//
if (requestCode == PHOTOZOOM) {
startPhotoZoom(data.getData());
}
//
if (requestCode == PHOTORESOULT) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);// (0 - 100)
imageView.setImageBitmap(photo);
}
}
if(requestCode == CODE_VIDEO)
{
Uri uri = data.getData();
String[] proj = { MediaStore.Video.Media.DATA };
Cursor actualimagecursor = managedQuery(uri,proj,null,null,null);
int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
actualimagecursor.moveToFirst();
String img_path = actualimagecursor.getString(actual_image_column_index);
File file = new File(img_path);
fileVideoName = Environment.getExternalStorageDirectory ().getAbsolutePath()+"/MyMediaStore/";
File out = new File(fileVideoName);
if(!out.exists()){
out.mkdirs();
}
out = new File(fileVideoName, new Date().getTime()+".mp4");
FileOpreateUtils.copyfile(file, out, true);
if(file.exists()){
file.delete();
}
// if(!out.exists()){
// try {
// out.createNewFile();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
//
// FileOutputStream outStream = null;
// try {
// outStream = new FileOutputStream(out);
// outStream.write(videoBytes);
// outStream.close();
// } catch (FileNotFoundException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
}
super.onActivityResult(requestCode, resultCode, data);
}
public void startPhotoZoom(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, IMAGE_UNSPECIFIED);
intent.putExtra("crop", "true");
// aspectX aspectY
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX outputY
intent.putExtra("outputX", 64);
intent.putExtra("outputY", 64);
intent.putExtra("return-data", true);
startActivityForResult(intent, PHOTORESOULT);
}
public void editForm(View v){
setEdites();
}
private void setEdites(){
law_spinner.setEnabled(isform);
//edit_Enterprisename.setEnabled(isform);
enterpris_remarksEdit.setEnabled(isform);
if(isform){
isform =false;
}else{
isform = true;
}
}
public void submitBtn(View v){
Button btn = (Button)v;
btn.setEnabled(false);
if(law.getGeo() == null){
Toast.makeText(LawEnforcementActivity.this, " , !", Toast.LENGTH_LONG).show();
return;
}
SQLiteDatabase db = msh.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("OBJECTID", law.getObjectid());
values.put("ENTERPRIS_NAME", edit_Enterprisename.getText().toString());
values.put("ENTERPRIS_ILLEGAL_TYPE",((TextView)law_spinner.getSelectedView()).getText().toString());
values.put("EXAMINE_PERSONNAME", law.getExamine_personName());
values.put("EQUIPMENT_NUMBER", law.getEquipment_number());
values.put("REMARKS",enterpris_remarksEdit.getText().toString());
values.put("FILEPHOTONAME",filePhotoName);
values.put("FILEVIDEONAME",fileVideoName);
String shape = GeometryUtils.GeometryToWKT(law.getGeo());
values.put("SHAPE", shape);
values.put("MARK", 1);
db.update(MySQLiteHelper.LAW_ENFOREMENT, values, "OBJECTID=?", new String[]{law.getObjectid().toString()});
Map<String,Object> attrs = new HashMap<String, Object>();
attrs.put("OBJECTID", law.getObjectid());
attrs.put("ENTERPRIS_NAME", edit_Enterprisename.getText().toString());
attrs.put("ENTERPRIS_ILLEGAL_TYPE", ((TextView)law_spinner.getSelectedView()).getText().toString());
attrs.put("EXAMINE_PERSONNAME", law.getExamine_personName());
attrs.put("EQUIPMENT_NUMBER",law.getEquipment_number());
attrs.put("REMARKS", enterpris_remarksEdit.getText().toString());
if(law.getGeo() != null){
Point pt = (Point)law.getGeo();
attrs.put("X", pt.getX());
attrs.put("Y", pt.getY());
}
Graphic gp = new Graphic(null, null, attrs,null);
if(filePhotoName != null && !filePhotoName.equals(""))
featureLayer.addAttachment(law.getObjectid(), new File(filePhotoName), new CallbackListener<FeatureEditResult>() {
@Override
public void onCallback(FeatureEditResult objs) {
// TODO Auto-generated method stub
Log.i("image", String.valueOf(objs.isSuccess()));
}
@Override
public void onError(Throwable e) {
// TODO Auto-generated method stub
}
});
//featureLayer.addAttachment(law.getObjectid(), new File(filePhotoName), null);
prog = ProgressDialog.show(LawEnforcementActivity.this, " ", " ....");
prog.setCanceledOnTouchOutside(true);
featureLayer.applyEdits(null, null, new Graphic[] { gp }, createEditCallbackListener(true));
}
CallbackListener<FeatureEditResult[][]> createEditCallbackListener(final boolean updateLayer) {
return new CallbackListener<FeatureEditResult[][]>() {
public void onCallback(FeatureEditResult[][] result) {
if (updateLayer) {
featureLayer.refresh();
LawEnforcementActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
prog.dismiss();
Toast.makeText(LawEnforcementActivity.this, " ", Toast.LENGTH_LONG).show();
finish();
}
});
}
}
public void onError(Throwable e) {
prog.dismiss();
}
};
}
public void enterpriseView(View v){
//featureQuery(FeatureUtils.ENTERPRISE_URL,MySQLiteHelper.ENTERPRISE_EXAMINE);
MyQueryTask myquery = new MyQueryTask();
myquery.execute();
}
private void featureQuery(String url,String tabletemp){
Query query = new Query();
query.setWhere("ENTERPRISE_ID = '"+law.getEnterpries_id()+"' order by objectid asc");
query.setOutFields(new String[] { "*" });
query.setReturnGeometry(false);
//query.setInSpatialReference(temp.getSpatialReference());
QueryTask qTask = new QueryTask(url);
try {
FeatureSet queryResults = qTask.execute(query);
Map<String, Object> attu = null;
if(queryResults != null){
attu = queryResults.getGraphics()[0].getAttributes();
}else{
return ;
}
Intent intent = new Intent();
Enterprise enterprise = new Enterprise();
enterprise.setEnterprise_Name(attu.get("ENTERPRISE_NAME").toString());
if(attu.get("ENTERPRISE_ADDRESS") != null){
enterprise.setEnterprise_Address(attu.get("ENTERPRISE_ADDRESS").toString());
}
if(attu.get("ENTERPRISE_CONTAMINATION")!=null){
enterprise.setEnterprise_contamination(attu.get("ENTERPRISE_CONTAMINATION").toString());
}
if(attu.get("ENTERPRISE_ILLEGAL_RECORDS") != null){
enterprise.setEnterprise_illegal_records(attu.get("ENTERPRISE_ILLEGAL_RECORDS").toString());
}
if(attu.get("ENTERPRISE_LAST_EXAMINE_TIMES") != null){
enterprise.setEnterprise_last_query_times(attu.get("ENTERPRISE_LAST_EXAMINE_TIMES").toString());
}
if(attu.get("ENTERPRISE_LEGAL_PERSON") != null){
enterprise.setEnterprise_legal_person(attu.get("ENTERPRISE_LEGAL_PERSON").toString());
}
Bundle bd = new Bundle();
bd.putSerializable("enterprise", enterprise);
intent.putExtras(bd);
intent.setClass(LawEnforcementActivity.this, EnterpriseActivity.class);
startActivityForResult(intent, 1002);
} catch (Exception e1) {
// TODO Auto-generated catch block
//e1.printStackTrace();
Toast.makeText(LawEnforcementActivity.this, " , ", Toast.LENGTH_LONG);
}
}
class MyQueryTask extends AsyncTask<String, Void, FeatureSet>{
@Override
protected FeatureSet doInBackground(String... params) {
// TODO Auto-generated method stub
featureQuery(FeatureUtils.ENTERPRISE_URL,MySQLiteHelper.ENTERPRISE_EXAMINE);
return null;
}
@Override
protected void onPostExecute(FeatureSet result) {
// TODO Auto-generated method stub
prog.dismiss();
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stube
prog = ProgressDialog.show(LawEnforcementActivity.this, " ", " ....");
}
}
}