Back to project page ShareList-Android.
The source code is released under:
GNU General Public License
If you think the Android project ShareList-Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package org.mybop.sharelist.app.control; /* ww w.j a v a2 s . c om*/ import android.annotation.SuppressLint; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.Bundle; import android.support.v4.app.NavUtils; import android.support.v7.app.ActionBarActivity; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.Button; import android.widget.EditText; import org.androidannotations.annotations.AfterViews; import org.androidannotations.annotations.EActivity; import org.androidannotations.annotations.Extra; import org.androidannotations.annotations.FragmentById; import org.androidannotations.annotations.InstanceState; import org.androidannotations.annotations.OptionsItem; import org.androidannotations.annotations.OptionsMenu; import org.androidannotations.annotations.OrmLiteDao; import org.mybop.sharelist.app.R; import org.mybop.sharelist.app.database.DatabaseHelper; import org.mybop.sharelist.app.database.entity.ElementEntity; import org.mybop.sharelist.app.database.entity.ListEntity; import org.mybop.sharelist.app.database.repository.ElementRepository; import org.mybop.sharelist.app.database.repository.ListRepository; import org.mybop.sharelist.app.view.DetailFragment; import java.sql.SQLException; import java.util.Arrays; @EActivity(R.layout.activity_detail) @OptionsMenu(R.menu.detail) public class DetailActivity extends ActionBarActivity implements DetailController { private static final String LOGGER_TAG = DetailActivity.class.getSimpleName(); @Extra protected long listId; @Extra protected boolean newlyCreated = false; @OrmLiteDao(helper = DatabaseHelper.class, model = ListEntity.class) protected ListRepository listRepository; @OrmLiteDao(helper = DatabaseHelper.class, model = ElementEntity.class) protected ElementRepository elementRepository; @FragmentById(R.id.detailFragment) protected DetailFragment detailFragment; private ListEntity list; protected AlertDialog editDialog = null; @InstanceState protected boolean editShown = false; @InstanceState protected boolean edition = false; @InstanceState protected long editId = -1; @InstanceState protected String editValue = ""; protected AlertDialog deleteDialog = null; @InstanceState protected boolean deleteShown = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getSupportActionBar().setDisplayHomeAsUpEnabled(true); } @Override protected void onResume() { super.onResume(); if (editShown) { editDialog.show(); } if (deleteShown) { deleteDialog.show(); } } @Override protected void onPause() { super.onPause(); if (editShown) { editDialog.dismiss(); } if (deleteShown) { deleteDialog.dismiss(); } } @AfterViews protected void restoreDialogs() { if (deleteShown) { deleteCompletely(); } if (editShown) { showAddDialog(editValue, edition, editId); } } @AfterViews protected void findList() { try { list = listRepository.queryForId(listId); } catch (SQLException e) { Log.e(LOGGER_TAG, "unable to find the list in db", e); } if (list == null) { setResult(RESULT_CANCELED); finish(); } else { try { detailFragment.setData(list, list.getElementsCursor(), elementRepository.getSelectStarRowMapper()); } catch (SQLException e) { Log.e(LOGGER_TAG, "unable to get the mapper", e); } } } @SuppressLint("InlinedApi") @OptionsItem(android.R.id.home) protected void navigateToHome() { NavUtils.navigateUpTo(this, MainActivity_.intent(this).get()); } @Override public void addNewElement() { showAddDialog(""); } @Override public void updateName(String name) { list.setName(name); try { list.update(); } catch (SQLException e) { Log.e(LOGGER_TAG, "unable to rename list", e); } } @Override public void deleteAll(ElementEntity[] entities) { try { elementRepository.delete(Arrays.asList(entities)); } catch (SQLException e) { Log.e(LOGGER_TAG, "unable to delete elements", e); } detailFragment.updateData(list.getElementsCursor()); } @Override public void updateElementChecked(long id, boolean checked) { ElementEntity element = null; try { element = elementRepository.queryForId(id); } catch (SQLException e) { Log.e(LOGGER_TAG, "unable to retrieve element", e); } if (element != null) { element.setChecked(checked); try { element.update(); } catch (SQLException e) { Log.e(LOGGER_TAG, "unable to save element", e); } } detailFragment.updateData(list.getElementsCursor()); } @Override public boolean isNewlyCreated() { return newlyCreated; } @Override public void deleteCompletely() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(R.string.title_are_you_sure); builder.setMessage(R.string.message_are_you_sure); builder.setCancelable(true); builder.setNegativeButton(R.string.action_cancel, null); builder.setPositiveButton(R.string.action_delete, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { deleteList(); dialog.dismiss(); finish(); } }); builder.setOnCancelListener(new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { deleteShown = false; } }); deleteDialog = builder.create(); deleteDialog.show(); deleteShown = true; } @Override public void edit(final ElementEntity entity) { showAddDialog(entity.getName(), entity.getId()); } private void createNewElement(String elementName) { ElementEntity element = new ElementEntity(elementName, list); list.getElements().add(element); detailFragment.updateData(list.getElementsCursor()); } public void updateElementName(long id, String name) { ElementEntity element = null; try { element = elementRepository.queryForId(id); } catch (SQLException e) { Log.e(LOGGER_TAG, "unable to retrieve element", e); } if (element != null) { element.setName(name); try { element.update(); } catch (SQLException e) { Log.e(LOGGER_TAG, "unable to save element", e); } } detailFragment.updateData(list.getElementsCursor()); } protected void deleteList() { try { listRepository.delete(list); } catch (SQLException e) { Log.e(LOGGER_TAG, "unable to delete list", e); e.printStackTrace(); } } protected void showAddDialog(String name) { showAddDialog(name, false, -1); } protected void showAddDialog(String name, long id) { showAddDialog(name, true, id); } protected void showAddDialog(String name, final boolean edit, final long id) { @SuppressLint("InflateParams") View contentView = getLayoutInflater().inflate(R.layout.dialog_add, null); assert contentView != null; final EditText elementNameEditText = (EditText) contentView.findViewById(R.id.editTextProduitAdd); elementNameEditText.setText(name); elementNameEditText.setSelection(name.length()); elementNameEditText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { editValue = s.toString(); } @Override public void afterTextChanged(Editable s) { } }); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(R.string.title_add_element); builder.setCancelable(true); builder.setNegativeButton(R.string.action_cancel, null); builder.setPositiveButton(edit ? R.string.action_edit : R.string.action_end, null); if (!edit) { builder.setNeutralButton(R.string.action_next, null); } builder.setView(contentView); builder.setOnCancelListener(new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { editShown = false; } }); editDialog = builder.create(); editDialog.setOnShowListener(new DialogInterface.OnShowListener() { @Override public void onShow(final DialogInterface dialog) { Log.d(LOGGER_TAG, "alert dialog show"); InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm.showSoftInput(elementNameEditText, InputMethodManager.SHOW_IMPLICIT); Button positive = editDialog.getButton(ProgressDialog.BUTTON_POSITIVE); assert positive != null; positive.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Editable editable = elementNameEditText.getText(); if (editable != null && editable.length() > 0) { if (edit) { updateElementName(id, editable.toString()); } else { createNewElement(editable.toString()); } } editDialog.cancel(); } }); if (!edit) { Button neutral = editDialog.getButton(ProgressDialog.BUTTON_NEUTRAL); assert neutral != null; neutral.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Editable editable = elementNameEditText.getText(); if (editable != null && editable.length() > 0) { createNewElement(elementNameEditText.getText().toString()); elementNameEditText.setText(""); } } }); } Button negative = editDialog.getButton(ProgressDialog.BUTTON_NEGATIVE); assert negative != null; negative.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { editDialog.cancel(); } }); } }); editDialog.setOnDismissListener(new DialogInterface.OnDismissListener() { @Override public void onDismiss(DialogInterface dialog) { InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm.hideSoftInputFromInputMethod(elementNameEditText.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY); } }); editDialog.show(); editValue = name; editId = id; edition = edit; editShown = true; } }