1
0
Fork 0

Initial Import. Version 2.0

This commit is contained in:
Carlos Mogas da Silva 2010-05-17 00:14:00 +01:00
commit fbbd2673ff
69 changed files with 2994 additions and 0 deletions

View file

@ -0,0 +1,23 @@
package org.r3pek.pharmacies;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
public class ConfigValues {
private static SharedPreferences settings;
public ConfigValues(Context ctx) {
settings = PreferenceManager.getDefaultSharedPreferences(ctx);
}
public boolean isOnlyService() {
return settings.getBoolean("pref_gen_just_onservice", false);
}
public int getRadius() {
int dist = settings.getInt("pref_gen_maxdist", 2000);
return dist == 2000 ? dist : dist * 1000;
}
}

View file

@ -0,0 +1,54 @@
package org.r3pek.pharmacies;
import org.r3pek.pharmacies.utils.GlobalVars;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationManager;
import android.os.Handler;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
public class GetPositionOverlay extends ItemizedOverlay<OverlayItem> {
private PharmaciesOverlay pharmacies;
private MyPositionOverlay myLocation;
private Handler onSelectionHandler;
public GetPositionOverlay(Drawable defaultMarker, PharmaciesOverlay pharmacies, MyPositionOverlay myLocation, Handler onSelectionHandler) {
super(defaultMarker);
this.pharmacies = pharmacies;
this.myLocation = myLocation;
this.onSelectionHandler = onSelectionHandler;
populate();
}
@Override
protected OverlayItem createItem(int i) {
return null;
}
@Override
public int size() {
return 0;
}
@Override
public boolean onTap(GeoPoint p, MapView map) {
Location l = new Location(LocationManager.GPS_PROVIDER);
l.setLatitude(p.getLatitudeE6()/1000000.0);
l.setLongitude(p.getLongitudeE6()/1000000.0);
l.setAccuracy(0.0f);
GlobalVars.getPharmacies().clear();
pharmacies.refresh();
onSelectionHandler.sendEmptyMessage(0);
GlobalVars.positioner.setFixedLocation(l);
map.getOverlays().clear();
map.getOverlays().add(pharmacies);
map.getOverlays().add(myLocation);
map.postInvalidate();
return super.onTap(p, map);
}
}

View file

@ -0,0 +1,161 @@
package org.r3pek.pharmacies;
import java.util.Timer;
import java.util.TimerTask;
import org.r3pek.pharmacies.utils.GeoUtils;
import org.r3pek.pharmacies.utils.GlobalVars;
import org.r3pek.pharmacies.utils.OnPositionChangeListener;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Paint.Style;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LevelListDrawable;
import android.os.Handler;
import android.os.Message;
import android.os.Handler.Callback;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;
import com.google.android.maps.MapView.LayoutParams;
public class MyPositionOverlay extends Overlay implements OnPositionChangeListener {
private static final float dotScale = 0.6f;
private MapView map;
private Runnable runOnFirstFix;
private boolean isFirstFix;
private LevelListDrawable netLocationImage;
private LevelListDrawable gpsLocationImage;
private Timer updateDotTask;
private Context context;
private View shownView;
private Activity mapActivity;
public MyPositionOverlay(Context ctx, MapView mapView, Activity mapActivity) {
super();
map = mapView;
this.mapActivity = mapActivity;
GlobalVars.positioner.registerNotification(this);
isFirstFix = true;
context = ctx;
netLocationImage = (LevelListDrawable) ctx.getResources().getDrawable(R.drawable.network_fix);
gpsLocationImage = (LevelListDrawable) ctx.getResources().getDrawable(R.drawable.gps_fix);
updateDotTask = new Timer();
TimerTask tt = new TimerTask() {
@Override
public void run() {
if (GlobalVars.positioner.isGPSFix()) return;
int current = netLocationImage.getLevel();
current = (current + 1) % 4;
netLocationImage.setLevel(current);
map.postInvalidate();
}
};
updateDotTask.scheduleAtFixedRate(tt, 100, 500);
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
if (shadow || isFirstFix) return;
if (GlobalVars.positioner.getLastGeoPoint() == null) return;
Projection projection = mapView.getProjection();
Point point = new Point();
projection.toPixels(GlobalVars.positioner.getLastGeoPoint(), point);
// Draw the accuracy circle
float radius = projection.metersToEquatorPixels(GlobalVars.positioner.getLastAccuracy());
canvas.save();
Paint circlePaint = new Paint();
circlePaint.setStyle(Style.FILL);
circlePaint.setARGB(10, 0, 0, 255);
canvas.drawCircle(point.x, point.y, radius, circlePaint);
circlePaint.setARGB(200, 0, 0, 255);
circlePaint.setStyle(Style.STROKE);
canvas.drawCircle(point.x, point.y, radius, circlePaint);
canvas.restore();
// Draw in the right spot
Drawable d = GlobalVars.positioner.isGPSFix() ? gpsLocationImage.getCurrent() : netLocationImage.getCurrent();
int scaledImageWidth = (int)(d.getIntrinsicWidth() * dotScale);
int scaledImageHeight = (int)(d.getIntrinsicHeight() * dotScale);
float scaledImageCenterX = scaledImageWidth / 2;
float scaledImageCenterY = scaledImageHeight / 2;
canvas.save();
canvas.translate(-(scaledImageCenterX - point.x), -(scaledImageCenterY - point.y));
d.setBounds(0, 0, scaledImageWidth, scaledImageHeight);
d.getCurrent().draw(canvas);
canvas.restore();
super.draw(canvas, mapView, shadow);
}
@Override
public void onLocationChange(GeoPoint currentPos, float accuracy) {
if (isFirstFix) {
isFirstFix = false;
runOnFirstFix.run();
}
map.postInvalidate();
}
public void disableMyLocation() {
GlobalVars.positioner.unregisterNotification(this);
}
public void enableMyLocation() {
GlobalVars.positioner.registerNotification(this);
}
public void runOnFirstFix(Runnable r) {
runOnFirstFix = r;
}
@Override
public boolean onTap(GeoPoint p, MapView map) {
Point pointTap = map.getProjection().toPixels(p, null);
Point pointMap = map.getProjection().toPixels(GlobalVars.positioner.getLastGeoPoint(), null);
if (Math.abs(pointTap.x - pointMap.x) <= 20 && Math.abs(pointMap.y - pointTap.y) <= 20 && shownView == null) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout ll = (LinearLayout)inflater.inflate(R.layout.mylocation_balloon, null);
LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, GlobalVars.positioner.getLastGeoPoint(), 0, -35, LayoutParams.MODE_MAP | LayoutParams.CENTER);
final TextView tvLocation = (TextView)ll.findViewById(R.id.tvLocation);
tvLocation.setText(context.getString(R.string.loading));
map.getController().animateTo(GlobalVars.positioner.getLastGeoPoint());
map.addView(ll, lp);
shownView = ll;
GeoUtils.getLocationName(context, new Handler(new Callback() {
@Override
public boolean handleMessage(Message msg) {
final String location = msg.getData().getString("location");
mapActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
tvLocation.setText(location);
}
});
return true;
}
}));
} else if (shownView != null) {
map.removeView(shownView);
shownView = null;
}
return super.onTap(p, map);
}
}

