Java Code Examples for com.google.android.gms.wearable.DataEvent#TYPE_DELETED

The following examples show how to use com.google.android.gms.wearable.DataEvent#TYPE_DELETED . 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: NotificationUpdateService.java    From AndroidWearable-Samples with Apache License 2.0 6 votes vote down vote up
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
    for (DataEvent dataEvent : dataEvents) {
        if (dataEvent.getType() == DataEvent.TYPE_CHANGED) {
            DataMap dataMap = DataMapItem.fromDataItem(dataEvent.getDataItem()).getDataMap();
            String content = dataMap.getString(Constants.KEY_CONTENT);
            String title = dataMap.getString(Constants.KEY_TITLE);
            if (Constants.WATCH_ONLY_PATH.equals(dataEvent.getDataItem().getUri().getPath())) {
                buildWearableOnlyNotification(title, content, false);
            } else if (Constants.BOTH_PATH.equals(dataEvent.getDataItem().getUri().getPath())) {
                buildWearableOnlyNotification(title, content, true);
            }
        } else if (dataEvent.getType() == DataEvent.TYPE_DELETED) {
            if (Log.isLoggable(TAG, Log.DEBUG)) {
                Log.d(TAG, "DataItem deleted: " + dataEvent.getDataItem().getUri().getPath());
            }
            if (Constants.BOTH_PATH.equals(dataEvent.getDataItem().getUri().getPath())) {
                // Dismiss the corresponding notification
                ((NotificationManager) getSystemService(NOTIFICATION_SERVICE))
                        .cancel(Constants.BOTH_ID);
            }
        }
    }
}
 
Example 2
Source File: MainActivity.java    From wearable with Apache License 2.0 6 votes vote down vote up
/**
 * Receives the data, note since we are using the same data base, we will also receive
 * data that we sent as well.  Likely should add some kind of id number to datamap, so it
 * can tell between mobile device and wear device.  or this maybe the functionality you want.
 */
@Override
public void onDataChanged(@NonNull DataEventBuffer dataEventBuffer) {
    Log.d(TAG, "onDataChanged: " + dataEventBuffer);
    for (DataEvent event : dataEventBuffer) {
        if (event.getType() == DataEvent.TYPE_CHANGED) {
            String path = event.getDataItem().getUri().getPath();
            if (datapath.equals(path)) {
                DataMapItem dataMapItem = DataMapItem.fromDataItem(event.getDataItem());
                String message = dataMapItem.getDataMap().getString("message");
                Log.v(TAG, "Wear activity received message: " + message);
                // Display message in UI
                logthis(message);
            } else {
                Log.e(TAG, "Unrecognized path: " + path);
            }
        } else if (event.getType() == DataEvent.TYPE_DELETED) {
            Log.v(TAG, "Data deleted : " + event.getDataItem().toString());
        } else {
            Log.e(TAG, "Unknown data event Type = " + event.getType());
        }
    }
}
 
Example 3
Source File: MainActivity.java    From wearable with Apache License 2.0 6 votes vote down vote up
@Override
public void onDataChanged(@NonNull DataEventBuffer dataEventBuffer) {
    Log.d(TAG, "onDataChanged: " + dataEventBuffer);
    for (DataEvent event : dataEventBuffer) {
        if (event.getType() == DataEvent.TYPE_CHANGED) {
            String path = event.getDataItem().getUri().getPath();
            if (datapath.equals(path)) {
                DataMapItem dataMapItem = DataMapItem.fromDataItem(event.getDataItem());
                String message = dataMapItem.getDataMap().getString("message");
                Log.v(TAG, "Wear activity received message: " + message);
                // Display message in UI
                mTextView.setText(message);

            } else {
                Log.e(TAG, "Unrecognized path: " + path);
            }
        } else if (event.getType() == DataEvent.TYPE_DELETED) {
            Log.v(TAG, "Data deleted : " + event.getDataItem().toString());
        } else {
            Log.e(TAG, "Unknown data event Type = " + event.getType());
        }
    }
}
 
