/**
 * 
 */
package com.qiyou.mb.android.content_providers;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.qiyou.mb.android.QiYouConstant;
import com.qiyou.mb.android.utils.Utils;
import com.qiyou.mb.android.utils.log.QLog;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

/**
 * @author mwang
 *
 */
public class TrackProvider extends ContentProvider {
 
	private static final String TAG = "com.qiyou.TrackProvider";
	public static final String dTAG ="com.qiyou";
	  
	  
	 /** The underlying database */
	 private static SQLiteDatabase tracksDB;
	  
	  public static SQLiteDatabase getTracksDB() {
		return tracksDB;
	} 

	private static final String DATABASE_NAME = "tracks.db"; 
	  
	  private static final int DATABASE_VERSION =20;
	  private static final String TRACKS_TABLE = "TRACKS";
	  private static final String GEOS_TABLE = "GEOS";
	  private static final String TRACK_GEOS_TABLE = "TRACK_GEOS";
	  private static final String USERS_TABLE = "USERS";
	  private static final String GROUPS_TABLE = "GROUPS"; 
//	  private static final String CATEGORY_TABLE = "track_1";
	  private static final String CATE_TRACK_TABLE = "CATEGORY";
	  private static final String ACTIVITY_TABLE = "ACTIVITY";
	  private static final String MESSAGE_TABLE = "MESSAGE";
	  private static final String GROUP_USER_TABLE ="GROUP_USER";
	  
	  
	  
	  public static final String KEY_ID = "_id";	
	  public static final String KEY_SVR_NUMBER = "SVRNUMBER";
	  public static final String KEY_LAST_UPDATE_TIME = "LAST_UPDATETIME";
	  
	  // Tracks Column Names	
	  public static final String KEY_TRACK_NAME = "NAME";
	  public static final String KEY_TRACK_COMMENT = "COMMENT";
	  public static final String KEY_TRACK_DESC = "DESC";	 
	  public static final String KEY_TRACK_START_WP= "STARTWP";
	  public static final String KEY_TRACK_LAST_WP = "LASTWP";	  
	  public static final String KEY_TRACK_START_TIME = "START_TIME";
	  public static final String KEY_TRACK_END_TIME = "END_TIME";
	  public static final String KEY_TRACK_CONTENT = "TRACK_CONTENT";
	  public static final String KEY_TRACK_ROAD_BEARS = "ROADBEARS";
	  public static final String KEY_TRACK_USER_ID = "USERID";
	  public static final String KEY_TRACK_DISTANCE = "DISTANCE";
	  public static final String KEY_TRACK_EUP_DIST = "EUP";
	  public static final String KEY_TRACK_EDOWN_DIST = "EDOWN";
	  public static final String KEY_TRACK_TOPSPEED = "TOPSPEED";
	  public static final String KEY_TRACK_SPORT_TIME = "SPORT_TIME";
	  public static final String KEY_TRACK_PAUSE_TIME = "PAUSE_TIME";	
	  public static final String KEY_TRACK_IS_SETS = "CHECKEDIN";
	  
	  public static final String KEY_TRACK_STATUS = "STATUS";
	  public static final String KEY_TRACK_TYPE = "TYPE";
	  public static final String KEY_TRACK_OFFTIME = "OFF_TIME"; //no gps time 
	  public static final String KEY_TRACK_GPSSPAN = "GPS_SPAN"; //gpsSpan
//	  public static final String KEY_TRACK_MAXRD_WP = "maxRdWp";
//	  public static final String KEY_TRACK_MAXRD = "maxRd";
	  public static final String KEY_TRACK_MAXRD_WP = "MAX_RDWP";
	  public static final String KEY_TRACK_MAXRD = "MAR_RD";
	  public static final String KEY_TRACK_GEO_STATUS = "GEO_STATUS";
	  public static final String KEY_TRACK_MDWPS = "MD_WPS";
	  public static final String KEY_TRACK_CAL_BURNING = "CALORIES";
	  public static final String KEY_TRACK_CREATOR = "CREATOR";
	  public static final String KEY_TRACK_SRC = "SRC";
	  public static final String KEY_TRACK_AVSTATUS = "AV_STATUS";
	  public static final String KEY_TRACK_CREATE_TIME = "CREATE_TIME";
	  public static final String KEY_TRACK_5 = "TRACK_5";
	  public static final String KEY_TRACK_6 = "TRACK_6";
	  public static final String KEY_TRACK_7 = "TRACK_7";
	  public static final String KEY_TRACK_8 = "TRACK_8";
	  
	  // geos Column Names
	  public static final String KEY_GEO_PARENTID = "PARENTID";
	  public static final String KEY_GEO_TYPE = "TYPE";	
	  public static final String KEY_GEO_NAME = "NAME";
	  public static final String KEY_GEO_COMMENT = "COMMENT";
	  public static final String KEY_GEO_SVRID = "SVRID";
	  
	  public static final String KEY_GEO_LAT = "LAT";
	  public static final String KEY_GEO_LON = "LON";
	  public static final String KEY_GEO_3 = "GEO_3";
	  public static final String KEY_GEO_4 = "GEO_4";
	  public static final String KEY_GEO_5 = "GEO_5";
	  public static final String KEY_GEO_6 = "GEO_6";
	  