View file

@ -0,0 +1,18 @@
package org.r3pek.pharmacies;
import org.r3pek.pharmacies.widgets.SeekBarPreference;
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class MyPreferences extends PreferenceActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
SeekBarPreference sbp = (SeekBarPreference) findPreference("pref_gen_maxdist");
sbp.setMin(1);
}
}

View file

@ -0,0 +1,166 @@
package org.r3pek.pharmacies;
import java.util.LinkedList;
import org.r3pek.pharmacies.utils.GeoUtils;
import org.r3pek.pharmacies.utils.GlobalVars;
import org.r3pek.pharmacies.widgets.Compass;
import org.r3pek.pharmacies.widgets.ImageRotater;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.MapView.LayoutParams;
public class PharmaciesOverlay extends ItemizedOverlay<OverlayItem> {
private Context ctx;
private MapView mapView;
private View shownView;
private SensorManager sm;
private ImageRotater cRotator;
private Compass compass;
private LinkedList<Pharmacy> pharmacies;
public PharmaciesOverlay(Drawable defaultMarker, Context ctx, MapView mapView) {
super(boundCenterBottom(defaultMarker));
this.ctx = ctx;
this.mapView = mapView;
shownView = null;
populate();
sm = (SensorManager)ctx.getSystemService(Context.SENSOR_SERVICE);
cRotator = new ImageRotater();
pharmacies = new LinkedList<Pharmacy>();
}
@Override
protected synchronized OverlayItem createItem(int i) {
Pharmacy p = pharmacies.get(i);
OverlayItem result = new OverlayItem(p.getGeoPoint(), p.getName(), "");
if (p.isAtService())
result.setMarker(boundCenterBottom(ctx.getResources().getDrawable(R.drawable.pin_on)));
return result;
}
@Override
public synchronized int size() {
if (pharmacies == null) return 0;
return pharmacies.size();
}
@Override
protected boolean onTap(final int i) {
if (mapView.getChildCount() > 0) return true;
LayoutInflater inflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout ll = (LinearLayout)inflater.inflate(R.layout.pharmacy_balloon, null);
final Pharmacy pharm = GlobalVars.getPharmacies().get(i);
LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, pharm.getGeoPoint(), 0, -70, LayoutParams.MODE_MAP | LayoutParams.CENTER);
TextView tvName = (TextView)ll.findViewById(R.id.tvBalloonName);
TextView tvDistance = (TextView)ll.findViewById(R.id.tvDistance);
ImageView ivStatus = (ImageView)ll.findViewById(R.id.ivStatus);
Button btnDetalhes = (Button)ll.findViewById(R.id.btnBalloonDetalhes);
Button btnTelef = (Button)ll.findViewById(R.id.btnBalloonTelef);
Button btnNav = (Button)ll.findViewById(R.id.btnBalloonNav);
compass = (Compass)ll.findViewById(R.id.compass);
btnTelef.setEnabled(!pharm.getPhoneNumber().equals(""));
compass.setBearing(GeoUtils.bearing(GlobalVars.positioner.getLastGeoPoint(), pharm.getGeoPoint()));
compass.setOrientation(cRotator.getLastOrientation());
sm.registerListener(cRotator, sm.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_NORMAL);
cRotator.addImage(compass);
tvName.setText(pharm.getName());
tvDistance.setText(pharm.getDistance().substring(0, pharm.getDistance().indexOf(".")) + "m");
ivStatus.setBackgroundResource(pharm.isAtService() ? R.drawable.status_on : R.drawable.status_off);
btnDetalhes.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent pharmInfo = new Intent(ctx, PharmacyInfo.class);
pharmInfo.putExtra("org.r3pek.pharmacies.Pharmacy", pharm);
pharmInfo.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(pharmInfo);
}
});
btnTelef.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + pharm.getPhoneNumber()));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(intent);
}
});
btnNav.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
StringBuilder url = new StringBuilder();
url.append("http://maps.google.com/maps?f=d&hl=en");
url.append("&saddr=");
url.append( Double.toString((double)GlobalVars.positioner.getLastGeoPoint().getLatitudeE6()/1.0E6 ));
url.append(",");
url.append( Double.toString((double)GlobalVars.positioner.getLastGeoPoint().getLongitudeE6()/1.0E6 ));
url.append("&daddr=");
url.append( Double.toString((double)pharm.getGeoPoint().getLatitudeE6()/1.0E6 ));
url.append(",");
url.append( Double.toString((double)pharm.getGeoPoint().getLongitudeE6()/1.0E6 ));
url.append("&dirflg=d&nav=1");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse(url.toString()));
intent.addFlags(0x10800000);
intent.setClassName("com.google.android.apps.m4ps", "com.google.android.maps.driveabout.app.NavigationActivity");
try {
ctx.startActivity(intent);
} catch (ActivityNotFoundException e) {
intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.driveabout.app.NavigationActivity");
try {
ctx.startActivity(intent);
} catch (ActivityNotFoundException e2) { }
}
}
});
shownView = ll;
mapView.addView(ll, lp);
mapView.getController().animateTo(pharm.getGeoPoint());
return super.onTap(i);
}
public void removeBallon() {
cRotator.delImage(compass);
sm.unregisterListener(cRotator);
mapView.removeView(shownView);
}
@Override
public boolean onTap(GeoPoint p, MapView map) {
removeBallon();
return super.onTap(p, map);
}
public synchronized void refresh() {
pharmacies.clear();
for (Pharmacy p : GlobalVars.getPharmacies())
pharmacies.add(p);
populate();
}
}

View file

