Categories
discuss

Java – parse and unsigned hex string into a signed long

I have a bunch of hex strings, one of them, for example is:

  d1bc4f7154ac9edb

which is the hex value of “-3333702275990511909”. This is the same hex you get if you do Long.toHexString(“d1bc4f7154ac9edb”);

For now, let’s just assume I only have access to the hex string values and that is it. Doing this:

  Long.parseLong(hexstring, 16);

Doesn’t work because it converts it to a different value that is too large for a Long. Is there away to convert these unsigned hex values into signed longs?

Thanks!

Answer

You can use BigInteger to parse it and get back a long:

long value = new BigInteger("d1bc4f7154ac9edb", 16).longValue();
System.out.println(value); // this outputs -3333702275990511909
Categories
discuss

MapActivity with loader framework

Is there any solution for using the a MapView (and thus the MapActivity) with the honeycomb pre-3.0 compatibility Loader library (and thus FragmentActivity)?

(as an aside, I never quite understood why one needed to extend MapActivity in order to use the map – it’s terribly limiting when writing generic activities. Wouldn’t an interface be more appropriate?)

Answer

At Carter Jernigan’s suggestion received out-of-band, I ended up taking the source of the compat library and put a copy of everything in the android.support.v4.app package into my own source tree under the package android.support.v4_map.app. Then I modified the FragmentActivity to inherit from MapActivity instead of Activity and named it MapFragmentActivity. It’s a fairly crude hack, but it works well (so far). I now have all the benefit of the LoaderManager.

As an aside, trying to use CursorLoaders without a LoaderManager is a recipe for lots of unclosed cursors.

Categories
discuss

Debugging javascript on Android tablets/phones?

How do I enable the debug view like I can in Safari on iOS? I simply need to see if a Xoom that I’m testing a page on is generating javascript errors. I was trying to find how to enable the dev tools in the Android browser like I do for iOS but can’t seem to locate it.

Answer

I’ve worked on an Android app in the past where the java developer set it to alert JavaScript errors – caught an extra bug that we didn’t catch in the iOS version because of it. So, if you have access to the java layer, I’d check that out. I asked him what he did specifically and he said: “There’s a callback from the WebView class that lets me know when the JS code throws an error. I implemented that callback to display an android dialog.”

There’s two solutions other ideas on top of this that I use for debugging (ios/android). These are especially useful for embedded web views in games where you don’t have access to the built-in console:

1) Weinre a still beta, but functional, remote debugger. It’ll give you a faux inspector on your desktop that you can query / see errors on your remote device with. Has a whole dom inspector and anything. The guy that develops it is pretty responsive, too.

2) I write a javascript log function that hits my servers error log. Just tail your log file and you’re good to go. My javascript function looks something like this:

function hlog(){
    var s = Array.prototype.slice.apply(arguments).join('¶');
    document.createElement('img').src = 'http://yourdevbox/debugger/?m=' + encodeURIComponent(s);
}

That way I can take any number of arguments. My php page that recieves this request looks like this:

# ensure this can't be used in production 
if (strpos($GLOBALS['HTTP_HOST'], 'devboxhostname') < 0) die(':(');
error_log($_GET['m']);

Hopefully in the future, mobile devs will have way better debugging tools.

Categories
discuss

How to access widgets in a custom DialogPreference with a inflated layout?

Im very new to android and Im trying to load/persist values from my customized DialogPreference. Currently, this fails because findViewById returns null. Is the way I (try) to do it correct? How do I get access to my EditText widgets in the code?

