package com.example.svc.activities;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.example.svc.R;

public class RemoteBoundServicesActivity extends Activity {
    private static final int MSG_DOWNLOAD_START = 0;
    private static final int MSG_DOWNLOAD_STOP = 1;

    private boolean isBound;
    private Messenger messenger;
    private ServiceConnection conn = new RemoteServiceConnection();

    class RemoteServiceConnection implements ServiceConnection {
        @Override
        public void onServiceConnected(ComponentName c, IBinder svc) {
            messenger = new Messenger(svc);
            isBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName classname) {
            messenger = null;
            isBound = false;
        }
    }
    
    @Override
    protected void onStart() {
        super.onStart();
        Intent intent = new Intent();
        ComponentName cn = new ComponentName(
              "com.example.svc.remote",
              "com.example.svc.remote.DownloadService4");
        intent.setComponent(cn);
        bindService(intent, conn, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (isBound) {
            unbindService(conn);
            isBound = false;
        }
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.remote);

        Button start = (Button) findViewById(R.id.startDownload);
        start.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                EditText urlEditText =
                        (EditText) findViewById(R.id.url);
                String url = urlEditText.getText().toString();

                if (isBound) {
                    Message msg = Message.obtain();
                    msg.what = MSG_DOWNLOAD_START;
                    Bundle bundle = new Bundle();
                    bundle.putString("URL", url);
                    msg.setData(bundle);
                    try {
                        messenger.send(msg);
                    }
                    catch (RemoteException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        
        Button stop = (Button) findViewById(R.id.stopDownload);
        stop.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (isBound) {
                    Message msg = Message.obtain();
                    msg.what = MSG_DOWNLOAD_STOP;
                    try {
                        messenger.send(msg);
                    }
                    catch (RemoteException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }
}