1/*
2 * Copyright (C) 2013 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
17package com.android.example.notificationshowcase;
18
19import android.app.IntentService;
20import android.app.NotificationManager;
21import android.app.PendingIntent;
22import android.content.Context;
23import android.content.Intent;
24import android.util.Log;
25
26public class UpdateService extends IntentService {
27
28    private static final String TAG = "UpdateService";
29
30    private static final String ACTION_UPDATE = "update";
31
32    public UpdateService() {
33        super(TAG);
34    }
35
36    public UpdateService(String name) {
37        super(name);
38    }
39
40    @Override
41    protected void onHandleIntent(Intent intent) {
42
43        try {
44            // allow the user close the shade, if they want to test that.
45            Thread.sleep(3000);
46        } catch (Exception e) {
47        }
48        Log.v(TAG, "clicked a thing! intent=" + intent.toString());
49        if (intent.hasExtra("id") && intent.hasExtra("when")) {
50            final int id = intent.getIntExtra("id", 0);
51                NotificationManager noMa =
52                        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
53                final int update = intent.getIntExtra("update", 0);
54                final long when = intent.getLongExtra("when", 0L);
55                Log.v(TAG, "id: " + id + " when: " + when + " update: " + update);
56                noMa.notify(NotificationService.NOTIFICATION_ID + id,
57                        NotificationService.makeBigTextNotification(this, update, id, when));
58        } else {
59            Log.v(TAG, "id extra was " + (intent.hasExtra("id") ? "present" : "missing"));
60            Log.v(TAG, "when extra was " + (intent.hasExtra("when") ? "present"  : "missing"));
61        }
62    }
63
64    public static PendingIntent getPendingIntent(Context context, int update, int id, long when) {
65        Intent updateIntent = new Intent(context, UpdateService.class);
66        updateIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
67        updateIntent.setAction(ACTION_UPDATE);
68        updateIntent.putExtra("id", id);
69        updateIntent.putExtra("when", when);
70        updateIntent.putExtra("update", update);
71        PendingIntent pi = PendingIntent.getService(
72                context, 58, updateIntent, PendingIntent.FLAG_UPDATE_CURRENT);
73        return pi;
74    }
75}
76