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.voiceremote2; /*from w ww . j a v a2 s. c o 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 com.kniezrec.voiceremote2.R; 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.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemLongClickListener; import android.widget.Button; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; public class ListActionsFragment extends Fragment { private View fragView; 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[] { "favr", "LanguageSet", "timeoutSec", "serverHost", "toLoudKeys", "toQuietKey", "serverPort", "keyCodeSenderFactory", "pauseSec" })); private MainActivity mAct; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { fragView = inflater.inflate(R.layout.activity_list_actions, null); prefs = PreferenceManager.getDefaultSharedPreferences(fragView .getContext()); 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(); } final Button newAction = (Button) fragView .findViewById(R.id.addNewAction); newAction.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { showDetails(null, null, null); } }); actionsSet = new HashSet<String>(); actionsSet = prefs.getStringSet(NewAction.PREFS_ACTIONS, null); ListView list = (ListView) fragView.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(fragView.getContext(), listData, android.R.layout.simple_list_item_2, from, to) { public View getView(int position, View convertView, ViewGroup parent) { View view = super.getView(position, convertView, parent); TextView text1 = (TextView) view .findViewById(android.R.id.text1); text1.setTextColor(getResources().getColor(R.color.title)); TextView text2 = (TextView) view .findViewById(android.R.id.text2); text2.setTextColor(getResources().getColor(R.color.whiteText)); return view; }; }; 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(fragView.getContext(), 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); return fragView; } 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( fragView.getContext()); // 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 void onResume() { super.onResume(); listData.clear(); prepareCommands(); actionsSet = prefs.getStringSet(NewAction.PREFS_ACTIONS, null); buildData(); arrayAdapter.notifyDataSetChanged(); } @Override public void onAttach(Activity activity) { super.onAttach(activity); mAct = (MainActivity) activity; } @Override public void onDetach() { super.onDetach(); mAct = null; } private void showDetails(String cmd, String steps, String action) { Intent intent; if (cmd == null) { intent = new Intent(fragView.getContext(), NewAction.class); } else { if (action == null) { intent = new Intent(fragView.getContext(), NewActionEdit.class); intent.putExtra("stpsList", steps); } else { intent = new Intent(fragView.getContext(), NewActionSingleEdit.class); intent.putExtra("oneStep", action); } intent.putExtra("cmdName", cmd); } startActivity(intent); mAct.overridePendingTransition(R.anim.slide_x, R.anim.slidebackx); } }