package com.qiyou.mb.android.service;

import android.app.Service;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.os.IBinder;
import android.util.Log; 
import com.qiyou.mb.android.QiYouApplication;
import com.qiyou.mb.android.QiYouConstant;
import com.qiyou.mb.android.content_providers.TrackProvider;
import com.qiyou.mb.android.utils.Utils;
import com.qiyou.mb.android.utils.log.QLog;

public class DBserviceNew extends Service { 
	public static String TAG ="com.qiyou.DBserviceNew";
	public static final String dTAG ="com.qiyou";
//	public DBserviceNew(String name) {
//		super(name);
//		// TODO Auto-generated constructor stub
//	}


	@Override
	public void onStart(Intent intent, int startId) {
		QLog.getLogger().d(dTAG,TAG+"... service TimtickService onStart.....," );
		if(intent!=null)
			handleIntent(intent);
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId){
		QLog.getLogger().d(dTAG,TAG+"... service TimtickService onStartCommand.....," );
		handleIntent(intent);
	    // We want this service to continue running until it is explicitly
	    // stopped, so return sticky.
		 return START_STICKY;		
	}
	protected  void handleIntent(Intent intent){
	    	
	    	//to check if the application restart with new instances or not
	    	// The check result is, the new application instance created, the original referenced application still exits.
	    	
	    	//TODO: to find what cause creating new application instances?
	    	
//	    	String debugstring="Time Tick regist at: "+ registTime+ ",  Application instance start time is:" + app.startTime +",Time past:"+( app.startTime -registTime);
//	    	QLog.getLogger().d(dTAG,TAG+ debugstring);
//	    	Utils.logStackTrace(new Exception(debugstring),dTAG);
	    	
	    	
//	    	String key = QiYouConstant.EXTRA_KEY_TICK_UPDATE; 
	    	 
	    }
 
	protected ContentResolver contentResolver;
	protected String[] projection;
 	protected QiYouApplication app;
	

	 @Override
	  public void onCreate() {
	    super.onCreate();
	    app =(QiYouApplication)this.getApplication();
	    contentResolver = getContentResolver();
 	  }
 
	
	 
	
	/*
	 * update  track
	 */
	protected void updateTable(Uri uri,ContentValues values,int  Id,String[] wps)
	{
		if(values ==null || values.size()==0) return;
		 	 
		if(Id <=0){ 
			Log.e(dTAG,TAG+" Only can updated tracks that in db already, turn to add new track.");
//			addNewWayPoints(); //new track
			return;
		}
	 
		    // Update the TrackProvider
//		    String where = TrackProvider.KEY_ID + " = '" + Id + "'";
		    QLog.getLogger().d(dTAG,TAG+" "+ "update Track where id is " + Id );
		    try {  
		    	 uri = Uri.withAppendedPath(TrackProvider.TRACKS_URI, String.valueOf(Id));
		      if (contentResolver.update(uri, values, null, wps) == 0) {
		    	  //没有找到记录,新增track.
		    	  Log.e(dTAG,TAG+" "+"没有可匹配更新。");
//		    	  tb.setTrackId(0);
//		    	  addNewWayPoints();  
		      }  
	    	  
		    }
		    catch (Exception er) { 
		      Log.e(dTAG, TAG+ " Update Tracks db Adding/Update failed.");
		      Utils.logStackTrace(er, dTAG);
		    }
		    
														 
											    
	}
	 @Override
	public void onDestroy() { 
	       super.onDestroy(); 
	 }

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
}
