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.content.DialogInterface; import android.content.Intent; import android.graphics.Color; import android.support.design.widget.Snackbar; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.webkit.WebView; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.Toast; import org.json.JSONArray; import org.json.JSONException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.Locale; import java.util.TimeZone; 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.Note; import eu.codeplumbers.cosi.utils.DateUtils; import jp.wasabeef.richeditor.RichEditor; import static android.R.attr.password; public class NoteDetailsActivity extends AppCompatActivity { private Note note; private RichEditor body; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_note_details); body = (RichEditor) findViewById(R.id.body); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setDisplayShowHomeEnabled(true); body.setPlaceholder("Insert text here..."); body.setEnabled(true); setupEditor(); long noteId = getIntent().getLongExtra("noteId", -1); if (noteId != -1) { note = Note.load(Note.class, noteId); if (note != null) { body.setHtml(note.getContent()); getSupportActionBar().setSubtitle("Note details"); } else { Toast.makeText(this, "Something went terribly wrong... Can't load note information", Toast.LENGTH_LONG).show(); } } else { getSupportActionBar().setSubtitle("New note"); } } @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_note_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_note) { saveNoteAndSync(); return true; } if (id == R.id.action_delete_note) { deleteNoteAndSync(); return true; } return super.onOptionsItemSelected(item); } private void deleteNoteAndSync() { new DeleteDocumentTask(note.getRemoteId(), this).execute(); } private void saveNoteAndSync() { SimpleDateFormat isoFormat = new SimpleDateFormat(DateUtils.DATE_FORMAT, Locale.FRANCE); isoFormat.setTimeZone(TimeZone.getTimeZone("UTC")); Date date = new Date(); if (note == null) { note = new Note(); note.setCreationDate(isoFormat.format(date)); note.setVersion(0); note.setParentId("tree-node-all"); note.setRemoteId(""); } else { note.setVersion(note.getVersion() + 1); } getNoteTitle(isoFormat, date); } private void getNoteTitle(final SimpleDateFormat isoFormat, final Date date) { AlertDialog.Builder alertDialog = new AlertDialog.Builder(NoteDetailsActivity.this); alertDialog.setTitle("Title"); alertDialog.setMessage("Enter note title"); final EditText input = new EditText(NoteDetailsActivity.this); if (note.getTitle() != "") { input.setText(note.getTitle()); } LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); input.setLayoutParams(lp); alertDialog.setView(input); alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { String title = input.getText().toString(); note.setTitle(title); ArrayList<String> list = new ArrayList<String>(); list.add(note.getTitle()); note.setPath(new JSONArray(list).toString()); note.setLastModificationDate(isoFormat.format(date)); note.setLastModificationValueOf(String.valueOf(date.getTime())); note.setContent(body.getHtml()); note.save(); try { new SyncDocumentTask(NoteDetailsActivity.this).execute(note.toJsonObject()); } catch (JSONException e) { e.printStackTrace(); } } }); alertDialog.show(); } private void setupEditor() { findViewById(R.id.action_undo).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { body.undo(); } }); findViewById(R.id.action_redo).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { body.redo(); } }); findViewById(R.id.action_bold).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { body.setBold(); } }); findViewById(R.id.action_italic).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { body.setItalic(); } }); findViewById(R.id.action_subscript).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { body.setSubscript(); } }); findViewById(R.id.action_superscript).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { body.setSuperscript(); } }); findViewById(R.id.action_strikethrough).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { body.setStrikeThrough(); } }); findViewById(R.id.action_underline).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { body.setUnderline(); } }); findViewById(R.id.action_heading1).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { body.setHeading(1); } }); findViewById(R.id.action_heading2).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { body.setHeading(2); } }); findViewById(R.id.action_heading3).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { body.setHeading(3); } }); findViewById(R.id.action_heading4).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { body.setHeading(4); } }); findViewById(R.id.action_heading5).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { body.setHeading(5); } }); findViewById(R.id.action_heading6).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { body.setHeading(6); } }); findViewById(R.id.action_txt_color).setOnClickListener(new View.OnClickListener() { private boolean isChanged; @Override public void onClick(View v) { body.setTextColor(isChanged ? Color.BLACK : Color.RED); isChanged = !isChanged; } }); findViewById(R.id.action_bg_color).setOnClickListener(new View.OnClickListener() { private boolean isChanged; @Override public void onClick(View v) { body.setTextBackgroundColor(isChanged ? Color.TRANSPARENT : Color.YELLOW); isChanged = !isChanged; } }); findViewById(R.id.action_indent).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { body.setIndent(); } }); findViewById(R.id.action_outdent).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { body.setOutdent(); } }); findViewById(R.id.action_align_left).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { body.setAlignLeft(); } }); findViewById(R.id.action_align_center).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { body.setAlignCenter(); } }); findViewById(R.id.action_align_right).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { body.setAlignRight(); } }); findViewById(R.id.action_blockquote).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { body.setBlockquote(); } }); findViewById(R.id.action_insert_bullets).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { body.setBullets(); } }); findViewById(R.id.action_insert_numbers).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { body.setNumbers(); } }); findViewById(R.id.action_insert_image).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { body.insertImage("http://www.1honeywan.com/dachshund/image/7.21/7.21_3_thumb.JPG", "dachshund"); } }); findViewById(R.id.action_insert_link).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { body.insertLink("https://github.com/wasabeef", "wasabeef"); } }); findViewById(R.id.action_insert_checkbox).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { body.insertTodo(); } }); } public void onNoteSynced(String result) { note.setRemoteId(result); note.save(); Snackbar.make(body, "Note synced successfully!", Snackbar.LENGTH_LONG).show(); } public void onNoteDeleted(String result) { note.delete(); Snackbar.make(body, "Note deleted from Cozy!", Snackbar.LENGTH_LONG).show(); finish(); } }