Skip to content

Commit 50c7046

Browse files
authored
Merge branch 'microg:master' into patch-names
2 parents 4cf95fd + b9b7c06 commit 50c7046

File tree

5 files changed

+105
-27
lines changed

5 files changed

+105
-27
lines changed

play-services-core/src/main/java/org/microg/gms/auth/login/LoginActivity.java

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import com.google.android.gms.R;
4949

5050
import org.json.JSONArray;
51+
import org.microg.gms.accountaction.AccountNotificationKt;
5152
import org.microg.gms.auth.AuthConstants;
5253
import org.microg.gms.auth.AuthManager;
5354
import 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 {

play-services-core/src/main/kotlin/org/microg/gms/accountaction/AccountNotification.kt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,47 @@ import android.content.Context
1010
import android.content.Intent
1111
import android.content.pm.PackageManager
1212
import android.os.Build
13+
import android.util.Log
1314
import androidx.annotation.RequiresApi
1415
import androidx.core.app.ActivityCompat
1516
import androidx.core.app.NotificationCompat
1617
import androidx.core.app.NotificationManagerCompat
1718
import com.google.android.gms.R
19+
import org.microg.gms.auth.login.LoginActivity
1820

1921
private const val CHANNEL_ID = "AccountNotification"
2022

23+
@RequiresApi(21)
24+
fun Context.sendAccountReAuthNotification(account: Account) {
25+
Log.d(TAG, "sendAccountReAuthNotification: account: ${account.name}")
26+
27+
registerAccountNotificationChannel()
28+
29+
val intent = Intent(this, LoginActivity::class.java).apply {
30+
putExtra(LoginActivity.EXTRA_RE_AUTH_ACCOUNT, account)
31+
}.let {
32+
PendingIntent.getActivity(
33+
this, account.hashCode(), it, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_CANCEL_CURRENT
34+
)
35+
}
36+
37+
val notification: Notification =
38+
NotificationCompat.Builder(this, CHANNEL_ID)
39+
.setSmallIcon(R.drawable.ic_manage_accounts)
40+
.setSound(null)
41+
.setContentTitle(getString(R.string.auth_action_reauth_notification_title))
42+
.setContentText(account.name)
43+
.setOnlyAlertOnce(true)
44+
.setContentIntent(intent)
45+
.setAutoCancel(true)
46+
.build()
47+
48+
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) ==
49+
PackageManager.PERMISSION_GRANTED
50+
) {
51+
NotificationManagerCompat.from(this).notify(account.hashCode(), notification)
52+
}
53+
}
2154

2255
@RequiresApi(21)
2356
fun Context.sendAccountActionNotification(account: Account, action: UserSatisfyRequirements) {
@@ -67,4 +100,8 @@ fun Context.registerAccountNotificationChannel() {
67100
getSystemService(NotificationManager::class.java)
68101
.createNotificationChannel(channel)
69102
}
103+
}
104+
105+
fun Context.cancelAccountNotificationChannel(account: Account) {
106+
NotificationManagerCompat.from(this).cancel(account.hashCode())
70107
}

play-services-core/src/main/kotlin/org/microg/gms/accountaction/ErrorResolver.kt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package org.microg.gms.accountaction
22

33
import android.accounts.Account
44
import android.content.Context
5-
import android.os.Build
5+
import android.content.Intent
6+
import android.os.Build.VERSION.SDK_INT
67
import android.util.Log
78
import kotlinx.coroutines.runBlocking
9+
import org.microg.gms.auth.login.LoginActivity
810
import org.microg.gms.common.Constants
911
import org.microg.gms.cryptauth.isLockscreenConfigured
1012
import org.microg.gms.cryptauth.sendDeviceScreenlockState
@@ -39,6 +41,8 @@ const val DEVICE_MANAGEMENT_ADMIN_PENDING_APPROVAL = "DeviceManagementAdminPendi
3941
*/
4042
const val BAD_AUTHENTICATION = "BadAuthentication"
4143

44+
const val SERVER_ERROR = "Error 500"
45+
4246
const val TAG = "GmsAccountErrorResolve"
4347

