Skip to content

Commit

Permalink
Merge pull request #48 from CSE-110-Winter-2023/US-12b
Browse files Browse the repository at this point in the history
Us 12b
  • Loading branch information
aajc authored Mar 6, 2023
2 parents ab4264d + 9cee8cd commit 0913059
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 58 deletions.
85 changes: 85 additions & 0 deletions app/src/main/java/com/example/demo5/BestFriend.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.example.demo5;

import android.util.Log;

import java.util.UUID;

import androidx.core.util.Pair;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import static java.util.UUID.randomUUID;

public class BestFriend {
MutableLiveData<Pair<Double, Double>> loc = new MutableLiveData<>();
UUID uid;

BestFriend() {
loc.setValue(new Pair<Double, Double>(0.0, 0.0));
this.uid = new UUID(1, 0);
uid = randomUUID();
//testMove();
}


public Double getLatitude() {
return getLocation().getValue().first;
}

public Double getLongitude() {
return getLocation().getValue().second;
}

public UUID getUid() {
return uid;
}

public String getUidString() {
return getUid().toString();
}

public LiveData<Pair<Double, Double>> getLocation() {
return loc;
}

public void testMove() {
for (int i = 0; i < 100; ++i) {

switch (i % 4) {
case 0:
loc.postValue(new Pair<Double, Double>(1.0, 1.0));
break;
case 1:
loc.postValue(new Pair<Double, Double>(1.0, -1.0));
break;
case 2:
loc.postValue(new Pair<Double, Double>(-1.0, -1.0));
break;
case 3:
loc.postValue(new Pair<Double, Double>(-1.0, 1.0));
break;
default:
Log.i("ERROR", "yeah, this isn't working");
break;
}

try {
Thread.sleep(1000);
Log.i("SLEEP", String.valueOf(i));
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
}
}

public void testMove2() {
loc.postValue(new Pair<Double, Double>(0.0, -1.0));
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
throw new RuntimeException(ex);
}
//loc.postValue(new Pair<Double, Double>(-1.0, -1.0));
}
}

43 changes: 31 additions & 12 deletions app/src/main/java/com/example/demo5/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.example.demo5;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.util.Pair;
Expand All @@ -19,14 +21,27 @@ public class MainActivity extends AppCompatActivity {
private static final String HOUSE_LABEL_STRING = "houseLabel";
private LocationService locationService;
private OrientationService orientationService;
private bestFriend bestFriend = new bestFriend();
public BestFriend bestFriend;
private double bestFriendRad;
private Future<?> future;
private ExecutorService backgroundThreadExecutor = Executors.newSingleThreadExecutor();
public Pair<Double, Double> userLocation;

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

locationService = LocationService.singleton(this);
bestFriend = new BestFriend();

if (future != null) {
this.future.cancel(true);
}
this.future = backgroundThreadExecutor.submit(() -> {
bestFriend.testMove2();
});

this.reobserveLocation();
}

