1#!/usr/bin/env python3 2# 3# Copyright 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. 16import enum 17import logging 18import sys 19 20from acts.controllers.android_device import AndroidDevice 21from acts.libs import version_selector 22 23 24class AndroidApi: 25 OLDEST = 0 26 MINIMUM = 0 27 L = 21 28 L_MR1 = 22 29 M = 23 30 N = 24 31 N_MR1 = 25 32 O = 26 33 O_MR1 = 27 34 P = 28 35 LATEST = sys.maxsize 36 MAX = sys.maxsize 37 38 39def android_api(min_api=AndroidApi.OLDEST, 40 max_api=AndroidApi.LATEST): 41 """Decorates a function to only be called for the given API range. 42 43 Only gets called if the AndroidDevice in the args is within the specified 44 API range. Otherwise, a different function may be called instead. If the 45 API level is out of range, and no other function handles that API level, an 46 error is raise instead. 47 48 Note: In Python3.5 and below, the order of kwargs is not preserved. If your 49 function contains multiple AndroidDevices within the kwargs, and no 50 AndroidDevices within args, you are NOT guaranteed the first 51 AndroidDevice is the same one chosen each time the function runs. Due 52 to this, we do not check for AndroidDevices in kwargs. 53 54 Args: 55 min_api: The minimum API level. Can be an int or an AndroidApi value. 56 max_api: The maximum API level. Can be an int or an AndroidApi value. 57 """ 58 def get_api_level(*args, **_): 59 for arg in args: 60 if isinstance(arg, AndroidDevice): 61 return arg.sdk_api_level() 62 logging.getLogger().error('An AndroidDevice was not found in the given ' 63 'arguments.') 64 return None 65 66 return version_selector.set_version(get_api_level, min_api, max_api) 67