1 
2 /*
3  * Copyright (C) 2018 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License
16  */
17 
18 package com.android.cts.verifier.camera.performance;
19 
20 import android.app.Instrumentation;
21 import android.os.Bundle;
22 import android.content.Context;
23 
24 import android.util.Log;
25 
26 import com.android.compatibility.common.util.ReportLog;
27 import com.android.compatibility.common.util.ReportLog.Metric;
28 
29 import java.util.Set;
30 
31 // A custom Instrumentation intended to be used only for
32 // collecting camera performance test result data.
33 // Instantiation is done explicitly by the application and not
34 // automatically through the Android manifest. Clients are
35 // also responsible for the instrumentation registration.
36 public class CameraTestInstrumentation extends Instrumentation {
37     private static final String TAG = "CameraTestInstrumentation";
38 
39     private MetricListener mMetricListener;
40     private Context mTargetContext;
41 
42     public interface MetricListener {
onResultMetric(Metric metric)43         public void onResultMetric(Metric metric);
44     }
45 
initialize(Context targetContext, MetricListener listener)46     public void initialize(Context targetContext, MetricListener listener) {
47         mTargetContext = targetContext;
48         mMetricListener = listener;
49     }
50 
release()51     public void release() {
52         mTargetContext = null;
53         mMetricListener = null;
54     }
55 
56     @Override
getTargetContext()57     public Context getTargetContext() {
58         return mTargetContext;
59     }
60 
61     @Override
sendStatus(int resultCode, Bundle results)62     public void sendStatus(int resultCode, Bundle results) {
63         super.sendStatus(resultCode, results);
64 
65         if (results == null) {
66             return;
67         }
68 
69         Set<String> keys = results.keySet();
70         if (keys.isEmpty()) {
71             Log.v(TAG,"Empty keys");
72             return;
73         }
74 
75         for (String key : keys) {
76             ReportLog report;
77             try {
78                 report = ReportLog.parse(results.getString(key));
79             } catch (Exception e) {
80                 Log.e(TAG, "Failed parsing report log!");
81                 return;
82             }
83 
84             Metric metric = report.getSummary();
85             if (metric == null) {
86                 Log.v(TAG, "Empty metric");
87                 return;
88             }
89             String message = metric.getMessage();
90             double[] values = metric.getValues();
91             String source = metric.getSource();
92             if ((message == null) || (message.isEmpty()) || (values == null) ||
93                     (values.length == 0) || (source == null) || (source.isEmpty())) {
94                 Log.v(TAG, "Metric has no valid entries");
95                 return;
96             }
97 
98             if (mMetricListener != null) {
99                 mMetricListener.onResultMetric(metric);
100             }
101         }
102     }
103 }
104 
105