package com.qiyou.mb.android.service;

import android.app.IntentService;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.util.Log; 
import com.qiyou.mb.android.QiYouApplication;
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 DBservice extends IntentService { 
	public static String TAG ="com.qiyou.DBservice";
	public static final String dTAG ="com.qiyou";
	public DBservice(String name) {
		super(name);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void onHandleIntent(Intent arg0) {
		// TODO Auto-generated method stub

	}
	
 
	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(); 
	 }
}
