4848import com .google .android .gms .R ;
4949
5050import org .json .JSONArray ;
51+ import org .microg .gms .accountaction .AccountNotificationKt ;
5152import org .microg .gms .auth .AuthConstants ;
5253import org .microg .gms .auth .AuthManager ;
5354import org .microg .gms .auth .AuthRequest ;
@@ -85,10 +86,12 @@ public class LoginActivity extends AssistantActivity {
8586 public static final String EXTRA_TMPL = "tmpl" ;
8687 public static final String EXTRA_EMAIL = "email" ;
8788 public static final String EXTRA_TOKEN = "masterToken" ;
89+ public static final String EXTRA_RE_AUTH_ACCOUNT = "re_auth_account" ;
8890 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000 ;
8991
9092 private static final String TAG = "GmsAuthLoginBrowser" ;
9193 private static final String EMBEDDED_SETUP_URL = "https://accounts.google.com/EmbeddedSetup" ;
94+ private static final String EMBEDDED_RE_AUTH_URL = "https://accounts.google.com/embedded/reauth/v2/android" ;
9295 private static final String PROGRAMMATIC_AUTH_URL = "https://accounts.google.com/o/oauth2/programmatic_auth" ;
9396 private static final String GOOGLE_SUITE_URL = "https://accounts.google.com/signin/continue" ;
9497 private static final String MAGIC_USER_AGENT = " MinuteMaid" ;
@@ -106,6 +109,8 @@ public class LoginActivity extends AssistantActivity {
106109 private InputMethodManager inputMethodManager ;
107110 private ViewGroup authContent ;
108111 private int state = 0 ;
112+ private boolean isReAuth = false ;
113+ private Account reAuthAccount ;
109114
110115 @ SuppressLint ("AddJavascriptInterface" )
111116 @ Override
@@ -148,6 +153,10 @@ public void onPageFinished(WebView view, String url) {
148153 response = (AccountAuthenticatorResponse ) tempObject ;
149154 }
150155 }
156+ if (getIntent ().hasExtra (EXTRA_RE_AUTH_ACCOUNT )) {
157+ reAuthAccount = getIntent ().getParcelableExtra (EXTRA_RE_AUTH_ACCOUNT );
158+ isReAuth = reAuthAccount != null ;
159+ }
151160 if (getIntent ().hasExtra (EXTRA_TOKEN )) {
152161 if (getIntent ().hasExtra (EXTRA_EMAIL )) {
153162 AccountManager accountManager = AccountManager .get (this );
@@ -160,7 +169,7 @@ public void onPageFinished(WebView view, String url) {
160169 } else {
161170 retrieveRtToken (getIntent ().getStringExtra (EXTRA_TOKEN ));
162171 }
163- } else if (android .os .Build .VERSION .SDK_INT < 21 ) {
172+ } else if (android .os .Build .VERSION .SDK_INT < 21 || isReAuth ) {
164173 init ();
165174 } else {
166175 setMessage (R .string .auth_before_connect );
@@ -320,26 +329,10 @@ private void retrieveRtToken(String oAuthToken) {
320329 @ Override
321330 public void onResponse (AuthResponse response ) {
322331 Account account = new Account (response .email , accountType );
323- if (accountManager .addAccountExplicitly (account , response .token , null )) {
324- accountManager .setAuthToken (account , "SID" , response .Sid );
325- accountManager .setAuthToken (account , "LSID" , response .LSid );
326- accountManager .setUserData (account , "flags" , "1" );
327- accountManager .setUserData (account , "services" , response .services );
328- accountManager .setUserData (account , "oauthAccessToken" , "1" );
329- accountManager .setUserData (account , "firstName" , response .firstName );
330- accountManager .setUserData (account , "lastName" , response .lastName );
331- if (!TextUtils .isEmpty (response .accountId ))
332- accountManager .setUserData (account , "GoogleUserId" , response .accountId );
333-
334- retrieveGmsToken (account );
335- setResult (RESULT_OK );
332+ if (isReAuth && reAuthAccount != null && reAuthAccount .name .equals (account .name )) {
333+ accountManager .removeAccount (account , future -> saveAccount (account , response ), null );
336334 } else {
337- Log .w (TAG , "Account NOT created!" );
338- runOnUiThread (() -> {
339- showError (R .string .auth_general_error_desc );
340- setNextButtonText (android .R .string .ok );
341- });
342- state = -2 ;
335+ saveAccount (account , response );
343336 }
344337 }
345338
@@ -354,7 +347,35 @@ public void onException(Exception exception) {
354347 }
355348 });
356349 }
350+
351+ private void saveAccount (Account account , AuthResponse response ) {
352+ if (accountManager .addAccountExplicitly (account , response .token , null )) {
353+ accountManager .setAuthToken (account , "SID" , response .Sid );
354+ accountManager .setAuthToken (account , "LSID" , response .LSid );
355+ accountManager .setUserData (account , "flags" , "1" );
356+ accountManager .setUserData (account , "services" , response .services );
357+ accountManager .setUserData (account , "oauthAccessToken" , "1" );
358+ accountManager .setUserData (account , "firstName" , response .firstName );
359+ accountManager .setUserData (account , "lastName" , response .lastName );
360+ if (!TextUtils .isEmpty (response .accountId ))
361+ accountManager .setUserData (account , "GoogleUserId" , response .accountId );
362+
363+ retrieveGmsToken (account );
364+ setResult (RESULT_OK );
365+ } else {
366+ Log .w (TAG , "Account NOT created!" );
367+ runOnUiThread (() -> {
368+ showError (R .string .auth_general_error_desc );
369+ setNextButtonText (android .R .string .ok );
370+ });
371+ state = -2 ;
372+ }
373+ }
374+
357375 private void returnSuccessResponse (Account account ){
376+ if (isReAuth && reAuthAccount != null ) {
377+ AccountNotificationKt .cancelAccountNotificationChannel (this , reAuthAccount );
378+ }
358379 if (response != null ){
359380 Bundle bd = new Bundle ();
360381 bd .putString (AccountManager .KEY_ACCOUNT_NAME ,account .name );
@@ -426,16 +447,20 @@ public boolean onKeyDown(int keyCode, KeyEvent event) {
426447 return super .onKeyDown (keyCode , event );
427448 }
428449
429- private static String buildUrl (String tmpl , Locale locale ) {
430- return Uri .parse (EMBEDDED_SETUP_URL ).buildUpon ()
450+ private String buildUrl (String tmpl , Locale locale ) {
451+ String uriString = isReAuth ? EMBEDDED_RE_AUTH_URL : EMBEDDED_SETUP_URL ;
452+ Uri .Builder builder = Uri .parse (uriString ).buildUpon ()
431453 .appendQueryParameter ("source" , "android" )
432454 .appendQueryParameter ("xoauth_display_name" , "Android Device" )
433455 .appendQueryParameter ("lang" , locale .getLanguage ())
434456 .appendQueryParameter ("cc" , locale .getCountry ().toLowerCase (Locale .US ))
435457 .appendQueryParameter ("langCountry" , locale .toString ().toLowerCase (Locale .US ))
436458 .appendQueryParameter ("hl" , locale .toString ().replace ("_" , "-" ))
437- .appendQueryParameter ("tmpl" , tmpl )
438- .build ().toString ();
459+ .appendQueryParameter ("tmpl" , tmpl );
460+ if (isReAuth && reAuthAccount != null ) {
461+ builder .appendQueryParameter ("Email" , reAuthAccount .name );
462+ }
463+ return builder .build ().toString ();
439464 }
440465
441466 private class JsBridge {
0 commit comments