Ejemplo n.º 1
0
 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 Artwork artwork = Artwork.fromBundle(artworkDataMap.toBundle());
   final Asset asset = dataMapItem.getDataMap().getAsset("image");
   if (asset == null) {
     Log.w(TAG, "No image asset in datamap.");
     return false;
   }
   // Convert asset into a file descriptor and block until it's ready
   final DataApi.GetFdForAssetResult getFdForAssetResult =
       Wearable.DataApi.getFdForAsset(googleApiClient, asset).await();
   InputStream assetInputStream = getFdForAssetResult.getInputStream();
   if (assetInputStream == null) {
     Log.w(TAG, "Empty asset input stream (probably an unknown asset).");
     return false;
   }
   Bitmap image = BitmapFactory.decodeStream(assetInputStream);
   if (image == null) {
     Log.w(TAG, "Couldn't decode a bitmap from the stream.");
     return false;
   }
   File localCache = new File(getCacheDir(), "cache.png");
   FileOutputStream out = null;
   try {
     out = new FileOutputStream(localCache);
     image.compress(Bitmap.CompressFormat.PNG, 100, out);
   } catch (IOException e) {
     Log.e(TAG, "Error writing local cache", e);
   } finally {
     try {
       if (out != null) {
         out.close();
       }
     } catch (IOException e) {
       Log.e(TAG, "Error closing local cache file", e);
     }
   }
   enableComponents(FullScreenActivity.class);
   if (MuzeiProvider.saveCurrentArtworkLocation(this, localCache)) {
     getContentResolver().insert(MuzeiContract.Artwork.CONTENT_URI, artwork.toContentValues());
   }
   return true;
 }