Skip to content

Most visited

Recently visited

navigation
WearNotifications / Application / src / com.example.android.wearable.wear.wearnotifications /

MainActivity.java

1
/*
2
Copyright 2016 The Android Open Source Project
3
 
4
Licensed under the Apache License, Version 2.0 (the "License");
5
you may not use this file except in compliance with the License.
6
You may obtain a copy of the License at
7
 
8
http://www.apache.org/licenses/LICENSE-2.0
9
 
10
Unless required by applicable law or agreed to in writing, software
11
distributed under the License is distributed on an "AS IS" BASIS,
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
See the License for the specific language governing permissions and
14
limitations under the License.
15
 */
16
package com.example.android.wearable.wear.wearnotifications;
17
 
18
import android.app.Notification;
19
import android.app.PendingIntent;
20
import android.content.Intent;
21
import android.graphics.BitmapFactory;
22
import android.os.Build;
23
import android.os.Bundle;
24
import android.support.design.widget.Snackbar;
25
import android.support.v4.app.NotificationCompat.BigPictureStyle;
26
import android.support.v4.app.NotificationCompat.BigTextStyle;
27
import android.support.v4.app.NotificationCompat.InboxStyle;
28
import android.support.v4.app.NotificationCompat.MessagingStyle;
29
import android.support.v4.app.NotificationManagerCompat;
30
import android.support.v4.app.RemoteInput;
31
import android.support.v4.app.TaskStackBuilder;
32
import android.support.v7.app.AppCompatActivity;
33
import android.support.v7.app.NotificationCompat;
34
import android.util.Log;
35
import android.view.View;
36
import android.widget.AdapterView;
37
import android.widget.ArrayAdapter;
38
import android.widget.RelativeLayout;
39
import android.widget.Spinner;
40
import android.widget.TextView;
41
 
42
 
43
 
44
import com.example.android.wearable.wear.wearnotifications.handlers.BigPictureSocialIntentService;
45
import com.example.android.wearable.wear.wearnotifications.handlers.BigPictureSocialMainActivity;
46
import com.example.android.wearable.wear.wearnotifications.handlers.BigTextIntentService;
47
import com.example.android.wearable.wear.wearnotifications.handlers.BigTextMainActivity;
48
import com.example.android.wearable.wear.wearnotifications.handlers.InboxMainActivity;
49
import com.example.android.wearable.wear.wearnotifications.handlers.MessagingIntentService;
50
import com.example.android.wearable.wear.wearnotifications.handlers.MessagingMainActivity;
51
import com.example.android.wearable.wear.wearnotifications.mock.MockDatabase;
52
 
53
/**
54
 * The Activity demonstrates several popular Notification.Style examples along with their best
55
 * practices (include proper Android Wear support when you don't have a dedicated Android Wear
56
 * app).
57
 */
58
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
59
 
60
    public static final String TAG = "MainActivity";
61
 
62
    public static final int NOTIFICATION_ID = 888;
63
 
64
    // Used for Notification Style array and switch statement for Spinner selection
65
    private static final String BIG_TEXT_STYLE = "BIG_TEXT_STYLE";
66
    private static final String BIG_PICTURE_STYLE = "BIG_PICTURE_STYLE";
67
    private static final String INBOX_STYLE = "INBOX_STYLE";
68
    private static final String MESSAGING_STYLE = "MESSAGING_STYLE";
69
 
70
    // Collection of notification styles to back ArrayAdapter for Spinner
71
    private static final String[] NOTIFICATION_STYLES =
72
            {BIG_TEXT_STYLE, BIG_PICTURE_STYLE, INBOX_STYLE, MESSAGING_STYLE};
73
 
74
    private static final String[] NOTIFICATION_STYLES_DESCRIPTION =
75
            {
76
                    "Demos reminder type app using BIG_TEXT_STYLE",
77
                    "Demos social type app using BIG_PICTURE_STYLE + inline notification response",
78
                    "Demos email type app using INBOX_STYLE",
79
                    "Demos messaging app using MESSAGING_STYLE + inline notification responses"
80
            };
81
 
82
    private NotificationManagerCompat mNotificationManagerCompat;
83
 
84
    private int mSelectedNotification = 0;
85
 
86
    // RelativeLayout is needed for SnackBars to alert users when Notifications are disabled for app
87
    private RelativeLayout mMainRelativeLayout;
88
    private Spinner mSpinner;
89
    private TextView mNotificationDetailsTextView;
90
 
91
    @Override