Example 4
Source File: MainActivity.java    From wear with MIT License 6 votes vote down vote up
@Override
public void onDataChanged(DataEvent event) {
    if (DataEvent.TYPE_CHANGED == event.getType()) {
        DataMapItem item = DataMapItem.fromDataItem(event.getDataItem());
        Asset asset = item.getDataMap().getAsset(ASSET_KEY);

        ConnectionResult result = mGoogleApiClient
                .blockingConnect(TIMEOUT, TimeUnit.SECONDS);
        if (result.isSuccess()) {
            InputStream stream = Wearable.DataApi
                    .getFdForAsset(mGoogleApiClient, asset)
                    .await().getInputStream();
            if (null != asset) {
                Bitmap bitmap = BitmapFactory.decodeStream(stream);
                showAsset(bitmap);
            }
        }
    } else if (DataEvent.TYPE_DELETED == event.getType()) {
        hideAsset();
    }
}
 
Example 5
Source File: HomeListenerService.java    From AndroidWearable-Samples with Apache License 2.0 6 votes vote down vote up
/**
 * Listen for DataItems added/deleted from the geofence service running on the companion.
 */
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
    if (Log.isLoggable(TAG, Log.DEBUG)) {
        Log.d(TAG, "onDataChanged: " + dataEvents + " for " + getPackageName());
    }
    for (DataEvent event : dataEvents) {
        if (event.getType() == DataEvent.TYPE_DELETED) {
            cancelNotificationForDataItem(event.getDataItem());
        } else if (event.getType() == DataEvent.TYPE_CHANGED) {
            // The user has entered a geofence - post a notification!
            String geofenceId = DataMap.fromByteArray(event.getDataItem().getData())
                    .getString(KEY_GEOFENCE_ID);
            postNotificationForGeofenceId(geofenceId, event.getDataItem().getUri());
        }
    }
    dataEvents.close();
}
 
Example 6
Source File: HomeListenerService.java    From android-Geofencing with Apache License 2.0 6 votes vote down vote up
/**
 * Listen for DataItems added/deleted from the geofence service running on the companion.
 */
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
    if (Log.isLoggable(TAG, Log.DEBUG)) {
        Log.d(TAG, "onDataChanged: " + dataEvents + " for " + getPackageName());
    }
    for (DataEvent event : dataEvents) {
        if (event.getType() == DataEvent.TYPE_DELETED) {
            cancelNotificationForDataItem(event.getDataItem());
        } else if (event.getType() == DataEvent.TYPE_CHANGED) {
            // The user has entered a geofence - post a notification!
            String geofenceId = DataMap.fromByteArray(event.getDataItem().getData())
                    .getString(KEY_GEOFENCE_ID);
            postNotificationForGeofenceId(geofenceId, event.getDataItem().getUri());
        }
    }
}
 
Example 7
Source File: NotificationUpdateService.java    From android-SynchronizedNotifications with Apache License 2.0 6 votes vote down vote up
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
    for (DataEvent dataEvent : dataEvents) {
        if (dataEvent.getType() == DataEvent.TYPE_CHANGED) {
            DataMap dataMap = DataMapItem.fromDataItem(dataEvent.getDataItem()).getDataMap();
            String content = dataMap.getString(Constants.KEY_CONTENT);
            String title = dataMap.getString(Constants.KEY_TITLE);
            if (Constants.WATCH_ONLY_PATH.equals(dataEvent.getDataItem().getUri().getPath())) {
                buildWearableOnlyNotification(title, content, false);
            } else if (Constants.BOTH_PATH.equals(dataEvent.getDataItem().getUri().getPath())) {
                buildWearableOnlyNotification(title, content, true);
            }
        } else if (dataEvent.getType() == DataEvent.TYPE_DELETED) {
            if (Log.isLoggable(TAG, Log.DEBUG)) {
                Log.d(TAG, "DataItem deleted: " + dataEvent.getDataItem().getUri().getPath());
            }
            if (Constants.BOTH_PATH.equals(dataEvent.getDataItem().getUri().getPath())) {
                // Dismiss the corresponding notification
                ((NotificationManager) getSystemService(NOTIFICATION_SERVICE))
                        .cancel(Constants.WATCH_ONLY_ID);
            }
        }
    }
}
 
