diff --git a/app/src/main/java/com/gh/common/util/DataUtils.java b/app/src/main/java/com/gh/common/util/DataUtils.java index 4cf03cda43..550cc4e8b1 100644 --- a/app/src/main/java/com/gh/common/util/DataUtils.java +++ b/app/src/main/java/com/gh/common/util/DataUtils.java @@ -2,17 +2,14 @@ package com.gh.common.util; import android.annotation.SuppressLint; import android.app.Application; -import android.content.ContentResolver; import android.content.ContentValues; import android.content.Context; import android.net.Uri; -import android.os.Build; import android.preference.PreferenceManager; import android.text.TextUtils; import android.util.Log; import com.gh.base.GlobalActivityManager; - import com.gh.gamecenter.BuildConfig; import com.gh.gamecenter.common.base.activity.BaseActivity; import com.gh.gamecenter.common.constant.Constants; @@ -126,6 +123,16 @@ public class DataUtils { // 避免初始化顺序问题导致 MetaUtil 一直持有空的 gid MetaUtil.INSTANCE.refreshMeta(); + + ContentValues values = new ContentValues(); + values.put(GhContentProvider.KEY_GID, gid); + values.put(GhContentProvider.KEY_ANDROID_ID, MetaUtil.getBase64EncodedAndroidId()); + try { + HaloApp.getInstance().getContentResolver().insert(Uri.parse("content://com.gh.gamecenter.provider/device"), values); + } catch (Exception exception) { + SentryHelper.INSTANCE.onEvent("DEVICE_INSERT_ERROR", "exception_digest", exception.getLocalizedMessage()); + exception.printStackTrace(); + } } @Override diff --git a/app/src/main/java/com/gh/gamecenter/provider/GhContentProvider.kt b/app/src/main/java/com/gh/gamecenter/provider/GhContentProvider.kt index 95845fabb6..14f9a61f4e 100644 --- a/app/src/main/java/com/gh/gamecenter/provider/GhContentProvider.kt +++ b/app/src/main/java/com/gh/gamecenter/provider/GhContentProvider.kt @@ -8,13 +8,13 @@ import android.database.sqlite.SQLiteOpenHelper import android.net.Uri import android.text.TextUtils import android.util.Base64 -import com.halo.assistant.HaloApp import com.gh.gamecenter.BuildConfig import com.gh.gamecenter.common.constant.Constants import com.gh.gamecenter.core.utils.GsonUtils import com.gh.gamecenter.core.utils.SPUtils import com.gh.gamecenter.login.entity.UserInfoEntity import com.gh.gamecenter.login.user.UserRepository +import com.halo.assistant.HaloApp import com.lightgame.utils.Utils class GhContentProvider : ContentProvider() { @@ -25,12 +25,13 @@ class GhContentProvider : ContentProvider() { init { mUriMatcher.addURI(AUTHORITY, CERTIFICATION_TABLE_NAME, 1) mUriMatcher.addURI(AUTHORITY, SYNC_CERTIFICATION_TABLE_NAME, 2) + mUriMatcher.addURI(AUTHORITY, DEVICE_TABLE_NAME, 3) } private fun initProviderSqliteHelper(context: Context?) { val helper: SQLiteOpenHelper = - object : SQLiteOpenHelper(context, CERTIFICATION_DATABASE_NAME, null, 1) { + object : SQLiteOpenHelper(context, CERTIFICATION_DATABASE_NAME, null, 2) { override fun onCreate(db: SQLiteDatabase) { // 创建表格 val sql = "CREATE TABLE $CERTIFICATION_TABLE_NAME(" + @@ -46,10 +47,24 @@ class GhContentProvider : ContentProvider() { "$KEY_ID_CARD TEXT" + ")" db.execSQL(syncSql) + + val deviceSql = "CREATE TABLE ${DEVICE_TABLE_NAME}(" + + "$KEY_PRIMARY_KEY INTEGER PRIMARY KEY AUTOINCREMENT," + + "$KEY_GID TEXT," + + "$KEY_ANDROID_ID TEXT" + + ")" + db.execSQL(deviceSql) } override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) { - // do nothing + if (oldVersion == 1 && newVersion == 2) { + val deviceSql = "CREATE TABLE ${DEVICE_TABLE_NAME}(" + + "$KEY_PRIMARY_KEY INTEGER PRIMARY KEY AUTOINCREMENT," + + "$KEY_GID TEXT," + + "$KEY_ANDROID_ID TEXT" + + ")" + db.execSQL(deviceSql) + } } } mSqLiteDatabase = helper.writableDatabase @@ -87,6 +102,15 @@ class GhContentProvider : ContentProvider() { null, null ) + 3 -> return mSqLiteDatabase?.query( + DEVICE_TABLE_NAME, + null, + null, + null, + null, + null, + null + ) } return null @@ -166,7 +190,29 @@ class GhContentProvider : ContentProvider() { UserRepository.getInstance().syncCertificate(finalRealName, finalIdCard) return ContentUris.withAppendedId(uri, 1) } - + } + } else if (mUriMatcher.match(uri) == 3) { + try { + // 固定主键(只保留一条数据即可) + values?.put(KEY_PRIMARY_KEY, 1) + // 如果已存在则直接替换 + val rowId: Long? = mSqLiteDatabase?.insertWithOnConflict( + DEVICE_TABLE_NAME, + null, + values, + SQLiteDatabase.CONFLICT_REPLACE + ) + if (rowId != null && rowId > 0) { + val nameUri = ContentUris.withAppendedId(uri, rowId) + context.contentResolver.notifyChange(nameUri, null) + Utils.log( + "DeviceContentProvider", + "insert success:" + uri.authority + ", status => " + values?.toString() + ) + return nameUri + } + } catch (e: SQLiteFullException) { + Utils.toast(context, "数据库内存已满,无法同步") } } return null @@ -193,10 +239,13 @@ class GhContentProvider : ContentProvider() { private const val AUTHORITY = "${BuildConfig.APPLICATION_ID}.provider" private const val CERTIFICATION_DATABASE_NAME = "gh_certification.db" private const val CERTIFICATION_TABLE_NAME = "certification" + private const val DEVICE_TABLE_NAME = "device" const val KEY_PRIMARY_KEY = "primary_key" const val KEY_IS_CERTIFICATED = "is_certificated" const val KEY_IS_ADULT = "is_adult" + const val KEY_GID = "gid" + const val KEY_ANDROID_ID = "android_id" private const val SYNC_CERTIFICATION_TABLE_NAME = "sync_certification"