1# Copyright 2016 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import os
16import re
17import subprocess
18import sys
19import time
20
21
22def main():
23    """Load charts on device and display."""
24    camera_id = -1
25    scene = None
26    for s in sys.argv[1:]:
27        if s[:6] == 'scene=' and len(s) > 6:
28            scene = s[6:]
29        elif s[:7] == 'screen=' and len(s) > 7:
30            screen_id = s[7:]
31
32    cmd = ('adb -s %s shell am force-stop com.google.android.apps.docs' %
33           screen_id)
34    subprocess.Popen(cmd.split())
35
36    if not scene:
37        print 'Error: need to specify which scene to load'
38        assert False
39
40    if not screen_id:
41        print 'Error: need to specify screen serial'
42        assert False
43
44    remote_scene_file = '/sdcard/Download/%s.pdf' % scene
45    local_scene_file = os.path.join(os.environ['CAMERA_ITS_TOP'], 'tests',
46                                    scene, scene+'.pdf')
47    print 'Loading %s on %s' % (remote_scene_file, screen_id)
48    cmd = 'adb -s %s push %s /mnt%s' % (screen_id, local_scene_file,
49                                        remote_scene_file)
50    subprocess.Popen(cmd.split())
51    time.sleep(1)  # wait-for-device doesn't always seem to work...
52    # The intent require PDF viewing app be installed on device.
53    # Also the first time such app is opened it might request some permission,
54    # so it's  better to grant those permissions before using this script
55    cmd = ("adb -s %s wait-for-device shell am start -d 'file://%s'"
56           " -a android.intent.action.VIEW" % (screen_id,
57                                               remote_scene_file))
58    subprocess.Popen(cmd.split())
59
60if __name__ == '__main__':
61    main()
62