Skip to content

Instantly share code, notes, and snippets.

@mjohnsullivan
mjohnsullivan / TimerActivity.java
Created July 9, 2015 13:15
Example of how to create a long running timer service that survives activity destruction by moving to the foreground, and back to the background when a new activity bind to it.
//
// Copyright 2015 Google Inc. All Rights Reserved.
//
// 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
@mjohnsullivan
mjohnsullivan / WifiReceiver.java
Created July 8, 2015 09:02
Broadcast receiver to detect when wifi connects on an Android device, displaying the SSID name and MAC address in a notfication
//
// Copyright 2015 Google Inc. All Rights Reserved.
//
// 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
@mjohnsullivan
mjohnsullivan / http_server.rs
Last active May 5, 2025 15:14
Simple HTTP server example for Rust
// Updated example from http://rosettacode.org/wiki/Hello_world/Web_server#Rust
// to work with Rust 1.0 beta
use std::net::{TcpStream, TcpListener};
use std::io::{Read, Write};
use std::thread;
fn handle_read(mut stream: &TcpStream) {
let mut buf = [0u8 ;4096];
@mjohnsullivan
mjohnsullivan / MainActivity.java
Last active March 1, 2024 01:28
Android Wear activity that reads and displays sensor data from the device
package com.example.wear;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.wearable.view.WatchViewStub;
import android.util.Log;
@mjohnsullivan
mjohnsullivan / download.py
Last active December 17, 2022 10:05
Python HTTP download with resume and optional MD5 hash checking
import os.path
import shutil
import hashlib
import logging
# Support both Python 2 and 3 urllib2 importing
try:
from urllib.request import urlopen, Request
except ImportError:
from urllib2 import urlopen, Request
@mjohnsullivan
mjohnsullivan / motion3.js
Created May 13, 2013 08:21
AxisCam motion detection with new Node streams Transform without prototypes
/**
* Parses out motion data from the Axis camera motion stream
* Implemented with the new Node streams
*/
var stream = require('stream'),
util = require('util')
var MotionLevelStream = function() {
stream.Transform.call(this, {objectMode: true})
@mjohnsullivan
mjohnsullivan / motion2.js
Created May 13, 2013 08:21
AxisCam motion detection with new Node streams Transform and prototypes
/**
* Parses out motion data from the Axis camera motion stream
* Implemented with the new Node streams using prototypes
*/
var Transform = require('stream').Transform
var MotionLevelStream = function() {
Transform.call(this, {objectMode: true})
}
@mjohnsullivan
mjohnsullivan / motion1.js
Last active December 17, 2015 06:39
AxisCam motion detection with classic Node streams
/**
* Parses out motion data from the Axis camera motion stream
*/
var util = require('util'),
Stream = require('stream').Stream
var MotionLevelStream = function() {
this.readable = true
this.writable = true
}
@mjohnsullivan
mjohnsullivan / scuttlebutt_muxdemux_test.js
Created March 7, 2013 17:16
My naive attempt at multiplexing/demultiplexing Scuttlebutt Model streams.
var MuxDemux = require('mux-demux');
var net = require('net');
var Model = require('scuttlebutt/model');
var aModel = new Model
var aModelStream = aModel.createStream()
var bModel = new Model
var bModelStream = bModel.createStream()
// Server
@mjohnsullivan
mjohnsullivan / mxf.py
Created August 17, 2012 10:53
Determines the essence type of an mxf file for DCI digital cinema content: JPEG2000, MPEG or PCM
import sys
import struct
# MXF Keys
MXF_KEYS = {
'\x06\x0e\x2b\x34\x02\x53\x01\x01\x0d\x01\x01\x01\x01\x01\x51\x00' : 'MPEG2VIDEODESCRIPTOR',
'\x06\x0e\x2b\x34\x02\x53\x01\x01\x0d\x01\x01\x01\x01\x01\x5a\x00' : 'JPEG2000PICTURESUBDESCRIPTOR',
'\x06\x0e\x2b\x34\x02\x53\x01\x01\x0d\x01\x01\x01\x01\x01\x48\x00' : 'WAVEAUDIODESCRIPTOR',
'\x06\x0e\x2b\x34\x02\x53\x01\x01\x0d\x01\x01\x01\x01\x01\x37\x00' : 'SOURCEPACKAGE',
'\x06\x0e\x2b\x34\x01\x02\x01\x01\x0d\x01\x03\x01\x15\x01\x05\x00' : 'MPEG2ESSENCE',