Java tutorial
/* * Cos android client for Cozy Cloud * * Copyright (C) 2016 Hamza Abdelkebir * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package eu.codeplumbers.cosi.activities; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.os.Bundle; import android.provider.MediaStore; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.EditText; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.Toast; import org.json.JSONException; import java.util.Date; import eu.codeplumbers.cosi.R; import eu.codeplumbers.cosi.api.tasks.DeleteDocumentTask; import eu.codeplumbers.cosi.api.tasks.SyncDocumentTask; import eu.codeplumbers.cosi.db.models.Device; import eu.codeplumbers.cosi.db.models.Expense; import eu.codeplumbers.cosi.db.models.Receipt; import eu.codeplumbers.cosi.utils.Constants; import eu.codeplumbers.cosi.utils.DateUtils; import eu.codeplumbers.cosi.utils.FileUtils; public class ExpenseDetailsActivity extends AppCompatActivity { private Expense expense; private EditText expenseDate; private EditText expenseLabel; private EditText expenseAmount; private EditText expenseCategory; private ImageButton expenseReceipt; private Receipt currentReceipt; private Bitmap photo; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_expense_details); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setDisplayShowHomeEnabled(true); expenseDate = (EditText) findViewById(R.id.expenseDate); expenseLabel = (EditText) findViewById(R.id.expenseLabel); expenseAmount = (EditText) findViewById(R.id.expenseAmount); expenseCategory = (EditText) findViewById(R.id.expenseCategory); expenseReceipt = (ImageButton) findViewById(R.id.expenseReceipt); expenseReceipt.setScaleType(ImageView.ScaleType.FIT_CENTER); expenseReceipt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); ((Activity) ExpenseDetailsActivity.this).startActivityForResult(intent, Constants.REQUEST_RECEIPT_CAPTURE); } }); long expenseId = getIntent().getLongExtra("expenseId", -1); if (expenseId != -1) { expense = Expense.load(Expense.class, expenseId); if (expense != null) { expenseDate.setText(expense.getDate()); expenseLabel.setText(expense.getLabel()); expenseAmount.setText(expense.getAmount() + ""); expenseCategory.setText(expense.getCategory()); currentReceipt = Receipt.getByExpense(expense); if (currentReceipt != null) { expenseReceipt.setImageBitmap(FileUtils.base64ToBitmap(currentReceipt.getBase64())); } getSupportActionBar().setSubtitle("Expense details"); } else { Toast.makeText(this, "Something went terribly wrong... Can't load note information", Toast.LENGTH_LONG).show(); } } else { getSupportActionBar().setSubtitle(getString(R.string.lbl_expenses_new)); expenseDate.setText(DateUtils.formatDate(new Date().getTime())); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_expense_details, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_save_expense) { saveExpenseAndSync(); return true; } if (id == R.id.action_delete_expense) { deleteExpenseAndSync(); return true; } return super.onOptionsItemSelected(item); } private void deleteExpenseAndSync() { if (expense != null && !expense.getRemoteId().equalsIgnoreCase("")) new DeleteDocumentTask(expense.getRemoteId(), this).execute(); else onExpenseDeleted(""); } private void saveExpenseAndSync() { String m_label = expenseLabel.getText().toString(); String m_amount = expenseAmount.getText().toString(); String m_category = expenseCategory.getText().toString(); if (m_label.isEmpty()) { expenseLabel.setError(getString(R.string.lbl_expenses_label_empty)); } if (m_amount.isEmpty()) { expenseAmount.setError(getString(R.string.lbl_expenses_amount_empty)); } if (m_category.isEmpty()) { m_category = ""; } if (!m_label.isEmpty() && !m_amount.isEmpty()) { if (expense == null) { expense = new Expense(); expense.setDate(expenseDate.getText().toString()); expense.setRemoteId(""); expense.setDeviceId(Device.registeredDevice().getLogin()); } expense.setCategory(expenseCategory.getText().toString().isEmpty() ? "Uncategorized" : expenseCategory.getText().toString()); expense.setLabel(expenseLabel.getText().toString()); expense.setAmount(Double.valueOf(expenseAmount.getText().toString())); expense.save(); if (photo != null) { currentReceipt.setBase64(FileUtils.bitmapToBase64(photo)); currentReceipt.setExpense(expense); currentReceipt.save(); } try { new SyncDocumentTask(ExpenseDetailsActivity.this).execute(expense.toJsonObject()); } catch (JSONException e) { e.printStackTrace(); } } } protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK && requestCode == Constants.REQUEST_RECEIPT_CAPTURE) { photo = (Bitmap) data.getExtras().get("data"); expenseReceipt.setImageBitmap(photo); if (currentReceipt == null) currentReceipt = new Receipt(); } } public void onExpenseSynced(String result) { expense.setRemoteId(result); expense.save(); Snackbar.make(expenseLabel, "Expense synced successfully!", Snackbar.LENGTH_LONG).show(); } public void onExpenseDeleted(String result) { Receipt.deleteRelatedReceipts(expense); expense.delete(); Snackbar.make(expenseLabel, "Expense deleted from Cozy!", Snackbar.LENGTH_LONG).show(); finish(); } }