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 org.drrickorang.loopback; 18 19 import android.app.Notification; 20 import android.app.NotificationChannel; 21 import android.app.NotificationManager; 22 import android.app.Service; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.os.Build; 26 import android.os.IBinder; 27 import android.os.Binder; 28 import android.util.Log; 29 30 31 /** 32 * This is the Service being created during the first onStart() in the activity. 33 * Threads that are needed for the test will be created under this Service. 34 * At the end of the test, this Service will pass the test results back to LoopbackActivity. 35 */ 36 37 public class AudioTestService extends Service { 38 private static final String TAG = "AudioTestService"; 39 private static final String CHANNEL_ID = "AudioTestChannel"; 40 private static final int NOTIFICATION_ID = 1400; 41 42 private final IBinder mBinder = new AudioTestBinder(); 43 private NotificationChannel mNotificationChannel; 44 45 @Override 46 public void onCreate() { 47 runAsForegroundService(); 48 log("Audio Test Service created!"); 49 } 50 51 52 @Override 53 public int onStartCommand(Intent intent, int flags, int startId) { 54 log("Service onStartCommand: " + startId); 55 //runAsForegroundService(); 56 return Service.START_NOT_STICKY; 57 } 58 59 60 /** 61 * This method will run the Service as Foreground Service, so the Service won't be killed 62 * and restarted after a while. 63 */ 64 private void runAsForegroundService() { 65 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 66 mNotificationChannel = new NotificationChannel( 67 CHANNEL_ID, 68 getString(R.string.notificationText), 69 NotificationManager.IMPORTANCE_LOW); 70 NotificationManager notificationManager = 71 (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 72 notificationManager.createNotificationChannel(mNotificationChannel); 73 } 74 75 Notification.Builder builder = new Notification.Builder(this) 76 .setSmallIcon(R.drawable.ic_launcher).setContentTitle(getString(R.string.app_name)) 77 .setContentText(getString(R.string.notificationText)); 78 if (mNotificationChannel != null) { 79 builder.setChannelId(CHANNEL_ID); 80 } 81 Notification notification; 82 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { 83 notification = builder.build(); 84 } else { 85 notification = builder.getNotification(); 86 } 87 88 startForeground(NOTIFICATION_ID, notification); 89 } 90 91 92 @Override 93 public IBinder onBind(Intent intent) { 94 log("Service onBind"); 95 return mBinder; 96 } 97 98 99 @Override 100 public void onDestroy() { 101 log("Service onDestroy"); 102 if (mNotificationChannel != null) { 103 NotificationManager notificationManager = 104 (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 105 notificationManager.deleteNotificationChannel(CHANNEL_ID); 106 } 107 } 108 109 110 private static void log(String msg) { 111 Log.v(TAG, msg); 112 } 113 114 115 /** 116 * This class is only used by AudioTestService to create a binder that passes the 117 * AudioTestService back to LoopbackActivity. 118 */ 119 public class AudioTestBinder extends Binder { 120 AudioTestService getService() { 121 return AudioTestService.this; 122 } 123 } 124 125 } 126