• 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.Service;
27 import android.app.Notification;
28 import android.app.Notification.Builder;
29 import android.app.PendingIntent;
30 import android.content.Intent;
31 import android.os.Binder;
32 import android.os.IBinder;
33 
34 import com.drawelements.deqp.execserver.ExecServerActivity;
35 import com.drawelements.deqp.testercore.Log;
36 import com.drawelements.deqp.R;
37 
38 public class ExecService extends Service {
39 
40 	public static final String	LOG_TAG			= "dEQP";
41 	public static final int		DEFAULT_PORT	= 50016;
42 
43 	private int					m_curPort		= -1;
44 	private boolean				m_isRunning		= false;
45 
46 	static {
47 		System.loadLibrary("deqp");
48 	}
49 
50 	long m_server = 0; //!< Server pointer.
51 
52 	// \note No IPC handling, all clients must be in same process
53 	public class LocalBinder extends Binder {
getService()54 		ExecService getService () {
55 			return ExecService.this;
56 		}
57 	}
58 
59 	private final IBinder m_binder = new LocalBinder();
60 
61 	@Override
onStartCommand(Intent intent, int flags, int startId)62 	public int onStartCommand (Intent intent, int flags, int startId) {
63 		final int port = intent != null ? intent.getIntExtra("port", DEFAULT_PORT) : DEFAULT_PORT;
64 
65 		if (m_isRunning && (m_curPort != port)) {
66 			Log.i(LOG_TAG, String.format("Port changed (old: %d, new: %d), killing old server", m_curPort, port));
67 			stopServer();
68 			m_isRunning = false;
69 		}
70 
71 		if (!m_isRunning) {
72 			startServer(port);
73 
74 			m_isRunning	= true;
75 			m_curPort	= port;
76 
77 			Log.i(LOG_TAG, String.format("Listening on port %d", m_curPort));
78 		}
79 
80 		// Intent to launch when notification is clicked.
81 		Intent launchIntent = new Intent(this, ExecServerActivity.class);
82 		launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
83 		PendingIntent pm = PendingIntent.getActivity(this, 0, launchIntent, 0);
84 
85 		// Start as foreground service.
86 		Notification.Builder builder = new Notification.Builder(this);
87 		Notification notification = builder.setContentIntent(pm)
88 			.setSmallIcon(R.drawable.deqp_app_small).setTicker("ExecServer is running in the background.")
89 			.setWhen(System.currentTimeMillis()).setAutoCancel(true).setContentTitle("dEQP ExecServer")
90 			.setContentText("ExecServer is running in the background.").build();
91 		startForeground(1, notification);
92 
93 		return START_STICKY; // Keep us running until explictly stopped
94 	}
95 
96 	@Override
onBind(Intent intent)97 	public IBinder onBind (Intent intent) {
98 		return m_binder;
99 	}
100 
101 	@Override
onDestroy()102 	public void onDestroy () {
103 		if (m_isRunning) {
104 			stopServer();
105 			m_isRunning = false;
106 		}
107 	}
108 
startServer(int port)109 	private native void startServer	(int port);
stopServer()110 	private native void stopServer	();
111 }
112