From 79ae1056361233588265fe16f3ccfb54eddb2f31 Mon Sep 17 00:00:00 2001 From: davinci9196 Date: Fri, 6 Dec 2024 11:57:52 +0800 Subject: [PATCH] Synchronize the modified avatar to local --- .../org/microg/gms/people/PeopleManager.java | 12 ++++++++++++ .../gms/accountsettings/ui/MainActivity.kt | 18 ++++++++++++++++-- .../gms/accountsettings/ui/extensions.kt | 2 ++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/play-services-core/src/main/java/org/microg/gms/people/PeopleManager.java b/play-services-core/src/main/java/org/microg/gms/people/PeopleManager.java index e6ea7dbf80..0323c81407 100644 --- a/play-services-core/src/main/java/org/microg/gms/people/PeopleManager.java +++ b/play-services-core/src/main/java/org/microg/gms/people/PeopleManager.java @@ -20,6 +20,7 @@ import android.content.ContentValues; import android.content.Context; import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.util.Log; @@ -114,6 +115,17 @@ public static Bitmap getOwnerAvatarBitmap(Context context, String accountName, b return BitmapFactory.decodeFile(avaterFile.getPath()); } + public static void updateOwnerAvatar(Context context, String accountName, String newAvatar) { + try (DatabaseHelper databaseHelper = new DatabaseHelper(context); SQLiteDatabase db = databaseHelper.getWritableDatabase()) { + ContentValues contentValues = new ContentValues(); + contentValues.put("avatar", newAvatar); + int rowsAffected = db.update(DatabaseHelper.OWNERS_TABLE, contentValues, "account_name = ?", new String[]{accountName}); + Log.d(TAG, "updateOwnerAvatar affected: " + rowsAffected); + } catch (Exception e) { + Log.e(TAG, "Error updating avatar: " + e.getMessage()); + } + } + public static String loadUserInfo(Context context, Account account) { try { URLConnection conn = new URL(USERINFO_URL).openConnection(); diff --git a/play-services-core/src/main/kotlin/org/microg/gms/accountsettings/ui/MainActivity.kt b/play-services-core/src/main/kotlin/org/microg/gms/accountsettings/ui/MainActivity.kt index 0393c06fb5..e4e70545f7 100644 --- a/play-services-core/src/main/kotlin/org/microg/gms/accountsettings/ui/MainActivity.kt +++ b/play-services-core/src/main/kotlin/org/microg/gms/accountsettings/ui/MainActivity.kt @@ -9,9 +9,9 @@ import android.accounts.Account import android.accounts.AccountManager import android.content.Intent import android.os.Bundle +import android.text.TextUtils import android.util.Log import android.view.View -import android.view.inputmethod.InputMethodManager import android.webkit.JavascriptInterface import android.webkit.WebView import android.widget.ProgressBar @@ -19,10 +19,12 @@ import android.widget.RelativeLayout import android.widget.RelativeLayout.LayoutParams.MATCH_PARENT import android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT import androidx.appcompat.app.AppCompatActivity +import androidx.lifecycle.lifecycleScope import org.json.JSONException import org.json.JSONObject import org.microg.gms.auth.AuthConstants import org.microg.gms.common.Constants +import org.microg.gms.people.PeopleManager private const val TAG = "AccountSettings" @@ -131,6 +133,7 @@ private val ACTION_TO_SCREEN_ID = hashMapOf( class MainActivity : AppCompatActivity() { private lateinit var webView: WebView private var accountName: String? = null + private var resultBundle: Bundle? = null private fun getSelectedAccountName(): String? = null @@ -205,8 +208,16 @@ class MainActivity : AppCompatActivity() { } } + private fun updateLocalAccountAvatar(newAvatarUrl: String?) { + if (TextUtils.isEmpty(newAvatarUrl) || accountName == null) { + return + } + lifecycleScope.launchWhenCreated { + PeopleManager.updateOwnerAvatar(this@MainActivity, accountName, newAvatarUrl) + } + } + private inner class UiBridge { - private var resultBundle: Bundle? = null @JavascriptInterface fun close() { @@ -287,6 +298,9 @@ class MainActivity : AppCompatActivity() { fun setResult(resultJsonStr: String?) { Log.d(TAG, "setResult: resultJsonStr -> $resultJsonStr") val map = jsonToMap(resultJsonStr) ?: return + if (map.containsKey(KEY_UPDATED_PHOTO_URL)) { + updateLocalAccountAvatar(map[KEY_UPDATED_PHOTO_URL]) + } resultBundle = Bundle().apply { for ((key, value) in map) { putString("result.$key", value) diff --git a/play-services-core/src/main/kotlin/org/microg/gms/accountsettings/ui/extensions.kt b/play-services-core/src/main/kotlin/org/microg/gms/accountsettings/ui/extensions.kt index 30a3b01be9..d1850ea56e 100644 --- a/play-services-core/src/main/kotlin/org/microg/gms/accountsettings/ui/extensions.kt +++ b/play-services-core/src/main/kotlin/org/microg/gms/accountsettings/ui/extensions.kt @@ -22,4 +22,6 @@ const val EXTRA_THEME_CHOICE = "extra.themeChoice" const val EXTRA_SCREEN_MY_ACTIVITY_PRODUCT = "extra.screen.myactivityProduct" const val EXTRA_SCREEN_KID_ONBOARDING_PARAMS = "extra.screen.kidOnboardingParams" +const val KEY_UPDATED_PHOTO_URL = "updatedPhotoUrl" + const val OPTION_SCREEN_FLAVOR = "screenFlavor" \ No newline at end of file