Search in sources :

Example 1 with RemoteIntent

use of com.google.android.wearable.intent.RemoteIntent 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)

Aggregations

NotificationManager (android.app.NotificationManager)1 PendingIntent (android.app.PendingIntent)1 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 Bundle (android.os.Bundle)1 Handler (android.os.Handler)1 ResultReceiver (android.os.ResultReceiver)1 ConnectionResult (com.google.android.gms.common.ConnectionResult)1 GoogleApiClient (com.google.android.gms.common.api.GoogleApiClient)1 Node (com.google.android.gms.wearable.Node)1 RemoteIntent (com.google.android.wearable.intent.RemoteIntent)1