public class AddressDialogPreference extends DialogPreference {

public AddressDialogPreference(Context context, AttributeSet attrs) {
    super(context, attrs);

    setDialogLayoutResource(R.layout.address_dialog);
}

@Override
protected void onBindDialogView(View view) {

    EditText idField = (EditText) view.findViewById(R.id.hostID);
    EditText ipField = (EditText) view.findViewById(R.id.hostIP);

    SharedPreferences pref = getSharedPreferences();
    idField.setText(pref.getString(getKey() + "_id","ExampleHostname"));
    ipField.setText(pref.getString(getKey() + "_ip","192.168.1.1"));

    super.onBindDialogView(view);
}

@Override
protected void onDialogClosed(boolean positiveResult) {

    if(!positiveResult)
        return;

    Dialog myDial = getDialog();
    EditText idField = (EditText) myDial.findViewById(R.id.hostID);
    EditText ipField = (EditText) myDial.findViewById(R.id.hostIP);

    SharedPreferences.Editor editor = getEditor();
    editor.putString(getKey() + "_id",idField.getText().toString());
    editor.putString(getKey() + "_ip",ipField.getText().toString());
}

address_dialog.xml:

<TextView
    android:text="Insert IP address"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/hostIP" />

<TextView
    android:text="Insert identifier"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/hostID" />

Answer

Ok I found it out myself. Well, I still do not know what caused the error, but I did a lot of changes to the layout and code and suddenly it just worked. I tried to revert to the code that I posted here, but I cannot reproduce the error. Im posting my working code, so anybody who runs into this problem, may use it.

An admin may also choose to delete this post, as it may be not possible to reproduce the error.

Here is the layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
    android:text="Insert IP address"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/AddressBox" />

<TextView
    android:text="Insert identifier"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/HostnameBox" />
</LinearLayout>

and the AddressDialogPreference.java:

public class AddressDialogPreference extends DialogPreference {

private EditText ipBox;
private EditText hostBox;

public AddressDialogPreference(Context context, AttributeSet attrs) {
    super(context, attrs);

    setDialogLayoutResource(R.layout.address_dialog);
}

@Override
protected void onBindDialogView(View view) {

    ipBox = (EditText) view.findViewById(R.id.AddressBox);
    hostBox = (EditText) view.findViewById(R.id.HostnameBox);

    SharedPreferences pref = getSharedPreferences();

    hostBox.setText(pref.getString(getKey() + "_host","ExampleHostname"));
    ipBox.setText(pref.getString(getKey() + "_ip","192.168.1.1"));

    super.onBindDialogView(view);
}

@Override
protected void onDialogClosed(boolean positiveResult) {

    if(!positiveResult)
        return;

    SharedPreferences.Editor editor = getEditor();
    editor.putString(getKey() + "_host",hostBox.getText().toString());
    editor.putString(getKey() + "_ip",ipBox.getText().toString());
    editor.commit();

    super.onDialogClosed(positiveResult);
}
}
Categories
discuss

android maps circle overlay, dynamically change radius?

I have a MapView in my app and am drawing a few circle overlays on this map. Everything is working fine, but when I zoom on the map, the overlay radius does not change. I have tried searching forums and google for a solution but could not find one that worked for me. Does anyone have any ideas?

Here is my code:

HelloGoogleMaps.java (main activity)

package com.adam.maps;

import java.util.Iterator;
import java.util.List;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.AbsoluteLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.widget.ZoomButtonsController.OnZoomListener;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

public class HelloGoogleMaps extends MapActivity {  
//create new LocationManager
//and LocationListener objects
LocationManager lm;
LocationListener locationListener;

OnZoomListener listener;

//create a new MapView
//and MapController object
MapView mapView;
MapController mc;

RelativeLayout parent;

int num = 4;
//LoopRegion region[] = new LoopRegion[num];
//LoopRegion border[] = new LoopRegion[num];
float regionX[] = {(float) 42.91556645193364, (float) 42.9151598328247, 
        (float) 43.00110298764482, (float) 43.00054196511636};
float regionY[] = {(float) -78.87073255078127, (float) -78.8714594294243, 
        (float) -78.78354466454317, (float) -78.78226256863405};
int regionR[] = {100, 70, 150, 75};
GeoPoint regionC[] = new GeoPoint[num];
CustomOverlay overlay[] = new CustomOverlay[num];
CustomOverlay overlayLoc;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Toast.makeText(getBaseContext(), 
            "Welcome to 'sound clusters'" , 
            Toast.LENGTH_LONG).show();

    //---use the LocationManager class to obtain GPS locations---
    lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);    

    locationListener = new MyLocationListener();

    lm.requestLocationUpdates(
        LocationManager.GPS_PROVIDER, 
        0, 
        0, 
        locationListener);

    //set our mapViewer object to our "mapview" namespace in the xml layout file
    //this allows us to set the zoom control "ON" in our view
    mapView = (MapView) findViewById(R.id.mapview);
    //this will enable zoom controls, and put it on the screen
    mapView.setBuiltInZoomControls(true);
    //--------------------------------------------------------//
    parent = (RelativeLayout) findViewById(R.id.parent);

    //-------this is part of creating an overlay icon-------------------------------
    /*List<Overlay> mapOverlays = mapView.getOverlays();
    Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
    CustomItemizedOverlay itemizedOverlay =
        new CustomItemizedOverlay(drawable, this);*/
    //------------------------------------------------------------------------------

    // Create new Overlay
    for (int i = 0; i < num; i++){
        regionC[i] = new GeoPoint(
                (int) (regionX[i] * 1E6), 
                (int) (regionY[i] * 1E6));
        int newRadius = (int) feetToPixels(mapView.getZoomLevel(), regionR[i]);
        overlay[i] = new CustomOverlay(regionC[i], newRadius);
        mapView.getOverlays().add(overlay[i]);
    }

    //-------this is part of creating an overlay icon-------------------------------
    /*OverlayItem overlayitem =
         new OverlayItem(point, "Hello", "I'm in Athens, Greece!");
    itemizedOverlay.addOverlay(overlayitem);
    mapOverlays.add(itemizedOverlay);*/
    //------------------------------------------------------------------------------



    mc = mapView.getController();
    mc.setZoom(20);
    mapView.setSatellite(true);
    Toast.makeText(getBaseContext(), 
            "Zoom level: " + mapView.getZoomLevel(), 
            Toast.LENGTH_SHORT).show();


}

