11package com .betaminus .fitbitsample ;
22
3- import org .json .JSONException ;
4- import org .json .JSONObject ;
5- import org .scribe .builder .ServiceBuilder ;
63import org .scribe .model .OAuthRequest ;
74import org .scribe .model .Response ;
85import org .scribe .model .Token ;
96import org .scribe .model .Verb ;
107import org .scribe .model .Verifier ;
118import org .scribe .oauth .OAuthService ;
129
13- import android .net .Uri ;
14- import android .os .Bundle ;
1510import android .app .Activity ;
16- import android .view .Menu ;
11+ import android .content .Intent ;
12+ import android .os .Bundle ;
1713import android .view .View ;
18- import android .webkit .WebView ;
19- import android .webkit .WebViewClient ;
2014import android .widget .EditText ;
2115import android .widget .TextView ;
22- import android . widget . Toast ;
16+
2317
2418public 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