Search in sources :

Example 1 with Node

use of com.google.android.gms.wearable.Node in project Talon-for-Twitter by klinker24.

the class WearableUtils method getNodes.

public HashSet<String> getNodes(GoogleApiClient apiClient) {
    final HashSet<String> results = new HashSet<String>();
    NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(apiClient).await();
    for (Node node : nodes.getNodes()) {
        results.add(node.getId());
    }
    return results;
}
Also used : Node(com.google.android.gms.wearable.Node) NodeApi(com.google.android.gms.wearable.NodeApi) HashSet(java.util.HashSet)

Example 2 with Node

use of com.google.android.gms.wearable.Node in project ETSMobile-Android2 by ApplETS.

the class Utils method getRemoteNodeId.

public static String getRemoteNodeId(GoogleApiClient googleApiClient) {
    NodeApi.GetConnectedNodesResult nodesResult = Wearable.NodeApi.getConnectedNodes(googleApiClient).await();
    List<Node> nodes = nodesResult.getNodes();
    for (Node node : nodes) {
        if (node.isNearby()) {
            return node.getId();
        }
    }
    if (nodes.size() > 0) {
        return nodes.get(0).getId();
    }
    return null;
}
Also used : Node(com.google.android.gms.wearable.Node) NodeApi(com.google.android.gms.wearable.NodeApi)

Example 3 with Node

use of com.google.android.gms.wearable.Node in project muzei by romannurik.

the class ActivateMuzeiIntentService method onHandleIntent.

@Override
protected void onHandleIntent(Intent intent) {
    final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    String action = intent.getAction();
    if (TextUtils.equals(action, ACTION_MARK_NOTIFICATION_READ)) {
        preferences.edit().putBoolean(ACTIVATE_MUZEI_NOTIF_SHOWN_PREF_KEY, true).apply();
        return;
    } else if (TextUtils.equals(action, ACTION_REMOTE_INSTALL_MUZEI)) {
        Intent remoteIntent = new Intent(Intent.ACTION_VIEW).addCategory(Intent.CATEGORY_BROWSABLE).setData(Uri.parse("market://details?id=" + getPackageName()));
        RemoteIntent.startRemoteActivity(this, remoteIntent, new ResultReceiver(new Handler()) {

            @Override
            protected void onReceiveResult(int resultCode, Bundle resultData) {
                if (resultCode == RemoteIntent.RESULT_OK) {
                    FirebaseAnalytics.getInstance(ActivateMuzeiIntentService.this).logEvent("activate_notif_install_sent", null);
                    preferences.edit().putBoolean(ACTIVATE_MUZEI_NOTIF_SHOWN_PREF_KEY, true).apply();
                } else {
                    Toast.makeText(ActivateMuzeiIntentService.this, R.string.activate_install_failed, Toast.LENGTH_SHORT).show();
                }
            }
        });
        return;
    }
    // else -> Open on Phone action
    GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this).addApi(Wearable.API).build();
    ConnectionResult connectionResult = googleApiClient.blockingConnect(30, TimeUnit.SECONDS);
    if (!connectionResult.isSuccess()) {
        Log.e(TAG, "Failed to connect to GoogleApiClient.");
        Toast.makeText(this, R.string.activate_failed, Toast.LENGTH_SHORT).show();
        return;
    }
    Set<Node> nodes = Wearable.CapabilityApi.getCapability(googleApiClient, "activate_muzei", CapabilityApi.FILTER_REACHABLE).await().getCapability().getNodes();
    if (nodes.isEmpty()) {
        Toast.makeText(this, R.string.activate_failed, Toast.LENGTH_SHORT).show();
    } else {
        FirebaseAnalytics.getInstance(this).logEvent("activate_notif_message_sent", null);
        // Show the open on phone animation
        Intent openOnPhoneIntent = new Intent(this, ConfirmationActivity.class);
        openOnPhoneIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        openOnPhoneIntent.putExtra(ConfirmationActivity.EXTRA_ANIMATION_TYPE, ConfirmationActivity.OPEN_ON_PHONE_ANIMATION);
        startActivity(openOnPhoneIntent);
        // Clear the notification
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.cancel(INSTALL_NOTIFICATION_ID);
        preferences.edit().putBoolean(ACTIVATE_MUZEI_NOTIF_SHOWN_PREF_KEY, true).apply();
        // Send the message to the phone to open Muzei
        for (Node node : nodes) {
            Wearable.MessageApi.sendMessage(googleApiClient, node.getId(), "notification/open", null).await();
        }
    }
    googleApiClient.disconnect();
}
Also used : GoogleApiClient(com.google.android.gms.common.api.GoogleApiClient) NotificationManager(android.app.NotificationManager) SharedPreferences(android.content.SharedPreferences) Bundle(android.os.Bundle) ConnectionResult(com.google.android.gms.common.ConnectionResult) Node(com.google.android.gms.wearable.Node) Handler(android.os.Handler) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) RemoteIntent(com.google.android.wearable.intent.RemoteIntent) ResultReceiver(android.os.ResultReceiver)

Example 4 with Node

use of com.google.android.gms.wearable.Node in project muzei by romannurik.

the class ActivateMuzeiIntentService method maybeShowActivateMuzeiNotification.

