use of com.google.android.gms.wearable.Asset in project muzei by romannurik.
the class WearableController method updateArtwork.
public static synchronized void updateArtwork(Context context) {
if (ConnectionResult.SUCCESS != GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context)) {
return;
}
GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context).addApi(Wearable.API).build();
ConnectionResult connectionResult = googleApiClient.blockingConnect(5, TimeUnit.SECONDS);
if (!connectionResult.isSuccess()) {
if (connectionResult.getErrorCode() != ConnectionResult.API_UNAVAILABLE) {
Log.w(TAG, "onConnectionFailed: " + connectionResult);
}
return;
}
ContentResolver contentResolver = context.getContentResolver();
Bitmap image = null;
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(contentResolver.openInputStream(MuzeiContract.Artwork.CONTENT_URI), null, options);
options.inJustDecodeBounds = false;
if (options.outWidth > options.outHeight) {
options.inSampleSize = ImageUtil.calculateSampleSize(options.outHeight, 320);
} else {
options.inSampleSize = ImageUtil.calculateSampleSize(options.outWidth, 320);
}
image = BitmapFactory.decodeStream(contentResolver.openInputStream(MuzeiContract.Artwork.CONTENT_URI), null, options);
} catch (FileNotFoundException e) {
Log.e(TAG, "Unable to read artwork to update Android Wear", e);
}
if (image != null) {
int rotation = getRotation(context);
if (rotation != 0) {
// Rotate the image so that Wear always gets a right side up image
Matrix matrix = new Matrix();
matrix.postRotate(rotation);
image = Bitmap.createBitmap(image, 0, 0, image.getWidth(), image.getHeight(), matrix, true);
}
final ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, byteStream);
Asset asset = Asset.createFromBytes(byteStream.toByteArray());
PutDataMapRequest dataMapRequest = PutDataMapRequest.create("/artwork");
Artwork artwork = MuzeiContract.Artwork.getCurrentArtwork(context);
dataMapRequest.getDataMap().putDataMap("artwork", DataMap.fromBundle(artwork.toBundle()));
dataMapRequest.getDataMap().putAsset("image", asset);
Wearable.DataApi.putDataItem(googleApiClient, dataMapRequest.asPutDataRequest().setUrgent()).await();
}
googleApiClient.disconnect();
}
use of com.google.android.gms.wearable.Asset in project muzei by romannurik.
the class ArtworkCacheIntentService method processDataItem.
private boolean processDataItem(GoogleApiClient googleApiClient, DataItem dataItem) {
if (!dataItem.getUri().getPath().equals("/artwork")) {
Log.w(TAG, "Ignoring data item " + dataItem.getUri().getPath());
return false;
}
DataMapItem dataMapItem = DataMapItem.fromDataItem(dataItem);
DataMap artworkDataMap = dataMapItem.getDataMap().getDataMap("artwork");
if (artworkDataMap == null) {
Log.w(TAG, "No artwork in datamap.");
return false;
}
final Asset asset = dataMapItem.getDataMap().getAsset("image");
if (asset == null) {
Log.w(TAG, "No image asset in datamap.");
return false;
}
final Artwork artwork = Artwork.fromBundle(artworkDataMap.toBundle());
// Change it so that all Artwork from the phone is attributed to the DataLayerArtSource
artwork.setComponentName(this, DataLayerArtSource.class);
// Check if the source info row exists at all.
ComponentName componentName = artwork.getComponentName();
Cursor sourceQuery = getContentResolver().query(MuzeiContract.Sources.CONTENT_URI, null, MuzeiContract.Sources.COLUMN_NAME_COMPONENT_NAME + "=?", new String[] { componentName.flattenToShortString() }, null);
if (sourceQuery == null || !sourceQuery.moveToFirst()) {
// If the row does not exist, insert a dummy row
ContentValues values = new ContentValues();
values.put(MuzeiContract.Sources.COLUMN_NAME_COMPONENT_NAME, componentName.flattenToShortString());
values.put(MuzeiContract.Sources.COLUMN_NAME_IS_SELECTED, true);
getContentResolver().insert(MuzeiContract.Sources.CONTENT_URI, values);
}
if (sourceQuery != null) {
sourceQuery.close();
}
Uri artworkUri = getContentResolver().insert(MuzeiContract.Artwork.CONTENT_URI, artwork.toContentValues());
if (artworkUri == null) {
Log.w(TAG, "Unable to write artwork information to MuzeiProvider");
return false;
}
DataApi.GetFdForAssetResult result = null;
InputStream in = null;
try (OutputStream out = getContentResolver().openOutputStream(artworkUri)) {
if (out == null) {
// We've already cached the artwork previously, so call this a success
return true;
}
// Convert asset into a file descriptor and block until it's ready
result = Wearable.DataApi.getFdForAsset(googleApiClient, asset).await();
in = result.getInputStream();
if (in == null) {
Log.w(TAG, "Unable to open asset input stream");
return false;
}
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = in.read(buffer)) > 0) {
out.write(buffer, 0, bytesRead);
}
out.flush();
} catch (IOException e) {
Log.e(TAG, "Error writing artwork", e);
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException e) {
Log.e(TAG, "Error closing artwork input stream", e);
}
if (result != null) {
result.release();
}
}
return true;
}
Aggregations