package com.dysania.artofandroid.chapter02.aidl;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
import com.dysania.artofandroid.chapter02.R;
import java.util.List;

/**
 * Created by DysaniazzZ on 31/05/2017.
 */

public class BookManagerActivity extends AppCompatActivity {

    private static final String TAG = "BookManagerActivity";
    private static final int MESSAGE_NEW_BOOK_ARRIVED = 1;

    private IBookManager mBookManager;

    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MESSAGE_NEW_BOOK_ARRIVED:
                    Log.i(TAG, "receive new book: " + msg.obj);
                    break;
                default:
                    super.handleMessage(msg);
            }
        }
    };

    private ServiceConnection mConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mBookManager = IBookManager.Stub.asInterface(service);
            try {
                List<Book> bookList = mBookManager.getBookList();
                Log.i(TAG, "query book list, list type: " + bookList.getClass().getCanonicalName());
                Log.i(TAG, "query book list: " + bookList.toString());

                Book newBook = new Book(3, "Symbian");
                mBookManager.addBook(newBook);
                Log.i(TAG, "add new book: " + newBook);

                List<Book> newBookList = mBookManager.getBookList();
                Log.i(TAG, "query book list: " + newBookList.toString());

                mBookManager.registerListener(mIOnNewBookArrivedListener);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mBookManager = null;
            Log.e(TAG, "binder died.");
        }
    };

    private IOnNewBookArrivedListener mIOnNewBookArrivedListener = new IOnNewBookArrivedListener.Stub() {

        @Override
        public void onNewBookArrivedListener(Book newBook) throws RemoteException {
            //该方法运行在Binder连接池中，需要Handler切换到UI线程
            mHandler.obtainMessage(MESSAGE_NEW_BOOK_ARRIVED, newBook).sendToTarget();
        }
    };

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, BookManagerActivity.class);
        context.startActivity(intent);
    }

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

        Intent intent = new Intent(this, BookManagerService.class);
        bindService(intent, mConnection, BIND_AUTO_CREATE);

        findViewById(R.id.btn_get_book).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(BookManagerActivity.this, "click get book button", Toast.LENGTH_SHORT).show();

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        if (mBookManager != null) {
                            try {
                                List<Book> bookList = mBookManager.getBookList();
                            } catch (RemoteException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }).start();
            }
        });
    }

    @Override
    protected void onDestroy() {
        if (mBookManager != null && mBookManager.asBinder().isBinderAlive()) {
            try {
                mBookManager.unregisterListener(mIOnNewBookArrivedListener);
                Log.i(TAG, "unregister listener: " + mIOnNewBookArrivedListener);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
        unbindService(mConnection);
        super.onDestroy();
    }
}

@@ -262,24 +262,66 @@
 os.IBinder;%0A
+import android.os.IBinder.DeathRecipient;%0A
 import andro
@@ -335,16 +335,16 @@
 essage;%0A
-
 import a
@@ -1262,32 +1262,254 @@
       %7D%0A    %7D;%0A%0A
+    private DeathRecipient mDeathRecipient = new DeathRecipient() %7B%0A        @Override%0A        public void binderDied() %7B%0A            Log.e(TAG, %22binder died. tname: %22 + Thread.currentThread().getName());%0A        %7D%0A    %7D;%0A%0A
     private Serv
@@ -1736,32 +1736,105 @@
           try %7B%0A
+                mBookManager.asBinder().linkToDeath(mDeathRecipient, 0);%0A
                 
@@ -2681,21 +2681,74 @@
 G, %22
-binder died.%22
+onServiceDisconnected. tname: %22 + Thread.currentThread().getName()
 );%0A 

add the binder life listener