Search in sources :

Example 1 with DataMapItem

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;
}
Also used : ContentValues(android.content.ContentValues) Artwork(com.google.android.apps.muzei.api.Artwork) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) Cursor(android.database.Cursor) Uri(android.net.Uri) DataApi(com.google.android.gms.wearable.DataApi) DataMap(com.google.android.gms.wearable.DataMap) Asset(com.google.android.gms.wearable.Asset) ComponentName(android.content.ComponentName) DataMapItem(com.google.android.gms.wearable.DataMapItem)

Example 2 with DataMapItem

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);
}
Also used : ArrayList(java.util.ArrayList) Intent(android.content.Intent) DataEvent(com.google.android.gms.wearable.DataEvent) Seances(ca.etsmtl.applets.etsmobile.Seances) DataMapItem(com.google.android.gms.wearable.DataMapItem) DataMap(com.google.android.gms.wearable.DataMap)

Aggregations

DataMap (com.google.android.gms.wearable.DataMap)2 DataMapItem (com.google.android.gms.wearable.DataMapItem)2 ComponentName (android.content.ComponentName)1 ContentValues (android.content.ContentValues)1 Intent (android.content.Intent)1 Cursor (android.database.Cursor)1 Uri (android.net.Uri)1 Seances (ca.etsmtl.applets.etsmobile.Seances)1 Artwork (com.google.android.apps.muzei.api.Artwork)1 Asset (com.google.android.gms.wearable.Asset)1 DataApi (com.google.android.gms.wearable.DataApi)1 DataEvent (com.google.android.gms.wearable.DataEvent)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1