view src/com/five_ten_sg/connectbot/monitor/MonitorActivity.java @ 21:665324c9716e

enable some debug logging
author Carl Byington <carl@five-ten-sg.com>
date Tue, 08 Jul 2014 09:25:54 -0700
parents 2be5bca648ab
children adc776858a2d
line wrap: on
line source

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) {
                printer((String)msg.obj);
            } 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);
        printer(getString(R.string.copyright));
        String external_dir = Environment.getExternalStorageDirectory().getAbsolutePath();
        printer("");
        printer(String.format("External directory is %s", external_dir));
        printer("");
        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);
    }

    private void printer(String msg) {
        if (count < LINES) count++;
        else               start = (start+1) % LINES;
        texts[(start+count-1) % LINES] = msg + "\n";
        String c = "";
        for (int i=0; i<count; i++) c = c.concat(texts[(start+i) % LINES]);
        text.setText(c);
    }

    @Override
    protected void onStart() {
        super.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();
    }
}