Skip to content

Commit

Permalink
Merge pull request #2127 from brussee/is-android
Browse files Browse the repository at this point in the history
osutils.py rewrite is_android() using get_android_api_version()
  • Loading branch information
whirm committed May 3, 2016
2 parents 5830961 + 08c1231 commit 8f3fc57
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 41 deletions.
13 changes: 6 additions & 7 deletions Tribler/Core/SessionConfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,14 @@ def __init__(self, sessconfig=None):
if not sessconfig:
return

# TODO(emilon): This is to work around the case where windows has
# non-ASCI chars on %PATH% contents. Should be removed if we migrate to
# python 3.
if sys.platform == 'win32':
# TODO(emilon): This is to work around the case where windows has
# non-ASCI chars on %PATH% contents. Should be removed if we migrate to
# python 3.
from Tribler.Main.hacks import get_environment_variable
path_env = get_environment_variable(u"PATH")
elif is_android():
path_env = os.environ["PYTHONPATH"]
else:
path_env = os.environ["PATH"]

Expand All @@ -70,7 +72,7 @@ def __init__(self, sessconfig=None):
ffmpegname = u"ffmpeg.exe"
elif sys.platform == 'darwin':
ffmpegname = u"ffmpeg"
elif find_executable("avconv"):
elif find_executable("avconv", path_env):
ffmpegname = u"avconv"
else:
ffmpegname = u"ffmpeg"
Expand All @@ -80,9 +82,6 @@ def __init__(self, sessconfig=None):
if ffmpegpath is None:
if sys.platform == 'darwin':
self.sessconfig.set(u'general', u'videoanalyserpath', u"vlc/ffmpeg")
elif is_android(strict=True):
self.sessconfig.set(u'general', u'videoanalyserpath', os.path.join(
os.environ['ANDROID_PRIVATE'], 'ffmpeg'))
else:
self.sessconfig.set(u'general', u'videoanalyserpath', ffmpegname)
else:
Expand Down
60 changes: 35 additions & 25 deletions Tribler/Core/osutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,27 @@

logger = logging.getLogger(__name__)


def get_android_api_version():
"""
:return: integer Runtime API version or None.
"""
try:
from android import api_version
return api_version
except ImportError:
return None


def is_android():
"""
This functions checks whether Tribler is running on Android or not, using the Android runtime API version.
:return: boolean True if running on Android. False otherwise.
"""
return get_android_api_version() is not None


if sys.platform == "win32":
try:
from win32com.shell import shell, shellcon
Expand Down Expand Up @@ -119,6 +140,20 @@ def get_desktop_dir():
home = get_home_dir()
return os.path.join(home, u"Desktop")

elif is_android():

def get_home_dir():
return os.path.realpath(os.environ['ANDROID_PRIVATE'])

def get_appstate_dir():
return os.path.join(get_home_dir(), '.Tribler')

def get_picture_dir():
return os.path.join(get_desktop_dir(), 'DCIM')

def get_desktop_dir():
return os.path.realpath(os.environ['EXTERNAL_STORAGE'])

else:
# linux or darwin (mac)
def get_home_dir():
Expand Down Expand Up @@ -224,28 +259,3 @@ def startfile(filepath):
subprocess.call(('xdg-open', filepath))
elif hasattr(os, "startfile"):
os.startfile(filepath)


def is_android(strict=False):
"""
This functions checks whether Tribler is running on Android or not, using the ANDROID_HOST environment variable.
When Tribler is launched on Android, this variable is set to "ANDROID-99" where 99 is the current SDK version.
:param strict: Check if ANDROID_HOST actually starts with "ANDROID". This can be useful for code that must
absolutely only run on Android, and not on a computer testing the Android specific code.
:return: Whether this is Android or not.
"""

# This is not an Android device at all
if not 'ANDROID_HOST' in os.environ:
return False

# No strict mode: always return true when ANDROID_HOST is defined
if not strict:
return True
# Strict mode: actually check whether the variable starts with "ANDROID"
elif os.environ['ANDROID_HOST'].startswith("ANDROID"):
return True
# Strict mode, but the variable doesn't start with "ANDROID"
else:
return False
24 changes: 15 additions & 9 deletions Tribler/Test/Core/test_osutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
elif os.path.exists('LICENSE.txt'):
BASE_DIR = '.'

from Tribler.Core.osutils import (fix_filebasename, is_android, get_home_dir, get_appstate_dir, get_picture_dir,
get_desktop_dir)
from Tribler.Core.osutils import (fix_filebasename, get_android_api_version, is_android,
get_home_dir, get_appstate_dir, get_picture_dir, get_desktop_dir)
from Tribler.Test.test_as_server import BaseTestCase


Expand Down Expand Up @@ -61,14 +61,20 @@ def test_fix_filebasename(self):
fixedname = fix_filebasename(name)
assert fixedname == name_table[name], (fixedname, name_table[name])

def test_get_android_api_version(self):
if sys.platform.startswith('linux') and 'ANDROID_PRIVATE' in os.environ:
version = get_android_api_version()
self.assertIsInstance(version, int)
self.assertGreater(version, 0)
self.assertLess(version, 50)
else:
self.assertIsNone(get_android_api_version())

def test_is_android(self):
# act like the computer is an Android
self.assertFalse(is_android())
os.environ["ANDROID_HOST"] = "ANDROID_HOST"
self.assertTrue(is_android())
self.assertTrue(is_android(strict=True))
os.environ["ANDROID_HOST"] = "HOST"
self.assertFalse(is_android(strict=True))
if sys.platform.startswith('linux') and 'ANDROID_PRIVATE' in os.environ:
self.assertTrue(is_android())
else:
self.assertFalse(is_android())

def test_home_dir(self):
home_dir = get_home_dir()
Expand Down

0 comments on commit 8f3fc57

Please sign in to comment.