| 1 | from jnius import autoclass |
| 2 | |
| 3 | from plyer.facades import IrBlaster |
| 4 | from plyer.platforms.android import activity, SDK_INT, ANDROID_VERSION |
| 5 | |
| 6 | if SDK_INT >= 19: |
| 7 | Context = autoclass('android.content.Context') |
| 8 | ir_manager = activity.getSystemService(Context.CONSUMER_IR_SERVICE) |
| 9 | else: |
| 10 | ir_manager = None |
| 11 | |
| 12 | |
| 13 | class AndroidIrBlaster(IrBlaster): |
| 14 | def _exists(self): |
| 15 | if ir_manager and ir_manager.hasIrEmitter(): |
| 16 | return True |
| 17 | return False |
| 18 | |
| 19 | @property |
| 20 | def multiply_pulse(self): |
| 21 | '''Android 4.4.3+ uses microseconds instead of period counts |
| 22 | ''' |
| 23 | return not (SDK_INT == 19 |
| 24 | and int(str(ANDROID_VERSION.RELEASE).rsplit('.', 1)[-1]) |
| 25 | < 3) |
| 26 | |
| 27 | def _get_frequencies(self): |
| 28 | if not ir_manager: |
| 29 | return None |
| 30 | |
| 31 | if hasattr(self, '_frequencies'): |
| 32 | return self._frequencies |
| 33 | |
| 34 | ir_frequencies = ir_manager.getCarrierFrequencies() |
| 35 | if not ir_frequencies: |
| 36 | return [] |
| 37 | |
| 38 | frequencies = [] |
| 39 | for freqrange in ir_frequencies: |
| 40 | freq = (freqrange.getMinFrequency(), freqrange.getMaxFrequency()) |
| 41 | frequencies.append(freq) |
| 42 | |
| 43 | self._frequencies = frequencies |
| 44 | return frequencies |
| 45 | |
| 46 | def _transmit(self, frequency, pattern, mode): |
| 47 | if self.multiply_pulse and mode == 'period': |
| 48 | pattern = self.periods_to_microseconds(frequency, pattern) |
| 49 | elif not self.multiply_pulse and mode == 'microseconds': |
| 50 | pattern = self.microseconds_to_periods(frequency, pattern) |
| 51 | ir_manager.transmit(frequency, pattern) |
| 52 | |
| 53 | |
| 54 | def instance(): |
| 55 | return AndroidIrBlaster() |