---
API: 2
OpenSesame: 3.1.5a4
Platform: posix
---
set width 1280
set uniform_coordinates no
set title "Runtime GUI for Android"
set synth_backend droid
set subject_parity even
set subject_nr 0
set start experiment
set sound_sample_size -16
set sound_freq 48000
set sound_channels 2
set sound_buf_size 1024
set sampler_backend legacy
set round_decimals 2
set mouse_backend droid
set keyboard_backend droid
set height 800
set fullscreen no
set form_clicks no
set foreground white
set font_underline no
set font_size 28
set font_italic no
set font_family sans
set font_bold no
set experiment_path "/home/sebastiaan/git/OpenSesame/opensesame_resources/android"
set disable_garbage_collection yes
set description "Implements the launcher menu for the Android runtime"
set coordinates relative
set compensation 0
set canvas_backend droid
set bidi no
set background "#2e3436"

define sequence experiment
	set flush_keyboard yes
	set description "Runs a number of items in sequence"
	run inline_script always

define inline_script inline_script
	set description "Executes Python code"
	___run__
	from libopensesame import widgets, debug, misc, metadata
	import os
	try:
		import android
	except:
		android = None
	default_folders = [
			'/sdcard/Experiments/',
			'/mnt/sdcard/Experiments/',
			'/sdcard/',
			'/mnt/sdcard/',
			'examples',
			]
	example_src = 'examples/'
	max_file_list_size = 8
	# According to <https://github.com/keesj/gomo/wiki/AndroidUserland>, the
	# standard location for the external storage is `/mnt/sdcard`, but there are
	# apparently some alternative routes as well and it probably depends on the
	# Android version. The logic here is to try a few default locations and fall
	# back to a '?' if none of the default paths exists.
	for src in default_folders:
		if os.path.isdir(src):
			break
	# Try to load the experiment and logfile path from the settings file
	save_path = os.path.join(src, 'opensesame-settings.txt')
	experiment_folder = src
	logfile_folder = src
	try:
		fd = open(save_path)
		print('opened %s for reading' % save_path)
	except:
		print('failed to open %s for reading' % save_path)
		fd = None
	if fd is not None:
		for line in fd:
			l = line.strip().split(': ')
			if len(l) != 2:
				continue
			if l[0] == 'experiment_folder' and os.path.exists(l[1]):
				experiment_folder = l[1]
			if l[0] == 'logfile_folder' and os.path.exists(l[1]):
				logfile_folder = l[1]
	print('experiment_folder = %s' % experiment_folder)
	print('logfile_folder = %s' % logfile_folder)
	# Create a form to select the experiment folder
	form = widgets.form(exp, cols=[1], rows=[1,4], margins=[32,32,32,32])
	label_title = widgets.label(form,
		text=('Which folder contains your experiments?'
		'<br /><span size=16>Examples will be auto-detected</span>'
		'<br><span size=16>(%s)</span>') % metadata.__version__)
	text_input_src = widgets.text_input(form, text=experiment_folder, var='_src',
		return_accepts=True)
	form.set_widget(label_title, (0,0))
	form.set_widget(text_input_src, (0,1))
	form._exec(focus_widget=text_input_src)
	src = exp._src
	# Detect available experiments
	experiments = []
	if os.path.isdir(src):
		for path in os.listdir(src):
			if path.endswith('.opensesame') or path.endswith('.opensesame.tar.gz') \
				or path.endswith('.osexp'):
				experiments.append(os.path.join(src, path))
	if os.path.isdir(example_src) and example_src != src:
		for path in os.listdir(example_src):
			if path.endswith('.opensesame') or path.endswith('.opensesame.tar.gz') \
				or path.endswith('.osexp'):
				experiments.append(os.path.join(example_src, path))
	experiments = experiments[:max_file_list_size]
	# Create a form	to select the experiment
	form = widgets.form(exp, cols=[1], rows=[1]*max_file_list_size+[1],
		margins=[32,32,32,32])
	i = 1
	path_dict = {}
	if len(experiments) == 0:
		label_notfound = widgets.label(form, text='No experiments found!')
		form.set_widget(label_notfound, (0,0))
	else:
		label_title = widgets.label(form,
			text='Select experiment<br><span size=16>Showing up to %d files</span>' \
			% max_file_list_size)
		form.set_widget(label_title, (0,0))
		for path in experiments:
			# We want to have a pretty name, and show the full path smaller below
			name = os.path.basename(path).split('.')[0].replace('_', ' ').capitalize()
			text = '%s<br /><span size=16>%s</span>' % (name, path)
			path_dict[text] = path
			button_exp = widgets.button(form, text=text)
			form.set_widget(button_exp, (0,i))
			i += 1
	exp._experiment = path_dict[form._exec()]
	# Create a form to select the subject nr
	form = widgets.form(exp, cols=[1], rows=[1,4], margins=[32,32,32,32])
	label_title = widgets.label(form, text='Enter subject number')
	text_input_nr = widgets.text_input(form, text='0', var='_subject_nr',
		return_accepts=True)
	form.set_widget(label_title, (0,0))
	form.set_widget(text_input_nr, (0,1))
	# Loop until a valid subject nr is entered
	while True:
		form._exec(focus_widget=text_input_nr)
		try:
			exp._subject_nr = int(exp._subject_nr)
			break
		except:
			pass
	# Create a form to select the logfile
	form = widgets.form(exp, cols=[1], rows=[1,4], margins=[32,32,32,32])
	label_title = widgets.label(form, text='Enter location for logfile')
	text_input_logfile = widgets.text_input(form,
		text=os.path.join(logfile_folder, 'subject-%d.csv' % exp._subject_nr),
		var='_logfile', return_accepts=True)
	form.set_widget(label_title, (0,0))
	form.set_widget(text_input_logfile, (0,1))
	form._exec(focus_widget=text_input_logfile)
	# Try to save the experiment and logfile folders to a settings file.
	try:
		fd = open(save_path, 'w')
		print('opened %s for writing' % save_path)
	except:
		print('failed to open %s for writing' % save_path)
		fd = None
	if fd is not None:
		fd.write('experiment_folder: %s\n' % os.path.dirname(exp._experiment))
		fd.write('logfile_folder: %s\n' % os.path.dirname(exp._logfile))
	__end__
	set _prepare ""