//not sure what this does, but Google says you need it----//
@Override
protected boolean isRouteDisplayed() {
    return false;
}
//--------------------------------------------------------//    

private class MyLocationListener implements LocationListener 
{

    //@Override
    public void onLocationChanged(Location loc) {
        if (loc != null) {

            List overlays = mapView.getOverlays();
            // first remove old overlay
            if (overlays.size() > 0) {
                for (Iterator iterator = overlays.iterator(); iterator
                        .hasNext();) {
                    iterator.next();
                    iterator.remove();
                }
            }

            GeoPoint p = new GeoPoint(
                    (int) (loc.getLatitude() * 1E6), 
                    (int) (loc.getLongitude() * 1E6));

            overlayLoc = new CustomOverlay(p, 5);
            mapView.getOverlays().add(overlayLoc);
            for (int i = 0; i < num; i++){
                mapView.getOverlays().add(overlay[i]);
            }
            //mc.animateTo(p);
            //mc.setZoom(16);
            mapView.invalidate();
        }
    }

    //@Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
    }

    //@Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
    }

    //@Override
    public void onStatusChanged(String provider, int status, 
        Bundle extras) {
        // TODO Auto-generated method stub
    }
}

//custom functions--------------------------------------------------------------------
private static final double equatorFeet = 131479920; 
private double feetToPixels(int zoomLevel, int feet) { 
    double equatorPixels = 256; 
    for (int i = 1; i < zoomLevel; i++) { 
        equatorPixels = equatorPixels * 2; 
    } 
    double pixelPerFoot = equatorPixels / equatorFeet; 
    return feet * pixelPerFoot; 
} 
//------------------------------------------------------------------------------------

}

And the Overlay class CustomOverlay.java

package com.adam.maps;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class CustomOverlay extends Overlay {

private GeoPoint geopoint;
private int rad;

public CustomOverlay(GeoPoint point, int radius) {
    geopoint = point;
    rad = radius;
}


@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
    // Transform geo-position to Point on canvas
    Projection projection = mapView.getProjection();
    Point point = new Point();
    //store the transformed geopoint into a point with pixel values
    projection.toPixels(geopoint, point);

    /*// text "My Location"
    Paint text = new Paint();
    text.setAntiAlias(true);
    text.setColor(Color.BLUE);
    text.setTextSize(12);
    text.setTypeface(Typeface.MONOSPACE);*/

    // the circle to mark the spot
    Paint circlePaint = new Paint();
    circlePaint.setAntiAlias(true);
    //fill region
    circlePaint.setColor(Color.RED);
    circlePaint.setAlpha(90);
    circlePaint.setStyle(Paint.Style.FILL);
    canvas.drawCircle(point.x, point.y, rad, circlePaint);
    //border region
    circlePaint.setColor(Color.WHITE);
    circlePaint.setAlpha(255);
    circlePaint.setStyle(Paint.Style.STROKE);
    circlePaint.setStrokeWidth(3);
    canvas.drawCircle(point.x, point.y, rad, circlePaint);

    /*canvas.drawText("My Location", point.x + 3 * CIRCLERADIUS, point.y + 3
            * CIRCLERADIUS, text);*/
}
}

Thank you in advance for your help!

Answer

I know this thread is a few months old, but there are ALWAYS easier ways to achieve very similar results. I understand that the presented method gives a much more precise calculation regarding the size of the circle, but still, if you only need any circle around averaging some area, this code requires much less processing making the UI more fluid:

public class MapCircleOverlay extends Overlay {

private GeoPoint point;
private Paint paint1, paint2;

public MapCircleOverlay(GeoPoint point) {
    this.point = point;

    paint1 = new Paint();
    paint1.setARGB(128, 0, 0, 255);
    paint1.setStrokeWidth(2);
    paint1.setStrokeCap(Paint.Cap.ROUND);
    paint1.setAntiAlias(true);
    paint1.setDither(false);
    paint1.setStyle(Paint.Style.STROKE);

    paint2 = new Paint();
    paint2.setARGB(64, 0, 0, 255);  

}

@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {

    Point pt = mapView.getProjection().toPixels(point, null);
    float radius = (float) Math.pow(2, mapView.getZoomLevel() - 10);

    if(radius < canvas.getHeight()/25){
        radius = canvas.getHeight()/25;
    }

    canvas.drawCircle(pt.x, pt.y, radius, paint2);
    canvas.drawCircle(pt.x, pt.y, radius, paint1);

}

}

Explanation: the documentation states that for each zoomLevel the map doubles (or halves) the size, so as long as the radius is doubling or halving, its size will be consistent across drawings.

The “-10” can be changed to vary the size of the circle (maybe be done in Constructor if desired)

Also the min_r (minimum radius) calculation can be tweaked as desired, it’s only there to prevent the circle to completely disappear as the user zooms out.

hope it helps someone 😉

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..