Skip to content

Commit 46eeb04

Browse files
committed
Merge pull request #1 from MeTaXaS4/master
fixed the bugs.. ?
2 parents 3b71dfe + 5f4a598 commit 46eeb04

File tree

9 files changed

+209
-94
lines changed

9 files changed

+209
-94
lines changed

AndroidManifest.xml

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,32 @@
1-
<?xml version="1.0" encoding="utf-8"?>
2-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
package="com.betaminus.fitbitsample"
4-
android:versionCode="1"
5-
android:versionName="1.0" >
6-
7-
<uses-sdk
8-
android:minSdkVersion="8"
9-
android:targetSdkVersion="18" />
10-
11-
<uses-permission android:name="android.permission.INTERNET" />
12-
13-
<application
14-
android:allowBackup="true"
15-
android:icon="@drawable/ic_launcher"
16-
android:label="@string/app_name"
17-
android:theme="@style/AppTheme" >
18-
<activity
19-
android:name="com.betaminus.fitbitsample.MainActivity"
20-
android:label="@string/app_name" >
21-
<intent-filter>
22-
<action android:name="android.intent.action.MAIN" />
23-
24-
<category android:name="android.intent.category.LAUNCHER" />
25-
</intent-filter>
26-
</activity>
27-
</application>
28-
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.betaminus.fitbitsample"
4+
android:versionCode="1"
5+
android:versionName="1.0" >
6+
7+
<uses-sdk
8+
android:minSdkVersion="14"
9+
android:targetSdkVersion="16" />
10+
11+
<uses-permission android:name="android.permission.INTERNET" />
12+
13+
<application
14+
android:allowBackup="true"
15+
android:icon="@drawable/ic_launcher"
16+
android:label="@string/app_name" >
17+
<activity
18+
android:name=".MainActivity"
19+
android:label="@string/app_name" >
20+
<intent-filter>
21+
<action android:name="android.intent.action.MAIN" />
22+
23+
<category android:name="android.intent.category.LAUNCHER" />
24+
</intent-filter>
25+
</activity>
26+
<activity
27+
android:name=".AuthenticationActivity"
28+
android:label="@string/title_activity_authentication" >
29+
</activity>
30+
</application>
31+
2932
</manifest>

lint.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<lint>
3+
</lint>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:tools="http://schemas.android.com/tools"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:paddingBottom="@dimen/activity_vertical_margin"
6+
android:paddingLeft="@dimen/activity_horizontal_margin"
7+
android:paddingRight="@dimen/activity_horizontal_margin"
8+
android:paddingTop="@dimen/activity_vertical_margin"
9+
tools:context=".AuthenticationActivity" >
10+
11+
<WebView
12+
android:id="@+id/wvAuthorise"
13+
android:layout_width="match_parent"
14+
android:layout_height="fill_parent"
15+
/>
16+
</RelativeLayout>

res/layout/activity_main.xml

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,14 @@
1010
android:paddingTop="@dimen/activity_vertical_margin"
1111
tools:context=".MainActivity" >
1212

13-
<WebView
14-
android:id="@+id/wvAuthorise"
15-
android:layout_width="match_parent"
16-
android:layout_height="fill_parent"
17-
android:layout_weight="999999" />
18-
19-
<TextView
20-
android:id="@+id/TextView01"
21-
android:layout_width="wrap_content"
22-
android:layout_height="wrap_content"
23-
android:layout_gravity="center_horizontal"
24-
android:layout_weight="0"
25-
android:text="Once you have pin, paste it in here..." />
26-
2713
<EditText
2814
android:id="@+id/etPIN"
2915
android:layout_width="match_parent"
3016
android:layout_height="wrap_content"
3117
android:layout_weight="0"
3218
android:ems="10" >
33-
34-
<requestFocus />
3519
</EditText>
36-
20+
3721
<TextView
3822
android:id="@+id/textView1"
3923
android:layout_width="wrap_content"
@@ -42,6 +26,7 @@
4226
android:layout_weight="0"
4327
android:text="Then click this..." />
4428

29+
4530
<Button
4631
android:id="@+id/button1"
4732
android:layout_width="wrap_content"

