Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / components / devtools_bridge / android / javatests / src / org / chromium / components / devtools_bridge / tests / DebugService.java
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.
4
5 package org.chromium.components.devtools_bridge.tests;
6
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;
15
16 import org.chromium.components.devtools_bridge.LocalTunnelBridge;
17
18 import java.io.IOException;
19
20 /**
21  * Service for testing devtools bridge.
22  */
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;
28
29     private LocalTunnelBridge mBridge;
30
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);
34     }
35
36     @Override
37     public int onStartCommand(Intent intent, int flags, int startId) {
38         if (intent == null) return START_NOT_STICKY;
39
40         String action = intent.getAction();
41         if (START_ACTION.equals(action)) {
42             return start();
43         } else if (STOP_ACTION.equals(action)) {
44             return stop();
45         }
46         return START_NOT_STICKY;
47     }
48
49     private int start() {
50         if (mBridge != null)
51             return START_NOT_STICKY;
52
53         try {
54             mBridge = createBridge();
55             mBridge.start();
56         } catch (Exception e) {
57             Toast.makeText(this, "Failed to start", Toast.LENGTH_SHORT).show();
58             mBridge.dispose();
59             mBridge = null;
60             return START_NOT_STICKY;
61         }
62
63         startForeground(NOTIFICATION_ID, makeForegroundServiceNotification());
64         Toast.makeText(this, "Service started", Toast.LENGTH_SHORT).show();
65         return START_STICKY;
66     }
67
68     private int stop() {
69         if (mBridge == null)
70             return START_NOT_STICKY;
71
72         mBridge.stop();
73         mBridge.dispose();
74         mBridge = null;
75         stopSelf();
76         Toast.makeText(this, "Service stopped", Toast.LENGTH_SHORT).show();
77         return START_NOT_STICKY;
78     }
79
80     @Override
81     public IBinder onBind(Intent intent) {
82         return new Binder();
83     }
84
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);
90
91         Intent stopIntent = new Intent(this, DebugService.class);
92         stopIntent.setAction(STOP_ACTION);
93         PendingIntent stopPendingIntent =
94                     PendingIntent.getService(DebugService.this, 0, stopIntent, 0);
95
96         return new Notification.Builder(this)
97                 // Mandatory fiends
98                 .setSmallIcon(android.R.drawable.alert_dark_frame)
99                 .setContentTitle("DevTools Bridge")
100                 .setContentText("DevTools socket local test tunnel works")
101
102                 // Optional
103                 .setContentIntent(showInfoPendingIntent)
104                 .addAction(android.R.drawable.ic_delete,
105                         "Stop", stopPendingIntent)
106                 .setOngoing(true)
107                 .setWhen(System.currentTimeMillis())
108                 .build();
109     }
110 }
111