Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright 2012 Google Inc.
 * 
 * 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
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

import android.util.Log;

import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class Main {
    public static int emulatorPort = 5554;
    private static final String ANDROID_LOCAL_IP = "10.0.2.2";
    public static final double START_LONGITUDE = 51;
    public static final double START_LATITUDE = -1.3f;
    public static final double DELTA_LONGITUDE = 0.0005f;
    public static final double DELTA_LADITUDE = 0.0005f;
    public static final String LOG_TAG = "MyTracksTest";
    public static final int TINY_WAIT_TIME = 200;
    public static final int SHORT_WAIT_TIME = 2000;
    public static boolean isEmulator = true;

    /**
     * Sends Gps data to emulator, and the start value has an offset.
     * 
     * @param number send times
     * @param offset is used to compute the start latitude and longitude
     * @param pause pause interval between each sending
     */
    public static void sendGps(int number, int offset, int pause) {
        if (number < 1) {
            return;
        }

        int pauseInterval = TINY_WAIT_TIME;
        if (pause != -1) {
            pauseInterval = pause;
        }

        // If it's a real device, does not send simulated GPS signal.
        if (!isEmulator) {
            return;
        }

        PrintStream out = null;
        Socket socket = null;
        try {
            socket = new Socket(ANDROID_LOCAL_IP, emulatorPort);
            out = new PrintStream(socket.getOutputStream());
            double longitude = START_LONGITUDE + offset * DELTA_LONGITUDE;
            double latitude = START_LATITUDE + offset * DELTA_LADITUDE;
            for (int i = 0; i < number; i++) {
                out.println("geo fix " + longitude + " " + latitude);
                longitude += DELTA_LONGITUDE;
                latitude += DELTA_LADITUDE;
                Thread.sleep(pauseInterval);
            }
            // Wait the GPS signal can be obtained by MyTracks.
            Thread.sleep(SHORT_WAIT_TIME);
        } catch (UnknownHostException e) {
            System.exit(-1);
        } catch (IOException e) {
            System.exit(-1);
        } catch (InterruptedException e) {
            System.exit(-1);
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }

    /**
     * Send Gps data to emulator.
     * 
     * @param number number of signals
     * @param offset is used to compute the start latitude and longitude
     */
    public static void sendGps(int number, int offset) {
        sendGps(number, offset, -1);
    }

    /**
     * Send Gps data to emulator.
     * 
     * @param number number of signals
     */
    public static void sendGps(int number) {
        sendGps(number, 0, -1);
    }

    /**
     * Waits n milliseconds.
     * 
     * @param milliseconds time to sleep
     */
    public static void sleep(long milliseconds) {
        try {
            Thread.sleep(milliseconds);
        } catch (InterruptedException e) {
            Log.e(LOG_TAG, "Unable to sleep " + milliseconds, e);
        }
    }
}