StopwatchService.java revision 6d603b7c62bb38d763a681a8bf20fadb1442e833
1/*
2 * Copyright (C) 2015 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.deskclock.stopwatch;
18
19import android.app.Service;
20import android.content.Intent;
21import android.os.IBinder;
22
23import com.android.deskclock.HandleDeskClockApiCalls;
24import com.android.deskclock.R;
25import com.android.deskclock.data.DataModel;
26import com.android.deskclock.events.Events;
27
28/**
29 * This service exists primarily to allow the stopwatch notification to alter the state of the
30 * stopwatch without disturbing the notification shade. If an activity were used instead (even one
31 * that is not displayed) the notification manager implicitly closes the notification shade which
32 * clashes with the use case of starting/pausing/lapping/resetting the stopwatch without
33 * interruption.
34 */
35public final class StopwatchService extends Service {
36
37    @Override
38    public IBinder onBind(Intent intent) {
39        return null;
40    }
41
42    @Override
43    public int onStartCommand(Intent intent, int flags, int startId) {
44        final boolean fromNotification =
45                intent.getBooleanExtra(HandleDeskClockApiCalls.EXTRA_FROM_NOTIFICATION, false);
46
47        switch (intent.getAction()) {
48            case HandleDeskClockApiCalls.ACTION_START_STOPWATCH: {
49                DataModel.getDataModel().startStopwatch();
50                if (fromNotification) {
51                    Events.sendStopwatchEvent(R.string.action_start, R.string.label_notification);
52                }
53                break;
54            }
55            case HandleDeskClockApiCalls.ACTION_PAUSE_STOPWATCH: {
56                DataModel.getDataModel().pauseStopwatch();
57                if (fromNotification) {
58                    Events.sendStopwatchEvent(R.string.action_pause, R.string.label_notification);
59                }
60                break;
61            }
62            case HandleDeskClockApiCalls.ACTION_LAP_STOPWATCH: {
63                DataModel.getDataModel().addLap();
64                if (fromNotification) {
65                    Events.sendStopwatchEvent(R.string.action_lap, R.string.label_notification);
66                }
67                break;
68            }
69            case HandleDeskClockApiCalls.ACTION_RESET_STOPWATCH: {
70                DataModel.getDataModel().clearLaps();
71                DataModel.getDataModel().resetStopwatch();
72                if (fromNotification) {
73                    Events.sendStopwatchEvent(R.string.action_reset, R.string.label_notification);
74                }
75                break;
76            }
77        }
78
79        return Service.START_NOT_STICKY;
80    }
81}