Saturday, August 4, 2012

Re: [android-developers] SecurityException in RemoteViewsService?

 

The error happened when I call mResolver.query(Abc.CONTENT_URI, null, null, null, null); in onDataSetChanged().
 
Does that help?

On Saturday, August 4, 2012 11:20:51 AM UTC-4, Kostya Vasilyev wrote:
I've run into this exact issue before.

My fix was to add Binder.clearCallingIdentity() in my RemoteViewsService callbacks where I needed to access my non-exported provider.

Now we're back from the launcher's identity to the identity of the app's process, where access is allowed.

It fit into the code structure like this:

public static class MyWidgetService extends RemoteViewsService {
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
return new MyWidgetRemoteViewsFactory(getApplicationContext(), intent);
}
}

private static class MyWidgetRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
...
@Override
public void onDataSetChanged() {
MyLog.i(TAG, "onDataSetChanged");

// Revert back to our process' identity so we can work with our content provider
long identityToken = Binder.clearCallingIdentity();
try {
// Query the message list
Cursor listCursor = mContent.queryList(LIST_ITEM_LIMIT);
if (listCursor != null) {
try {
....
} finally {
listCursor.close();
}
}
} finally {
// Restore the identity - not sure if it's needed since we're going to return right here, but
// it just *seems* cleaner
Binder.restoreCallingIdentity(identityToken);
}
}


The only place where MyWidgetRemoteViewsFactory uses the content provider is its onDataSetChanged() -- contrary to proimises made by the documentation, I've decided to play it safe and avoid keeping an open cursor during the lifetime of the factory.

If your factory keeps an open cursor outside of one method's scope, you may need this trick in the other methods as well.

-- K

 

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment