/** * Creates a {@link GeofencingEvent} object from the given intent. * * @param intent the intent to extract the geofencing event data from * @return a {@link GeofencingEvent} object or {@code null} if the given intent is {@code null} */ public static GeofencingEvent fromIntent(Intent intent) { if (intent == null) { return null; } GeofencingEvent event = new GeofencingEvent(); event.errorCode = intent.getIntExtra(EXTRA_ERROR_CODE, -1); event.geofenceTransition = intent.getIntExtra(EXTRA_TRANSITION, -1); if (event.geofenceTransition != 1 && event.geofenceTransition != 2 && event.geofenceTransition != 4) event.geofenceTransition = -1; ArrayList<byte[]> parceledGeofences = (ArrayList<byte[]>) intent.getSerializableExtra(EXTRA_GEOFENCE_LIST); if (parceledGeofences != null) { event.triggeringGeofences = new ArrayList<Geofence>(); for (byte[] parceledGeofence : parceledGeofences) { event.triggeringGeofences.add(SafeParcelUtil.fromByteArray(parceledGeofence, ParcelableGeofence.CREATOR)); } } event.triggeringLocation = intent.getParcelableExtra(EXTRA_TRIGGERING_LOCATION); return event; }
/** * Extracts the ActivityRecognitionResult from an Intent. * <p> * This is a utility function which extracts the ActivityRecognitionResult from the extras of * an Intent that was sent from the activity detection service. * * @return an ActivityRecognitionResult, or {@code null} if the intent doesn't contain an * ActivityRecognitionResult. */ public static ActivityRecognitionResult extractResult(Intent intent) { if (intent.hasExtra(EXTRA_ACTIVITY_RESULT_LIST)) { intent.setExtrasClassLoader(ActivityRecognitionResult.class.getClassLoader()); List<ActivityRecognitionResult> list = intent.getParcelableArrayListExtra(EXTRA_ACTIVITY_RESULT_LIST); if (list != null && !list.isEmpty()) return list.get(list.size() - 1); } if (intent.hasExtra(EXTRA_ACTIVITY_RESULT)) { Bundle extras = intent.getExtras(); extras.setClassLoader(ActivityRecognitionResult.class.getClassLoader()); Object res = extras.get(EXTRA_ACTIVITY_RESULT); if (res instanceof ActivityRecognitionResult) return (ActivityRecognitionResult) res; if (res instanceof byte[]) return SafeParcelUtil.fromByteArray((byte[]) res, CREATOR); } return null; }