From d173822486eb6f2942e4b6dbf8bbd3ad58157621 Mon Sep 17 00:00:00 2001 From: unknown <leo112071@gmail.com> Date: Fri, 23 Feb 2018 16:17:39 +0700 Subject: [PATCH] Perbaikan di joggin activity --- app/src/main/AndroidManifest.xml | 54 +++++---- .../leo/fitnessdiy/JoggingActivity.java | 109 +++++++++++++----- .../example/leo/fitnessdiy/RunActivity.java | 64 ++++++++++ app/src/main/res/drawable/round_button.xml | 6 + app/src/main/res/layout/activity_jogging.xml | 43 ++++--- app/src/main/res/layout/activity_run.xml | 13 +++ app/src/main/res/values/strings.xml | 3 + 7 files changed, 230 insertions(+), 62 deletions(-) create mode 100644 app/src/main/java/com/example/leo/fitnessdiy/RunActivity.java create mode 100644 app/src/main/res/drawable/round_button.xml create mode 100644 app/src/main/res/layout/activity_run.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 40a985f..d67ba61 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -28,7 +28,8 @@ android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> - <activity android:name=".MainActivity" + <activity + android:name=".MainActivity" android:theme="@style/AppThemeNoTitle"> <intent-filter> <action android:name="android.intent.action.MAIN" /> @@ -36,26 +37,36 @@ <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> - <activity android:name=".LoginActivity" - android:theme="@style/AppThemeNoTitle"/> - <activity android:name=".RegistrationActivity" - android:theme="@style/AppThemeNoTitle"/> - <activity android:name=".HIstoryActivity" - android:theme="@style/AppTheme"/> - <activity android:name=".HomeActivity" - android:theme="@style/AppTheme"/> - <activity android:name=".PlankActivity" - android:theme="@style/AppTheme"/> - <activity android:name=".PushupActivity" - android:theme="@style/AppTheme"/> - <activity android:name=".SitupActivity" - android:theme="@style/AppTheme"/> - <activity android:name=".ChatActivity" - android:theme="@style/AppTheme"/> - <activity android:name=".LevelActivity" - android:theme="@style/AppTheme"/> - <activity android:name=".ExerciseActivity" - android:theme="@style/AppTheme"/> + <activity + android:name=".LoginActivity" + android:theme="@style/AppThemeNoTitle" /> + <activity + android:name=".RegistrationActivity" + android:theme="@style/AppThemeNoTitle" /> + <activity + android:name=".HIstoryActivity" + android:theme="@style/AppTheme" /> + <activity + android:name=".HomeActivity" + android:theme="@style/AppTheme" /> + <activity + android:name=".PlankActivity" + android:theme="@style/AppTheme" /> + <activity + android:name=".PushupActivity" + android:theme="@style/AppTheme" /> + <activity + android:name=".SitupActivity" + android:theme="@style/AppTheme" /> + <activity + android:name=".ChatActivity" + android:theme="@style/AppTheme" /> + <activity + android:name=".LevelActivity" + android:theme="@style/AppTheme" /> + <activity + android:name=".ExerciseActivity" + android:theme="@style/AppTheme" /> <!-- The API key for Google Maps-based APIs is defined as a string resource. (See the file "res/values/google_maps_api.xml"). @@ -72,6 +83,7 @@ android:name=".JoggingActivity" android:label="@string/title_activity_jogging" /> <activity android:name=".SettingActivity" /> + <activity android:name=".RunActivity"></activity> </application> </manifest> \ No newline at end of file diff --git a/app/src/main/java/com/example/leo/fitnessdiy/JoggingActivity.java b/app/src/main/java/com/example/leo/fitnessdiy/JoggingActivity.java index e2e8276..51384d2 100644 --- a/app/src/main/java/com/example/leo/fitnessdiy/JoggingActivity.java +++ b/app/src/main/java/com/example/leo/fitnessdiy/JoggingActivity.java @@ -11,6 +11,7 @@ import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.location.Address; +import android.location.Criteria; import android.location.Geocoder; import android.location.Location; import android.location.LocationManager; @@ -24,12 +25,14 @@ import android.support.v4.app.ActivityCompat; import android.support.v4.app.FragmentActivity; import android.os.Bundle; import android.support.v4.content.ContextCompat; +import android.support.v7.widget.CardView; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.FrameLayout; +import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; @@ -40,8 +43,10 @@ import com.example.leo.fitnessdiy.routes.api; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.api.GoogleApiClient; import com.google.android.gms.location.FusedLocationProviderClient; +import com.google.android.gms.location.LocationCallback; import com.google.android.gms.location.LocationListener; import com.google.android.gms.location.LocationRequest; +import com.google.android.gms.location.LocationResult; import com.google.android.gms.location.LocationServices; import com.google.android.gms.location.places.GeoDataClient; import com.google.android.gms.location.places.PlaceDetectionClient; @@ -93,13 +98,15 @@ public class JoggingActivity extends FragmentActivity implements OnMapReadyCallb // A default location (Sydney, Australia) and default zoom to use when location permission is // not granted. private final LatLng mDefaultLocation = new LatLng(-33.8523341, 151.2106085); - private static final int DEFAULT_ZOOM = 25; + private static final int DEFAULT_ZOOM = 17; private static final int PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1; private boolean mLocationPermissionGranted; // The geographical location where the device is currently located. That is, the last-known // location retrieved by the Fused Location Provider. private Location mLastKnownLocation; + private Location startLocation; + private Location endLocation; // Keys for storing activity state. private static final String KEY_CAMERA_POSITION = "camera_position"; @@ -118,6 +125,9 @@ public class JoggingActivity extends FragmentActivity implements OnMapReadyCallb SensorManager sensorManager; Sensor stepSensor; private double distance = 0; + private CardView info; + private LocationCallback mLocationCallback; + LocationRequest mLocationRequest; @Override protected void onCreate(Bundle savedInstanceState) { @@ -137,16 +147,29 @@ public class JoggingActivity extends FragmentActivity implements OnMapReadyCallb mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this); - // Obtain the SupportMapFragment and get notified when the map is ready to be used. - SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() - .findFragmentById(R.id.map); - mapFragment.getMapAsync(this); - infoJoggingStart = (TextView) findViewById(R.id.tv_info_start); infoJoggingEnd = (TextView) findViewById(R.id.tv_info_end); mButton = (Button) findViewById(R.id.control_button); + + mLocationCallback = new LocationCallback() { + @Override + public void onLocationResult(LocationResult locationResult) { + for (Location location : locationResult.getLocations()) { + distance += PositionUtils.distance(mLastKnownLocation.getLatitude(), + mLastKnownLocation.getLongitude(), + location.getLatitude(), + location.getLongitude()); + } + } + }; + + // Obtain the SupportMapFragment and get notified when the map is ready to be used. + SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() + .findFragmentById(R.id.map); + mapFragment.getMapAsync(this); + } @Override @@ -197,6 +220,8 @@ public class JoggingActivity extends FragmentActivity implements OnMapReadyCallb updateLocationUI(); getDeviceLocation(); + +// mMap.addMarker(new MarkerOptions()) } private void getLocationPermission() { @@ -221,8 +246,7 @@ public class JoggingActivity extends FragmentActivity implements OnMapReadyCallb mLastKnownLocation = task.getResult(); LatLng position = new LatLng(mLastKnownLocation.getLatitude(), mLastKnownLocation.getLongitude()); -// mMap.addMarker( -// new MarkerOptions().position(position).title("Your Position")); + mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(position, DEFAULT_ZOOM)); } else { @@ -233,12 +257,14 @@ public class JoggingActivity extends FragmentActivity implements OnMapReadyCallb DEFAULT_ZOOM)); mMap.getUiSettings().setMyLocationButtonEnabled(false); } + Log.d(TAG, getCurrentLocation(mLastKnownLocation)); } }); } } catch (SecurityException e) { e.printStackTrace(); } + } @@ -261,10 +287,6 @@ public class JoggingActivity extends FragmentActivity implements OnMapReadyCallb public void onLocationChanged(Location location) { mMap.clear(); - distance += PositionUtils.distance(mLastKnownLocation.getLatitude(), - mLastKnownLocation.getLongitude(), - location.getLatitude(), - location.getLongitude()); MarkerOptions mp = new MarkerOptions(); mp.position(new LatLng(location.getLatitude(), location.getLongitude())); @@ -321,6 +343,7 @@ public class JoggingActivity extends FragmentActivity implements OnMapReadyCallb } private void updateLocationUI() { + Log.d(TAG, "Updating Location UI"); if (mMap == null) { return; } @@ -428,15 +451,15 @@ public class JoggingActivity extends FragmentActivity implements OnMapReadyCallb .show(); } - public String getCurrentLocation() { + public String getCurrentLocation(Location location) { String loc = ""; Geocoder geocoder = new Geocoder(this, Locale.getDefault()); List<Address> addresses = null; String errorMessage = ""; try { - addresses = geocoder.getFromLocation(mLastKnownLocation.getLatitude(), - mLastKnownLocation.getLongitude(), 1); + addresses = geocoder.getFromLocation(location.getLatitude(), + location.getLongitude(), 1); loc = addresses.get(0).getAddressLine(0); } catch (IOException e) { @@ -445,15 +468,28 @@ public class JoggingActivity extends FragmentActivity implements OnMapReadyCallb errorMessage = "using invalid latitude and longitude data"; Log.e(TAG, errorMessage + ". " + - "Latitude = " + mLastKnownLocation.getLatitude() + + "Latitude = " + location.getLatitude() + ", Longitude = " + - mLastKnownLocation.getLongitude(), e); + location.getLongitude(), e); } return loc; } + + protected void createLocationRequest() { + mLocationRequest = new LocationRequest(); + mLocationRequest.setInterval(10000); + mLocationRequest.setFastestInterval(5000); + mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); + } + public void startJogging(View view) { - infoJoggingStart.setText(getCurrentLocation()); + info = (CardView) findViewById(R.id.tv_info); + getDeviceLocation(); + startLocation = mLastKnownLocation; + info.setVisibility(View.VISIBLE); + infoJoggingStart.setText(getCurrentLocation(startLocation)); + infoJoggingEnd.setText(""); mButton.setText("STOP"); mButton.setOnClickListener(new View.OnClickListener() { @Override @@ -465,10 +501,25 @@ public class JoggingActivity extends FragmentActivity implements OnMapReadyCallb startTime = new SimpleDateFormat("HH:mm:ss") .format(Calendar.getInstance().getTime()); distance = 0; + try { + createLocationRequest(); + + if (mLocationPermissionGranted) { + mFusedLocationProviderClient.requestLocationUpdates(mLocationRequest, + mLocationCallback, + null /* Looper */); + } + } catch (SecurityException e) { + e.printStackTrace(); + } + } public void stopJogging(View view) { - infoJoggingEnd.setText(getCurrentLocation()); + + getDeviceLocation(); + endLocation = mLastKnownLocation; + infoJoggingEnd.setText(getCurrentLocation(endLocation)); mButton.setText("START"); mButton.setOnClickListener(new View.OnClickListener() { @Override @@ -516,13 +567,17 @@ public class JoggingActivity extends FragmentActivity implements OnMapReadyCallb } catch (IOException e) { e.printStackTrace(); } - } -// private class AddNewJogging extends AsyncTask<Void, Void, Void> { -// -// @Override -// protected Void doInBackground(Void... voids) { -// -// } -// } + //Add Marker start and end Position + mMap.addMarker(new MarkerOptions().title("Start") + .position(new LatLng(startLocation.getLatitude(), startLocation.getLongitude())) + .snippet("You start from Here")); + + mMap.addMarker(new MarkerOptions().title("End") + .position(new LatLng(endLocation.getLatitude(), endLocation.getLongitude())) + .snippet("You're done Here")); + + mFusedLocationProviderClient.removeLocationUpdates(mLocationCallback); + + } } diff --git a/app/src/main/java/com/example/leo/fitnessdiy/RunActivity.java b/app/src/main/java/com/example/leo/fitnessdiy/RunActivity.java new file mode 100644 index 0000000..3217df6 --- /dev/null +++ b/app/src/main/java/com/example/leo/fitnessdiy/RunActivity.java @@ -0,0 +1,64 @@ +package com.example.leo.fitnessdiy; + +import android.*; +import android.content.pm.PackageManager; +import android.location.Location; +import android.support.v4.app.ActivityCompat; +import android.support.v4.content.ContextCompat; +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; +import android.view.View; +import android.widget.Toast; + +import com.google.android.gms.location.FusedLocationProviderClient; +import com.google.android.gms.location.LocationServices; +import com.google.android.gms.tasks.OnSuccessListener; + +public class RunActivity extends AppCompatActivity { + private static final int PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1; + private FusedLocationProviderClient fusedLocationProviderClient; + private boolean mLocationPermissionGranted; + + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_run); + + fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this); + getLocationPermission(); + } + + private void getLocationPermission() { + if (ContextCompat.checkSelfPermission(this.getApplicationContext(), + android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { + mLocationPermissionGranted = true; + } else { + ActivityCompat.requestPermissions(this, + new String[] {android.Manifest.permission.ACCESS_FINE_LOCATION}, + PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION); + } + } + + public void startJogging(View view) { + try { + if (mLocationPermissionGranted) { + fusedLocationProviderClient.getLastLocation() + .addOnSuccessListener(this, new OnSuccessListener<Location>() { + @Override + public void onSuccess(Location location) { + if (location != null) { + Toast.makeText(RunActivity.this, + "Can't Find Your Location", + Toast.LENGTH_SHORT).show(); + } else { + + } + } + }); + } + } catch (SecurityException e) { + e.printStackTrace(); + } + } +} diff --git a/app/src/main/res/drawable/round_button.xml b/app/src/main/res/drawable/round_button.xml new file mode 100644 index 0000000..e062d0e --- /dev/null +++ b/app/src/main/res/drawable/round_button.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="utf-8"?> +<shape xmlns:android="http://schemas.android.com/apk/res/android" + android:shape="oval"> + <size android:width="150dp" android:height="150dp"/> + <solid android:color="#4cff00"/> +</shape> \ No newline at end of file diff --git a/app/src/main/res/layout/activity_jogging.xml b/app/src/main/res/layout/activity_jogging.xml index 9c9f270..2b464c2 100644 --- a/app/src/main/res/layout/activity_jogging.xml +++ b/app/src/main/res/layout/activity_jogging.xml @@ -20,37 +20,52 @@ android:id="@+id/control_button" android:layout_width="match_parent" android:layout_height="wrap_content" - android:padding="12dp" - android:text="@string/start" - android:textSize="24dp" - android:layout_gravity="bottom" + android:layout_centerHorizontal="true" + android:layout_marginBottom="16dp" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" - android:layout_marginBottom="16dp" + android:layout_gravity="bottom" + android:background="#4cff00" android:onClick="startJogging" + android:padding="12dp" + android:text="@string/start" android:textColor="#ffffff" - android:background="#4cff00"/> + android:textSize="24dp" /> <android.support.v7.widget.CardView - android:layout_width="match_parent" + android:layout_width="200dp" android:layout_height="wrap_content" - android:id="@+id/tv_info" android:layout_gravity="top" - android:layout_marginLeft="16dp" - android:layout_marginRight="16dp" - android:layout_marginTop="16dp"> + android:id="@+id/tv_info" + android:padding="16dp" + android:layout_below="@id/control_button" + android:visibility="gone"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" - android:orientation="vertical"> + android:orientation="vertical" + android:padding="16dp"> + <TextView + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:text="@string/start_run" + style="@style/TextAppearance.AppCompat.Title" + android:layout_marginBottom="8dp"/> + <TextView + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:id="@+id/tv_info_start" + android:layout_marginBottom="16dp"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" - android:id="@+id/tv_info_start"/> + android:text="@string/end_run" + style="@style/TextAppearance.AppCompat.Title" + android:layout_marginBottom="8dp"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" - android:id="@+id/tv_info_end"/> + android:id="@+id/tv_info_end" /> </LinearLayout> </android.support.v7.widget.CardView> diff --git a/app/src/main/res/layout/activity_run.xml b/app/src/main/res/layout/activity_run.xml new file mode 100644 index 0000000..8c044a7 --- /dev/null +++ b/app/src/main/res/layout/activity_run.xml @@ -0,0 +1,13 @@ +<?xml version="1.0" encoding="utf-8"?> +<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:app="http://schemas.android.com/apk/res-auto" + xmlns:tools="http://schemas.android.com/tools" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:orientation="vertical" + android:padding="16dp" + tools:context="com.example.leo.fitnessdiy.RunActivity"> + + + +</RelativeLayout> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e85a56c..db2bc10 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -37,4 +37,7 @@ <string name="plank_demo">How to Plank</string> <string name="motivation_plank">Benefit of Plank</string> <string name="register_req">Don\'t Have Account?</string> + <string name="kosong">...</string> + <string name="start_run">Starting Point</string> + <string name="end_run">End Point</string> </resources> -- GitLab