1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
| package com.android.P2A;
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.SQLException;
import android.database.sqlite.SQLiteDatabase;
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;
public class LocationsProvider extends ContentProvider{
/*
* Fields:
*/
public static final Uri CONTENT_URI =
Uri.parse("content://com.android.P2A.LocationsProvider/locations");
// The underlying database
private SQLiteDatabase LocalisationDB;
private static final String TAG = "LocationProvider";
private static final String DATABASE_NAME = "Locations.db";
private static final int DATABASE_VERSION = 1;
private static final String LOCATIONS_TABLE = "Locations";
// Column Names
public static final String KEY_ID = "_id";
public static final String KEY_LOCATION_REGION = "région";
public static final String KEY_LOCATION_LAT = "latitude";
public static final String KEY_LOCATION_LNG = "longitude";
// Column indexes
public static final int REGION_COLUMN = 1;
public static final int LATITUDE_COLUMN =2;
public static final int LONGITUDE_COLUMN =3;
// Create the constants used to differentiate between the different URI
// requests.
private static final int LOCATIONS = 1;
private static final int LOCATION_ID = 2;
private static final UriMatcher uriMatcher;
static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI("com.android.P2A.LocationsProvider", "Localions",
LOCATIONS);
uriMatcher.addURI("com.android.P2A.LocationsProvider", "Locations/#",
LOCATION_ID);
}
// Helper class for opening, creating, and managing
// database version control
private static class LocationsDatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_CREATE =
"create table " + LOCATIONS_TABLE + " ("
+ KEY_ID + " integer primary key autoincrement, "
+ KEY_LOCATION_REGION + " VARCHAR(255), "
+ KEY_LOCATION_LAT + " FLOAT, "
+ KEY_LOCATION_LNG + " FLOAT, ";
public LocationsDatabaseHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,int newVersion) {
Log.w(TAG,"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + LOCATIONS_TABLE);
onCreate(db);
}
}
@Override
public int delete(Uri uri, String where, String[] whereArgs) {
// TODO Auto-generated method stub
int count;
switch (uriMatcher.match(uri)) {
case LOCATIONS:
count = LocalisationDB.delete(LOCATIONS_TABLE, where, whereArgs);
break;
case LOCATION_ID:
String segment = uri.getPathSegments().get(1);
count = LocalisationDB.delete(LOCATIONS_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;
}
@Override
public String getType(Uri arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public Uri insert(Uri _uri, ContentValues _initialValues) {
// TODO Auto-generated method stub
// Insert the new row, will return the row number if
// successful.
long rowID = LocalisationDB.insert(LOCATIONS_TABLE, "quake",
_initialValues);
// Return a URI to the newly inserted row on success.
if (rowID > 0) {
Uri uri = ContentUris.withAppendedId(CONTENT_URI, rowID);
getContext().getContentResolver().notifyChange(uri, null);
return uri;
}
throw new SQLException("Failed to insert row into " + _uri);
}
@Override
public boolean onCreate() {
// TODO Auto-generated method stub
Context context = getContext();
LocationsDatabaseHelper dbHelper;
dbHelper = new LocationsDatabaseHelper(context, DATABASE_NAME, null,
DATABASE_VERSION);
LocalisationDB = dbHelper.getWritableDatabase();
return (LocalisationDB == null) ? false : true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
String sort) {
// TODO Auto-generated method stub
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
qb.setTables(LOCATIONS_TABLE);
// If this is a row query, limit the result set to the passed in row.
switch (uriMatcher.match(uri)) {
case LOCATION_ID:
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 = KEY_ID;
} else {
orderBy = sort;
}
// Apply the query to the underlying database.
Cursor c = qb.query(LocalisationDB,projection,selection, selectionArgs,null, null,orderBy);
// Register the contexts ContentResolver to be notified if
// the cursor result set changes.
c.setNotificationUri(getContext().getContentResolver(), uri);
// Return a cursor to the query result.
return c;
}
@Override
public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
// TODO Auto-generated method stub
int count;
switch (uriMatcher.match(uri)) {
case LOCATIONS:
count = LocalisationDB.update(LOCATIONS_TABLE, values,
where, whereArgs);
break;
case LOCATION_ID:
String segment = uri.getPathSegments().get(1);
count = LocalisationDB.update(LOCATIONS_TABLE, values, KEY_ID
+ "=" + segment
+ (!TextUtils.isEmpty(where) ? " AND ("+ where + ')' : ""), whereArgs);
break;
default: throw new IllegalArgumentException("Unknown URI " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
} |
Partager