1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.components.devtools_bridge.tests;
7 import android.app.Notification;
8 import android.app.PendingIntent;
9 import android.app.Service;
10 import android.content.Intent;
11 import android.os.Binder;
12 import android.os.IBinder;
13 import android.os.Process;
14 import android.widget.Toast;
16 import org.chromium.components.devtools_bridge.LocalTunnelBridge;
18 import java.io.IOException;
21 * Service for testing devtools bridge.
23 public class DebugService extends Service {
24 private static final String PACKAGE = "org.chromium.components.devtools_bridge.tests";
25 public static final String START_ACTION = PACKAGE + ".START_ACTION";
26 public static final String STOP_ACTION = PACKAGE + ".STOP_ACTION";
27 private static final int NOTIFICATION_ID = 1;
29 private LocalTunnelBridge mBridge;
31 private LocalTunnelBridge createBridge() throws IOException {
32 String exposingSocketName = "webview_devtools_remote_" + Integer.valueOf(Process.myPid());
33 return new LocalTunnelBridge("chrome_shell_devtools_remote", exposingSocketName);
37 public int onStartCommand(Intent intent, int flags, int startId) {
38 if (intent == null) return START_NOT_STICKY;
40 String action = intent.getAction();
41 if (START_ACTION.equals(action)) {
43 } else if (STOP_ACTION.equals(action)) {
46 return START_NOT_STICKY;
51 return START_NOT_STICKY;
54 mBridge = createBridge();
56 } catch (Exception e) {
57 Toast.makeText(this, "Failed to start", Toast.LENGTH_SHORT).show();
60 return START_NOT_STICKY;
63 startForeground(NOTIFICATION_ID, makeForegroundServiceNotification());
64 Toast.makeText(this, "Service started", Toast.LENGTH_SHORT).show();
70 return START_NOT_STICKY;
76 Toast.makeText(this, "Service stopped", Toast.LENGTH_SHORT).show();
77 return START_NOT_STICKY;
81 public IBinder onBind(Intent intent) {
85 private Notification makeForegroundServiceNotification() {
86 Intent showInfoIntent = new Intent(this, DebugActivity.class);
87 showInfoIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
88 PendingIntent showInfoPendingIntent =
89 PendingIntent.getActivity(DebugService.this, 0, showInfoIntent, 0);
91 Intent stopIntent = new Intent(this, DebugService.class);
92 stopIntent.setAction(STOP_ACTION);
93 PendingIntent stopPendingIntent =
94 PendingIntent.getService(DebugService.this, 0, stopIntent, 0);
96 return new Notification.Builder(this)
98 .setSmallIcon(android.R.drawable.alert_dark_frame)
99 .setContentTitle("DevTools Bridge")
100 .setContentText("DevTools socket local test tunnel works")
103 .setContentIntent(showInfoPendingIntent)
104 .addAction(android.R.drawable.ic_delete,
105 "Stop", stopPendingIntent)
107 .setWhen(System.currentTimeMillis())