use of com.google.android.gms.wearable.DataMapItem 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;
}
use of com.google.android.gms.wearable.DataMapItem in project ETSMobile-Android2 by ApplETS.
the class ListenerService method onDataChanged.
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
Log.d(TAG, "onDataChanged: " + dataEvents);
for (DataEvent event : dataEvents) {
if (event.getType() == DataEvent.TYPE_CHANGED && event.getDataItem() != null && event.getDataItem().getUri().getPath().equals("/today_req")) {
DataMapItem dataMapItem = DataMapItem.fromDataItem(event.getDataItem());
ArrayList<DataMap> seancesDataMapList = dataMapItem.getDataMap().getDataMapArrayList("list_seances");
ArrayList<Seances> seances = new ArrayList<>();
for (DataMap seanceDataMap : seancesDataMapList) {
Seances seance = new Seances();
seance.getData(seanceDataMap);
seances.add(seance);
}
Intent intent = new Intent("seances_update");
intent.putExtra("seances", seances);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
}
super.onDataChanged(dataEvents);
}
Aggregations