Example 8
Source File: UARTCommandsActivity.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void onDataChanged(final DataEventBuffer dataEventBuffer) {
	for (final DataEvent event : dataEventBuffer) {
		final DataItem item = event.getDataItem();
		final long id = ContentUris.parseId(item.getUri());

		// Update the configuration only if ID matches
		if (id != configurationId)
			continue;

		// Configuration added or edited
		if (event.getType() == DataEvent.TYPE_CHANGED) {
			final DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();
			final UartConfiguration configuration = new UartConfiguration(dataMap, id);

			// Update UI on UI thread
			runOnUiThread(() -> adapter.setConfiguration(configuration));
		} else if (event.getType() == DataEvent.TYPE_DELETED) {
			// Configuration removed

			// Update UI on UI thread
			runOnUiThread(() -> adapter.setConfiguration(null));
		}
	}
}
 
Example 9
Source File: DismissListener.java    From android-SynchronizedNotifications with Apache License 2.0 5 votes vote down vote up
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
    for (DataEvent dataEvent : dataEvents) {
        if (dataEvent.getType() == DataEvent.TYPE_DELETED) {
            if (Constants.BOTH_PATH.equals(dataEvent.getDataItem().getUri().getPath())) {
                // notification on the phone should be dismissed
                NotificationManagerCompat.from(this).cancel(Constants.BOTH_ID);
            }
        }
    }
}
 
Example 10
Source File: SoundAlarmListenerService.java    From android-FindMyPhone with Apache License 2.0 5 votes vote down vote up
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
    if (Log.isLoggable(TAG, Log.DEBUG)) {
        Log.d(TAG, "onDataChanged: " + dataEvents + " for " + getPackageName());
    }
    for (DataEvent event : dataEvents) {
        if (event.getType() == DataEvent.TYPE_DELETED) {
            Log.i(TAG, event + " deleted");
        } else if (event.getType() == DataEvent.TYPE_CHANGED) {
            Boolean alarmOn =
                    DataMap.fromByteArray(event.getDataItem().getData()).get(FIELD_ALARM_ON);
            if (alarmOn) {
                mOrigVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_ALARM);
                mMediaPlayer.reset();
                // Sound alarm at max volume.
                mAudioManager.setStreamVolume(AudioManager.STREAM_ALARM, mMaxVolume, 0);
                mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
                try {
                    mMediaPlayer.setDataSource(getApplicationContext(), mAlarmSound);
                    mMediaPlayer.prepare();
                } catch (IOException e) {
                    Log.e(TAG, "Failed to prepare media player to play alarm.", e);
                }
                mMediaPlayer.start();
            } else {
                // Reset the alarm volume to the user's original setting.
                mAudioManager.setStreamVolume(AudioManager.STREAM_ALARM, mOrigVolume, 0);
                if (mMediaPlayer.isPlaying()) {
                    mMediaPlayer.stop();
                }
            }
        }
    }
}
 
Example 11
Source File: MainActivity.java    From wear-os-samples with Apache License 2.0 5 votes vote down vote up
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
    Log.d(TAG, "onDataChanged(): " + dataEvents);

    for (DataEvent event : dataEvents) {
        if (event.getType() == DataEvent.TYPE_CHANGED) {
            String path = event.getDataItem().getUri().getPath();
            if (DataLayerListenerService.IMAGE_PATH.equals(path)) {
                DataMapItem dataMapItem = DataMapItem.fromDataItem(event.getDataItem());
                Asset photoAsset =
                        dataMapItem.getDataMap().getAsset(DataLayerListenerService.IMAGE_KEY);
                // Loads image on background thread.
                new LoadBitmapAsyncTask().execute(photoAsset);

            } else if (DataLayerListenerService.COUNT_PATH.equals(path)) {
                Log.d(TAG, "Data Changed for COUNT_PATH");
                mCustomRecyclerAdapter.appendToDataEventLog(
                        "DataItem Changed", event.getDataItem().toString());
            } else {
                Log.d(TAG, "Unrecognized path: " + path);
            }

        } else if (event.getType() == DataEvent.TYPE_DELETED) {
            mCustomRecyclerAdapter.appendToDataEventLog(
                    "DataItem Deleted", event.getDataItem().toString());

        } else {
            mCustomRecyclerAdapter.appendToDataEventLog(
                    "Unknown data event type", "Type = " + event.getType());
        }
    }
}
 