92
    protected void onCreate(Bundle savedInstanceState) {
93
 
94
        super.onCreate(savedInstanceState);
95
        setContentView(R.layout.activity_main);
96
 
97
        mMainRelativeLayout = (RelativeLayout) findViewById(R.id.mainRelativeLayout);
98
        mNotificationDetailsTextView = (TextView) findViewById(R.id.notificationDetails);
99
        mSpinner = (Spinner) findViewById(R.id.spinner);
100
 
101
        mNotificationManagerCompat = NotificationManagerCompat.from(getApplicationContext());
102
 
103
        // Create an ArrayAdapter using the string array and a default spinner layout
104
        ArrayAdapter<CharSequence> adapter =
105
                new ArrayAdapter(
106
                        this,
107
                        android.R.layout.simple_spinner_item,
108
                        NOTIFICATION_STYLES);
109
        // Specify the layout to use when the list of choices appears
110
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
111
        // Apply the adapter to the spinner
112
        mSpinner.setAdapter(adapter);
113
        mSpinner.setOnItemSelectedListener(this);
114
    }
115
 
116
    @Override
117
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
118
        Log.d(TAG, "onItemSelected(): position: " + position + " id: " + id);
119
 
120
        mSelectedNotification = position;
121
 
122
        mNotificationDetailsTextView.setText(
123
                NOTIFICATION_STYLES_DESCRIPTION[mSelectedNotification]);
124
    }
125
    @Override
126
    public void onNothingSelected(AdapterView<?> parent) {
127
        // Required
128
    }
129
 
130
    public void onClick(View view) {
131
 
132
        Log.d(TAG, "onClick()");
133
 
134
        boolean areNotificationsEnabled = mNotificationManagerCompat.areNotificationsEnabled();
135
 
136
        if (!areNotificationsEnabled) {
137
            // Because the user took an action to create a notification, we create a prompt to let
138
            // the user re-enable notifications for this application again.
139
            Snackbar snackbar = Snackbar
140
                    .make(
141
                            mMainRelativeLayout,
142
                            "You need to enable notifications for this app",
143
                            Snackbar.LENGTH_LONG)
144
                    .setAction("ENABLE", new View.OnClickListener() {
145
                        @Override
146
                        public void onClick(View view) {
147
                            // Links to this app's notification settings
148
                            openNotificationSettingsForApp();
149
                        }
150
                    });
151
            snackbar.show();
152
            return;
153
        }
154
 
155
        String notificationStyle = NOTIFICATION_STYLES[mSelectedNotification];
156
 
157
        switch (notificationStyle) {
158
            case BIG_TEXT_STYLE:
159
                generateBigTextStyleNotification();
160
                break;
161
 
162
            case BIG_PICTURE_STYLE:
163
                generateBigPictureStyleNotification();
164
                break;
165
 
166
            case INBOX_STYLE:
167
                generateInboxStyleNotification();
168
                break;
169
 
170
            case MESSAGING_STYLE:
171
                generateMessagingStyleNotification();
172
                break;
173
 
174
            default:
175
                // continue below
176
        }
177
    }
178
 
179
    /*
180
     * Generates a BIG_TEXT_STYLE Notification that supports both phone/tablet and wear. For devices
181
     * on API level 16 (4.1.x - Jelly Bean) and after, displays BIG_TEXT_STYLE. Otherwise, displays
182
     * a basic notification.
183
     */
