Search in sources :

Example 1 with Asset

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();
}
Also used : GoogleApiClient(com.google.android.gms.common.api.GoogleApiClient) Artwork(com.google.android.apps.muzei.api.Artwork) FileNotFoundException(java.io.FileNotFoundException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ContentResolver(android.content.ContentResolver) Bitmap(android.graphics.Bitmap) Matrix(android.graphics.Matrix) ConnectionResult(com.google.android.gms.common.ConnectionResult) Asset(com.google.android.gms.wearable.Asset) BitmapFactory(android.graphics.BitmapFactory) PutDataMapRequest(com.google.android.gms.wearable.PutDataMapRequest)

Example 2 with Asset

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;
}
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)

Aggregations

Artwork (com.google.android.apps.muzei.api.Artwork)2 Asset (com.google.android.gms.wearable.Asset)2 ComponentName (android.content.ComponentName)1 ContentResolver (android.content.ContentResolver)1 ContentValues (android.content.ContentValues)1 Cursor (android.database.Cursor)1 Bitmap (android.graphics.Bitmap)1 BitmapFactory (android.graphics.BitmapFactory)1 Matrix (android.graphics.Matrix)1 Uri (android.net.Uri)1 ConnectionResult (com.google.android.gms.common.ConnectionResult)1 GoogleApiClient (com.google.android.gms.common.api.GoogleApiClient)1 DataApi (com.google.android.gms.wearable.DataApi)1 DataMap (com.google.android.gms.wearable.DataMap)1 DataMapItem (com.google.android.gms.wearable.DataMapItem)1 PutDataMapRequest (com.google.android.gms.wearable.PutDataMapRequest)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1