Back to project page Android-Apps.
The source code is released under:
Apache License
If you think the Android project Android-Apps 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 com.kniezrec.voiceremotefree; // w ww .ja v a 2s .co m import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemLongClickListener; import android.widget.ListView; import android.widget.SimpleAdapter; public class ListActionsActivity extends Activity { private SharedPreferences prefs; private SimpleAdapter arrayAdapter; private Set<String> actionsSet; private HashMap<String, String> command; private ArrayList<Map<String, String>> listData; private static final Set<String> keysToDel = new HashSet<String>( Arrays.asList(new String[] { "LanguageSet", "timeoutSec", "serverHost", "serverPort", "keyCodeSenderFactory", "pauseSec", "drt" })); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list_actions); getActionBar().setTitle(getResources().getString(R.string.actionsAct)); getActionBar().setDisplayHomeAsUpEnabled(true); prefs = PreferenceManager.getDefaultSharedPreferences(this); prepareCommands(); if (!prefs.contains(NewAction.PREFS_ACTIONS)) { SharedPreferences.Editor edit = prefs.edit(); Set<String> _set = new HashSet<String>(); edit.putStringSet(NewAction.PREFS_ACTIONS, _set); edit.commit(); } actionsSet = new HashSet<String>(); actionsSet = prefs.getStringSet(NewAction.PREFS_ACTIONS, null); ListView list = (ListView) findViewById(R.id.actionsList); String[] from = { "name", "purpose" }; int[] to = { android.R.id.text1, android.R.id.text2 }; listData = new ArrayList<Map<String, String>>(); arrayAdapter = new SimpleAdapter(getApplicationContext(), listData, android.R.layout.simple_list_item_2, from, to); buildData(); list.setAdapter(arrayAdapter); // Click event for single list row list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String cmdName = listData.get(position).get("name"); if (command.containsKey(cmdName)) { showDetails(cmdName, prefs.getString(cmdName + NewAction.SUFFIX, ""), command.get(cmdName)); } else { showDetails(cmdName, prefs.getString(cmdName + NewAction.SUFFIX, ""), null); } } }); list.setOnItemLongClickListener(new OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int positon, long id) { MainActivity.vibrate(getApplicationContext(), 15); showDialogDelete(positon); return false; } }); // AnimationSet set = new AnimationSet(true); // // Animation animation = new AlphaAnimation(0.0f, 1.0f); // animation.setDuration(50); // set.addAnimation(animation); // // animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, // Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, // -1.0f, Animation.RELATIVE_TO_SELF, 0.0f); // animation.setDuration(100); // set.addAnimation(animation); // // LayoutAnimationController controller = new LayoutAnimationController( // set, 0.5f); // list.setLayoutAnimation(controller); } private void prepareCommands() { if (command == null) { command = new HashMap<String, String>(); } else { command.clear(); } Map<String, ?> keys = prefs.getAll(); String key, value; for (Map.Entry<String, ?> entry : keys.entrySet()) { key = entry.getKey(); value = entry.getValue().toString(); if (!key.contains("_") && !keysToDel.contains(key)) { command.put(key, value); } } } private void buildData() { String pur = getResources().getString(R.string.set); for (String s : actionsSet) { listData.add(putData(s, pur)); } pur = getResources().getString(R.string.sinAc); for (String s : command.keySet()) { listData.add(putData(s, pur)); } } private HashMap<String, String> putData(String name, String purpose) { HashMap<String, String> item = new HashMap<String, String>(); item.put("name", name); item.put("purpose", purpose); return item; } private void remove(int id) { Map<String, String> cmd = listData.remove(id); arrayAdapter.notifyDataSetChanged(); String name = cmd.get("name"); actionsSet.remove(name); SharedPreferences.Editor editor = prefs.edit(); if (command.containsKey(name)) { editor.remove(name); } else { editor.remove(name + NewAction.SUFFIX); } editor.putStringSet(NewAction.PREFS_ACTIONS, actionsSet); editor.commit(); } private void showDialogDelete(int idElement) { AlertDialog.Builder builder = new AlertDialog.Builder(this); // Add the buttons final int _id = idElement; builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { remove(_id); dialog.cancel(); } }); builder.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); builder.setTitle(getResources().getString(R.string.confirm)); builder.setMessage(getResources().getString(R.string.delete)); AlertDialog dialog = builder.create(); dialog.show(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_list_actions, menu); return true; } @Override protected void onResume() { super.onResume(); listData.clear(); prepareCommands(); actionsSet = prefs.getStringSet(NewAction.PREFS_ACTIONS, null); buildData(); arrayAdapter.notifyDataSetChanged(); } private void showDetails(String cmd, String steps, String action) { Intent intent; if (cmd == null) { intent = new Intent(this, NewAction.class); } else { if (action == null) { intent = new Intent(this, NewActionEdit.class); intent.putExtra("stpsList", steps); } else { intent = new Intent(this, NewActionSingleEdit.class); intent.putExtra("oneStep", action); } intent.putExtra("cmdName", cmd); } startActivity(intent); this.overridePendingTransition(R.anim.slide, R.anim.sout); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { back(); return true; } return super.onKeyDown(keyCode, event); } private void back() { super.onBackPressed(); this.overridePendingTransition(R.anim.slide_x_out, R.anim.slide_x_out_back); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: back(); return true; case R.id.addNew: if (actionsSet.size() == 3) { showInfoDialog(); } else { showDetails(null, null, null); } return true; default: return super.onOptionsItemSelected(item); } } private void showInfoDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); // Add the buttons builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); builder.setTitle(getResources().getString(R.string.listTitle)); builder.setMessage(getResources().getString(R.string.listMessage)); AlertDialog dialog = builder.create(); dialog.show(); } }