Finishing my provider. Query and insert working smoothly.
authorgumartinm <gu.martinm@gmail.com>
Mon, 20 Feb 2012 06:58:15 +0000 (07:58 +0100)
committergumartinm <gu.martinm@gmail.com>
Mon, 20 Feb 2012 06:58:15 +0000 (07:58 +0100)
Next step, store the downloaded ads as files on the file system.

Android/Testing/Test3/src/de/android/test3/Indexer.java
Android/Testing/Test3/src/de/android/test3/IndexerProvider.java
Android/Testing/Test3/src/de/android/test3/MobieAdHttpClient.java

index bbccaad..91ad65b 100644 (file)
@@ -31,7 +31,7 @@ public final class Indexer {
         
         /**
          * 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";
         
@@ -52,10 +52,10 @@ public final class Indexer {
         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/");
     }
 }
index 1dec16c..b8bb3e6 100644 (file)
@@ -218,7 +218,7 @@ public class IndexerProvider extends ContentProvider {
         // 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.
@@ -227,7 +227,7 @@ public class IndexerProvider extends ContentProvider {
                
         // 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.
index ad024d7..97dcce2 100644 (file)
@@ -21,7 +21,6 @@ import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
 import org.json.JSONTokener;
-
 import android.content.ContentValues;
 import android.content.Context;
 import android.database.Cursor;
@@ -95,16 +94,20 @@ public class MobieAdHttpClient implements Runnable
                                   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);  
@@ -169,7 +172,7 @@ public class MobieAdHttpClient implements Runnable
                   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();
@@ -184,7 +187,7 @@ public class MobieAdHttpClient implements Runnable
                           //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 {