1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package android.device.collectors.util;
17 
18 import android.app.Instrumentation;
19 import android.os.Bundle;
20 
21 import java.io.File;
22 
23 /**
24  * Utility to send information to the instrumentation. This ensure that the format used to send
25  * the information is understood by the infrastructure side.
26  */
27 public class SendToInstrumentation {
28 
29     /**
30      * Metrics will be reported under the "status in progress" for test cases to be associated with
31      * the running use cases.
32      */
33     public static final int INST_STATUS_IN_PROGRESS = 2;
34 
35     /**
36      * Send a file to be logged in the instrumentation results with an expected format that the
37      * infrastructure can understand.
38      *
39      * @param instru the current {@link Instrumentation}.
40      * @param key The key under which to report the file.
41      * @param file The file to be logged.
42      */
sendFile(Instrumentation instru, String key, File file)43     public static void sendFile(Instrumentation instru, String key, File file) {
44         // TODO: include the file type in our output format for the host to read it.
45         Bundle b = new Bundle();
46         b.putString(key, file.getAbsolutePath());
47         sendStatus(INST_STATUS_IN_PROGRESS, instru, b);
48     }
49 
50     /**
51      * Send a bundle of information to the instrumentation results.
52      *
53      * @param instru the current {@link Instrumentation}.
54      * @param bundle the {@link Bundle} to be sent.
55      */
sendBundle(Instrumentation instru, Bundle bundle)56     public static void sendBundle(Instrumentation instru, Bundle bundle) {
57         sendStatus(INST_STATUS_IN_PROGRESS, instru, bundle);
58     }
59 
60     /**
61      * Convenience method for {@link Instrumentation#sendStatus(int, Bundle)}.
62      */
sendStatus(int code, Instrumentation instrumentation, Bundle bundle)63     private static void sendStatus(int code, Instrumentation instrumentation, Bundle bundle) {
64         instrumentation.sendStatus(code, bundle);
65     }
66 }
67