diff --git a/app/src/main/java/com/teester/whatsnearby/data/PreferenceList.java b/app/src/main/java/com/teester/whatsnearby/data/PreferenceList.java index 6b92404..1807bf3 100644 --- a/app/src/main/java/com/teester/whatsnearby/data/PreferenceList.java +++ b/app/src/main/java/com/teester/whatsnearby/data/PreferenceList.java @@ -28,4 +28,5 @@ public class PreferenceList { public final static String LAST_QUERY_LOCATION_LONGITUDE = "last_query_location_longitude"; public final static String LOCATION_PROVIDER = "location_provider"; public final static String NOT_QUERY_REASON = "reason_not_to_query"; + public final static String OSM_USER_NAME = "osm_user_name"; } diff --git a/app/src/main/java/com/teester/whatsnearby/data/source/OAuth.java b/app/src/main/java/com/teester/whatsnearby/data/source/OAuth.java index 488ddfe..059ea0f 100644 --- a/app/src/main/java/com/teester/whatsnearby/data/source/OAuth.java +++ b/app/src/main/java/com/teester/whatsnearby/data/source/OAuth.java @@ -60,6 +60,8 @@ public void processOAuth() { provider.setOAuth10a(true); provider.retrieveAccessToken(consumer, verifier); + new UploadToOSM(preferences); + preferences.setStringPreference(PreferenceList.OAUTH_TOKEN_SECRET, consumer.getTokenSecret()); preferences.setStringPreference(PreferenceList.OAUTH_TOKEN, consumer.getToken()); preferences.setBooleanPreference(PreferenceList.LOGGED_IN_TO_OSM, true); diff --git a/app/src/main/java/com/teester/whatsnearby/data/source/QueryOverpass.java b/app/src/main/java/com/teester/whatsnearby/data/source/QueryOverpass.java index 346b615..5f116e5 100644 --- a/app/src/main/java/com/teester/whatsnearby/data/source/QueryOverpass.java +++ b/app/src/main/java/com/teester/whatsnearby/data/source/QueryOverpass.java @@ -74,6 +74,12 @@ public String getOverpassUri(double latitude, double longitude, float accuracy) return String.format("https://www.overpass-api.de/api/interpreter?data=[out:json][timeout:25];(%s%s%s);out%%20center%%20meta%%20qt;", node, way, relation); } + /** + * Querys overpass with a given string and gets json back + * + * @param urlString the url to query + * @return json retrieved from overpass + */ @Override public String queryOverpassApi(String urlString) { @@ -94,6 +100,11 @@ public String queryOverpassApi(String urlString) { return resultToDisplay; } + /** + * Converts the json returned from overpass into usable objects + * + * @param result The json from overpass + */ @Override public void processResult(String result) { if (result != null) { @@ -150,6 +161,13 @@ public void processResult(String result) { } } + /** + * Entry point to query Overpass + * + * @param latitude Location latitude + * @param longitude Location longitude + * @param accuracy Location accuracy + */ @Override public void queryOverpass(double latitude, double longitude, float accuracy) { SourceContract.Preferences preferences = new Preferences(context); @@ -207,6 +225,12 @@ private void prepareNotification() { } } + /** + * Checks the room database to see if we've been here before recently + * + * @param osmId the id of the object we're interested in + * @return true if we've been there in the last week + */ private boolean checkDatabaseForLocation(long osmId) { SourceContract.Preferences preferences = new Preferences(context); if (BuildConfig.DEBUG && preferences.getBooleanPreference(PreferenceList.DEBUG_MODE)) { @@ -226,6 +250,11 @@ private boolean checkDatabaseForLocation(long osmId) { return location != null && location.getTimeVisited() > time; } + /** + * Adds an object to the room database + * + * @param osmObject the object to be added + */ private void updateDatabase(OsmObject osmObject) { AppDatabase db = AppDatabase.getAppDatabase(context); VisitedLocation location = db.visitedLocationDao().findByOsmId(osmObject.getId()); diff --git a/app/src/main/java/com/teester/whatsnearby/data/source/SourceContract.java b/app/src/main/java/com/teester/whatsnearby/data/source/SourceContract.java index 39228bc..f20c185 100644 --- a/app/src/main/java/com/teester/whatsnearby/data/source/SourceContract.java +++ b/app/src/main/java/com/teester/whatsnearby/data/source/SourceContract.java @@ -38,6 +38,8 @@ interface Overpass { interface upload { void uploadToOsm(); + + void setUsername(); } interface oAuth { diff --git a/app/src/main/java/com/teester/whatsnearby/data/source/UploadToOSM.java b/app/src/main/java/com/teester/whatsnearby/data/source/UploadToOSM.java index 36dcfbf..ad9b961 100644 --- a/app/src/main/java/com/teester/whatsnearby/data/source/UploadToOSM.java +++ b/app/src/main/java/com/teester/whatsnearby/data/source/UploadToOSM.java @@ -11,6 +11,7 @@ import de.westnordost.osmapi.common.errors.OsmAuthorizationException; import de.westnordost.osmapi.map.MapDataDao; import de.westnordost.osmapi.map.data.Element; +import de.westnordost.osmapi.user.UserDao; import oauth.signpost.OAuthConsumer; import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer; @@ -97,4 +98,13 @@ private Element modifyCurrentElement(Element modifiedElement) { return modifiedElement; } + + @Override + public void setUsername() { + OsmConnection connection = getConnection(); + + UserDao userDao = new UserDao(connection); + String name = userDao.getMine().displayName; + preferences.setStringPreference(PreferenceList.OSM_USER_NAME, name); + } } diff --git a/app/src/main/java/com/teester/whatsnearby/main/MainActivity.java b/app/src/main/java/com/teester/whatsnearby/main/MainActivity.java index 0858c16..7f969ce 100644 --- a/app/src/main/java/com/teester/whatsnearby/main/MainActivity.java +++ b/app/src/main/java/com/teester/whatsnearby/main/MainActivity.java @@ -55,7 +55,6 @@ protected void onCreate(Bundle savedInstanceState) { this.textView = this.findViewById(R.id.textView); this.button = this.findViewById(R.id.button); Toolbar toolbar = findViewById(R.id.toolbar); - setSupportActionBar(toolbar); this.button.setOnClickListener(this); @@ -200,8 +199,8 @@ public void toggleDebugMode(boolean state) { } @Override - public void showIfLoggedIn(int messageStringId, int buttonStringId) { - textView.setText(getString(messageStringId)); + public void showIfLoggedIn(int messageStringId, int buttonStringId, String user) { + textView.setText(String.format(getString(messageStringId), user)); button.setText(getString(buttonStringId)); } diff --git a/app/src/main/java/com/teester/whatsnearby/main/MainActivityContract.java b/app/src/main/java/com/teester/whatsnearby/main/MainActivityContract.java index 739ae02..d3a88ce 100644 --- a/app/src/main/java/com/teester/whatsnearby/main/MainActivityContract.java +++ b/app/src/main/java/com/teester/whatsnearby/main/MainActivityContract.java @@ -18,7 +18,7 @@ interface Presenter { interface View { - void showIfLoggedIn(int message, int button); + void showIfLoggedIn(int message, int button, String user); void startOAuth(); } diff --git a/app/src/main/java/com/teester/whatsnearby/main/MainActivityPresenter.java b/app/src/main/java/com/teester/whatsnearby/main/MainActivityPresenter.java index 919592a..df2c27f 100644 --- a/app/src/main/java/com/teester/whatsnearby/main/MainActivityPresenter.java +++ b/app/src/main/java/com/teester/whatsnearby/main/MainActivityPresenter.java @@ -4,6 +4,7 @@ import com.teester.whatsnearby.Utilities; import com.teester.whatsnearby.data.PreferenceList; import com.teester.whatsnearby.data.source.SourceContract; +import com.teester.whatsnearby.data.source.UploadToOSM; import java.io.UnsupportedEncodingException; import java.net.URI; @@ -32,6 +33,7 @@ public void showIfLoggedIn() { boolean loggedIn = preferences.getBooleanPreference(PreferenceList.LOGGED_IN_TO_OSM); int message; int button; + String userName = getUserName(); if (loggedIn) { message = R.string.logged_in_as; button = R.string.log_out; @@ -39,7 +41,7 @@ public void showIfLoggedIn() { message = R.string.not_logged_in; button = R.string.authorise_openstreetmap; } - view.showIfLoggedIn(message, button); + view.showIfLoggedIn(message, button, userName); } /** @@ -91,4 +93,19 @@ public void toggleDebugMode() { preferences.setBooleanPreference(preference, true); } } + + private String getUserName() { + String preference = preferences.getStringPreference(PreferenceList.OSM_USER_NAME); + if ("".equals(preference)) { + new Thread(new Runnable() { + @Override + public void run() { + SourceContract.upload upload = new UploadToOSM(preferences); + upload.setUsername(); + } + }).start(); + preference = preferences.getStringPreference(PreferenceList.OSM_USER_NAME); + } + return preference; + } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f954760..2972fa5 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -47,7 +47,7 @@ None of these places Click the button below to check for points of interest around your current location. Check Now! - You are currently logged in to OpenStreetMap.\nWhat\'s Nearby? will notify you when you\'re in a location where there is missing information in OpenStreetMap. + You are currently logged in to OpenStreetMap as %s.\nWhat\'s Nearby? will notify you when you\'re in a location where there is missing information in OpenStreetMap. Log Out Added details to %s survey diff --git a/app/src/test/java/com/teester/whatsnearby/main/MainActivityPresenterTest.java b/app/src/test/java/com/teester/whatsnearby/main/MainActivityPresenterTest.java index 7663c47..c3df641 100644 --- a/app/src/test/java/com/teester/whatsnearby/main/MainActivityPresenterTest.java +++ b/app/src/test/java/com/teester/whatsnearby/main/MainActivityPresenterTest.java @@ -11,6 +11,7 @@ import java.net.URI; import java.net.URISyntaxException; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.verify; @@ -27,7 +28,9 @@ public class MainActivityPresenterTest { private MainActivityContract.Presenter presenter; @Before - public void setUp() { + public void + + setUp() { presenter = new MainActivityPresenter(view, preferences); } @@ -39,7 +42,7 @@ public void logoutButtonClicked() { verify(preferences).setStringPreference(eq("oauth_token"), eq("")); verify(preferences).setStringPreference(eq("oauth_token_secret"), eq("")); verify(view).startOAuth(); - verify(view).showIfLoggedIn(anyInt(), anyInt()); + verify(view).showIfLoggedIn(anyInt(), anyInt(), (String) any()); } @Test @@ -54,4 +57,10 @@ public void checkIfInOAuthFlow() { verify(view).startOAuth(); } + + @Test + public void checkIfUsernameIsSetCorrectly() { + presenter.showIfLoggedIn(); + verify(view).showIfLoggedIn(anyInt(), anyInt(), (String) any()); + } } \ No newline at end of file