Java Code Examples for com.google.android.gms.wearable.DataMap#getAsset()

The following examples show how to use com.google.android.gms.wearable.DataMap#getAsset() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DataLayerListenerService.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
@Override
public void onDataChanged(final DataEventBuffer dataEvents) {
    super.onDataChanged(dataEvents);
    for (DataEvent event : dataEvents) {
        Uri uri = event.getDataItem().getUri();
        if (event.getType() == DataEvent.TYPE_CHANGED
                && uri.getPath().startsWith(WearConst.PATH_CANVAS)) {

            DataMapItem dataMapItem = DataMapItem.fromDataItem(event.getDataItem());
            DataMap map = dataMapItem.getDataMap();
            List<String> segments = uri.getPathSegments();

            String selfId = ((WearApplication) getApplication()).getSelfId();
            String wearId = segments.get(2);
            if (selfId == null || !selfId.equals(wearId)) {
                //WearのIDが違っていれば無視
                return;
            }
            String nodeId = uri.getHost();
            String requestId = segments.get(3);
            Asset profileAsset = map.getAsset(WearConst.PARAM_BITMAP);
            int x = map.getInt(WearConst.PARAM_X);
            int y = map.getInt(WearConst.PARAM_Y);
            int mode = map.getInt(WearConst.PARAM_MODE);
            Intent intent = new Intent();
            intent.setClass(this, CanvasActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra(WearConst.PARAM_SOURCE_ID, nodeId);
            intent.putExtra(WearConst.PARAM_REQUEST_ID, requestId);
            intent.putExtra(WearConst.PARAM_BITMAP, profileAsset);
            intent.putExtra(WearConst.PARAM_X, x);
            intent.putExtra(WearConst.PARAM_Y, y);
            intent.putExtra(WearConst.PARAM_MODE, mode);
            startActivity(intent);
        }
    }
}
 
Example 2
Source File: DataBundleUtil.java    From android_external_GmsLib with Apache License 2.0 4 votes vote down vote up
@Override
Asset load(DataMap dataMap, String key) {
    return dataMap.getAsset(key);
}
 
Example 3
Source File: HomeListenerService.java    From AndroidWearable-Samples with Apache License 2.0 4 votes vote down vote up
/**
 * Posts a local notification to show calendar card.
 */
private void UpdateNotificationForDataItem(DataItem dataItem) {
    DataMapItem mapDataItem = DataMapItem.fromDataItem(dataItem);
    DataMap data = mapDataItem.getDataMap();

    String description = data.getString(DESCRIPTION);
    if (TextUtils.isEmpty(description)) {
        description = "";
    } else {
        // Add a space between the description and the time of the event.
        description += " ";
    }
    String contentText;
    if (data.getBoolean(ALL_DAY)) {
        contentText = getString(R.string.desc_all_day, description);
    } else {
        String startTime = DateFormat.getTimeFormat(this).format(new Date(data.getLong(BEGIN)));
        String endTime = DateFormat.getTimeFormat(this).format(new Date(data.getLong(END)));
        contentText = getString(R.string.desc_time_period, description, startTime, endTime);
    }

    Intent deleteOperation = new Intent(this, DeleteService.class);
    // Use a unique identifier for the delete action.
    String deleteAction = "action_delete" + dataItem.getUri().toString() + sNotificationId;
    deleteOperation.setAction(deleteAction);
    deleteOperation.setData(dataItem.getUri());
    PendingIntent deleteIntent = PendingIntent.getService(this, 0, deleteOperation,
            PendingIntent.FLAG_ONE_SHOT);
    PendingIntent silentDeleteIntent = PendingIntent.getService(this, 1,
            deleteOperation.putExtra(EXTRA_SILENT, true), PendingIntent.FLAG_ONE_SHOT);

    Notification.Builder notificationBuilder = new Notification.Builder(this)
            .setContentTitle(data.getString(TITLE))
            .setContentText(contentText)
            .setSmallIcon(R.drawable.ic_launcher)
            .addAction(R.drawable.ic_menu_delete, getText(R.string.delete), deleteIntent)
            .setDeleteIntent(silentDeleteIntent)  // Delete DataItem if notification dismissed.
            .setLocalOnly(true)
            .setPriority(Notification.PRIORITY_MIN);

    // Set the event owner's profile picture as the notification background.
    Asset asset = data.getAsset(PROFILE_PIC);
    if (null != asset) {
        if (mGoogleApiClient.isConnected()) {
            DataApi.GetFdForAssetResult assetFdResult =
                    Wearable.DataApi.getFdForAsset(mGoogleApiClient, asset).await();
            if (assetFdResult.getStatus().isSuccess()) {
                Bitmap profilePic = BitmapFactory.decodeStream(assetFdResult.getInputStream());
                notificationBuilder.extend(new Notification.WearableExtender()
                        .setBackground(profilePic));
            } else if (Log.isLoggable(TAG, Log.DEBUG)) {
                Log.d(TAG, "asset fetch failed with statusCode: "
                        + assetFdResult.getStatus().getStatusCode());
            }
        } else {
            Log.e(TAG, "Failed to set notification background"
                     + " - Client disconnected from Google Play Services");
        }
    }
    Notification card = notificationBuilder.build();

    ((NotificationManager) getSystemService(NOTIFICATION_SERVICE))
            .notify(sNotificationId, card);

    sNotificationIdByDataItemUri.put(dataItem.getUri(), sNotificationId++);
}
 
Example 4
Source File: NotificationListenerService.java    From wear-notify-for-reddit with Apache License 2.0 4 votes vote down vote up
private void createNotificationForPost(DataMap dataMap, boolean openOnPhoneDismisses,
                                       ArrayList<Integer> actionOrder, Bitmap themeBlueBitmap,
                                       NotificationManager notificationManager, Post post) {
    try {
        Bitmap backgroundBitmap = null;
        if (dataMap.containsKey(post.getId()) && dataMap.getAsset(post.getId()) != null) {
            backgroundBitmap = loadBitmapFromAsset(dataMap.getAsset(post.getId()));
        }

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setContentTitle(
                post.getTitle())
                .setContentText(post.getPostContents())
                .setSmallIcon(R.drawable.ic_launcher);

        boolean hasCachedImage;
        if (backgroundBitmap != null) {
            // If the post has a thumbnail, use it - this will filter out nfsw etc thumbnails
            // but will still allow the user to see the full image if they like
            builder.setLargeIcon(backgroundBitmap);
            hasCachedImage = cacheBackgroundToDisk(sNotificationId, backgroundBitmap);
        } else {
            hasCachedImage = false;
            setBlueBackground(themeBlueBitmap, builder);
            enableNotificationGrouping(builder);
        }

        addActions(actionOrder,
                openOnPhoneDismisses,
                hasCachedImage,
                post,
                sNotificationId,
                builder);

        if (hasCachedImage) {
            // When the notification is dismissed, we will remove this image from the file cache
            builder.setDeleteIntent(getDeletePendingIntent(sNotificationId));
        }

        notificationManager.notify(sNotificationId, builder.build());

        if (backgroundBitmap != null) {
            backgroundBitmap.recycle();
        }

        sNotificationId += NOTIFICATION_ID_INCREMENT;
        sendBroadcast(new Intent(getString(R.string.force_finish_main_activity)));
    } catch (Exception e) {
        logErrorToPhone("Failed to create notification for post: " + post, e);
    }
}