// 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;
/*
 * Copyright (C) 2016 Tielei Zhang (zhangtielei.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.zhangtielei.demos.async.programming.introduction.servicebinding;


/**
 * 用于演示最普通的Service绑定和解绑.
 */
public class ServiceBindingDemoActivity extends AppCompatActivity implements SomeService.ServiceListener {
    /**
     * 对于Service的引用
     */
    private SomeService someService;
    /**
     * 指示本Activity是否处于running状态：执行过onResume就变为running状态。
     */
    private boolean running;

    private TextView description;
    private TextView logTextView;

    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            //解除Activity与Service的引用和监听关系
            //...
            if (someService != null) {
                someService.removeListener(ServiceBindingDemoActivity.this);
                someService = null;
            }
// 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;

/*
 * Copyright (C) 2016 Tielei Zhang (zhangtielei.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.zhangtielei.demos.async.programming.introduction.servicebinding;


/**
 * 用于演示最普通的Service绑定和解绑.
 */
public class ServiceBindingDemoActivity extends AppCompatActivity implements SomeService.ServiceListener {
    /**
     * 对于Service的引用
     */
    private SomeService someService;
    /**
     * 指示本Activity是否处于running状态：执行过onResume就变为running状态。
     */
    private boolean running;

    private TextView description;
    private TextView logTextView;

    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName name) {
            //解除Activity与Service的引用和监听关系
            //...
            if (someService != null) {
                someService.removeListener(ServiceBindingDemoActivity.this);
                someService = null;
            }
            TextLogUtil.println(logTextView, "disconnected to SomeService...");