/**
* Column name for the ad unique identifier number
- * <P>Type: REAL</P>
+ * <P>Type: INTEGER</P>
*/
public static final String COLUMN_NAME_ID_AD = "idad";
public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.google.index";
/**
- * The content URI base for a single note. Callers must
- * append a numeric note id to this Uri to retrieve a note
+ * The content URI base for a single index. Callers must
+ * append a numeric note id to this Uri to retrieve an index
*/
public static final Uri CONTENT_ID_URI_BASE
- = Uri.parse("content://com.google.provider.NotePad/notes/");
+ = Uri.parse("content://de.android.test3.provider/index/");
}
}
// This will trigger its creation if it doesn't already exist.
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
- // Performs the insert and returns the ID of the new note.
+ // Performs the insert and returns the ID of the new index.
long rowId = db.insert(
Indexer.Index.TABLE_NAME, // The table to insert into.
null, // A hack, SQLite sets this column value to null if values is empty.
// If the insert succeeded, the row ID exists.
if (rowId > 0) {
- // Creates a URI with the note ID pattern and the new row ID appended to it.
+ // Creates a URI with the index ID pattern and the new row ID appended to it.
Uri noteUri = ContentUris.withAppendedId(Indexer.Index.CONTENT_ID_URI_BASE, rowId);
// Notifies observers registered against this provider that the data changed.
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
-
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
JSONArray finalResult = new JSONArray(tokener);
for (int i = 0; i < finalResult.length(); i++) {
JSONObject objects = finalResult.getJSONObject(i);
- ContentValues values = new ContentValues();
- values.put(Indexer.Index.COLUMN_NAME_PATH, "18188181");
- values.put(Indexer.Index.COLUMN_NAME_ID_AD, "22");
- Uri probando = Uri.parse("content://" + "de.android.test3.provider" + "/" + "indexer" + "/idad/" + "22");
-
- Cursor cursor = this.context.getContentResolver().query(probando, null, null, null, null);
+ Uri uri = Uri.parse("content://" + "de.android.test3.provider" + "/" + "indexer" + "/idad/" + objects.get("id"));
+ Cursor cursor = this.context.getContentResolver().query(uri, null, null, null, null);
if (cursor != null) {
- }
- //this.context.getContentResolver().insert(probando, values);
- downloadAds((Integer) objects.get("id"), (String)objects.get("domain"), (String)objects.get("link"));
+ if (!cursor.moveToFirst()) {
+ //If the advertisement was not stored on the database
+ ContentValues values = new ContentValues();
+ values.put(Indexer.Index.COLUMN_NAME_ID_AD, new Integer((String) objects.get("id")));
+ String path = generateName(this.random, 10);
+ values.put(Indexer.Index.COLUMN_NAME_PATH, path);
+ uri = this.context.getContentResolver().insert(Indexer.Index.CONTENT_ID_URI_BASE, values);
+ downloadAds(new Integer((String) objects.get("id")), (String)objects.get("domain"), (String)objects.get("link"), path);
+ }
+ cursor.close();
+ }
}
} catch (URISyntaxException e) {
Log.e(TAG, "Error while creating URI from URL.", e);
return builder;
}
- public void downloadAds(Integer id, String domain, String link) {
+ public void downloadAds(Integer id, String domain, String link, String path) {
//if the id is not on the data base, download the ad, otherwise do nothing. USE synchronize
final HttpGet httpGet = new HttpGet();
//By default max 2 connections at the same time per host.
//and infinite time out (we could wait here forever...)
httpResponse = this.httpClient.execute(httpGet);
- outputStream = new FileOutputStream(generateName(this.random, 10));
+ outputStream = new FileOutputStream(path);
switch (httpResponse.getStatusLine().getStatusCode()) {
case HttpStatus.SC_OK:
try {