From 718bef72f9f674151580a1babe237fb6fdeb8266 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Mon, 5 May 2014 23:39:25 -0400 Subject: Whitespace and formatting --- src/us/minak/SettingsActivity.java | 515 +++++++++++++++++++------------------ 1 file changed, 266 insertions(+), 249 deletions(-) (limited to 'src/us/minak/SettingsActivity.java') diff --git a/src/us/minak/SettingsActivity.java b/src/us/minak/SettingsActivity.java index c052e6c..a3dea5c 100644 --- a/src/us/minak/SettingsActivity.java +++ b/src/us/minak/SettingsActivity.java @@ -44,253 +44,270 @@ import java.util.HashMap; import java.util.Comparator; public class SettingsActivity extends ListActivity { - private static final int STATUS_SUCCESS = 0; - private static final int STATUS_CANCELLED = 1; - private static final int STATUS_NO_STORAGE = 2; - private static final int STATUS_NOT_LOADED = 3; - - private static final int MENU_ID_REMOVE = 1; - - private static final int REQUEST_NEW_GESTURE = 1; - - private final Comparator mSorter = new Comparator() { - public int compare(NamedGesture object1, NamedGesture object2) { - return object1.name.compareTo(object2.name); - } - }; - - private GesturesAdapter mAdapter; - private GesturesLoadTask mTask; - private TextView mEmptyMessageView; - - // Hacky constructor to get 'this' out of scope /////////////////////////// - - private SettingsActivity mThis; - public SettingsActivity() { - super(); - 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)); - - 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, SettingsCreateGestureActivity.class); - startActivityForResult(intent, REQUEST_NEW_GESTURE); - } - - @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: - loadGestures(); - break; - } - } - } - - // Context menu /////////////////////////////////////////////////////////// - - @Override - 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_REMOVE, 0, R.string.gestures_delete); - } - - @Override - public boolean onContextItemSelected(MenuItem item) { - final AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) - item.getMenuInfo(); - final NamedGesture gesture = (NamedGesture) menuInfo.targetView.getTag(); - - switch (item.getItemId()) { - case MENU_ID_REMOVE: - SettingsUtil.getGestureLibrary(this).removeGesture(gesture.name, gesture.gesture); - SettingsUtil.getGestureLibrary(this).save(); - - mAdapter.setNotifyOnChange(false); - mAdapter.remove(gesture); - mAdapter.sort(mSorter); - checkForEmpty(); - mAdapter.notifyDataSetChanged(); - - Toast.makeText(this, R.string.gestures_delete_success, Toast.LENGTH_SHORT).show(); - return true; - } - - return super.onContextItemSelected(item); - } - - /////////////////////////////////////////////////////////////////////////// - - private class GesturesLoadTask extends AsyncTask { - private int mThumbnailSize; - private int mThumbnailInset; - private int mPathColor; - - @Override - protected void onPreExecute() { - super.onPreExecute(); - - final Resources resources = getResources(); - mPathColor = resources.getColor(R.color.gesture_color); - mThumbnailInset = (int) resources.getDimension(R.dimen.gesture_thumbnail_inset); - mThumbnailSize = (int) resources.getDimension(R.dimen.gesture_thumbnail_size); - - findViewById(R.id.addButton).setEnabled(false); - findViewById(R.id.reloadButton).setEnabled(false); - - mAdapter.setNotifyOnChange(false); - mAdapter.clear(); - } - - @Override - protected Integer doInBackground(Void... params) { - if (isCancelled()) return STATUS_CANCELLED; - if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { - return STATUS_NO_STORAGE; - } - - final GestureLibrary store = SettingsUtil.getGestureLibrary(mThis); - - if (store.load()) { - for (String name : store.getGestureEntries()) { - if (isCancelled()) break; - - for (Gesture gesture : store.getGestures(name)) { - final Bitmap bitmap = gesture.toBitmap(mThumbnailSize, mThumbnailSize, - mThumbnailInset, mPathColor); - final NamedGesture namedGesture = new NamedGesture(); - namedGesture.gesture = gesture; - namedGesture.name = name; - - mAdapter.addBitmap(namedGesture.gesture.getID(), bitmap); - publishProgress(namedGesture); - } - } - - return STATUS_SUCCESS; - } - - return STATUS_NOT_LOADED; - } - - @Override - protected void onProgressUpdate(NamedGesture... values) { - super.onProgressUpdate(values); - - final GesturesAdapter adapter = mAdapter; - adapter.setNotifyOnChange(false); - - for (NamedGesture gesture : values) { - adapter.add(gesture); - } - - adapter.sort(mSorter); - adapter.notifyDataSetChanged(); - } - - @Override - protected void onPostExecute(Integer result) { - super.onPostExecute(result); - - if (result == STATUS_NO_STORAGE) { - getListView().setVisibility(View.GONE); - mEmptyMessageView.setVisibility(View.VISIBLE); - mEmptyMessageView.setText(getString(R.string.gestures_error_loading, SettingsUtil.getGestureFile(mThis).getAbsolutePath())); - } else { - findViewById(R.id.addButton).setEnabled(true); - findViewById(R.id.reloadButton).setEnabled(true); - checkForEmpty(); - } - } - } - - static class NamedGesture { - String name; - Gesture gesture; - } - - private class GesturesAdapter extends ArrayAdapter { - private final LayoutInflater mInflater; - private final Map mThumbnails = Collections.synchronizedMap( - new HashMap()); - - public GesturesAdapter(Context context) { - super(context, 0); - mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); - } - - void addBitmap(Long id, Bitmap bitmap) { - mThumbnails.put(id, new BitmapDrawable(getResources(), bitmap)); - } - - @Override - public View getView(int position, View convertView, ViewGroup parent) { - if (convertView == null) { - convertView = mInflater.inflate(R.layout.gestures_item, parent, false); - } - - final NamedGesture gesture = getItem(position); - final TextView label = (TextView) convertView; - - label.setTag(gesture); - label.setText(gesture.name); - label.setCompoundDrawablesWithIntrinsicBounds(mThumbnails.get(gesture.gesture.getID()), - null, null, null); - - return convertView; - } - } + private static final int STATUS_SUCCESS = 0; + private static final int STATUS_CANCELLED = 1; + private static final int STATUS_NO_STORAGE = 2; + private static final int STATUS_NOT_LOADED = 3; + + private static final int MENU_ID_REMOVE = 1; + + private static final int REQUEST_NEW_GESTURE = 1; + + private final Comparator mSorter = new Comparator() { + public int compare(NamedGesture object1, NamedGesture object2) { + return object1.name.compareTo(object2.name); + } + }; + + private GesturesAdapter mAdapter; + private GesturesLoadTask mTask; + private TextView mEmptyMessageView; + + // Hacky constructor to get 'this' out of scope /////////////////////////// + + private SettingsActivity mThis; + + public SettingsActivity() { + super(); + 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)); + + 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, SettingsCreateGestureActivity.class); + startActivityForResult(intent, REQUEST_NEW_GESTURE); + } + + @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: + loadGestures(); + break; + } + } + } + + // Context menu /////////////////////////////////////////////////////////// + + @Override + 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_REMOVE, 0, R.string.gestures_delete); + } + + @Override + public boolean onContextItemSelected(MenuItem item) { + final AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) item + .getMenuInfo(); + final NamedGesture gesture = (NamedGesture) menuInfo.targetView + .getTag(); + + switch (item.getItemId()) { + case MENU_ID_REMOVE: + SettingsUtil.getGestureLibrary(this).removeGesture(gesture.name, + gesture.gesture); + SettingsUtil.getGestureLibrary(this).save(); + + mAdapter.setNotifyOnChange(false); + mAdapter.remove(gesture); + mAdapter.sort(mSorter); + checkForEmpty(); + mAdapter.notifyDataSetChanged(); + + Toast.makeText(this, R.string.gestures_delete_success, + Toast.LENGTH_SHORT).show(); + return true; + } + + return super.onContextItemSelected(item); + } + + // ///////////////////////////////////////////////////////////////////////// + + private class GesturesLoadTask extends + AsyncTask { + private int mThumbnailSize; + private int mThumbnailInset; + private int mPathColor; + + @Override + protected void onPreExecute() { + super.onPreExecute(); + + final Resources resources = getResources(); + mPathColor = resources.getColor(R.color.gesture_color); + mThumbnailInset = (int) resources + .getDimension(R.dimen.gesture_thumbnail_inset); + mThumbnailSize = (int) resources + .getDimension(R.dimen.gesture_thumbnail_size); + + findViewById(R.id.addButton).setEnabled(false); + findViewById(R.id.reloadButton).setEnabled(false); + + mAdapter.setNotifyOnChange(false); + mAdapter.clear(); + } + + @Override + protected Integer doInBackground(Void... params) { + if (isCancelled()) + return STATUS_CANCELLED; + if (!Environment.MEDIA_MOUNTED.equals(Environment + .getExternalStorageState())) { + return STATUS_NO_STORAGE; + } + + final GestureLibrary store = SettingsUtil.getGestureLibrary(mThis); + + if (store.load()) { + for (String name : store.getGestureEntries()) { + if (isCancelled()) + break; + + for (Gesture gesture : store.getGestures(name)) { + final Bitmap bitmap = gesture.toBitmap(mThumbnailSize, + mThumbnailSize, mThumbnailInset, mPathColor); + final NamedGesture namedGesture = new NamedGesture(); + namedGesture.gesture = gesture; + namedGesture.name = name; + + mAdapter.addBitmap(namedGesture.gesture.getID(), bitmap); + publishProgress(namedGesture); + } + } + + return STATUS_SUCCESS; + } + + return STATUS_NOT_LOADED; + } + + @Override + protected void onProgressUpdate(NamedGesture... values) { + super.onProgressUpdate(values); + + final GesturesAdapter adapter = mAdapter; + adapter.setNotifyOnChange(false); + + for (NamedGesture gesture : values) { + adapter.add(gesture); + } + + adapter.sort(mSorter); + adapter.notifyDataSetChanged(); + } + + @Override + protected void onPostExecute(Integer result) { + super.onPostExecute(result); + + if (result == STATUS_NO_STORAGE) { + getListView().setVisibility(View.GONE); + mEmptyMessageView.setVisibility(View.VISIBLE); + mEmptyMessageView.setText(getString( + R.string.gestures_error_loading, SettingsUtil + .getGestureFile(mThis).getAbsolutePath())); + } else { + findViewById(R.id.addButton).setEnabled(true); + findViewById(R.id.reloadButton).setEnabled(true); + checkForEmpty(); + } + } + } + + static class NamedGesture { + String name; + Gesture gesture; + } + + private class GesturesAdapter extends ArrayAdapter { + private final LayoutInflater mInflater; + private final Map mThumbnails = Collections + .synchronizedMap(new HashMap()); + + public GesturesAdapter(Context context) { + super(context, 0); + mInflater = (LayoutInflater) context + .getSystemService(Context.LAYOUT_INFLATER_SERVICE); + } + + void addBitmap(Long id, Bitmap bitmap) { + mThumbnails.put(id, new BitmapDrawable(getResources(), bitmap)); + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + if (convertView == null) { + convertView = mInflater.inflate(R.layout.gestures_item, parent, + false); + } + + final NamedGesture gesture = getItem(position); + final TextView label = (TextView) convertView; + + label.setTag(gesture); + label.setText(gesture.name); + label.setCompoundDrawablesWithIntrinsicBounds( + mThumbnails.get(gesture.gesture.getID()), null, null, null); + + return convertView; + } + } } -- cgit v1.2.3