1#!/usr/bin/env python3
2#
3#   Copyright 2017 - 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.
16import time
17import enum
18
19from acts.controllers.relay_lib.relay import SynchronizeRelays
20from acts.controllers.relay_lib.devices.bluetooth_relay_device import BluetoothRelayDevice
21
22PAIRING_MODE_WAIT_TIME = 5.2
23
24
25class Buttons(enum.Enum):
26    HOME = 'Home'
27    BACK = 'Back'
28    PLAY_PAUSE = 'Play'
29
30
31class FuguRemote(BluetoothRelayDevice):
32    """A Nexus Player (Fugu) Remote.
33
34    Wraps the button presses, as well as the special features like pairing.
35    """
36
37    def __init__(self, config, relay_rig):
38        BluetoothRelayDevice.__init__(self, config, relay_rig)
39        self._ensure_config_contains_relays(button.value for button in Buttons)
40
41    def setup(self):
42        """Sets all relays to their default state (off)."""
43        BluetoothRelayDevice.setup(self)
44        # If the Fugu remote does have a power relay attached, turn it on.
45        power = 'Power'
46        if power in self.relays:
47            self.relays[power].set_nc()
48
49    def clean_up(self):
50        """Sets all relays to their default state (off)."""
51        BluetoothRelayDevice.clean_up(self)
52
53    def enter_pairing_mode(self):
54        """Enters pairing mode. Blocks the thread until pairing mode is set.
55
56        Holds down the 'Home' and 'Back' buttons for a little over 5 seconds.
57        """
58        with SynchronizeRelays():
59            self.hold_down(Buttons.HOME.value)
60            self.hold_down(Buttons.BACK.value)
61
62        time.sleep(PAIRING_MODE_WAIT_TIME)
63
64        with SynchronizeRelays():
65            self.release(Buttons.HOME.value)
66            self.release(Buttons.BACK.value)
67
68    def press_play_pause(self):
69        """Briefly presses the Play/Pause button."""
70        self.press(Buttons.PLAY_PAUSE.value)
71
72    def press_home(self):
73        """Briefly presses the Home button."""
74        self.press(Buttons.HOME.value)
75
76    def press_back(self):
77        """Briefly presses the Back button."""
78        self.press(Buttons.BACK.value)
79