Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8"/>
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />
Expand Down
2 changes: 1 addition & 1 deletion project.properties
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
#proguard.config=${sdk.dir}\tools\proguard\proguard-android.txt:proguard-project.txt

# Project target.
target=android-8
target=android-16
208 changes: 129 additions & 79 deletions src/net/multassociais/mobile/MultasSociaisActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Matrix;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
Expand Down Expand Up @@ -72,12 +74,14 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
try {
reduzEComprimeBitmap(f);
Uri contentUri = Uri.fromFile(f);
Bitmap bm = MediaStore.Images.Media.getBitmap(getContentResolver(), contentUri);
Bitmap bm = MediaStore.Images.Media.getBitmap(
getContentResolver(), contentUri);
thumbnailImageView.setImageBitmap(bm);
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
showToast("Descreva a multa!");
Intent mediaScanIntent = new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
showToast("Descreva a multa!");
} catch (IOException e) {
// TODO Auto-generated catch block
imageFilePath = "";
Expand All @@ -88,23 +92,29 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
break;
case ACTIVITY_SELECT_IMAGE:
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
thumbnailImageView.setImageBitmap(BitmapFactory.decodeFile(filePath));
thumbnailImageView.setImageBitmap(BitmapFactory
.decodeFile(filePath));
imageFilePath = filePath;
showToast("Descreva a multa!");
}
}
}

private File criaArquivoDeImagem() {
File storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MultasSociais");
File storageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"MultasSociais");
storageDir.mkdirs();
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());
String imageFileName = "multa_" + timeStamp;
File image = new File(storageDir, imageFileName + ".jpg");
imageFilePath = image.getAbsolutePath();
Expand All @@ -113,20 +123,25 @@ private File criaArquivoDeImagem() {

public void bateFoto(View view) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(criaArquivoDeImagem())); //imageFilePath defined inside createImageFile()
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(criaArquivoDeImagem())); // imageFilePath defined
// inside
// createImageFile()
startActivityForResult(takePictureIntent, TAKE_PICTURE_REQUEST_CODE);
}

public void selecionaFotoDaGaleria(View view) {
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, ACTIVITY_SELECT_IMAGE);
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, ACTIVITY_SELECT_IMAGE);
}

// TODO: catch exception
public void enviaMulta(View view) throws IOException {

//verify if there's an image and description to be sent
String descricao = ((EditText) findViewById(R.id.edt_descricao)).getText().toString();

// verify if there's an image and description to be sent
String descricao = ((EditText) findViewById(R.id.edt_descricao))
.getText().toString();
if (imageFilePath.equals("")) {
showToast("Selecione uma imagem ou use a c�mera!");
return;
Expand All @@ -135,83 +150,92 @@ public void enviaMulta(View view) throws IOException {
showToast("Adicione uma descri��o!");
return;
}

File f = new File(imageFilePath);
Date modifiedDate = new Date(f.lastModified());
String year = new SimpleDateFormat("yyyy").format(modifiedDate);
String month = new SimpleDateFormat("MM").format(modifiedDate);
String day = new SimpleDateFormat("dd").format(modifiedDate);
String hour = new SimpleDateFormat("HH").format(modifiedDate);
String minute = new SimpleDateFormat("mm").format(modifiedDate);
String placa = ((EditText) findViewById(R.id.edt_placa)).getText().toString();

MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
String placa = ((EditText) findViewById(R.id.edt_placa)).getText()
.toString();

MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("api_id", new StringBody("908237202"));
entity.addPart("api_secret", new StringBody("14feefc9725729307649b526bd83x11"));
entity.addPart("api_secret", new StringBody(
"14feefc9725729307649b526bd83x11"));
entity.addPart("multa[data_ocorrencia(1i)]", new StringBody(year));
entity.addPart("multa[data_ocorrencia(2i)]", new StringBody(month));
entity.addPart("multa[data_ocorrencia(3i)]", new StringBody(day));
entity.addPart("multa[data_ocorrencia(4i)]", new StringBody(hour));
entity.addPart("multa[data_ocorrencia(5i)]", new StringBody(minute));
entity.addPart("multa[placa]", new StringBody(placa, Charset.forName("UTF-8")));
entity.addPart("multa[placa]",
new StringBody(placa, Charset.forName("UTF-8")));
entity.addPart("multa[foto]", new FileBody(f));
entity.addPart("multa[video]", new StringBody(""));
entity.addPart("multa[descricao]", new StringBody(descricao, Charset.forName("UTF-8")));
entity.addPart("multa[descricao]",
new StringBody(descricao, Charset.forName("UTF-8")));

WebServiceCallTask task = new WebServiceCallTask();
task.execute(entity);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.about_menu_item:
mostraDialogoSobre();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

public void mostraDialogoSobre(){
final TextView message = new TextView(this);
final SpannableString s = new SpannableString(this.getText(R.string.txt_sobre));
Linkify.addLinks(s, Linkify.WEB_URLS);
message.setText(s);
message.setMovementMethod(LinkMovementMethod.getInstance());

AlertDialog about = new AlertDialog.Builder(this)
.setTitle(R.string.app_name)
.setIcon(android.R.drawable.ic_dialog_info)
.setPositiveButton(this.getString(android.R.string.ok), null)
.setNeutralButton("Deixe sua opini�o!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=net.multassociais.mobile"));
startActivity(intent);
}
})
.setView(message)
.create();
about.show();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.about_menu_item:
mostraDialogoSobre();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

