-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathTabsManager.kt
921 lines (797 loc) · 31.2 KB
/
TabsManager.kt
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
package acr.browser.lightning.browser
import acr.browser.lightning.R
import acr.browser.lightning.browser.sessions.Session
import acr.browser.lightning.extensions.snackbar
import acr.browser.lightning.search.SearchEngineProvider
import acr.browser.lightning.settings.NewTabPosition
import acr.browser.lightning.settings.preferences.UserPreferences
import acr.browser.lightning.utils.*
import acr.browser.lightning.view.*
import android.app.Activity
import android.app.Application
import android.app.SearchManager
import android.content.Intent
import android.os.Bundle
import androidx.lifecycle.LifecycleOwner
import kotlinx.coroutines.launch
import timber.log.Timber
import java.io.File
import java.util.*
import javax.inject.Inject
import javax.inject.Singleton
import kotlin.collections.ArrayList
/**
* A manager singleton that holds all the [WebPageTab] and tracks the current tab. It handles
* creation, deletion, restoration, state saving, and switching of tabs and sessions.
*/
//@HiltViewModel
@Singleton
class TabsManager @Inject constructor(
private val application: Application,
private val searchEngineProvider: SearchEngineProvider,
private val homePageInitializer: HomePageInitializer,
private val incognitoPageInitializer: IncognitoPageInitializer,
private val bookmarkPageInitializer: BookmarkPageInitializer,
private val historyPageInitializer: HistoryPageInitializer,
private val downloadPageInitializer: DownloadPageInitializer,
private val noOpPageInitializer: NoOpInitializer,
private val userPreferences: UserPreferences
): fulguris.Component() {
private val tabList = arrayListOf<WebPageTab>()
var iRecentTabs = mutableSetOf<WebPageTab>()
// This is just used when loading and saving sessions.
// TODO: Ideally it should not be a data member.
val savedRecentTabsIndices = mutableSetOf<Int>()
private var iIsIncognito = false;
// Our persisted list of sessions
// TODO: Consider using a map instead of an array
var iSessions: ArrayList<Session> = arrayListOf<Session>()
var iCurrentSessionName: String = ""
set(value) {
// Most unoptimized way to maintain our current item but that should do for now
iSessions.forEach { s -> s.isCurrent = false }
iSessions.filter { s -> s.name == value}.apply {if (isNotEmpty()) get(0).isCurrent = true }
field = value
}
/**
* Return the current [WebPageTab] or null if no current tab has been set.
*
* @return a [WebPageTab] or null if there is no current tab.
*/
var currentTab: WebPageTab? = null
private set
private var tabNumberListeners = emptySet<(Int) -> Unit>()
var isInitialized = false
private var postInitializationWorkList = mutableListOf<InitializationListener>()
init {
addTabNumberChangedListener {
// Update current session tab count
//TODO: Have a getCurrentSession function
//TODO: during shutdown initiated by session switch we get stray events here not matching the proper session since it current session name was changed
//TODO: it's no big deal and does no harm at all but still not consistent, we may want to fix it at some point
//TODO: after shutdown our tab counts are fixed by [loadSessions]
val session=iSessions.filter { s -> s.name == iCurrentSessionName }
if (session.isNotEmpty()) {
session[0].tabCount = it
}
}
}
/*
override fun onCleared() {
super.onCleared()
shutdown()
app.tabsManager = null;
}
*/
/**
* From [DefaultLifecycleObserver.onStop]
*
* This is called once our activity is not visible anymore.
* That's where we should save our data according to the docs.
* https://developer.android.com/guide/components/activities/activity-lifecycle#onstop
* Saving data can't wait for onDestroy as there is no guarantee onDestroy will ever be called.
* In fact even when user closes our Task from recent Task list our activity is just terminated without getting any notifications.
*/
override fun onStop(owner: LifecycleOwner) {
// Once we go background make sure the current tab is not new anymore
currentTab?.isNewTab = false
saveIfNeeded()
}
override fun onDestroy(owner: LifecycleOwner) {
//shutdown()
}
/**
*/
fun currentSessionIndex() : Int {
return iSessions.indexOfFirst { s -> s.name == iCurrentSessionName }
}
/**
*/
fun currentSession() : Session {
return session(iCurrentSessionName)
}
/**
* Provide the session matching the given name
* TODO: have a better implementation
*/
fun session(aName: String) : Session {
if (iSessions.isNullOrEmpty()) {
// TODO: Return session with Default name
return Session()
}
val list = iSessions.filter { s -> s.name == aName }
if (list.isNullOrEmpty()) {
// TODO: Return session with Default name
return Session()
}
// Should only be one session item in that list
return list[0]
}
/**
* Adds a listener to be notified when the number of tabs changes.
*/
fun addTabNumberChangedListener(listener: ((Int) -> Unit)) {
tabNumberListeners += listener
}
/**
* Cancels any pending work that was scheduled to run after initialization.
*/
fun cancelPendingWork() {
postInitializationWorkList.clear()
}
/**
* Executes the [runnable] once after the next time this manager has been initialized.
*/
fun doOnceAfterInitialization(runnable: () -> Unit) {
if (isInitialized) {
runnable()
} else {
postInitializationWorkList.add(object : InitializationListener {
override fun onInitializationComplete() {
runnable()
postInitializationWorkList.remove(this)
}
})
}
}
/**
* Executes the [runnable] every time after this manager has been initialized.
*/
fun doAfterInitialization(runnable: () -> Unit) {
if (isInitialized) {
runnable()
} else {
postInitializationWorkList.add(object : InitializationListener {
override fun onInitializationComplete() {
runnable()
}
})
}
}
/**
*
*/
private fun finishInitialization() {
try {
if (allTabs.size == savedRecentTabsIndices.size) { // Defensive
// Populate our recent tab list from our persisted indices
iRecentTabs.clear()
// Looks like we can somehow persist -1 as a tab index
// TODO: That should never be the case. We ought to find out what's causing this.
// See: https://console.firebase.google.com/u/0/project/fulguris-b1f69/crashlytics/app/android:net.slions.fulguris.full.playstore/issues/d70a65025a98104878bf2da4aa06287e?time=last-seven-days&sessionEventKey=650AE750014800016260FF77850BA317_1859332239793370743
savedRecentTabsIndices.forEach { iRecentTabs.add(allTabs.elementAt(it))}
} else {
// Defensive, if we have missing tabs in our recent tab list just reset it
resetRecentTabsList()
}
}
catch (ex: Exception) {
Timber.d("Failed to load recent tab list")
resetRecentTabsList()
}
isInitialized = true
// Iterate through our collection while allowing item to be removed and avoid ConcurrentModificationException
// To do that we need to make a copy of our list
val listCopy = postInitializationWorkList.toList()
for (listener in listCopy) {
listener.onInitializationComplete()
}
}
/**
*
*/
private fun resetRecentTabsList()
{
Timber.d("resetRecentTabsList")
// Reset recent tabs list to arbitrary order
iRecentTabs.clear()
iRecentTabs.addAll(allTabs)
// Put back current tab on top
currentTab?.let {
iRecentTabs.apply {
remove(it)
add(it)
}
}
}
/**
* Initialize the state of the [TabsManager] based on previous state of the browser.
*
* TODO: See how you can offload IO to a background thread
*/
fun initializeTabs(activity: Activity, incognito: Boolean) : MutableList<WebPageTab> {
Timber.d("initializeTabs")
iIsIncognito = incognito
shutdown()
val list = mutableListOf<WebPageTab>()
if (incognito) {
list.add(newTab(activity, incognitoPageInitializer, incognito, NewTabPosition.END_OF_TAB_LIST))
} else {
tryRestorePreviousTabs(activity).forEach {
try {
list.add(newTab(activity, it, incognito, NewTabPosition.END_OF_TAB_LIST))
} catch (ex: Throwable) {
// That's a corrupted session file, can happen when importing garbage.
activity.snackbar(R.string.error_session_file_corrupted)
}
}
// Make sure we have one tab
if (list.isEmpty()) {
list.add(newTab(activity, homePageInitializer, incognito, NewTabPosition.END_OF_TAB_LIST))
}
}
finishInitialization()
return list
}
/**
* Returns the URL for a search [Intent]. If the query is empty, then a null URL will be
* returned.
*/
fun extractSearchFromIntent(intent: Intent): String? {
val query = intent.getStringExtra(SearchManager.QUERY)
val searchUrl = "${searchEngineProvider.provideSearchEngine().queryUrl}$QUERY_PLACE_HOLDER"
return if (query?.isNotBlank() == true) {
smartUrlFilter(query, true, searchUrl).first
} else {
null
}
}
/**
* Load tabs from the given file
*/
private fun loadSession(aFilename: String): MutableList<TabInitializer>
{
val bundle = FileUtils.readBundleFromStorage(application, aFilename)
// Defensive. should have happened in the shutdown already
savedRecentTabsIndices.clear()
// Read saved current tab index if any
bundle?.let{
it.getIntArray(RECENT_TAB_INDICES)?.toList()?.let { it1 -> savedRecentTabsIndices.addAll(it1) }
}
val list = mutableListOf<TabInitializer>()
readSavedStateFromDisk(bundle).forEach {
list.add(if (it.url.isSpecialUrl()) {
tabInitializerForSpecialUrl(it.url)
} else {
FreezableBundleInitializer(it)
})
}
// Make sure we have at least one tab
if (list.isEmpty()) {
list.add(homePageInitializer)
}
return list
}
/**
* Create a recovery session
*/
private fun loadRecoverySession(): MutableList<TabInitializer>
{
// Defensive. should have happened in the shutdown already
savedRecentTabsIndices.clear()
val list = mutableListOf<TabInitializer>()
// Make sure we have at least one tab
if (list.isEmpty()) {
list.add(noOpPageInitializer)
}
return list
}
/**
* Rename the session [aOldName] to [aNewName].
* Takes care of checking parameters validity before proceeding.
* Changes current session name if needed.
* Rename matching session data file too.
* Commit session list changes to persistent storage.
*
* @param [aOldName] Name of the session to rename in our session list.
* @param [aNewName] New name to be assumed by specified session.
*/
fun renameSession(aOldName: String, aNewName: String) {
Timber.d("Try rename session $aOldName to $aNewName")
val index = iSessions.indexOf(session(aOldName))
Timber.d("Session index $index")
// Check if we can indeed rename that session
if (iSessions.isEmpty() // Check if we have sessions at all
or !isValidSessionName(aNewName) // Check if new session name is valid
or !(index>=0 && index<iSessions.count())) { // Check if index is in range
Timber.d("Session rename aborted")
return
}
// Proceed with rename then
val oldName = iSessions[index].name
// Change session name
iSessions[index].name = aNewName
// Renamed session is the current session
if (iCurrentSessionName == oldName) {
iCurrentSessionName = aNewName
}
Timber.d("Rename session files $oldName to $aNewName")
// Rename our session file
FileUtils.renameBundleInStorage(application, fileNameFromSessionName(oldName), fileNameFromSessionName(aNewName))
// I guess it makes sense to persist our changes
saveSessions()
}
/**
* Check if the given string is a valid session name
*/
fun isValidSessionName(aName: String): Boolean {
// Empty strings are not valid names
if (aName.isNullOrBlank()) {
return false
}
if (iSessions.isNullOrEmpty()) {
// Null or empty session list so that name is valid
return true
} else {
// That name is valid if not already in use
return iSessions.filter { s -> s.name == aName }.isNullOrEmpty()
}
}
/**
* Returns an observable that emits the [TabInitializer] for each previously opened tab as
* saved on disk. Can potentially be empty.
*/
private fun restorePreviousTabs(): MutableList<TabInitializer>
{
//throw Exception("Hi There!")
// First load our sessions
loadSessions()
// Check if we have a current session
if (iCurrentSessionName.isBlank()) {
// No current session name meaning first load with version support
// Add our default session
iCurrentSessionName = application.getString(R.string.session_default)
// At this stage we must have at least an empty list
iSessions.add(Session(iCurrentSessionName))
// Than load legacy session file to make sure tabs from earlier version are preserved
return loadSession(FILENAME_SESSION_DEFAULT)
// TODO: delete legacy session file at some point
} else {
// Load current session then
return loadSession(fileNameFromSessionName(iCurrentSessionName))
}
}
/**
* Safely restore previous tabs
*/
private fun tryRestorePreviousTabs(activity: Activity): MutableList<TabInitializer>
{
return try {
restorePreviousTabs()
} catch (ex: Throwable) {
// TODO: report this using firebase or local crash logs
Timber.e(ex,"restorePreviousTabs failed")
activity.snackbar(R.string.error_recovery_session)
createRecoverySession()
}
}
/**
* Called whenever we fail to load a session properly.
* The idea is that it should enable the app to start even when it's pointing to a corrupted session.
*/
private fun createRecoverySession(): MutableList<TabInitializer>
{
recoverSessions()
// Add our recovery session using timestamp
iCurrentSessionName = application.getString(R.string.session_recovery) + "-" + Date().time
iSessions.add(Session(iCurrentSessionName,1, true))
return loadRecoverySession()
}
/**
* Provide a tab initializer for the given special URL
*/
fun tabInitializerForSpecialUrl(url: String): TabInitializer {
return when {
url.isBookmarkUrl() -> bookmarkPageInitializer
url.isDownloadsUrl() -> downloadPageInitializer
url.isStartPageUrl() -> homePageInitializer
url.isIncognitoPageUrl() -> incognitoPageInitializer
url.isHistoryUrl() -> historyPageInitializer
else -> homePageInitializer
}
}
/**
* Method used to resume all the tabs in the browser. This is necessary because we cannot pause
* the WebView when the application is open currently due to a bug in the WebView, where calling
* onResume doesn't consistently resume it.
*/
fun resumeAll() {
currentTab?.resumeTimers()
for (tab in tabList) {
tab.onResume()
tab.initializePreferences()
}
}
/**
* Method used to pause all the tabs in the browser. This is necessary because we cannot pause
* the WebView when the application is open currently due to a bug in the WebView, where calling
* onResume doesn't consistently resume it.
*/
fun pauseAll() {
currentTab?.pauseTimers()
tabList.forEach(WebPageTab::onPause)
}
/**
* Return the tab at the given position in tabs list, or null if position is not in tabs list
* range.
*
* @param position the index in tabs list
* @return the corespondent [WebPageTab], or null if the index is invalid
*/
fun getTabAtPosition(position: Int): WebPageTab? =
if (position < 0 || position >= tabList.size) {
null
} else {
tabList[position]
}
val allTabs: List<WebPageTab>
get() = tabList
/**
* Shutdown the manager. This destroys all tabs and clears the references to those tabs.
* Current tab is also released for garbage collection.
*/
fun shutdown() {
Timber.d("shutdown")
repeat(tabList.size) { deleteTab(0) }
savedRecentTabsIndices.clear()
isInitialized = false
currentTab = null
}
/**
* The current number of tabs in the manager.
*
* @return the number of tabs in the list.
*/
fun size(): Int = tabList.size
/**
* The index of the last tab in the manager.
*
* @return the last tab in the list or -1 if there are no tabs.
*/
fun last(): Int = tabList.size - 1
/**
* The last tab in the tab manager.
*
* @return the last tab, or null if there are no tabs.
*/
fun lastTab(): WebPageTab? = tabList.lastOrNull()
/**
* Create and return a new tab. The tab is automatically added to the tabs list.
*
* @param activity the activity needed to create the tab.
* @param tabInitializer the initializer to run on the tab after it's been created.
* @param isIncognito whether the tab is an incognito tab or not.
* @return a valid initialized tab.
*/
fun newTab(
activity: Activity,
tabInitializer: TabInitializer,
isIncognito: Boolean,
newTabPosition: NewTabPosition
): WebPageTab {
Timber.i("New tab")
val tab = WebPageTab(
activity,
tabInitializer,
isIncognito,
homePageInitializer,
incognitoPageInitializer,
bookmarkPageInitializer,
downloadPageInitializer,
historyPageInitializer
)
// Add our new tab at the specified position
when(newTabPosition){
NewTabPosition.BEFORE_CURRENT_TAB -> tabList.add(indexOfCurrentTab(), tab)
NewTabPosition.AFTER_CURRENT_TAB -> tabList.add(indexOfCurrentTab() + 1, tab)
NewTabPosition.START_OF_TAB_LIST -> tabList.add(0, tab)
NewTabPosition.END_OF_TAB_LIST -> tabList.add(tab)
}
tabNumberListeners.forEach { it(size()) }
return tab
}
/**
* Removes a tab from the list and destroys the tab. If the tab removed is the current tab, the
* reference to the current tab will be nullified.
*
* @param position The position of the tab to remove.
*/
private fun removeTab(position: Int) {
if (position >= tabList.size) {
return
}
val tab = tabList.removeAt(position)
iRecentTabs.remove(tab)
if (currentTab == tab) {
currentTab = null
}
tab.destroy()
}
/**
* Deletes a tab from the manager. If the tab being deleted is the current tab, this method will
* switch the current tab to a new valid tab.
*
* @param position the position of the tab to delete.
* @return returns true if the current tab was deleted, false otherwise.
*/
fun deleteTab(position: Int): Boolean {
Timber.i("Delete tab: $position")
val currentTab = currentTab
val current = positionOf(currentTab)
if (current == position) {
when {
size() == 1 -> this.currentTab = null
// Switch to previous tab
else -> switchToTab(indexOfTab(iRecentTabs.elementAt(iRecentTabs.size - 2)))
}
}
removeTab(position)
tabNumberListeners.forEach { it(size()) }
return current == position
}
/**
* Return the position of the given tab.
*
* @param tab the tab to look for.
* @return the position of the tab or -1 if the tab is not in the list.
*/
fun positionOf(tab: WebPageTab?): Int = tabList.indexOf(tab)
/**
* Save our states if needed.
*/
private fun saveIfNeeded() {
if (iIsIncognito) {
// We don't persist anything when browsing incognito
return
}
if (userPreferences.restoreTabsOnStartup) {
saveState()
}
else {
clearSavedState()
}
}
/**
* Saves the state of the current WebViews, to a bundle which is then stored in persistent
* storage and can be unparceled.
*/
fun saveState() {
Timber.d("saveState")
// Fix bug where all tabs would get lost
// See: https://github.com/Slion/Fulguris/issues/193
if (!isInitialized) {
Timber.d("saveState - Don't do that")
return
}
// Save sessions info
saveSessions()
// Save our session
saveCurrentSession(iCurrentSessionName)
}
/**
* Save current session including WebView tab states and recent tab list in the specified file.
*/
private fun saveCurrentSession(aName: String) {
Timber.d("saveCurrentSession - $aName")
val outState = Bundle(ClassLoader.getSystemClassLoader())
tabList
.withIndex()
.forEach { (index, tab) ->
// Index padding with zero to make sure they are restored in the correct order
// That gives us proper sorting up to 99999 tabs which should be more than enough :)
outState.putBundle(TAB_KEY_PREFIX + String.format("%05d", index), tab.saveState())
}
//Now save our recent tabs
// Create an array of tab indices from our recent tab list to be persisted
savedRecentTabsIndices.clear()
iRecentTabs.forEach { savedRecentTabsIndices.add(indexOfTab(it))}
outState.putIntArray(RECENT_TAB_INDICES, savedRecentTabsIndices.toIntArray())
// Write our bundle to disk
iScopeThreadPool.launch {
// Guessing delay is not needed since we do not use the main thread scope anymore
//delay(1L)
val temp = FILENAME_TEMP_PREFIX + aName
val backup = FILENAME_BACKUP_PREFIX + aName
val session = FILENAME_SESSION_PREFIX + aName
// Save to temporary session file
FileUtils.writeBundleToStorage(application, outState, temp)
// Defensively delete session backup, that should never be needed really
FileUtils.deleteBundleInStorage(application, backup)
// Rename our session file as backup
FileUtils.renameBundleInStorage(application, session, backup)
// Rename our temporary session to actual session
FileUtils.renameBundleInStorage(application, temp, session)
// Delete session backup
FileUtils.deleteBundleInStorage(application, backup)
// We used that loop to test that our jobs are completed no matter what when the app is closed.
// However long running tasks could run into race condition I guess if we queue it multiple times.
// I really don't understand what's going on exactly when we close the app twice and we have two instances of that job running.
// It looks like the process was not terminated when exiting the app the first time and both jobs are running in different thread on the same process.
// Though even when waiting for the end of that job before restarting the app Android can reuse that process anyway…
// Log example:
// date time PID-TID/package priority/tag: message
// 2022-01-11 11:32:59.939 23094-23207/net.slions.fulguris.full.download.debug D/TabsManager: Tick: 28
// 2022-01-11 11:33:00.224 23094-23208/net.slions.fulguris.full.download.debug D/TabsManager: Tick: 20
// repeat(30) {
// delay(1000L)
// logger.log(TAG, "Tick: $it")
// }
}
}
/**
* Provide session file name from session name
*/
private fun fileNameFromSessionName(aSessionName: String) : String {
return FILENAME_SESSION_PREFIX + aSessionName
}
/**
* Provide session file from session name
*/
fun fileFromSessionName(aName: String) : File {
return File(application.filesDir, fileNameFromSessionName(aName))
}
/**
* Use this method to clear the saved state if you do not wish it to be restored when the
* browser next starts.
*/
fun clearSavedState() = FileUtils.deleteBundleInStorage(application, FILENAME_SESSION_DEFAULT)
/**
*
*/
fun deleteSession(aSessionName: String) {
// TODO: handle case where we delete current session
if (aSessionName == iCurrentSessionName) {
// Can't do that for now
return
}
val index = iSessions.indexOf(session(aSessionName))
// Delete session file
FileUtils.deleteBundleInStorage(application, fileNameFromSessionName(iSessions[index].name))
// Remove session from our list
iSessions.removeAt(index)
}
/**
* Save our session list and current session name to disk.
*/
fun saveSessions() {
Timber.d("saveState")
val bundle = Bundle(javaClass.classLoader)
bundle.putString(KEY_CURRENT_SESSION, iCurrentSessionName)
bundle.putParcelableArrayList(KEY_SESSIONS, iSessions)
// Write our bundle to disk
iScopeThreadPool.launch {
// Guessing delay is not needed since we do not use the main thread scope anymore
//delay(1L)
FileUtils.writeBundleToStorage(application, bundle, FILENAME_SESSIONS)
}
}
/**
* Just the sessions list really
*/
fun deleteSessions() {
FileUtils.deleteBundleInStorage(application, FILENAME_SESSIONS)
}
/**
* Load our session list and current session name from disk.
*/
private fun loadSessions() {
val bundle = FileUtils.readBundleFromStorage(application, FILENAME_SESSIONS)
bundle?.apply{
getParcelableArrayList<Session>(KEY_SESSIONS)?.let { iSessions = it}
// Sessions must have been loaded when we load that guys
getString(KEY_CURRENT_SESSION)?.let {iCurrentSessionName = it}
}
// Somehow we lost that file again :)
// That crazy bug we keep chasing after
// TODO: consider running recovery even when our session list was loaded
if (iSessions.isNullOrEmpty()) {
recoverSessions()
// Set the first one as current one
if (!iSessions.isNullOrEmpty()) {
iCurrentSessionName = iSessions[0].name
}
}
}
/**
* Reset our session collection and repopulate by searching the file system for session files.
*/
private fun recoverSessions() {
// TODO: report this in firebase or local logs
Timber.i("recoverSessions")
//
iSessions.clear() // Defensive, should already be empty if we get there
// Search for session files
val files = application.filesDir?.let{it.listFiles { d, name -> name.startsWith(FILENAME_SESSION_PREFIX) }}
// Add recovered sessions to our collection
files?.forEach { f -> iSessions.add(Session(f.name.substring(FILENAME_SESSION_PREFIX.length), -1)) }
}
/**
*
*/
private fun readSavedStateFromDisk(aBundle: Bundle?): MutableList<TabModel> {
val list = mutableListOf<TabModel>()
aBundle?.keySet()
?.filter { it.startsWith(TAB_KEY_PREFIX) }
?.mapNotNull { bundleKey ->
aBundle.getBundle(bundleKey)?.let { list.add(TabModelFromBundle(it))}
}
return list;
}
/**
* Returns the index of the current tab.
*
* @return Return the index of the current tab, or -1 if the current tab is null.
*/
fun indexOfCurrentTab(): Int = tabList.indexOf(currentTab)
/**
* Returns the index of the tab.
*
* @return Return the index of the tab, or -1 if the tab isn't in the list.
*/
fun indexOfTab(tab: WebPageTab): Int = tabList.indexOf(tab)
/**
* Returns the [WebPageTab] with the provided hash, or null if there is no tab with the hash.
*
* @param hashCode the hashcode.
* @return the tab with an identical hash, or null.
*/
fun getTabForHashCode(hashCode: Int): WebPageTab? =
tabList.firstOrNull { lightningView -> lightningView.webView?.let { it.hashCode() == hashCode } == true }
/**
* Switch from the current tab to the one at the given [aPosition].
*
* @param aPosition Index of the tab we want to switch to.
* @exception IndexOutOfBoundsException if the provided index is out of range.
* @return The selected tab we just switched to.
*/
fun switchToTab(aPosition: Int): WebPageTab {
Timber.i("switch to tab: $aPosition")
return tabList[aPosition].also {
currentTab = it
// Put that tab at the top of our recent tab list
iRecentTabs.apply{
remove(it)
add(it)
}
//logger.log(TAG, "Recent indices: $recentTabsIndices")
}
}
/**
* Was needed instead of simple runnable to be able to implement run once after init function
*/
interface InitializationListener {
fun onInitializationComplete()
}
companion object {
private const val TAB_KEY_PREFIX = "TAB_"
// Preserve this file name for compatibility
private const val FILENAME_SESSION_DEFAULT = "SAVED_TABS.parcel"
private const val KEY_CURRENT_SESSION = "KEY_CURRENT_SESSION"
private const val KEY_SESSIONS = "KEY_SESSIONS"
private const val FILENAME_SESSIONS = "SESSIONS"
const val FILENAME_SESSION_PREFIX = "SESSION_"
const val FILENAME_TEMP_PREFIX = "TEMP_SESSION_"
const val FILENAME_BACKUP_PREFIX = "BACKUP_SESSION_"
private const val RECENT_TAB_INDICES = "RECENT_TAB_INDICES"
}
}