Mercurial > 510ConnectbotMonitor
diff src/com/five_ten_sg/connectbot/monitor/MonitorActivity.java @ 0:5564580fe160
initial version
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Mon, 05 May 2014 13:37:31 -0700 |
parents | |
children | f6a1aabf384f |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/com/five_ten_sg/connectbot/monitor/MonitorActivity.java Mon May 05 13:37:31 2014 -0700 @@ -0,0 +1,102 @@ +package com.five_ten_sg.connectbot.monitor; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.ServerSocket; +import java.net.Socket; + +import android.app.Activity; +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.content.ServiceConnection; +import android.net.wifi.WifiManager.WifiLock; +import android.net.wifi.WifiManager; +import android.os.Binder; +import android.os.Bundle; +import android.os.Handler; +import android.os.IBinder; +import android.os.Message; +import android.os.PowerManager; +import android.util.Log; +import android.widget.TextView; + +public class MonitorActivity extends Activity { + public final static String TAG = "ConnectBot.MonitorActivity"; + + public static final int MESSAGE_CODE_PRINT = 6000; + private final int LINES = 20; + private String[] texts = new String[LINES]; + private int start = 0; + private int count = 0; + private TextView text = null; + private MonitorService bound = null; + private Handler handler = new Handler() { + @Override + public void handleMessage (Message msg) { + if (msg.what == MESSAGE_CODE_PRINT) { + String s = (String)msg.obj; + if (count < LINES) count++; + else start = (start+1) % LINES; + texts[(start+count-1) % LINES] = s + "\n"; + String c = ""; + for (int i=0; i<count; i++) c = c.concat(texts[(start+i) % LINES]); + text.setText(c); + } else { + super.handleMessage(msg); + } + } + }; + private ServiceConnection connection = new ServiceConnection() { + public void onServiceConnected(ComponentName className, IBinder service) { + Log.i(TAG, "onServiceConnected()"); + bound = ((MonitorService.MonitorBinder)service).getService(); + bound.handler = handler; + } + public void onServiceDisconnected(ComponentName className) { + Log.i(TAG, "onServiceDisconnected()"); + bound = null; + } + }; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.main); + text = (TextView) findViewById(R.id.text2); + Log.i(TAG, "binding to monitor service"); + Intent intent = new Intent ("com.five_ten_sg.connectbot.monitor.MonitorService"); + bindService(intent, connection, Context.BIND_AUTO_CREATE); + } + + public void printer(String msg) { + handler.sendMessage(handler.obtainMessage(MonitorActivity.MESSAGE_CODE_PRINT, msg)); + } + + @Override + protected void onStart() { + super.onStart(); + printer("activity onStart()"); + Log.i(TAG, "activity onStart()"); + } + + @Override + protected void onRestart() { + super.onRestart(); + Log.i(TAG, "activity onRestart()"); + } + + @Override + protected void onStop() { + super.onStop(); + Log.i(TAG, "activity onStop()"); + } + + @Override + protected void onDestroy() { + Log.i(TAG, "activity onDestroy()"); + unbindService(connection); + super.onDestroy(); + } +}