Skip to content

Commit 35f9315

Browse files
author
AppDevForAll
committed
ADFA-4593: refresh cached help.db so schema changes take effect
The app copied assets/help.db to filesDir only when the app versionCode changed, so reinstalling the same version kept a stale DB (missing the new 'lang' column -> 'no such column: T.lang'). Now: debug builds always re-copy on launch; release builds re-copy when a bundled DB version constant (DB_ASSET_VERSION) changes. Copy happens at most once per process.
1 parent 4380ae5 commit 35f9315

1 file changed

Lines changed: 15 additions & 11 deletions

File tree

‎controller/app/src/main/java/org/iiab/controller/help/TooltipManager.java‎

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
import android.content.Context;
1717
import android.content.SharedPreferences;
18-
import android.content.pm.PackageInfo;
18+
import android.content.pm.ApplicationInfo;
1919
import android.content.res.Configuration;
2020
import android.database.Cursor;
2121
import android.database.sqlite.SQLiteDatabase;
@@ -37,7 +37,6 @@
3737
import android.widget.TextView;
3838

3939
import androidx.appcompat.app.AlertDialog;
40-
import androidx.core.content.pm.PackageInfoCompat;
4140

4241
import org.iiab.controller.R;
4342

@@ -64,6 +63,8 @@ public final class TooltipManager {
6463
private static final ExecutorService IO = Executors.newSingleThreadExecutor();
6564
private static final Handler MAIN = new Handler(Looper.getMainLooper());
6665
private static PopupWindow activePopup;
66+
private static final int DB_ASSET_VERSION = 2;
67+
private static volatile boolean copiedThisProcess = false;
6768

6869
private TooltipManager() {}
6970

@@ -174,16 +175,18 @@ private static SQLiteDatabase openDatabase(Context context) {
174175
private static synchronized File ensureDatabase(Context context) {
175176
File out = new File(context.getFilesDir(), LOCAL_DB);
176177
try {
177-
long appVersion = -1;
178-
try {
179-
PackageInfo pi = context.getPackageManager()
180-
.getPackageInfo(context.getPackageName(), 0);
181-
appVersion = PackageInfoCompat.getLongVersionCode(pi);
182-
} catch (Exception ignored) {}
178+
if (copiedThisProcess && out.exists()) return out;
183179

180+
boolean debuggable = (context.getApplicationInfo().flags
181+
& ApplicationInfo.FLAG_DEBUGGABLE) != 0;
184182
SharedPreferences sp = context.getSharedPreferences("help_db", Context.MODE_PRIVATE);
185-
long copied = sp.getLong("version", -1);
186-
if (out.exists() && copied == appVersion) return out;
183+
int stored = sp.getInt("db_asset_version", -1);
184+
// Debug builds always refresh (content changes without a version bump);
185+
// release builds refresh only when the bundled DB version changes.
186+
if (out.exists() && !debuggable && stored == DB_ASSET_VERSION) {
187+
copiedThisProcess = true;
188+
return out;
189+
}
187190

188191
try (InputStream in = context.getAssets().open(ASSET_DB);
189192
OutputStream os = new FileOutputStream(out)) {
@@ -193,7 +196,8 @@ private static synchronized File ensureDatabase(Context context) {
193196
os.write(buf, 0, n);
194197
}
195198
}
196-
sp.edit().putLong("version", appVersion).apply();
199+
sp.edit().putInt("db_asset_version", DB_ASSET_VERSION).apply();
200+
copiedThisProcess = true;
197201
return out;
198202
} catch (Exception e) {
199203
Log.e(TAG, "ensureDatabase failed: " + e.getMessage());

0 commit comments

Comments
 (0)