-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathGlobalApp.kt
More file actions
426 lines (380 loc) · 17.3 KB
/
GlobalApp.kt
File metadata and controls
426 lines (380 loc) · 17.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
package com.greybox.projectmesh
import android.annotation.SuppressLint
import android.app.Application
import android.content.Context
import android.content.SharedPreferences
import android.os.Build
import android.os.Environment
import android.util.Log
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.intPreferencesKey
import androidx.room.Room
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import com.greybox.projectmesh.db.MeshDatabase
import com.greybox.projectmesh.extension.deviceInfo
import com.greybox.projectmesh.messaging.repository.ConversationRepository
import com.greybox.projectmesh.extension.networkDataStore
import com.greybox.projectmesh.server.AppServer
import com.ustadmobile.meshrabiya.ext.addressToDotNotation
import com.ustadmobile.meshrabiya.ext.asInetAddress
import com.ustadmobile.meshrabiya.ext.requireAddressAsInt
import com.ustadmobile.meshrabiya.vnet.AndroidVirtualNode
import com.ustadmobile.meshrabiya.vnet.randomApipaAddr
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.serialization.json.Json
import okhttp3.OkHttpClient
import org.kodein.di.DI
import org.kodein.di.DIAware
import org.kodein.di.bind
import org.kodein.di.instance
import org.kodein.di.singleton
import java.io.File
import java.net.InetAddress
import java.time.Duration
import java.util.UUID
import java.util.concurrent.ConcurrentHashMap
import com.greybox.projectmesh.user.UserRepository
import com.greybox.projectmesh.messaging.data.entities.Message
import com.greybox.projectmesh.messaging.utils.MessageMigrationUtils
import com.greybox.projectmesh.testing.TestDeviceService
import com.greybox.projectmesh.user.UserEntity
import com.ustadmobile.meshrabiya.log.MNetLogger
import java.text.SimpleDateFormat
import java.util.Date
/*
initialize global variables and DI(dependency injection) container
why use DI?
All dependencies are defined in one place, which makes it easier to manage and test.
*/
class GlobalApp : Application(), DIAware {
// it is an instance of Preferences.key<Int>, used to interact with "DataStore"
private val addressKey = intPreferencesKey("virtual_node_address")
/*data object DeviceInfoManager {
// Global HashMap to store IP-DeviceName mapping
private val deviceNameMap = ConcurrentHashMap<String, String?>()
// Helper method to add/update a device name
fun addDevice(ipAddress: String, name: String?) {
deviceNameMap[ipAddress] = name
}
fun removeDevice(ipAddress: String) {
deviceNameMap.remove(ipAddress)
}
fun getDeviceName(inetAddress: String): String? {
return deviceNameMap[inetAddress]
}
fun getDeviceName(inetAddress: InetAddress): String? {
return deviceNameMap[inetAddress.hostAddress]
}
fun getChatName(inetAddress: InetAddress): String {
return inetAddress.hostAddress
}
}*/
object GlobalUserRepo {
// Lateinit or lazy property
lateinit var userRepository: UserRepository
lateinit var prefs: SharedPreferences
lateinit var conversationRepository: ConversationRepository
}
override fun onCreate() {
super.onCreate()
val sharedPrefs: SharedPreferences by di.instance(tag = "settings")
val uuid = sharedPrefs.getString("UUID", null)
if (uuid == null) {
// Generate a new UUID if one doesn't exist
val newUuid = java.util.UUID.randomUUID().toString()
sharedPrefs.edit().putString("UUID", newUuid).apply()
Log.d("GlobalApp", "Generated new UUID: $newUuid")
}
//get the repositories from DI
val repo: UserRepository by di.instance()
GlobalUserRepo.userRepository = repo
GlobalUserRepo.prefs = sharedPrefs
val convRepo: ConversationRepository by di.instance()
GlobalUserRepo.conversationRepository = convRepo
//initialize deviceStatus Manager:
val appServer: AppServer by di.instance()
DeviceStatusManager.initialize(appServer)
//version checking migrating messages
val hasMigratedMessages = sharedPrefs.getBoolean("has_migrated_messages", false)
if (!hasMigratedMessages) {
GlobalScope.launch {
try {
Log.d("GlobalApp", "Starting message migration...")
val migrationUtils = MessageMigrationUtils(di)
migrationUtils.migrateMessagesToChatIds()
// Mark migration as complete
sharedPrefs.edit().putBoolean("has_migrated_messages", true).apply()
Log.d("GlobalApp", "Message migration completed and marked as done")
} catch (e: Exception) {
Log.e("GlobalApp", "Error during message migration", e)
// Don't mark as complete if there was an error
}
}
} else {
Log.d("GlobalApp", "Message migration already performed, skipping")
}
//add test convos
insertTestConversations()
}
fun insertTestConversations() {
GlobalScope.launch {
try {
//get database instance
val db: MeshDatabase by di.instance()
// Check if any messages exist first
val existingMessages = db.messageDao().getAll()
Log.d("GlobalApp", "Found ${existingMessages.size} existing messages")
if (existingMessages.isEmpty()) {
Log.d("GlobalApp", "No messages found, creating test messages...")
val localUuid = GlobalUserRepo.prefs.getString("UUID", null) ?: "local-user"
//insert test convo with online test device
val testDevice = TestDeviceService.getTestDeviceAddress()
val testUser = UserEntity(
uuid = "test-device-uuid",
name = TestDeviceService.TEST_DEVICE_NAME,
address = testDevice.hostAddress
)
//make sure the test user exists in the database
GlobalUserRepo.userRepository.insertOrUpdateUser(
testUser.uuid,
testUser.name,
testUser.address
)
//create convo with the test device
val onlineConversation =
GlobalUserRepo.conversationRepository.getOrCreateConversation(
localUuid = localUuid,
remoteUser = testUser
)
//create online test message
val onlineTestMessage = Message(
id = 0,
dateReceived = System.currentTimeMillis() - 3600000, // 1 hour ago
content = "Hello world! This is a test message.",
sender = TestDeviceService.TEST_DEVICE_NAME,
chat = "local-user-test-device-uuid" // Use ONLY conversation ID format
)
//insert the message
db.messageDao().addMessage(onlineTestMessage)
Log.d(
"GlobalApp",
"Inserted online test message with chat name: local-user-test-device-uuid"
)
//update convo with a test message
GlobalUserRepo.conversationRepository.updateWithMessage(
conversationId = onlineConversation.id,
message = onlineTestMessage
)
//create offline test user and conversation
val offlineUser = UserEntity(
uuid = "offline-test-device-uuid",
name = TestDeviceService.TEST_DEVICE_NAME_OFFLINE,
address = null // null address means offline
)
//make sure the offline test user exists in the database
GlobalUserRepo.userRepository.insertOrUpdateUser(
offlineUser.uuid,
offlineUser.name,
offlineUser.address
)
//create convo with the offline test device
val offlineConversation =
GlobalUserRepo.conversationRepository.getOrCreateConversation(
localUuid = localUuid,
remoteUser = offlineUser
)
//create offline test message - use ONLY the conversation ID format
val offlineTestMessage = Message(
id = 0,
dateReceived = System.currentTimeMillis() - 3600000, // 1 hour ago
content = "I'm currently offline. Messages won't be delivered.",
sender = TestDeviceService.TEST_DEVICE_NAME_OFFLINE,
chat = "local-user-offline-test-device-uuid" // Use ONLY conversation ID format
)
//update test device statuses
DeviceStatusManager.updateDeviceStatus(TestDeviceService.TEST_DEVICE_IP, true)
DeviceStatusManager.updateDeviceStatus(TestDeviceService.TEST_DEVICE_IP_OFFLINE, false)
// Insert the message
db.messageDao().addMessage(offlineTestMessage)
Log.d(
"GlobalApp",
"Inserted offline test message with chat name: local-user-offline-test-device-uuid"
)
//update convo with the message
GlobalUserRepo.conversationRepository.updateWithMessage(
conversationId = offlineConversation.id,
message = offlineTestMessage
)
Log.d("GlobalApp", "Test messages inserted successfully")
}else {
Log.d("GlobalApp", "Messages already exist, skipping insertion")
}
}catch (e: Exception ) {
Log.e("GlobalApp", "Error inserting test conversation", e)
}
}
}
@SuppressLint("SimpleDateFormat")
private val diModule = DI.Module("project_mesh") {
// create a single instance of "InetAddress" for the entire lifetime of the application
bind<InetAddress>(tag=TAG_VIRTUAL_ADDRESS) with singleton {
// fetch an IP address from the data store or generate a random one
// Run a coroutine in a blocking way, it will block the main thread
runBlocking {
// fetch the address from the data store
val address = applicationContext.networkDataStore.data.map { preference ->
preference[addressKey] ?: 0
}.first()
// if the address is not 0, converted to an IP address
if(address != 0) {
address.asInetAddress()
}
else{
// if not, generate a random one,
// store it in the data store and converted to IP address
randomApipaAddr().also {
randomAddress -> applicationContext.networkDataStore.edit {
// "it" used to access the 'Preferences' object
it[addressKey] = randomAddress
}
}.asInetAddress()
}
}
}
bind<MNetLogger>() with singleton {
val logFileNameDateComp = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
val logDir: File = instance(tag = TAG_LOG_DIR)
MNetLoggerAndroid(
deviceInfo = deviceInfo(),
minLogLevel = Log.DEBUG,
logFile = File(logDir, "${logFileNameDateComp}_${Build.MANUFACTURER}_${Build.MODEL}.log")
)
}
bind <Json>() with singleton {
Json {
encodeDefaults = true
}
}
bind<File>(tag = TAG_LOG_DIR) with singleton {
File(filesDir, "log")
}
/*
Ensuring a directory named "www" was created
*/
bind<File>(tag = TAG_WWW_DIR) with singleton {
File(filesDir, "www").also{
if(!it.exists()) {
it.mkdirs()
}
}
}
bind<File>(tag = TAG_RECEIVE_DIR) with singleton {
File(filesDir, "receive")
}
bind<AndroidVirtualNode>() with singleton {
// initialize the AndroidVirtualNode Constructor
AndroidVirtualNode(
appContext = applicationContext,
logger = instance(),
json = instance(),
// inject the "InetAddress" instance
address = instance(tag = TAG_VIRTUAL_ADDRESS),
dataStore = applicationContext.networkDataStore
)
}
// The OkHttpClient will be created only once and shared across the app when needed
bind<OkHttpClient>() with singleton {
val node: AndroidVirtualNode = instance()
OkHttpClient.Builder()
.socketFactory(node.socketFactory)
// The maximum time to wait for a connection to be established
.connectTimeout(Duration.ofSeconds(30))
// The maximum time to wait for data to be read from the server
.readTimeout(Duration.ofSeconds(30))
// The maximum time to wait for data to be written to the server
.writeTimeout(Duration.ofSeconds(30))
.build()
}
bind<MeshDatabase>() with singleton {
Room.databaseBuilder(applicationContext,
MeshDatabase::class.java,
"mesh-database"
)
.addMigrations(object : Migration(3,4){
override fun migrate(database: SupportSQLiteDatabase){
//create convo table
database.execSQL(
"""
CREATE TABLE IF NOT EXISTS conversations (
id TEXT PRIMARY KEY NOT NULL,
user_uuid TEXT NOT NULL,
user_name TEXT NOT NULL,
user_address TEXT,
last_message TEXT,
last_message_time INTEGER NOT NULL,
unread_count INTEGER NOT NULL DEFAULT 0,
is_online INTEGER NOT NULL DEFAULT 0
)
"""
)
}
})
.fallbackToDestructiveMigration() // handle migrations destructively
// .allowMainThreadQueries() // this should generally be avoided for production apps
.build()
}
bind<SharedPreferences>(tag = "settings") with singleton {
applicationContext.getSharedPreferences("settings", Context.MODE_PRIVATE)
}
bind<SharedPreferences>(tag = "mesh") with singleton {
applicationContext.getSharedPreferences("project_mesh_prefs", Context.MODE_PRIVATE)
}
bind<UserRepository>() with singleton {
UserRepository(instance<MeshDatabase>().userDao())
}
bind<ConversationRepository>() with singleton {
ConversationRepository(instance<MeshDatabase>().conversationDao(), di)
}
bind<AppServer>() with singleton {
val node: AndroidVirtualNode = instance()
AppServer(
appContext = applicationContext,
httpClient = instance(),
mLogger = instance(),
port = AppServer.DEFAULT_PORT,
name = node.addressAsInt.addressToDotNotation(),
localVirtualAddr = node.address,
receiveDir = instance(tag = TAG_RECEIVE_DIR),
json = instance(),
di = di,
db = instance(),
userRepository = instance()
)
}
onReady {
// clears all data in the existing tables
//GlobalScope.launch {
// instance<MeshDatabase>().messageDao().clearTable()
//}
val logger: MNetLogger = instance()
instance<AppServer>().start()
logger(Log.DEBUG,"AppServer started successfully on Port: ${AppServer.DEFAULT_PORT}")
}
}
// DI container and its bindings are only set up when they are first needed
override val di: DI by DI.lazy {
import(diModule)
}
companion object {
const val TAG_VIRTUAL_ADDRESS = "virtual_address"
const val TAG_RECEIVE_DIR = "receive_dir"
const val TAG_WWW_DIR = "www_dir"
const val TAG_LOG_DIR = "log_dir"
}
}