From 8c10d8999f748f77a3bf0c208306d9c99ed0180e Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Mon, 5 May 2014 01:11:48 -0400 Subject: make errors go away --- src/us/minak/SettingsActivity.java | 246 +++++++++---------------------------- 1 file changed, 57 insertions(+), 189 deletions(-) (limited to 'src/us/minak/SettingsActivity.java') diff --git a/src/us/minak/SettingsActivity.java b/src/us/minak/SettingsActivity.java index 30b1e7b..4d6eca8 100644 --- a/src/us/minak/SettingsActivity.java +++ b/src/us/minak/SettingsActivity.java @@ -16,8 +16,6 @@ package us.minak; -import android.app.Dialog; -import android.app.AlertDialog; import android.app.ListActivity; import android.os.Bundle; import android.os.AsyncTask; @@ -30,15 +28,12 @@ import android.view.ViewGroup; import android.gesture.Gesture; import android.gesture.GestureLibrary; import android.widget.TextView; -import android.widget.EditText; import android.widget.AdapterView; import android.widget.Toast; import android.widget.ArrayAdapter; -import android.content.DialogInterface; import android.content.Context; import android.content.Intent; import android.content.res.Resources; -import android.text.TextUtils; import android.graphics.Bitmap; import android.graphics.drawable.Drawable; import android.graphics.drawable.BitmapDrawable; @@ -47,7 +42,6 @@ import java.util.Map; import java.util.Collections; import java.util.HashMap; import java.util.Comparator; -import java.util.Set; public class SettingsActivity extends ListActivity { private static final int STATUS_SUCCESS = 0; @@ -55,15 +49,9 @@ public class SettingsActivity extends ListActivity { private static final int STATUS_NO_STORAGE = 2; private static final int STATUS_NOT_LOADED = 3; - private static final int MENU_ID_RENAME = 1; - private static final int MENU_ID_REMOVE = 2; - - private static final int DIALOG_RENAME_GESTURE = 1; + private static final int MENU_ID_REMOVE = 1; private static final int REQUEST_NEW_GESTURE = 1; - - // Type: long (id) - private static final String GESTURES_INFO_ID = "gestures.info_id"; private final Comparator mSorter = new Comparator() { public int compare(NamedGesture object1, NamedGesture object2) { @@ -73,11 +61,9 @@ public class SettingsActivity extends ListActivity { private GesturesAdapter mAdapter; private GesturesLoadTask mTask; - private TextView mEmpty; + private TextView mEmptyMessageView; - private Dialog mRenameDialog; - private EditText mInput; - private NamedGesture mCurrentRenameGesture; + // Hacky constructor to get 'this' out of scope /////////////////////////// private SettingsActivity mThis; public SettingsActivity() { @@ -85,25 +71,53 @@ public class SettingsActivity extends ListActivity { mThis = this; } + /////////////////////////////////////////////////////////////////////////// + + private void loadGestures() { + if (mTask != null && mTask.getStatus() != GesturesLoadTask.Status.FINISHED) { + mTask.cancel(true); + } + mTask = (GesturesLoadTask) new GesturesLoadTask().execute(); + } + + private void checkForEmpty() { + if (mAdapter.getCount() == 0) { + mEmptyMessageView.setText(R.string.gestures_empty); + } + } + + // Basic life-cycle /////////////////////////////////////////////////////// + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setContentView(R.layout.gestures_list); + setListAdapter(mAdapter = new GesturesAdapter(this)); - mAdapter = new GesturesAdapter(this); - setListAdapter(mAdapter); - - mEmpty = (TextView) findViewById(android.R.id.empty); + mEmptyMessageView = (TextView) findViewById(android.R.id.empty); loadGestures(); registerForContextMenu(getListView()); } + @Override + protected void onDestroy() { + super.onDestroy(); + + if (mTask != null && mTask.getStatus() != GesturesLoadTask.Status.FINISHED) { + mTask.cancel(true); + mTask = null; + } + } + + // The buttons at the bottom ////////////////////////////////////////////// + + /** Called by onClick */ public void reloadGestures(View v) { loadGestures(); } - + + /** Called by onClick */ public void addGesture(View v) { Intent intent = new Intent(this, CreateGestureActivity.class); startActivityForResult(intent, REQUEST_NEW_GESTURE); @@ -112,7 +126,7 @@ public class SettingsActivity extends ListActivity { @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); - + if (resultCode == RESULT_OK) { switch (requestCode) { case REQUEST_NEW_GESTURE: @@ -122,70 +136,15 @@ public class SettingsActivity extends ListActivity { } } - private void loadGestures() { - if (mTask != null && mTask.getStatus() != GesturesLoadTask.Status.FINISHED) { - mTask.cancel(true); - } - mTask = (GesturesLoadTask) new GesturesLoadTask().execute(); - } - - @Override - protected void onDestroy() { - super.onDestroy(); - - if (mTask != null && mTask.getStatus() != GesturesLoadTask.Status.FINISHED) { - mTask.cancel(true); - mTask = null; - } - - cleanupRenameDialog(); - } - - private void checkForEmpty() { - if (mAdapter.getCount() == 0) { - mEmpty.setText(R.string.gestures_empty); - } - } + // Context menu /////////////////////////////////////////////////////////// @Override - protected void onSaveInstanceState(Bundle outState) { - super.onSaveInstanceState(outState); - - if (mCurrentRenameGesture != null) { - outState.putLong(GESTURES_INFO_ID, mCurrentRenameGesture.gesture.getID()); - } - } - - @Override - protected void onRestoreInstanceState(Bundle state) { - super.onRestoreInstanceState(state); - - long id = state.getLong(GESTURES_INFO_ID, -1); - if (id != -1) { - final Set entries = SettingsUtil.getGestureLibrary(this).getGestureEntries(); -out: for (String name : entries) { - for (Gesture gesture : SettingsUtil.getGestureLibrary(this).getGestures(name)) { - if (gesture.getID() == id) { - mCurrentRenameGesture = new NamedGesture(); - mCurrentRenameGesture.name = name; - mCurrentRenameGesture.gesture = gesture; - break out; - } - } - } - } - } - - @Override - public void onCreateContextMenu(ContextMenu menu, View v, - ContextMenu.ContextMenuInfo menuInfo) { - + public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo; - menu.setHeaderTitle(((TextView) info.targetView).getText()); - menu.add(0, MENU_ID_RENAME, 0, R.string.gestures_rename); + menu.setHeaderTitle(((TextView) info.targetView).getText()); menu.add(0, MENU_ID_REMOVE, 0, R.string.gestures_delete); } @@ -196,115 +155,24 @@ out: for (String name : entries) { final NamedGesture gesture = (NamedGesture) menuInfo.targetView.getTag(); switch (item.getItemId()) { - case MENU_ID_RENAME: - renameGesture(gesture); - return true; case MENU_ID_REMOVE: - deleteGesture(gesture); - return true; - } - - return super.onContextItemSelected(item); - } - - private void renameGesture(NamedGesture gesture) { - mCurrentRenameGesture = gesture; - showDialog(DIALOG_RENAME_GESTURE); - } - - @Override - protected Dialog onCreateDialog(int id) { - if (id == DIALOG_RENAME_GESTURE) { - return createRenameDialog(); - } - return super.onCreateDialog(id); - } + SettingsUtil.getGestureLibrary(this).removeGesture(gesture.name, gesture.gesture); + SettingsUtil.getGestureLibrary(this).save(); - @Override - protected void onPrepareDialog(int id, Dialog dialog) { - super.onPrepareDialog(id, dialog); - if (id == DIALOG_RENAME_GESTURE) { - mInput.setText(mCurrentRenameGesture.name); - } - } - - private Dialog createRenameDialog() { - final View layout = View.inflate(this, R.layout.dialog_rename, null); - mInput = (EditText) layout.findViewById(R.id.name); - ((TextView) layout.findViewById(R.id.label)).setText(R.string.gestures_rename_label); - - AlertDialog.Builder builder = new AlertDialog.Builder(this); - builder.setIcon(0); - builder.setTitle(getString(R.string.gestures_rename_title)); - builder.setCancelable(true); - builder.setOnCancelListener(new Dialog.OnCancelListener() { - public void onCancel(DialogInterface dialog) { - cleanupRenameDialog(); - } - }); - builder.setNegativeButton(getString(R.string.cancel_action), - new Dialog.OnClickListener() { - public void onClick(DialogInterface dialog, int which) { - cleanupRenameDialog(); - } - } - ); - builder.setPositiveButton(getString(R.string.rename_action), - new Dialog.OnClickListener() { - public void onClick(DialogInterface dialog, int which) { - changeGestureName(); - } - } - ); - builder.setView(layout); - return builder.create(); - } - - private void changeGestureName() { - final String name = mInput.getText().toString(); - if (!TextUtils.isEmpty(name)) { - final NamedGesture renameGesture = mCurrentRenameGesture; - final GesturesAdapter adapter = mAdapter; - final int count = adapter.getCount(); - - // Simple linear search, there should not be enough items to warrant - // a more sophisticated search - for (int i = 0; i < count; i++) { - final NamedGesture gesture = adapter.getItem(i); - if (gesture.gesture.getID() == renameGesture.gesture.getID()) { - SettingsUtil.getGestureLibrary(this).removeGesture(gesture.name, gesture.gesture); - gesture.name = mInput.getText().toString(); - SettingsUtil.getGestureLibrary(this).addGesture(gesture.name, gesture.gesture); - break; - } - } + mAdapter.setNotifyOnChange(false); + mAdapter.remove(gesture); + mAdapter.sort(mSorter); + checkForEmpty(); + mAdapter.notifyDataSetChanged(); - adapter.notifyDataSetChanged(); + Toast.makeText(this, R.string.gestures_delete_success, Toast.LENGTH_SHORT).show(); + return true; } - mCurrentRenameGesture = null; - } - private void cleanupRenameDialog() { - if (mRenameDialog != null) { - mRenameDialog.dismiss(); - mRenameDialog = null; - } - mCurrentRenameGesture = null; + return super.onContextItemSelected(item); } - private void deleteGesture(NamedGesture gesture) { - SettingsUtil.getGestureLibrary(this).removeGesture(gesture.name, gesture.gesture); - SettingsUtil.getGestureLibrary(this).save(); - - final GesturesAdapter adapter = mAdapter; - adapter.setNotifyOnChange(false); - adapter.remove(gesture); - adapter.sort(mSorter); - checkForEmpty(); - adapter.notifyDataSetChanged(); - - Toast.makeText(this, R.string.gestures_delete_success, Toast.LENGTH_SHORT).show(); - } + /////////////////////////////////////////////////////////////////////////// private class GesturesLoadTask extends AsyncTask { private int mThumbnailSize; @@ -322,8 +190,8 @@ out: for (String name : entries) { findViewById(R.id.addButton).setEnabled(false); findViewById(R.id.reloadButton).setEnabled(false); - - mAdapter.setNotifyOnChange(false); + + mAdapter.setNotifyOnChange(false); mAdapter.clear(); } @@ -379,8 +247,8 @@ out: for (String name : entries) { if (result == STATUS_NO_STORAGE) { getListView().setVisibility(View.GONE); - mEmpty.setVisibility(View.VISIBLE); - mEmpty.setText(getString(R.string.gestures_error_loading, + mEmptyMessageView.setVisibility(View.VISIBLE); + mEmptyMessageView.setText(getString(R.string.gestures_error_loading, SettingsUtil.getGestureFile(mThis).getAbsolutePath())); } else { findViewById(R.id.addButton).setEnabled(true); @@ -406,7 +274,7 @@ out: for (String name : entries) { } void addBitmap(Long id, Bitmap bitmap) { - mThumbnails.put(id, new BitmapDrawable(bitmap)); + mThumbnails.put(id, new BitmapDrawable(getResources(), bitmap)); } @Override -- cgit v1.2.3