Clean up from comments, enable throttling.
Change-Id: I719b0491a7953ab57d76634699c6fdf3ad8558e0
diff --git a/samples/ApiDemos/AndroidManifest.xml b/samples/ApiDemos/AndroidManifest.xml
index 007e0ed..561096e 100644
--- a/samples/ApiDemos/AndroidManifest.xml
+++ b/samples/ApiDemos/AndroidManifest.xml
@@ -343,7 +343,8 @@
</activity>
<!-- Loader Samples -->
-
+
+<!-- BEGIN_INCLUDE(loader_throttle) -->
<activity android:name=".app.LoaderThrottle"
android:label="@string/loader_throttle">
<intent-filter>
@@ -353,6 +354,7 @@
</activity>
<provider android:name=".app.LoaderThrottle$SimpleProvider"
android:authorities="com.example.android.apis.app.LoaderThrottle" />
+<!-- END_INCLUDE(loader_throttle) -->
<!-- Intent Samples -->
diff --git a/samples/ApiDemos/src/com/example/android/apis/app/LoaderThrottle.java b/samples/ApiDemos/src/com/example/android/apis/app/LoaderThrottle.java
index 34642ce..2139679 100644
--- a/samples/ApiDemos/src/com/example/android/apis/app/LoaderThrottle.java
+++ b/samples/ApiDemos/src/com/example/android/apis/app/LoaderThrottle.java
@@ -16,6 +16,7 @@
package com.example.android.apis.app;
+//BEGIN_INCLUDE(complete)
import android.app.Activity;
import android.app.FragmentManager;
import android.app.ListFragment;
@@ -29,6 +30,7 @@
import android.content.Loader;
import android.content.UriMatcher;
import android.database.Cursor;
+import android.database.DatabaseUtils;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
@@ -56,12 +58,12 @@
public class LoaderThrottle extends Activity {
// Debugging.
static final String TAG = "LoaderThrottle";
-
+
/**
* The authority we use to get to our sample provider.
*/
public static final String AUTHORITY = "com.example.android.apis.app.LoaderThrottle";
-
+
/**
* Definition of the contract for the main table of our provider.
*/
@@ -86,12 +88,7 @@
*/
public static final Uri CONTENT_ID_URI_BASE
= 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}.
*/
@@ -107,14 +104,14 @@
* The default sort order for this table
*/
public static final String DEFAULT_SORT_ORDER = "data COLLATE LOCALIZED ASC";
-
+
/**
* Column name for the single column holding our data.
* <P>Type: TEXT</P>
*/
public static final String COLUMN_NAME_DATA = "data";
}
-
+
/**
* This class helps open, create, and upgrade the database file.
*/
@@ -189,7 +186,7 @@
mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
mUriMatcher.addURI(AUTHORITY, MainTable.TABLE_NAME, MAIN);
mUriMatcher.addURI(AUTHORITY, MainTable.TABLE_NAME + "/#", MAIN_ID);
-
+
// Create and initialize projection map for all columns. This is
// simply an identity mapping.
mNotesProjectionMap = new HashMap<String, String>();
@@ -227,8 +224,9 @@
case MAIN_ID:
// The incoming URI is for a single row.
qb.setProjectionMap(mNotesProjectionMap);
- qb.appendWhere(MainTable._ID + "=" + uri.getPathSegments().get(
- MainTable.MAIN_ID_PATH_POSITION));
+ qb.appendWhere(MainTable._ID + "=?");
+ selectionArgs = DatabaseUtils.appendSelectionArgs(selectionArgs,
+ new String[] { uri.getLastPathSegment() });
break;
default:
@@ -299,7 +297,7 @@
throw new SQLException("Failed to insert row into " + uri);
}
-
+
/**
* Handle deleting data.
*/
@@ -322,14 +320,8 @@
case MAIN_ID:
// If URI is for a particular row ID, delete is based on incoming
// data but modified to restrict to the given ID.
- finalWhere = MainTable._ID + " = " + uri.getPathSegments().get(
- MainTable.MAIN_ID_PATH_POSITION);
-
- if (where != null) {
- // Combine with incoming where, if specified.
- finalWhere = finalWhere + " AND " + where;
- }
-
+ finalWhere = DatabaseUtils.concatenateWhere(
+ MainTable._ID + " = " + ContentUris.parseId(uri), where);
count = db.delete(MainTable.TABLE_NAME, finalWhere, whereArgs);
break;
@@ -360,17 +352,11 @@
case MAIN_ID:
// If URI is for a particular row ID, update is based on incoming
// data but modified to restrict to the given ID.
- finalWhere = MainTable._ID + " = " + uri.getPathSegments().get(
- MainTable.MAIN_ID_PATH_POSITION);
-
- if (where != null) {
- // Combine with incoming where, if specified.
- finalWhere = finalWhere + " AND " + where;
- }
-
+ finalWhere = DatabaseUtils.concatenateWhere(
+ MainTable._ID + " = " + ContentUris.parseId(uri), where);
count = db.update(MainTable.TABLE_NAME, values, finalWhere, whereArgs);
break;
-
+
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
@@ -380,13 +366,13 @@
return count;
}
}
-
+
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentManager fm = getFragmentManager();
-
+
// Create the list fragment and add it as our sole content.
if (fm.findFragmentById(android.R.id.content) == null) {
ThrottledLoaderListFragment list = new ThrottledLoaderListFragment();
@@ -400,7 +386,7 @@
// Menu identifiers
static final int POPULATE_ID = Menu.FIRST;
static final int CLEAR_ID = Menu.FIRST+1;
-
+
// This is the Adapter being used to display the list's data.
SimpleCursorAdapter mAdapter;
@@ -409,11 +395,11 @@
// Task we have running to populate the database.
AsyncTask<Void, Void, Void> mPopulatingTask;
-
+
@Override public void onActivityCreated(Bundle 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);
// Create an empty adapter we will use to display the loaded data.
@@ -422,7 +408,7 @@
new String[] { MainTable.COLUMN_NAME_DATA },
new int[] { android.R.id.text1 }, 0);
setListAdapter(mAdapter);
-
+
// Prepare the loader. Either re-connect with an existing one,
// or start a new one.
getLoaderManager().initLoader(0, null, this);
@@ -437,7 +423,7 @@
@Override public boolean onOptionsItemSelected(MenuItem item) {
final ContentResolver cr = getActivity().getContentResolver();
-
+
switch (item.getItemId()) {
case POPULATE_ID:
if (mPopulatingTask != null) {
@@ -465,7 +451,7 @@
};
mPopulatingTask.execute((Void[])null);
return true;
-
+
case CLEAR_ID:
if (mPopulatingTask != null) {
mPopulatingTask.cancel(false);
@@ -479,7 +465,7 @@
};
task.execute((Void[])null);
return true;
-
+
default:
return super.onOptionsItemSelected(item);
}
@@ -499,7 +485,7 @@
@Override public Loader<Cursor> onCreateLoader(int id, Bundle args) {
CursorLoader cl = new CursorLoader(getActivity(), MainTable.CONTENT_URI,
PROJECTION, null, null, null);
- //cl.setUpdateThrottle(2000); // update at most every 2 seconds.
+ cl.setUpdateThrottle(2000); // update at most every 2 seconds.
return cl;
}
@@ -512,3 +498,4 @@
}
}
}
+//END_INCLUDE(complete)