Android Open Source - android-chess Custom Commands






From Project

Back to project page android-chess.

License

The source code is released under:

MIT License

If you think the Android project android-chess 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 jwtc.android.chess.ics;
import org.json.JSONArray;
import org.json.JSONException;
//w w w  . j a va  2 s  . co  m
import jwtc.android.chess.MyBaseActivity;
import jwtc.android.chess.R;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class CustomCommands extends MyBaseActivity implements OnItemClickListener {

  public static final String TAG = "CustomCommands";
  public static final String DEFAULT_COMMANDS = "[" +
      "\"obs /b\"," +
      "\"obs /s\"," +
      "\"tell relay listgames\"," + 
      "\"tell puzzlebot getmate\"," +
      "\"tell puzzlebot gettactics\"," +
      "\"tell puzzlebot getstudy\"," +
      "\"exl\"," +
      "\"help commands\"" +
      "]";
  
  private ListView _listCommands;
  ArrayAdapter<String> _adapter;

  @Override
  public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // TODO Auto-generated method stub
    
    final int position = arg2;
    
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(getString(R.string.title_edit_or_delete));
    
    builder.setPositiveButton(getString(R.string.choice_edit), new DialogInterface.OnClickListener(){
      public void onClick(DialogInterface dialog, int which) {
        
        final FrameLayout fl = new FrameLayout(CustomCommands.this);
        final EditText input = new EditText(CustomCommands.this);
        input.setText(_adapter.getItem(position));
        input.setGravity(Gravity.CENTER);
        
        fl.addView(input, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT));
        
        AlertDialog.Builder builder = new AlertDialog.Builder(CustomCommands.this)
          .setView(fl)
          .setTitle(getString(R.string.title_edit_command))
          .setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int which) {
            String s = input.getText().toString();
            
            _adapter.remove(_adapter.getItem(position));
            _adapter.insert(s, position);
            
            _listCommands.invalidateViews();
            
            dialog.dismiss();
          }
          
        });
        AlertDialog alert = builder.create();
        alert.show();
        
        dialog.dismiss();
      }
      
    });
    
    builder.setNegativeButton(getString(R.string.choice_delete), new DialogInterface.OnClickListener(){
      public void onClick(DialogInterface dialog, int which) {
        
        _adapter.remove(_adapter.getItem(position));        
        _listCommands.invalidateViews();
        dialog.dismiss();        
        
      }
    });
    
    AlertDialog alert = builder.create();
    alert.show();
    
  }

  
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    setContentView(R.layout.customcommands);
    
    _listCommands = (ListView) findViewById(R.id.ListCustomCommands);
    
    _listCommands.setOnItemClickListener(this);
  
  }
  
  @Override
  protected void onResume() {
    
    super.onResume();
  
    
    SharedPreferences prefs = getSharedPreferences("ChessPlayer", MODE_PRIVATE);
    
    _adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
    
    try {
      JSONArray jArray = new JSONArray(prefs.getString("ics_custom_commands", DEFAULT_COMMANDS));
      for(int i = 0; i < jArray.length(); i++){
        _adapter.add(jArray.getString(i));
      }
    } catch (JSONException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    //commands = prefs.getS
    
    _listCommands.setAdapter(_adapter);
    
  }
  

  @Override
  protected void onPause() {

    SharedPreferences.Editor editor = getSharedPreferences("ChessPlayer", MODE_PRIVATE).edit();
    
    JSONArray jArray = new JSONArray();
    for(int i = 0; i < _adapter.getCount(); i++){
      jArray.put(_adapter.getItem(i));
    }
    editor.putString("ics_custom_commands", jArray.toString());

    editor.commit();
    
    super.onPause();
  }

   public boolean onCreateOptionsMenu(Menu menu) {

    MenuItem item1;

    item1 = menu.add(getString(R.string.menu_new_command));
    item1.setIcon(R.drawable.content_new);

    return true;
  }

  public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getTitle().equals(getString(R.string.menu_new_command))) {

      final FrameLayout fl = new FrameLayout(this);
      final EditText input = new EditText(this);
      input.setGravity(Gravity.CENTER);
      
      fl.addView(input, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT));
      
      AlertDialog.Builder builder = new AlertDialog.Builder(this)
        .setView(fl)
        .setTitle("New command")
        .setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
          String s = input.getText().toString();
          
          _adapter.add(s);
          _listCommands.invalidateViews();
          
          dialog.dismiss();
        }
        
      });
      AlertDialog alert = builder.create();
      alert.show();

      return true;
    }

    return false;
  }

}




