// Path: AndroidApp/src/ca/psiphon/ploggy/Utils.java
// public static class ApplicationError extends Exception {
//     private static final long serialVersionUID = -3656367025650685613L;
// 
//     public ApplicationError(String tag, String message) {
//         if (tag != null) {
//             Log.addEntry(tag, message);
//         }
//     }
// 
//     public ApplicationError(String tag, Exception e) {
//         // TODO: require message param as well?
//         super(e);
//         String message = e.getLocalizedMessage();
//         if (message == null) {
//             message = "(null)";
//         }
//         Log.addEntry(tag, String.format("%s: %s", e.getClass().toString(), message));
//         // TODO: log stack trace?
//     }
// }


import android.content.Context;
import android.location.Address;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import ca.psiphon.ploggy.Utils.ApplicationError;
/*
 * Copyright (c) 2013, Psiphon Inc.
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package ca.psiphon.ploggy;


/**
 * Schedule and monitor location events from Android OS.
 *
 * Implements best practices from:
 * - http://developer.android.com/guide/topics/location/strategies.html
 * - http://code.google.com/p/android-protips-location/
 *
 * Does not use the newer, higher level Play Services location API as it's
 * not available on open source Android builds and its source is not available
 * to review (e.g., verify that location isn't sent to 3rd party).
 */
public class LocationMonitor implements android.location.LocationListener {

    private static final String LOG_TAG = "Location Monitor";

    Engine mEngine;
    Handler mHandler;
    Runnable mStartLocationFixTask;
    Runnable mFinishLocationFixTask;
    Runnable mStopLocationUpdatesTask;
    Location mLastReportedLocation;
    Location mCurrentLocation;

    LocationMonitor(Engine engine) {

        // TODO: use Utils.FixedDelayExecutor

        mEngine = engine;
        mHandler = new Handler();
        initRunnables();
    }

// Path: AndroidApp/src/ca/psiphon/ploggy/Utils.java
// public static class ApplicationError extends Exception {
//     private static final long serialVersionUID = -3656367025650685613L;
// 
//     public ApplicationError(String tag, String message) {
//         if (tag != null) {
//             Log.addEntry(tag, message);
//         }
//     }
// 
//     public ApplicationError(String tag, Exception e) {
//         // TODO: require message param as well?
//         super(e);
//         String message = e.getLocalizedMessage();
//         if (message == null) {
//             message = "(null)";
//         }
//         Log.addEntry(tag, String.format("%s: %s", e.getClass().toString(), message));
//         // TODO: log stack trace?
//     }
// }


// Path: AndroidApp/src/ca/psiphon/ploggy/LocationMonitor.java
import android.content.Context;
import android.location.Address;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import ca.psiphon.ploggy.Utils.ApplicationError;

/*
 * Copyright (c) 2013, Psiphon Inc.
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package ca.psiphon.ploggy;


/**
 * Schedule and monitor location events from Android OS.
 *
 * Implements best practices from:
 * - http://developer.android.com/guide/topics/location/strategies.html
 * - http://code.google.com/p/android-protips-location/
 *
 * Does not use the newer, higher level Play Services location API as it's
 * not available on open source Android builds and its source is not available
 * to review (e.g., verify that location isn't sent to 3rd party).
 */
public class LocationMonitor implements android.location.LocationListener {

    private static final String LOG_TAG = "Location Monitor";

    Engine mEngine;
    Handler mHandler;
    Runnable mStartLocationFixTask;
    Runnable mFinishLocationFixTask;
    Runnable mStopLocationUpdatesTask;
    Location mLastReportedLocation;
    Location mCurrentLocation;

    LocationMonitor(Engine engine) {

        // TODO: use Utils.FixedDelayExecutor

        mEngine = engine;
        mHandler = new Handler();
        initRunnables();
    }

    public void start() throws Utils.ApplicationError {