FreeRDP
Loading...
Searching...
No Matches
HistoryDatabase.java
1/*
2 Room database for connection history storage
3
4 Copyright 2026 Ibrahim Sevinc <ibrahim.sevinc.mail@gmail.com>
5
6 This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
7 If a copy of the MPL was not distributed with this file, You can obtain one at
8 http://mozilla.org/MPL/2.0/.
9*/
10
11package com.freerdp.freerdpcore.data;
12
13import android.content.Context;
14
15import androidx.annotation.NonNull;
16import androidx.room.Database;
17import androidx.room.Room;
18import androidx.room.RoomDatabase;
19import androidx.room.migration.Migration;
20import androidx.sqlite.db.SupportSQLiteDatabase;
21
22@Database(entities = { HistoryEntity.class }, version = 3, exportSchema = false)
23public abstract class HistoryDatabase extends RoomDatabase
24{
25 private static final String DB_NAME = "history.db";
26
27 private static volatile HistoryDatabase instance;
28
29 public abstract HistoryDao historyDao();
30
31 public static HistoryDatabase getInstance(Context context)
32 {
33 if (instance == null)
34 {
35 synchronized (HistoryDatabase.class)
36 {
37 if (instance == null)
38 {
39 instance = Room.databaseBuilder(context.getApplicationContext(),
40 HistoryDatabase.class, DB_NAME)
41 .addMigrations(MIGRATION_1_2, MIGRATION_2_3)
42 .fallbackToDestructiveMigration()
43 .build();
44 }
45 }
46 }
47 return instance;
48 }
49
50 // v3: Add DEFAULT '' to item column to match Room schema
51 private static final Migration MIGRATION_2_3 = new Migration(2, 3) {
52 @Override public void migrate(@NonNull SupportSQLiteDatabase db)
53 {
54 db.execSQL("CREATE TABLE IF NOT EXISTS `quick_connect_history_new` ("
55 + "`item` TEXT NOT NULL DEFAULT '', "
56 + "`timestamp` INTEGER NOT NULL DEFAULT 0, "
57 + "PRIMARY KEY(`item`))");
58 db.execSQL("INSERT INTO `quick_connect_history_new` (item, timestamp) "
59 + "SELECT item, timestamp FROM `quick_connect_history`");
60 db.execSQL("DROP TABLE `quick_connect_history`");
61 db.execSQL("ALTER TABLE `quick_connect_history_new` RENAME TO `quick_connect_history`");
62 }
63 };
64
65 // v1: item TEXT PRIMARY KEY, timestamp INTEGER (both nullable — old SQLiteOpenHelper schema)
66 // v2: item TEXT NOT NULL PRIMARY KEY, timestamp INTEGER NOT NULL (Room schema)
67 private static final Migration MIGRATION_1_2 = new Migration(1, 2) {
68 @Override public void migrate(@NonNull SupportSQLiteDatabase db)
69 {
70 db.execSQL("CREATE TABLE IF NOT EXISTS `quick_connect_history_new` ("
71 + "`item` TEXT NOT NULL, "
72 + "`timestamp` INTEGER NOT NULL DEFAULT 0, "
73 + "PRIMARY KEY(`item`))");
74 db.execSQL("INSERT INTO `quick_connect_history_new` (item, timestamp) "
75 + "SELECT item, IFNULL(timestamp, 0) FROM `quick_connect_history`");
76 db.execSQL("DROP TABLE `quick_connect_history`");
77 db.execSQL("ALTER TABLE `quick_connect_history_new` RENAME TO `quick_connect_history`");
78 }
79 };
80}