@ -0,0 +1,91 @@
package org.r3pek.pharmacies;
import java.io.Serializable;
import com.google.android.maps.GeoPoint;
public class Pharmacy implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private String address;
private String phoneNumber;
private String latitude;
private String longitude;
private String distance;
private Boolean atService;
private String parish;
public Pharmacy() {
}
public String getName() {
return name == null ? "" : name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address == null ? "" : address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhoneNumber() {
return phoneNumber == null ? "" : phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getLatitude() {
return latitude == null ? "" : latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
public String getLongitude() {
return longitude == null ? "" : longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
public String getDistance() {
return distance == null ? "" : distance;
}
public void setDistance(String distance) {
this.distance = distance;
}
public boolean isAtService() {
return atService;
}
public void setAtService(boolean atService) {
this.atService = atService;
}
public String getParish() {
return parish == null ? "" : parish;
}
public void setParish(String parish) {
this.parish = parish;
}
public GeoPoint getGeoPoint() {
return new GeoPoint((int)(Double.parseDouble(latitude) * 1E6), (int)(Double.parseDouble(longitude) * 1E6));
}
}

View file

@ -0,0 +1,169 @@
package org.r3pek.pharmacies;
import org.r3pek.pharmacies.utils.GeoUtils;
import org.r3pek.pharmacies.utils.GlobalVars;
import org.r3pek.pharmacies.widgets.Arrow;
import org.r3pek.pharmacies.widgets.ImageRotater;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.location.Location;
import android.net.Uri;
import android.os.Bundle;
import android.text.ClipboardManager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
public class PharmacyInfo extends Activity {
private final static int MENU_DIAL = 0;
private final static int MENU_SHARE = 1;
private final static int MENU_NAVIGATE = 2;
private final static int MENU_COPY = 3;
private final static int MENU_SHOWONMAP = 4;
private SensorManager sm;
private Pharmacy p;
private ImageRotater arrowChangerSensor;
private int pharmacyIndex;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pharmacyinfo);
p = (Pharmacy)getIntent().getSerializableExtra("org.r3pek.pharmacies.Pharmacy");
pharmacyIndex = getIntent().getIntExtra("org.r3pek.pharmacies.PharmacyIndex", -1);
TextView tvName = (TextView)findViewById(R.id.tvName);
TextView tvAddress = (TextView)findViewById(R.id.tvAddress);
TextView tvParish = (TextView)findViewById(R.id.tvParish);
TextView tvPhoneNumber = (TextView)findViewById(R.id.tvPhoneNumber);
TextView tvLatitude = (TextView)findViewById(R.id.tvLatitude);
TextView tvLongitude = (TextView)findViewById(R.id.tvLongitude);
TextView tvDist = (TextView)findViewById(R.id.tvDistance);
Arrow arrow = (Arrow)findViewById(R.id.ivInfoDirection);
TextView tvService = (TextView)findViewById(R.id.tvOnDuty);
sm = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
arrowChangerSensor = new ImageRotater();
arrow.setBearing(GeoUtils.bearing(GlobalVars.positioner.getLastGeoPoint(), p.getGeoPoint()));
arrow.setOrientation(arrowChangerSensor.getLastOrientation());
tvName.setText(p.getName());
tvAddress.setText(p.getAddress());
tvParish.setText(p.getParish());
tvPhoneNumber.setText(p.getPhoneNumber().equals("") ? getString(R.string.na) : p.getPhoneNumber());
tvLatitude.setText(p.getLatitude());
tvLongitude.setText(p.getLongitude());
tvDist.setText(p.getDistance().substring(0, p.getDistance().indexOf(".")) + "m");
tvService.setText(p.isAtService() ? getString(R.string.yes) : getString(R.string.no));
}
@Override
public void onResume() {
sm.registerListener(arrowChangerSensor, sm.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_NORMAL);
arrowChangerSensor.addImage((Arrow)findViewById(R.id.ivInfoDirection));
super.onResume();
}
@Override
public void onPause() {
arrowChangerSensor.delImage((Arrow)findViewById(R.id.ivInfoDirection));
sm.unregisterListener(arrowChangerSensor);
super.onPause();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, MENU_DIAL, 0, getString(R.string.dial)).setIcon(android.R.drawable.ic_menu_call);
menu.add(0, MENU_SHARE, 1, getString(R.string.share)).setIcon(android.R.drawable.ic_menu_share);
menu.add(0, MENU_NAVIGATE, 2, getString(R.string.navigate)).setIcon(android.R.drawable.ic_menu_directions);
menu.add(0, MENU_COPY, 3, getString(R.string.copy_to_clipboard)).setIcon(R.drawable.copy);
menu.add(0, MENU_SHOWONMAP, 4, getString(R.string.show_on_map)).setIcon(android.R.drawable.ic_menu_mapmode);
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.findItem(MENU_DIAL).setEnabled(!p.getPhoneNumber().equals(""));
menu.findItem(MENU_SHOWONMAP).setEnabled(pharmacyIndex != -1);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_DIAL:
makeCall();
break;
case MENU_SHARE:
share();
break;
case MENU_NAVIGATE:
openNavigation();
break;
case MENU_COPY:
copyAddrToClipboard();
break;
case MENU_SHOWONMAP:
showOnMap();
break;
}
return true;
}
private void showOnMap() {
GlobalVars.pharmaciesOverlay.onTap(pharmacyIndex);
Toast.makeText(getApplicationContext(), R.string.go_back, Toast.LENGTH_SHORT).show();
}
private void share() {
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_TEXT, p.getName() + '\n' +
getString(R.string.address) + " " + p.getAddress() + '\n' +
getString(R.string.distance) + " " + p.getDistance().substring(0, p.getDistance().indexOf(".")) + "m" + '\n' +
getString(R.string.phone_number) + " " + (p.getPhoneNumber().equals("") ? getString(R.string.na) : p.getPhoneNumber()) + '\n' +
getString(R.string.at_service) + " " + (p.isAtService() ? getString(R.string.yes) : getString(R.string.no)));
startActivity(i);
}
private void copyAddrToClipboard() {
ClipboardManager cm = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
cm.setText(p.getAddress());
Toast.makeText(getApplicationContext(), R.string.copied_to_clipboard, Toast.LENGTH_SHORT).show();
}
private void makeCall() {
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + p.getPhoneNumber()));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
private void openNavigation() {
double lat = Double.parseDouble(p.getLatitude());
double lng = Double.parseDouble(p.getLongitude());
Location loc = GlobalVars.positioner.getLastLocation();
StringBuilder url = new StringBuilder();
url.append("http://maps.google.com/maps?f=d&hl=en");
url.append("&saddr=");
url.append( Double.toString(loc.getLatitude()));
url.append(",");
url.append( Double.toString(loc.getLongitude()));
url.append("&daddr=");
url.append( Double.toString(lat));
url.append(",");
url.append( Double.toString(lng));
url.append("&ie=UTF8&0&om=0");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse(url.toString()));
startActivity(intent);
}
}

View file