	  // track_geos Column Names
	  public static final String KEY_TRACK_GEO_GID = "GEO_ID";
	  public static final String KEY_TRACK_GEO_TID = "TRACK_ID";	
	  public static final String KEY_TRACK_GEO_TYPE = "TYPE"; 
	  public static final String KEY_TRACK_GEO_TIME = "TIMESTAMP";
	  public static final String KEY_TRACK_GEO_2 = "track_geo_2";
	  public static final String KEY_TRACK_GEO_3 = "track_geo_3";
	  public static final String KEY_TRACK_GEO_4 = "track_geo_4";
	  public static final String KEY_TRACK_GEO_5 = "track_geo_5";
	  public static final String KEY_TRACK_GEO_6 = "track_geo_6";
	  
	  
	  // user Column Names
//	  public static final String KEY_USER_ID = "USERID";
	  public static final String KEY_USER_NAME = "USERNAME";
	  public static final String KEY_USER_PASSWD = "PASSWD";
	  public static final String KEY_USER_LEVEL = "LEVEL";
	  public static final String KEY_USER_TYPE = "TYPE";
	  public static final String KEY_USER_SCORE = "SCORE"; 
	  public static final String KEY_USER_HONORS = "HONORS";
	  public static final String KEY_USER_GENDA = "GENDA";
	  public static final String KEY_USER_LOCATION = "LOCATION";
	  public static final String KEY_USER_MAIL = "MAIL";
	  public static final String KEY_USER_BIRTHDAY = "BIRTHDAY";
	  public static final String KEY_USER_ALBUM = "ALBUM";
	  public static final String KEY_USER_DESC = "DESC";
	  public static final String KEY_USER_GROUPS = "GROUPS";
	  public static final String KEY_USER_SIGN = "SIGN";
	  public static final String KEY_USER_STATUS = "STATUS";
	  public static final String KEY_USER_1 = "KEY_USER_1";
	  public static final String KEY_USER_2 = "KEY_USER_2";
	  public static final String KEY_USER_3 = "KEY_USER_3";
	  public static final String KEY_USER_4 = "KEY_USER_4";
	  public static final String KEY_USER_5 = "KEY_USER_5";
	  public static final String KEY_USER_6 = "KEY_USER_6";
	  public static final String KEY_USER_7 = "KEY_USER_7";
	  
	  
	  // group Column Names
//	  public static final String KEY_USER_GROUP_ID = "GROUPID";
	  public static final String KEY_GROUP_NAME = "NAME";
	  public static final String KEY_GROUP_TYPE = "TYPE";
	  public static final String KEY_GROUP_LEVEL = "LEVEL";
	  public static final String KEY_GROUP_SCORE = "SCORE";
	  public static final String KEY_GROUP_LOCATION = "LOCATION";
	  public static final String KEY_GROUP_HONORS = "HONORS";
	  public static final String KEY_GROUP_DESC = "DESC";
	  public static final String KEY_GROUP_OWNER = "OWNER";
	  public static final String KEY_GROUP_ADMINS = "ADMINS";
	  public static final String KEY_GROUP_STATUS = "STATUS";
	  public static final String KEY_GROUP_7 = "KEY_7";
	  public static final String KEY_GROUP_8 = "KEY_8";
	  public static final String KEY_GROUP_9 = "KEY_9";
	  public static final String KEY_GROUP_10 = "KEY_10";
	  public static final String KEY_GROUP_11 = "KEY_11";
	  public static final String KEY_GROUP_12 = "KEY_12";
	  public static final String KEY_GROUP_13 = "KEY_13";
	  public static final String KEY_GROUP_14 = "KEY_14";
	  public static final String KEY_GROUP_15 = "KEY_15";
	  
	  
//	  public static final String KEY_TRACK_1_COL1 = "T1COL1";
//	  public static final String KEY_CATE_DESC = "T1COL2";
//	  public static final String KEY_CATE_STATUS = "T1COL3";
//	  public static final String KEY_TRACK_1_COL4 = "T1COL4";
//	  public static final String KEY_TRACK_1_COL5 = "T1COL5";
//	  public static final String KEY_TRACK_1_COL6 = "T1COL6";
//	  public static final String KEY_TRACK_1_COL7 = "T1COL7";
//	  public static final String KEY_TRACK_1_COL8 = "T1COL8";
	  
	  // track_category Column Names
	  public static final String KEY_CATE_NAME = "T2COL1";
	  public static final String KEY_TRACK_ID = "T2COL2";
	  public static final String KEY_CATE_TRACK_STATUS = "T2COL3";
	  public static final String KEY_TRACK_2_COL4 = "T2COL4";
	  public static final String KEY_TRACK_2_COL5 = "T2COL5";
	  public static final String KEY_TRACK_2_COL6 = "T2COL6";
	  public static final String KEY_TRACK_2_COL7 = "T2COL7";
	  public static final String KEY_TRACK_2_COL8 = "T2COL8";
	  
	  // activity Column Names
	  public static final String KEY_ACTIVITY_NAME = "NAME";
	  public static final String KEY_ACTIVITY_OWNER = "OWNER"; 
	  public static final String KEY_ACTIVITY_DESC = "DESC";
	  public static final String KEY_ACTIVITY_LOCATION = "LOCATION";
	  public static final String KEY_ACTIVITY_TYPE = "TYPE";
	  public static final String KEY_ACTIVITY_ST_DATE = "ST_DATE";
	  public static final String KEY_ACTIVITY_END_DATE = "END_DATE";
	  public static final String KEY_ACTIVITY_SPONSORS = "SPONSORS";
	  public static final String KEY_ACTIVITY_STATUS = "STATUS";
//	  public static final String KEY_ACTIVITY_SVRNUMBER = "SVRNUMBER";
	  public static final String KEY_ACTIVITY_COL1 = "KEY_1";
	  public static final String KEY_ACTIVITY_COL2 = "KEY_2";
	  public static final String KEY_ACTIVITY_COL3 = "KEY_3";
	  public static final String KEY_ACTIVITY_COL4 = "KEY_4";
	  public static final String KEY_ACTIVITY_COL5 = "KEY_5";
	  public static final String KEY_ACTIVITY_COL6= "KEY_6";
	  public static final String KEY_ACTIVITY_COL7 = "KEY_7";
	  public static final String KEY_ACTIVITY_COL8 = "KEY_8";
	  
	  // GROUP_USER Column Names
	  public static final String KEY_GROUP_USER_USERID = "USERID";
	  public static final String KEY_GROUP_USER_GROUPID = "GROUPID";
	  public static final String KEY_GROUP_USER_IS_OWNER = "ISOWNER";
	  public static final String KEY_GROUP_USER_IS_ADMIN = "ISADMIN";
	  public static final String KEY_GROUP_USER_STATUS = "STATUS";
	  public static final String KEY_GROUP_USER_KEY_1 = "KEY_1";
	  public static final String KEY_GROUP_USER_KEY_2 = "KEY_2";
	  public static final String KEY_GROUP_USER_KEY_3 = "KEY_3";
	  public static final String KEY_GROUP_USER_KEY_4 = "KEY_4";
	  public static final String KEY_GROUP_USER_KEY_5 = "KEY_5";
	  public static final String KEY_GROUP_USER_KEY_6 = "KEY_6";
	  
	  // TODO Replace this URI with something unique to your own application.
	  public static final Uri TRACKS_URI = Uri.parse("content://com.qiyou.mb.android.tracks/tracks");
	  public static final Uri GEOS_URI = Uri.parse("content://com.qiyou.mb.android.tracks/geos"); 
	  public static final Uri TRACK_GEOS_URI = Uri.parse("content://com.qiyou.mb.android.tracks/track_geos"); 
//	  public static final Uri CATEGORY_URI = Uri.parse("content://com.qiyou.mb.android.tracks/"+CATEGORY_TABLE); 
	  public static final Uri CATE_TRACK_URI = Uri.parse("content://com.qiyou.mb.android.tracks/"+CATE_TRACK_TABLE); 
	  public static final Uri USER_URI = Uri.parse("content://com.qiyou.mb.android.tracks/"+USERS_TABLE); 
	  public static final Uri GROUP_URI = Uri.parse("content://com.qiyou.mb.android.tracks/"+GROUPS_TABLE); 
	  public static final Uri ACTIVITIES_URI = Uri.parse("content://com.qiyou.mb.android.tracks/"+ACTIVITY_TABLE); 
	  //Create the constants used to differentiate between the different URI requests.
	  private static final int TRACKS = 1;
	  private static final int TRACKS_ID = 2;
	  private static final int TRACKS_LIST = 3;   //show formated db content
	  
