Skip to content

Instantly share code, notes, and snippets.

@jamiehuson
Last active August 29, 2015 13:56
Show Gist options
  • Save jamiehuson/9097996 to your computer and use it in GitHub Desktop.
Save jamiehuson/9097996 to your computer and use it in GitHub Desktop.
Extending Muzei with a FileProvider
public class MuzeiExtensionService extends MuzeiArtSource {
private SubscriberManager mSubscriberManager;
public MuzeiExtensionService() {
super(MuzeiExtensionService.class.getName());
}
@Override
public void onCreate() {
super.onCreate();
mSubscriberManager = SubscriberManager.obtain(this);
}
@Override
protected void onSubscriberAdded(ComponentName subscriber) {
super.onSubscriberAdded(subscriber);
mSubscriberManager.add(subscriber.getPackageName());
}
@Override
protected void onSubscriberRemoved(ComponentName subscriber) {
super.onSubscriberRemoved(subscriber);
mSubscriberManager.remove(subscriber.getPackageName());
}
@Override
protected void onUpdate(int reason) {
// obtain a Uri for from your FileProvider
Uri wallpaper = FileProvider.getUriForFile(context, PROVIDER_AUTHORITY, wallpaperFile);
// grant permission to the Uri so Muzei can retrieve it through your FileProvider
// you can later revoke access to a Uri with Context.revokeUriPermission(Uri, int);
Set<String> subscribers = mSubscriberManager.subscribers();
for (String subscriberPackage : subscribers)
getApplicationContext().grantUriPermission(subscriberPackage, wallpaper, Intent.FLAG_GRANT_READ_URI_PERMISSION);
// publishArtwork ...
}
private static class SubscriberManager {
private static final String NAME = "muzei";
private static final String KEY = "subscribers";
public static SubscriberManager obtain(Context context) {
return new SubscriberManager(context);
}
private SharedPreferences preferences;
private SubscriberManager(Context context) {
preferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
}
public void add(String componentName) {
Set<String> value = subscribers();
value.add(componentName);
save(value);
}
public void remove(String componentName) {
Set<String> value = subscribers();
value.remove(componentName);
save(value);
}
public Set<String> subscribers() {
return preferences.getStringSet(KEY, new TreeSet<String>());
}
private void save(Set<String> value) {
SharedPreferences.Editor editor = preferences.edit();
editor.putStringSet(KEY, value);
editor.commit();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment