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 w w . j av a 2 s . c om*/ import java.util.ArrayList; import java.util.HashSet; import java.util.Set; import android.app.Activity; import android.app.AlertDialog; import android.content.ActivityNotFoundException; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; import android.speech.RecognizerIntent; import android.text.InputType; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.widget.AdapterView.AdapterContextMenuInfo; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class NewAction extends Activity { private String Command; private ArrayList<String> steps; private SharedPreferences prefs; private Button removeCommand; private Button addCommand; private TextView commandName; private ListView actualActions; private ArrayAdapter<String> arrayAdapter; public static String SUFFIX = "_"; public static String SEP = ";"; public static String CH_SEP = "ch-"; public static String PREFS_ACTIONS = "_actions_list"; private boolean changed = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_new_action); getActionBar().setDisplayHomeAsUpEnabled(true); prefs = PreferenceManager.getDefaultSharedPreferences(this); removeCommand = (Button) findViewById(R.id.delCommand); Command = ""; addCommand = (Button) findViewById(R.id.addVoice); commandName = (TextView) findViewById(R.id.commandLabel); actualActions = (ListView) findViewById(R.id.listView1); steps = new ArrayList<String>(); arrayAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1); actualActions.setAdapter(arrayAdapter); registerForContextMenu(actualActions); getActionBar().setTitle(R.string.newAction); } @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_new_action, menu); return true; } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == MainActivity.VOICE_RECOGNITION_REQUEST_CODE) if (resultCode == RESULT_OK) { ArrayList<String> textMatchList = null; Animation alphaAnim = new AlphaAnimation(0.0f, 1.0f); alphaAnim.setDuration(270); alphaAnim.setFillAfter(true); textMatchList = data .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); if (!textMatchList.isEmpty()) { Command = textMatchList.get(0); commandName.setText(Command); // Animations commandName.bringToFront(); removeCommand.bringToFront(); commandName.startAnimation(alphaAnim); removeCommand.startAnimation(alphaAnim); addCommand.animate().setDuration(270).alpha(0); addCommand.setEnabled(false); changed = true; } } else if (resultCode == RecognizerIntent.RESULT_AUDIO_ERROR) { showToastMessage(getResources().getString(R.string.AudioError)); } else if (resultCode == RecognizerIntent.RESULT_CLIENT_ERROR) { showToastMessage(getResources().getString(R.string.ClientError)); } else if (resultCode == RecognizerIntent.RESULT_NETWORK_ERROR) { showToastMessage(getResources() .getString(R.string.NetworkError)); } else if (resultCode == RecognizerIntent.RESULT_NO_MATCH) { showToastMessage(getResources().getString(R.string.NoMatch)); } else if (resultCode == RecognizerIntent.RESULT_SERVER_ERROR) { showToastMessage(getResources().getString(R.string.ServerError)); } super.onActivityResult(requestCode, resultCode, data); } void showToastMessage(String message) { Toast.makeText(this, message, Toast.LENGTH_SHORT).show(); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.context_menu, menu); } private void ask() { AlertDialog.Builder builder = new AlertDialog.Builder(this); // Add the buttons builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); back(); } }); builder.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); builder.setTitle(getResources().getString(R.string.notSave)); builder.setMessage(getResources().getString(R.string.wouldyoulikesave)); AlertDialog dialog = builder.create(); dialog.show(); } private void back() { super.onBackPressed(); this.overridePendingTransition(R.anim.slideback, R.anim.soutback); } public void addVoice(View view) { MainActivity.vibrate(getApplicationContext(), 10); try { Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getClass() .getPackage().getName()); intent.putExtra(RecognizerIntent.EXTRA_SECURE, true); String pref = prefs.getString(MainActivity.PREFS_LANGUAGE, "default"); if (pref.equals("english")) { intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US"); } startActivityForResult(intent, MainActivity.VOICE_RECOGNITION_REQUEST_CODE); } catch (ActivityNotFoundException a) { Toast.makeText(this, getResources().getString(R.string.voiceNotPresent), Toast.LENGTH_LONG).show(); } } public void addAction(View view) { MainActivity.vibrate(getApplicationContext(), 10); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(R.string.addCommand); builder.setItems(R.array.buttons2, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { if (which == 23) { showCustomDialog(which, -1); } else { String[] names = getResources().getStringArray( R.array.buttons2); NewAction.this.arrayAdapter.add(names[which]); NewAction.this.steps.add(Integer.toString(which)); changed = true; } } }); AlertDialog dialog = builder.create(); dialog.show(); } private void showCustomDialog(final int which, final int pos) { final EditText text = new EditText(this); text.setInputType(InputType.TYPE_CLASS_NUMBER); LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); text.setLayoutParams(lp); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setView(text); // Add the buttons builder.setPositiveButton(R.string.addSave, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); String txt = text.getText().toString(); if (!txt.isEmpty()) { if (pos != -1) { String[] names = getResources().getStringArray( R.array.buttons2); NewAction.this.arrayAdapter .remove(NewAction.this.arrayAdapter .getItem(pos)); NewAction.this.arrayAdapter.insert(names[which] + " " + txt, pos); NewAction.this.steps.set(pos, CH_SEP + txt); changed = true; } else { String[] names = getResources().getStringArray( R.array.buttons2); NewAction.this.arrayAdapter.add(names[which] + " " + txt); NewAction.this.steps.add(CH_SEP + txt); changed = true; } } } }); builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); builder.setTitle(R.string.channelLabel); AlertDialog dialog = builder.create(); dialog.show(); } public void editAction(int pos) { MainActivity.vibrate(getApplicationContext(), 10); final int _pos = pos; AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(R.string.addCommand); builder.setItems(R.array.buttons2, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { if (which == 23) { showCustomDialog(which, _pos); } else { String[] names = getResources().getStringArray( R.array.buttons2); NewAction.this.arrayAdapter .remove(NewAction.this.arrayAdapter .getItem(_pos)); NewAction.this.arrayAdapter.insert(names[which], _pos); NewAction.this.steps.set(_pos, Integer.toString(which)); changed = true; } } }); AlertDialog dialog = builder.create(); dialog.show(); } public void removeCommand(View view) { changed = true; MainActivity.vibrate(getApplicationContext(), 10); Animation alphaAnim = new AlphaAnimation(1.0f, 0.0f); alphaAnim.setDuration(270); alphaAnim.setFillAfter(true); Command = ""; if (addCommand.getVisibility() == View.INVISIBLE) { addCommand.setVisibility(View.VISIBLE); } addCommand.bringToFront(); commandName.startAnimation(alphaAnim); removeCommand.startAnimation(alphaAnim); addCommand.animate().setDuration(270).alpha(1.0f); addCommand.setEnabled(true); } @Override public boolean onContextItemSelected(MenuItem item) { AdapterContextMenuInfo info = (AdapterContextMenuInfo) item .getMenuInfo(); switch (item.getItemId()) { case R.id.editStep: editAction(info.position); return true; case R.id.deleteStep: arrayAdapter.remove(arrayAdapter.getItem(info.position)); steps.remove(info.position); changed = true; return true; default: return super.onContextItemSelected(item); } } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: if (!changed) { back(); return true; } if (!arrayAdapter.isEmpty() || !Command.equals("")) { ask(); } else { back(); } return true; case R.id.saveAction: if (Command.equals("")) { showToastMessage(getResources() .getString(R.string.emptyCommand)); } else if (arrayAdapter.isEmpty()) { showToastMessage(getResources().getString(R.string.emptyList)); } else { save(); } return true; default: return super.onOptionsItemSelected(item); } } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { back(); return true; } return super.onKeyDown(keyCode, event); } private void save() { StringBuilder sb; if (prefs.contains(Command + SUFFIX) || prefs.contains(Command)) { sb = new StringBuilder(); sb.append(getResources().getString(R.string.exist) + " " + Command + " " + getResources().getString(R.string.exist2)); showToastMessage(sb.toString()); } else { SharedPreferences.Editor editor = prefs.edit(); // Retrieve the values Set<String> set = new HashSet<String>(); set = prefs.getStringSet(PREFS_ACTIONS, null); set.add(Command); editor.putStringSet(PREFS_ACTIONS, set); sb = new StringBuilder(); for (String step : steps) { sb.append(step + SEP); } editor.putString(Command + SUFFIX, sb.toString()); editor.commit(); showToastMessage(getResources().getString(R.string.save)); back(); } } }