1 /*------------------------------------------------------------------------- 2 * drawElements Quality Program Tester Core 3 * ---------------------------------------- 4 * 5 * Copyright 2014 The Android Open Source Project 6 * 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 * 19 *//*! 20 * \file 21 * \brief ExecServer service. 22 *//*--------------------------------------------------------------------*/ 23 24 package com.drawelements.deqp.execserver; 25 26 import android.app.Service; 27 import android.app.Notification; 28 import android.app.Notification.Builder; 29 import android.app.NotificationChannel; 30 import android.app.NotificationManager; 31 import android.app.PendingIntent; 32 import android.content.Context; 33 import android.content.Intent; 34 import android.os.Binder; 35 import android.os.Build; 36 import android.os.IBinder; 37 38 import com.drawelements.deqp.execserver.ExecServerActivity; 39 import com.drawelements.deqp.testercore.Log; 40 import com.drawelements.deqp.R; 41 42 public class ExecService extends Service { 43 44 public static final String LOG_TAG = "dEQP"; 45 public static final int DEFAULT_PORT = 50016; 46 47 private int m_curPort = -1; 48 private boolean m_isRunning = false; 49 50 static { 51 System.loadLibrary("deqp"); 52 } 53 54 long m_server = 0; //!< Server pointer. 55 56 // \note No IPC handling, all clients must be in same process 57 public class LocalBinder extends Binder { getService()58 ExecService getService () { 59 return ExecService.this; 60 } 61 } 62 63 private final IBinder m_binder = new LocalBinder(); 64 65 @Override onStartCommand(Intent intent, int flags, int startId)66 public int onStartCommand (Intent intent, int flags, int startId) { 67 final int port = intent != null ? intent.getIntExtra("port", DEFAULT_PORT) : DEFAULT_PORT; 68 69 if (m_isRunning && (m_curPort != port)) { 70 Log.i(LOG_TAG, String.format("Port changed (old: %d, new: %d), killing old server", m_curPort, port)); 71 stopServer(); 72 m_isRunning = false; 73 } 74 75 if (!m_isRunning) { 76 startServer(port); 77 78 m_isRunning = true; 79 m_curPort = port; 80 81 Log.i(LOG_TAG, String.format("Listening on port %d", m_curPort)); 82 } 83 84 // Intent to launch when notification is clicked. 85 Intent launchIntent = new Intent(this, ExecServerActivity.class); 86 launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 87 PendingIntent pm = PendingIntent.getActivity(this, 0, launchIntent, 0); 88 89 // Start as foreground service. 90 String channel = ""; 91 92 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) 93 { 94 channel = "com.drawelements.deqp.execserver"; 95 96 NotificationChannel noteChan = new NotificationChannel(channel, "dEQP ExecServer", NotificationManager.IMPORTANCE_LOW); 97 NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 98 manager.createNotificationChannel(noteChan); 99 } 100 101 Notification.Builder builder = new Notification.Builder(this, channel); 102 Notification notification = builder.setContentIntent(pm) 103 .setSmallIcon(R.drawable.deqp_app_small).setTicker("ExecServer is running in the background.") 104 .setWhen(System.currentTimeMillis()).setAutoCancel(true).setContentTitle("dEQP ExecServer") 105 .setContentText("ExecServer is running in the background.").build(); 106 startForeground(1, notification); 107 108 return START_STICKY; // Keep us running until explictly stopped 109 } 110 111 @Override onBind(Intent intent)112 public IBinder onBind (Intent intent) { 113 return m_binder; 114 } 115 116 @Override onDestroy()117 public void onDestroy () { 118 if (m_isRunning) { 119 stopServer(); 120 m_isRunning = false; 121 } 122 } 123 startServer(int port)124 private native void startServer (int port); stopServer()125 private native void stopServer (); 126 } 127