package com.qiyou.mb.android.receivers;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import com.qiyou.mb.android.QiYouConstant;
import com.qiyou.mb.android.service.BDLocationSvc;
import com.qiyou.mb.android.service.TrackGEOService;
import com.qiyou.mb.android.utils.Utils;
import com.qiyou.mb.android.utils.log.QLog;

//这个receiver不需要注册么?
public class NetworkChangesReceiver extends BroadcastReceiver {

	public static String TAG ="com.qiyou.NetworkChangesReceiver"; 
    public static final String dTAG ="com.qiyou";
	
	 @Override
    public void onReceive(Context context, Intent intent) {
	/*	 ConnectivityManager connectMgr = (ConnectivityManager) getSystemService; 
		 NetworkInfo mobNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 
		 NetworkInfo wifiNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 
		 if (!mobNetInfo.isConnected() && !wifiNetInfo.isConnected()) { 
		 Log.i(TAG, "unconnect"); 
		 // unconnect network 
		 }else { 
		 // connect network 
		 } */
		 
	 
		 
		 String action = intent.getAction();
	        QLog.getLogger().d(dTAG, TAG+" NetworkChangesReceiver  get action " + action);
	        if(TextUtils.equals(action, QiYouConstant.ACTION_CONNECTIVITY_CHANGE )){//网络变化的时候会发送通知
	        	QiYouConstant.NETWORK_CONNECTED =Utils.isNetworkConnected(context);
	        	
	            QLog.getLogger().d(dTAG, TAG+" 网络变化了....");
	            int networkType =Utils.getNetworkType(context);
	            QLog.getLogger().d(dTAG, TAG +( networkType>-1?(networkType ==1?" WIFII 可用 ":" Mobile Network 可用"): " 网络断掉了"));
	            if(  networkType==QiYouConstant.NETWORK_TYPE_WIFII && Utils.canProcessWifiConn(context)){	
//		            SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
//		        	QiYouConstant.LAST_GEO_UPDATE =settings.getLong(QiYouConstant.PK_LONG_LAST_GEO_UPDATE, 0);  
//		        	QiYouConstant.CAN_GEO_UPDATE = System.currentTimeMillis()- QiYouConstant.LAST_GEO_UPDATE >  QiYouConstant.GEO_UPDATE_INTERVAL*1000*60;
//	        	
		          if(Utils.canUpdateGEOfootmark(context)){    
		            	//update GEO, roadbook info
		               QLog.getLogger().d(dTAG, TAG+" 开始联网 更新  GEO 信息... ");
		               Intent serviceIntent = new Intent(context, TrackGEOService.class); 
				       serviceIntent.putExtra(QiYouConstant.ACTION_CONNECTIVITY_CHANGE, "True");
				       context.startService(serviceIntent);  
			        }
			       if(Utils.canUpdateOffmap(context)){ //update offmap
			    	   Intent serviceIntent = new Intent(context, BDLocationSvc.class);  
					   serviceIntent.putExtra(QiYouConstant.EXTRA_KEY_NETWORK_LOCATION,QiYouConstant.EXTRA_KEY_GET_CURRENT_CITY); 
				       context.startService(serviceIntent); 
			       }
			       
			       
	            }
	            
	           
	        }
    }
}
