Android Open Source - Android-Apps New Action Single Edit






From Project

Back to project page Android-Apps.

License

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.

Java Source Code

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();
    }
  }

}




Java Source Code List

com.kniezrec.remoterecorder.Communication.java
com.kniezrec.remoterecorder.MainServiceConnection.java
com.kniezrec.remoterecorder.MainService.java
com.kniezrec.remoterecorder.RequestType.java
com.kniezrec.remoterecorder.Request.java
com.kniezrec.voiceremote2.BSeriesKeyCodeSenderFactory.java
com.kniezrec.voiceremote2.BSeriesSender.java
com.kniezrec.voiceremote2.CSeriesButtons.java
com.kniezrec.voiceremote2.CSeriesKeyCodeSenderFactory.java
com.kniezrec.voiceremote2.CSeriesSender.java
com.kniezrec.voiceremote2.CommandsFragment.java
com.kniezrec.voiceremote2.Commands.java
com.kniezrec.voiceremote2.Discovery.java
com.kniezrec.voiceremote2.FSeriesButtons.java
com.kniezrec.voiceremote2.Group.java
com.kniezrec.voiceremote2.HelpFragment.java
com.kniezrec.voiceremote2.HostnamePreference.java
com.kniezrec.voiceremote2.KeyCodeSender.java
com.kniezrec.voiceremote2.ListActionsFragment.java
com.kniezrec.voiceremote2.MainActivity.java
com.kniezrec.voiceremote2.MainFragment.java
com.kniezrec.voiceremote2.Mapper.java
com.kniezrec.voiceremote2.MyExpandableListAdapter.java
com.kniezrec.voiceremote2.NewActionEdit.java
com.kniezrec.voiceremote2.NewActionSingleEdit.java
com.kniezrec.voiceremote2.NewAction.java
com.kniezrec.voiceremote2.RemoteButton.java
com.kniezrec.voiceremote2.SenderFactory.java
com.kniezrec.voiceremote2.Sender.java
com.kniezrec.voiceremote2.SettingsActivity.java
com.kniezrec.voiceremote2.TextSender.java
com.kniezrec.voiceremotefree.BSeriesKeyCodeSenderFactory.java
com.kniezrec.voiceremotefree.BSeriesSender.java
com.kniezrec.voiceremotefree.CSeriesButtons.java
com.kniezrec.voiceremotefree.CSeriesKeyCodeSenderFactory.java
com.kniezrec.voiceremotefree.CSeriesSender.java
com.kniezrec.voiceremotefree.Commands.java
com.kniezrec.voiceremotefree.Discovery.java
com.kniezrec.voiceremotefree.FSeriesButtons.java
com.kniezrec.voiceremotefree.HelpActivity.java
com.kniezrec.voiceremotefree.HostnamePreference.java
com.kniezrec.voiceremotefree.KeyCodeSender.java
com.kniezrec.voiceremotefree.ListActionsActivity.java
com.kniezrec.voiceremotefree.MainActivity.java
com.kniezrec.voiceremotefree.Mapper.java
com.kniezrec.voiceremotefree.NewActionEdit.java
com.kniezrec.voiceremotefree.NewActionSingleEdit.java
com.kniezrec.voiceremotefree.NewAction.java
com.kniezrec.voiceremotefree.RemoteButton.java
com.kniezrec.voiceremotefree.SenderFactory.java
com.kniezrec.voiceremotefree.Sender.java
com.kniezrec.voiceremotefree.Setings.java
com.kniezrec.voiceremotefree.SettingsActivity.java
com.kniezrec.voiceremotefree.TextSender.java
com.kniezrec.xbmcgear.connection.AndroidApplication.java
com.kniezrec.xbmcgear.connection.Connection.java
com.kniezrec.xbmcgear.connection.GearJSON.java
com.kniezrec.xbmcgear.connection.JSONRPCRequest.java
com.kniezrec.xbmcgear.connection.JSONRequestFactory.java
com.kniezrec.xbmcgear.connection.NSDResolve.java
com.kniezrec.xbmcgear.connection.NSDSearch.java
com.kniezrec.xbmcgear.connection.ProviderConnection.java
com.kniezrec.xbmcgear.connection.ProviderService.java
com.kniezrec.xbmcgear.connection.ResponseParser.java
com.kniezrec.xbmcgear.connection.WakeOnLan.java
com.kniezrec.xbmcgear.player.Kodi.java
com.kniezrec.xbmcgear.player.Player.java
com.kniezrec.xbmcgear.player.Playlist.java
com.kniezrec.xbmcgear.player.Song.java
com.kniezrec.xbmcgear.player.Video.java
com.kniezrec.xbmcgear.preferences.HostTable.java
com.kniezrec.xbmcgear.preferences.Host.java
com.kniezrec.xbmcgear.preferences.HostsDataSource.java
com.kniezrec.xbmcgear.preferences.HostsDatabaseHelper.java
com.kniezrec.xbmcgear.preferences.SharedPreferencesUtil.java
com.kniezrec.xbmcgear.presentation.AnimationManager.java
com.kniezrec.xbmcgear.presentation.AutoConfigurationActivity.java
com.kniezrec.xbmcgear.presentation.HostSetActivity.java
com.kniezrec.xbmcgear.presentation.InstanceActivity.java
com.kniezrec.xbmcgear.presentation.MainActivity.java
com.kniezrec.xbmcgear.presentation.StyleDialogFragment.java
com.kniezrec.xbmcgear.presentation.ViewMode.java
com.uraroji.garage.android.lame.SimpleLame.java
com.uraroji.garage.android.mp3recvoice.RecMicToMp3.java
de.quist.samy.remocon.Base64.java
de.quist.samy.remocon.Base64.java
de.quist.samy.remocon.ConnectionDeniedException.java
de.quist.samy.remocon.ConnectionDeniedException.java
de.quist.samy.remocon.Key.java
de.quist.samy.remocon.Key.java
de.quist.samy.remocon.Loggable.java
de.quist.samy.remocon.Loggable.java
de.quist.samy.remocon.RemoteReader.java
de.quist.samy.remocon.RemoteReader.java
de.quist.samy.remocon.RemoteSession.java
de.quist.samy.remocon.RemoteSession.java