Example 12
Source File: DismissListener.java    From AndroidWearable-Samples with Apache License 2.0 5 votes vote down vote up
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
    for (DataEvent dataEvent : dataEvents) {
        if (dataEvent.getType() == DataEvent.TYPE_DELETED) {
            if (Constants.BOTH_PATH.equals(dataEvent.getDataItem().getUri().getPath())) {
                // notification on the phone should be dismissed
                NotificationManagerCompat.from(this).cancel(Constants.BOTH_ID);
            }
        }
    }
}
 
Example 13
Source File: SoundAlarmListenerService.java    From AndroidWearable-Samples with Apache License 2.0 5 votes vote down vote up
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
    if (Log.isLoggable(TAG, Log.DEBUG)) {
        Log.d(TAG, "onDataChanged: " + dataEvents + " for " + getPackageName());
    }
    for (DataEvent event : dataEvents) {
        if (event.getType() == DataEvent.TYPE_DELETED) {
            Log.i(TAG, event + " deleted");
        } else if (event.getType() == DataEvent.TYPE_CHANGED) {
            Boolean alarmOn =
                    DataMap.fromByteArray(event.getDataItem().getData()).get(FIELD_ALARM_ON);
            if (alarmOn) {
                mOrigVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_ALARM);
                mMediaPlayer.reset();
                // Sound alarm at max volume.
                mAudioManager.setStreamVolume(AudioManager.STREAM_ALARM, mMaxVolume, 0);
                mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
                try {
                    mMediaPlayer.setDataSource(getApplicationContext(), mAlarmSound);
                    mMediaPlayer.prepare();
                } catch (IOException e) {
                    Log.e(TAG, "Failed to prepare media player to play alarm.", e);
                }
                mMediaPlayer.start();
            } else {
                // Reset the alarm volume to the user's original setting.
                mAudioManager.setStreamVolume(AudioManager.STREAM_ALARM, mOrigVolume, 0);
                if (mMediaPlayer.isPlaying()) {
                    mMediaPlayer.stop();
                }
            }
        }
    }
    dataEvents.close();
}
 
Example 14
Source File: MainActivity.java    From AndroidWearable-Samples with Apache License 2.0 5 votes vote down vote up
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
    LOGD(TAG, "onDataChanged(): " + dataEvents);

    final List<DataEvent> events = FreezableUtils.freezeIterable(dataEvents);
    dataEvents.close();
    for (DataEvent event : events) {
        if (event.getType() == DataEvent.TYPE_CHANGED) {
            String path = event.getDataItem().getUri().getPath();
            if (DataLayerListenerService.IMAGE_PATH.equals(path)) {
                DataMapItem dataMapItem = DataMapItem.fromDataItem(event.getDataItem());
                Asset photo = dataMapItem.getDataMap()
                        .getAsset(DataLayerListenerService.IMAGE_KEY);
                final Bitmap bitmap = loadBitmapFromAsset(mGoogleApiClient, photo);
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        Log.d(TAG, "Setting background image..");
                        mLayout.setBackground(new BitmapDrawable(getResources(), bitmap));
                    }
                });

            } else if (DataLayerListenerService.COUNT_PATH.equals(path)) {
                LOGD(TAG, "Data Changed for COUNT_PATH");
                generateEvent("DataItem Changed", event.getDataItem().toString());
            } else {
                LOGD(TAG, "Unrecognized path: " + path);
            }

        } else if (event.getType() == DataEvent.TYPE_DELETED) {
            generateEvent("DataItem Deleted", event.getDataItem().toString());
        } else {
            generateEvent("Unknown data event type", "Type = " + event.getType());
        }
    }
}
 
Example 15
Source File: HomeListenerService.java    From AndroidWearable-Samples with Apache License 2.0 5 votes vote down vote up
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
    if (Log.isLoggable(TAG, Log.DEBUG)) {
        Log.d(TAG, "onDataChanged: " + dataEvents + " for " + getPackageName());
    }
    for (DataEvent event : dataEvents) {
        if (event.getType() == DataEvent.TYPE_DELETED) {
            deleteDataItem(event.getDataItem());
        } else if (event.getType() == DataEvent.TYPE_CHANGED) {
            UpdateNotificationForDataItem(event.getDataItem());
        }
    }
    dataEvents.close();
}
 
