use of com.google.android.apps.muzei.api.Artwork in project muzei by romannurik.
the class SourceSubscriberService method onHandleIntent.
@Override
protected void onHandleIntent(Intent intent) {
if (intent == null || intent.getAction() == null) {
return;
}
String action = intent.getAction();
if (!ACTION_PUBLISH_STATE.equals(action)) {
return;
}
// Handle API call from source
String token = intent.getStringExtra(EXTRA_TOKEN);
ComponentName selectedSource = SourceManager.getSelectedSource(this);
if (selectedSource == null || !TextUtils.equals(token, selectedSource.flattenToShortString())) {
Log.w(TAG, "Dropping update from non-selected source, token=" + token + " does not match token for " + selectedSource);
return;
}
SourceState state = null;
if (intent.hasExtra(EXTRA_STATE)) {
Bundle bundle = intent.getBundleExtra(EXTRA_STATE);
if (bundle != null) {
state = SourceState.fromBundle(bundle);
}
}
if (state == null) {
// If there is no state, there is nothing to change
return;
}
ContentValues values = new ContentValues();
values.put(MuzeiContract.Sources.COLUMN_NAME_COMPONENT_NAME, selectedSource.flattenToShortString());
values.put(MuzeiContract.Sources.COLUMN_NAME_IS_SELECTED, true);
values.put(MuzeiContract.Sources.COLUMN_NAME_DESCRIPTION, state.getDescription());
values.put(MuzeiContract.Sources.COLUMN_NAME_WANTS_NETWORK_AVAILABLE, state.getWantsNetworkAvailable());
JSONArray commandsSerialized = new JSONArray();
int numSourceActions = state.getNumUserCommands();
boolean supportsNextArtwork = false;
for (int i = 0; i < numSourceActions; i++) {
UserCommand command = state.getUserCommandAt(i);
if (command.getId() == MuzeiArtSource.BUILTIN_COMMAND_ID_NEXT_ARTWORK) {
supportsNextArtwork = true;
} else {
commandsSerialized.put(command.serialize());
}
}
values.put(MuzeiContract.Sources.COLUMN_NAME_SUPPORTS_NEXT_ARTWORK_COMMAND, supportsNextArtwork);
values.put(MuzeiContract.Sources.COLUMN_NAME_COMMANDS, commandsSerialized.toString());
ContentResolver contentResolver = getContentResolver();
Cursor existingSource = contentResolver.query(MuzeiContract.Sources.CONTENT_URI, new String[] { BaseColumns._ID }, MuzeiContract.Sources.COLUMN_NAME_COMPONENT_NAME + "=?", new String[] { selectedSource.flattenToShortString() }, null, null);
if (existingSource != null && existingSource.moveToFirst()) {
Uri sourceUri = ContentUris.withAppendedId(MuzeiContract.Sources.CONTENT_URI, existingSource.getLong(0));
contentResolver.update(sourceUri, values, null, null);
} else {
contentResolver.insert(MuzeiContract.Sources.CONTENT_URI, values);
}
if (existingSource != null) {
existingSource.close();
}
Artwork artwork = state.getCurrentArtwork();
if (artwork != null) {
artwork.setComponentName(selectedSource);
contentResolver.insert(MuzeiContract.Artwork.CONTENT_URI, artwork.toContentValues());
// Download the artwork contained from the newly published SourceState
startService(TaskQueueService.getDownloadCurrentArtworkIntent(this));
}
}
use of com.google.android.apps.muzei.api.Artwork in project muzei by romannurik.
the class FeaturedArtSource method onCustomCommand.
@Override
protected void onCustomCommand(int id) {
super.onCustomCommand(id);
if (COMMAND_ID_SHARE == id) {
Artwork currentArtwork = getCurrentArtwork();
if (currentArtwork == null) {
Log.w(TAG, "No current artwork, can't share.");
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Toast.makeText(FeaturedArtSource.this, R.string.featuredart_source_error_no_artwork_to_share, Toast.LENGTH_SHORT).show();
}
});
return;
}
String detailUrl = ("initial".equals(currentArtwork.getToken())) ? "http://www.wikipaintings.org/en/vincent-van-gogh/the-starry-night-1889" : currentArtwork.getViewIntent().getDataString();
String artist = currentArtwork.getByline().replaceFirst("\\.\\s*($|\\n).*", "").trim();
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "My Android wallpaper today is '" + currentArtwork.getTitle().trim() + "' by " + artist + ". #MuzeiFeaturedArt\n\n" + detailUrl);
shareIntent = Intent.createChooser(shareIntent, "Share artwork");
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(shareIntent);
} else if (COMMAND_ID_VIEW_ARCHIVE == id) {
CustomTabsIntent cti = new CustomTabsIntent.Builder().setShowTitle(true).setToolbarColor(ContextCompat.getColor(this, R.color.featuredart_color)).build();
Intent intent = cti.intent;
intent.setData(ARCHIVE_URI);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
} else if (COMMAND_ID_DEBUG_INFO == id) {
long nextUpdateTimeMillis = getSharedPreferences().getLong("scheduled_update_time_millis", 0);
final String nextUpdateTime;
if (nextUpdateTimeMillis > 0) {
Date d = new Date(nextUpdateTimeMillis);
nextUpdateTime = SimpleDateFormat.getDateTimeInstance().format(d);
} else {
nextUpdateTime = "None";
}
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Toast.makeText(FeaturedArtSource.this, "Next update time: " + nextUpdateTime, Toast.LENGTH_LONG).show();
}
});
}
}
use of com.google.android.apps.muzei.api.Artwork in project muzei by romannurik.
the class GalleryArtSource method publishNextArtwork.
private void publishNextArtwork(Uri forceUri) {
// schedule next
scheduleNext();
Cursor chosenUris = getContentResolver().query(GalleryContract.ChosenPhotos.CONTENT_URI, new String[] { BaseColumns._ID, GalleryContract.ChosenPhotos.COLUMN_NAME_URI, GalleryContract.ChosenPhotos.COLUMN_NAME_IS_TREE_URI }, null, null, null);
int numChosenUris = (chosenUris != null) ? chosenUris.getCount() : 0;
Artwork currentArtwork = getCurrentArtwork();
String lastToken = (currentArtwork != null) ? currentArtwork.getToken() : null;
Uri imageUri;
Random random = new Random();
if (forceUri != null) {
imageUri = forceUri;
} else if (numChosenUris > 0) {
// First build a list of all image URIs, recursively exploring any tree URIs that were added
List<Uri> allImages = new ArrayList<>(numChosenUris);
while (chosenUris.moveToNext()) {
Uri chosenUri = ContentUris.withAppendedId(GalleryContract.ChosenPhotos.CONTENT_URI, chosenUris.getLong(chosenUris.getColumnIndex(BaseColumns._ID)));
boolean isTreeUri = chosenUris.getInt(chosenUris.getColumnIndex(GalleryContract.ChosenPhotos.COLUMN_NAME_IS_TREE_URI)) != 0;
if (isTreeUri && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Uri treeUri = Uri.parse(chosenUris.getString(chosenUris.getColumnIndex(GalleryContract.ChosenPhotos.COLUMN_NAME_URI)));
addAllImagesFromTree(allImages, treeUri, DocumentsContract.getTreeDocumentId(treeUri));
} else {
allImages.add(chosenUri);
}
}
int numImages = allImages.size();
if (numImages == 0) {
Log.e(TAG, "No photos in the selected directories.");
return;
}
while (true) {
imageUri = allImages.get(random.nextInt(numImages));
if (numImages <= 1 || !imageUri.toString().equals(lastToken)) {
break;
}
}
} else {
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
Log.w(TAG, "Missing read external storage permission.");
return;
}
Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[] { MediaStore.MediaColumns._ID }, MediaStore.Images.Media.BUCKET_DISPLAY_NAME + " NOT LIKE '%Screenshots%'", null, null);
if (cursor == null) {
Log.w(TAG, "Empty cursor.");
return;
}
int count = cursor.getCount();
if (count == 0) {
Log.e(TAG, "No photos in the gallery.");
return;
}
while (true) {
cursor.moveToPosition(random.nextInt(count));
imageUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cursor.getLong(0));
if (!imageUri.toString().equals(lastToken)) {
break;
}
}
cursor.close();
}
if (chosenUris != null) {
chosenUris.close();
}
String token = imageUri.toString();
// Retrieve metadata for item
ensureMetadataExists(imageUri);
String[] projection = { GalleryContract.MetadataCache.COLUMN_NAME_DATETIME, GalleryContract.MetadataCache.COLUMN_NAME_LOCATION };
Cursor metadata = getContentResolver().query(GalleryContract.MetadataCache.CONTENT_URI, projection, GalleryContract.MetadataCache.COLUMN_NAME_URI + "=?", new String[] { imageUri.toString() }, null);
long datetime = 0;
String location = null;
if (metadata != null && metadata.moveToFirst()) {
datetime = metadata.getLong(metadata.getColumnIndex(GalleryContract.MetadataCache.COLUMN_NAME_DATETIME));
location = metadata.getString(metadata.getColumnIndex(GalleryContract.MetadataCache.COLUMN_NAME_LOCATION));
}
if (metadata != null) {
metadata.close();
}
// Publish the actual artwork
String title;
if (datetime > 0) {
title = DateUtils.formatDateTime(this, datetime, DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_WEEKDAY);
} else {
title = getString(R.string.gallery_from_gallery);
}
String byline;
if (!TextUtils.isEmpty(location)) {
byline = location;
} else {
byline = getString(R.string.gallery_touch_to_view);
}
publishArtwork(new Artwork.Builder().imageUri(imageUri).title(title).byline(byline).token(token).viewIntent(new Intent(Intent.ACTION_VIEW).setDataAndType(imageUri, "image/jpeg")).build());
}
use of com.google.android.apps.muzei.api.Artwork in project muzei-nationalgeographic by msal.
the class NationalGeographicArtSource method onCustomCommand.
@Override
protected void onCustomCommand(int id) {
super.onCustomCommand(id);
switch(id) {
case USER_COMMAND_ID_PHOTO_DESCRIPTION:
if (getCurrentArtwork().getToken().length() < 32) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), R.string.photo_desc_nothing_to_show, Toast.LENGTH_SHORT).show();
}
});
scheduleUpdate(System.currentTimeMillis() + 500);
} else {
Intent intent = new Intent(this, PhotoDescriptionActivity.class);
intent.putExtra(PhotoDescriptionActivity.EXTRA_TITLE, getCurrentArtwork().getTitle());
intent.putExtra(PhotoDescriptionActivity.EXTRA_DESC, getCurrentArtwork().getToken());
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
break;
case USER_COMMAND_ID_SHARE:
Artwork currentArtwork = getCurrentArtwork();
if (currentArtwork == null) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), R.string.share_artwork_nothing_to_share, Toast.LENGTH_SHORT).show();
}
});
} else {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, getString(R.string.share_artwork_message, currentArtwork.getTitle(), currentArtwork.getByline(), currentArtwork.getViewIntent().getDataString()));
shareIntent = Intent.createChooser(shareIntent, getString(R.string.share_artwork_title));
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(shareIntent);
}
break;
}
}
Aggregations