	  private static final int GEOS = 4;
	  private static final int GEOS_ID = 5;
	  private static final int GEOS_LIST = 6;   //show formated db content

	  private static final int TRACK_GEOS = 7;
	  private static final int TRACK_GEOS_ID = 8;
	  private static final int TRACK_GEOS_LIST = 9;   //show formated db content
	  
	  
//	  private static final int CATEGORIES = 10;
//	  private static final int CATEGORY_ID = 11;
//	  private static final int CATEGORY_LIST = 12;   //show formated db content
	  
	  private static final int CATEGORY_TRACKS = 13;
	  private static final int CATEGORY_TRACK_ID = 14;
	  private static final int CATEGORY_TRACK_LIST = 15;   //show formated db content
	  
	  private static final int USER = 16;
	  private static final int USER_ID = 17;
	  private static final int USER_LIST = 18;   //show formated db content
	  
	  private static final int GROUP = 19;
	  private static final int GROUP_ID = 20;
	  private static final int GROUP_LIST = 21;   //show formated db content

	  private static final int ACTIVITY = 22;
	  private static final int ACTIVITY_ID = 23;
	  private static final int ACTIVITY_LIST = 24;   //show formated db content
	  
	  //Allocate the UriMatcher object, where a URI ending in 'places' will
	  //correspond to a request for all places, and 'places' with a trailing '/[Unique ID]' will represent a single place details row.
	  private static final UriMatcher uriMatcher;
	  static {
	   uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
	   uriMatcher.addURI("com.qiyou.mb.android.tracks", "tracks", TRACKS);
	   uriMatcher.addURI("com.qiyou.mb.android.tracks", "tracks/*", TRACKS_ID);
	   uriMatcher.addURI("com.qiyou.mb.android.tracks", "tracks/list/", TRACKS_LIST);
	   
	   uriMatcher.addURI("com.qiyou.mb.android.tracks", "geos", GEOS);
	   uriMatcher.addURI("com.qiyou.mb.android.tracks", "geos/*", GEOS_ID);
	   uriMatcher.addURI("com.qiyou.mb.android.tracks", "geos/list/", GEOS_LIST);
	   
	   uriMatcher.addURI("com.qiyou.mb.android.tracks", "track_geos", TRACK_GEOS);
	   uriMatcher.addURI("com.qiyou.mb.android.tracks", "track_geos/*", TRACK_GEOS_ID);
	   uriMatcher.addURI("com.qiyou.mb.android.tracks", "track_geos/list/", TRACK_GEOS_LIST);
	   
	   uriMatcher.addURI("com.qiyou.mb.android.tracks", CATE_TRACK_TABLE, CATEGORY_TRACKS);
	   uriMatcher.addURI("com.qiyou.mb.android.tracks", CATE_TRACK_TABLE+"/*", CATEGORY_TRACK_ID);
	   uriMatcher.addURI("com.qiyou.mb.android.tracks", CATE_TRACK_TABLE+"/list/", CATEGORY_TRACK_LIST);
	   
	   uriMatcher.addURI("com.qiyou.mb.android.tracks",  USERS_TABLE , USER);
	   uriMatcher.addURI("com.qiyou.mb.android.tracks",  USERS_TABLE+"/*", USER_ID);
	   uriMatcher.addURI("com.qiyou.mb.android.tracks",  USERS_TABLE+"/list/", USER_LIST);
	   
	   uriMatcher.addURI("com.qiyou.mb.android.tracks",  GROUPS_TABLE , GROUP);
	   uriMatcher.addURI("com.qiyou.mb.android.tracks",  GROUPS_TABLE+"/*", GROUP_ID);
	   uriMatcher.addURI("com.qiyou.mb.android.tracks",  GROUPS_TABLE+"/list/", GROUP_LIST);
	   
	   uriMatcher.addURI("com.qiyou.mb.android.tracks",  ACTIVITY_TABLE , ACTIVITY);
	   uriMatcher.addURI("com.qiyou.mb.android.tracks",  ACTIVITY_TABLE+"/*", ACTIVITY_ID);
	   uriMatcher.addURI("com.qiyou.mb.android.tracks",  ACTIVITY_TABLE+"/list/", ACTIVITY_ID);
	   
	  }
	  
	  @Override
	  public boolean onCreate() {
	    Context context = getContext();
	    Utils.initSdPath(context);
	    PlacesDatabaseHelper dbHelper = new PlacesDatabaseHelper(context, DATABASE_NAME, 
	                                                             null, DATABASE_VERSION);
	    try {
	    	
	    	 if(QiYouConstant.DEVELOPER_MODE){
	    		 
	    		    File path = new File(QiYouConstant.SD_PATH);// 创建目录   
	    		    File f = new File(QiYouConstant.SD_PATH + DATABASE_NAME);// 创建文件  
	    		 if (!path.exists()) {// 目录存在返回false  
			          path.mkdirs();// 创建一个目录  
			      }  
			      if (!f.exists()) {// 文件存在返回false  
			          try {  
			              f.createNewFile();//创建文件   //TODO: 机身内存创建文件出错.
			              tracksDB =SQLiteDatabase.openOrCreateDatabase(f, null);
			              dbHelper.onCreate(tracksDB);
			              // QLog.getLogger().d(dTAG,TAG+" "+ "Database Created as  " +f);	 
			          } catch (IOException e) {  
			              // TODO Auto-generated catch block  
			              e.printStackTrace();  
			              Utils.logStackTrace(e, dTAG);
			          }  
			      } else{
			    	  tracksDB =SQLiteDatabase.openOrCreateDatabase(f, null);
			    	   // QLog.getLogger().d(dTAG,TAG+" "+ "Database Openned " +f);	 
			      }
			     
			    
			    
	    	 }else{
	    		 tracksDB = dbHelper.getWritableDatabase();
	    	 }     
		    
     	
	    } catch (SQLiteException e) {
	    	tracksDB = null; 
	      Log.e(dTAG,TAG+" "+ "Database Opening exception,"+e.getStackTrace());
	      Utils.logStackTrace(e, dTAG);
	    }
	    
	    return (tracksDB == null) ? false : true;
	  }

