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 
17 package com.android.deskclock.stopwatch;
18 
19 import android.app.Service;
20 import android.content.Intent;
21 import android.os.IBinder;
22 
23 import com.android.deskclock.HandleDeskClockApiCalls;
24 import com.android.deskclock.R;
25 import com.android.deskclock.data.DataModel;
26 import com.android.deskclock.events.Events;
27 
28 /**
29  * This service exists solely 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  * disturbing the notification shade.
34  */
35 public final class StopwatchService extends Service {
36 
37     @Override
onBind(Intent intent)38     public IBinder onBind(Intent intent) {
39         return null;
40     }
41 
42     @Override
onStartCommand(Intent intent, int flags, int startId)43     public int onStartCommand(Intent intent, int flags, int startId) {
44         switch (intent.getAction()) {
45             case HandleDeskClockApiCalls.ACTION_START_STOPWATCH: {
46                 DataModel.getDataModel().startStopwatch();
47                 Events.sendStopwatchEvent(R.string.action_start, R.string.label_notification);
48                 break;
49             }
50             case HandleDeskClockApiCalls.ACTION_PAUSE_STOPWATCH: {
51                 DataModel.getDataModel().pauseStopwatch();
52                 Events.sendStopwatchEvent(R.string.action_pause, R.string.label_notification);
53                 break;
54             }
55             case HandleDeskClockApiCalls.ACTION_LAP_STOPWATCH: {
56                 DataModel.getDataModel().addLap();
57                 Events.sendStopwatchEvent(R.string.action_lap, R.string.label_notification);
58                 break;
59             }
60             case HandleDeskClockApiCalls.ACTION_RESET_STOPWATCH: {
61                 DataModel.getDataModel().clearLaps();
62                 DataModel.getDataModel().resetStopwatch();
63                 Events.sendStopwatchEvent(R.string.action_reset, R.string.label_notification);
64                 break;
65             }
66         }
67 
68         return Service.START_NOT_STICKY;
69     }
70 }