Java Source Code List

jwtc.android.chess.ChessFieldView.java
jwtc.android.chess.ChessImageView.java
jwtc.android.chess.ChessPreferences.java
jwtc.android.chess.ChessViewBase.java
jwtc.android.chess.ChessView.java
jwtc.android.chess.GamesListView.java
jwtc.android.chess.HtmlActivity.java
jwtc.android.chess.ImageCacheObject.java
jwtc.android.chess.MyBaseActivity.java
jwtc.android.chess.MyPGNProvider.java
jwtc.android.chess.PGNView.java
jwtc.android.chess.SaveGameDlg.java
jwtc.android.chess.UI.java
jwtc.android.chess.convergence.Connection.java
jwtc.android.chess.convergence.ConvergenceActivity.java
jwtc.android.chess.convergence.RestServer.java
jwtc.android.chess.iconifiedlist.IconifiedTextListAdapter.java
jwtc.android.chess.iconifiedlist.IconifiedTextView.java
jwtc.android.chess.iconifiedlist.IconifiedText.java
jwtc.android.chess.ics.CustomCommands.java
jwtc.android.chess.ics.ICSChatDlg.java
jwtc.android.chess.ics.ICSChessView.java
jwtc.android.chess.ics.ICSClient.java
jwtc.android.chess.ics.ICSConfirmDlg.java
jwtc.android.chess.ics.ICSMatchDlg.java
jwtc.android.chess.ics.TelnetSocket.java
jwtc.android.chess.ics.TimesealInputStream.java
jwtc.android.chess.ics.TimesealOutputStream.java
jwtc.android.chess.ics.TimesealPipe.java
jwtc.android.chess.ics.TimesealingSocket.java
jwtc.android.chess.puzzle.ChessViewPractice.java
jwtc.android.chess.puzzle.ChessViewPuzzle.java
jwtc.android.chess.puzzle.MyPuzzleProvider.java
jwtc.android.chess.puzzle.practice.java
jwtc.android.chess.puzzle.puzzle.java
jwtc.android.chess.tools.FileListView.java
jwtc.android.chess.tools.PGNProcessor.java
jwtc.android.chess.tools.importactivity.java
jwtc.android.chess.tools.pgntool.java
jwtc.android.chess.main.java
jwtc.android.chess.options.java
jwtc.android.chess.setup.java
jwtc.android.chess.start.java
jwtc.android.timeseal.TimesealingSocket.java
jwtc.android.timeseal.streams.a.java
jwtc.android.timeseal.streams.b.java
jwtc.android.timeseal.streams.c.java
jwtc.chess.ChessPuzzleProvider.java
jwtc.chess.GameControl.java
jwtc.chess.JNI.java
jwtc.chess.Move.java
jwtc.chess.PGNColumns.java
jwtc.chess.PGNEntry.java
jwtc.chess.PGNProvider.java
jwtc.chess.Pos.java
jwtc.chess.Valuation.java
jwtc.chess.algorithm.SearchAlgorithmRunner.java
jwtc.chess.algorithm.UCIWrapper.java
jwtc.chess.board.BoardConstants.java
jwtc.chess.board.BoardHashKeys.java
jwtc.chess.board.BoardMembers.java
jwtc.chess.board.BoardStatics.java
jwtc.chess.board.ChessBoard.java