Search in sources :

Example 1 with DataMap

use of com.google.android.gms.wearable.DataMap 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 DataMap

use of com.google.android.gms.wearable.DataMap in project instructure-android by instructure.

the class HomeListenerService method setupNotification.

/**
 * Builds notification for wear based on the data in the Data Item that is passed in.
 */
private void setupNotification(DataItem dataItem) {
    Log.d(TAG, "setupNotification(): DataItem=" + dataItem.getUri());
    PutDataMapRequest putDataMapRequest = PutDataMapRequest.createFromDataMapItem(DataMapItem.fromDataItem(dataItem));
    final DataMap dataMap = putDataMapRequest.getDataMap();
    String id = dataMap.getString(KEY_ID);
    String title = dataMap.getString(KEY_TITLE);
    String description = dataMap.getString(KEY_DESCRIPTION);
    String date = dataMap.getString(KEY_DATE);
    int color = dataMap.getInt(KEY_COLOR);
    Log.d("NOTIFICATION", "Title " + title + " " + "Desc " + description + " " + "Date " + date);
    Intent intent = new Intent(ACTION_DISMISS);
    intent.putExtra(KEY_ID, dataMap.getString(KEY_ID));
    NotificationCompat.BigTextStyle bigStyle = new NotificationCompat.BigTextStyle();
    bigStyle.bigText(getString(R.string.due) + " " + date + "\n\n" + Html.fromHtml(description));
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.triangle_background);
    // we need to create a copy of the bitmap because we'll be modifying it to tint the color
    Bitmap resultBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth() - 1, bitmap.getHeight() - 1);
    Paint p = new Paint();
    ColorFilter filter = new LightingColorFilter(color, 1);
    p.setColorFilter(filter);
    Canvas canvas = new Canvas(resultBitmap);
    canvas.drawBitmap(resultBitmap, 0, 0, p);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setSmallIcon(R.mipmap.canvas_logo).setLargeIcon(resultBitmap).setContentTitle(title).setContentText(date).setGroup(GROUP_ID).extend(new NotificationCompat.WearableExtender().setBackground(resultBitmap)).setStyle(bigStyle);
    NotificationManagerCompat.from(this).notify(id, NOTIFICATION_ID, builder.build());
}
Also used : Canvas(android.graphics.Canvas) Intent(android.content.Intent) Paint(android.graphics.Paint) Paint(android.graphics.Paint) DataMap(com.google.android.gms.wearable.DataMap) Bitmap(android.graphics.Bitmap) ColorFilter(android.graphics.ColorFilter) LightingColorFilter(android.graphics.LightingColorFilter) LightingColorFilter(android.graphics.LightingColorFilter) NotificationCompat(android.support.v4.app.NotificationCompat) PutDataMapRequest(com.google.android.gms.wearable.PutDataMapRequest)

Example 3 with DataMap

use of com.google.android.gms.wearable.DataMap in project xDrip by NightscoutFoundation.

the class SendToDataLayerThread method sendToWear.

// Debug function to expose where it might be locking up
private synchronized void sendToWear(final DataMap... params) {
    if (!lock.tryLock()) {
        Log.d(TAG, "Concurrent access - waiting for thread unlock");
        // enforce single threading
        lock.lock();
        Log.d(TAG, "Thread unlocked - proceeding");
    }
    lastlock = JoH.tsl();
    try {
        if (state != 0) {
            UserError.Log.e(TAG, "WEAR STATE ERROR: state=" + state);
        }
        state = 1;
        final NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(googleApiClient).await(15, TimeUnit.SECONDS);
        state = 2;
        for (Node node : nodes.getNodes()) {
            state = 3;
            for (DataMap dataMap : params) {
                state = 4;
                PutDataMapRequest putDMR = PutDataMapRequest.create(path);
                state = 5;
                putDMR.getDataMap().putAll(dataMap);
                putDMR.setUrgent();
                state = 6;
                PutDataRequest request = putDMR.asPutDataRequest();
                state = 7;
                DataApi.DataItemResult result = Wearable.DataApi.putDataItem(googleApiClient, request).await(15, TimeUnit.SECONDS);
                state = 8;
                if (result.getStatus().isSuccess()) {
                    UserError.Log.d(TAG, "DataMap: " + dataMap + " sent to: " + node.getDisplayName());
                } else {
                    UserError.Log.e(TAG, "ERROR: failed to send DataMap");
                    result = Wearable.DataApi.putDataItem(googleApiClient, request).await(30, TimeUnit.SECONDS);
                    if (result.getStatus().isSuccess()) {
                        UserError.Log.d(TAG, "DataMap retry: " + dataMap + " sent to: " + node.getDisplayName());
                    } else {
                        UserError.Log.e(TAG, "ERROR on retry: failed to send DataMap: " + result.getStatus().toString());
                    }
                }
                state = 9;
            }
        }
        state = 0;
    } catch (Exception e) {
        UserError.Log.e(TAG, "Got exception in sendToWear: " + e.toString());
    } finally {
        lastlock = 0;
        lock.unlock();
    }
}
Also used : Node(com.google.android.gms.wearable.Node) NodeApi(com.google.android.gms.wearable.NodeApi) PutDataRequest(com.google.android.gms.wearable.PutDataRequest) PutDataMapRequest(com.google.android.gms.wearable.PutDataMapRequest) DataApi(com.google.android.gms.wearable.DataApi) DataMap(com.google.android.gms.wearable.DataMap)

