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).

1  
!747
2  
3  
import android.app.*;
4  
import android.widget.*;
5  
import android.view.*;
6  
import android.view.View;
7  
import android.view.KeyEvent;
8  
import android.view.inputmethod.*;
9  
import android.text.*;
10  
11  
m {
12  
  static S ip = "second.tinybrain.de";
13  
  static int port = 5000;
14  
  static boolean function = true;
15  
  
16  
  static Activity androidContext;
17  
  static Lg lg;
18  
  static volatile DialogIO io;
19  
  
20  
  static class Lg {
21  
    static int maxBufferLength = 2048;
22  
    
23  
    Activity context;
24  
    ScrollView sv;
25  
    TextView tv;
26  
    StringBuilder buf = new StringBuilder();
27  
    
28  
    Lg(Activity context) {
29  
      this.context = context;
30  
      sv = new ScrollView(context);
31  
      tv = new TextView(context);
32  
      tv.setText(buf.toString());
33  
      sv.addView(tv);
34  
    }
35  
    
36  
    View getView() {
37  
      return sv;
38  
    }
39  
    
40  
    void shortenBuffer() {
41  
      while (buf.length() > maxBufferLength) {
42  
        String s = buf.toString();
43  
        int i = s.indexOf('\n');
44  
        if (i < 0) return;
45  
        buf = new StringBuilder(s.substring(i+1));
46  
      }
47  
    }
48  
    
49  
    void print(final String s) {
50  
      context.runOnUiThread(new Runnable() {
51  
        public void run() {
52  
          buf.append(s);
53  
          shortenBuffer();
54  
          tv.setText(buf.toString());
55  
          
56  
          sv.post(new Runnable() {
57  
            public void run() {
58  
              // This method works but animates the scrolling 
59  
              // which looks weird on first load
60  
              sv.fullScroll(View.FOCUS_DOWN);
61  
62  
              // This method works even better because there are no animations.
63  
              //sv.scrollTo(0, sv.getBottom());
64  
            }
65  
          });
66  
        }
67  
      });
68  
    }
69  
    
70  
    void println(String s) {
71  
      print(s + "\n");
72  
    }
73  
    
74  
    void println() { println(""); }
75  
  }
76  
  
77  
  
78  
  p {
79  
    /*if (args.length > 0) ip = args[0];
80  
    if (args.length > 1) port = parseInt(args[1]);*/
81  
    
82  
    androidContext.runOnUiThread(runnable {
83  
      Activity context = androidContext;
84  
      
85  
      LinearLayout ll = new LinearLayout(context);
86  
      ll.setOrientation(ll.VERTICAL);
87  
      
88  
      lg = new Lg(context);
89  
      lg.tv.setBackgroundColor(0xFFFFFFCC);
90  
      lg.sv.setBackgroundColor(0xFFFFFFCC);
91  
     
92  
      lg.sv.setLayoutParams(new LinearLayout.LayoutParams(
93  
                             LinearLayout.LayoutParams.MATCH_PARENT,
94  
                             LinearLayout.LayoutParams.MATCH_PARENT, 1.0f));
95  
      
96  
      ll.addView(lg.sv);
97  
        
98  
      final EditText et = new EditText(context);
99  
      et.setInputType(InputType.TYPE_CLASS_TEXT); // Hopefully turns off multiline
100  
      
101  
      et.setOnEditorActionListener(new TextView.OnEditorActionListener() {
102  
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
103  
            if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
104  
              if (function && io != null) {
105  
                S line = et.getText().toString();
106  
                lg.println(line);
107  
                io.sendLine(line);
108  
              }
109  
              //et.selectAll();
110  
              
111  
              androidContext.runOnUiThread(runnable { 
112  
                //et.selectAll();
113  
                et.setText("");
114  
              });
115  
            }
116  
            return false;
117  
        }
118  
      });
119  
      
120  
      /*et.setOnFocusChangeListener(new View.OnFocusChangeListener(){
121  
    public void onFocusChange(View v, boolean hasFocus) {
122  
        if (hasFocus)
123  
          et.selectAll();
124  
    }
125  
});*/
126  
    
127  
      ll.addView(et);
128  
      
129  
      context.setContentView(ll);
130  
      
131  
      if (!function) return;
132  
    
133  
      thread {  
134  
        io = talkTo(ip, port);
135  
        
136  
        // input
137  
        while (io.isStillConnected()) {
138  
          if (io.waitForLine()) {
139  
            S line = io.readLineNoBlock();
140  
            lg.println("> " + line);
141  
          }
142  
        }
143  
144  
        lg.println();
145  
        lg.println("[logout]");
146  
      }
147  
    
148  
    });
149  
  }
150  
  
151  
}

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: 731 / 1121
Referenced in: [show references]