1# SPDX-License-Identifier: Apache-2.0
2#
3# Copyright (C) 2015, ARM Limited, Google and contributors.
4#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may
6# 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, WITHOUT
13# 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
18import re
19import os
20import logging
21
22from time import sleep
23
24from target_script import TargetScript
25from android import Screen, System
26from android.workload import Workload
27
28
29class CameraPreview(Workload):
30    """
31    Android CameraPreview workload
32    """
33
34    # Package required by this workload
35    package = 'com.google.android.GoogleCamera'
36    action = 'android.intent.action.MAIN'
37
38    def __init__(self, test_env):
39        super(CameraPreview, self).__init__(test_env)
40        self._log = logging.getLogger('CameraPreview')
41        self._log.debug('Workload created')
42
43    def run(self, out_dir, duration_s=30, collect='surfaceflinger, systrace'):
44        """
45        Run a camera preview workload
46
47        :param out_dir: Path to experiment directory where to store results.
48        :type out_dir: str
49
50        :param duration_s: Duration of test
51        :type duration_s: int
52
53        :param collect: Specifies what to collect. Possible values:
54            - 'energy'
55            - 'systrace'
56            - 'ftrace'
57            - any combination of the above
58        :type collect: list(str)
59        """
60        self._log.info("Running CameraPreview for {}s and collecting {}".format(duration_s, collect))
61
62        # Keep track of mandatory parameters
63        self.out_dir = out_dir
64        self.collect = collect
65
66        # Unlock device screen (assume no password required)
67        Screen.unlock(self._target)
68
69        # Set airplane mode
70        System.set_airplane_mode(self._target, on=True)
71
72        # Set min brightness
73        Screen.set_brightness(self._target, auto=False, percent=0)
74
75        # Use the monkey tool to start CameraPreview
76        # This allows to subsequently set the screen orientation to LANDSCAPE
77        # and to reset the frame statistics.
78        System.monkey(self._target, self.package)
79
80        # Force screen in PORTRAIT  mode
81        Screen.set_orientation(self._target, portrait=True)
82
83        sleep(2)
84
85        self.tracingStart()
86
87        sleep(duration_s)
88
89        self.tracingStop()
90
91        # Close the app without clearing the local data to
92        # avoid the dialog to select the account at next start
93        System.force_stop(self._target, self.package, clear=False)
94
95        # Go back to home screen
96        System.home(self._target)
97
98        # Set brightness back to auto
99        Screen.set_brightness(self._target, auto=True)
100
101        # Switch back to screen auto rotation
102        Screen.set_orientation(self._target, auto=True)
103
104        # Switch off airplane mode
105        System.set_airplane_mode(self._target, on=False)
106