4
4
5
5
package io .afero .aferolab ;
6
6
7
+ import android .app .Activity ;
7
8
import android .content .BroadcastReceiver ;
8
9
import android .content .Context ;
9
10
import android .content .Intent ;
10
11
import android .content .IntentFilter ;
11
12
import android .net .ConnectivityManager ;
12
13
import android .net .NetworkInfo ;
14
+ import android .net .Uri ;
13
15
import android .os .Bundle ;
14
16
import android .support .annotation .NonNull ;
15
17
import android .support .v7 .app .AppCompatActivity ;
19
21
import android .view .MenuItem ;
20
22
import android .view .View ;
21
23
import android .view .ViewGroup ;
24
+ import android .widget .Button ;
22
25
import android .widget .TextView ;
23
26
27
+ import androidx .activity .result .ActivityResultLauncher ;
28
+ import androidx .activity .result .contract .ActivityResultContracts ;
29
+
30
+ import net .openid .appauth .AuthState ;
31
+ import net .openid .appauth .AuthorizationException ;
32
+ import net .openid .appauth .AuthorizationRequest ;
33
+ import net .openid .appauth .AuthorizationResponse ;
34
+ import net .openid .appauth .AuthorizationService ;
35
+ import net .openid .appauth .AuthorizationServiceConfiguration ;
36
+ import net .openid .appauth .ResponseTypeValues ;
37
+
24
38
import java .lang .ref .WeakReference ;
25
39
import java .net .HttpURLConnection ;
26
40
@@ -74,6 +88,11 @@ public class MainActivity extends AppCompatActivity {
74
88
private AferoSofthub mAferoSofthub ;
75
89
private ConnectivityReceiver mConnectivityReceiver ;
76
90
91
+ private ActivityResultLauncher <Intent > launcher ;
92
+ private AuthorizationService mAuthService ;
93
+ private AuthorizationServiceConfiguration mServiceConfig ;
94
+ private AuthState mAuthState ;
95
+
77
96
private String mUserId ;
78
97
79
98
private final Observer <AferoSofthub > mAferoSofthubStartObserver = new RxUtils .IgnoreResponseObserver <>();
@@ -99,6 +118,12 @@ public class MainActivity extends AppCompatActivity {
99
118
@ BindView (R .id .edit_text_password )
100
119
AferoEditText mPasswordEditText ;
101
120
121
+ @ BindView (R .id .button_forgot_password )
122
+ Button mForgotPasswordButton ;
123
+
124
+ @ BindView (R .id .button_sign_in )
125
+ Button mSignInButton ;
126
+
102
127
@ BindView (R .id .group_sign_in )
103
128
ViewGroup mSignInGroup ;
104
129
@@ -135,14 +160,38 @@ protected void onCreate(Bundle savedInstanceState) {
135
160
? new AccessToken (accessToken , refreshToken )
136
161
: null ;
137
162
138
- AferoClientRetrofit2 .Config aferoClientConfig = new AferoClientRetrofit2 .ConfigBuilder ()
163
+ AferoClientRetrofit2 .ConfigBuilder configBuilder = new AferoClientRetrofit2 .ConfigBuilder ()
139
164
.oauthClientId (BuildConfig .AFERO_CLIENT_ID )
140
- .oauthClientSecret (BuildConfig .AFERO_CLIENT_SECRET )
141
165
.baseUrl (BuildConfig .AFERO_SERVICE_URL )
142
- .logLevel (BuildConfig .HTTP_LOG_LEVEL )
143
- .build ();
166
+ .logLevel (BuildConfig .HTTP_LOG_LEVEL );
167
+
168
+ if (BuildConfig .AFERO_CLIENT_SECRET != null ) {
169
+ configBuilder .oauthClientSecret (BuildConfig .AFERO_CLIENT_SECRET );
170
+ }
171
+
172
+ if (BuildConfig .AFERO_OAUTH_AUTH_URL != null && BuildConfig .AFERO_OAUTH_TOKEN_URL != null ) {
173
+ mServiceConfig =
174
+ new AuthorizationServiceConfiguration (
175
+ Uri .parse (BuildConfig .AFERO_OAUTH_AUTH_URL ), // authorization endpoint
176
+ Uri .parse (BuildConfig .AFERO_OAUTH_TOKEN_URL ));
177
+ mAuthState = new AuthState (mServiceConfig );
178
+ launcher = registerForActivityResult (
179
+ new ActivityResultContracts .StartActivityForResult (),
180
+ result -> {
181
+ if (result .getResultCode () == Activity .RESULT_OK ) {
182
+ // There are no request codes
183
+ Intent data = result .getData ();
184
+ AuthorizationResponse response = AuthorizationResponse .fromIntent (data );
185
+ AuthorizationException ex = AuthorizationException .fromIntent (data );
186
+
187
+ System .out .println ("Access Token " + response .authorizationCode );
188
+ exchangeAuthorizationCode (response );
189
+ }
190
+ });
191
+ }
144
192
145
- mAferoClient = new AferoClientRetrofit2 (aferoClientConfig );
193
+
194
+ mAferoClient = new AferoClientRetrofit2 (configBuilder .build ());
146
195
mAferoClient .setOwnerAndActiveAccountId (accountId );
147
196
148
197
mDeviceCollection = new DeviceCollection (mAferoClient );
@@ -320,6 +369,12 @@ private void stopDeviceInspector() {
320
369
}
321
370
322
371
private void setupViews () {
372
+ if (BuildConfig .AFERO_OAUTH_AUTH_URL != null ) {
373
+ mEmailEditText .setVisibility (View .GONE );
374
+ mPasswordEditText .setVisibility (View .GONE );
375
+ mForgotPasswordButton .setVisibility (View .GONE );
376
+ }
377
+
323
378
if (isSignedIn ()) {
324
379
mSignInGroup .setVisibility (View .GONE );
325
380
mStatusGroup .setVisibility (View .VISIBLE );
@@ -380,8 +435,30 @@ public boolean onEditorActionSignIn(TextView textView, int actionId, KeyEvent ev
380
435
381
436
@ OnClick (R .id .button_sign_in )
382
437
public void onClickSignIn () {
383
- mPasswordEditText .hideKeyboard ();
384
- startSignIn (mEmailEditText .getText ().toString (), mPasswordEditText .getText ().toString ());
438
+ if (BuildConfig .AFERO_OAUTH_AUTH_URL != null ) {
439
+
440
+ mSignInButton .setEnabled (false );
441
+ showConclaveStatus (ConclaveClient .Status .CONNECTING );
442
+
443
+ AuthorizationRequest .Builder authRequestBuilder =
444
+ new AuthorizationRequest .Builder (
445
+ mServiceConfig , // the authorization service configuration
446
+ "hubspace_android" ,
447
+ // the client ID, typically pre-registered and static
448
+ ResponseTypeValues .CODE , // the response_type value: we want a code
449
+ Uri .parse ("hubspace-app://loginredirect" )
450
+ );
451
+
452
+
453
+ mAuthService = new AuthorizationService (this );
454
+ Intent authIntent = mAuthService .getAuthorizationRequestIntent (authRequestBuilder .build ());
455
+
456
+ launcher .launch (authIntent );
457
+ } else {
458
+
459
+ mPasswordEditText .hideKeyboard ();
460
+ startSignIn (mEmailEditText .getText ().toString (), mPasswordEditText .getText ().toString ());
461
+ }
385
462
}
386
463
387
464
@ OnClick (R .id .button_forgot_password )
0 commit comments