Skip to content

Commit

Permalink
Merge pull request #4 from Ville2000/develop
Browse files Browse the repository at this point in the history
Merge develop to master for release 2
  • Loading branch information
Ville Haapavaara authored Apr 26, 2017
2 parents 11d08ac + 8be6722 commit da2e135
Show file tree
Hide file tree
Showing 18 changed files with 347 additions and 116 deletions.
3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ dependencies {
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.google.android.gms:play-services:10.2.1'

// https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.0.pr2'


compile 'com.google.android.gms:play-services:10.2.1'
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:design:24.2.1'
compile 'com.google.android.gms:play-services-maps:10.2.1'
testCompile 'junit:junit:4.12'
}
11 changes: 8 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,21 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Activities.MapActivity"></activity>
<activity android:name=".Activities.MapActivity" />

<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyBALAux7Qo8b5N8ud8gkR3QBvzoCC2uoqU"/>
android:value="AIzaSyBALAux7Qo8b5N8ud8gkR3QBvzoCC2uoqU" />

<service
android:name=".Services.LocationService"
android:enabled="true"
android:exported="true" />

<activity android:name=".Activities.StoredMapActivity"></activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private HikeList hikes;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -49,10 +50,6 @@ public void startTracking(View v) {
startActivity(i);
}

public void stopTracking(View v) {
System.out.println("stopTracking");
}

private void setupView() {
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Expand All @@ -70,4 +67,8 @@ private void setupView() {
adapter.addFragment(new HikePlansFragment(), "Hike Plans");
viewPager.setAdapter(adapter);
}

public HikeList getHikes() {
return hikes;
}
}
121 changes: 79 additions & 42 deletions app/src/main/java/ville/fi/hikemate/Activities/MapActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
Expand All @@ -10,16 +11,24 @@
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;

import java.util.LinkedList;

import ville.fi.hikemate.R;
import ville.fi.hikemate.Resources.Hike;
import ville.fi.hikemate.Resources.HikeList;
import ville.fi.hikemate.Resources.HikeLocation;
import ville.fi.hikemate.Utils.Debug;
import ville.fi.hikemate.Utils.StorageHandler;

public class MapActivity extends FragmentActivity implements OnMapReadyCallback{

Expand All @@ -30,28 +39,38 @@ public class MapActivity extends FragmentActivity implements OnMapReadyCallback{
private LocationManager locationManager;
private LocationListener locationListener;
private Location location;
private LinkedList<Hike> hikes;
private Hike hike;
private StorageHandler sh;
private Polyline hikePolyLine;
private boolean firstLocationGot = false;
private boolean isTrackingLocation;
private Intent mainActivityIntent;
private LinkedList<LatLng> hikeLatLng;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);

mainActivityIntent = new Intent(host, MainActivity.class);
isTrackingLocation = true;
sh = new StorageHandler();
hikes = sh.readStorage(host);
hike = new Hike("MyHike");
hikeLatLng = getLatLng(hike);

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);

locationManager = (LocationManager) host.getSystemService(Context.LOCATION_SERVICE);
initLocationListener();
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(host,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {

System.out.println("Asking permissions, no permissions");
Debug.print(host, "Asking permissions", "Requesting permission", "No permissions, dialog", 1);

// Should we show an explanation?
Expand All @@ -63,42 +82,53 @@ protected void onCreate(Bundle savedInstanceState) {
// sees the explanation, try again to request the permission.

} else {

// No explanation needed, we can request the permission.

ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_LOCATION);

// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
} else {

ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_LOCATION);

/*
Debug.print(host, "Asking permissions", "Requesting permission", "We have a permission", 1);
try {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
Debug.print(host, "Asking permissions", "Requesting permission", "Location Manager Requesting", 1);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 1, locationListener);
Debug.print(host, "onRequestPermissionResult", "LocationGranted", "LocationManager requesting", 1);
} catch(SecurityException e) {
e.printStackTrace();
}
*/
}
}

/**
* Returns the user to the main activity view.
*
* @param v
*/
public void cancelTracking(View v) {
isTrackingLocation = false;
startActivity(mainActivityIntent);
}

