1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17 package com.android.locationtracker.data; 18 19 import android.content.ContentProvider; 20 import android.content.ContentUris; 21 import android.content.ContentValues; 22 import android.content.Context; 23 import android.database.Cursor; 24 import android.database.sqlite.SQLiteDatabase; 25 import android.database.sqlite.SQLiteOpenHelper; 26 import android.net.Uri; 27 import android.util.Log; 28 29 /** 30 * Content provider for location tracking. 31 * 32 * It is recommended to use the TrackerDataHelper class to access this data 33 * rather than this class directly 34 */ 35 public class TrackerProvider extends ContentProvider { 36 37 public static final Uri CONTENT_URI = Uri 38 .parse("content://com.android.locationtracker"); 39 40 private static final String DB_NAME = "tracking.db"; 41 private static final String TABLE_NAME = "tracking"; 42 private static final int DB_VERSION = 1; 43 44 private static final String LOG_TAG = "TrackerProvider"; 45 46 /** 47 * This class helps open, create, and upgrade the database file. 48 */ 49 private static class DatabaseHelper extends SQLiteOpenHelper { 50 51 DatabaseHelper(Context context) { 52 super(context, DB_NAME, null, DB_VERSION); 53 } 54 55 @Override 56 public void onCreate(SQLiteDatabase db) { 57 StringBuilder queryBuilder = new StringBuilder(); 58 queryBuilder.append(String.format("CREATE TABLE %s (", TABLE_NAME)); 59 TrackerEntry.buildCreationString(queryBuilder); 60 61 queryBuilder.append(");"); 62 db.execSQL(queryBuilder.toString()); 63 db.setVersion(DB_VERSION); 64 } 65 66 @Override 67 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 68 // TODO: reimplement this when dB version changes 69 Log.w(LOG_TAG, "Upgrading database from version " + oldVersion 70 + " to " + newVersion 71 + ", which will destroy all old data"); 72 db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); 73 onCreate(db); 74 } 75 } 76 77 private DatabaseHelper mOpenHelper; 78 79 @Override 80 public boolean onCreate() { 81 mOpenHelper = new DatabaseHelper(getContext()); 82 return true; 83 } 84 85 @Override 86 public int delete(Uri uri, String selection, String[] selectionArgs) { 87 SQLiteDatabase db = mOpenHelper.getWritableDatabase(); 88 int result = db.delete(TABLE_NAME, selection, selectionArgs); 89 getContext().getContentResolver().notifyChange(uri, null); 90 return result; 91 } 92 93 @Override 94 public String getType(Uri uri) { 95 throw new UnsupportedOperationException(); 96 } 97 98 @Override 99 public Uri insert(Uri uri, ContentValues values) { 100 SQLiteDatabase db = mOpenHelper.getWritableDatabase(); 101 long rowId = db.insert(TABLE_NAME, null, values); 102 if (rowId > 0) { 103 Uri addedUri = ContentUris.withAppendedId(CONTENT_URI, rowId); 104 getContext().getContentResolver().notifyChange(addedUri, null); 105 return addedUri; 106 } 107 return null; 108 } 109 110 @Override 111 public Cursor query(Uri uri, String[] projection, String selection, 112 String[] selectionArgs, String sortOrder) { 113 SQLiteDatabase db = mOpenHelper.getReadableDatabase(); 114 // TODO: extract limit from URI ? 115 Cursor cursor = db.query(TABLE_NAME, projection, selection, 116 selectionArgs, null, null, sortOrder); 117 getContext().getContentResolver().notifyChange(uri, null); 118 return cursor; 119 } 120 121 @Override 122 public int update(Uri uri, ContentValues values, String selection, 123 String[] selectionArgs) { 124 throw new UnsupportedOperationException(); 125 } 126 } 127