Commits

Matjaž Črnko committed 263b8af

Some improvements on ContentProvider access. Concurrency crash fix when using a content provider.

Comments (0)

Files changed (4)

src/com/jinzo/mporaba/MporabaContentProvider.java

     private static final int PORABA = 1;
 
 	@Override
-	public int delete(Uri uri, String selection, String[] selectionArgs) {
+	public synchronized int delete(Uri uri, String selection, String[] selectionArgs) {
 		if (porabaUriMatcher.match(uri) != PORABA) { throw new IllegalArgumentException("Unknown URI " + uri); }
 		db.open();
 		int count = db.db.delete("usage", selection, selectionArgs);
 	}
 
 	@Override
-	public String getType(Uri uri) {
+	public synchronized String getType(Uri uri) {
 		if (porabaUriMatcher.match(uri) != PORABA) { 
 			throw new IllegalArgumentException("Unknown URI " + uri); 
 		} else {
 	}
 
 	@Override
-	public Uri insert(Uri uri, ContentValues initialValues) {
+	public synchronized Uri insert(Uri uri, ContentValues initialValues) {
 		if (porabaUriMatcher.match(uri) != PORABA) { throw new IllegalArgumentException("Unknown URI " + uri); }
         ContentValues values;
         if (initialValues != null) {
 	}
 
 	@Override
-	public boolean onCreate() {
+	public synchronized boolean onCreate() {
 		this.db = new DBAdapter(getContext());
 		return true;
 	}
 
 	@Override
-	public Cursor query(Uri uri, String[] projection, String selection,
+	public synchronized Cursor query(Uri uri, String[] projection, String selection,
 			String[] selectionArgs, String sortOrder) {
 		if (porabaUriMatcher.match(uri) != PORABA) { throw new IllegalArgumentException("Unknown URI " + uri); }
 		db.open();
 	}
 
 	@Override
-	public int update(Uri uri, ContentValues values, String selection,
+	public synchronized int update(Uri uri, ContentValues values, String selection,
 			String[] selectionArgs) {
 		if (porabaUriMatcher.match(uri) != PORABA) { throw new IllegalArgumentException("Unknown URI " + uri); }
 		db.open();

src/com/jinzo/mporaba/Preferences.java

 import com.actionbarsherlock.app.SherlockPreferenceActivity;
 import com.actionbarsherlock.view.MenuItem;
 
+import android.content.ContentProviderClient;
 import android.content.ContentResolver;
 import android.content.Intent;
 import android.database.Cursor;
 import android.os.Bundle;
+import android.os.RemoteException;
+import android.util.Log;
 
 public class Preferences extends SherlockPreferenceActivity {
     @Override
             
             addPreferencesFromResource(R.layout.preferences);	
             ListPreferenceMultiSelect widget_vsebina = (ListPreferenceMultiSelect) findPreference("widget_vsebina");
+                
     		ContentResolver cr = getContentResolver();
-    		Cursor c = cr.query(QuotaItem.CONTENT_URI, null, null, null, null);
+    		ContentProviderClient cpc = cr.acquireContentProviderClient(QuotaItem.CONTENT_URI);
+    		
+    		Cursor c;
+    		try {
+    			c = cpc.query(QuotaItem.CONTENT_URI, null, null, null, null);
+    		} catch (RemoteException e) {
+    			Log.e("Preference", "RemoteException: " + e.getMessage());
+    			return;
+    		}
             startManagingCursor(c);
             
             int count = c.getCount();
             	entries[0] = "Ni kategorij, prosim osvežite";
             	entryValues[0] = "NO_CAT";
             }
-            c.close();
             widget_vsebina.setEntries(entries);
             widget_vsebina.setEntryValues(entryValues);
             widget_vsebina.setDefaultValue("#ALL#");
+            c.close();
+            cpc.release();
     }
     
     @Override

src/com/jinzo/mporaba/tasks/LoadFromCPTask.java

 
 import java.util.ArrayList;
 
+import android.content.ContentProviderClient;
 import android.content.ContentResolver;
 import android.content.Context;
 import android.database.Cursor;
 import android.os.Handler;
+import android.os.RemoteException;
 import android.util.Log;
 
 import com.jinzo.mporaba.DBAdapter;
 	
 	public void run() {
 		ContentResolver cr = context.getContentResolver();
-		Cursor c = cr.query(QuotaItem.CONTENT_URI, null, null, null, null);
+    	ContentProviderClient cpc = cr.acquireContentProviderClient(QuotaItem.CONTENT_URI);
+    	Cursor c;
+    	try {
+    		c = cpc.query(QuotaItem.CONTENT_URI, null, null, null, null);
+    	} catch (RemoteException e) {
+    		Log.e("LoadFromCPTask", "RemoteException:" + e.getMessage());
+    		return;
+    	}
+    	
 		if (c != null && (c.getCount() > 0)) {
 			c.moveToFirst();
 			do {
 				Log.i("LoadFromCPTask", "Loaded QuotaItem");
 			} while (!c.isLast() && c.moveToNext()); /* FIXIT: Hacky */
 			c.close();
+			cpc.release();
 		}
 		callback.sendEmptyMessage(Mporaba.REQUEST_SUCCESS);
 	}

src/com/jinzo/mporaba/tasks/SaveToCPTask.java

 import com.jinzo.mporaba.Mporaba;
 import com.jinzo.mporaba.QuotaItem;
 
+import android.content.ContentProviderClient;
 import android.content.ContentResolver;
 import android.content.ContentValues;
 import android.content.Context;
 import android.database.Cursor;
 import android.os.Handler;
+import android.os.RemoteException;
 import android.util.Log;
 
 public class SaveToCPTask implements Runnable {;
 	
     public void run() {
     	ContentResolver cr = context.getContentResolver();
-    	Cursor first = cr.query(QuotaItem.CONTENT_URI, null, "_ID = 1", null, null); // Fragile probably
+    	ContentProviderClient cpc = cr.acquireContentProviderClient(QuotaItem.CONTENT_URI);
+    	Cursor first;
+    	try {
+    		first = cpc.query(QuotaItem.CONTENT_URI, null, "_ID = 1", null, null); // Fragile probably
+    	} catch (RemoteException e) {
+    		Log.e("SaveToCPTask", "RemoteException:" + e.getMessage());
+    		return;
+    	}
     	ContentValues values = new ContentValues();
     	if (first == null | first.getCount() == 0) {
     		// We need to insert
     		}	
     	}
     	first.close();
+    	cpc.release();
     	callback.sendEmptyMessage(Mporaba.REQUEST_SUCCESS);
     }
 }