edu.poly.cs9033.nowaiting.service.LocationReceiver.java Source code

Java tutorial

Introduction

Here is the source code for edu.poly.cs9033.nowaiting.service.LocationReceiver.java

Source

/***
  Copyright (c) 2010 CommonsWare, LLC
      
  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.
 */

package edu.poly.cs9033.nowaiting.service;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;

import edu.poly.cs9033.nowaiting.controller.MainActivity;
import edu.poly.cs9033.nowaiting.service.LocationPoller;
import edu.poly.cs9033.nowaiting.utils.JSONParser;
import edu.poly.cs9033.nowaiting.utils.Logger;

public class LocationReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        Logger.i("Location changes detected!");

        Bundle b = intent.getExtras();
        Location loc = (Location) b.get(LocationPoller.EXTRA_LOCATION);
        String msg;

        if (loc == null) {
            loc = (Location) b.get(LocationPoller.EXTRA_LASTKNOWN);

            if (loc == null) {
                msg = intent.getStringExtra(LocationPoller.EXTRA_ERROR);
            } else {
                msg = "TIMEOUT, lastKnown=" + loc.toString();
            }
        } else {
            msg = loc.toString();
            uploadCurrentLocation(loc);
        }

        if (msg == null) {
            msg = "Invalid broadcast received!";
        }

    }

    private void uploadCurrentLocation(Location location) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("command", "UPDATE_LOCATION");
        params.put("latitude", location.getLatitude());
        params.put("longitude", location.getLongitude());
        params.put("datetime", String.valueOf((System.currentTimeMillis() / 1000L)));
        Logger.i("Uploading location info to server:" + location.toString());
        JSONObject response = new JSONParser().makeHttpRequest(MainActivity.SERVER_URL, params);
        if (response == null) {
            Logger.e("can't talk with the server,JSON response is null");
        } else {
            Integer responseCode = -100;
            try {
                responseCode = (Integer) response.get("response_code");
                if (responseCode != 0)
                    Logger.e("http request to the server failed,reason:" + response.get("error_string"));
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}