#
# Copyright 2014 Shahriyar Amini
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

__author__ = 'shahriyar'
__copyright__ = 'Copyright 2014, Shahriyar Amini'

import subprocess
import re
import AndroidAdapter.utils as utils

"""
def focusedActivity(serial_id): 

	try:
		output =  subprocess.check_output(["adb", "-s", serial_id, "shell", "dumpsys", "activity"], stderr = subprocess.PIPE)
	except subprocess.CalledProcessError:
		return None

	outputSplit = output.split('\n')

	# sample 
	#mFocusedActivity: HistoryRecord{408238d8 com.metago.astro/.FileManagerActivity}
	for line in outputSplit:
		if (line.find("mFocusedActivity") >= 0):
			lineSplit = line.split(' ')
			retval = lineSplit[len(lineSplit)-1].rstrip('}\r\n')
			return retval

	return None
"""

# returns the device time in seconds since epoch
def deviceTime(serial_id):
	if not serial_id:
		return None
	
	commandArray = ["adb", "-s", serial_id, "shell", "date", '+"%s"']
	output = None
	
	try:
		output = subprocess.check_output(commandArray, stderr = subprocess.PIPE)
	except AttributeError:
		output = subprocess.Popen(commandArray, stdout = subprocess.PIPE).communicate()[0]
	except subprocess.CalledProcessError:
		pass
	
	if output is None:
		return None
	
	return float(output)

def focusedActivity(serial_id):
	tmp = recentActivities(serial_id)
	
	if not tmp: