Saturday, August 4, 2012

Re: [android-developers] SecurityException in RemoteViewsService?

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

2012/8/4 NickL <nicklonginow@gmail.com>
Error accessing content provider in RemoteViewService

Hi,

I'm trying to build a home screen widget for my app and I am using stackview, remoteviewservice. In the RemoteViewsService, I need to query the content provider. My cotent provider is set exported=false. However, when I want to add the widget on screen, I get this error
java.lang.SecurityException: Permission Denial: reading com.xxx.android.provider.yyyContentProvider uri content://com.zzz.an

If I set the provider to exported=true, it works well. But I don't want this solution. Anyone can helps me? Thanks.

--
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

--
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