Example usage for com.google.common.io.protocol ProtoBuf getString

List of usage examples for com.google.common.io.protocol ProtoBuf getString

Introduction

In this page you can find the example usage for com.google.common.io.protocol ProtoBuf getString.

Prototype

public String getString(int tag) 

Source Link

Document

Returns the string value for a given tag converted to a Java String assuming UTF-8 encoding.

Usage

From source file:com.codetastrophe.cellfinder.LocationFetcher.java

private void parseNetworkLocationReply(ProtoBuf response, LocationCallback callback) {
    if (response == null) {
        callback.Error("response is null");
        return;/*w w w .jav  a2 s .  c o m*/
    }

    int status1 = response.getInt(GLocReply.STATUS);
    if (status1 != ResponseCodes.STATUS_STATUS_SUCCESS) {
        callback.Error("RPC failed with status " + status1);
        return;
    }

    if (response.has(GLocReply.PLATFORM_KEY)) {
        String platformKey = response.getString(GLocReply.PLATFORM_KEY);
        if (!TextUtils.isEmpty(platformKey)) {
            setPlatformKey(platformKey);
        }
    }

    if (!response.has(GLocReply.REPLY_ELEMENTS)) {
        callback.Error("no ReplyElement");
        return;
    }
    ProtoBuf replyElement = response.getProtoBuf(GLocReply.REPLY_ELEMENTS);

    int status2 = replyElement.getInt(GLocReplyElement.STATUS);
    if (status2 != ResponseCodes.STATUS_STATUS_SUCCESS && status2 != ResponseCodes.STATUS_STATUS_FAILED) {
        callback.Error("failed with status " + status2);
        return;
    }

    double lat = 0, lng = 0;
    int mcc = -1, mnc = -1, cid = -1, lac = -1;
    int locType = -1;
    int acc = -1, alt = 0;

    Log.d(TAG, "getNetworkLocation(): Number of prefetched entries "
            + replyElement.getCount(GLocReplyElement.DEVICE_LOCATION));

    // most of the time there is only one response here, but loop through all of them
    // just in case one of them is the real tower location and not just the centroid
    for (int i = 0; i < replyElement.getCount(GLocReplyElement.DEVICE_LOCATION); i++) {
        ProtoBuf device = replyElement.getProtoBuf(GLocReplyElement.DEVICE_LOCATION, i);
        if (device.has(GDeviceLocation.LOCATION)) {
            ProtoBuf deviceLocation = device.getProtoBuf(GDeviceLocation.LOCATION);
            if (deviceLocation.has(GLocation.LAT_LNG)) {
                lat = deviceLocation.getProtoBuf(GLocation.LAT_LNG).getInt(GLatLng.LAT_E7) / E7;
                lng = deviceLocation.getProtoBuf(GLocation.LAT_LNG).getInt(GLatLng.LNG_E7) / E7;
            }
            if (deviceLocation.has(GLocation.ACCURACY)) {
                acc = deviceLocation.getInt(GLocation.ACCURACY);
            }

            if (deviceLocation.has(GLocation.ALTITUDE)) {
                alt = deviceLocation.getInt(GLocation.ALTITUDE);
            }

            if (deviceLocation.has(GLocation.LOC_TYPE)) {
                locType = deviceLocation.getInt(GLocation.LOC_TYPE);
            }
        }

        Log.d(TAG, String.format("lat %f lon %f locType %d", lat, lng, locType));

        // get cell info
        if (device.has(GDeviceLocation.CELL)) {
            ProtoBuf deviceCell = device.getProtoBuf(GDeviceLocation.CELL);
            cid = deviceCell.getInt(GCell.CELLID);
            lac = deviceCell.getInt(GCell.LAC);
            if (deviceCell.has(GCell.MNC) && deviceCell.has(GCell.MCC)) {
                mcc = deviceCell.getInt(GCell.MCC);
                mnc = deviceCell.getInt(GCell.MNC);
            }
        }

        Log.d(TAG, String.format("mcc %d mnc %d lac %d cid %d lat %f lon %f locType %d", mcc, mnc, lac, cid,
                lat, lng, locType));

        // if we have the actual tower location, break, otherwise keep going
        // just in case
        if (locType == GLocation.LOCTYPE_TOWER_LOCATION) {
            break;
        }
    }

    if (cid != -1 && lac != -1) {
        callback.GotLocation(lac, cid, mcc, mnc, lat, lng, alt, acc, locType);
    } else {
        callback.Error("no cell information");
    }
}