184
    private void generateBigTextStyleNotification() {
185
 
186
        Log.d(TAG, "generateBigTextStyleNotification()");
187
 
188
        // Main steps for building a BIG_TEXT_STYLE notification:
189
        //      0. Get your data
190
        //      1. Build the BIG_TEXT_STYLE
191
        //      2. Set up main Intent for notification
192
        //      3. Create additional Actions for the Notification
193
        //      4. Build and issue the notification
194
 
195
        // 0. Get your data (everything unique per Notification)
196
        MockDatabase.BigTextStyleReminderAppData bigTextStyleReminderAppData =
197
                MockDatabase.getBigTextStyleData();
198
 
199
        // 1. Build the BIG_TEXT_STYLE
200
        BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle()
201
                // Overrides ContentText in the big form of the template
202
                .bigText(bigTextStyleReminderAppData.getBigText())
203
                // Overrides ContentTitle in the big form of the template
204
                .setBigContentTitle(bigTextStyleReminderAppData.getBigContentTitle())
205
                // Summary line after the detail section in the big form of the template
206
                // Note: To improve readability, don't overload the user with info. If Summary Text
207
                // doesn't add critical information, you should skip it.
208
                .setSummaryText(bigTextStyleReminderAppData.getSummaryText());
209
 
210
 
211
        // 2. Set up main Intent for notification
212
        Intent notifyIntent = new Intent(this, BigTextMainActivity.class);
213
 
214
        // When creating your Intent, you need to take into account the back state, i.e., what
215
        // happens after your Activity launches and the user presses the back button.
216
 
217
        // There are two options:
218
        //      1. Regular activity - You're starting an Activity that's part of the application's
219
        //      normal workflow.
220
 
221
        //      2. Special activity - The user only sees this Activity if it's started from a
222
        //      notification. In a sense, the Activity extends the notification by providing
223
        //      information that would be hard to display in the notification itself.
224
 
225
        // For the BIG_TEXT_STYLE notification, we will consider the activity launched by the main
226
        // Intent as a special activity, so we will follow option 2.
227
 
228
        // For an example of option 1, check either the MESSAGING_STYLE or BIG_PICTURE_STYLE
229
        // examples.
230
 
231
        // For more information, check out our dev article:
232
        // https://developer.android.com/training/notify-user/navigation.html
233
 
234
        // Sets the Activity to start in a new, empty task
235
        notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
236
 
237
        PendingIntent notifyPendingIntent =
238
                PendingIntent.getActivity(
239
                        this,
240
                        0,
241
                        notifyIntent,
242
                        PendingIntent.FLAG_UPDATE_CURRENT
243
                );
244
 
245
 
246
        // 3. Create additional Actions (Intents) for the Notification
247
 
248
        // In our case, we create two additional actions: a Snooze action and a Dismiss action
249
        // Snooze Action
250
        Intent snoozeIntent = new Intent(this, BigTextIntentService.class);
251
        snoozeIntent.setAction(BigTextIntentService.ACTION_SNOOZE);
252
 
253
        PendingIntent snoozePendingIntent = PendingIntent.getService(this, 0, snoozeIntent, 0);
254
        NotificationCompat.Action snoozeAction =
255
                new NotificationCompat.Action.Builder(
256
                        R.drawable.ic_alarm_white_48dp,
257
                        "Snooze",
258
                        snoozePendingIntent)
259
                        .build();
260
 
261
 
262
        // Dismiss Action
263
        Intent dismissIntent = new Intent(this, BigTextIntentService.class);
264
        dismissIntent.setAction(BigTextIntentService.ACTION_DISMISS);
265
 
266
        PendingIntent dismissPendingIntent = PendingIntent.getService(this, 0, dismissIntent, 0);
267
        NotificationCompat.Action dismissAction =
268
                new NotificationCompat.Action.Builder(
269
                        R.drawable.ic_cancel_white_48dp,
270
                        "Dismiss",
271
                        dismissPendingIntent)
272
                        .build();
273
 
274
 
275
        // 4. Build and issue the notification
276
 
277
        // Because we want this to be a new notification (not updating a previous notification), we
278
        // create a new Builder. Later, we use the same global builder to get back the notification
279
        // we built here for the snooze action, that is, canceling the notification and relaunching
280
        // it several seconds later.
281
 
282
        NotificationCompat.Builder notificationCompatBuilder =
283
                new NotificationCompat.Builder(getApplicationContext());
284
 
285
        GlobalNotificationBuilder.setNotificationCompatBuilderInstance(notificationCompatBuilder);
286
 
287
        Notification notification = notificationCompatBuilder
288
                // BIG_TEXT_STYLE sets title and content for API 16 (4.1 and after)
289
                .setStyle(bigTextStyle)
290
                // Title for API <16 (4.0 and below) devices
291
                .setContentTitle(bigTextStyleReminderAppData.getContentTitle())
292
                // Content for API <24 (7.0 and below) devices
293
                .setContentText(bigTextStyleReminderAppData.getContentText())
294
                .setSmallIcon(R.drawable.ic_launcher)
295
                .setLargeIcon(BitmapFactory.decodeResource(
296
                        getResources(),
297
                        R.drawable.ic_alarm_white_48dp))
298
                .setContentIntent(notifyPendingIntent)
299
                // Set primary color (important for Wear 2.0 Notifications)
300
                .setColor(getResources().getColor(R.color.colorPrimary))
301
 
302
                // SIDE NOTE: Auto-bundling is enabled for 4 or more notifications on API 24+ (N+)
303
                // devices and all Android Wear devices. If you have more than one notification and
304
                // you prefer a differen