@ -0,0 +1,148 @@
package org.r3pek.pharmacies;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import org.r3pek.pharmacies.utils.GeoUtils;
import org.r3pek.pharmacies.utils.GlobalVars;
import org.r3pek.pharmacies.widgets.ImageRotater;
import org.r3pek.pharmacies.widgets.ScrollTextView;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Handler.Callback;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.view.animation.Animation.AnimationListener;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;
public class PharmacyList extends Activity implements OnItemClickListener {
private Context context;
private SensorManager sm;
private ImageRotater arrowChangerSensor;
private ListView list;
private ScrollTextView tvLocation;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
context = getApplicationContext();
list = (ListView)findViewById(R.id.list);
sm = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
arrowChangerSensor = new ImageRotater();
tvLocation = (ScrollTextView) findViewById(R.id.tvLocation);
tvLocation.setText(getString(R.string.loading));
tvLocation.setRndDuration(10000);
GeoUtils.getLocationName(context, new Handler(new Callback() {
@Override
public boolean handleMessage(Message msg) {
final String location = msg.getData().getString("location");
runOnUiThread(new Runnable() {
@Override
public void run() {
tvLocation.setText(location);
}
});
if (location.equals(getString(R.string.not_available))) {
final LinearLayout ll02 = (LinearLayout)findViewById(R.id.LinearLayout02);
TranslateAnimation anim = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, -1.0f
);
anim.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
ll02.setVisibility(View.GONE);
}
});
anim.setStartOffset(2000);
anim.setDuration(2000);
anim.setInterpolator(new AccelerateInterpolator(1));
ll02.startAnimation(anim);
} else
runOnUiThread(new Runnable() {
@Override
public void run() {
tvLocation.startScroll();
}
});
return true;
}
}));
list.setOnItemClickListener(this);
LinkedList<Pharmacy> allPharm = GlobalVars.getPharmacies();
ArrayList<HashMap<String, String>> pharmacies = new ArrayList<HashMap<String, String>>();
for (Pharmacy p : allPharm) {
HashMap<String, String> pharmacy = new HashMap<String, String>();
pharmacy.put("name", p.getName());
pharmacy.put("addr", p.getAddress());
DecimalFormat df = new DecimalFormat("#.#");
double dist = Double.parseDouble(p.getDistance().substring(0, p.getDistance().indexOf("."))) / 1000;
pharmacy.put("dist", df.format(dist) + "km");
pharmacies.add(pharmacy);
}
/* Create the Adapter to show the information */
SimpleAdapter adap;
adap = new PharmacyListSimpleAdapter(context, pharmacies, R.layout.list_row,
new String[] {"name", "addr", "dist"},
new int[] {R.id.tvNome, R.id.tvMorada, R.id.tvDistancia},
arrowChangerSensor
);
list.setAdapter(adap);
}
@Override
protected void onResume() {
super.onResume();
sm.registerListener(arrowChangerSensor, sm.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
sm.unregisterListener(arrowChangerSensor);
super.onPause();
}
@Override
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
Intent i = new Intent(getApplicationContext(), PharmacyInfo.class);
i.putExtra("org.r3pek.pharmacies.Pharmacy", GlobalVars.getPharmacies().get(position));
i.putExtra("org.r3pek.pharmacies.PharmacyIndex", position);
startActivity(i);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
tvLocation.startScroll();
}
}

View file

@ -0,0 +1,53 @@
package org.r3pek.pharmacies;
import java.util.List;
import java.util.Map;
import org.r3pek.pharmacies.utils.GeoUtils;
import org.r3pek.pharmacies.utils.GlobalVars;
import org.r3pek.pharmacies.widgets.Arrow;
import org.r3pek.pharmacies.widgets.ImageRotater;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class PharmacyListSimpleAdapter extends SimpleAdapter {
private ImageRotater arrowCallback;
public PharmacyListSimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to, ImageRotater arrowCallback) {
super(context, data, resource, from, to);
this.arrowCallback = arrowCallback;
}
private Pharmacy getPharmacyWithName(String name) {
for (Pharmacy p : GlobalVars.getPharmacies())
if (p.getName().equals(name)) return p;
return null;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
ImageView ivStatus = (ImageView) v.findViewById(R.id.ivStatus);
Arrow ivDirection = (Arrow) v.findViewById(R.id.ivDirection);
TextView tvNome = (TextView) v.findViewById(R.id.tvNome);
Pharmacy p = getPharmacyWithName(tvNome.getText().toString());
ivDirection.setBearing(GeoUtils.bearing(GlobalVars.positioner.getLastGeoPoint(), p.getGeoPoint()));
ivDirection.setOrientation(arrowCallback.getLastOrientation());
arrowCallback.addImage(ivDirection);
if (p.isAtService())
ivStatus.setImageResource(R.drawable.status_on);
else
ivStatus.setImageResource(R.drawable.status_off);
return v;
}
}

View file

