Clean up from comments, enable throttling.

Change-Id: I719b0491a7953ab57d76634699c6fdf3ad8558e0
This commit is contained in:
Dianne Hackborn
2011-01-08 18:34:02 -08:00
parent ddfb4b38f5
commit 69c72dfdf9
2 changed files with 31 additions and 42 deletions

View File

@@ -344,6 +344,7 @@
<!-- Loader Samples --> <!-- Loader Samples -->
<!-- BEGIN_INCLUDE(loader_throttle) -->
<activity android:name=".app.LoaderThrottle" <activity android:name=".app.LoaderThrottle"
android:label="@string/loader_throttle"> android:label="@string/loader_throttle">
<intent-filter> <intent-filter>
@@ -353,6 +354,7 @@
</activity> </activity>
<provider android:name=".app.LoaderThrottle$SimpleProvider" <provider android:name=".app.LoaderThrottle$SimpleProvider"
android:authorities="com.example.android.apis.app.LoaderThrottle" /> android:authorities="com.example.android.apis.app.LoaderThrottle" />
<!-- END_INCLUDE(loader_throttle) -->
<!-- Intent Samples --> <!-- Intent Samples -->

View File

@@ -16,6 +16,7 @@
package com.example.android.apis.app; package com.example.android.apis.app;
//BEGIN_INCLUDE(complete)
import android.app.Activity; import android.app.Activity;
import android.app.FragmentManager; import android.app.FragmentManager;
import android.app.ListFragment; import android.app.ListFragment;
@@ -29,6 +30,7 @@ import android.content.CursorLoader;
import android.content.Loader; import android.content.Loader;
import android.content.UriMatcher; import android.content.UriMatcher;
import android.database.Cursor; import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.SQLException; import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteOpenHelper;
@@ -87,11 +89,6 @@ public class LoaderThrottle extends Activity {
public static final Uri CONTENT_ID_URI_BASE public static final Uri CONTENT_ID_URI_BASE
= Uri.parse("content://" + AUTHORITY + "/main/"); = Uri.parse("content://" + AUTHORITY + "/main/");
/**
* 0-relative position of a main ID segment in the path part of a main ID URI
*/
public static final int MAIN_ID_PATH_POSITION = 1;
/** /**
* The MIME type of {@link #CONTENT_URI}. * The MIME type of {@link #CONTENT_URI}.
*/ */
@@ -227,8 +224,9 @@ public class LoaderThrottle extends Activity {
case MAIN_ID: case MAIN_ID:
// The incoming URI is for a single row. // The incoming URI is for a single row.
qb.setProjectionMap(mNotesProjectionMap); qb.setProjectionMap(mNotesProjectionMap);
qb.appendWhere(MainTable._ID + "=" + uri.getPathSegments().get( qb.appendWhere(MainTable._ID + "=?");
MainTable.MAIN_ID_PATH_POSITION)); selectionArgs = DatabaseUtils.appendSelectionArgs(selectionArgs,
new String[] { uri.getLastPathSegment() });
break; break;
default: default:
@@ -322,14 +320,8 @@ public class LoaderThrottle extends Activity {
case MAIN_ID: case MAIN_ID:
// If URI is for a particular row ID, delete is based on incoming // If URI is for a particular row ID, delete is based on incoming
// data but modified to restrict to the given ID. // data but modified to restrict to the given ID.
finalWhere = MainTable._ID + " = " + uri.getPathSegments().get( finalWhere = DatabaseUtils.concatenateWhere(
MainTable.MAIN_ID_PATH_POSITION); MainTable._ID + " = " + ContentUris.parseId(uri), where);
if (where != null) {
// Combine with incoming where, if specified.
finalWhere = finalWhere + " AND " + where;
}
count = db.delete(MainTable.TABLE_NAME, finalWhere, whereArgs); count = db.delete(MainTable.TABLE_NAME, finalWhere, whereArgs);
break; break;
@@ -360,14 +352,8 @@ public class LoaderThrottle extends Activity {
case MAIN_ID: case MAIN_ID:
// If URI is for a particular row ID, update is based on incoming // If URI is for a particular row ID, update is based on incoming
// data but modified to restrict to the given ID. // data but modified to restrict to the given ID.
finalWhere = MainTable._ID + " = " + uri.getPathSegments().get( finalWhere = DatabaseUtils.concatenateWhere(
MainTable.MAIN_ID_PATH_POSITION); MainTable._ID + " = " + ContentUris.parseId(uri), where);
if (where != null) {
// Combine with incoming where, if specified.
finalWhere = finalWhere + " AND " + where;
}
count = db.update(MainTable.TABLE_NAME, values, finalWhere, whereArgs); count = db.update(MainTable.TABLE_NAME, values, finalWhere, whereArgs);
break; break;
@@ -413,7 +399,7 @@ public class LoaderThrottle extends Activity {
@Override public void onActivityCreated(Bundle savedInstanceState) { @Override public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
setEmptyText("No data"); setEmptyText("No data. Select 'Populate' to fill with data from Z to A at a rate of 4 per second.");
setHasOptionsMenu(true); setHasOptionsMenu(true);
// Create an empty adapter we will use to display the loaded data. // Create an empty adapter we will use to display the loaded data.
@@ -499,7 +485,7 @@ public class LoaderThrottle extends Activity {
@Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { @Override public Loader<Cursor> onCreateLoader(int id, Bundle args) {
CursorLoader cl = new CursorLoader(getActivity(), MainTable.CONTENT_URI, CursorLoader cl = new CursorLoader(getActivity(), MainTable.CONTENT_URI,
PROJECTION, null, null, null); PROJECTION, null, null, null);
//cl.setUpdateThrottle(2000); // update at most every 2 seconds. cl.setUpdateThrottle(2000); // update at most every 2 seconds.
return cl; return cl;
} }
@@ -512,3 +498,4 @@ public class LoaderThrottle extends Activity {
} }
} }
} }
//END_INCLUDE(complete)