Skip to content

Commit

Permalink
Merge pull request #79 from RoadXY/master
Browse files Browse the repository at this point in the history
(RoadXY) Geolocation - address implementation
  • Loading branch information
galadril committed Dec 11, 2015
2 parents a080c7d + 1e2bc7d commit 4d6d06f
Show file tree
Hide file tree
Showing 9 changed files with 389 additions and 142 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ android {
compileSdkVersion 'Google Inc.:Google APIs:23'
buildToolsVersion "23.0.1"
defaultConfig {
multiDexEnabled true
applicationId 'nl.hnogames.domoticz'
minSdkVersion 17
targetSdkVersion 23
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -33,31 +34,34 @@
import android.widget.CompoundButton;
import android.widget.TextView;

import com.google.android.gms.maps.model.LatLng;

import java.util.ArrayList;

import nl.hnogames.domoticz.Containers.LocationInfo;
import nl.hnogames.domoticz.Interfaces.LocationClickListener;
import nl.hnogames.domoticz.R;
import nl.hnogames.domoticz.Utils.SharedPrefUtil;
import nl.hnogames.domoticz.Utils.GeoUtil;

// Example used: http://www.ezzylearning.com/tutorial/customizing-android-listview-items-with-custom-arrayadapter
// And: http://www.survivingwithandroid.com/2013/02/android-listview-adapter-checkbox-item_7.html
public class LocationAdapter extends BaseAdapter {

@SuppressWarnings("unused")
private static final String TAG = LocationAdapter.class.getSimpleName();
private GeoUtil mGeoUtil;
public ArrayList<LocationInfo> data = null;
private Context context;
private SharedPrefUtil prefs;

private LocationClickListener listener;

public LocationAdapter(Context context,
ArrayList<LocationInfo> data,
LocationClickListener l) {
super();

mGeoUtil = new GeoUtil(context);

this.context = context;
this.data = data;
prefs = new SharedPrefUtil(context);
this.listener = l;
}

Expand Down Expand Up @@ -95,20 +99,40 @@ public View getView(int position, View convertView, ViewGroup parent) {
holder.enable = (CheckBox) convertView.findViewById(R.id.enableSwitch);
holder.name = (TextView) convertView.findViewById(R.id.location_name);
holder.radius = (TextView) convertView.findViewById(R.id.location_radius);
holder.longitude = (TextView) convertView.findViewById(R.id.location_longitude);
holder.latitude = (TextView) convertView.findViewById(R.id.location_latitude);
holder.connectedSwitch = (TextView) convertView.findViewById(R.id.location_connectedswitch);
holder.country = (TextView) convertView.findViewById(R.id.location_country);
holder.address = (TextView) convertView.findViewById(R.id.location_address);
holder.connectedSwitch = (TextView) convertView.findViewById(R.id.location_connectedSwitch);
holder.remove = (Button) convertView.findViewById(R.id.remove_button);

holder.name.setText(mLocationInfo.getName());
holder.latitude.setText(context.getString(R.string.latitude) + ": " + mLocationInfo.getLocation().latitude);
holder.longitude.setText(context.getString(R.string.longitude) + ": " + mLocationInfo.getLocation().longitude);
holder.radius.setText(context.getString(R.string.radius) + ": " + mLocationInfo.getRange());
Address address = mGeoUtil.getAddressFromLatLng(
new LatLng(mLocationInfo.getLocation().latitude, mLocationInfo.getLocation().longitude));

if (mLocationInfo.getSwitchidx() > 0)
holder.connectedSwitch.setText(context.getString(R.string.connectedswitch) + ": " + mLocationInfo.getSwitchidx());
else
holder.connectedSwitch.setText(context.getString(R.string.connectedswitch) + ": N/A");
String addressString;
String countryString;

if (address != null) {
addressString = address.getAddressLine(0) + ", " + address.getLocality();
countryString = address.getCountryName();
} else {
addressString = context.getString(R.string.unknown);
countryString = context.getString(R.string.unknown);
}

holder.name.setText(mLocationInfo.getName());
holder.address.setText(addressString);
holder.country.setText(countryString);
String text = context.getString(R.string.radius) + ": " + mLocationInfo.getRadius();
holder.radius.setText(text);

if (mLocationInfo.getSwitchidx() > 0) {
text = context.getString(R.string.connectedswitch) + ": " + mLocationInfo.getSwitchidx();
holder.connectedSwitch.setText(text);
}
else {
text = context.getString(R.string.connectedswitch)
+ ": " + context.getString(R.string.not_available);
holder.connectedSwitch.setText(text);
}

holder.remove.setId(mLocationInfo.getID());
holder.remove.setOnClickListener(new View.OnClickListener() {
Expand Down Expand Up @@ -147,9 +171,9 @@ private void handleEnableChanged(LocationInfo location, boolean enabled) {

static class ViewHolder {
TextView name;
TextView latitude;
TextView address;
TextView radius;
TextView longitude;
TextView country;
TextView connectedSwitch;
CheckBox enable;
Button remove;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ public class LocationInfo {
LatLng Location;
int id = 0;
int switchidx = 0;
int range = 400;//meters
int radius = 400; //meters
boolean enabled = false;


public LocationInfo(int i, String n, LatLng l, int radius) {
this.Name = n;
this.Location = l;
this.id = i;
this.range = range;
this.radius = radius;
}


Expand Down Expand Up @@ -66,12 +66,12 @@ public void setSwitchidx(int idx) {
switchidx = idx;
}

public int getRange() {
return range;
public int getRadius() {
return radius;
}

public void setRange(int e) {
range = e;
public void setRadius(int e) {
radius = e;
}

public LatLng getLocation() {
Expand All @@ -90,7 +90,7 @@ public Geofence toGeofence() {
.setRequestId(String.valueOf(id))
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER |
Geofence.GEOFENCE_TRANSITION_EXIT)
.setCircularRegion(Location.latitude, Location.longitude, range)
.setCircularRegion(Location.latitude, Location.longitude, radius)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.build();
}
Expand Down
20 changes: 16 additions & 4 deletions app/src/main/java/nl/hnogames/domoticz/GeoSettingsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ public class GeoSettingsActivity extends AppCompatActivity

private CoordinatorLayout coordinatorLayout;
private Location currectLocation;
private String usedLocationService;
private LocationRequest mLocationRequest;

@Override
Expand All @@ -106,7 +105,9 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
// The user picked a place.
Place place = PlacePicker.getPlace(data, this);
Snackbar.make(coordinatorLayout, String.format(getString(R.string.geofence_place), place.getName()), Snackbar.LENGTH_SHORT).show();
Snackbar.make(coordinatorLayout,
String.format(getString(R.string.geofence_place), place.getName()),
Snackbar.LENGTH_SHORT).show();
}
}
}
Expand Down Expand Up @@ -179,8 +180,11 @@ protected void onResume() {

mApiClient.connect();
}
if (map == null)
if (map == null) {
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
map.getUiSettings().setMapToolbarEnabled(true);
map.getUiSettings().setTiltGesturesEnabled(false);
}

}

Expand Down Expand Up @@ -243,10 +247,15 @@ public void onClick(DialogInterface dialog, int which) {

ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
setMarker(locations.get(position).getLocation());
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
setMarker(locations.get(position).getLocation());
domoticz.getSwitches(new SwitchesReceiver() {
@Override
public void onReceiveSwitches(ArrayList<SwitchInfo> switches) {
Expand All @@ -255,6 +264,9 @@ public void onReceiveSwitches(ArrayList<SwitchInfo> switches) {

@Override
public void onError(Exception error) {
Snackbar.make(coordinatorLayout,
R.string.unable_to_get_switches,
Snackbar.LENGTH_SHORT).show();
}
});
return false;
Expand Down
Loading

0 comments on commit 4d6d06f

Please sign in to comment.