I am trying to achieve the following things.
1.Activity via cursor loader will query to the content provider by query() method.
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
CursorLoader cursorLoader = new CursorLoader(getActivity(), MyProvider.toUri(MyApis.FILTER), new String[]{baseUrl, categoryCode}, MyApis.XYZ, null, null);
return cursorLoader;
}
2.Content Provider in its query() will get some data from network.
3.We will create the Custom Matrix Cursor from response and return the cursor to the loader with initial set of data.
public Cursor query(Uri uri, String[] serviceParams, String serviceApiName, String[] selectionArgs, String sortOrder) {
RestAdapter retrofit = new RestAdapter.Builder().setEndpoint(serviceParams[0]).setLogLevel(RestAdapter.LogLevel.FULL).setConverter(new SimpleXMLConverter()).build();
MyResponse response = retrofit.create(MyApis.class).filter(getFilterParams(serviceParams[1]));
MyMatrixCursor cursor = new MyMatrixCursor (new String[]{"_id", "cat", "name", "imageURI", "free", "year", "runtime", "stars"}, getContext(), uri, serviceApiName, serviceParams);
List<Thumbnails> thumbnailsList = response.getThumbnails();
for (Thumbnails thumbnails : thumbnailsList) {
cursor.addRow(new Object[]{thumbnails.getId(), thumbnails.getCat(), thumbnails.getName(), thumbnails.getImageURI(), thumbnails.getFree(), thumbnails.getYear(), thumbnails.getRuntime(), thumbnails.getStars()});
}
return cursor;
}
4.While movement of the cursor (Custom Cursor which has override the onMove and hit the network again while newPosition reaches to a certain fixed value to get additional data while user scrolls)we update the cursor by adding some rows into it.
5.Notify the resolver by notifyChange() API to requery it.
public class MyCursor extends MatrixCursor {
public MyCursor (String[] columnNames, Context mContext, Uri uri,
String serviceApiName, String[] serviceParams) {
super(columnNames);
this.mContext = mContext;
this.uri = uri;
this.serviceApiName = serviceApiName;
this.serviceParams = serviceParams;
setNotificationUri(mContext.getContentResolver(), uri);
}
#Override
public boolean onMove(int oldPosition, int newPosition) {
Log.d(TAG, "Old Position : " + oldPosition + " New Position : " + newPosition + " Count " + getCount());
if(newPosition == getCount()-1){
//Suppose this data comes from network asynchronously
addRow(new Object[]{1010, "Category", "Name", "ImageUrl", true,"2012","Android","5"});
mContext.getContentResolver().notifyChange(uri, null);
}
return super.onMove(oldPosition, newPosition);
}
}
Problems :
1.Is this the right way of doing the things if not suggest the best optimized approach for large set real time data.
2.Calling the notify calls the query method of provider again which results to return with the initial set of data instead of getting the additional data with initial set of data which I added in onMove.
I think i have made the things clear.Please ask if any doubt in use case
Related
My app displays playlists where for each playlist I show albumart in recyclerview.
A custom adapter displays the rows. It all works fine but when I add another playlist, using an asynctask, the display gets redrawn several times. I have checked that there are no adapter.notifydatasetchanged() calls. Stepping through the code I have discovered that it happens when there is a resolver.insert
In this example, the routine creates a new playlist, either for whole albums or tracks.
public void addTracksToPlaylist(Context context, String music_id,
long playlist_id, String nmode, int base) {
// feed it the album_id or track_id
Uri mediauri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
ContentResolver resolver = context.getContentResolver();
Uri exturi = MediaStore.Audio.Playlists.Members.getContentUri(
"external", playlist_id);
String[] projection = {MediaStore.Audio.Media._ID, MediaStore.Audio.Media.ALBUM, MediaStore.Audio.Media.TRACK};
String where = MediaStore.Audio.Media.ALBUM_ID + " =?";
String orderBy = null;
if (nmode.equals(context.getString(R.string.track_mode))) {
where = MediaStore.Audio.Media._ID + "=?";
}
orderBy = MediaStore.Audio.Media.ARTIST + " ASC, "
+ MediaStore.Audio.Media.ALBUM + " ASC , " +
"CAST(" + (MediaStore.Audio.Media.TRACK) + " AS INTEGER) ASC";
String[] whereVal = {music_id};
Cursor c = resolver.query(mediauri, projection, where, whereVal,
orderBy);
if (c != null && c.moveToFirst()) {
int idColumn = c.getColumnIndex(MediaStore.Audio.Media._ID);
ContentValues values = new ContentValues();
boolean stamp = prefs.getstampSelected(context);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
String audio_id = c.getString(idColumn);
values.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, base);
values.put(MediaStore.Audio.Playlists.Members.AUDIO_ID, audio_id);
try {
resolver.insert(exturi, values);
} catch (Exception e) {
e.printStackTrace();
}
// String thistrack= track.getfullPathfromAudioId(context,audio_id);
// Log.i(TAG,"position : "+base +" Inserted track = "+ audio_id + " track "+thistrack+ " Album_id = "+ music_id);
base++;
if (stamp) {
try {
track.updateTrackModifiedDate(audio_id, context);
} catch (Exception e) {
e.printStackTrace();
}
}
}
c.close();
}
}
What can I do to prevent these actions, which happen in the background, affect the display of playlists. Or in other words, why/how is my recyclerView "active" as it appears to be aware of changes in the database.
Update:
From the Android Developers website:
The Loader will monitor for changes to the data, and report them to you through new calls here. You should not monitor the data yourself. For example, if the data is a Cursor and you place it in a CursorAdapter, use the CursorAdapter(android.content.Context, android.database.Cursor, int) constructor without passing in either FLAG_AUTO_REQUERY or FLAG_REGISTER_CONTENT_OBSERVER (that is, use 0 for the flags argument). This prevents the CursorAdapter from doing its own observing of the Cursor, which is not needed since when a change happens you will get a new Cursor throw another call here.
the monitoring and reporting back by the loader manifests itself in onLoadFinished calls which in my case reset the adapter.
As was stated in the comments, the Loader monitors changes in the underlying data. The solution was simple in the end.
I declare a boolean variable
private boolean processing=false;
Set the variable to true in the onPreExecute() and to false in onPostExecute() of the AsyncTask.
As a change fires the OnLoadFinished call I test for the value of processing and skip the code. Once completed, I call getLoaderManager().restartLoader etc
My application uses the Contacts ContentProvider to store some of its data. When I load a contact into memory, I want to save its ID (so that I know how to save changes later), and the ID of all data fields it is using (so they can be directly updated). Here is some of my code:
Uri entityUri = Uri.withAppendedPath(
ContentUris.withAppendedId(RawContacts.CONTENT_URI, id),
Entity.CONTENT_DIRECTORY);
Cursor resultData = context.getContentResolver().query(
entityUri,
new String[]{RawContacts.SOURCE_ID, Entity.DATA_ID, Entity.MIMETYPE, Entity.DATA1},
null, null, null);
resultData.moveToFirst();
this.id = id;
while (resultData.isAfterLast() == false) {
this.source_id = resultData.getInt(0);
if (!resultData.isNull(1)) {
if (resultData.getString(2).equals(Fields.DISPLAY_NAME)) {
this.display_name = resultData.getString(3);
this.display_name_id = resultData.getInt(1);
}
}
resultData.moveToNext();
}
resultData.close();
return this;
That queries the ContentProvider and gets the DISPLAY_NAME field from the data. The ID of the data record is stored in the display_name_id variable. It comes out as 4612 when I run it on my device.
I tried saving it, but it does not update as expected. In order to debug, I added a query that tries to find the correct data table record.
Cursor c = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI,
new String[] {ContactsContract.Data._ID, CommonDataKinds.StructuredName.DISPLAY_NAME},
ContactsContract.Data._ID + "=?",
new String[] {String.valueOf(this.display_name_id)}, null);
However, this cursor comes back as having a length of 0. How can this be? Why is the ID incorrect?
I tried locally and it works for me, here's my code slightly adapted from yours:
public void testContacts(final #Nonnull Context context, final int rawContactId, final #Nonnull String expectedDisplayName) {
Uri entityUri = Uri.withAppendedPath(
ContentUris.withAppendedId(ContactsContract.RawContacts.CONTENT_URI, rawContactId),
ContactsContract.RawContacts.Entity.CONTENT_DIRECTORY);
Cursor resultData = context.getContentResolver().query(
entityUri,
new String[]{
ContactsContract.RawContacts.SOURCE_ID,
ContactsContract.RawContacts.Entity.DATA_ID,
ContactsContract.RawContacts.Entity.MIMETYPE,
ContactsContract.RawContacts.Entity.DATA1
},
null, null, null);
int displayNameId = -1;
try {
final int columnIndexDataId = resultData.getColumnIndex(ContactsContract.RawContacts.Entity.DATA_ID);
final int columnIndexMimetype = resultData.getColumnIndex(ContactsContract.RawContacts.Entity.MIMETYPE);
final int columnIndexData = resultData.getColumnIndex(ContactsContract.RawContacts.Entity.DATA1);
while (resultData.moveToNext()) {
if (!resultData.isNull(columnIndexDataId)) {
if (ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE.equals(resultData.getString(columnIndexMimetype)) &&
expectedDisplayName.equals(resultData.getString(columnIndexData))) {
displayNameId = resultData.getInt(1);
break;
}
}
}
} finally {
resultData.close();
}
String reLookedUpDisplayName = null;
if (displayNameId != -1) {
Cursor reLookupCursor = context.getContentResolver().query(
ContactsContract.Data.CONTENT_URI,
new String[] {
ContactsContract.Data._ID,
ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME
},
ContactsContract.Data._ID + "=?",
new String[] {String.valueOf(displayNameId)},
null);
try {
final int columnIndexId = reLookupCursor.getColumnIndex(ContactsContract.Data._ID);
final int columnIndexDisplayName = reLookupCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME);
while (reLookupCursor.moveToNext()) {
reLookedUpDisplayName = reLookupCursor.getString(columnIndexDisplayName);
}
} finally {
reLookupCursor.close();
}
}
Toast.makeText(
context,
reLookedUpDisplayName != null ? "Found re-looked up name: " + reLookedUpDisplayName : "Didn't find name re-looking it up",
Toast.LENGTH_LONG)
.show();
}
There's no big difference from your code, so compare or try to replace bits of it to see where you have a problem. Make sure you use a fresh Cursor for each query, and close it correctly afterwards (in a finally clause).
Another thing, make sure that if (resultData.getString(2).equals(Fields.DISPLAY_NAME)) is really what you're wanting to do (it compares the entry mime type with Fields.DISPLAY_NAME), but since you're saying you get the data ID correctly this shouldn't be the problem.
I'm implementing my own SearchRecentSuggestionsProvider and everything's working except one thing: I can't get the device search to display icons for the results. I'd like to display images from my application's data folder (located at /{sdcard}/Android/data/package_name/files/)
According to the documentation, it's achievable by using SearchManager.SUGGEST_COLUMN_ICON_1, and it apparently supports a number of schemes, including ContentResolver.SCHEME_FILE, which is file. Here's a quote from the official docs:
Column name for suggestions cursor. Optional. If your cursor includes this column, then all suggestions will be provided in a format that includes space for two small icons, one at the left and one at the right of each suggestion. The data in the column must be a resource ID of a drawable, or a URI in one of the following formats:
content (SCHEME_CONTENT)
android.resource (SCHEME_ANDROID_RESOURCE)
file (SCHEME_FILE)
I've tried a number of obvious things, including manual creation of the file URI and automated creation using Uri.Builder(). None of this worked.
I also found someone else asking about the same thing on Google Groups, and it's sadly unanswered: https://groups.google.com/forum/#!topic/android-developers/MJj7GIaONjc
Does anyone have any experience in getting the device search to display local images from the device?
UPDATE (December 15):
I've just tried using the ContentProvider with a SearchView as the searchable info, and it works exactly as expected - including the cover art images. Still, global search doesn't show it...
I had a similar issue and could not make the other solutions work. I finally substituted the cursor with a new one containing the data I needed in query().
public class RecentQueriesProvider extends SearchRecentSuggestionsProvider {
public final static String AUTHORITY = "com.package.RecentQueriesProvider";
public final static int MODE = DATABASE_MODE_QUERIES;
public RecentQueriesProvider() {
setupSuggestions(AUTHORITY, MODE);
}
// We override query() to replace the history icon in the recent searches suggestions. We create a new cursor
#Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
Cursor superCursor = super.query(uri, projection, selection, selectionArgs, sortOrder);
Uri iconUri = Uri.parse("android.resource://" + getContext().getPackageName() + "/drawable/ic_action_time");
MatrixCursor newCursor = new MatrixCursor(superCursor.getColumnNames());
superCursor.moveToFirst();
while (superCursor.moveToNext()){
newCursor.addRow(new Object[]{
superCursor.getInt(superCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_FORMAT)),
iconUri,
superCursor.getString(superCursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_1)),
superCursor.getString(superCursor.getColumnIndex("suggest_intent_query")),
superCursor.getInt(superCursor.getColumnIndex("_id"))
});
}
return newCursor;
}
}
One way is to copy source code from android.content.SearchRecentSuggestionsProvider, place it in your class that extends ContentProvider, and customize setupSuggestions(String, int). Specifically, you would be changing this:
Uri uriFile = Uri.fromFile(new File("path/to/file"));
mSuggestionProjection = new String [] {
"0 AS " + SearchManager.SUGGEST_COLUMN_FORMAT,
// Here, you would return a file uri: 'uriFile'
"'android.resource://system/"
+ com.android.internal.R.drawable.ic_menu_recent_history + "' AS "
+ SearchManager.SUGGEST_COLUMN_ICON_1,
"display1 AS " + SearchManager.SUGGEST_COLUMN_TEXT_1,
"query AS " + SearchManager.SUGGEST_COLUMN_QUERY,
"_id"
};
I prefer the following though. Extend SearchRecentSuggestionsProvider and override the query(...) method. Here, intercept SearchManager.SUGGEST_URI_PATH_QUERY and return a cursor.
public class SearchSuggestionProvider extends SearchRecentSuggestionsProvider {
private UriMatcher matcher;
private static final int URI_MATCH_SUGGEST = 1;
public SearchSuggestionProvider() {
super();
matcher = new UriMatcher(UriMatcher.NO_MATCH);
// Add uri to return URI_MATCH_SUGGEST
matcher.addURI(SearchSuggestionProvider.class.getName(),
SearchManager.SUGGEST_URI_PATH_QUERY, URI_MATCH_SUGGEST);
setupSuggestions(SearchSuggestionProvider.class.getName(),
DATABASE_MODE_QUERIES);
}
#Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// special case for actual suggestions (from search manager)
if (matcher.match(uri) == URI_MATCH_SUGGEST) {
// File to use
File f = new File("/path/to/file");
Uri uriFile = Uri.fromFile(f);
final String[] PROJECTION = new String[] {
"_id",
"display1 AS " + SearchManager.SUGGEST_COLUMN_TEXT_1,
"query AS " + SearchManager.SUGGEST_COLUMN_QUERY,
"'" + uriFile + "'" + " AS " + SearchManager.SUGGEST_COLUMN_ICON_1,
};
final Uri URI = Uri.parse("content://" +
SearchSuggestionProvider.class.getName() + "/suggestions");
// return cursor
return getContext().getContentResolver().query(
URI,
PROJECTION,
"display1 LIKE ?",
new String[] {selectionArgs[0] + "%"},
"date DESC");
}
// Let super method handle the query if the check fails
return super.query(uri, projection, selection, selectionArgs, sortOrder);
}
}
I am using LoaderCallbacks to get all data from my tabel.
And After getting the cursor like this and setting it in cursoradapter.
#Override
public Loader<Cursor> onCreateLoader(int loaderNumber, Bundle bundle) {
String[] projection = {
MyDeals.Columns._ID,
MyDeals.Columns.LATITUDE,
MyDeals.Columns.LONGITUDE,
MyDeals.Columns.PHONENUMBER
};
return new CursorLoader(mActivity, MyDeals.CONTENT_URI, projection, null, null, null);
}
#Override
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
dealsBaseAdapter = new DealsBaseAdapter(mActivity, cursor, CursorAdapter.NO_SELECTION);
mDealListView.setAdapter(dealsBaseAdapter);
}
Now in my cursoradapter i want to filter some data based on distance. for each item i fetch from db.
Suppose if get three data as
Data A , lat_A = 5.5, long_A=5.2
Data B , lat_B = 5.5, long_B=5.2
Data C , lat_C = 5.5, long_C=5.2
Now based on by present location i calculate the distance between my location and Data A location. if location is <=(someValue) then show deal in list else not.
The same for all deals i get from cursor.
At which place i can filter my deal like this :)
see CursorAdapter.setFilterQueryProvider()
I am trying to create an app which simply offers an edittext and imagebutton. If the butten gets clicked, the idea is that an album is added to the Playlist, named in the edittext box. Albums should be selected randomly. Goes without saying that the album tracks should be in the correct order.
I can add more functionality later eg. save, overwrite, delete etc.
I have the interface but am struggling with the code. I sort of get the concept of ContentProviders.
so the code needs to:
access the Playlists, which I believe is achieved by using MediaStore.Audio.Playlists
access the Albums, which I believe is achieved by using MediaStore.Audio.Albums
add to the Playlist
I have the following code (most bits obtained from this site. Thanks btw) to access the Playlist but it crashes with a Null Exception error.
public void checkforplaylists()
{
//Get a cursor over all playlists.
final ContentResolver resolver= MediaProvider.mContentResolver;
final Uri uri=MediaStore.Audio.Playlists.INTERNAL_CONTENT_URI;
final String id=MediaStore.Audio.Playlists._ID;
final String name=MediaStore.Audio.Playlists.NAME;
final String[]columns={id,name};
final Cursor playlists= resolver.query(uri, columns, null, null, null);
if(playlists==null)
{
Log.e(TAG,"Found no playlists.");
return;
}
return;
}
Anyone who can help?
I think you mean NullPointerException, which means one of your assignments is null and then you try to access a member of the object you intended it to be. Most likely it is resolver, but to be sure you need the line number reported and/or to step through that with a debugger.
This works. When using the ContentResolver, the Context (this) is required.
public void checkforplaylists(Context context)
{
ContentResolver cr = context.getContentResolver();
final Uri uri=MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
final String id=MediaStore.Audio.Playlists._ID;
final String name=MediaStore.Audio.Playlists.NAME;
final String[]columns={id,name};
final Cursor playlists= cr.query(uri, columns, null, null, null);
if(playlists==null)
{
Log.e(TAG,"Found no playlists.");
return;
}
Log.e(TAG,"Found playlists.");
return;
}
use this code, will work
public boolean addPlaylist(String pname) {
Uri playlists = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
Cursor c = resolver.query(playlists, new String[] { "*" }, null, null,
null);
long playlistId = 0;
c.moveToFirst();
do {
String plname = c.getString(c
.getColumnIndex(MediaStore.Audio.Playlists.NAME));
if (plname.equalsIgnoreCase(pname)) {
playlistId = c.getLong(c
.getColumnIndex(MediaStore.Audio.Playlists._ID));
break;
}
} while (c.moveToNext());
c.close();
if (playlistId != 0) {
Uri deleteUri = ContentUris.withAppendedId(playlists, playlistId);
Log.d(TAG, "REMOVING Existing Playlist: " + playlistId);
// delete the playlist
resolver.delete(deleteUri, null, null);
}
Log.d(TAG, "CREATING PLAYLIST: " + pname);
ContentValues v1 = new ContentValues();
v1.put(MediaStore.Audio.Playlists.NAME, pname);
v1.put(MediaStore.Audio.Playlists.DATE_MODIFIED,
System.currentTimeMillis());
Uri newpl = resolver.insert(playlists, v1);
Log.d(TAG, "Added PlayLIst: " + newpl);
flag=true;
return flag;
}