Example 16
Source File: MainActivity.java    From wear-os-samples with Apache License 2.0 5 votes vote down vote up
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
    LOGD(TAG, "onDataChanged: " + dataEvents);

    for (DataEvent event : dataEvents) {
        if (event.getType() == DataEvent.TYPE_CHANGED) {
            mDataItemListAdapter.add(
                    new Event("DataItem Changed", event.getDataItem().toString()));
        } else if (event.getType() == DataEvent.TYPE_DELETED) {
            mDataItemListAdapter.add(
                    new Event("DataItem Deleted", event.getDataItem().toString()));
        }
    }
}
 
Example 17
Source File: QuizListenerService.java    From android-Quiz with Apache License 2.0 4 votes vote down vote up
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
    GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .build();

    ConnectionResult connectionResult = googleApiClient.blockingConnect(CONNECT_TIMEOUT_MS,
            TimeUnit.MILLISECONDS);
    if (!connectionResult.isSuccess()) {
        Log.e(TAG, "QuizListenerService failed to connect to GoogleApiClient.");
        return;
    }

    for (DataEvent event : dataEvents) {
        if (event.getType() == DataEvent.TYPE_CHANGED) {
            DataItem dataItem = event.getDataItem();
            DataMap dataMap = DataMapItem.fromDataItem(dataItem).getDataMap();
            if (dataMap.getBoolean(QUESTION_WAS_ANSWERED)
                    || dataMap.getBoolean(QUESTION_WAS_DELETED)) {
                // Ignore the change in data; it is used in MainActivity to update
                // the question's status (i.e. was the answer right or wrong or left blank).
                continue;
            }
            String question = dataMap.getString(QUESTION);
            int questionIndex = dataMap.getInt(QUESTION_INDEX);
            int questionNum = questionIndex + 1;
            String[] answers = dataMap.getStringArray(ANSWERS);
            int correctAnswerIndex = dataMap.getInt(CORRECT_ANSWER_INDEX);
            Intent deleteOperation = new Intent(this, DeleteQuestionService.class);
            deleteOperation.setData(dataItem.getUri());
            PendingIntent deleteIntent = PendingIntent.getService(this, 0,
                    deleteOperation, PendingIntent.FLAG_UPDATE_CURRENT);
            // First page of notification contains question as Big Text.
            Notification.BigTextStyle bigTextStyle = new Notification.BigTextStyle()
                    .setBigContentTitle(getString(R.string.question, questionNum))
                    .bigText(question);
            Notification.Builder builder = new Notification.Builder(this)
                    .setStyle(bigTextStyle)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setLocalOnly(true)
                    .setDeleteIntent(deleteIntent);

            // Add answers as actions.
            Notification.WearableExtender wearableOptions = new Notification.WearableExtender();
            for (int i = 0; i < answers.length; i++) {
                Notification answerPage = new Notification.Builder(this)
                        .setContentTitle(question)
                        .setContentText(answers[i])
                        .extend(new Notification.WearableExtender()
                                .setContentAction(i))
                        .build();

                boolean correct = (i == correctAnswerIndex);
                Intent updateOperation = new Intent(this, UpdateQuestionService.class);
                // Give each intent a unique action.
                updateOperation.setAction("question_" + questionIndex + "_answer_" + i);
                updateOperation.setData(dataItem.getUri());
                updateOperation.putExtra(UpdateQuestionService.EXTRA_QUESTION_INDEX,
                        questionIndex);
                updateOperation.putExtra(UpdateQuestionService.EXTRA_QUESTION_CORRECT, correct);
                PendingIntent updateIntent = PendingIntent.getService(this, 0, updateOperation,
                        PendingIntent.FLAG_UPDATE_CURRENT);
                Notification.Action action = new Notification.Action.Builder(
                        questionNumToDrawableId.get(i), null, updateIntent)
                        .build();
                wearableOptions.addAction(action).addPage(answerPage);
            }
            builder.extend(wearableOptions);
            Notification notification = builder.build();
            ((NotificationManager) getSystemService(NOTIFICATION_SERVICE))
                    .notify(questionIndex, notification);
        } else if (event.getType() == DataEvent.TYPE_DELETED) {
            Uri uri = event.getDataItem().getUri();
            // URI's are of the form "/question/0", "/question/1" etc.
            // We use the question index as the notification id.
            int notificationId = Integer.parseInt(uri.getLastPathSegment());
            ((NotificationManager) getSystemService(NOTIFICATION_SERVICE))
                    .cancel(notificationId);
        }
        // Delete the quiz report, if it exists.
        ((NotificationManager) getSystemService(NOTIFICATION_SERVICE))
                .cancel(QUIZ_REPORT_NOTIF_ID);
    }
    googleApiClient.disconnect();
}
 