public void mostraDialogoSobre() {
final TextView message = new TextView(this);
final SpannableString s = new SpannableString(
this.getText(R.string.txt_sobre));
Linkify.addLinks(s, Linkify.WEB_URLS);
message.setText(s);
message.setMovementMethod(LinkMovementMethod.getInstance());

AlertDialog about = new AlertDialog.Builder(this)
.setTitle(R.string.app_name)
.setIcon(android.R.drawable.ic_dialog_info)
.setPositiveButton(this.getString(android.R.string.ok), null)
.setNeutralButton("Deixe sua opini�o!",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri
.parse("market://details?id=net.multassociais.mobile"));
startActivity(intent);
}
}).setView(message).create();
about.show();
}

private void showToast(String string) {
Toast feedback;
feedback = Toast.makeText(MultasSociaisActivity.this, string, Toast.LENGTH_SHORT);
feedback = Toast.makeText(MultasSociaisActivity.this, string,
Toast.LENGTH_SHORT);
feedback.show();
}

private void reduzEComprimeBitmap(File imageFile) throws FileNotFoundException, IOException {
private void reduzEComprimeBitmap(File imageFile)
throws FileNotFoundException, IOException {
Uri contentUri = Uri.fromFile(imageFile);
Bitmap bm = MediaStore.Images.Media.getBitmap(getContentResolver(), contentUri);
Bitmap bm = MediaStore.Images.Media.getBitmap(getContentResolver(),
contentUri);
Options opt = new Options();
opt.inPurgeable = true;
opt.inScaled = true;
Expand All @@ -220,18 +244,44 @@ private void reduzEComprimeBitmap(File imageFile) throws FileNotFoundException,
} else if (bm.getWidth() > 1024 || bm.getHeight() > 1024) {
opt.inSampleSize = 2;
}
Bitmap result = BitmapFactory.decodeFile(imageFile.getAbsolutePath(), opt);
Bitmap result = BitmapFactory.decodeFile(imageFile.getAbsolutePath(),
opt);
result = rotateBitmapIfNecessary(result);

FileOutputStream newFile = new FileOutputStream(imageFile);
result.compress(Bitmap.CompressFormat.JPEG, 85, newFile);
}

private class WebServiceCallTask extends AsyncTask<MultipartEntity, Void, Integer> {
Bitmap rotateBitmapIfNecessary(Bitmap bitmapOriginal) {
// note... to avoid OutOfMemory errors, one must first resize the image and than rotate
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
return bitmapOriginal;
}

// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map

matrix.postScale(1, 1);
// rotate the Bitmap
matrix.postRotate(90);

// recreate the new Bitmap
System.gc();
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOriginal, 0, 0,
bitmapOriginal.getWidth(), bitmapOriginal.getHeight(), matrix,
false);
return resizedBitmap;
}

private class WebServiceCallTask extends
AsyncTask<MultipartEntity, Void, Integer> {
ProgressDialog dialog;

@Override
protected void onPreExecute() {
dialog = new ProgressDialog(MultasSociaisActivity.this);

super.onPreExecute();
dialog.setMessage("Enviando...");
dialog.show();
Expand All @@ -241,15 +291,16 @@ protected void onPreExecute() {
protected Integer doInBackground(MultipartEntity... entity) {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost("http://testes.multassociais.net/api");
HttpPost httpPost = new HttpPost(
"http://testes.multassociais.net/api");

httpPost.setEntity(entity[0]);
HttpResponse response;
try {
response = httpClient.execute(httpPost, localContext);
int statusCode = response.getStatusLine().getStatusCode();
return statusCode;

} catch (ClientProtocolException e) {
showToast("Falhou");
cancel(true);
Expand All @@ -260,21 +311,20 @@ protected Integer doInBackground(MultipartEntity... entity) {
return null;
}
}

@Override
protected void onCancelled() {
dialog.dismiss();
super.onCancelled();
}

@Override
protected void onPostExecute(Integer statusCode) {
super.onPostExecute(statusCode);

if (statusCode >= 200 && statusCode < 300) {
showToast("Sucesso");
}
else {
} else {
showToast("Falhou!");
}
dialog.dismiss();
Expand Down