Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add lib for commonshttp (needed for oauth) & some progress in making …

…oauth work
  • Loading branch information...
commit a36d1df079614c9e503d0e1aaab2844b31b10231 1 parent 20bd9c6
@Ignifazius Ignifazius authored
View
1  .classpath
@@ -16,5 +16,6 @@
</attributes>
</classpathentry>
<classpathentry kind="lib" path="libs/signpost-core-1.2.1.1.jar"/>
+ <classpathentry kind="lib" path="libs/signpost-commonshttp4-1.2.1.1.jar"/>
<classpathentry kind="output" path="bin/classes"/>
</classpath>
View
BIN  libs/signpost-commonshttp4-1.2.1.1.jar
Binary file not shown
View
55 res/layout/osmsettings.xml
@@ -0,0 +1,55 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:orientation="vertical" >
+
+ <Button
+ android:id="@+id/button_set_osm"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="@string/set_osm" />
+
+ <TextView
+ android:id="@+id/textView_osm_token"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="@string/osm_token"
+ android:textAppearance="?android:attr/textAppearanceLarge" />
+
+ <TextView
+ android:id="@+id/textView_osm_token_text"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="Large Text"
+ android:textAppearance="?android:attr/textAppearanceLarge" />
+
+ <TextView
+ android:id="@+id/textView_osm_secret_token"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="@string/osm_secret_token"
+ android:textAppearance="?android:attr/textAppearanceLarge" />
+
+ <TextView
+ android:id="@+id/textView_osm_secret_token_text"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="Large Text"
+ android:textAppearance="?android:attr/textAppearanceLarge" />
+
+ <TextView
+ android:id="@+id/textView_osm_access_token"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="@string/osm_access_token"
+ android:textAppearance="?android:attr/textAppearanceLarge" />
+
+ <TextView
+ android:id="@+id/textView_osm_access_token_text"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:text="Large Text"
+ android:textAppearance="?android:attr/textAppearanceLarge" />
+
+</LinearLayout>
View
4 res/values/strings.xml
@@ -29,5 +29,9 @@
<string name="emptyString"></string>
<string name="transmitChangesString">Transmit Changes to OpenStreetMap</string>
<string name="enableEditmode">Toggle Editmode</string>
+ <string name="set_osm">Set OSM-Account</string>
+ <string name="osm_token">Your Token:</string>
+ <string name="osm_secret_token">Your Secret Token:</string>
+ <string name="osm_access_token">Your Access Token:</string>
</resources>
View
200 src/org/yaoha/OAuth.java
@@ -0,0 +1,200 @@
+import android.content.Context;
+import android.content.Intent;
+import android.content.SharedPreferences;
+import android.net.Uri;
+import android.os.Environment;
+import android.preference.PreferenceManager;
+import oauth.signpost.OAuthConsumer;
+import oauth.signpost.OAuthProvider;
+import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
+import oauth.signpost.commonshttp.CommonsHttpOAuthProvider;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.mime.HttpMultipartMode;
+import org.apache.http.entity.mime.MultipartEntity;
+import org.apache.http.entity.mime.content.FileBody;
+import org.apache.http.entity.mime.content.StringBody;
+import org.apache.http.impl.client.DefaultHttpClient;
+
+import java.io.File;
+
+public class OSMHelper implements IActionListener
+{
+
+ IActionListener callback;
+ Context ctx;
+
+ public OSMHelper(Context ctx, IActionListener callback)
+ {
+
+ this.ctx = ctx;
+ this.callback = callback;
+ }
+
+ public static OAuthProvider GetOSMAuthProvider(Context ctx)
+ {
+ return new CommonsHttpOAuthProvider(
+ ctx.getString(R.string.osm_requesttoken_url),
+ ctx.getString(R.string.osm_accesstoken_url),
+ ctx.getString(R.string.osm_authorize_url));
+
+ }
+
+ public static boolean IsOsmAuthorized(Context ctx)
+ {
+ SharedPreferences prefs = PreferenceManager
+ .getDefaultSharedPreferences(ctx);
+ String oAuthAccessToken = prefs.getString("osm_accesstoken", "");
+
+ return (oAuthAccessToken != null && oAuthAccessToken.length() > 0);
+ }
+
+ public static Intent GetOsmSettingsIntent(Context ctx)
+ {
+ Intent intentOsm;
+
+ if (!IsOsmAuthorized(ctx))
+ {
+ intentOsm = new Intent(ctx.getPackageName() + ".OSM_AUTHORIZE");
+ intentOsm.setData(Uri.parse("gpslogger://authorize"));
+ }
+ else
+ {
+ intentOsm = new Intent(ctx.getPackageName() + ".OSM_SETUP");
+
+ }
+
+ return intentOsm;
+ }
+
+
+ public static OAuthConsumer GetOSMAuthConsumer(Context ctx)
+ {
+
+ OAuthConsumer consumer = null;
+
+ try
+ {
+ int osmConsumerKey = ctx.getResources().getIdentifier(
+ "osm_consumerkey", "string", ctx.getPackageName());
+ int osmConsumerSecret = ctx.getResources().getIdentifier(
+ "osm_consumersecret", "string", ctx.getPackageName());
+ consumer = new CommonsHttpOAuthConsumer(
+ ctx.getString(osmConsumerKey),
+ ctx.getString(osmConsumerSecret));
+
+ SharedPreferences prefs = PreferenceManager
+ .getDefaultSharedPreferences(ctx);
+ String osmAccessToken = prefs.getString("osm_accesstoken", "");
+ String osmAccessTokenSecret = prefs.getString(
+ "osm_accesstokensecret", "");
+
+ if (osmAccessToken != null && osmAccessToken.length() > 0
+ && osmAccessTokenSecret != null
+ && osmAccessTokenSecret.length() > 0)
+ {
+ consumer.setTokenWithSecret(osmAccessToken,
+ osmAccessTokenSecret);
+ }
+
+ }
+ catch (Exception e)
+ {
+ //Swallow the exception
+ }
+
+ return consumer;
+ }
+
+
+ public void UploadGpsTrace(String fileName)
+ {
+
+ File gpxFolder = new File(Environment.getExternalStorageDirectory(), "GPSLogger");
+ File chosenFile = new File(gpxFolder, fileName);
+ OAuthConsumer consumer = GetOSMAuthConsumer(ctx);
+ String gpsTraceUrl = ctx.getString(R.string.osm_gpstrace_url);
+
+ SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
+ String description = prefs.getString("osm_description", "");
+ String tags = prefs.getString("osm_tags", "");
+ String visibility = prefs.getString("osm_visibility", "private");
+
+ Thread t = new Thread(new OsmUploadHandler(this, consumer, gpsTraceUrl, chosenFile, description, tags, visibility));
+ t.start();
+ }
+
+ public void OnComplete()
+ {
+ callback.OnComplete();
+ }
+
+ public void OnFailure()
+ {
+ callback.OnFailure();
+ }
+
+
+ private class OsmUploadHandler implements Runnable
+ {
+ OAuthConsumer consumer;
+ String gpsTraceUrl;
+ File chosenFile;
+ String description;
+ String tags;
+ String visibility;
+ IActionListener helper;
+
+ public OsmUploadHandler(IActionListener helper, OAuthConsumer consumer, String gpsTraceUrl, File chosenFile, String description, String tags, String visibility)
+ {
+ this.consumer = consumer;
+ this.gpsTraceUrl = gpsTraceUrl;
+ this.chosenFile = chosenFile;
+ this.description = description;
+ this.tags = tags;
+ this.visibility = visibility;
+ this.helper = helper;
+ }
+
+ public void run()
+ {
+ try
+ {
+ HttpPost request = new HttpPost(gpsTraceUrl);
+
+ consumer.sign(request);
+
+ MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
+
+ FileBody gpxBody = new FileBody(chosenFile);
+
+ entity.addPart("file", gpxBody);
+ if (description == null || description.length() <= 0)
+ {
+ description = "GPSLogger for Android";
+ }
+
+ entity.addPart("description", new StringBody(description));
+ entity.addPart("tags", new StringBody(tags));
+ entity.addPart("visibility", new StringBody(visibility));
+
+ request.setEntity(entity);
+ DefaultHttpClient httpClient = new DefaultHttpClient();
+
+ HttpResponse response = httpClient.execute(request);
+ int statusCode = response.getStatusLine().getStatusCode();
+ Utilities.LogDebug("OSM Upload - " + String.valueOf(statusCode));
+ helper.OnComplete();
+
+ }
+ catch (Exception e)
+ {
+ helper.OnFailure();
+ Utilities.LogError("OsmUploadHelper.run", e);
+ }
+ }
+ }
+
+}
+
+
View
13 src/org/yaoha/OAuthHelper.java
@@ -4,6 +4,8 @@
import oauth.signpost.OAuthProvider;
import oauth.signpost.basic.DefaultOAuthConsumer;
import oauth.signpost.basic.DefaultOAuthProvider;
+import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
+import oauth.signpost.commonshttp.CommonsHttpOAuthProvider;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
@@ -17,12 +19,13 @@
private String mCallbackUrl = "osm-login://yaoha.org";
public OAuthHelper(){
- OSMconsumer = new DefaultOAuthConsumer("LXhdgmfvvoGRmVCc0EPZajUS8458AXYZ2615f9hs", "ZTfY5iYZ8Lszgy6DtRh0b258qciz4aYm1XnMciDi");
- OSMprovider = new DefaultOAuthProvider(
- "http://www.openstreetmap.org/oauth/request_token"/*+ URLEncoder.encode(scope, "utf-8")*/,
+ this.OSMconsumer = new CommonsHttpOAuthConsumer("LXhdgmfvvoGRmVCc0EPZajUS8458AXYZ2615f9hs", "ZTfY5iYZ8Lszgy6DtRh0b258qciz4aYm1XnMciDi");
+ this.OSMprovider = new CommonsHttpOAuthProvider(
+ "http://www.openstreetmap.org/oauth/request_token",
"http://www.openstreetmap.org/oauth/access_token",
"http://www.openstreetmap.org/oauth/authorize");
- OSMprovider.setOAuth10a(true);
+ OSMconsumer.setTokenWithSecret("LXhdgmfvvoGRmVCc0EPZajUS8458AXYZ2615f9hs", "ZTfY5iYZ8Lszgy6DtRh0b258qciz4aYm1XnMciDi");
+ this.OSMprovider.setOAuth10a(true);
//mCallbackUrl = (callbackUrl == null ? OAuth.OUT_OF_BAND : callbackUrl);
}
public String getRequestToken() throws OAuthMessageSignerException, OAuthNotAuthorizedException, OAuthExpectationFailedException, OAuthCommunicationException {
@@ -31,7 +34,7 @@ public String getRequestToken() throws OAuthMessageSignerException, OAuthNotAuth
}
public String[] getAccessToken(String verifier) throws OAuthMessageSignerException, OAuthNotAuthorizedException, OAuthExpectationFailedException, OAuthCommunicationException {
- OSMprovider.retrieveAccessToken(OSMconsumer, verifier);
+ this.OSMprovider.retrieveAccessToken(this.OSMconsumer, verifier);
return new String[] {OSMconsumer.getToken(), OSMconsumer.getTokenSecret()};
}
}
View
72 src/org/yaoha/OAuthOSM.java
@@ -0,0 +1,72 @@
+package org.yaoha;
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.net.URLEncoder;
+
+import android.content.Intent;
+import android.net.Uri;
+import android.widget.Toast;
+
+import oauth.signpost.OAuth;
+import oauth.signpost.OAuthConsumer;
+import oauth.signpost.OAuthProvider;
+import oauth.signpost.basic.DefaultOAuthConsumer;
+import oauth.signpost.basic.DefaultOAuthProvider;
+
+public class OAuthOSM {
+
+
+
+
+
+ private static final String PASSWORD = null;
+ private static final String USERNAME = null;
+
+ public static void main(String[] args) throws Exception {
+
+ OAuthConsumer consumer = new DefaultOAuthConsumer(USERNAME, PASSWORD);
+
+// String scope = "http://www.blogger.com/feeds";
+ OAuthProvider provider = new DefaultOAuthProvider(
+ "http://www.openstreetmap.org/oauth/request_token"/*+ URLEncoder.encode(scope, "utf-8")*/,
+ "http://www.openstreetmap.org/oauth/access_token",
+ "http://www.openstreetmap.org/oauth/authorize");
+
+ Toast.makeText(this, "Fetching request token...", Toast.LENGTH_SHORT).show();
+
+ String authUrl = provider.retrieveRequestToken(consumer, OAuth.OUT_OF_BAND);
+
+ System.out.println("Request token: " + consumer.getToken());
+ System.out.println("Token secret: " + consumer.getTokenSecret());
+
+
+
+ Intent viewIntent = new Intent("android.intent.action.VIEW", Uri.parse(authUrl));
+ startActivity(viewIntent);
+
+// System.out.println("Now visit:\n" + authUrl + "\n... and grant this app authorization");
+// System.out.println("Enter the verification code and hit ENTER when you're done:");
+
+ BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
+ String verificationCode = br.readLine();
+
+ System.out.println("Fetching access token...");
+
+ provider.retrieveAccessToken(consumer, verificationCode.trim());
+
+ System.out.println("Access token: " + consumer.getToken());
+ System.out.println("Token secret: " + consumer.getTokenSecret());
+
+ URL url = new URL("http://openstreetmap.de/karte.html");
+ HttpURLConnection request = (HttpURLConnection) url.openConnection();
+
+ consumer.sign(request);
+
+ System.out.println("Sending request...");
+ request.connect();
+
+ System.out.println("Response: " + request.getResponseCode() + " " + request.getResponseMessage());
+ }
+}
View
55 src/org/yaoha/OSMSettingsActivity.java
@@ -4,22 +4,48 @@
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
+import android.view.View;
+import android.view.View.OnClickListener;
+import android.widget.Button;
+import android.widget.MultiAutoCompleteTextView;
+import android.widget.TextView;
+import android.widget.Toast;
-public class OSMSettingsActivity extends Activity {
+public class OSMSettingsActivity extends Activity implements OnClickListener{
OAuthHelper helper = new OAuthHelper();
private String OSM_TOKEN = "";
private String OSM_SECRET_TOKEN = "";
+ TextView osmtoken;
+ TextView osmsecrettoken;
+ TextView osmaccesstoken;
-// @Override
-// protected void onCreate(Bundle savedInstanceState) {
-// onResume();
-// super.onCreate(savedInstanceState);
-// }
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.osmsettings);
+ Button setOSM = (Button) findViewById(R.id.button_set_osm);
+ osmtoken = (TextView) findViewById(R.id.textView_osm_token_text);
+ osmsecrettoken = (TextView) findViewById(R.id.textView_osm_secret_token_text);
+ osmaccesstoken = (TextView) findViewById(R.id.textView_osm_access_token_text);
+ try {
+ setOSM.setOnClickListener(this);
+ } catch (Exception e) {
+ String bla = e.getMessage();
+ String bla2 = bla;
+ }
+
+ }
+ public void onClick(View v) {
+ if(v.getId() == R.id.button_set_osm) {
+ registerToOSM();
+ //Toast.makeText(this, "click", Toast.LENGTH_SHORT).show();
+ }
+ }
protected void onResume() {
super.onResume();
-
+ Toast.makeText(this, "onResume()", Toast.LENGTH_SHORT).show();
String[] token = getVerifier();
if (token != null) {
try {
@@ -30,9 +56,20 @@ protected void onResume() {
YaohaActivity.setSecretToken(accessToken[1]);
} catch (Exception e) {
String Teststring = e.getMessage();
- String abc = Teststring;
+ Toast.makeText(this, Teststring, Toast.LENGTH_LONG).show();
}
-
+ osmaccesstoken.setText(token[1]);
+ }
+ osmtoken.setText(OSM_TOKEN);
+ osmsecrettoken.setText(OSM_SECRET_TOKEN);
+ }
+
+ public void registerToOSM(){
+ try {
+ String uri = helper.getRequestToken();
+ startActivity(new Intent("android.intent.action.VIEW", Uri.parse(uri)));
+ } catch (Exception e) {
+ e.getMessage();
}
}
View
18 src/org/yaoha/YaohaActivity.java
@@ -1,9 +1,7 @@
package org.yaoha;
-import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
-import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
@@ -39,7 +37,6 @@
import android.widget.Toast;
public class YaohaActivity extends Activity implements OnClickListener {
- Button mapButton;
Button startButton;
static String OSM_TOKEN = "", OSM_SECRET_TOKEN = "";
ImageButton button_favorite_1, button_favorite_2, button_favorite_3, button_favorite_4, button_favorite_5, button_favorite_6;
@@ -47,7 +44,6 @@
final static String EDIT_FAV_STRING = "edit favorite";
final static String EDIT_FAV_PIC = "edit picture";
final static String REMOVE_FAV = "remove favorite";
- OAuthHelper helper = new OAuthHelper();
TextView text_fav_1, text_fav_2, text_fav_3, text_fav_4, text_fav_5, text_fav_6;
final static int SELECT_PICTURE = 1;
Uri selectedImageUri;
@@ -152,9 +148,8 @@ public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(this, "You just payed 49,99€. Enjoy this Pro-Version!", Toast.LENGTH_LONG).show();
return true;
case R.id.setOSM:
-// Intent intent = new Intent(this, OSMSettingsActivity.class);
-// startActivity(intent);
- registerToOSM();
+ Intent intent = new Intent(this, OSMSettingsActivity.class);
+ startActivity(intent);
return true;
case R.id.connectToOSM:
connectToOSM();
@@ -192,15 +187,6 @@ public boolean onOptionsItemSelected(MenuItem item) {
this.OSM_TOKEN = OSMconsumer.getToken();
this.OSM_SECRET_TOKEN = OSMconsumer.getTokenSecret();
}*/
-
- public void registerToOSM(){
- try {
- String uri = helper.getRequestToken();
- startActivity(new Intent("android.intent.action.VIEW", Uri.parse(uri)));
- } catch (Exception e) {
- e.getMessage();
- }
- }
private void connectToOSM(){
Please sign in to comment.
Something went wrong with that request. Please try again.