package tw.frb.sharecam;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

    private static final String TAG = "MainActivity";
    private ServerDialogFragment serverFragment;
    private ShareCam shareCam;
    private CommandRunnable cmdRunnable;
    private Thread cmdThread;
    private PowerManager powerManager;
    private PowerManager.WakeLock wakeLock;

    final Handler handler = new Handler() {

        public void handleMessage(Message msg) {
            TextView tvServerMessage = (TextView)findViewById(R.id.tvServerMessage);
            boolean status = msg.getData().getBoolean("status");
            int port = msg.getData().getInt("client_port");

            if (status)
                tvServerMessage.setText(getResources().getString(R.string.server_msg_port) + port);
            else
                tvServerMessage.setText(getResources().getString(R.string.server_msg_error));
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.powerManager = (PowerManager)getSystemService(POWER_SERVICE);
        this.wakeLock = powerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "sharecam");

        this.serverFragment = new ServerDialogFragment();
        this.serverFragment.setCancelable(false);
        this.serverFragment.callback = new ServerDialogFragment.Callback() {

            @Override
            public void positive() {
                startServer();
            }
        };
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onStart() {
        super.onStart();

        this.wakeLock.acquire(150 * 1000);

        if (this.serverFragment.username.length() == 0) {
            this.serverFragment.show(getFragmentManager(), "server");
        } else {
            startServer();
        }
    }

    @Override
    public void onStop() {
        stopServer();

        if (this.wakeLock.isHeld())
            this.wakeLock.release();

        super.onStop();
    }

    public void startServer() {
        this.shareCam = new ShareCam(this);
        this.cmdRunnable = new CommandRunnable(shareCam, serverFragment.username, serverFragment.password, handler);
        this.cmdThread = new Thread(cmdRunnable);
        this.cmdThread.start();
    }

    public void stopServer() {

        if (cmdRunnable != null)
            cmdRunnable.kill();

        try {
            if (cmdThread != null)
                cmdThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        if (shareCam != null)
            shareCam.release();
    }
}

@@ -49,94 +49,8 @@
 le;%0A
-import android.os.Handler;%0Aimport android.os.Message;%0Aimport android.os.PowerManager;%0A
 impo
@@ -103,40 +103,8 @@
 enu;
-%0Aimport android.widget.TextView;
 %0A%0Apu
@@ -205,2753 +205,543 @@
 y%22;%0A
-    private ServerDialogFragment serverFragment;%0A    private ShareCam shareCam;%0A    private CommandRunnable cmdRunnable;%0A    private Thread cmdThread;%0A    private PowerManager powerManager;%0A    private PowerManager.WakeLock wakeLock;%0A%0A    final Handler handler = new Handler() %7B%0A%0A        public void handleMessage(Message msg) %7B%0A            TextView tvServerMessage = (TextView)findViewById(R.id.tvServerMessage);%0A            boolean status = msg.getData().getBoolean(%22status%22);%0A            int port = msg.getData().getInt(%22client_port%22);%0A%0A            if (status)%0A                tvServerMessage.setText(getResources().getString(R.string.server_msg_port) + port);%0A            else%0A                tvServerMessage.setText(getResources().getString(R.string.server_msg_error));%0A        %7D%0A    %7D;%0A%0A    @Override%0A    protected void onCreate(Bundle savedInstanceState) %7B%0A        super.onCreate(savedInstanceState);%0A        setContentView(R.layout.activity_main);%0A%0A        this.powerManager = (PowerManager)getSystemService(POWER_SERVICE);%0A        this.wakeLock = powerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, %22sharecam%22);%0A%0A        this.serverFragment = new ServerDialogFragment();%0A        this.serverFragment.setCancelable(false);%0A        this.serverFragment.callback = new ServerDialogFragment.Callback() %7B%0A%0A            @Override%0A            public void positive() %7B%0A                startServer();%0A            %7D%0A        %7D;%0A    %7D%0A%0A    @Override%0A    public boolean onCreateOptionsMenu(Menu menu) %7B%0A        // Inflate the menu; this adds items to the action bar if it is present.%0A        getMenuInflater().inflate(R.menu.main, menu);%0A        return true;%0A    %7D%0A%0A    @Override%0A    public void onStart() %7B%0A        super.onStart();%0A%0A        this.wakeLock.acquire(150 * 1000);%0A%0A        if (this.serverFragment.username.length() == 0) %7B%0A            this.serverFragment.show(getFragmentManager(), %22server%22);%0A        %7D else %7B%0A            startServer();%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void onStop() %7B%0A        stopServer();%0A%0A        if (this.wakeLock.isHeld())%0A            this.wakeLock.release();%0A%0A        super.onStop();%0A    %7D%0A%0A    public void startServer() %7B%0A        this.shareCam = new ShareCam(this);%0A        this.cmdRunnable = new CommandRunnable(shareCam, serverFragment.username, serverFragment.password, handler);%0A        this.cmdThread = new Thread(cmdRunnable);%0A        this.cmdThread.start();%0A    %7D%0A%0A    public void stopServer() %7B%0A%0A        if (cmdRunnable != null)%0A            cmdRunnable.kill();%0A%0A        try %7B%0A            if (cmdThread != null)%0A                cmdThread.join();%0A        %7D catch (InterruptedException e) %7B%0A            e.printStackTrace();%0A        %7D%0A%0A        if (shareCam != null)%0A            shareCam.release
+%0A    @Override%0A    protected void onCreate(Bundle savedInstanceState) %7B%0A        super.onCreate(savedInstanceState);%0A        setContentView(R.layout.activity_main);%0A    %7D%0A%0A    @Override%0A    public boolean onCreateOptionsMenu(Menu menu) %7B%0A        // Inflate the menu; this adds items to the action bar if it is present.%0A        getMenuInflater().inflate(R.menu.main, menu);%0A        return true;%0A    %7D%0A%0A    @Override%0A    public void onStart() %7B%0A        super.onStart();%0A    %7D%0A%0A    @Override%0A    public void onStop() %7B%0A        super.onStop
 ();%0A

clear main activity