Expand All @@ -38,25 +53,29 @@ private void reobserveLocation() {
private void onLocationChanged(Pair<Double, Double> latLong) {
TextView locationText = findViewById(R.id.locationText);
locationText.setText(Utilities.formatLocation(latLong.first, latLong.second));
whenFriendLocationChanges(latLong.first, latLong.second);
userLocation = latLong;
whenFriendLocationChanges();
}

public void whenFriendLocationChanges(Double latitude, Double longitude) {
double ang = angleCalculation(latitude, longitude);

updateFriendDirection(ang);
public void whenFriendLocationChanges() {
//rad = angleCalculation(location);
var bestFriendLocationData = bestFriend.getLocation();
bestFriendLocationData.observe(this, this::angleCalculation);
updateFriendDirection();
}

public void updateFriendDirection(double ang) {
public void updateFriendDirection() {
TextView bestFriend = findViewById(R.id.best_friend);
ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams)
bestFriend.getLayoutParams();
layoutParams.circleAngle = (float) Math.toDegrees(ang);
layoutParams.circleAngle = (float) Math.toDegrees(bestFriendRad);
bestFriend.setLayoutParams(layoutParams);
}

public double angleCalculation(Double latitude, Double longitude) {
return Math.atan2(bestFriend.getLongitude() - longitude, bestFriend.getLatitude() - latitude);
public void angleCalculation(Pair<Double, Double> friendLocation) {
//returns in radians
//rad = Math.atan2(bestFriend.getLongitude() - userLocation.second, bestFriend.getLatitude() - userLocation.first);
bestFriendRad = Math.atan2(friendLocation.second - userLocation.second, friendLocation.first - userLocation.first);
}

// @Override
Expand Down
34 changes: 0 additions & 34 deletions app/src/main/java/com/example/demo5/bestFriend.java

This file was deleted.

37 changes: 25 additions & 12 deletions app/src/test/java/com/example/demo5/UnitTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.example.demo5;

import android.content.SharedPreferences;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import org.junit.Test;
Expand All @@ -14,6 +11,8 @@
import androidx.lifecycle.MutableLiveData;
import androidx.test.core.app.ActivityScenario;

import static org.junit.Assert.assertEquals;

@RunWith(RobolectricTestRunner.class)
public class UnitTest {
public static final String EMPTY_STRING = "";
Expand All @@ -25,6 +24,7 @@ public class UnitTest {
public static final float HALF_PIE_RADIANS = (float)1.57;
public static final Double NINETY_DEGREES = 90.0;
public static final long NINETY_DEGREES_LONG = 90;
private static final float TWO_SEVENTY_LONG = 270;

/*@Test
public void checkNum() {
Expand Down Expand Up @@ -104,17 +104,16 @@ public void testUpdateCompassWhenLocationChanges() {
ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams)
bestFriend.getLayoutParams();

activity.updateFriendDirection(Math.toRadians(NINETY_DEGREES));
activity.updateFriendDirection();

float expected = NINETY_DEGREES_LONG;
float expected = 0 - NINETY_DEGREES_LONG;

System.out.println(layoutParams.circleAngle + " vs " + expected);
assert(layoutParams.circleAngle == expected);
assertEquals(expected, layoutParams.circleAngle, 0.001);
});
}

@Test
public void testUpdateCompassWhenLocationChanges2() {
public void testUpdateCompassWhenFriendLocationChanges() {
ActivityScenario<MainActivity> scenario = ActivityScenario.launch(MainActivity.class);
scenario.moveToState(Lifecycle.State.CREATED);
scenario.moveToState(Lifecycle.State.STARTED);
Expand All @@ -129,17 +128,31 @@ public void testUpdateCompassWhenLocationChanges2() {
double expectedLong = (double)0;

mockLocationSource.setValue(new androidx.core.util.Pair(expectedLat,expectedLong));
activity.userLocation = new androidx.core.util.Pair(expectedLat,expectedLong);

TextView bestFriend = activity.findViewById(R.id.best_friend);
ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams)
bestFriend.getLayoutParams();

activity.updateFriendDirection(Math.toRadians(NINETY_DEGREES));
activity.whenFriendLocationChanges();

float expected = 0 - NINETY_DEGREES_LONG;
System.out.println(activity.bestFriend.getLatitude() + ", " + activity.bestFriend.getLongitude());

assertEquals(layoutParams.circleAngle, expected, 0.001);

float expected = NINETY_DEGREES_LONG;
/*try {
Thread.sleep(5000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
expected = NINETY_DEGREES_LONG;
layoutParams = (ConstraintLayout.LayoutParams)
bestFriend.getLayoutParams();
System.out.println(activity.bestFriend.getLatitude() + ", " + activity.bestFriend.getLongitude());
System.out.println(layoutParams.circleAngle + " vs " + expected);
assert(layoutParams.circleAngle == expected);
assertEquals(layoutParams.circleAngle, expected, 0.001);*/
});
}

Expand Down

0 comments on commit 0913059

Please sign in to comment.