Skip to content

Commit 3d95498

Browse files
committed
Optimize the code logic.
1 parent b04ba6b commit 3d95498

4 files changed

Lines changed: 41 additions & 15 deletions

File tree

play-services-core/src/main/kotlin/org/microg/gms/accountsettings/ui/MainActivity.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ import org.json.JSONException
2626
import org.json.JSONObject
2727
import org.microg.gms.auth.AuthConstants
2828
import org.microg.gms.common.Constants
29+
import org.microg.gms.common.Constants.GMS_PACKAGE_NAME
30+
import org.microg.gms.gcm.ACTION_GCM_NOTIFY_COMPLETE
31+
import org.microg.gms.gcm.EXTRA_NOTIFICATION_ACCOUNT
2932
import org.microg.gms.people.PeopleManager
3033

3134
private const val TAG = "AccountSettings"
@@ -136,6 +139,7 @@ class MainActivity : AppCompatActivity() {
136139
private lateinit var webView: WebView
137140
private var accountName: String? = null
138141
private var resultBundle: Bundle? = null
142+
private var is2StepVerification: Boolean = false
139143

140144
private fun getSelectedAccountName(): String? = null
141145

@@ -148,6 +152,7 @@ class MainActivity : AppCompatActivity() {
148152
val product = intent?.getStringExtra(EXTRA_SCREEN_MY_ACTIVITY_PRODUCT)
149153
val kidOnboardingParams = intent?.getStringExtra(EXTRA_SCREEN_KID_ONBOARDING_PARAMS)
150154
val screenUrl = intent?.getStringExtra(EXTRA_URL)
155+
is2StepVerification = intent?.getBooleanExtra(KEY_IS_2_STEP_VERIFICATION, false) ?: false
151156

152157
val screenOptions = intent.extras?.keySet().orEmpty()
153158
.filter { it.startsWith(EXTRA_SCREEN_OPTIONS_PREFIX) }
@@ -200,6 +205,12 @@ class MainActivity : AppCompatActivity() {
200205
}
201206

202207
override fun onDestroy() {
208+
if (is2StepVerification) {
209+
Intent(ACTION_GCM_NOTIFY_COMPLETE).apply {
210+
setPackage(GMS_PACKAGE_NAME)
211+
putExtra(EXTRA_NOTIFICATION_ACCOUNT, accountName)
212+
}.let { sendBroadcast(it) }
213+
}
203214
super.onDestroy()
204215
}
205216

play-services-core/src/main/kotlin/org/microg/gms/accountsettings/ui/WebViewHelper.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,6 @@ class WebViewHelper(private val activity: AppCompatActivity, private val webView
6464
}
6565
val overrideUri = Uri.parse(url)
6666
if (overrideUri.getQueryParameter(QUERY_GNOTS_ACTION) == ACTION_CLOSE || overrideUri.getQueryParameter(QUERY_WC_ACTION) == ACTION_CLOSE) {
67-
Intent(ACTION_GCM_NOTIFY_COMPLETE).apply {
68-
setPackage(GMS_PACKAGE_NAME)
69-
putExtra(EXTRA_NOTIFICATION_ACCOUNT, accountName)
70-
}.let { activity.sendBroadcast(it) }
7167
activity.finish()
7268
return true
7369
}

play-services-core/src/main/kotlin/org/microg/gms/accountsettings/ui/extensions.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@ const val QUERY_GNOTS_ACTION = "gnotswvaction"
2929
const val ACTION_CLOSE = "close"
3030

3131
const val KEY_UPDATED_PHOTO_URL = "updatedPhotoUrl"
32+
const val KEY_IS_2_STEP_VERIFICATION = "is2StepVerification"
3233

3334
const val OPTION_SCREEN_FLAVOR = "screenFlavor"

play-services-core/src/main/kotlin/org/microg/gms/gcm/GcmInGmsService.kt

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import kotlinx.coroutines.withContext
3838
import okhttp3.Interceptor
3939
import okhttp3.OkHttpClient
4040
import okio.ByteString
41+
import org.microg.gms.accountsettings.ui.KEY_IS_2_STEP_VERIFICATION
4142
import org.microg.gms.accountsettings.ui.MainActivity
4243
import org.microg.gms.auth.AuthConstants
4344
import org.microg.gms.auth.AuthManager
@@ -112,7 +113,9 @@ class GcmInGmsService : LifecycleService() {
112113
Log.d(TAG, "onStartCommand: $intent")
113114
lifecycleScope.launchWhenStarted {
114115
if (checkGcmStatus()) {
115-
handleIntent(intent)
116+
withContext(Dispatchers.IO) {
117+
handleIntent(intent)
118+
}
116119
} else {
117120
val intent = Intent(ACTION_GCM_RECONNECT).apply {
118121
setPackage(Constants.GMS_PACKAGE_NAME)
@@ -169,6 +172,7 @@ class GcmInGmsService : LifecycleService() {
169172
if (GMS_GCM_NOTIFICATIONS != gcmBodyType) return
170173
val payloadData = data.getString(GcmConstants.EXTRA_GMS_GNOTS_PAYLOAD) ?: return
171174
val notificationData = NotificationData.ADAPTER.decode(Base64.decode(payloadData, DEFAULT_FLAGS))
175+
Log.w(TAG, "notifyVerificationInfo: $notificationData")
172176
if (notificationData.isActive == true) return
173177
val account = notificationData.userInfo?.userId?.let { id ->
174178
accountManager?.getAccountsByType(AuthConstants.DEFAULT_ACCOUNT_TYPE)?.find {
@@ -209,6 +213,7 @@ class GcmInGmsService : LifecycleService() {
209213
val intent = Intent(this, MainActivity::class.java).apply {
210214
`package` = Constants.GMS_PACKAGE_NAME
211215
flags = Intent.FLAG_ACTIVITY_NEW_TASK
216+
putExtra(KEY_IS_2_STEP_VERIFICATION, true)
212217
intentExtras.forEach { putExtra(it.key, it.value_) }
213218
}
214219
val requestCode = notificationIdGenerator.incrementAndGet()
@@ -315,16 +320,29 @@ class GcmInGmsService : LifecycleService() {
315320
}
316321
try {
317322
val identifier = notificationData.identifier
318-
val actionButtons = notificationData.content?.actionButtons
319-
if (actionButtons.isNullOrEmpty()) {
320-
return
321-
}
322-
val readStateList = actionButtons.map { actionButton ->
323-
ReadStateItem.Builder().apply {
324-
notification = identifier
325-
state = actionButton.icon
326-
status = readState
327-
}.build()
323+
val readStateList = when {
324+
readState == NOTIFICATION_STATUS_COMPLETE -> {
325+
listOf(
326+
ReadStateItem.Builder().apply {
327+
this.notification = identifier
328+
this.state = null
329+
this.status = readState
330+
}.build()
331+
)
332+
}
333+
notificationData.content?.actionButtons.isNullOrEmpty() -> {
334+
Log.w(TAG, "No action buttons found, skipping read state update.")
335+
return
336+
}
337+
else -> {
338+
notificationData.content!!.actionButtons.map {
339+
ReadStateItem.Builder().apply {
340+
this.notification = identifier
341+
this.state = it.icon
342+
this.status = readState
343+
}.build()
344+
}
345+
}
328346
}
329347
sendNotificationReadState(accountName, ReadStateList.Builder().apply { items = readStateList }.build())
330348
Log.i(TAG, "Notification read state updated successfully for account: $accountName")

0 commit comments

Comments
 (0)