package com.imofan.android.pns;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import com.imofan.android.basic.MFDatabaseManager;
import com.imofan.android.basic.MFStatEvent;
import com.imofan.android.basic.MFStatInfo;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 */
public class PNBroadcastReceiver extends BroadcastReceiver {
    private final static String LOG_TAG = "Mofang_PNS_PNBroadcastReceiver";

    public final static String ACTION_NOTIFICATION = "com.imofan.pns.action.NOTIFICATION";

    @Override
    public void onReceive(Context context, Intent intent) {
        if(PNService.DEBUG) {
            PNLog.d(LOG_TAG, "[onReceive]action = " + intent.getAction());
        }
        try {
            if(ACTION_NOTIFICATION.equals(intent.getAction())) {
                //获取到推送消息时
                String token = intent.getStringExtra("token");
                if(token != null && token.equalsIgnoreCase(PNService.getToken(context))) {
                    String message = intent.getStringExtra("content");
                    PNListener listener = PNService.getNotificationListener(context);
                    if(listener != null) {
                        Notification notification = new Notification();
                        notification.setId(intent.getStringExtra("pushid"));
                        notification.setContent(message);
                        listener.onNotificationReceived(context, notification);
                    }
                }
            } else if("android.intent.action.SCREEN_ON".equals(intent.getAction())) {
                PNService.setScreenOn(true);
            } else if("android.intent.action.SCREEN_OFF".equals(intent.getAction())) {
                PNService.setScreenOn(false);
            } else if("android.intent.action.ACTION_POWER_CONNECTED".equals(intent.getAction())) {
                PNService.setPowerConnected(true);
            } else if("android.intent.action.ACTION_POWER_DISCONNECTED".equals(intent.getAction())) {
                PNService.setPowerConnected(false);
            } else if("android.net.conn.CONNECTIVITY_CHANGE".equals(intent.getAction())) {
                PNService.setNetworkChanged(true);
            } else {
                //启动MFNotificationService
                PNService.start(context, null, false);
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}
