Beispiel #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;
 }
 @Override
 public void onDataChanged(DataEventBuffer dataEvents) {
   for (DataEvent dataEvent : dataEvents) {
     if (dataEvent.getType() == DataEvent.TYPE_CHANGED) {
       if (Constants.NOTIFICATION_PATH.equals(dataEvent.getDataItem().getUri().getPath())) {
         DataMapItem dataMapItem = DataMapItem.fromDataItem(dataEvent.getDataItem());
         String title = dataMapItem.getDataMap().getString(Constants.NOTIFICATION_TITLE);
         String content = dataMapItem.getDataMap().getString(Constants.NOTIFICATION_CONTENT);
         sendNotification(title, content);
       }
     }
   }
 }
 @Override
 public void onResult(DataApi.DataItemResult dataItemResult) {
   if (dataItemResult.getStatus().isSuccess()) {
     if (dataItemResult.getDataItem() != null) {
       DataItem configDataItem = dataItemResult.getDataItem();
       DataMapItem dataMapItem = DataMapItem.fromDataItem(configDataItem);
       DataMap config = dataMapItem.getDataMap();
       mCallback.onConfigDataMapFetched(config);
     } else {
       mCallback.onConfigDataMapFetched(new DataMap());
     }
   }
 }