Example 4 with DataMap

use of com.google.android.gms.wearable.DataMap in project xDrip by NightscoutFoundation.

the class WatchUpdaterService method dataMap.

private static DataMap dataMap(Calibration bg) {
    // KS
    DataMap dataMap = new DataMap();
    String json = bg.toS();
    Log.d(TAG, "dataMap BG GSON: " + json);
    dataMap.putString("bgs", json);
    return dataMap;
}
Also used : DataMap(com.google.android.gms.wearable.DataMap)

Example 5 with DataMap

use of com.google.android.gms.wearable.DataMap in project xDrip by NightscoutFoundation.

the class WatchUpdaterService method dataMap.

// sending to watch - beware we munge the calculated value and replace with display glucose
private DataMap dataMap(BgReading bg, SharedPreferences sPrefs, BgGraphBuilder bgGraphBuilder, int battery) {
    Double highMark = Double.parseDouble(sPrefs.getString("highValue", "170"));
    Double lowMark = Double.parseDouble(sPrefs.getString("lowValue", "70"));
    DataMap dataMap = new DataMap();
    // int battery = BgSendQueue.getBatteryLevel(getApplicationContext());
    // TODO this is inefficent when we are called in a loop instead should be passed in or already stored in bgreading
    // current best
    final BestGlucose.DisplayGlucose dg = BestGlucose.getDisplayGlucose();
    dataMap.putString("sgvString", dg != null && bg.dg_mgdl > 0 ? dg.unitized : bgGraphBuilder.unitized_string(bg.calculated_value));
    dataMap.putString("slopeArrow", bg.slopeArrow());
    // TODO: change that to long (was like that in NW)
    dataMap.putDouble("timestamp", bg.timestamp);
    // This delta string only applies to the last reading even if we are processing historical data here
    if (dg != null) {
        dataMap.putString("delta", dg.unitized_delta);
    } else {
        dataMap.putString("delta", bgGraphBuilder.unitizedDeltaString(true, true, true));
    }
    dataMap.putString("battery", "" + battery);
    dataMap.putLong("sgvLevel", sgvLevel(bg.dg_mgdl > 0 ? bg.dg_mgdl : bg.calculated_value, sPrefs, bgGraphBuilder));
    dataMap.putInt("batteryLevel", (battery >= 30) ? 1 : 0);
    dataMap.putDouble("sgvDouble", bg.dg_mgdl > 0 ? bg.dg_mgdl : bg.calculated_value);
    dataMap.putDouble("high", inMgdl(highMark, sPrefs));
    dataMap.putDouble("low", inMgdl(lowMark, sPrefs));
    // Used in DexCollectionService
    dataMap.putInt("bridge_battery", mPrefs.getInt("bridge_battery", -1));
    // dataMap.putString("rawString", threeRaw((prefs.getString("units", "mgdl").equals("mgdl"))));
    return dataMap;
}
Also used : BestGlucose(com.eveningoutpost.dexdrip.BestGlucose) DataMap(com.google.android.gms.wearable.DataMap)

Aggregations

DataMap (com.google.android.gms.wearable.DataMap)147 Date (java.util.Date)38 ArrayList (java.util.ArrayList)36 Gson (com.google.gson.Gson)18 Intent (android.content.Intent)17 GsonBuilder (com.google.gson.GsonBuilder)16 DateTypeAdapter (com.google.gson.internal.bind.DateTypeAdapter)16 BgReading (com.eveningoutpost.dexdrip.Models.BgReading)12 Sensor (com.eveningoutpost.dexdrip.Models.Sensor)12 PendingIntent (android.app.PendingIntent)11 Context (android.content.Context)8 Calibration (com.eveningoutpost.dexdrip.Models.Calibration)8 Treatments (com.eveningoutpost.dexdrip.Models.Treatments)8 Bundle (android.os.Bundle)6 HeartRate (com.eveningoutpost.dexdrip.Models.HeartRate)6 Paint (android.graphics.Paint)5 BloodTest (com.eveningoutpost.dexdrip.Models.BloodTest)5 BroadcastReceiver (android.content.BroadcastReceiver)4 SharedPreferences (android.content.SharedPreferences)4 Point (android.graphics.Point)4