aboutsummaryrefslogtreecommitdiff
path: root/wearable/wear/WearAccessibilityApp/Wearable/src/main/java/com/example/android/wearable/wear/wearaccessibilityapp/NotificationsActivity.java
blob: 2cf8bd757831dff9c398dce6420c7299221a0fa4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
 * Copyright (C) 2017 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.android.wearable.wear.wearaccessibilityapp;

import android.app.Notification;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceFragment;
import android.preference.SwitchPreference;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.MessagingStyle;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.app.RemoteInput;
import android.support.wearable.activity.WearableActivity;
import android.util.Log;

public class NotificationsActivity extends WearableActivity {

    public static final int NOTIFICATION_ID = 888;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Display the fragment as the main content.
        getFragmentManager()
                .beginTransaction()
                .replace(android.R.id.content, new NotificationsPrefsFragment())
                .commit();
    }

    public static class NotificationsPrefsFragment extends PreferenceFragment {

        private static final String TAG = "NotificationsActivity";
        private NotificationManagerCompat mNotificationManagerCompat;
        private boolean mActionOn; // if true, displays in-line action
        private boolean mAvatarOn; // if true, displays avatar of messenger

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // Load the preferences from an XML resource
            addPreferencesFromResource(R.xml.prefs_notifications);

            mNotificationManagerCompat = NotificationManagerCompat.from(getActivity());

            final SwitchPreference mActionSwitchPref =
                    (SwitchPreference) findPreference(getString(R.string.key_pref_action));
            final SwitchPreference mAvatarSwitchPref =
                    (SwitchPreference) findPreference(getString(R.string.key_pref_avatar));
            Preference mPushNotificationPref =
                    findPreference(getString(R.string.key_pref_push_notification));

            initInLineAction(mActionSwitchPref);
            initAvatar(mAvatarSwitchPref);
            initPushNotification(mPushNotificationPref);
        }

        public void initInLineAction(SwitchPreference switchPref) {
            switchPref.setChecked(true);
            mActionOn = switchPref.isChecked();
            switchPref.setOnPreferenceChangeListener(
                    new OnPreferenceChangeListener() {
                        @Override
                        public boolean onPreferenceChange(Preference preference, Object newValue) {
                            mActionOn = (Boolean) newValue;
                            return true;
                        }
                    });
        }

        public void initAvatar(SwitchPreference switchPref) {
            switchPref.setChecked(true);
            mAvatarOn = switchPref.isChecked();
            switchPref.setOnPreferenceChangeListener(
                    new OnPreferenceChangeListener() {
                        @Override
                        public boolean onPreferenceChange(Preference preference, Object newValue) {
                            mAvatarOn = (Boolean) newValue;
                            return true;
                        }
                    });
        }

        public void initPushNotification(Preference pref) {
            pref.setOnPreferenceClickListener(
                    new OnPreferenceClickListener() {
                        @Override
                        public boolean onPreferenceClick(Preference preference) {
                            generateMessagingStyleNotification();
                            return true;
                        }
                    });
        }

        /*
         * Generates a MESSAGING_STYLE Notification that supports both Wear 1.+ and Wear 2.0. For
         * devices on API level 24 (Wear 2.0) and after, displays MESSAGING_STYLE. Otherwise,
         * displays a basic BIG_TEXT_STYLE.
         *
         * IMPORTANT NOTE:
         * Notification Styles behave slightly different on Wear 2.0 when they are launched by a
         * native/local Wear app, i.e., they will NOT expand when the user taps them but will
         * instead take the user directly into the local app for the richest experience. In
         * contrast, a bridged Notification launched from the phone will expand with the style
         * details (whether there is a local app or not).
         *
         * If you want to enable an action on your Notification without launching the app, you can
         * do so with the setHintDisplayActionInline() feature (shown below), but this only allows
         * one action.
         *
         * If you wish to replicate the original experience of a bridged notification, please
         * review the generateBigTextStyleNotification() method above to see how.
         */
        private void generateMessagingStyleNotification() {
            Log.d(TAG, "generateMessagingStyleNotification()");

            // Main steps for building a MESSAGING_STYLE notification:
            //      0. Get your data
            //      1. Build the MESSAGING_STYLE
            //      2. Set up main Intent for notification
            //      3. Set up RemoteInput (users can input directly from notification)
            //      4. Build and issue the notification

            // 0. Get your data (everything unique per Notification)
            MockDatabase.MessagingStyleCommsAppData messagingStyleCommsAppData =
                    MockDatabase.getMessagingStyleData();

            // 1. Build the Notification.Style (MESSAGING_STYLE)
            String contentTitle = messagingStyleCommsAppData.getContentTitle();

            MessagingStyle messagingStyle =
                    new NotificationCompat.MessagingStyle(
                                    messagingStyleCommsAppData.getReplayName())
                            // You could set a different title to appear when the messaging style
                            // is supported on device (24+) if you wish. In our case, we use the
                            // same
                            // title.
                            .setConversationTitle(contentTitle);

            // Adds all Messages
            // Note: Messages include the text, timestamp, and sender
            for (MessagingStyle.Message message : messagingStyleCommsAppData.getMessages()) {
                messagingStyle.addMessage(message);
            }

            // 2. Set up main Intent for notification
            Intent notifyIntent = new Intent(getActivity(), MessagingMainActivity.class);

            PendingIntent mainPendingIntent =
                    PendingIntent.getActivity(
                            getActivity(), 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            // 3. Set up a RemoteInput Action, so users can input (keyboard, drawing, voice)
            // directly from the notification without entering the app.

            // Create the RemoteInput specifying this key.
            String replyLabel = getString(R.string.reply_label);
            RemoteInput remoteInput =
                    new RemoteInput.Builder(MessagingIntentService.EXTRA_REPLY)
                            .setLabel(replyLabel)
                            .build();

            // Create PendingIntent for service that handles input.
            Intent replyIntent = new Intent(getActivity(), MessagingIntentService.class);
            replyIntent.setAction(MessagingIntentService.ACTION_REPLY);
            PendingIntent replyActionPendingIntent =
                    PendingIntent.getService(getActivity(), 0, replyIntent, 0);

            // Enable action to appear inline on Wear 2.0 (24+). This means it will appear over the
            // lower portion of the Notification for easy action (only possible for one action).
            final NotificationCompat.Action.WearableExtender inlineActionForWear2 =
                    new NotificationCompat.Action.WearableExtender()
                            .setHintDisplayActionInline(mActionOn)
                            .setHintLaunchesActivity(false);

            NotificationCompat.Action replyAction =
                    new NotificationCompat.Action.Builder(
                                    R.drawable.reply, replyLabel, replyActionPendingIntent)
                            .addRemoteInput(remoteInput)
                            // Allows system to generate replies by context of conversation
                            .setAllowGeneratedReplies(true)
                            // Add WearableExtender to enable inline actions
                            .extend(inlineActionForWear2)
                            .build();

            // 4. Build and issue the notification

            // Because we want this to be a new notification (not updating current notification),
            // we create a new Builder. Later, we update this same notification, so we need to save
            // this Builder globally (as outlined earlier).

            NotificationCompat.Builder notificationCompatBuilder =
                    new NotificationCompat.Builder(getActivity());

            GlobalNotificationBuilder.setNotificationCompatBuilderInstance(
                    notificationCompatBuilder);

            // Builds and issues notification
            notificationCompatBuilder
                    // MESSAGING_STYLE sets title and content for Wear 1.+ and Wear 2.0 devices.
                    .setStyle(messagingStyle)
                    .setContentTitle(contentTitle)
                    .setContentText(messagingStyleCommsAppData.getContentText())
                    .setSmallIcon(R.drawable.watch)
                    .setContentIntent(mainPendingIntent)

                    // Number of new notifications for API <24 (Wear 1.+) devices
                    .setSubText(
                            Integer.toString(messagingStyleCommsAppData.getNumberOfNewMessages()))
                    .addAction(replyAction)
                    .setCategory(Notification.CATEGORY_MESSAGE)
                    .setPriority(Notification.PRIORITY_HIGH)

                    // Hides content on the lock-screen
                    .setVisibility(Notification.VISIBILITY_PRIVATE);

            notificationCompatBuilder.setLargeIcon(
                    BitmapFactory.decodeResource(
                            getResources(), mAvatarOn ? R.drawable.avatar : R.drawable.watch));

            // If the phone is in "Do not disturb mode, the user will still be notified if
            // the sender(s) is starred as a favorite.
            for (String name : messagingStyleCommsAppData.getParticipants()) {
                notificationCompatBuilder.addPerson(name);
            }

            Notification notification = notificationCompatBuilder.build();
            mNotificationManagerCompat.notify(NOTIFICATION_ID, notification);

            // Close app to demonstrate notification in steam.
            getActivity().finish();
        }
    }
}