Example 18
Source File: QuizListenerService.java    From AndroidWearable-Samples with Apache License 2.0 4 votes vote down vote up
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
    final List<DataEvent> events = FreezableUtils.freezeIterable(dataEvents);
    dataEvents.close();

    GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .build();

    ConnectionResult connectionResult = googleApiClient.blockingConnect(CONNECT_TIMEOUT_MS,
            TimeUnit.MILLISECONDS);
    if (!connectionResult.isSuccess()) {
        Log.e(TAG, "QuizListenerService failed to connect to GoogleApiClient.");
        return;
    }

    for (DataEvent event : events) {
        if (event.getType() == DataEvent.TYPE_CHANGED) {
            DataItem dataItem = event.getDataItem();
            DataMap dataMap = DataMapItem.fromDataItem(dataItem).getDataMap();
            if (dataMap.getBoolean(QUESTION_WAS_ANSWERED)
                    || dataMap.getBoolean(QUESTION_WAS_DELETED)) {
                // Ignore the change in data; it is used in MainActivity to update
                // the question's status (i.e. was the answer right or wrong or left blank).
                continue;
            }
            String question = dataMap.getString(QUESTION);
            int questionIndex = dataMap.getInt(QUESTION_INDEX);
            int questionNum = questionIndex + 1;
            String[] answers = dataMap.getStringArray(ANSWERS);
            int correctAnswerIndex = dataMap.getInt(CORRECT_ANSWER_INDEX);
            Intent deleteOperation = new Intent(this, DeleteQuestionService.class);
            deleteOperation.setData(dataItem.getUri());
            PendingIntent deleteIntent = PendingIntent.getService(this, 0,
                    deleteOperation, PendingIntent.FLAG_UPDATE_CURRENT);
            // First page of notification contains question as Big Text.
            Notification.BigTextStyle bigTextStyle = new Notification.BigTextStyle()
                    .setBigContentTitle(getString(R.string.question, questionNum))
                    .bigText(question);
            Notification.Builder builder = new Notification.Builder(this)
                    .setStyle(bigTextStyle)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setLocalOnly(true)
                    .setDeleteIntent(deleteIntent);

            // Add answers as actions.
            Notification.WearableExtender wearableOptions = new Notification.WearableExtender();
            for (int i = 0; i < answers.length; i++) {
                Notification answerPage = new Notification.Builder(this)
                        .setContentTitle(question)
                        .setContentText(answers[i])
                        .extend(new Notification.WearableExtender()
                                .setContentAction(i))
                        .build();

                boolean correct = (i == correctAnswerIndex);
                Intent updateOperation = new Intent(this, UpdateQuestionService.class);
                // Give each intent a unique action.
                updateOperation.setAction("question_" + questionIndex + "_answer_" + i);
                updateOperation.setData(dataItem.getUri());
                updateOperation.putExtra(UpdateQuestionService.EXTRA_QUESTION_INDEX,
                        questionIndex);
                updateOperation.putExtra(UpdateQuestionService.EXTRA_QUESTION_CORRECT, correct);
                PendingIntent updateIntent = PendingIntent.getService(this, 0, updateOperation,
                        PendingIntent.FLAG_UPDATE_CURRENT);
                Notification.Action action = new Notification.Action.Builder(
                        questionNumToDrawableId.get(i), null, updateIntent)
                        .build();
                wearableOptions.addAction(action).addPage(answerPage);
            }
            builder.extend(wearableOptions);
            Notification notification = builder.build();
            ((NotificationManager) getSystemService(NOTIFICATION_SERVICE))
                    .notify(questionIndex, notification);
        } else if (event.getType() == DataEvent.TYPE_DELETED) {
            Uri uri = event.getDataItem().getUri();
            // URI's are of the form "/question/0", "/question/1" etc.
            // We use the question index as the notification id.
            int notificationId = Integer.parseInt(uri.getLastPathSegment());
            ((NotificationManager) getSystemService(NOTIFICATION_SERVICE))
                    .cancel(notificationId);
        }
        // Delete the quiz report, if it exists.
        ((NotificationManager) getSystemService(NOTIFICATION_SERVICE))
                .cancel(QUIZ_REPORT_NOTIF_ID);
    }
    googleApiClient.disconnect();
}
 