res/menu/authentication.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<menu xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:tools="http://schemas.android.com/tools"
3+
tools:context="com.betaminus.fitbitsample.AuthenticationActivity" >
4+
5+
<item
6+
android:id="@+id/action_settings"
7+
android:orderInCategory="100"
8+
android:showAsAction="never"
9+
android:title="@string/action_settings"/>
10+
11+
</menu>

res/values-w820dp/dimens.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<resources>
2+
3+
<!--
4+
Example customization of dimensions originally defined in res/values/dimens.xml
5+
(such as screen margins) for screens with more than 820dp of available width. This
6+
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively).
7+
-->
8+
<dimen name="activity_horizontal_margin">64dp</dimen>
9+
10+
</resources>

res/values/strings.xml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
<?xml version="1.0" encoding="utf-8"?>
2-
<resources>
3-
4-
<string name="app_name">Fitbit Plugin for Pebble Canvas</string>
5-
<string name="action_settings">Settings</string>
6-
<string name="hello_world">Hello world!</string>
7-
8-
</resources>
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
4+
<string name="app_name">Fitbit Plugin for Pebble Canvas</string>
5+
<string name="action_settings">Settings</string>
6+
<string name="hello_world">Hello world!</string>
7+
<string name="title_activity_authentication">AuthenticationActivity</string>
8+
9+
</resources>
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.betaminus.fitbitsample;
2+
3+
import org.scribe.builder.ServiceBuilder;
4+
5+
import android.annotation.SuppressLint;
6+
import android.app.Activity;
7+
import android.content.Intent;
8+
import android.os.Bundle;
9+
import android.webkit.WebView;
10+
import android.webkit.WebViewClient;
11+
12+
@SuppressLint("SetJavaScriptEnabled")
13+
public class AuthenticationActivity extends Activity {
14+
15+
@Override
16+
protected void onCreate(Bundle savedInstanceState) {
17+
super.onCreate(savedInstanceState);
18+
setContentView(R.layout.activity_authentication);
19+
final WebView wvAuthorise = (WebView) findViewById(R.id.wvAuthorise);
20+
wvAuthorise.getSettings().setJavaScriptEnabled(true);
21+
wvAuthorise.addJavascriptInterface(new MyJavaScriptInterface(/*this*/), "HtmlViewer");
22+
wvAuthorise.setWebViewClient(new WebViewClient() {
23+
@Override
24+
public void onPageFinished(WebView view, String url) {
25+
wvAuthorise.loadUrl("javascript:window.HtmlViewer.showHTML" +
26+
"('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');");
27+
}
28+
});
29+
30+
// Replace these with your own api key and secret
31+
String apiKey = "5dbccac0710c4d2ca0814f232beac1b5";
32+
String apiSecret = "f7e6a726b0eb49829816f75fccd30a54";
33+
34+
MainActivity.service = new ServiceBuilder().provider(FitbitApi.class).apiKey(apiKey)
35+
.apiSecret(apiSecret).build();
36+
37+
// network operation shouldn't run on main thread
38+
new Thread(new Runnable() {
39+
public void run() {
40+
MainActivity.requestToken = MainActivity.service.getRequestToken();
41+
final String authURL = MainActivity.service
42+
.getAuthorizationUrl(MainActivity.requestToken);
43+
44+
// Webview nagivation should run on main thread again...
45+
wvAuthorise.post(new Runnable() {
46+
@Override
47+
public void run() {
48+
wvAuthorise.loadUrl(authURL);
49+
}
50+
});
51+
}
52+
}).start();
53+
}
54+
55+
class MyJavaScriptInterface
56+
{
57+
boolean firstRun=true;
58+
//Context ctx;
59+
public MyJavaScriptInterface() {
60+
}
61+
/*public MyJavaScriptInterface(Context _ctx) {
62+
ctx = _ctx;
63+
}*/
64+
65+
public void showHTML(final String html) {
66+
if(firstRun){
67+
firstRun=false;
68+
return;
69+
}
70+
71+
try {
72+
73+
String divStr = "gap20\">";
74+
int first = html.indexOf(divStr);
75+
int second = html.indexOf("</div>",first);
76+
77+
if(first!=-1){
78+
final String pin = html.substring(first+divStr.length(),second);
79+
Intent intent = new Intent();
80+
intent.putExtra("PIN",pin);
81+
setResult(RESULT_OK,intent);
82+
finish();
83+
}
84+
else
85+
{
86+
/*new AlertDialog.Builder(ctx).setTitle("HTML").setMessage("first = "+first+" , second = "+second)
87+
.setPositiveButton(android.R.string.ok, null).setCancelable(false).create().show();*/
88+
}
89+
90+
} catch (Exception e) {
91+
/*new AlertDialog.Builder(ctx).setTitle("HTML").setMessage(e.getMessage())
92+
.setPositiveButton(android.R.string.ok, null).setCancelable(false).create().show();*/
93+
}
94+
}
95+
}
96+
}
Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,32 @@
11
package com.betaminus.fitbitsample;
22

