Skip to content

Commit

Permalink
fix: crash when database locked
Browse files Browse the repository at this point in the history
  • Loading branch information
Sevtinge committed Dec 18, 2024
1 parent 9b90fac commit 8a41fec
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 144 deletions.
135 changes: 79 additions & 56 deletions app/src/main/java/com/sevtinge/hyperceiler/prefs/PreferenceHeader.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,18 @@
import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.os.UserHandle;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabaseLockedException;
import android.database.sqlite.SQLiteException;
import android.util.AttributeSet;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.sevtinge.hyperceiler.R;
import com.sevtinge.hyperceiler.utils.DatabaseHelper;
import com.sevtinge.hyperceiler.utils.PackagesUtils;
import com.sevtinge.hyperceiler.utils.log.XposedLogUtils;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -103,66 +106,86 @@ private boolean isHidden(Context context) {
@SuppressLint("Range")
private void getScope() {
int userId = getCurrentUserId();

DatabaseHelper dbHelper = null;
SQLiteDatabase db = null;
Cursor cursor = null;

try {
rootExecCmd("mkdir -p /data/local/tmp/HyperCeiler/cache/ && cp -r /data/adb/lspd/config /data/local/tmp/HyperCeiler/cache/ && chmod -R 777 /data/local/tmp/HyperCeiler/cache/config");
dbHelper = new DatabaseHelper(this.getContext(), "/data/local/tmp/HyperCeiler/cache/config/modules_config.db");
} catch (Exception ignore) {
isScopeGetFailed = true;
return;
}

String tableName = "modules";
String[] columns = {"mid"};
String selection = "module_pkg_name = ?";
String[] selectionArgs = {APPLICATION_ID};

Cursor cursor = dbHelper.customQuery(tableName, columns, selection, selectionArgs, null);

if (cursor != null && cursor.moveToFirst()) {
do {
List<String> scopeMid = new ArrayList<String>();
List<String> scopeUid = new ArrayList<String>();
String mid = cursor.getString(cursor.getColumnIndex("mid"));

tableName = "scope";
columns = new String[]{"app_pkg_name"};
selection = "mid = ?";
selectionArgs = new String[]{mid};

cursor = dbHelper.customQuery(tableName, columns, selection, selectionArgs, null);

if (cursor != null && cursor.moveToFirst()) {
do {
String getScope = cursor.getString(cursor.getColumnIndex("app_pkg_name"));
if (getScope.equals("system")) getScope = "android";
scopeMid.add(getScope);
} while (cursor.moveToNext());
}

tableName = "scope";
columns = new String[]{"app_pkg_name"};
selection = "user_id = ?";
selectionArgs = new String[]{String.valueOf(userId)};

cursor = dbHelper.customQuery(tableName, columns, selection, selectionArgs, null);

if (cursor != null && cursor.moveToFirst()) {
do {
String getScope = cursor.getString(cursor.getColumnIndex("app_pkg_name"));
if (getScope.equals("system")) getScope = "android";
scopeUid.add(getScope);
} while (cursor.moveToNext());
}

scope = scopeMid;
scope.retainAll(scopeUid);
} while (cursor.moveToNext());
String dbPath = "/data/local/tmp/HyperCeiler/cache/config/modules_config.db";
db = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE);

if (isDatabaseLocked(db)) {
XposedLogUtils.logW("Database locked, skip get scope.");
isScopeGetFailed = true;
return;
}

String tableName = "modules";
String[] columns = {"mid"};
String selection = "module_pkg_name = ?";
String[] selectionArgs = {APPLICATION_ID};

cursor = db.query(tableName, columns, selection, selectionArgs, null, null, null);

if (cursor != null && cursor.moveToFirst()) {
List<String> scopeMid = new ArrayList<>();
List<String> scopeUid = new ArrayList<>();

do {
String mid = cursor.getString(cursor.getColumnIndex("mid"));

String scopeQuery = "SELECT app_pkg_name FROM scope WHERE mid = ?";
Cursor scopeCursor = db.rawQuery(scopeQuery, new String[]{mid});
if (scopeCursor != null && scopeCursor.moveToFirst()) {
do {
String getScope = scopeCursor.getString(scopeCursor.getColumnIndex("app_pkg_name"));
if ("system".equals(getScope)) getScope = "android";
scopeMid.add(getScope);
} while (scopeCursor.moveToNext());
scopeCursor.close();
}

String userScopeQuery = "SELECT app_pkg_name FROM scope WHERE user_id = ?";
Cursor userScopeCursor = db.rawQuery(userScopeQuery, new String[]{String.valueOf(userId)});
if (userScopeCursor != null && userScopeCursor.moveToFirst()) {
do {
String getScope = userScopeCursor.getString(userScopeCursor.getColumnIndex("app_pkg_name"));
if ("system".equals(getScope)) getScope = "android";
scopeUid.add(getScope);
} while (userScopeCursor.moveToNext());
userScopeCursor.close();
}

scope = new ArrayList<>(scopeMid);
scope.retainAll(scopeUid);

} while (cursor.moveToNext());
}

} catch (Exception e) {
isScopeGetFailed = true;
XposedLogUtils.logE("Database error: ", e);
} finally {
if (cursor != null) cursor.close();
if (db != null) db.close();
}

cursor.close();
isScopeGet = true;
}

private boolean isDatabaseLocked(SQLiteDatabase db) {
try {
Cursor cursor = db.rawQuery("SELECT 1", null);
if (cursor != null) {
cursor.close();
}
return false;
} catch (SQLiteDatabaseLockedException e) {
return true;
} catch (SQLiteException e) {
return false;
}
}

}

This file was deleted.

0 comments on commit 8a41fec

Please sign in to comment.