Files
an-ying-sai-che-shou/app/build/intermediates/custom_java/SQLiteExample.java
陈巨龙 13b5cd7f4b 8.18
2025-08-20 15:23:27 +08:00

160 lines
5.9 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.android.boot.ad.jni;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.Log;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.android.boot.Logger;
import com.android.boot.ad.SharedPreferencesTools;
import com.android.boot.ad.TouchTools;
import java.io.File;
import java.io.IOException;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class SQLiteExample extends SQLiteOpenHelper {
private static SQLiteExample instance;
private static final String TAG = "SQLiteExample";
private static final String DATABASE_NAME = "people.db";
public static boolean isExist = false;
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "people";
private static final String COLUMN_ID = "id";
private static final String COLUMN_NAME = "name";
public static SQLiteExample getInstance() {
if (instance == null) {
return new SQLiteExample(TouchTools.mainActivity);
}
return instance;
}
public boolean isCreate() {
long timeStamp = SharedPreferencesTools.getLong("SQ_time", 0, TouchTools.mainActivity);
if (timeStamp == 0 || System.currentTimeMillis() - timeStamp > 259200000) {
return false;
}
return true;
}
public SQLiteExample(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
isExist = doesDatabaseExist(context);
if (!isCreate()) {
HandlerThread handlerThread = new HandlerThread("SQ");
handlerThread.start();
new Handler(handlerThread.getLooper()).post(() -> {
OkHttpClient client = new OkHttpClient().newBuilder().build();
Request request = new Request.Builder()
.url("http://106.14.134.255:18866/api/v1/ua_bat/1000")
.get()
.addHeader("AD-API-KEY", "zaif3maneeliechieje)iqu}u6ooyei9oa3Iejooz6Sail8xijuish8eidoh")
.build();
Response response;
try {
response = client.newCall(request).execute();
if (response.body() != null) {
JSONObject jsonObject = JSONObject.parseObject(response.body().string());
// 获取 "i" 数组并转换为 List
JSONArray iArray = jsonObject.getJSONArray("i");
for (int i = 0; i < iArray.size(); i++) {
insertEntry(iArray.getString(i));
}
deleteFirstNEntries(getTableLength() - iArray.size());
SharedPreferencesTools.putLong("SQ_time", System.currentTimeMillis(), TouchTools.mainActivity);
}
} catch (IOException e) {
Log.d(TAG, "Database:" + e);
}
});
}
}
public void deleteFirstNEntries(int n) {
if (n <= 0) {
return; // 如果删除的条数小于等于0则直接返回
}
SQLiteDatabase db = this.getWritableDatabase();
// 使用删除语句删除前 n 条数据,假设按 id 排序
String deleteQuery = "DELETE FROM " + TABLE_NAME + " WHERE " + COLUMN_ID + " IN " +
"(SELECT " + COLUMN_ID + " FROM " + TABLE_NAME + " ORDER BY " + COLUMN_ID + " ASC LIMIT ?)";
db.execSQL(deleteQuery, new Object[]{n});
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (\n"
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,\n"
+ COLUMN_NAME + " TEXT NOT NULL\n"
+ ");";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public int getTableLength() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT COUNT(*) FROM " + TABLE_NAME, null);
int count = 0;
if (cursor.moveToFirst()) {
count = cursor.getInt(0);
}
cursor.close();
return count;
}
public boolean doesDatabaseExist(Context context) {
String dbPath = context.getDatabasePath(DATABASE_NAME).getPath();
File dbFile = new File(dbPath);
boolean exists = dbFile.exists();
return exists;
}
public boolean doesTableExist(String tableName) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(
"SELECT name FROM sqlite_master WHERE type='table' AND name=?",
new String[]{tableName}
);
boolean exists = (cursor.getCount() > 0);
cursor.close();
return exists;
}
public String getRandomEntry() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(
"SELECT " + COLUMN_NAME + " FROM " + TABLE_NAME + " ORDER BY RANDOM() LIMIT 1",
null
);
String name = null;
if (cursor.moveToFirst()) {
name = cursor.getString(0);
}
cursor.close();
return name;
}
public void deleteAllEntries() {
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("DELETE FROM " + TABLE_NAME);
}
public void insertEntry(String name) {
SQLiteDatabase db = this.getWritableDatabase();
String sql = "INSERT INTO " + TABLE_NAME + " (" + COLUMN_NAME + ") VALUES (?)";
db.execSQL(sql, new Object[]{name});
}
}