@ -0,0 +1,324 @@
package org.r3pek.pharmacies;
import java.net.URL;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.r3pek.pharmacies.utils.GlobalVars;
import org.r3pek.pharmacies.utils.OnPositionChangeListener;
import org.r3pek.pharmacies.utils.Positioner;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Handler.Callback;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
public class ViewOnMap extends MapActivity implements OnPositionChangeListener {
private static final int MENU_REFRESH = 0;
private static final int MENU_SETTINGS = 1;
private static final int MENU_LIST = 2;
private static final int MENU_ABOUT = 3;
private static final int MENU_SETMYPOSITION = 4;
private static final int MENU_GETPOSITIONFROMGPS = 5;
private static final int MENU_MYPOSITION = 6;
private static final int DIALOG_ABOUT = 0;
private PharmaciesOverlay pharmaciesOverlay;
private MyPositionOverlay myPositionOverlay;
private MapView map;
private ConfigValues configs;
private Handler endThreadHandler;
private boolean hadFirstFix;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
/* Create objects */
map = (MapView)findViewById(R.id.MapView);
pharmaciesOverlay = new PharmaciesOverlay(getResources().getDrawable(R.drawable.pin_off), getApplicationContext(), map);
GlobalVars.pharmaciesOverlay = pharmaciesOverlay;
configs = new ConfigValues(getApplicationContext());
GlobalVars.positioner = new Positioner(getApplicationContext());
myPositionOverlay = new MyPositionOverlay(getApplicationContext(), map, this);
GlobalVars.getPharmacies().clear();
pharmaciesOverlay.refresh();
/* Check if we have location providers enabled */
checkLocationProviders();
/* Setup the map */
map.setBuiltInZoomControls(true);
map.setClickable(true);
map.setEnabled(true);
MapController mc = map.getController();
List<Overlay> mapOverlays = map.getOverlays();
mapOverlays.add(pharmaciesOverlay);
mapOverlays.add(myPositionOverlay);
mc.setZoom(15);
/* Center view on ourselfs on first fix */
myPositionOverlay.runOnFirstFix(new Runnable() {
@Override
public void run() {
hadFirstFix = true;
GeoPoint p = GlobalVars.positioner.getLastGeoPoint();
populatePharmacies(p);
map.getController().animateTo(p);
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), R.string.location_acquired, Toast.LENGTH_SHORT).show();
}
});
}
});
/* Create the handler for when we receive the Pharmacy list */
endThreadHandler = new Handler(new Callback() {
@Override
public boolean handleMessage(Message msg) {
if (msg.what == 0) {
pharmaciesOverlay.refresh();
map.postInvalidate();
map.setClickable(true);
if (GlobalVars.getPharmacies().size() == 0)
Toast.makeText(getApplicationContext(), R.string.not_pharmacies_returned, Toast.LENGTH_LONG).show();
} else
Toast.makeText(getApplicationContext(), R.string.error_getting_pharmacies, Toast.LENGTH_SHORT).show();
return true;
}
});
hadFirstFix = false;
}
private void checkLocationProviders() {
if (!GlobalVars.positioner.isLocalizationActive())
buildAlertMessageNoProviders();
}
private void buildAlertMessageNoProviders() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.no_localization_active)
.setCancelable(false)
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int id) {
launchGPSOptions();
}
})
.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
}
private void launchGPSOptions() {
final Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
public void onPause() {
myPositionOverlay.disableMyLocation();
GlobalVars.positioner.unregisterNotification(this);
GlobalVars.positioner.cancelUpdates();
super.onPause();
}
public void onResume() {
myPositionOverlay.enableMyLocation();
GlobalVars.positioner.registerNotification(this);
GlobalVars.positioner.requestUpdates();
super.onResume();
}
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, MENU_REFRESH, 0, R.string.menu_refresh).setIcon(R.drawable.refresh);
menu.add(0, MENU_GETPOSITIONFROMGPS, 0, R.string.menu_get_gps_pos).setIcon(R.drawable.gps);
menu.add(0, MENU_SETMYPOSITION, 0, R.string.menu_set_my_location).setIcon(android.R.drawable.ic_menu_myplaces);
menu.add(0, MENU_MYPOSITION, 0, R.string.menu_my_location).setIcon(android.R.drawable.ic_menu_mylocation);
menu.add(0, MENU_LIST, 0, R.string.menu_list).setIcon(android.R.drawable.ic_menu_agenda);
menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings).setIcon(android.R.drawable.ic_menu_preferences);
menu.add(0, MENU_ABOUT, 0, R.string.menu_about).setIcon(android.R.drawable.ic_menu_info_details);;
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.findItem(MENU_REFRESH).setEnabled(hadFirstFix);
menu.findItem(MENU_LIST).setEnabled(GlobalVars.getPharmacies().size() > 0);
if (GlobalVars.positioner.isFixedLocation()) {
menu.findItem(MENU_GETPOSITIONFROMGPS).setVisible(true);
menu.findItem(MENU_SETMYPOSITION).setVisible(false);
} else {
menu.findItem(MENU_GETPOSITIONFROMGPS).setVisible(false);
menu.findItem(MENU_SETMYPOSITION).setVisible(true);
}
menu.findItem(MENU_MYPOSITION).setEnabled(hadFirstFix);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_REFRESH:
populatePharmacies(GlobalVars.positioner.getLastGeoPoint());
return true;
case MENU_SETTINGS:
startActivity(new Intent(getApplicationContext(), MyPreferences.class));
return true;
case MENU_LIST:
Intent i = new Intent(getApplicationContext(), PharmacyList.class);
startActivity(i);
return true;
case MENU_ABOUT:
showDialog(DIALOG_ABOUT);
return true;
case MENU_SETMYPOSITION:
setMyLocation();
return true;
case MENU_GETPOSITIONFROMGPS:
getLocationFromGPS();
return true;
case MENU_MYPOSITION:
gotoMyLocation();
return true;
}
return false;
}
private void gotoMyLocation() {
map.getController().animateTo(GlobalVars.positioner.getLastGeoPoint());
}
private void setMyLocation() {
List<Overlay> mapOverlays = map.getOverlays();
pharmaciesOverlay.removeBallon();
mapOverlays.clear();
Handler h = new Handler(new Callback() {
@Override
public boolean handleMessage(Message msg) {
hadFirstFix = true;
GlobalVars.getPharmacies().clear();
pharmaciesOverlay.refresh();
populatePharmacies(GlobalVars.positioner.getLastGeoPoint());
return true;
}
});
mapOverlays.add(new GetPositionOverlay(null, pharmaciesOverlay, myPositionOverlay, h));
map.postInvalidate();
Toast.makeText(getApplicationContext(), R.string.tap_on_screen, Toast.LENGTH_LONG).show();
}
private void getLocationFromGPS() {
hadFirstFix = false;
GlobalVars.positioner.cancelFixedLocation();
GlobalVars.getPharmacies().clear();
pharmaciesOverlay.refresh();
map.postInvalidate();
Toast.makeText(getApplicationContext(), R.string.now_from_GPS, Toast.LENGTH_SHORT).show();
}
public void populatePharmacies(final GeoPoint curPos) {
Runnable r = new Runnable() {
@Override
public void run() {
String service = configs.isOnlyService() ? "GetPharmaciesAtServiceByCoordinates" : "GetPharmaciesByCoordinates";
try {
URL url = new URL("http://services.sapo.pt/Pharmacy/" + service + "?latitude=" + (curPos.getLatitudeE6() / 1E6) + "&longitude=" + (curPos.getLongitudeE6() / 1E6) + "&radius=" + configs.getRadius());
/* Get a SAXParser from the SAXPArserFactory. */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
/* Get the XMLReader of the SAXParser we created. */
XMLReader xr = sp.getXMLReader();
/* Create a new ContentHandler and apply it to the XML-Reader*/
XMLParserHandler handler = new XMLParserHandler();
xr.setContentHandler(handler);
/* Parse the xml-data from our URL. */
xr.parse(new InputSource(url.openStream()));
/* Parsing has finished. */
GlobalVars.getPharmacies().clear();
pharmaciesOverlay.refresh();
/* Check if server returned an error */
if (handler.getPharmacies() == null) {
endThreadHandler.sendEmptyMessage(1);
return;
}
/* Our XMLParserHandler now provides the parsed data to us. */
for (Pharmacy p : handler.getPharmacies())
GlobalVars.getPharmacies().add(p);
pharmaciesOverlay.refresh();
endThreadHandler.sendEmptyMessage(0);
} catch (Exception e) { endThreadHandler.sendEmptyMessage(1); }
}
};
Thread t = new Thread(r);
t.start();
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
public Dialog onCreateDialog(int id) {
Dialog d;
switch (id) {
case DIALOG_ABOUT: {
LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.about, (ViewGroup)findViewById(R.id.aboutRoot));
ImageView image = (ImageView)layout.findViewById(R.id.ImageView01);
image.setImageResource(R.drawable.icon);
Builder builder = new AlertDialog.Builder(this);
builder.setView(layout);
d = builder.create();
break;
}
default: d = null;
}
return d;
}
@Override
public void onLocationChange(GeoPoint currentPos, float accuracy) {
if (!hadFirstFix) {
map.getController().animateTo(currentPos);
populatePharmacies(currentPos);
hadFirstFix = true;
}
map.postInvalidate();
}
}

View file