public static void maybeShowActivateMuzeiNotification(Context context) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    if (preferences.getBoolean(ACTIVATE_MUZEI_NOTIF_SHOWN_PREF_KEY, false)) {
        return;
    }
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
    StatusBarNotification[] notifications = notificationManager.getActiveNotifications();
    boolean hasInstallNotification = false;
    boolean hasActivateNotification = false;
    for (StatusBarNotification notification : notifications) {
        if (notification.getId() == INSTALL_NOTIFICATION_ID) {
            hasInstallNotification = true;
        } else if (notification.getId() == ACTIVATE_NOTIFICATION_ID) {
            hasActivateNotification = true;
        }
    }
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setSmallIcon(R.drawable.ic_stat_muzei).setColor(ContextCompat.getColor(context, R.color.notification)).setPriority(NotificationCompat.PRIORITY_MAX).setDefaults(NotificationCompat.DEFAULT_VIBRATE).setAutoCancel(true).setContentTitle(context.getString(R.string.activate_title));
    Intent deleteIntent = new Intent(context, ActivateMuzeiIntentService.class);
    deleteIntent.setAction(ACTION_MARK_NOTIFICATION_READ);
    builder.setDeleteIntent(PendingIntent.getService(context, 0, deleteIntent, 0));
    // Check if the Muzei main app is installed
    GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context).addApi(Wearable.API).build();
    ConnectionResult connectionResult = googleApiClient.blockingConnect(30, TimeUnit.SECONDS);
    Set<Node> nodes = new TreeSet<>();
    if (connectionResult.isSuccess()) {
        nodes = Wearable.CapabilityApi.getCapability(googleApiClient, "activate_muzei", CapabilityApi.FILTER_ALL).await().getCapability().getNodes();
        googleApiClient.disconnect();
    }
    if (nodes.isEmpty()) {
        if (hasInstallNotification) {
            // No need to repost the notification
            return;
        }
        // Send an install Muzei notification
        if (PlayStoreAvailability.getPlayStoreAvailabilityOnPhone(context) != PlayStoreAvailability.PLAY_STORE_ON_PHONE_AVAILABLE) {
            builder.setContentText(context.getString(R.string.activate_no_play_store));
            FirebaseAnalytics.getInstance(context).logEvent("activate_notif_no_play_store", null);
        } else {
            builder.setContentText(context.getString(R.string.activate_install_muzei));
            Intent installMuzeiIntent = new Intent(context, ActivateMuzeiIntentService.class);
            installMuzeiIntent.setAction(ACTION_REMOTE_INSTALL_MUZEI);
            PendingIntent pendingIntent = PendingIntent.getService(context, 0, installMuzeiIntent, 0);
            builder.addAction(new NotificationCompat.Action.Builder(R.drawable.open_on_phone, context.getString(R.string.activate_install_action), pendingIntent).extend(new NotificationCompat.Action.WearableExtender().setHintDisplayActionInline(true).setAvailableOffline(false)).build());
            builder.extend(new NotificationCompat.WearableExtender().setContentAction(0));
            FirebaseAnalytics.getInstance(context).logEvent("activate_notif_play_store", null);
        }
        notificationManager.notify(INSTALL_NOTIFICATION_ID, builder.build());
        return;
    }
    // else, Muzei is installed on the phone/tablet, but not activated
    if (hasInstallNotification) {
        // Clear any install Muzei notification
        notificationManager.cancel(INSTALL_NOTIFICATION_ID);
    }
    if (hasActivateNotification) {
        // No need to repost the notification
        return;
    }
    String nodeName = nodes.iterator().next().getDisplayName();
    builder.setContentText(context.getString(R.string.activate_enable_muzei, nodeName));
    Intent launchMuzeiIntent = new Intent(context, ActivateMuzeiIntentService.class);
    PendingIntent pendingIntent = PendingIntent.getService(context, 0, launchMuzeiIntent, 0);
    builder.addAction(new NotificationCompat.Action.Builder(R.drawable.open_on_phone, context.getString(R.string.activate_action, nodeName), pendingIntent).extend(new NotificationCompat.Action.WearableExtender().setHintDisplayActionInline(true).setAvailableOffline(false)).build());
    Bitmap background = null;
    try {
        background = BitmapFactory.decodeStream(context.getAssets().open("starrynight.jpg"));
    } catch (IOException e) {
        Log.e(TAG, "Error reading default background asset", e);
    }
    builder.extend(new NotificationCompat.WearableExtender().setContentAction(0).setBackground(background));
    FirebaseAnalytics.getInstance(context).logEvent("activate_notif_installed", null);
    notificationManager.notify(ACTIVATE_NOTIFICATION_ID, builder.build());
}
Also used : GoogleApiClient(com.google.android.gms.common.api.GoogleApiClient) NotificationManager(android.app.NotificationManager) SharedPreferences(android.content.SharedPreferences) Node(com.google.android.gms.wearable.Node) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) RemoteIntent(com.google.android.wearable.intent.RemoteIntent) IOException(java.io.IOException) Bitmap(android.graphics.Bitmap) StatusBarNotification(android.service.notification.StatusBarNotification) TreeSet(java.util.TreeSet) ConnectionResult(com.google.android.gms.common.ConnectionResult) NotificationCompat(android.support.v4.app.NotificationCompat) PendingIntent(android.app.PendingIntent)

Aggregations

Node (com.google.android.gms.wearable.Node)4 NotificationManager (android.app.NotificationManager)2 PendingIntent (android.app.PendingIntent)2 Intent (android.content.Intent)2 SharedPreferences (android.content.SharedPreferences)2 ConnectionResult (com.google.android.gms.common.ConnectionResult)2 GoogleApiClient (com.google.android.gms.common.api.GoogleApiClient)2 NodeApi (com.google.android.gms.wearable.NodeApi)2 RemoteIntent (com.google.android.wearable.intent.RemoteIntent)2 Bitmap (android.graphics.Bitmap)1 Bundle (android.os.Bundle)1 Handler (android.os.Handler)1 ResultReceiver (android.os.ResultReceiver)1 StatusBarNotification (android.service.notification.StatusBarNotification)1 NotificationCompat (android.support.v4.app.NotificationCompat)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 TreeSet (java.util.TreeSet)1