	  @Override
	  public String getType(Uri uri) {
	    switch (uriMatcher.match(uri)) {
	      case TRACKS:    return "vnd.android.cursor.dir/tracks";
	      case TRACKS_ID: return "vnd.android.cursor.item/track";
	      case TRACKS_LIST: return "vnd.android.cursor.dir/tracks/list";
	      
	      case GEOS:    return "vnd.android.cursor.dir/geos";
	      case GEOS_ID: return "vnd.android.cursor.item/geo";
	      case GEOS_LIST: return "vnd.android.cursor.dir/geos/list";
	      
	      case TRACK_GEOS:    return "vnd.android.cursor.dir/track_geos";
	      case TRACK_GEOS_ID: return "vnd.android.cursor.item/track_geo";
	      case TRACK_GEOS_LIST: return "vnd.android.cursor.dir/track_geos/list";
	      
	      case CATEGORY_TRACKS:    return "vnd.android.cursor.dir/categories";
	      case CATEGORY_TRACK_ID: return "vnd.android.cursor.item/category";
	      case CATEGORY_TRACK_LIST: return "vnd.android.cursor.dir/categories/list";
	      
//	      case CATEGORY_TRACKS:    return "vnd.android.cursor.dir/cate_tracks";
//	      case CATEGORY_TRACK_ID: return "vnd.android.cursor.item/cate_track";
//	      case CATEGORY_TRACK_LIST: return "vnd.android.cursor.dir/cate_tracks/list"; 
	      
	      case USER:    return "vnd.android.cursor.dir/users";
	      case USER_ID: return "vnd.android.cursor.item/user";
	      case USER_LIST: return "vnd.android.cursor.dir/users/list"; 
	      
	      case GROUP:    return "vnd.android.cursor.dir/groups";
	      case GROUP_ID: return "vnd.android.cursor.item/group";
	      case GROUP_LIST: return "vnd.android.cursor.dir/groups/list"; 
		   
	      case ACTIVITY:    return "vnd.android.cursor.dir/activities";
	      case ACTIVITY_ID: return "vnd.android.cursor.item/activity";
	      case ACTIVITY_LIST: return "vnd.android.cursor.dir/activities/list"; 
	      
	      default: throw new IllegalArgumentException("Unsupported URI: " + uri);
	    }
	  }
	/**
	 * 
	 */
	public TrackProvider() {
		// TODO Auto-generated constructor stub
	}

	/* (non-Javadoc)
	 * @see android.content.ContentProvider#delete(android.net.Uri, java.lang.String, java.lang.String[])
	 */
	@Override
	 public int delete(Uri uri, String where, String[] whereArgs) {
	    int count;
	    String segment;
	    switch (uriMatcher.match(uri)) {
	      case TRACKS:{
	        count = tracksDB.delete(TRACKS_TABLE, where, whereArgs);
	        break;
	      }

	      case TRACKS_ID:{
	        segment = uri.getPathSegments().get(1);
	        count = tracksDB.delete(TRACKS_TABLE, KEY_ID + "="
	                                    + segment
	                                    + (!TextUtils.isEmpty(where) ? " AND (" 
	                                    + where + ')' : ""), whereArgs);
	        break;
	      }
	        
	      case GEOS:{
		        count = tracksDB.delete(GEOS_TABLE, where, whereArgs);
		        break;
	      }
	      case GEOS_ID:{
	        segment = uri.getPathSegments().get(1);
	        count = tracksDB.delete(GEOS_TABLE, KEY_ID + "="
	                                    + segment
	                                    + (!TextUtils.isEmpty(where) ? " AND (" 
	                                    + where + ')' : ""), whereArgs);		        
	        break;
	      }
	      
	      
	      case TRACK_GEOS:{
		        count = tracksDB.delete(TRACK_GEOS_TABLE, where, whereArgs);
		        break;
	      }
	      case TRACK_GEOS_ID:{
	        segment = uri.getPathSegments().get(1);
	        count = tracksDB.delete(TRACK_GEOS_TABLE, KEY_ID + "="
	                                    + segment
	                                    + (!TextUtils.isEmpty(where) ? " AND (" 
	                                    + where + ')' : ""), whereArgs);		        
	        break;
	      } 
	      
	      case CATEGORY_TRACKS:{
		        count = tracksDB.delete(CATE_TRACK_TABLE, where, whereArgs);
		        break;
		      }

	      case CATEGORY_TRACK_ID:{
	        segment = uri.getPathSegments().get(1);
	        count = tracksDB.delete(CATE_TRACK_TABLE, KEY_ID + "="
	                                    + segment
	                                    + (!TextUtils.isEmpty(where) ? " AND (" 
	                                    + where + ')' : ""), whereArgs);
	        break;
	      }
		        
	      default: throw new IllegalArgumentException("Unsupported URI: " + uri);
	    }

	    getContext().getContentResolver().notifyChange(uri, null);
	    return count;
	  }

	/* (non-Javadoc)
	 * @see android.content.ContentProvider#insert(android.net.Uri, android.content.ContentValues)
	 */
	@Override
	public Uri insert(Uri _uri, ContentValues _initialValues) {
		 // Insert the new row, will return the row number if successful.
	  try{
		  long rowID ;
		   switch (uriMatcher.match(_uri)) {
		   case TRACKS:{
			   rowID = tracksDB.insert(TRACKS_TABLE, "nullhack", _initialValues);
			   if (rowID > 0) {
				      Uri uri = ContentUris.withAppendedId(TRACKS_URI, rowID);
				      getContext().getContentResolver().notifyChange(uri, null);
				      return uri;
				    }
		        break;
		   }
		   case GEOS:{
			   rowID = tracksDB.insert(GEOS_TABLE, "nullhack", _initialValues);
			   if (rowID > 0) {
				      Uri uri = ContentUris.withAppendedId(GEOS_URI, rowID);
				      getContext().getContentResolver().notifyChange(uri, null);
				      return uri;
				    }
		        break;    
		   }
		   case TRACK_GEOS:{
			   rowID = tracksDB.insert(TRACK_GEOS_TABLE, "nullhack", _initialValues);
			   if (rowID > 0) {
				      Uri uri = ContentUris.withAppendedId(TRACK_GEOS_URI, rowID);
				      getContext().getContentResolver().notifyChange(uri, null);
				      return uri;
				    }
		        break;    
		   }
		   
		  /* case CATEGORIES:{
			   rowID = tracksDB.insert(CATEGORY_TABLE, "nullhack", _initialValues);
			   if (rowID > 0) {
				      Uri uri = ContentUris.withAppendedId(CATEGORY_URI, rowID);
				      getContext().getContentResolver().notifyChange(uri, null);
				      return uri;
				    }
		        break;    
		   }*/
		   case CATEGORY_TRACKS:{
			   rowID = tracksDB.insert(CATE_TRACK_TABLE, "nullhack", _initialValues);
			   if (rowID > 0) {
				      Uri uri = ContentUris.withAppendedId(CATE_TRACK_URI, rowID);
				      getContext().getContentResolver().notifyChange(uri, null);
				      return uri;
				    }
		        break;    
		   }
		   
		   }
		  
		  
		/*long rowID = tracksDB.insert(TRACKS_TABLE, "nullhack", _initialValues);
	          
	    // Return a URI to the newly inserted row on success.
	    if (rowID > 0) {
	      Uri uri = ContentUris.withAppendedId(TRACKS_URI, rowID);
	      getContext().getContentResolver().notifyChange(uri, null);
	      return uri;
	    }*/
	 }catch(Exception e)
	 {
		 Log.e(dTAG,TAG+" Failed to insert a new record.");
		 Utils.logStackTrace(e, dTAG);
	 }
	  return _uri;
//	    throw new SQLException("Failed to insert row into " + _uri);
	}

	 