Example 19
Source File: MainActivity.java    From DronesWear with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
    for (DataEvent event : dataEvents) {

        DataItem dataItem = event.getDataItem();
        Message.MESSAGE_TYPE messageType = Message.getMessageType(dataItem);
        if (event.getType() == DataEvent.TYPE_DELETED) {
            switch (messageType) {
                case ACC:
                    synchronized (mDroneLock) {
                        if (mDrone != null && mUseWatchAccelero) {
                            mDrone.stopPiloting();
                        }

                    }
                    break;
                case JOYSTICK:
                    synchronized (mDroneLock) {
                        if (mDrone != null) {
                            mDrone.stopPiloting();
                        }

                    }
                    break;
            }
        } else if (event.getType() == DataEvent.TYPE_CHANGED) {
            switch (messageType) {
                case ACC:
                    synchronized (mDroneLock) {
                        if (mDrone != null && mUseWatchAccelero) {
                            AccelerometerData accelerometerData = Message.decodeAcceleroMessage(dataItem);
                            if (accelerometerData != null) {
                                mDrone.pilotWithAcceleroData(accelerometerData);
                            } else {
                                mDrone.stopPiloting();
                            }
                        }

                    }
                    break;
                case JOYSTICK:
                    synchronized (mDroneLock) {
                        if (mDrone != null) {
                            JoystickData joystickData = Message.decodeJoystickMessage(dataItem);
                            if (joystickData != null) {
                                mDrone.pilotWithJoystickData(joystickData);
                            } else {
                                mDrone.stopPiloting();
                            }
                        }

                    }
                    break;
                case ACTION:
                    if (mDrone != null) {
                        mDrone.sendAction();
                    }
                    break;
            }

        }
    }
}
 
Example 20
Source File: PixelWatchFace.java    From PixelWatchFace with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onDataChanged(DataEventBuffer dataEvents) {
  String TAG = "onDataChanged";
  Log.d(TAG, "Data changed");
  DataMap dataMap = new DataMap();
  for (DataEvent event : dataEvents) {
    if (event.getType() == DataEvent.TYPE_CHANGED) {
      // DataItem changed
      DataItem item = event.getDataItem();
      Log.d(TAG, "DataItem uri: " + item.getUri());

      if (item.getUri().getPath().compareTo("/settings") == 0) {
        Log.d(TAG, "Companion app changed a setting!");
        dataMap = DataMapItem.fromDataItem(item).getDataMap();
        Log.d(TAG, dataMap.toString());

        // handle legacy nested data map
        if (dataMap.containsKey("com.corvettecole.pixelwatchface")) {
          dataMap = dataMap.getDataMap("com.corvettecole.pixelwatchface");
        }

        Log.d(TAG, dataMap.toString());

        for (UpdatesRequired updateRequired : mSettings.updateSettings(dataMap)) {
          switch (updateRequired) {
            case WEATHER:
              initWeatherUpdater(true);
              break;
            case FONT:
              updateFontConfig();
              break;
          }
        }

        invalidate();// forces redraw
        //syncToPhone();
      } else if (item.getUri().getPath().compareTo("/requests") == 0) {
        if (dataMap.containsKey("settings-update")) {

        }
      }


    } else if (event.getType() == DataEvent.TYPE_DELETED) {
      // DataItem deleted
    }
  }
}