@ -0,0 +1,111 @@
package org.r3pek.pharmacies;
import java.util.ArrayList;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;
public class XMLParserHandler extends DefaultHandler {
private boolean inName = false;
private boolean inAddress = false;
private boolean inZipCode = false;
private boolean inMunicipality = false;
private boolean inLatitude = false;
private boolean inLongitude = false;
private boolean inPhone = false;
private boolean inDistance = false;
private boolean inIsAtService = false;
private boolean inParish = false;
private boolean error = false;
private boolean inErrorMessage;
private String errorMessage;
private Pharmacy currentPharmacy;
private ArrayList<Pharmacy> result;
public XMLParserHandler() {
result = new ArrayList<Pharmacy>();
}
private void checkTag(String tag, boolean newValue) {
if ((tag.equals("GetPharmaciesByCoordinatesResponse") || tag.equals("GetPharmaciesAtServiceByCoordinatesResponse")) && newValue) {
if (error) error = false;
result.clear();
} else if (tag.equals("Pharmacy")) {
if (newValue)
currentPharmacy = new Pharmacy();
else
result.add(currentPharmacy);
} else if (tag.equals("Name"))
inName = newValue;
else if (tag.equals("Street"))
inAddress = newValue;
else if (tag.equals("ZipCode"))
inZipCode = newValue;
else if (tag.equals("Municipality"))
inMunicipality = newValue;
else if (tag.equals("Latitude"))
inLatitude = newValue;
else if (tag.equals("Longitude"))
inLongitude = newValue;
else if (tag.equals("Phone"))
inPhone = newValue;
else if (tag.equals("Distance"))
inDistance = newValue;
else if (tag.equals("IsAtService"))
inIsAtService = newValue;
else if (tag.equals("Parish"))
inParish = newValue;
else if (tag.equals("Fault"))
error = true;
else if (tag.equals("FaultString"))
inErrorMessage = newValue;
}
public void startElement(String uri, String name, String qName, Attributes atts) {
checkTag(name.trim(), true);
}
public void endElement(String uri, String name, String qName) {
checkTag(name.trim(), false);
}
public void characters(char ch[], int start, int length) {
String chars = (new String(ch).substring(start, start + length));
if (inName)
currentPharmacy.setName(currentPharmacy.getName() + chars);
else if (inAddress)
currentPharmacy.setAddress(currentPharmacy.getAddress() + chars);
else if (inZipCode)
currentPharmacy.setAddress(currentPharmacy.getAddress() + "\n" + chars);
else if (inMunicipality)
currentPharmacy.setAddress(currentPharmacy.getAddress() + " " + chars);
else if (inLatitude)
currentPharmacy.setLatitude(currentPharmacy.getLatitude() + chars);
else if (inLongitude)
currentPharmacy.setLongitude(currentPharmacy.getLongitude() + chars);
else if (inPhone)
currentPharmacy.setPhoneNumber(currentPharmacy.getPhoneNumber() + chars);
else if (inDistance)
currentPharmacy.setDistance(currentPharmacy.getDistance() + chars);
else if (inIsAtService)
currentPharmacy.setAtService(Boolean.parseBoolean(chars));
else if (inParish)
currentPharmacy.setParish(currentPharmacy.getParish() + chars);
else if (inErrorMessage)
errorMessage += chars;
}
public ArrayList<Pharmacy> getPharmacies() {
if (error) return null;
return result;
}
public String getErrorMessage() {
return errorMessage;
}
}

View file

@ -0,0 +1,121 @@
package org.r3pek.pharmacies.utils;
/*
* Copyright (C) 2008 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 java.io.IOException;
import java.util.List;
import org.r3pek.pharmacies.R;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import com.google.android.maps.GeoPoint;
/**
* Library for some use useful latitude/longitude math
*/
public class GeoUtils {
private static double MILLION = 1000000;
/**
* Computes the bearing in degrees between two points on Earth.
*
* @param lat1 Latitude of the first point
* @param lon1 Longitude of the first point
* @param lat2 Latitude of the second point
* @param lon2 Longitude of the second point
* @return Bearing between the two points in degrees. A value of 0 means due
* north.
*/
public static float bearing(double lat1, double lon1, double lat2, double lon2) {
double lat1Rad = Math.toRadians(lat1);
double lat2Rad = Math.toRadians(lat2);
double deltaLonRad = Math.toRadians(lon2 - lon1);
double y = Math.sin(deltaLonRad) * Math.cos(lat2Rad);
double x = Math.cos(lat1Rad) * Math.sin(lat2Rad) - Math.sin(lat1Rad) * Math.cos(lat2Rad)
* Math.cos(deltaLonRad);
return radToBearing(Math.atan2(y, x));
}
/**
* Computes the bearing in degrees between two points on Earth.
*
* @param p1 First point
* @param p2 Second point
* @return Bearing between the two points in degrees. A value of 0 means due
* north.
*/
public static float bearing(GeoPoint p1, GeoPoint p2) {
double lat1 = p1.getLatitudeE6() / MILLION;
double lon1 = p1.getLongitudeE6() / MILLION;
double lat2 = p2.getLatitudeE6() / MILLION;
double lon2 = p2.getLongitudeE6() / MILLION;
return bearing(lat1, lon1, lat2, lon2);
}
/**
* Converts an angle in radians to degrees
*/
public static float radToBearing(double rad) {
return (float) ((Math.toDegrees(rad) + 360) % 360);
}
/**
* Returns the current's location address
*/
public static void getLocationName(final Context context, final Handler h) {
Runnable r = new Runnable() {
@Override
public void run() {
Geocoder g = new Geocoder(context);
Message m = new Message();
Bundle b = new Bundle();
List<Address> addresses = null;
try {
addresses = g.getFromLocation(GlobalVars.positioner.getLastLocation().getLatitude(), GlobalVars.positioner.getLastLocation().getLongitude(), 1);
} catch (IOException e) { }
if (addresses == null || addresses.size() == 0) {
b.putString("location", context.getString(R.string.not_available));
m.setData(b);
h.dispatchMessage(m);
}
String result = "";
for (int i = 0; i <= addresses.get(0).getMaxAddressLineIndex(); i++)
result += addresses.get(0).getAddressLine(i) + ", ";
if (result.equals(""))
b.putString("location", context.getString(R.string.not_available));
else
b.putString("location", result.substring(0, result.length() - 2));
m.setData(b);
h.dispatchMessage(m);
}
};
Thread t = new Thread(r);
t.start();
}
}

View file

@ -0,0 +1,19 @@
package org.r3pek.pharmacies.utils;
import java.util.LinkedList;
import org.r3pek.pharmacies.PharmaciesOverlay;
import org.r3pek.pharmacies.Pharmacy;
public class GlobalVars {
public static Positioner positioner;
public static PharmaciesOverlay pharmaciesOverlay;
private static LinkedList<Pharmacy> pharmacies;
public synchronized static LinkedList<Pharmacy> getPharmacies() {
if (pharmacies == null)
pharmacies = new LinkedList<Pharmacy>();
return pharmacies;
}
}

View file

@ -0,0 +1,9 @@
package org.r3pek.pharmacies.utils;
import com.google.android.maps.GeoPoint;
public interface OnPositionChangeListener {
void onLocationChange(GeoPoint currentPos, float accuracy);
}

View file

