1#!/usr/bin/env python3
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"""
18battery_simulator.py is an interactive shell that modifies a device's battery
19status.
20
21$ battery_simulator.py
22...> 60   # Sets battery level to 60%.
23...> on   # Plug in charger.
24...> 70   # Sets battery level to 70%.
25...> off  # Plug out charger.
26...> q    #quit
27
28"""
29
30import atexit
31import os
32import re
33import sys
34
35def echo_run(command):
36    print("\x1b[36m[Running: %s]\x1b[0m" % command)
37    os.system(command)
38
39def battery_unplug():
40    echo_run("adb shell dumpsys battery unplug")
41
42@atexit.register
43def battery_reset():
44    echo_run("adb shell dumpsys battery reset")
45
46def interactive_start():
47    while True:
48        try:
49            val = input("Type NUMBER, 'on', 'off' or 'q' > ").lower()
50        except EOFError:
51            print()
52            break
53        if val == 'q':
54            break
55        if val == "on":
56            echo_run("adb shell dumpsys battery set ac 1")
57            continue
58        if val == "off":
59            echo_run("adb shell dumpsys battery set ac 0")
60            continue
61        if re.match("\d+", val):
62            echo_run("adb shell dumpsys battery set level %s" % val)
63            continue
64        print("Unknown command.")
65
66
67def main():
68    battery_unplug()
69    interactive_start()
70
71if __name__ == '__main__':
72    main()
73