def __init__(self): super(BarometerSensorListener, self).__init__() service = activity.getSystemService(Context.SENSOR_SERVICE) self.SensorManager = cast('android.hardware.SensorManager', service) self.sensor = self.SensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE) self.value = None
def _get_audiomanager(self): if not hasattr(self, 'audiomanager'): if platform == 'android': Context = autoclass('android.content.Context') self.audiomanager = activity.getSystemService( Context.AUDIO_SERVICE) return self.audiomanager
def __init__(self): super(MFUSensorListener, self).__init__() service = activity.getSystemService(Context.SENSOR_SERVICE) self.SensorManager = cast('android.hardware.SensorManager', service) self.sensor = self.SensorManager.getDefaultSensor( Sensor.TYPE_MAGNETIC_FIELD_UNCALIBRATED) self.values = [None, None, None, None, None, None]
def __init__(self): super(GyroscopeSensorListener, self).__init__() self.SensorManager = cast('android.hardware.SensorManager', activity.getSystemService(Context.SENSOR_SERVICE)) self.sensor = self.SensorManager.getDefaultSensor( Sensor.TYPE_GYROSCOPE) self.values = [0, 0, 0]
def __init__(self): super(MagneticFieldSensorListener, self).__init__() self.SensorManager = cast('android.hardware.SensorManager', activity.getSystemService(Context.SENSOR_SERVICE)) self.sensor = self.SensorManager.getDefaultSensor( Sensor.TYPE_MAGNETIC_FIELD) self.values = [0, 0, 0]
def __init__(self): super(RelativeHumiditySensorListener, self).__init__() service = activity.getSystemService(Context.SENSOR_SERVICE) self.SensorManager = cast('android.hardware.SensorManager', service) self.sensor = self.SensorManager.getDefaultSensor( Sensor.TYPE_RELATIVE_HUMIDITY) self.value = None
def __init__(self): super(AmbientTemperatureSensorListener, self).__init__() service = activity.getSystemService(Context.SENSOR_SERVICE) self.SensorManager = cast('android.hardware.SensorManager', service) self.sensor = self.SensorManager.getDefaultSensor( Sensor.TYPE_AMBIENT_TEMPERATURE) self.value = None
def __init__(self): super(AccelerometerSensorListener, self).__init__() self.SensorManager = cast('android.hardware.SensorManager', activity.getSystemService(Context.SENSOR_SERVICE)) self.sensor = self.SensorManager.getDefaultSensor( Sensor.TYPE_ACCELEROMETER) self.values = [None, None, None]
def __init__(self): super(GyroUncalibratedSensorListener, self).__init__() service = activity.getSystemService(Context.SENSOR_SERVICE) self.SensorManager = cast('android.hardware.SensorManager', service) self.sensor = self.SensorManager.getDefaultSensor( Sensor.TYPE_GYROSCOPE_UNCALIBRATED) self.values = [None, None, None, None, None, None]
def __init__(self): super(GravitySensorListener, self).__init__() service = activity.getSystemService(Context.SENSOR_SERVICE) self.SensorManager = cast('android.hardware.SensorManager', service) self.sensor = self.SensorManager.getDefaultSensor( Sensor.TYPE_GRAVITY ) self.values = [None, None, None]
def notify(title='', message='', id=0, timeout=10, priority=None, defaults=False): icon = getattr(Drawable, 'icon') noti = NotificationBuilder(activity) if defaults: noti.setDefaults(autoclass('android.app.Notification').DEFAULT_ALL) noti.setContentTitle(String(title.encode('utf-8'))) noti.setContentText(String(message.encode('utf-8'))) noti.setSmallIcon(icon) noti.setAutoCancel(True) if priority is not None: noti.setPriority(priority) intent = activity.getPackageManager().getLaunchIntentForPackage( 'org.jtc.planilla') noti.setContentIntent(PendingIntent.getActivity(activity, 0, intent, 0)) activity.getSystemService(Context.NOTIFICATION_SERVICE).notify( id, noti.build())
def schedule_alarms(alarmas): Logger.debug("%s: schedule_alarms %s" % (APP, datetime.now())) from jnius import autoclass from android.broadcast import BroadcastReceiver SystemClock = autoclass('android.os.SystemClock') AlarmManager = autoclass('android.app.AlarmManager') global broadcast_receiver broadcast_receiver = BroadcastReceiver( process_broadcast, ['org.jtc.planilla.SERVICEALARM', Intent.ACTION_USER_PRESENT, 'org.jtc.planilla.APP_AWAKE']) broadcast_receiver.start() am = activity.getSystemService(Context.ALARM_SERVICE) # Cancelar todas las posibles alarmas que hubiera de un servicio anterior intent = Intent(String('org.jtc.planilla.SERVICEALARM')) for i in range(20): # Suponemos que no hay más de 20 alarmas! pi = PendingIntent.getBroadcast(activity, i, intent, 0) am.cancel(pi) # Fijar las nuevas alarmas i = 0 now = datetime.now() for id, alarma in alarmas.iteritems(): intent = Intent(String('org.jtc.planilla.SERVICEALARM')).putExtra( "id", id) pi = PendingIntent.getBroadcast( activity, i, intent, PendingIntent.FLAG_UPDATE_CURRENT) ms = (alarma['hora']-now).seconds * 1000 Logger.debug("%s: Hora: %s - En %s" % ( APP, alarma['hora'], tdformat(alarma['hora']-now))) am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+ms, pi) i += 1
from jnius import autoclass from plyer.facades import IrBlaster from plyer.platforms.android import activity, SDK_INT, ANDROID_VERSION if SDK_INT >= 19: Context = autoclass('android.content.Context') ir_manager = activity.getSystemService(Context.CONSUMER_IR_SERVICE) else: ir_manager = None class AndroidIrBlaster(IrBlaster): def _exists(self): if ir_manager and ir_manager.hasIrEmitter(): return True return False @property def multiply_pulse(self): '''Android 4.4.3+ uses microseconds instead of period counts ''' return not (SDK_INT == 19 and int(str(ANDROID_VERSION.RELEASE).rsplit('.', 1)[-1]) < 3) def _get_frequencies(self): if not ir_manager: return None if hasattr(self, '_frequencies'): return self._frequencies
def _get_vibrator(self): if not hasattr(self, 'vibrator') and platform == 'android': Context = autoclass('android.content.Context') self.vibrator = activity.getSystemService(Context.VIBRATOR_SERVICE) return self.vibrator
def _get_notification_service(self): if not self._ns: self._ns = activity.getSystemService(CONTEXT.NOTIFICATION_SERVICE) return self._ns
if __name__ == '__main__': # Por defecto se arranca foreground. Lo dejamos para que se no muera el # servicio activity.stopForeground(False) Logger.info("%s: service.__main__ %s" % (APP, datetime.now())) arg = loads(os.getenv('PYTHON_SERVICE_ARGUMENT')) Logger.debug("%s: PYTHON_SERVICE_ARGUMENT %s" % (APP, pformat(arg))) pasadas = arg['pasadas'] alarmas = arg['alarmas'] Config.set('kivy', 'log_level', arg['log_level']) Logger.debug("%s: Adquiriendo el PowerManager %s" % (APP, datetime.now())) pm = activity.getSystemService(Context.POWER_SERVICE) # No entiendo por qué no funciona con las tres primeras opciones # El uso de FULL_WAKE_LOCK y SCREEN BRIGHT es deprecated # pero si no no me rula. Logger.debug("%s: Adquiriendo el wake lock %s" % (APP, datetime.now())) wl = pm.newWakeLock( # PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE | PowerManager.FULL_WAKE_LOCK | PowerManager.SCREEN_BRIGHT_WAKE_LOCK | 0, "My tag") schedule_alarms(alarmas) if len(alarmas):
def _get_notification_service(self): if not hasattr('_ns'): self._ns = activity.getSystemService( PythonActivity.NOTIFICATION_SERVICE) return self._ns
def _get_uid(self): manager = cast('android.telephony.TelephonyManager', activity.getSystemService(Context.TELEPHONY_SERVICE)) return manager.getDeviceId()
from jnius import autoclass from plyer.platforms.android import activity String = autoclass('java.lang.String') Context = autoclass('android.content.Context') Intent = autoclass('android.content.Intent') PendingIntent = autoclass('android.app.PendingIntent') System = autoclass('java.lang.System') SystemClock = autoclass('android.os.SystemClock') AlarmManager = autoclass('android.app.AlarmManager') am = activity.getSystemService(Context.ALARM_SERVICE) intent = Intent(String('org.jtc.planilla.SERVICEALARM')).putExtra("id", 0) pi = PendingIntent.getBroadcast( activity, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT) ms = 10 # ms anadidos al elapsed real time del movil ert = SystemClock.elapsedRealtime() am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, ert + ms, pi)
'''Implementation Vibrator for Android.''' from jnius import autoclass from plyer.facades import Vibrator from plyer.platforms.android import activity from plyer.platforms.android import SDK_INT Context = autoclass('android.content.Context') vibrator = activity.getSystemService(Context.VIBRATOR_SERVICE) class AndroidVibrator(Vibrator): '''Android Vibrator class. Supported features: * vibrate for some period of time. * vibrate from given pattern. * cancel vibration. * check whether Vibrator exists. ''' def _vibrate(self, time=None, **kwargs): if vibrator: vibrator.vibrate(int(1000 * time)) def _pattern(self, pattern=None, repeat=None, **kwargs): pattern = [int(1000 * time) for time in pattern] if vibrator: vibrator.vibrate(pattern, repeat)
def _configure(self): if not hasattr(self, '_location_manager'): self._location_manager = activity.getSystemService( Context.LOCATION_SERVICE ) self._location_listener = _LocationListener(self)
def _exists(self, **kwargs): if SDK_INT >= 11: return vibrator.hasVibrator() elif activity.getSystemService(Context.VIBRATOR_SERVICE) is None: raise NotImplementedError() return True
def _configure(self): if not hasattr(self, '_location_manager'): self._location_manager = activity.getSystemService( Context.LOCATION_SERVICE) self._location_listener = _LocationListener(self)
def _get_notification_service(self): if not hasattr(self, "_ns"): self._ns = activity.getSystemService(Context.NOTIFICATION_SERVICE) return self._ns