3-
import org.json.JSONException;
4-
import org.json.JSONObject;
5-
import org.scribe.builder.ServiceBuilder;
63
import org.scribe.model.OAuthRequest;
74
import org.scribe.model.Response;
85
import org.scribe.model.Token;
96
import org.scribe.model.Verb;
107
import org.scribe.model.Verifier;
118
import org.scribe.oauth.OAuthService;
129

13-
import android.net.Uri;
14-
import android.os.Bundle;
1510
import android.app.Activity;
16-
import android.view.Menu;
11+
import android.content.Intent;
12+
import android.os.Bundle;
1713
import android.view.View;
18-
import android.webkit.WebView;
19-
import android.webkit.WebViewClient;
2014
import android.widget.EditText;
2115
import android.widget.TextView;
22-
import android.widget.Toast;
16+
2317

2418
public class MainActivity extends Activity {
25-
// Additions to Scribe taken from https://github.com/apakulov/scribe-java
26-
// General flow of Android oauth app taken from
27-
// http://schwiz.net/blog/2011/using-scribe-with-android/
2819

29-
OAuthService service;
30-
Token requestToken;
20+
static final int GET_PIN_REQUEST = 101; // The request code
21+
static OAuthService service;
22+
static Token requestToken;
3123

3224
@Override
3325
protected void onCreate(Bundle savedInstanceState) {
3426
super.onCreate(savedInstanceState);
3527
setContentView(R.layout.activity_main);
36-
37-
final WebView wvAuthorise = (WebView) findViewById(R.id.wvAuthorise);
38-
final EditText etPIN = (EditText) findViewById(R.id.etPIN);
39-
40-
// Replace these with your own api key and secret
41-
String apiKey = "apikey";
42-
String apiSecret = "apisecret";
43-
44-
service = new ServiceBuilder().provider(FitbitApi.class).apiKey(apiKey)
45-
.apiSecret(apiSecret).build();
46-
47-
// network operation shouldn't run on main thread
48-
new Thread(new Runnable() {
49-
public void run() {
50-
requestToken = service.getRequestToken();
51-
final String authURL = service
52-
.getAuthorizationUrl(requestToken);
53-
54-
// Webview nagivation should run on main thread again...
55-
wvAuthorise.post(new Runnable() {
56-
@Override
57-
public void run() {
58-
wvAuthorise.loadUrl(authURL);
59-
}
60-
});
61-
}
62-
}).start();
28+
29+
startActivityForResult(new Intent(this,AuthenticationActivity.class),GET_PIN_REQUEST);
6330
}
6431

6532
public void btnRetrieveData(View view) {
@@ -90,4 +57,27 @@ public void run() {
9057
}
9158
}).start();
9259
}
60+
61+
@Override
62+
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
63+
super.onActivityResult(requestCode, resultCode, intent);
64+
65+
if (requestCode == GET_PIN_REQUEST) {
66+
67+
if (resultCode == RESULT_OK) {
68+
Bundle extras = intent.getExtras();
69+
if(extras != null){
70+
final String pin = extras.getString("PIN");
71+
final EditText etPIN = (EditText) findViewById(R.id.etPIN);
72+
73+
etPIN.post(new Runnable() {
74+
@Override
75+
public void run() {
76+
etPIN.setText(pin);
77+
}
78+
});
79+
}
80+
}
81+
}
82+
}
9383
}

0 commit comments

Comments
 (0)