2
2
3
3
import android .content .Intent ;
4
4
import android .content .SharedPreferences ;
5
- import android .support .v7 .app .AppCompatActivity ;
6
5
import android .os .Bundle ;
6
+ import android .support .v7 .app .AppCompatActivity ;
7
7
import android .util .Log ;
8
- import android .view .View ;
9
8
import android .webkit .WebView ;
10
9
import android .webkit .WebViewClient ;
11
- import android .widget .ProgressBar ;
12
10
13
11
import org .json .JSONException ;
14
12
import org .json .JSONObject ;
15
13
16
14
import java .io .IOException ;
17
15
import java .util .ArrayList ;
18
- import java .util .Arrays ;
19
16
import java .util .List ;
20
17
21
18
import okhttp3 .Call ;
@@ -31,51 +28,54 @@ public class OauthActivity extends AppCompatActivity {
31
28
public static final String GITHUB_OAUTH = "https://github.com/login/oauth/access_token" ;
32
29
private static final String TAG = "github-oauth" ;
33
30
public static String CODE = "" ;
31
+ public static String PACKAGE = "" ;
34
32
public static String CLIENT_ID = "" ;
35
33
public static String CLIENT_SECRET = "" ;
36
34
public static String ACTIVITY_NAME = "" ;
37
- public static String PACKAGE = "" ;
38
- private ProgressBar spinner ;
35
+ public String scopeAppendToUrl = "" ;
39
36
private WebView webview ;
40
37
private Class c ;
41
- private boolean isScopeDefined ;
42
- private boolean debug ;
38
+ private boolean clearDataBeforeLaunch = false ;
39
+ private boolean isScopeDefined = false ;
40
+ private boolean debug = false ;
43
41
public List <String > scopeList ;
44
- public String scopeAppendToUrl ;
45
42
46
43
@ Override
47
44
protected void onCreate (Bundle savedInstanceState ) {
48
45
super .onCreate (savedInstanceState );
49
46
setContentView (R .layout .activity_oauth );
47
+
50
48
scopeList = new ArrayList <>();
49
+ scopeAppendToUrl = "" ;
50
+
51
51
if (getIntent ().getExtras () != null ) {
52
52
CLIENT_ID = getIntent ().getStringExtra ("id" );
53
+ PACKAGE = getIntent ().getStringExtra ("package" );
53
54
CLIENT_SECRET = getIntent ().getStringExtra ("secret" );
54
55
ACTIVITY_NAME = getIntent ().getStringExtra ("activity" );
55
56
debug = getIntent ().getBooleanExtra ("debug" , false );
56
- PACKAGE = getIntent ().getStringExtra ("package" );
57
57
isScopeDefined = getIntent ().getBooleanExtra ("isScopeDefined" , false );
58
- if (isScopeDefined ) {
59
- scopeList = getIntent ().getStringArrayListExtra ("scope_list" );
60
- }
58
+ clearDataBeforeLaunch = getIntent ().getBooleanExtra ("clearData" , false );
61
59
} else {
62
60
Log .d (TAG , "intent extras null" );
61
+ finish ();
63
62
}
64
- if (debug )
65
- Log .d (TAG , "intent recieved is -client id :" + CLIENT_ID + "-secret:" + CLIENT_SECRET + "-activit : " + ACTIVITY_NAME + "-Package : " + PACKAGE );
66
- // try {
67
- // c = Class.forName(ACTIVITY_NAME);
68
- // } catch (ClassNotFoundException exp) {
69
- // if (debug)Log.d(TAG, "error :" + exp.getMessage());
70
- // }
71
- spinner = (ProgressBar ) findViewById (R .id .progressBar );
72
- spinner .setVisibility (View .VISIBLE );
63
+
64
+ String url_load = GITHUB_URL + "?client_id=" + CLIENT_ID ;
65
+
73
66
if (isScopeDefined ) {
67
+ scopeList = getIntent ().getStringArrayListExtra ("scope_list" );
74
68
scopeAppendToUrl = getCsvFromList (scopeList );
75
- } else {
76
- scopeAppendToUrl = "" ;
69
+ url_load += "&scope=" + scopeAppendToUrl ;
77
70
}
78
- Log .d (TAG , "onCreate: Scope request are : " + scopeAppendToUrl );
71
+
72
+ if (debug ) {
73
+ Log .d (TAG , "intent received is -client id :" + CLIENT_ID + "-secret:" + CLIENT_SECRET + "-activity : " + ACTIVITY_NAME + "-Package : " + PACKAGE );
74
+ Log .d (TAG , "onCreate: Scope request are : " + scopeAppendToUrl );
75
+ }
76
+
77
+ // TODO: 7/24/2017 Clear the cache and cookies of the webView if clearDataBeforeLaunch is true
78
+
79
79
webview = (WebView ) findViewById (R .id .webview );
80
80
webview .getSettings ().setJavaScriptEnabled (true );
81
81
webview .setWebViewClient (new WebViewClient () {
@@ -92,16 +92,13 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {
92
92
String [] cleanToken = tokenFetchedIs .split ("&" );
93
93
if (debug ) Log .d (TAG , "token cleaned is :" + cleanToken [0 ]);
94
94
fetchOauthTokenWithCode (cleanToken [0 ]);
95
- } catch (NullPointerException | ArrayIndexOutOfBoundsException e ) {}
95
+ } catch (NullPointerException | ArrayIndexOutOfBoundsException e ) {
96
+ e .printStackTrace ();
97
+ }
96
98
return false ;
97
99
}
98
100
});
99
- String url_load = "" ;
100
- if (isScopeDefined ) {
101
- url_load = GITHUB_URL + "?client_id=" + CLIENT_ID + "&scope=" + scopeAppendToUrl ;
102
- } else {
103
- url_load = GITHUB_URL + "?client_id=" + CLIENT_ID ;
104
- }
101
+
105
102
webview .loadUrl (url_load );
106
103
}
107
104
@@ -124,22 +121,35 @@ public void onFailure(Call call, IOException e) {
124
121
125
122
@ Override
126
123
public void onResponse (Call call , Response response ) throws IOException {
124
+
127
125
if (response .isSuccessful ()) {
128
126
String JsonData = response .body ().string ();
129
- if (debug ) Log .d (TAG , "response is:" + JsonData );
127
+
128
+ if (debug ) {
129
+ Log .d (TAG , "response is:" + JsonData );
130
+ }
131
+
130
132
try {
131
133
JSONObject jsonObject = new JSONObject (JsonData );
132
134
String auth_token = jsonObject .getString ("access_token" );
133
- if ( debug ) Log . d ( TAG , "token is :" + auth_token );
135
+
134
136
storeToSharedPreference (auth_token );
137
+
138
+ if (debug ) {
139
+ Log .d (TAG , "token is :" + auth_token );
140
+ }
141
+
135
142
} catch (JSONException exp ) {
136
- if (debug ) Log .d (TAG , "json exception :" + exp .getMessage ());
143
+ if (debug ) {
144
+ Log .d (TAG , "json exception :" + exp .getMessage ());
145
+ }
137
146
}
138
147
139
148
} else {
140
- Log .d (TAG , "onResponse: not success : " + response .message ());
149
+ if (debug ) {
150
+ Log .d (TAG , "onResponse: not success : " + response .message ());
151
+ }
141
152
}
142
-
143
153
}
144
154
});
145
155
}
@@ -157,22 +167,35 @@ public void onBackPressed() {
157
167
private void storeToSharedPreference (String auth_token ) {
158
168
SharedPreferences prefs = getSharedPreferences ("github_prefs" , MODE_PRIVATE );
159
169
SharedPreferences .Editor edit = prefs .edit ();
170
+
160
171
edit .putString ("oauth_token" , auth_token );
161
- edit .commit ();
172
+ edit .apply ();
173
+
174
+ // TODO: 7/24/2017 Only open a new activity if specified
162
175
Intent intent = new Intent ();
163
176
intent .setClassName (PACKAGE , ACTIVITY_NAME );
164
177
intent .addFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
165
178
startActivity (intent );
179
+
166
180
finish ();
167
181
}
168
182
183
+ /**
184
+ * Generate a comma separated list of scopes out of the
185
+ *
186
+ * @param scopeList list of scopes as defined
187
+ * @return comma separated list of scopes
188
+ */
169
189
public String getCsvFromList (List <String > scopeList ) {
170
190
String csvString = "" ;
171
191
172
- for (int i = 0 ; i < scopeList .size () - 1 ; i ++) {
173
- csvString = csvString + scopeList .get (i ) + "," ;
192
+ for (String scope : scopeList ) {
193
+ if (!csvString .equals ("" )) {
194
+ csvString += "," ;
195
+ }
196
+
197
+ csvString += scope ;
174
198
}
175
- csvString = csvString + scopeList .get (scopeList .size () - 1 );
176
199
177
200
return csvString ;
178
201
}
0 commit comments