Commits

Matjaž Črnko committed 4058032

Tried and failed to migrate Preferences to use CursorLoader (lack of getSupportCursorManager in ABS), moved some logic to onResume so it doesen't crash anymore (and still works great), cleaned up a bit. Implemented a quick'n'dirty fix for load on startup. Misc improvements.

  • Participants
  • Parent commits 0d3dfd0

Comments (0)

Files changed (6)

AndroidManifest.xml

 
     <activity android:excludeFromRecents="true" 
         	  android:name="Preferences"
+        	  android:configChanges="orientation|keyboardHidden" 
         	  android:theme="@style/Theme.Sherlock.Light">
         <intent-filter>
         	<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />

res/layout/preferences.xml

         <PreferenceCategory
                 android:title="Obnašanje aplikacije">
                 <CheckBoxPreference
-                        android:title="Osveži ob zagonu"
+                        android:title="Osveži ob odprtju"
                         android:defaultValue="false"
-                        android:summary="Osveži podatke ob zagonu aplikacije"
+                        android:summary="Osveži podatke vsakič, ko se aplikacija prikaže"
                         android:key="osvezi_ob_zagonu" />
 		</PreferenceCategory>
 		<PreferenceCategory

src/com/jinzo/mporaba/Mporaba.java

 public class Mporaba extends SherlockFragmentActivity {
 	private Menu mMenu;
 	private SharedPreferences preferences;
-
+	private boolean taskRunning = false;
+	
 	static final int SHOW_SETUP = 11;
     static final int SHOW_WIFI_PREFERENCES = 10;
     static final int SHOW_MOBILE_PREFERENCES = 9;
 		
 		// set up a listener for the refresh item
         final MenuItem refresh = (MenuItem) menu.findItem(R.id.menu_refresh);
+        if (this.taskRunning) {
+        	// The refresh was scheduled on open, but the menu was not yet ready.
+        	// TODO: Possible endless progress action, because the task can finish before we load the menu too I guess.
+        	refresh.setActionView(R.layout.indeterminate_progress_action);
+        }
         refresh.setOnMenuItemClickListener(new OnMenuItemClickListener() {
                 // on selecting show progress spinner for 1s
                 public boolean onMenuItemClick(MenuItem item) {
     }
 	
 	public void onTaskStart() {
+		this.taskRunning = true;
 		// Starts the spinning refresh icon.
     	if (mMenu != null) {
     		final MenuItem refresh = (MenuItem) mMenu.findItem(R.id.menu_refresh);
     		refresh.setActionView(R.layout.indeterminate_progress_action);
+    	} else {
+    		// mMenu is null but we should still do something because we can crash it now/we need to show the 
+    		// user some feedback.
     	}
 	}
 	
 				showDialog(DIALOG_WRONG_PASSWORD);
 				break;
     	}
+    	this.taskRunning = false;
 	}	
 }

src/com/jinzo/mporaba/Preferences.java

             actionBar.setDisplayHomeAsUpEnabled(true);
             actionBar.setSubtitle("Nastavitve");
             
-            addPreferencesFromResource(R.layout.preferences);	
-            ListPreferenceMultiSelect widget_vsebina = (ListPreferenceMultiSelect) findPreference("widget_vsebina");
-                
-    		ContentResolver cr = getContentResolver();
-    		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();
-            if (count == 0) {
-            	count = 1;
-            }
-            CharSequence[] entries = new CharSequence[count];
-            CharSequence[] entryValues = new CharSequence[count];
-            if (c.moveToFirst()) {
-            	for(int i=0; i<count; i++) {
-            		entries[i] = c.getString(c.getColumnIndexOrThrow(DBAdapter.KEY_DISPLAYNAME));
-            		entryValues[i] = c.getString(c.getColumnIndexOrThrow(DBAdapter.KEY_ROWID));
-            		c.moveToNext();
-            	}
-            } else {
-            	entries[0] = "Ni kategorij, prosim osvežite";
-            	entryValues[0] = "NO_CAT";
-            }
-            widget_vsebina.setEntries(entries);
-            widget_vsebina.setEntryValues(entryValues);
-            widget_vsebina.setDefaultValue("#ALL#");
-            c.close();
-            cpc.release();
+            addPreferencesFromResource(R.layout.preferences);	      
+    }
+    
+    @Override
+    protected void onResume() {
+    	super.onResume();
+    	
+		ContentResolver cr = getContentResolver();
+		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;
+		}
+        
+        int count = c.getCount();
+        if (count == 0) {
+        	count = 1;
+        }
+        CharSequence[] entries = new CharSequence[count];
+        CharSequence[] entryValues = new CharSequence[count];
+        if (c.moveToFirst()) {
+        	for(int i=0; i<count; i++) {
+        		entries[i] = c.getString(c.getColumnIndexOrThrow(DBAdapter.KEY_DISPLAYNAME));
+        		entryValues[i] = c.getString(c.getColumnIndexOrThrow(DBAdapter.KEY_ROWID));
+        		c.moveToNext();
+        	}
+        } else {
+        	entries[0] = "Ni kategorij, prosim osvežite";
+        	entryValues[0] = "NO_CAT";
+        }
+        
+        ListPreferenceMultiSelect widget_vsebina = (ListPreferenceMultiSelect) findPreference("widget_vsebina");   
+        widget_vsebina.setEntries(entries);
+        widget_vsebina.setEntryValues(entryValues);
+        widget_vsebina.setDefaultValue("#ALL#");
+        
+        c.close();
+        cpc.release();    	
     }
     
     @Override

src/com/jinzo/mporaba/fragments/MainData.java

         
 	}
     public void refreshFromCP() {
+    	final Mporaba mp_activity = (Mporaba)getActivity();
     	final LoadFromCPTask loadcp = new LoadFromCPTask();
     	
     	Handler cphandler = new Handler() {
     					m_items.clear();
     					m_items.addAll(loadcp.getResult());
     					m_adapter.notifyDataSetChanged();
-    					((Mporaba)getActivity()).onTaskComplete(msg);
+    					mp_activity.onTaskComplete(msg);
     					break;
     	    	}
     		}

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

     			values.put(DBAdapter.KEY_UNIT, q.getUnit());
     			values.put(DBAdapter.KEY_AVAILABLE, q.getAvailable());
     			values.put(DBAdapter.KEY_USED, q.getUsed());
-    			int update_count = cr.update(QuotaItem.CONTENT_URI, values, DBAdapter.KEY_DISPLAYNAME + "= \"" + q.getDisplayName() +"\"", null);
+    			int update_count;
+    			try {
+    				update_count = cpc.update(QuotaItem.CONTENT_URI, values, DBAdapter.KEY_DISPLAYNAME + "= \"" + q.getDisplayName() +"\"", null);
+    			} catch (RemoteException e) {
+    				Log.e("SaveToCPTask", "RemoteException: " + e.getMessage());
+    				return;
+    			}
     			if (update_count == 0) {
     				// We didn't update anything, so nothing matched, insert the new stuff
     				cr.insert(QuotaItem.CONTENT_URI, values);