Skip to content

Commit

Permalink
Merge pull request #1086 from mendhak/milestone128
Browse files Browse the repository at this point in the history
Milestone v128
  • Loading branch information
mendhak authored Nov 5, 2023
2 parents 8fa6225 + 9a954ec commit 1dcd7a2
Show file tree
Hide file tree
Showing 15 changed files with 231 additions and 275 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,17 @@ Give it a minute and Android Studio will configure the projects and download the

Sign up for an account with [OpenStreetMap](https://openstreetmap.org) and log in.

Click on 'oauth settings'
Click on 'My Settings', then 'OAuth2 Applications'

Click on 'Register your application'

Fill in the form with these details
Fill in the form with these details. Remember to uncheck the 'Confidential Application' checkbox, since this is a mobile app.

![Oauth settings](assets/osm_oauth_settings.png)

After registering the application, you will receive a 'Consumer Key' and a 'Consumer Secret'.
Place the keys in OSMJob.java and OpenStreetMapManager.java.
After registering the application, you will receive a Client ID.
Place the Client ID in OpenStreetMapManager#getOpenStreetMapClientID().
If you used your own custom scheme, replace the value in AndroidManifest.xml and OpenStreetMapManager#getOpenStreetMapRedirect()


### Dropbox Setup (Optional)
Expand Down
Binary file modified assets/osm_oauth_settings.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 0 additions & 15 deletions assets/text/opensource.md
Original file line number Diff line number Diff line change
Expand Up @@ -793,21 +793,6 @@ limitations under the License.
[2]: http://www.gnu.org/licenses/lgpl-2.1.html


## [mttkay/signpost](https://github.com/mttkay/signpost)


Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

## [eltos/SimpleDialogFragments](https://github.com/eltos/SimpleDialogFragments)

Apache License
Expand Down
5 changes: 5 additions & 0 deletions fastlane/metadata/android/en-US/changelogs/128.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
* OpenStreetMap breaking change - authentication switching to OAuth2, you will need to reauthorize OpenStreetMap
* Default to auto send when logging stops
* Bugfix - Check passive locations to ensure their time is greater than the previous location's time
* Bugfix for monochrome icon on Nothing OS theme
* Disallow certain characters in the filename such as asterisk, slashes
12 changes: 2 additions & 10 deletions gpslogger/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ android {
minSdkVersion 16
targetSdkVersion 30

versionCode 127
versionName "127"
versionCode 128
versionName "128"

manifestPlaceholders = [
appAuthRedirectScheme: 'com.mendhak.gpslogger'
Expand Down Expand Up @@ -172,14 +172,6 @@ dependencies {
//OKHTTP client
implementation 'com.squareup.okhttp3:okhttp:3.10.0'

//OKHTTP interceptor, works with oauth signpost
implementation 'se.akerfeldt:okhttp-signpost:1.1.0'

//OAuth signing
implementation('oauth.signpost:signpost-core:1.2.1.1'){
exclude module: 'commons-codec'
}

//Dropbox API 3
implementation 'com.dropbox.core:dropbox-core-sdk:5.1.1'

Expand Down
15 changes: 7 additions & 8 deletions gpslogger/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@
<data android:scheme="com.mendhak.gpslogger"
android:path="/oauth2googledrive"/>
</intent-filter>

<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:host="oauth2openstreetmap" android:scheme="com.mendhak.gpslogger" />
</intent-filter>
</activity>

<activity
Expand All @@ -90,13 +95,7 @@
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mendhak.gpslogger.GpsMainActivity" />
<intent-filter>
<action android:name="com.mendhak.gpslogger.MAIN_PREFS" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:host="authorize" android:scheme="gpslogger" />
</intent-filter>

</activity>

<activity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,14 @@ void onLocationChanged(Location loc) {
return;
}

// Even if it's a passive location, the time should be greater than the previous location's time.
if(isPassiveLocation && session.getPreviousLocationInfo() != null && loc.getTime() <= session.getPreviousLocationInfo().getTime()){
LOG.debug("Passive location time: " + loc.getTime() + ", previous location's time: " + session.getPreviousLocationInfo().getTime());
LOG.debug("Passive location received, but its time was less than the previous point's time.");
return;
}


//Don't log a point if user has been still
// However, if user has set an annotation, just log the point, disregard time and distance filters
if(userHasBeenStillForTooLong()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,7 @@ public void setAutoSendInterval(String frequency){
*/
@ProfilePreference(name= PreferenceNames.AUTOSEND_ON_STOP)
public boolean shouldAutoSendOnStopLogging() {
return prefs.getBoolean(PreferenceNames.AUTOSEND_ON_STOP, false);
return prefs.getBoolean(PreferenceNames.AUTOSEND_ON_STOP, true);
}

public void setDebugToFile(boolean writeToFile) {
Expand Down Expand Up @@ -919,49 +919,15 @@ public void setOpenGTSAccountName(String account){
}


/**
* Sets OpenStreetMap OAuth Token for auto send
*/
public void setOSMAccessToken(String token) {
prefs.edit().putString(PreferenceNames.OPENSTREETMAP_ACCESS_TOKEN, token).apply();
}


/**
* Gets access token for OpenStreetMap auto send
*/
public String getOSMAccessToken() {
return prefs.getString(PreferenceNames.OPENSTREETMAP_ACCESS_TOKEN, "");
}


/**
* Sets OpenStreetMap OAuth secret for auto send
*/
public void setOSMAccessTokenSecret(String secret) {
prefs.edit().putString(PreferenceNames.OPENSTREETMAP_ACCESS_TOKEN_SECRET, secret).apply();
}

/**
* Gets access token secret for OpenStreetMap auto send
*/
public String getOSMAccessTokenSecret() {
return prefs.getString(PreferenceNames.OPENSTREETMAP_ACCESS_TOKEN_SECRET, "");
public String getOSMAuthState(){
return prefs.getString(PreferenceNames.OPENSTREETMAP_AUTH_STATE, "");
}

/**
* Sets request token for OpenStreetMap auto send
*/
public void setOSMRequestToken(String token) {
prefs.edit().putString(PreferenceNames.OPENSTREETMAP_REQUEST_TOKEN, token).apply();
public void setOSMAuthState(String auth_state_json_serialized){
prefs.edit().putString(PreferenceNames.OPENSTREETMAP_AUTH_STATE, auth_state_json_serialized).apply();
}

/**
* Sets request token secret for OpenStreetMap auto send
*/
public void setOSMRequestTokenSecret(String secret) {
prefs.edit().putString(PreferenceNames.OPENSTREETMAP_REQUEST_TOKEN_SECRET, secret).apply();
}

/**
* Description of uploaded trace on OpenStreetMap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,8 @@ public class PreferenceNames {
public static final String HIDE_NOTIFICATION_FROM_LOCK_SCREEN = "hide_notification_from_lock_screen";
public static final String DISPLAY_IMPERIAL = "useImperial";

public static final String OPENSTREETMAP_ACCESS_TOKEN = "osm_accesstoken";
public static final String OPENSTREETMAP_ACCESS_TOKEN_SECRET = "osm_accesstokensecret";
public static final String OPENSTREETMAP_REQUEST_TOKEN = "osm_requesttoken";
public static final String OPENSTREETMAP_REQUEST_TOKEN_SECRET = "osm_requesttokensecret";
public static final String OPENSTREETMAP_AUTH_STATE = "osm_oauthstate";

public static final String OPENSTREETMAP_DESCRIPTION = "osm_description";
public static final String OPENSTREETMAP_TAGS = "osm_tags";
public static final String OPENSTREETMAP_VISIBILITY = "osm_visibility";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,45 +26,47 @@
import com.birbit.android.jobqueue.Params;
import com.birbit.android.jobqueue.RetryConstraint;
import com.mendhak.gpslogger.common.AppSettings;
import com.mendhak.gpslogger.common.PreferenceHelper;
import com.mendhak.gpslogger.common.Strings;
import com.mendhak.gpslogger.common.events.UploadEvents;
import com.mendhak.gpslogger.common.network.Networks;
import com.mendhak.gpslogger.common.slf4j.Logs;

import net.openid.appauth.AuthState;
import net.openid.appauth.AuthorizationException;
import net.openid.appauth.AuthorizationService;

import org.slf4j.Logger;

import java.io.File;
import java.util.concurrent.atomic.AtomicBoolean;

import javax.net.ssl.X509TrustManager;

import de.greenrobot.event.EventBus;
import oauth.signpost.OAuthConsumer;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import se.akerfeldt.okhttp.signpost.OkHttpOAuthConsumer;
import se.akerfeldt.okhttp.signpost.SigningInterceptor;

public class OSMJob extends Job {


private static final Logger LOG = Logs.of(OSMJob.class);
//OAuthConsumer consumer;

private final AtomicBoolean taskDone = new AtomicBoolean(false);
private String openStreetMapAccessToken;
String gpsTraceUrl;
File chosenFile;
String description;
String tags;
String visibility;

protected OSMJob(OAuthConsumer consumer, String gpsTraceUrl, File chosenFile, String description, String tags, String visibility) {
protected OSMJob(String gpsTraceUrl, File chosenFile, String description, String tags, String visibility) {
super(new Params(1).requireNetwork().persist().addTags(getJobTag(chosenFile)));

// this.consumer = consumer;
this.gpsTraceUrl = gpsTraceUrl;
this.chosenFile = chosenFile;
this.description = description;
Expand All @@ -81,11 +83,45 @@ public void onAdded() {
@Override
public void onRun() throws Throwable {

OkHttpOAuthConsumer consumer = new OkHttpOAuthConsumer("NQ4ucS4F0RpQO1byUQB5JA", Strings.GetOSM());
consumer.setTokenWithSecret(PreferenceHelper.getInstance().getOSMAccessToken(), PreferenceHelper.getInstance().getOSMAccessTokenSecret());
AuthState authState = OpenStreetMapManager.getAuthState();

if(!authState.isAuthorized()){
EventBus.getDefault().post(new UploadEvents.OpenStreetMap().failed("Could not upload to OpenStreetMap. Not authorized."));
}

AuthorizationService authorizationService = OpenStreetMapManager.getAuthorizationService(AppSettings.getInstance());

// The performActionWithFreshTokens seems to happen on a UI thread! (Why??)
// So I can't do network calls on this thread.
// Instead, updating a class level variable, and waiting for it afterwards.
// https://github.com/openid/AppAuth-Android/issues/123
authState.performActionWithFreshTokens(authorizationService, new AuthState.AuthStateAction() {
@Override
public void execute(@Nullable String accessToken, @Nullable String idToken, @Nullable AuthorizationException ex) {
if (ex != null){
EventBus.getDefault().post(new UploadEvents.OpenStreetMap().failed(ex.toJsonString(), ex));
taskDone.set(true);
LOG.error(ex.toJsonString(), ex);
return;
}

openStreetMapAccessToken = accessToken;
taskDone.set(true);
}
});

// Wait for the performActionWithFreshTokens.execute callback
// (which happens on the UI thread for some reason) to complete.
while (!taskDone.get()) {
Thread.sleep(500);
}

if (Strings.isNullOrEmpty(openStreetMapAccessToken)) {
LOG.error("Failed to fetch Access Token for OpenStreetMap. Stopping this job.");
return;
}

OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new SigningInterceptor(consumer))
.sslSocketFactory(Networks.getSocketFactory(AppSettings.getInstance()),
(X509TrustManager) Networks.getTrustManager(AppSettings.getInstance()))
.build();
Expand All @@ -98,20 +134,18 @@ public void onRun() throws Throwable {
.addFormDataPart("visibility",visibility)
.build();

// consumer.sign(requestBody);


Request request = new Request.Builder()
.url(gpsTraceUrl)
.addHeader("Authorization", "Bearer " + openStreetMapAccessToken)
.post(requestBody)
.build();


Response response = client.newCall(request).execute();
ResponseBody body = response.body();

if(response.isSuccessful()){
String message = body.string();
LOG.debug("Response from OpenStreetMap: " + message);
LOG.info("OpenStreetMap - file uploaded");
EventBus.getDefault().post(new UploadEvents.OpenStreetMap().succeeded());
}
Expand Down
Loading

0 comments on commit 1dcd7a2

Please sign in to comment.