diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 49fc047..1724da4 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -4,7 +4,7 @@ android:versionCode="1" android:versionName="1.0" > - + diff --git a/project.properties b/project.properties old mode 100755 new mode 100644 index a026ea8..5383d8e --- a/project.properties +++ b/project.properties @@ -11,4 +11,4 @@ #proguard.config=${sdk.dir}\tools\proguard\proguard-android.txt:proguard-project.txt # Project target. -target=android-8 +target=android-16 diff --git a/src/net/multassociais/mobile/MultasSociaisActivity.java b/src/net/multassociais/mobile/MultasSociaisActivity.java index 8d33c8e..2eced37 100644 --- a/src/net/multassociais/mobile/MultasSociaisActivity.java +++ b/src/net/multassociais/mobile/MultasSociaisActivity.java @@ -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; @@ -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 = ""; @@ -88,13 +92,15 @@ 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!"); } @@ -102,9 +108,13 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) { } 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(); @@ -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; @@ -135,7 +150,7 @@ 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); @@ -143,75 +158,84 @@ public void enviaMulta(View view) throws IOException { 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; @@ -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 { + 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 { ProgressDialog dialog; - + @Override protected void onPreExecute() { dialog = new ProgressDialog(MultasSociaisActivity.this); - + super.onPreExecute(); dialog.setMessage("Enviando..."); dialog.show(); @@ -241,7 +291,8 @@ 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; @@ -249,7 +300,7 @@ protected Integer doInBackground(MultipartEntity... entity) { response = httpClient.execute(httpPost, localContext); int statusCode = response.getStatusLine().getStatusCode(); return statusCode; - + } catch (ClientProtocolException e) { showToast("Falhou"); cancel(true); @@ -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();