/**
* Saves the hike.
*
* Saves the hike to the storage and returns the user to the main activity.
*
* @param v
*/
public void saveHike(View v) {
isTrackingLocation = false;

for (LatLng latLng : hikeLatLng) {
hike.addLocation(latLng.latitude, latLng.longitude);
}

hikes.add(hike);
sh.writeStorage(host, hikes);
startActivity(mainActivityIntent);
}

@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(), location.getLongitude())));
}

@Override
Expand All @@ -109,12 +139,8 @@ public void onRequestPermissionsResult(int requestCode,
case MY_PERMISSIONS_REQUEST_LOCATION: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {

// permission was granted, yay! Do the
// contacts-related task you need to do.

try {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 1, locationListener);
Debug.print(host, "onRequestPermissionResult", "LocationGranted", "LocationManager requesting", 1);
} catch(SecurityException e) {
e.printStackTrace();
Expand All @@ -124,12 +150,10 @@ public void onRequestPermissionsResult(int requestCode,

// permission denied, boo! Disable the
// functionality that depends on this permission.
// TODO: Go back to MainActivity.
}
return;
}

// other 'case' lines to check for other
// permissions this app might request
}
}

Expand All @@ -139,32 +163,45 @@ private void initLocationListener() {
@Override
public void onLocationChanged(Location location) {
Debug.print(host, "onLocationChanged", "Changing location", "Location changed", 1);
System.out.println("Latitude: " + location.getLatitude() + ", Longitude: " + location.getLongitude());
location.setLatitude(location.getLatitude());
location.setLongitude(location.getLongitude());
mMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(), location.getLongitude())));

hike.addLocation(location.getLatitude(), location.getLongitude());
}
if (isTrackingLocation) {
hikeLatLng.add(new LatLng(location.getLatitude(), location.getLongitude()));

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
hikePolyLine = mMap.addPolyline(new PolylineOptions()
.clickable(false));
hikePolyLine.setPoints(hikeLatLng);

if (firstLocationGot == false) {
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), 15));
firstLocationGot = true;
} else {
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(), location.getLongitude()), mMap.getCameraPosition().zoom));
}
}
}

@Override
public void onProviderEnabled(String provider) {

}
public void onStatusChanged(String provider, int status, Bundle extras) {}

@Override
public void onProviderDisabled(String provider) {
public void onProviderEnabled(String provider) {}

}
@Override
public void onProviderDisabled(String provider) {}
};
}

public Hike getHike() {
return hike;
}

public LinkedList<LatLng> getLatLng(Hike hike) {
LinkedList<LatLng> locs = new LinkedList<>();

for (HikeLocation loc: hike.getLocations()) {
locs.add(new LatLng(loc.getLat(), loc.getLng()));
}

return locs;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package ville.fi.hikemate.Activities;

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;

import java.util.LinkedList;

import ville.fi.hikemate.R;
import ville.fi.hikemate.Resources.Hike;
import ville.fi.hikemate.Resources.HikeLocation;
import ville.fi.hikemate.Utils.HikeToLatLng;
import ville.fi.hikemate.Utils.StorageHandler;

public class StoredMapActivity extends FragmentActivity implements OnMapReadyCallback {

private Context host = this;
private GoogleMap mMap;
private LinkedList<Hike> hikes;
private LinkedList<LatLng> hike;
private StorageHandler sh;
private Polyline hikePolyLine;

/**
* Initializes this activity's attributes.
*
* @param savedInstanceState
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stored_map);

sh = new StorageHandler();

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.stored_map);
mapFragment.getMapAsync(this);

hikes = sh.readStorage(host);
hike = HikeToLatLng.getLatLng(hikes.get((int) getIntent().getExtras().get("position")));
}

@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;

hikePolyLine = mMap.addPolyline(new PolylineOptions()
.clickable(false));
hikePolyLine.setPoints(hike);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(hike.get(0).latitude, hike.get(0).longitude), 15));
}

public LinkedList<LatLng> getLatLng(Hike hike) {
LinkedList<LatLng> locs = new LinkedList<>();

for (HikeLocation loc: hike.getLocations()) {
locs.add(new LatLng(loc.getLat(), loc.getLng()));
}

return locs;
}
}
Loading

0 comments on commit da2e135

Please sign in to comment.