• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.Notification;
27 import android.app.Notification.Builder;
28 import android.app.NotificationChannel;
29 import android.app.NotificationManager;
30 import android.app.PendingIntent;
31 import android.app.Service;
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 import com.drawelements.deqp.R;
38 import com.drawelements.deqp.execserver.ExecServerActivity;
39 import com.drawelements.deqp.testercore.Log;
40 
41 public class ExecService extends Service {
42 
43     public static final String LOG_TAG = "dEQP";
44     public static final int DEFAULT_PORT = 50016;
45 
46     private int m_curPort = -1;
47     private boolean m_isRunning = false;
48 
49     static { System.loadLibrary("deqp"); }
50 
51     long m_server = 0; //!< Server pointer.
52 
53     // \note No IPC handling, all clients must be in same process
54     public class LocalBinder extends Binder {
getService()55         ExecService getService() { return ExecService.this; }
56     }
57 
58     private final IBinder m_binder = new LocalBinder();
59 
60     @Override
onStartCommand(Intent intent, int flags, int startId)61     public int onStartCommand(Intent intent, int flags, int startId) {
62         final int port = intent != null
63                              ? intent.getIntExtra("port", DEFAULT_PORT)
64                              : DEFAULT_PORT;
65 
66         if (m_isRunning && (m_curPort != port)) {
67             Log.i(LOG_TAG,
68                   String.format(
69                       "Port changed (old: %d, new: %d), killing old server",
70                       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             channel = "com.drawelements.deqp.execserver";
94 
95             NotificationChannel noteChan = new NotificationChannel(
96                 channel, "dEQP ExecServer", NotificationManager.IMPORTANCE_LOW);
97             NotificationManager manager = (NotificationManager)getSystemService(
98                 Context.NOTIFICATION_SERVICE);
99             manager.createNotificationChannel(noteChan);
100         }
101 
102         Notification.Builder builder = new Notification.Builder(this, channel);
103         Notification notification =
104             builder.setContentIntent(pm)
105                 .setSmallIcon(R.drawable.deqp_app_small)
106                 .setTicker("ExecServer is running in the background.")
107                 .setWhen(System.currentTimeMillis())
108                 .setAutoCancel(true)
109                 .setContentTitle("dEQP ExecServer")
110                 .setContentText("ExecServer is running in the background.")
111                 .build();
112         startForeground(1, notification);
113 
114         return START_STICKY; // Keep us running until explictly stopped
115     }
116 
117     @Override
onBind(Intent intent)118     public IBinder onBind(Intent intent) {
119         return m_binder;
120     }
121 
122     @Override
onDestroy()123     public void onDestroy() {
124         if (m_isRunning) {
125             stopServer();
126             m_isRunning = false;
127         }
128     }
129 
startServer(int port)130     private native void startServer(int port);
stopServer()131     private native void stopServer();
132 }
133