// Path: AndroidDemos/app/src/main/java/com/zhangtielei/demos/async/programming/common/utils/TextLogUtil.java
// public class TextLogUtil {
//     /**
//      * 在一个TextView上追加一行日志.
//      * @param logTextView
//      * @param log
//      */
//     public static void println(TextView logTextView, CharSequence log) {
//         SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
//         String timestamp = sdf.format(new Date());
// 
//         CharSequence newText = timestamp + ": " + log;
//         CharSequence oldText = logTextView.getText();
//         if (oldText == null || oldText.length() <= 0) {
//             logTextView.setText(newText);
//         }
//         else {
//             logTextView.setText(oldText + "\n" + newText);
//         }
//     }
// }
// 
// Path: AndroidDemos/app/src/main/java/com/zhangtielei/demos/async/programming/introduction/servicebinding/service/SomeService.java
// public class SomeService extends Service {
//     private static final String TAG = "SomeService";
// 
//     public SomeService() {
//     }
// 
//     /**
//      * 用于OnBind返回的Binder实例
//      */
//     private ServiceBinder binder = new ServiceBinder();
//     private List<ServiceListener> listeners = new ArrayList<ServiceListener>();
// 
//     private Random random = new Random();
// 
//     @Override
//     public IBinder onBind(Intent intent) {
//         // Return the communication channel to the service.
//         return binder;
//     }
// 
//     public void addListener(ServiceListener listener) {
//         if (!listeners.contains(listener)) {
//             listeners.add(listener);
//         }
//     }
// 
//     public void removeListener(ServiceListener listener) {
//         listeners.remove(listener);
//     }
// 
//     /**
//      * 与此Service通信的Binder类
//      */
//     public class ServiceBinder extends Binder {
//         public SomeService getService() {
//             return SomeService.this;
//         }
//     }
// 
//     /**
//      * 此Service对外界的回调接口定义.
//      */
//     public interface ServiceListener {
//         void callback();
//     }
// 
// }


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.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import com.zhangtielei.demos.async.programming.R;
import com.zhangtielei.demos.async.programming.common.utils.TextLogUtil;
import com.zhangtielei.demos.async.programming.introduction.servicebinding.service.SomeService;

        description = (TextView) findViewById(R.id.description);
        logTextView = (TextView) findViewById(R.id.log_display);

        description.setText(R.string.service_binding_description);

    }

    @Override
    public void onResume() {
        super.onResume();
        running = true;

        Intent intent = new Intent(this, SomeService.class);
        bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
        TextLogUtil.println(logTextView, "Requesting bind to SomeService...");
    }

    @Override
    public void onPause() {
        super.onPause();
        running = false;

        //解除Activity与Service的引用和监听关系
        //...
        if (someService != null) {
            someService.removeListener(ServiceBindingDemoActivity.this);
            someService = null;
        }
        unbindService(serviceConnection);
// Path: AndroidDemos/app/src/main/java/com/zhangtielei/demos/async/programming/common/utils/TextLogUtil.java
// public class TextLogUtil {
//     /**
//      * 在一个TextView上追加一行日志.
//      * @param logTextView
//      * @param log
//      */
//     public static void println(TextView logTextView, CharSequence log) {
//         SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
//         String timestamp = sdf.format(new Date());
// 
//         CharSequence newText = timestamp + ": " + log;
//         CharSequence oldText = logTextView.getText();
//         if (oldText == null || oldText.length() <= 0) {
//             logTextView.setText(newText);
//         }
//         else {
//             logTextView.setText(oldText + "\n" + newText);
//         }
//     }
// }
// 
// Path: AndroidDemos/app/src/main/java/com/zhangtielei/demos/async/programming/introduction/servicebinding/service/SomeService.java
// public class SomeService extends Service {
//     private static final String TAG = "SomeService";
// 
//     public SomeService() {
//     }
// 
//     /**
//      * 用于OnBind返回的Binder实例
//      */
//     private ServiceBinder binder = new ServiceBinder();
//     private List<ServiceListener> listeners = new ArrayList<ServiceListener>();
// 
//     private Random random = new Random();
// 
//     @Override
//     public IBinder onBind(Intent intent) {
//         // Return the communication channel to the service.
//         return binder;
//     }
// 
//     public void addListener(ServiceListener listener) {
//         if (!listeners.contains(listener)) {
//             listeners.add(listener);
//         }
//     }
// 
//     public void removeListener(ServiceListener listener) {
//         listeners.remove(listener);
//     }
// 
//     /**
//      * 与此Service通信的Binder类
//      */
//     public class ServiceBinder extends Binder {
//         public SomeService getService() {
//             return SomeService.this;
//         }
//     }
// 
//     /**
//      * 此Service对外界的回调接口定义.
//      */
//     public interface ServiceListener {
//         void callback();
//     }
// 
// }


// Path: AndroidDemos/app/src/main/java/com/zhangtielei/demos/async/programming/introduction/servicebinding/ServiceBindingDemoActivity.java
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.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import com.zhangtielei.demos.async.programming.R;
import com.zhangtielei.demos.async.programming.common.utils.TextLogUtil;
import com.zhangtielei.demos.async.programming.introduction.servicebinding.service.SomeService;


        description = (TextView) findViewById(R.id.description);
        logTextView = (TextView) findViewById(R.id.log_display);

        description.setText(R.string.service_binding_description);

    }

    @Override
    public void onResume() {
        super.onResume();
        running = true;

        Intent intent = new Intent(this, SomeService.class);
        bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
        TextLogUtil.println(logTextView, "Requesting bind to SomeService...");
    }

    @Override
    public void onPause() {
        super.onPause();
        running = false;

        //解除Activity与Service的引用和监听关系
        //...
        if (someService != null) {
            someService.removeListener(ServiceBindingDemoActivity.this);
            someService = null;
        }
        unbindService(serviceConnection);
        TextLogUtil.println(logTextView, "unbind to SomeService...");