@ -0,0 +1,175 @@
package org.r3pek.pharmacies.utils;
import java.util.LinkedList;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import com.google.android.maps.GeoPoint;
public class Positioner implements LocationListener {
private Location currentLocation;
private GeoPoint currentGeoPoint;
private float currentAccuracy;
private LinkedList<OnPositionChangeListener> toNotify;
private LocationManager locationManager;
private boolean isUpdating;
private boolean hadGPSFix;
private boolean isFixedLocation;
private boolean isGPSFix;
public Positioner(Context context) {
toNotify = new LinkedList<OnPositionChangeListener>();
locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
isUpdating = false;
hadGPSFix = false;
isFixedLocation = false;
isGPSFix = false;
initLocation();
requestUpdates();
}
private void initLocation() {
if (!isLocalizationActive()) return;
Location loc_net = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Location loc_gps = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location loc = loc_gps == null ? loc_net : loc_gps;
if (loc == null) return;
currentLocation = loc;
currentGeoPoint = new GeoPoint((int)(loc.getLatitude() * 1E6),
(int)(loc.getLongitude() * 1E6));
currentAccuracy = loc.getAccuracy();
notifyAllClients();
}
private String getBestLocationProvider() {
if (!isLocalizationActive())
return LocationManager.GPS_PROVIDER;
else if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
return LocationManager.GPS_PROVIDER;
else return LocationManager.NETWORK_PROVIDER;
}
public void requestUpdates() {
if (isUpdating) return;
Log.d("Positioner", "Enabling location Updates");
if (getBestLocationProvider().equals(LocationManager.GPS_PROVIDER) && !hadGPSFix)
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
locationManager.requestLocationUpdates(getBestLocationProvider(), 0, 0, this);
isUpdating = true;
}
public void cancelUpdates() {
if (!isUpdating) return;
Log.d("Positioner", "Disabling location Updates");
locationManager.removeUpdates(this);
isUpdating = false;
}
public boolean isLocalizationActive() {
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}
public void registerNotification(OnPositionChangeListener object) {
Log.d("Positioner", "Adding notification " + object.getClass().getCanonicalName());
toNotify.add(object);
}
public void unregisterNotification(OnPositionChangeListener object) {
Log.d("Positioner", "Removing notification " + object.getClass().getCanonicalName());
toNotify.remove(object);
}
public Location getLastLocation() {
return currentLocation;
}
public GeoPoint getLastGeoPoint() {
return currentGeoPoint;
}
public float getLastAccuracy() {
return currentAccuracy;
}
@Override
public synchronized void onLocationChanged(Location location) {
Log.d("Positioner", "New location arrived");
/* If we already have a GPS fix, discard Network locations */
if (hadGPSFix && location.getProvider().equals(LocationManager.NETWORK_PROVIDER))
return;
/* Save location */
currentLocation = location;
currentGeoPoint = new GeoPoint((int)(location.getLatitude() * 1E6),
(int)(location.getLongitude() * 1E6));
currentAccuracy = location.getAccuracy();
/* Did it come from GPS or Network? */
if (location.getProvider().equals(LocationManager.GPS_PROVIDER)) {
hadGPSFix = true;
isGPSFix = true;
} else {
hadGPSFix = false;
isGPSFix = false;
}
/* Notify the clients if there is any */
if (toNotify.size() > 0)
notifyAllClients();
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
Log.d("Positioner", "Provider enabled: " + provider);
initLocation();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
private synchronized void notifyAllClients() {
Log.d("Positioner", "Notifying clients");
for (OnPositionChangeListener l : toNotify)
l.onLocationChange(currentGeoPoint, currentAccuracy);
}
public synchronized void setFixedLocation(Location location) {
if (!isFixedLocation) {
cancelUpdates();
isFixedLocation = true;
}
onLocationChanged(location);
}
public void cancelFixedLocation() {
if (!isFixedLocation) return;
hadGPSFix = false;
requestUpdates();
isFixedLocation = false;
}
public boolean isFixedLocation() {
return isFixedLocation;
}
public boolean isGPSFix() {
return isGPSFix;
}
}

View file

@ -0,0 +1,97 @@
package org.r3pek.pharmacies.widgets;
import org.r3pek.pharmacies.R;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
public class Arrow extends View implements ImageOrientationSensor {
/* Orientation
* 0 -> portrait
* 1 -> landscape
* 2 -> inverted portrait
* 3 -> inverted landscape
*/
private final int orientationCompensation[] = {0, 90, 180, 270};
private Context ctx;
private float bearing;
private float orientation;
private Drawable d;
public Arrow(Context context) {
super(context);
ctx = context;
setDrawable();
}
public Arrow(Context context, AttributeSet attrs) {
super(context, attrs);
ctx = context;
setDrawable();
}
public Arrow(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
ctx = context;
setDrawable();
}
private void setDrawable() {
d = ctx.getResources().getDrawable(R.drawable.arrow);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Display display = ((WindowManager)ctx.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
float rotation = bearing - orientation - orientationCompensation[display.getOrientation()];
float keyPoints[] = {0, -9, -5, 9, 5, 9};
Matrix matrix = new Matrix();
matrix.preRotate(rotation);
matrix.mapPoints(keyPoints);
float maxX, maxY;
maxX = maxY = Float.MIN_VALUE;
float minX, minY;
minX = minY = Float.MAX_VALUE;
for (int i = 0; i < keyPoints.length; i++) {
if (i % 2 == 0) {
if (maxX < keyPoints[i]) maxX = keyPoints[i];
if (minX > keyPoints[i]) minX = keyPoints[i];
} else {
if (maxY < keyPoints[i]) maxY = keyPoints[i];
if (minY > keyPoints[i]) minY = keyPoints[i];
}
}
canvas.save();
canvas.translate((maxX + minX) / -2f, (maxY + minY) / -2f);
canvas.rotate(rotation, 10, 10);
d.setBounds(0, 0, 20, 20);
d.draw(canvas);
canvas.restore();
}
public void setBearing(float bearing) {
this.bearing = bearing;
}
public void setOrientation(float orientation) {
this.orientation = orientation;
postInvalidate();
}
}

View file

@ -0,0 +1,101 @@
package org.r3pek.pharmacies.widgets;
import org.r3pek.pharmacies.R;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
public class Compass extends View implements ImageOrientationSensor {
/* Orientation
* 0 -> portrait
* 1 -> landscape
* 2 -> inverted portrait
* 3 -> inverted landscape
*/
private final int orientationCompensation[] = {0, 90, 180, 270};
private Context ctx;
private float bearing;
private float orientation;
private Drawable needleBackgound;
private Drawable needle;
public Compass(Context context) {
super(context);
ctx = context;
setDrawable();
}
public Compass(Context context, AttributeSet attrs) {
super(context, attrs);
ctx = context;
setDrawable();
}
public Compass(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
ctx = context;
setDrawable();
}
private void setDrawable() {
needleBackgound = ctx.getResources().getDrawable(R.drawable.compass_background);
needle = ctx.getResources().getDrawable(R.drawable.compass_needle);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Display display = ((WindowManager)ctx.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
float rotation = bearing - orientation - orientationCompensation[display.getOrientation()] - 45;
float keyPoints[] = {-4, 4, 4, -4};
Matrix matrix = new Matrix();
matrix.preRotate(rotation);
matrix.mapPoints(keyPoints);
float maxX, maxY;
maxX = maxY = Float.MIN_VALUE;
float minX, minY;
minX = minY = Float.MAX_VALUE;
for (int i = 0; i < keyPoints.length; i++) {
if (i % 2 == 0) {
if (maxX < keyPoints[i]) maxX = keyPoints[i];
if (minX > keyPoints[i]) minX = keyPoints[i];
} else {
if (maxY < keyPoints[i]) maxY = keyPoints[i];
if (minY > keyPoints[i]) minY = keyPoints[i];
}
}
canvas.save();
needleBackgound.setBounds(0, 0, 15, 15);
needleBackgound.draw(canvas);
canvas.translate((maxX + minX) / -2f, (maxY + minY) / -2f);
canvas.rotate(rotation, 8, 8);
needle.setBounds(0, 0, 15, 15);
needle.draw(canvas);
canvas.restore();
}
public void setBearing(float bearing) {
this.bearing = bearing;
}
public void setOrientation(float orientation) {
this.orientation = orientation;
postInvalidate();
}
}

View file

@ -0,0 +1,5 @@
package org.r3pek.pharmacies.widgets;
public interface ImageOrientationSensor {
public void setOrientation(float orientation);
}

View file

@ -0,0 +1,48 @@
package org.r3pek.pharmacies.widgets;
import java.util.LinkedList;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.view.View;
public class ImageRotater implements SensorEventListener {
private LinkedList<ImageOrientationSensor> images = new LinkedList<ImageOrientationSensor>();
private float lastOrientation = 0;
public void addImage(ImageOrientationSensor image) {
synchronized (images) {
if (images.contains(image)) return;
images.add(image);
}
}
public void delImage(ImageOrientationSensor image) {
synchronized (images) {
images.remove(image);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public float getLastOrientation() {
return lastOrientation;
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() != Sensor.TYPE_ORIENTATION) return;
if (Math.abs(event.values[0] - lastOrientation) < 5) return;
lastOrientation = event.values[0];
synchronized (images) {
for (ImageOrientationSensor a : images) {
if (((View)a).isShown())
a.setOrientation(lastOrientation);
}
}
}
}

View file

@ -0,0 +1,147 @@
package org.r3pek.pharmacies.widgets;
import android.content.Context;
import android.graphics.Rect;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.animation.LinearInterpolator;
import android.widget.Scroller;
import android.widget.TextView;
public class ScrollTextView extends TextView {
// scrolling feature
private Scroller mSlr;
// milliseconds for a round of scrolling
private int mRndDuration = 250;
// the X offset when paused
private int mXPaused = 0;
// whether it's being paused
private boolean mPaused = true;
/*
* constructor
*/
public ScrollTextView(Context context) {
this(context, null);
}
/*
* constructor
*/
public ScrollTextView(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.textViewStyle);
}
/*
* constructor
*/
public ScrollTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// customize the TextView
setSingleLine();
setEllipsize(null);
}
/**
* begin to scroll the text from the original position
*/
public void startScroll() {
// begin from the very right side
mXPaused = -1 * getWidth();
// assume it's paused
mPaused = true;
resumeScroll();
}
/**
* resume the scroll from the pausing point
*/
public void resumeScroll() {
if (!mPaused)
return;
// Do not know why it would not scroll sometimes
// if setHorizontallyScrolling is called in constructor.
setHorizontallyScrolling(true);
// use LinearInterpolator for steady scrolling
mSlr = new Scroller(getContext(), new LinearInterpolator());
setScroller(mSlr);
int scrollingLen = calculateScrollingLen();
int distance = scrollingLen - (getWidth() + mXPaused);
int duration = (new Double(mRndDuration * distance * 1.00000
/ scrollingLen)).intValue();
setVisibility(VISIBLE);
mSlr.startScroll(mXPaused, 0, distance, 0, duration);
mPaused = false;
}
/**
* calculate the scrolling length of the text in pixel
*
* @return the scrolling length in pixels
*/
private int calculateScrollingLen() {
TextPaint tp = getPaint();
Rect rect = new Rect();
String strTxt = getText().toString();
tp.getTextBounds(strTxt, 0, strTxt.length(), rect);
int scrollingLen = rect.width() + getWidth();
rect = null;
return scrollingLen;
}
/**
* pause scrolling the text
*/
public void pauseScroll() {
if (null == mSlr)
return;
if (mPaused)
return;
mPaused = true;
// abortAnimation sets the current X to be the final X,
// and sets isFinished to be true
// so current position shall be saved
mXPaused = mSlr.getCurrX();
mSlr.abortAnimation();
}
@Override
/*
* override the computeScroll to restart scrolling when finished so as that
* the text is scrolled forever
*/
public void computeScroll() {
super.computeScroll();
if (null == mSlr) return;
if (mSlr.isFinished() && (!mPaused)) {
this.startScroll();
}
}
public int getRndDuration() {
return mRndDuration;
}
public void setRndDuration(int duration) {
this.mRndDuration = duration;
}
public boolean isPaused() {
return mPaused;
}
}

View file

@ -0,0 +1,103 @@
package org.r3pek.pharmacies.widgets;
/* The following code was written by Matthew Wiggins
* and is released under the APACHE 2.0 license
*
* http://www.apache.org/licenses/LICENSE-2.0
*/
import android.content.Context;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
public class SeekBarPreference extends DialogPreference implements SeekBar.OnSeekBarChangeListener {
private static final String androidns="http://schemas.android.com/apk/res/android";
private SeekBar mSeekBar;
private TextView mSplashText,mValueText;
private Context mContext;
private String mDialogMessage, mSuffix;
private int mDefault, mMax, mValue, mMin = 0;
public SeekBarPreference(Context context, AttributeSet attrs) {
super(context,attrs);
mContext = context;
mDialogMessage = attrs.getAttributeValue(androidns,"dialogMessage");
mSuffix = attrs.getAttributeValue(androidns,"text");
mDefault = attrs.getAttributeIntValue(androidns,"defaultValue", 0);
mMax = attrs.getAttributeIntValue(androidns,"max", 100);
mMin = attrs.getAttributeIntValue(androidns,"min", 0);
}
@Override
protected View onCreateDialogView() {
LinearLayout.LayoutParams params;
LinearLayout layout = new LinearLayout(mContext);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setPadding(6,6,6,6);
mSplashText = new TextView(mContext);
if (mDialogMessage != null)
mSplashText.setText(mDialogMessage);
layout.addView(mSplashText);
mValueText = new TextView(mContext);
mValueText.setGravity(Gravity.CENTER_HORIZONTAL);
mValueText.setTextSize(32);
params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layout.addView(mValueText, params);
mSeekBar = new SeekBar(mContext);
mSeekBar.setOnSeekBarChangeListener(this);
layout.addView(mSeekBar, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
if (shouldPersist())
mValue = getPersistedInt(mDefault);
mSeekBar.setMax(mMax - mMin);
mSeekBar.setProgress(mValue - mMin);
return layout;
}
@Override
protected void onBindDialogView(View v) {
super.onBindDialogView(v);
mSeekBar.setMax(mMax - mMin);
mSeekBar.setProgress(mValue - mMin);
String t = String.valueOf(mValue);
mValueText.setText(mSuffix == null ? t : t.concat(mSuffix));
}
@Override
protected void onSetInitialValue(boolean restore, Object defaultValue) {
super.onSetInitialValue(restore, defaultValue);
if (restore)
mValue = shouldPersist() ? getPersistedInt(mDefault) : 0;
else
mValue = (Integer)defaultValue;
}
public void onProgressChanged(SeekBar seek, int value, boolean fromTouch) {
String t = String.valueOf(value + mMin);
mValueText.setText(mSuffix == null ? t : t.concat(mSuffix));
if (shouldPersist())
persistInt(value + mMin);
callChangeListener(new Integer(value));
}
public void onStartTrackingTouch(SeekBar seek) {}
public void onStopTrackingTouch(SeekBar seek) {}
public void setMin(int min) {
mMin = min;
}
}