4448
/**
@@ -47,6 +51,8 @@ const val TAG = "GmsAccountErrorResolve"
4751
*/
4852
fun Context.resolveAuthErrorMessage(s: String): Resolution? = if (s.startsWith("Error=")) {
4953
resolveAuthErrorMessage(s.drop("Error=".length))
54+
} else if (s.contains(SERVER_ERROR)) {
55+
Reauthenticate
5056
} else when (s) {
5157
DEVICE_MANAGEMENT_SCREENLOCK_REQUIRED -> listOf(
5258
Requirement.ENABLE_CHECKIN,
@@ -123,13 +129,16 @@ fun <T> Resolution.initiateFromBackgroundBlocking(context: Context, account: Acc
123129
}
124130
is UserSatisfyRequirements -> {
125131
Log.w(TAG, "User intervention required! You need to ${actions.joinToString(", ")}.")
126-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
132+
if (SDK_INT >= 21) {
127133
context.sendAccountActionNotification(account, this)
128134
}
129135
return null
130136
}
131137
Reauthenticate -> {
132138
Log.w(TAG, "Your account credentials have expired! Please remove the account, then sign in again.")
139+
if (SDK_INT >= 21) {
140+
context.sendAccountReAuthNotification(account)
141+
}
133142
return null
134143
}
135144
}
@@ -150,7 +159,7 @@ fun <T> Resolution.initiateFromForegroundBlocking(context: Context, account: Acc
150159
}
151160
is UserSatisfyRequirements -> {
152161
Log.w(TAG, "User intervention required! You need to ${actions.joinToString(", ")}.")
153-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
162+
if (SDK_INT >= 21) {
154163
AccountActionActivity.createIntent(context, account, this).let {
155164
context.startActivity(it)
156165
}
@@ -159,6 +168,11 @@ fun <T> Resolution.initiateFromForegroundBlocking(context: Context, account: Acc
159168
}
160169
Reauthenticate -> {
161170
Log.w(TAG, "Your account credentials have expired! Please remove the account, then sign in again.")
171+
Intent(context, LoginActivity::class.java).apply {
172+
putExtra(LoginActivity.EXTRA_RE_AUTH_ACCOUNT, account)
173+
}.let {
174+
context.startActivity(it)
175+
}
162176
return null
163177
}
164178
}

play-services-core/src/main/res/values-zh-rCN/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ microG GmsCore 内置一套自由的 SafetyNet 实现,但是官方服务器要
281281
<string name="auth_action_notification_content">你的 Google 账户需要额外设置。</string>
282282
<string name="auth_action_activity_explanation">要能在这台设备上使用你的 Google 账户 %s 请完成下列步骤。</string>
283283
<string name="auth_action_step_enable_lockscreen_description">你的 Google 账户受工作场所或教育机构管理。你的管理员决定设备在可以访问账户数据前需要设置安全屏幕锁。\n\n请设置一个密码、PIN或手势屏幕锁。</string>
284+
<string name="auth_action_reauth_notification_title">需要执行账号相关操作</string>
284285
<string name="barcode_scanner_brand">由 microG 代表“%1$s”扫描</string>
285286
<string name="camera_permission_dialog_button">确定</string>
286287
<string name="camera_permission_dialog_message">microG 服务需要访问设备的摄像头,才能为%1$s扫描二维码。\n\n若要启用该权限,请在“设置”中向 microG 服务授予相机权限。</string>

play-services-core/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ This can take a couple of minutes."</string>
4343
<string name="auth_action_notification_title">Account action required</string>
4444
<string name="auth_action_notification_content">Your Google account needs additional setup.</string>
4545

46+
<string name="auth_action_reauth_notification_title">Account action required</string>
4647
<string name="auth_action_activity_header">Finish setting up your Google account</string>
4748
<string name="auth_action_activity_explanation">Complete the following steps to be able to use your Google account %s on this device.</string>
4849
<string name="auth_action_step_enable_checkin">Enable device registration</string>

0 commit comments

Comments
 (0)