	/* (non-Javadoc)
	 * @see android.content.ContentProvider#query(android.net.Uri, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String)
	 */
	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sort) {
		
		
		  SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
		   
		  switch (uriMatcher.match( uri)) {
		   case TRACKS: {
			   qb.setTables(TRACKS_TABLE);
			   break;		    
		   } 
		   case TRACKS_ID: {
			   qb.setTables(TRACKS_TABLE);
			   qb.appendWhere(KEY_ID + "=" + uri.getPathSegments().get(1)); 
//		      QLog.getLogger().d(dTAG,TAG+" "+ "query,  qb.appendWhere: "+ KEY_ID + "=" + uri.getPathSegments().get(1));
		                     break;
		      }
		   
		   
		   case GEOS:{
			   qb.setTables(GEOS_TABLE);
			   break;
		   }	    
		   case GEOS_ID: {
			   qb.setTables(GEOS_TABLE);
			   qb.appendWhere(KEY_ID + "=" + uri.getPathSegments().get(1)); 
//		      QLog.getLogger().d(dTAG,TAG+" "+ "query,  qb.appendWhere: "+ KEY_ID + "=" + uri.getPathSegments().get(1));
		                     break;
		      }
		   
		   
		   case TRACK_GEOS:{
			   qb.setTables(TRACK_GEOS_TABLE);
			   break;
		   }	    
		   case TRACK_GEOS_ID: {
			   qb.setTables(TRACK_GEOS_TABLE);
			   qb.appendWhere(KEY_ID + "=" + uri.getPathSegments().get(1)); 
//		      QLog.getLogger().d(dTAG,TAG+" "+ "query,  qb.appendWhere: "+ KEY_ID + "=" + uri.getPathSegments().get(1));
		                     break;
		      } 
		   
		   
		   
		  /* case CATEGORIES:{
			   qb.setTables(CATEGORY_TABLE);
			   break;
		   }	    
		   case CATEGORY_ID: {
			   qb.setTables(CATEGORY_TABLE);
			   qb.appendWhere(KEY_ID + "=" + uri.getPathSegments().get(1)); 
//		       QLog.getLogger().d(dTAG,TAG+" "+ "query,  qb.appendWhere: "+ KEY_ID + "=" + uri.getPathSegments().get(1));
		       break;
		      } */
		   
		   case CATEGORY_TRACKS:{
			   qb.setTables(CATE_TRACK_TABLE);
			   // QLog.getLogger().d(dTAG,TAG+" "+ "query,  CATE_TRACK_TABLE is " + CATE_TRACK_TABLE);
			   break;
		   }	    
		   case CATEGORY_TRACK_ID: {
			   qb.setTables(CATE_TRACK_TABLE);
			   qb.appendWhere(KEY_ID + "=" + uri.getPathSegments().get(1)); 
		       // QLog.getLogger().d(dTAG,TAG+" "+ "query,  qb.appendWhere: "+ KEY_ID + "=" + uri.getPathSegments().get(1));
		       break;
		      } 
		   default      : break;
		  }
		 

		    
		    
		  

		    // If no sort order is specified sort by date / time
		    String orderBy;
		    if (TextUtils.isEmpty(sort)) {
		      orderBy = TrackProvider.KEY_LAST_UPDATE_TIME + " DESC";
		    } else {
		      orderBy = sort;
		    }
//		    QLog.getLogger().d(dTAG,TAG+" query,  tables =" +qb.getTables());
		    // Apply the query to the underlying database.
		    Cursor c = null;
			try {
				c = qb.query(tracksDB, 
				                    projection, 
				                    selection, selectionArgs, 
				                    null, null, orderBy);
				if(tracksDB ==null)
				Log.e(dTAG,TAG+" "+  "com.qiyou.mb faild to open tracks db.");
		    // Register the contexts ContentResolver to be notified if
		    // the cursor result set changes. 
		    c.setNotificationUri(getContext().getContentResolver(), uri);	
			} catch (Exception e) {
				// TODO Auto-generated catch block
			 
				Utils.logStackTrace(e, dTAG);
			}

		    
		    // Return a cursor to the query result.
		    return c;
	}

	/* (non-Javadoc)
	 * @see android.content.ContentProvider#update(android.net.Uri, android.content.ContentValues, java.lang.String, java.lang.String[])
	 */
	@Override
	public int update(Uri uri, ContentValues values, String where, String[] newWps) {
	    int count =0;
	    String[] whereArgs=null;
//	    QLog.getLogger().d(dTAG,TAG+" "+ "uri is "  + uri  ); 
	 
	    switch (uriMatcher.match(uri)) {
	      case TRACKS: count = tracksDB.update(TRACKS_TABLE, values, where, whereArgs);
	    
	                   break;
	    /*  case CATEGORIES: count = tracksDB.update(CATEGORY_TABLE, values, where, whereArgs); 
          break;*/
	      case CATEGORY_TRACKS: count = tracksDB.update(CATE_TRACK_TABLE, values, where, whereArgs);
		    
          break;
	      case TRACKS_ID: String segment = uri.getPathSegments().get(1);
//	      		QLog.getLogger().d(dTAG,TAG+" "+ "Update db by id: " +KEY_ID + "=" + segment  );
	      		 tracksDB.beginTransaction();
	      		 try {
	      			  count = tracksDB.update(TRACKS_TABLE, values, KEY_ID 
	                             + "=" + segment 
	                             + (!TextUtils.isEmpty(where) ? " AND (" 
	                             + where + ')' : ""), whereArgs);
	                     if(newWps!=null && newWps.length==3){	                     
	                    	 //TODO: check to use sqlite statement for better performance.
 	                   
	                    	 int cnt =1;
	                    	 String mSql = "update "+ TRACKS_TABLE    +" set ";
 
							 if(!TextUtils.isEmpty(newWps[0])){
//								 QLog.getLogger().d(dTAG,TAG+" "+ "Update track wps is:"  + newWps[0]  );	 
								 mSql +=  KEY_TRACK_CONTENT + " = (" + KEY_TRACK_CONTENT + " ||?)"  ; 
							 	   
								 cnt++; 	
								
							 }
							 if(!TextUtils.isEmpty(newWps[1])){
//								 QLog.getLogger().d(dTAG,TAG+" "+ "Update rd wps is:"  + newWps[1]  );	 
								 if(cnt>1) mSql += ",";
								 mSql +=  KEY_TRACK_ROAD_BEARS + " = (" + KEY_TRACK_ROAD_BEARS + " ||?)" ;
								cnt++;	 
								
							 }
							 
							 if(!TextUtils.isEmpty(newWps[2])){
//							 	 QLog.getLogger().d(dTAG,TAG+" "+ "Update md wps is:"  + newWps[2]  );
								 if(cnt>1)mSql += ",";
								 mSql += KEY_TRACK_MDWPS + " = (" +"ifnull(" + TrackProvider.KEY_TRACK_MDWPS +","+ "',')" + " ||?)"; 
//								 mSql += "ifnull(" + TrackProvider.KEY_TRACK_MDWPS +",\",\")"  + " = (" + KEY_TRACK_MDWPS + " ||?)";
								cnt++;	 	 
							 }
							  
						
						  if(cnt>1){

							  mSql +=  " where "
	                    		 + KEY_ID 		                        
	                             + "=?"  
	                             + (!TextUtils.isEmpty(where) ? " AND (" 
	                             + where + ')' : "");
							  
							 Object[] wps = new Object[cnt]; 
							 int i=0;
							 if(!TextUtils.isEmpty(newWps[0])){
								 wps[i] =newWps[0];
								 i++;
							 }
							 if(!TextUtils.isEmpty(newWps[1])){
								 wps[i] =newWps[1];
								 i++;
							 }
							 if(!TextUtils.isEmpty(newWps[2])){ 
								 wps[i] =newWps[2];
								 i++;
							 }
							 wps[i]=segment;
//					        QLog.getLogger().d(dTAG,TAG+" Update mSql is:" + mSql ); 
							 tracksDB.execSQL(mSql,wps);
							 
						  } 
						  
//		                  Object[] wps ={newWps[0],newWps[1],segment};
		                    	 
					      
							 
//	                    		QLog.getLogger().d(dTAG,TAG+" Update trackcontent for wps, mSql is:" + mSql );
//	                    		QLog.getLogger().d(dTAG,TAG+" "  + newWps[0].toString() );
//							    QLog.getLogger().d(dTAG,TAG+" newWps[1] "  + newWps[1].toString() );
//	                    		QLog.getLogger().d(dTAG,TAG+" updated track id of "  + segment );
	                    		
	                    		
	                    										
	      		       
	                     } 
	                     tracksDB.setTransactionSuccessful();
	                     }catch(Exception er) { 
	                    	 Utils.logStackTrace(er, dTAG);
	                    } finally {
		      		      tracksDB.endTransaction();
		      		   } 
	      		 break;

	      default: throw new IllegalArgumentException("Unknown URI " + uri);
	    }

	    getContext().getContentResolver().notifyChange(uri, null);
	    return count;
	  }
	 
	
	 
	 /*
		public int updateTrackWps(Uri uri, ContentValues values,String newWps, String where, String[] whereArgs) {
		    int count;
		    switch (uriMatcher.match(uri)) {
		      case TRACKS: count = tracksDB.update(TRACKS_TABLE, values, where, whereArgs);
		                   break;

		      case TRACKS_ID: String segment = uri.getPathSegments().get(1);
		                     count = tracksDB.update(TRACKS_TABLE, values, KEY_ID 
		                             + "=" + segment 
		                             + (!TextUtils.isEmpty(where) ? " AND (" 
		                             + where + ')' : ""), whereArgs);
		                     String mSql = "update tracks set "+ KEY_TRACK_CONTENT 
		                    		 + " = "+ KEY_TRACK_CONTENT + newWps 
		                    		 + " where "
		                    		 + KEY_ID 
		                             + "=" + segment 
		                             + (!TextUtils.isEmpty(where) ? " AND (" 
		                             + where + ')' : "");
		                    		 
		                     tracksDB.execSQL(mSql);
		                     break;

		      default: throw new IllegalArgumentException("Unknown URI " + uri);
		    }

		    getContext().getContentResolver().notifyChange(uri, null);
		    return count;
		  }
	 */
	  
	  // Helper class for opening, creating, and managing database version control
	  private static class PlacesDatabaseHelper extends SQLiteOpenHelper {
		  
	   
	    private static final String CREATE_TRACK_TABLE =  "create table " + TRACKS_TABLE + " (" 
	      + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "	      
	      + KEY_TRACK_NAME + " TEXT, "  
	      + KEY_TRACK_USER_ID + " INTEGER, "
	      + KEY_TRACK_COMMENT + " TEXT, " 
	      + KEY_TRACK_DESC + " TEXT, "
	      + KEY_TRACK_START_WP + " TEXT, "   
	      + KEY_TRACK_LAST_WP + " TEXT, "	  
	      + KEY_TRACK_START_TIME + " LONG, "
	      + KEY_TRACK_END_TIME + " LONG, "
	      + KEY_TRACK_DISTANCE + " LONG, "  
	      + KEY_TRACK_EUP_DIST + " LONG, "
	      + KEY_TRACK_EDOWN_DIST + " LONG, "
	      + KEY_TRACK_TOPSPEED + " LONG, "
	      + KEY_TRACK_SPORT_TIME + " LONG, "
	      + KEY_TRACK_PAUSE_TIME + " LONG, "   
	      + KEY_TRACK_CONTENT + " TEXT, "
	      + KEY_TRACK_IS_SETS + " INTEGER, "	     
	      + KEY_SVR_NUMBER + " INTEGER, "
	      + KEY_TRACK_STATUS + " INTEGER, "
	      + KEY_TRACK_TYPE + " INTEGER, "
	      + KEY_TRACK_OFFTIME + " LONG, "   
 	      + KEY_TRACK_GPSSPAN + " LONG, "  
 	      + KEY_TRACK_ROAD_BEARS + " TEXT, "
	      + KEY_TRACK_MAXRD_WP + " TEXT, "
	      + KEY_TRACK_MAXRD + " TEXT, "
	      + KEY_TRACK_GEO_STATUS + " INTEGER, "
	      + KEY_TRACK_MDWPS + " TEXT, "
	      + KEY_TRACK_CAL_BURNING + " LONG, "
	       + KEY_TRACK_CREATOR + " TEXT, "	      
	       + KEY_TRACK_SRC + " TEXT, "	  
	       + KEY_TRACK_AVSTATUS + " TEXT, "	  
	       + KEY_TRACK_CREATE_TIME + " LONG, "	  
	       + KEY_TRACK_5 + " TEXT, "	  
	       + KEY_TRACK_6 + " TEXT, "	  
	       + KEY_TRACK_7 + " TEXT, "	  
	       + KEY_TRACK_8 + " TEXT, "	  
	       
	      + KEY_LAST_UPDATE_TIME + " LONG" 
	      	
	      +"); ";
	    
	 
	    private static final String CREATE_GEO_TABLE = "create table " + GEOS_TABLE + " (" 
	  	      + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "	   
	  	      + KEY_GEO_PARENTID + " INTEGER, "
	  	      + KEY_GEO_NAME + " TEXT, " 
		      + KEY_GEO_TYPE + " INTEGER, " 
		      + KEY_GEO_COMMENT + " TEXT, "	 
		      + KEY_SVR_NUMBER + " INTEGER, "
	          + KEY_GEO_LAT + " TEXT, "
	          + KEY_GEO_LON + " TEXT, "	
	          + KEY_GEO_3 + " TEXT, "	
	          + KEY_GEO_4 + " TEXT, "
	          + KEY_GEO_5 + " TEXT, "	
	          + KEY_GEO_6 + " TEXT, "	
	            
	          + KEY_LAST_UPDATE_TIME + " LONG); ";
	    
	    private static final String CREATE_TRACK_GEO_TABLE = "create table " + TRACK_GEOS_TABLE + " (" 
		  	      + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "	   
		  	      + KEY_TRACK_GEO_GID + " INTEGER, "
		  	      + KEY_TRACK_GEO_TID + " INTEGER, "
		  	      + KEY_TRACK_GEO_TYPE + " INTEGER, "	
		  	      + KEY_SVR_NUMBER + " INTEGER, "
		          + KEY_TRACK_GEO_TIME + " TEXT, "
		          + KEY_TRACK_GEO_2 + " TEXT, "	
		          + KEY_TRACK_GEO_3 + " TEXT, "	
		          + KEY_TRACK_GEO_4 + " TEXT, "
		          + KEY_TRACK_GEO_5 + " TEXT, "	
		          + KEY_TRACK_GEO_6 + " TEXT, "	
		          + KEY_LAST_UPDATE_TIME + " LONG); "; 
		  
	    private static final String CREATE_USER_TABLE = "create table " + USERS_TABLE + " (" 
		  	      + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "	
		  	      + KEY_SVR_NUMBER + " INTEGER, "	
		  	      + KEY_USER_PASSWD + " INTEGER, " 
		  	      + KEY_USER_NAME + " TEXT, "
		  	      + KEY_USER_TYPE + " INTEGER, "	
		  	      + KEY_USER_LEVEL + " INTEGER, "
		          + KEY_USER_SCORE + " INTEGER, "
		          + KEY_USER_GENDA + " INTEGER, "	
		          + KEY_USER_LOCATION + " TEXT, "	 
		          + KEY_USER_MAIL + " TEXT, "
		          + KEY_USER_BIRTHDAY+ " TEXT, "
		          + KEY_USER_ALBUM + " TEXT, " 
		          + KEY_USER_DESC + " TEXT, "
		          + KEY_USER_GROUPS+ " TEXT, "
		          + KEY_USER_STATUS+ " TEXT, "
		          + KEY_USER_SIGN + " TEXT, "
		          + KEY_USER_HONORS + " TEXT, " 
		          + KEY_USER_1 + " TEXT, "
		          + KEY_USER_2+ " TEXT, "
		          + KEY_USER_3 + " TEXT, " 
		          + KEY_USER_4 + " TEXT, "
		          + KEY_USER_5+ " TEXT, "
		          + KEY_USER_6 + " TEXT, "
		          + KEY_USER_7 + " TEXT, "
		          
		          + KEY_LAST_UPDATE_TIME + " LONG); ";
	    
	   
	    private static final String CREATE_GROUPS_TABLE = "create table " + GROUPS_TABLE + " (" 
		  	      + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "	 
		  	       + KEY_SVR_NUMBER + " INTEGER, "  
		  	      + KEY_GROUP_NAME + " TEXT, "
		  	      + KEY_GROUP_TYPE + " INTEGER, "	
		  	      + KEY_GROUP_LEVEL + " INTEGER, "  
		          + KEY_GROUP_SCORE + " TEXT, "
		          + KEY_GROUP_LOCATION+ " TEXT, "
		          + KEY_GROUP_HONORS + " TEXT, " 
		          + KEY_GROUP_DESC + " TEXT, "
		          + KEY_GROUP_OWNER+ " TEXT, "
		          + KEY_GROUP_ADMINS + " TEXT, " 
		           + KEY_GROUP_STATUS+ " TEXT, "
		           + KEY_GROUP_7 + " TEXT, "
		          + KEY_GROUP_8+ " TEXT, "
		          + KEY_GROUP_9 + " TEXT, " 
		          + KEY_GROUP_10 + " TEXT, "
		          + KEY_GROUP_11+ " TEXT, "
		          + KEY_GROUP_12 + " TEXT, " 
		          
		          + KEY_LAST_UPDATE_TIME + " LONG); ";
		 
	    
	    private static final String CREATE_GROUP_USER = "create table " + GROUP_USER_TABLE + " (" 
		  	      + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "	   
		  	      + KEY_GROUP_USER_USERID + " INTEGER, "
		  	      + KEY_GROUP_USER_GROUPID + " INTEGER, "
		  	      + KEY_GROUP_USER_IS_OWNER + " INTEGER, "	
		  	      + KEY_GROUP_USER_IS_ADMIN + " INTEGER, " 
		  	      + KEY_GROUP_USER_STATUS + " INTEGER, "
		          + KEY_GROUP_USER_KEY_1 + " TEXT, "
		          + KEY_GROUP_USER_KEY_2+ " TEXT, "
		          + KEY_GROUP_USER_KEY_3 + " TEXT, " 
		          + KEY_GROUP_USER_KEY_4 + " TEXT, " 
		          + KEY_GROUP_USER_KEY_5 + " TEXT, " 
		          + KEY_GROUP_USER_KEY_6 + " TEXT, " 
		          + KEY_LAST_UPDATE_TIME + " LONG); ";
	   
	    private static final String CREATE_CATE_TRACK = "create table " + CATE_TRACK_TABLE + " (" 
		  	      + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "	   
		  	      + KEY_CATE_NAME + " TEXT, "
		  	      + KEY_TRACK_ID + " TEXT, "
		  	      + KEY_CATE_TRACK_STATUS + " TEXT, "	
		  	      + KEY_TRACK_2_COL4 + " TEXT, " 
		          + KEY_TRACK_2_COL5 + " TEXT, "
		          + KEY_TRACK_2_COL6+ " TEXT, "
		          + KEY_TRACK_2_COL7 + " TEXT, " 
		          + KEY_TRACK_2_COL8 + " TEXT, " 
		          + KEY_LAST_UPDATE_TIME + " LONG); ";
	    
		 
		  
	    private static final String CREATE_ACTIVITY_TABLE = "create table " + ACTIVITY_TABLE + " (" 
		  	      + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "	    
		  	      + KEY_SVR_NUMBER + " INTEGER, " 
		  	      + KEY_ACTIVITY_NAME + " TEXT, "
		  	      + KEY_ACTIVITY_OWNER + " TEXT, "
		  	      + KEY_ACTIVITY_DESC + " TEXT, "	
		  	      + KEY_ACTIVITY_LOCATION + " TEXT, " 
		          + KEY_ACTIVITY_TYPE + " TEXT, "
		          + KEY_ACTIVITY_ST_DATE+ " TEXT, "
		          + KEY_ACTIVITY_END_DATE + " TEXT, " 
		          + KEY_ACTIVITY_SPONSORS + " TEXT, " 
		          + KEY_ACTIVITY_STATUS + " TEXT, " 
		          + KEY_ACTIVITY_COL1+ " TEXT, "
		          + KEY_ACTIVITY_COL2 + " TEXT, " 
		          + KEY_ACTIVITY_COL3 + " TEXT, " 
		          + KEY_ACTIVITY_COL4 + " TEXT, " 
		          + KEY_ACTIVITY_COL5+ " TEXT, "
		          + KEY_ACTIVITY_COL6 + " TEXT, " 
		          + KEY_ACTIVITY_COL7 + " TEXT, " 
		          + KEY_ACTIVITY_COL8 + " TEXT, "  
		          + KEY_LAST_UPDATE_TIME + " LONG); ";
	    
	    private static final String DATABASE_CREATE = CREATE_TRACK_TABLE + CREATE_GEO_TABLE +CREATE_TRACK_GEO_TABLE+"..."+CREATE_ACTIVITY_TABLE   ;
	    
	    public PlacesDatabaseHelper(Context context, String name, CursorFactory factory, int version) {
	      super(context, name, factory, version);
	    }

	    @Override
	    public void onCreate(SQLiteDatabase db) { 
	   
	      db.execSQL(CREATE_TRACK_TABLE);   
	      db.execSQL(CREATE_GEO_TABLE); 
	      db.execSQL(CREATE_TRACK_GEO_TABLE); 
	      db.execSQL(CREATE_CATE_TRACK);   
	      db.execSQL(CREATE_USER_TABLE);   
	      db.execSQL(CREATE_GROUPS_TABLE); 
	      db.execSQL(CREATE_ACTIVITY_TABLE);
	      db.execSQL(CREATE_GROUP_USER); 
 	      // QLog.getLogger().dLog(TAG, " DATABASE_CREATE: " + DATABASE_CREATE);
 	      insertInitialValues(db);
	    }

	    @Override
	    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	    	 // QLog.getLogger().dLog(TAG, " Upgrading database from version " + oldVersion + " to "                + newVersion + ", which will destroy all old data");
	              
	      //TODO: migrate data for db upgrade
	      db.execSQL("DROP TABLE IF EXISTS " + TRACKS_TABLE);
	     db.execSQL("DROP TABLE IF EXISTS " + GEOS_TABLE); 
	     db.execSQL("DROP TABLE IF EXISTS " + TRACK_GEOS_TABLE); 
	     db.execSQL("DROP TABLE IF EXISTS " + USERS_TABLE); 
	     db.execSQL("DROP TABLE IF EXISTS " + GROUPS_TABLE); 
//	     db.execSQL("DROP TABLE IF EXISTS " + CATEGORY_TABLE); 
	     db.execSQL("DROP TABLE IF EXISTS " + CATE_TRACK_TABLE); 
	     db.execSQL("DROP TABLE IF EXISTS " + ACTIVITY_TABLE); 
    	 db.execSQL("DROP TABLE IF EXISTS " + USERS_TABLE); 
	     db.execSQL("DROP TABLE IF EXISTS " + GROUPS_TABLE); 
	     db.execSQL("DROP TABLE IF EXISTS " + GROUP_USER_TABLE); 
	     onCreate(db);
	    
	    }
	    private void insertInitialValues(SQLiteDatabase db){
	    	// QLog.getLogger().dLog(TAG,  " adding default cates... " );
	    	/* String INSERT_CATES = "INSERT INTO "+CATE_TRACK_TABLE +"("
	    			 + KEY_CATE_NAME +","
			  	      + KEY_TRACK_ID 
			  	      + ")"
			  	      +	" VALUES ("
			  	      +"'通勤','0' );";
	    	 QLog.getLogger().dLog(TAG, " INSERT_CATES： "+INSERT_CATES );
	    	db.beginTransaction();
	    	db.execSQL(INSERT_CATES);
	    	
	    	INSERT_CATES = "INSERT INTO "+CATE_TRACK_TABLE +"("
	    			 + KEY_CATE_NAME +","
			  	      + KEY_TRACK_ID 
			  	      + ")"
			  	      +	" VALUES ("
			  	      +"'休闲','0' );";
	    	db.execSQL(INSERT_CATES);
	    	 QLog.getLogger().dLog(TAG, " INSERT_CATES： "+INSERT_CATES );
	    	INSERT_CATES = "INSERT INTO "+CATE_TRACK_TABLE +"("
	    			 + KEY_CATE_NAME +","
			  	      + KEY_TRACK_ID 
			  	      + ")"
			  	      +	" VALUES ("
			  	      +"'竞速','0');";
	    	 QLog.getLogger().dLog(TAG, " INSERT_CATES： "+INSERT_CATES );
	    	db.execSQL(INSERT_CATES);
	    	db.endTransaction();
	    	*/
	    	
//	    	  ContentValues[] values = new ContentValues[3];
//			  List<ContentValues> valueList = new  ArrayList<ContentValues>();
			  ContentValues value;
//			  int i=0; 
				  
					  
//					  valueList.add(value);
/*					  value = new ContentValues();
					  value.put(TrackProvider.KEY_CATE_NAME, "休闲"); 
					  value.put(TrackProvider.KEY_TRACK_ID, 0);
					  db.insert(CATE_TRACK_TABLE, null, value); */
//					  valueList.add(value);
					  value = new ContentValues();
					  value.put(TrackProvider.KEY_CATE_NAME, "竞速"); 
					  value.put(TrackProvider.KEY_TRACK_ID, 0);
					  db.insert(CATE_TRACK_TABLE, null, value); 
//					  valueList.add(value);
					  value = new ContentValues();
					  value.put(TrackProvider.KEY_CATE_NAME, "骑游"); 
					  value.put(TrackProvider.KEY_TRACK_ID, 0); 
//				    	QLog.getLogger().d(dTAG,  TAG+" adding new cate is  "+key);
					  db.insert(CATE_TRACK_TABLE, null, value); 
                
			  /*  try {   
			    	db.insert(table, nullColumnHack, values) ; 
	 	 		      
			    }
			    catch (Exception er) { 
			      Utils.logStackTrace(er, dTAG);
			    }*/
	    	 
	    	
	    	// QLog.getLogger().dLog(TAG, " adding default cates... done" );
	    	
	    	 
		  }
	  }
}
