1b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reck/*
2b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reck * Copyright (C) 2016 The Android Open Source Project
3b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reck *
4b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reck * Licensed under the Apache License, Version 2.0 (the "License");
5b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reck * you may not use this file except in compliance with the License.
6b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reck * You may obtain a copy of the License at
7b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reck *
8b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reck *      http://www.apache.org/licenses/LICENSE-2.0
9b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reck *
10b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reck * Unless required by applicable law or agreed to in writing, software
11b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reck * distributed under the License is distributed on an "AS IS" BASIS,
12b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reck * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reck * See the License for the specific language governing permissions and
14b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reck * limitations under the License.
15b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reck */
16b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reck
17b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reckpackage test.amslam;
18b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reck
19b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reckimport android.app.Service;
20b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reckimport android.content.Intent;
21b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reckimport android.os.Handler;
22b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reckimport android.os.IBinder;
23b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reckimport android.os.SystemClock;
24b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reck
25b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reckpublic class PingReceiver extends Service {
26b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reck
27b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reck    @Override
28b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reck    public int onStartCommand(Intent intent, int flags, int startId) {
29b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reck        Intent response = new Intent(this, PongReceiver.class);
30b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reck        response.putExtra("start_time", intent.getLongExtra("start_time", 0));
31b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reck        response.putExtra("bounce_time", SystemClock.uptimeMillis());
32b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reck        response.putExtra("receiver", getClass().getSimpleName());
33b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reck        sendBroadcast(response);
34b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reck        stopSelf();
35b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reck        // If we exit before returning from onStartCommand the system will
36b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reck        // think we crashed and attempt a re-delivery, which we don't want here.
37b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reck        // Post'ing the kill deals with this just fine.
38b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reck        new Handler().post(() -> System.exit(0));
39b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reck        return START_NOT_STICKY;
40b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reck    }
41b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reck
42b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reck    @Override
43b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reck    public IBinder onBind(Intent intent) {
44b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reck        return null;
45b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reck    }
46b89a63447e8fe6a31f5f4e4f23d3439f9e4ddc94John Reck}
47