aboutsummaryrefslogtreecommitdiff
path: root/third_party/sl4a/src/main/java/com/google/android/mobly/snippet/SnippetRunner.java
blob: ffa6a2ee21207239818c6a0b900b576476e5d41b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 * Copyright (C) 2016 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.android.mobly.snippet;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Bundle;
import android.os.Process;
import android.support.test.runner.AndroidJUnitRunner;

import com.google.android.mobly.snippet.rpc.AndroidProxy;
import com.google.android.mobly.snippet.util.Log;
import com.google.android.mobly.snippet.util.NotificationIdFactory;

/**
 * A launcher that starts the snippet server as an instrumentation so that it has access to the
 * target app's context.
 *
 * It is written this way to be compatible with 'am instrument'.
 */
public class SnippetRunner extends AndroidJUnitRunner {
    private static final String ARG_ACTION = "action";
    private static final String ARG_PORT = "port";

    private enum Action {START, STOP};

    private static final int NOTIFICATION_ID = NotificationIdFactory.create();

    private Bundle mArguments;
    private NotificationManager mNotificationManager;
    private Notification mNotification;

    @Override
    public void onCreate(Bundle arguments) {
        mArguments = arguments;
        mNotificationManager = (NotificationManager)
                getTargetContext().getSystemService(Context.NOTIFICATION_SERVICE);
        super.onCreate(arguments);
    }

    @Override
    public void onStart() {
        String actionStr = mArguments.getString(ARG_ACTION);
        if (actionStr == null) {
            throw new IllegalArgumentException("\"--e action <action>\" was not specified");
        }
        Action action = Action.valueOf(actionStr.toUpperCase());
        switch (action) {
        case START:
            String servicePort = mArguments.getString(ARG_PORT);
            if (servicePort == null) {
                throw new IllegalArgumentException("\"--e port <port>\" was not specified");
            }
            int port = Integer.parseInt(servicePort);
            startServer(port);
            break;
        case STOP:
            mNotificationManager.cancel(NOTIFICATION_ID);
            mNotificationManager.cancelAll();
            super.onStart();
        }
    }

    private void startServer(int port) {
        AndroidProxy androidProxy = new AndroidProxy(getContext());
        if (androidProxy.startLocal(port) == null) {
            throw new RuntimeException("Failed to start server on port " + port);
        }
        createNotification();
        Log.i("Snippet server started for process " + Process.myPid() + " on port " + port);
    }

    private void createNotification() {
        Notification.Builder builder = new Notification.Builder(getTargetContext());
        builder.setSmallIcon(android.R.drawable.btn_star)
                .setTicker(null)
                .setWhen(System.currentTimeMillis())
                .setContentTitle("Snippet Service");
        mNotification = builder.getNotification();
        mNotification.flags = Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;
        mNotificationManager.notify(NOTIFICATION_ID, mNotification);
    }
}