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 .j a va 2 s. com import java.util.ArrayList; 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 NewActionSingleEdit extends Activity { private String Command; private String oldCommand = null; private String steps; private SharedPreferences prefs; private Button removeCommand; private Button addCommand; private TextView commandName; private ListView actualActions; private ArrayAdapter<String> arrayAdapter; 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 String(); arrayAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1); actualActions.setAdapter(arrayAdapter); registerForContextMenu(actualActions); Intent inte = getIntent(); String cmd = inte.getStringExtra("cmdName"); String oneCommand = inte.getStringExtra("oneStep"); getActionBar().setTitle(R.string.edit); addCommand.setVisibility(View.INVISIBLE); removeCommand.setVisibility(View.VISIBLE); Command = cmd; oldCommand = new String(Command); commandName.setText(cmd); commandName.setVisibility(View.VISIBLE); steps = oneCommand; if (oneCommand.contains(NewAction.SUFFIX)) { arrayAdapter.add(Mapper.mapIntToName("23", getResources()) + " " + oneCommand.substring(0, oneCommand.length() - 1)); } else { arrayAdapter .add(Mapper.mapActionToName(oneCommand, getResources())); } } @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); if (arrayAdapter.getCount() == 1) { showToastMessage(getResources().getString(R.string.onlyOneAction)); } else { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(R.string.addCommand); builder.setItems(R.array.buttons, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { if (which == 0) { showCustomDialog(which, -1); } else { String[] names = getResources().getStringArray( R.array.buttons); NewActionSingleEdit.this.arrayAdapter .add(names[which]); NewActionSingleEdit.this.steps = 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.buttons); NewActionSingleEdit.this.arrayAdapter .remove(NewActionSingleEdit.this.arrayAdapter .getItem(pos)); NewActionSingleEdit.this.arrayAdapter.insert( names[which] + " " + txt, pos); NewActionSingleEdit.this.steps = txt + NewAction.SUFFIX; changed = true; } else { String[] names = getResources().getStringArray( R.array.buttons); NewActionSingleEdit.this.arrayAdapter .add(names[which] + " " + txt); NewActionSingleEdit.this.steps = txt + NewAction.SUFFIX; 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.buttons, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { if (which == 0) { showCustomDialog(which, _pos); } else { String[] names = getResources().getStringArray( R.array.buttons); NewActionSingleEdit.this.arrayAdapter .remove(NewActionSingleEdit.this.arrayAdapter .getItem(_pos)); NewActionSingleEdit.this.arrayAdapter.insert( names[which], _pos); NewActionSingleEdit.this.steps = 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 = ""; 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 { if (changed) { saveEdit(); } } 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 saveEdit() { SharedPreferences.Editor editor = prefs.edit(); // Retrieve the values StringBuilder sb = new StringBuilder(); if ((prefs.contains(Command + NewAction.SUFFIX) || prefs .contains(Command)) && !Command.equals(oldCommand)) { sb = new StringBuilder(); sb.append(getResources().getString(R.string.exist) + " " + Command + " " + getResources().getString(R.string.exist2)); showToastMessage(sb.toString()); } else { if (steps.contains(NewAction.SUFFIX)) { editor.putString(Command, steps); } else { int v = Mapper.mapOptionToAction(steps); editor.putInt(Command, v); } if (!oldCommand.equals(Command) && prefs.contains(oldCommand)) { editor.remove(oldCommand); } editor.commit(); showToastMessage(getResources().getString(R.string.save)); back(); } } }