package com.test.BTClient;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.Telephony;
import android.telephony.PhoneStateListener;
import android.telephony.SmsMessage;
import android.telephony.TelephonyManager;
import android.widget.Toast;

public class DemoReceiver extends BroadcastReceiver {
    PhoneStateListener mListener = null;

    public static SmsMessage[] getMessageFromIntent(Intent intent) {
        SmsMessage retmeMessage[] = null;
        Bundle bundle = intent.getExtras();
        Object pdus[] = (Object[]) bundle.get("pdus");
        retmeMessage = new SmsMessage[pdus.length];
        for (int i = 0; i < pdus.length; i++) {
            byte[] bytedata = (byte[]) pdus[i];
            retmeMessage[i] = SmsMessage.createFromPdu(bytedata);
        }
        return retmeMessage;
    }

    @Override
    public void onReceive(final Context context, final Intent intent) {
        String action = intent.getAction();
        if (Telephony.Sms.Intents.SMS_RECEIVED_ACTION
                .equals(intent.getAction())) {
            SmsMessage[] msgs = getMessageFromIntent(intent);
            StringBuilder sBuilder = new StringBuilder();
            if (msgs != null && msgs.length > 0) {
                for (SmsMessage msg : msgs) {
                    sBuilder.append("接收到了短信：\n发件人是：");
                    sBuilder.append(msg.getDisplayOriginatingAddress());
                    sBuilder.append("\n------短信内容-------\n");
                    sBuilder.append(msg.getDisplayMessageBody());
                }
            }
            notify(context, intent, sBuilder.toString());
        } else if (TelephonyManager.ACTION_PHONE_STATE_CHANGED.equals(action)) {
            TelephonyManager tm = (TelephonyManager) context
                    .getSystemService(Service.TELEPHONY_SERVICE);
            if (mListener == null) {
                mListener = new PhoneStateListener() {
                    @Override
                    public void onCallStateChanged(int state,
                            String incomingNumber) {
                        // state 当前状态 incomingNumber,貌似没有去电的API
                        super.onCallStateChanged(state, incomingNumber);
                        switch (state) {
                        case TelephonyManager.CALL_STATE_IDLE:
                            DemoReceiver.this.notify(context, intent, "idle");
                            break;
                        case TelephonyManager.CALL_STATE_OFFHOOK:
                            DemoReceiver.this.notify(context, intent, "接听");
                            break;
                        case TelephonyManager.CALL_STATE_RINGING:
                            DemoReceiver.this.notify(context, intent, "响铃:来电号码"
                                    + incomingNumber);
                            // 输出来电号码
                            break;
                        }
                    }
                };

                tm.listen(mListener, PhoneStateListener.LISTEN_CALL_STATE);
            }
        }
    }

    private void notify(Context context, Intent intent, String content) {
        Toast.makeText(context, content, Toast.LENGTH_LONG).show();
        Notification.Builder builder = new Notification.Builder(context);
        builder.setLargeIcon(BitmapFactory.decodeResource(
                context.getResources(), R.drawable.icon));
        builder.setWhen(System.currentTimeMillis());
        builder.setContentTitle("broadcast: " + intent.getAction());
        builder.setContentText(content);
        NotificationManager manager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(0x0, builder.build());
    }
}
