Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

151
LINES

< > BotCompany Repo | #1001081 // Android: Talk to the android

JavaX source code (Android) [tags: use-pretranspiled] - run with: the app

Libraryless. Click here for Pure Java version (250L/2K/8K).

!747

import android.app.*;
import android.widget.*;
import android.view.*;
import android.view.View;
import android.view.KeyEvent;
import android.view.inputmethod.*;
import android.text.*;

m {
  static S ip = "second.tinybrain.de";
  static int port = 5000;
  static boolean function = true;
  
  static Activity androidContext;
  static Lg lg;
  static volatile DialogIO io;
  
  static class Lg {
    static int maxBufferLength = 2048;
    
    Activity context;
    ScrollView sv;
    TextView tv;
    StringBuilder buf = new StringBuilder();
    
    Lg(Activity context) {
      this.context = context;
      sv = new ScrollView(context);
      tv = new TextView(context);
      tv.setText(buf.toString());
      sv.addView(tv);
    }
    
    View getView() {
      return sv;
    }
    
    void shortenBuffer() {
      while (buf.length() > maxBufferLength) {
        String s = buf.toString();
        int i = s.indexOf('\n');
        if (i < 0) return;
        buf = new StringBuilder(s.substring(i+1));
      }
    }
    
    void print(final String s) {
      context.runOnUiThread(new Runnable() {
        public void run() {
          buf.append(s);
          shortenBuffer();
          tv.setText(buf.toString());
          
          sv.post(new Runnable() {
            public void run() {
              // This method works but animates the scrolling 
              // which looks weird on first load
              sv.fullScroll(View.FOCUS_DOWN);

              // This method works even better because there are no animations.
              //sv.scrollTo(0, sv.getBottom());
            }
          });
        }
      });
    }
    
    void println(String s) {
      print(s + "\n");
    }
    
    void println() { println(""); }
  }
  
  
  p {
    /*if (args.length > 0) ip = args[0];
    if (args.length > 1) port = parseInt(args[1]);*/
    
    androidContext.runOnUiThread(runnable {
      Activity context = androidContext;
      
      LinearLayout ll = new LinearLayout(context);
      ll.setOrientation(ll.VERTICAL);
      
      lg = new Lg(context);
      lg.tv.setBackgroundColor(0xFFFFFFCC);
      lg.sv.setBackgroundColor(0xFFFFFFCC);
     
      lg.sv.setLayoutParams(new LinearLayout.LayoutParams(
                             LinearLayout.LayoutParams.MATCH_PARENT,
                             LinearLayout.LayoutParams.MATCH_PARENT, 1.0f));
      
      ll.addView(lg.sv);
        
      final EditText et = new EditText(context);
      et.setInputType(InputType.TYPE_CLASS_TEXT); // Hopefully turns off multiline
      
      et.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
              if (function && io != null) {
                S line = et.getText().toString();
                lg.println(line);
                io.sendLine(line);
              }
              //et.selectAll();
              
              androidContext.runOnUiThread(runnable { 
                //et.selectAll();
                et.setText("");
              });
            }
            return false;
        }
      });
      
      /*et.setOnFocusChangeListener(new View.OnFocusChangeListener(){
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus)
          et.selectAll();
    }
});*/
    
      ll.addView(et);
      
      context.setContentView(ll);
      
      if (!function) return;
    
      thread {  
        io = talkTo(ip, port);
        
        // input
        while (io.isStillConnected()) {
          if (io.waitForLine()) {
            S line = io.readLineNoBlock();
            lg.println("> " + line);
          }
        }

        lg.println();
        lg.println("[logout]");
      }
    
    });
  }
  
}

Author comment

Began life as a copy of #1001080

download  show line numbers  debug dex  old transpilations   

Travelled to 16 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, hxnwyiuffukg, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, nbgitpuheiab, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1001081
Snippet name: Android: Talk to the android
Eternal ID of this version: #1001081/1
Text MD5: 54286777b3a9b976f6b4907c22dc8d03
Transpilation MD5: 5c6d6c31a4a12f97893b9f8229492123
Author: stefan
Category: javax
Type: JavaX source code (Android)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-09-20 05:38:23
Source code size: 4063 bytes / 151 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 656 / 1026
Referenced in: [show references]