Java tutorial
/** * Funf: Open Sensing Framework * Copyright (C) 2010-2011 Nadav Aharony, Wei Pan, Alex Pentland. * Acknowledgments: Alan Gardner * Contact: nadav@media.mit.edu * * This file is part of Funf. * * Funf is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation, either version 3 of * the License, or (at your option) any later version. * * Funf is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with Funf. If not, see <http://www.gnu.org/licenses/>. */ package edu.mit.media.funf.wifiscanner; import static edu.mit.media.funf.AsyncSharedPrefs.async; import java.io.IOException; import java.io.InputStream; import java.nio.charset.Charset; import java.util.ArrayList; import org.json.JSONException; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.util.Log; import edu.mit.media.funf.IOUtils; import edu.mit.media.funf.Utils; import edu.mit.media.funf.configured.ConfiguredPipeline; import edu.mit.media.funf.configured.FunfConfig; import edu.mit.media.funf.probe.Probe; import edu.mit.media.funf.storage.BundleSerializer; import android.provider.Settings.Secure; public class MainPipeline extends ConfiguredPipeline { public static final String TAG = "FunfBGCollector"; public static final String MAIN_CONFIG = "main_config"; public static final String START_DATE_KEY = "START_DATE"; public static final String ACTION_RUN_ONCE = "RUN_ONCE"; public static final String RUN_ONCE_PROBE_NAME = "PROBE_NAME"; public static final String SCAN_COUNT_KEY = "SCAN_COUNT"; @Override protected void onHandleIntent(Intent intent) { if (ACTION_RUN_ONCE.equals(intent.getAction())) { String probeName = intent.getStringExtra(RUN_ONCE_PROBE_NAME); runProbeOnceNow(probeName); } else { String action = intent.getAction(); if (super.ACTION_UPLOAD_DATA.equals(action)) { //disable real-time upload if need be } super.onHandleIntent(intent); } } @Override public BundleSerializer getBundleSerializer() { return new BundleToJson(); } public static class BundleToJson implements BundleSerializer { public String serialize(Bundle bundle) { return JsonUtils.getGson().toJson(Utils.getValues(bundle)); } } @Override public void onDataReceived(Bundle data) { String android_id = Secure.getString(getBaseContext().getContentResolver(), Secure.ANDROID_ID); data.putString("Device_ID", android_id); super.onDataReceived(data); if (MainActivity.real_time) { data.putString("WoTK_ID", "coffeejack"); //add WotKit info here String dataJson = getBundleSerializer().serialize(data); new RequestTask().execute(MainActivity.REAL_TIME_UPLOAD_URL, dataJson); } incrementCount(); } @Override public void onStatusReceived(Probe.Status status) { super.onStatusReceived(status); // Fill this in with extra behaviors on status received //Log.i("Debug","Status Received"); } @Override public void onDetailsReceived(Probe.Details details) { super.onDetailsReceived(details); // Fill this in with extra behaviors on details received Log.i("Debug", "Detail Received"); } public static boolean isEnabled(Context context) { return getSystemPrefs(context).getBoolean(ENABLED_KEY, true); } @Override public SharedPreferences getSystemPrefs() { return getSystemPrefs(this); } public static SharedPreferences getSystemPrefs(Context context) { return async(context.getSharedPreferences(MainPipeline.class.getName() + "_system", MODE_PRIVATE)); } @Override public FunfConfig getConfig() { return getMainConfig(this); } /** * Easy access to Funf config. * As long as this service is running, changes will be automatically picked up. * @param context * @return */ public static FunfConfig getMainConfig(Context context) { FunfConfig config = getConfig(context, MAIN_CONFIG); if (config.getName() == null) { String jsonString = getStringFromAsset(context, "default_config.json"); if (jsonString == null) { Log.e(TAG, "Error loading default config. Using blank config."); jsonString = "{}"; } try { config.edit().setAll(jsonString).commit(); } catch (JSONException e) { Log.e(TAG, "Error parsing default config", e); } } return config; } public static String getStringFromAsset(Context context, String filename) { InputStream is = null; try { is = context.getAssets().open(filename); return IOUtils.inputStreamToString(is, Charset.defaultCharset().name()); } catch (IOException e) { Log.e(TAG, "Unable to read asset to string", e); return null; } finally { if (is != null) { try { is.close(); } catch (IOException e) { Log.e(TAG, "Unable to close asset input stream", e); } } } } public void runProbeOnceNow(final String probeName) { FunfConfig config = getMainConfig(this); ArrayList<Bundle> updatedRequests = new ArrayList<Bundle>(); Bundle[] existingRequests = config.getDataRequests(probeName); if (existingRequests != null) { for (Bundle existingRequest : existingRequests) { updatedRequests.add(existingRequest); } } Bundle oneTimeRequest = new Bundle(); oneTimeRequest.putLong(Probe.Parameter.Builtin.PERIOD.name, 0L); updatedRequests.add(oneTimeRequest); Intent request = new Intent(Probe.ACTION_REQUEST); request.setClassName(this, probeName); request.putExtra(Probe.CALLBACK_KEY, getCallback()); request.putExtra(Probe.REQUESTS_KEY, updatedRequests); startService(request); } // @Override // public void uploadData(){ // Log.i("Debug","upload override!"); // super.uploadData(); // } //ADDED AS PER TUTORIAL public static long getScanCount(Context context) { return getSystemPrefs(context).getLong(SCAN_COUNT_KEY, 0L); } private void incrementCount() { boolean success = false; while (!success) { SharedPreferences.Editor editor = getSystemPrefs().edit(); editor.putLong(SCAN_COUNT_KEY, getScanCount(this) + 1L); success = editor.commit(); } } }