-
Notifications
You must be signed in to change notification settings - Fork 23
/
AppGrid.qml
550 lines (506 loc) · 23.3 KB
/
AppGrid.qml
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
import QtQuick 2.12
import QtQuick.Window 2.2
import QtQuick.Controls 2.5
import QtQuick.Controls.Universal 2.12
import QtQuick.XmlListModel 2.12
import QtGraphicalEffects 1.12
import Qt.labs.settings 1.0
import AndroidNative 1.0 as AN
import FileIO 1.0
LauncherPage {
id: appLauncher
anchors.fill: parent
property string textInput
property real labelPointSize: 16
property var appGroups: [] // QML elements with app grids
property var pinnedShortcuts: []
property int appCount: 0
property int selectedGroup: 0
property int maxAppCount: mainView.isTablet ? 15 : 12
property double lastAppsCheck: 0.0
onTextInputChanged: {
console.log("AppGrid | Text input changed: " + appLauncher.textInput)
for (var i = 0; i < appLauncher.appGroups.length; i++) {
var appGroup = appLauncher.appGroups[i]
appGroup.textInput = appLauncher.textInput
}
}
function updateAppLauncher(key, value) {
console.log("AppGrid | Will update app launcher: " + key + ", " + value)
if (key === "backgroundOpacity") {
for (var i = 0; i < appLauncher.appGroups.length; i++) {
var appGroup = appLauncher.appGroups[i]
if (value !== undefined) {
appGroup.backgroundOpacity = value
}
}
} else if (key === "useCategories") {
settings.useCategories = value
if (settings.useGroupedApps) {
appLauncher.selectedGroup = 0
var apps = getAllApps()
appLauncher.destroyAppGroups()
appLauncher.createAppGroups(getGroupedApps(apps))
}
} else if (key === "useGroupedApps") {
settings.useGroupedApps = value
appLauncher.selectedGroup = 0
apps = getAllApps()
appLauncher.destroyAppGroups()
appLauncher.createAppGroups(getGroupedApps(apps))
} else if (key === "coloredIcons") {
settings.useColoredIcons = value
for (i = 0; i < appLauncher.appGroups.length; i++) {
appGroup = appLauncher.appGroups[i]
appGroup.desaturation = value ? 0.0 : 1.0
}
}
}
function updateNotifications() {
AN.SystemDispatcher.dispatch("volla.launcher.callCountAction", {"is_read": 0})
AN.SystemDispatcher.dispatch("volla.launcher.threadsCountAction", {"read": 0})
AN.SystemDispatcher.dispatch("volla.launcher.otherAppNotificationAction", {"read": 0})
}
function getAllApps() {
var allApps = new Array
for (var i = 0; i < appLauncher.appGroups.length; i++) {
var appGroup = appLauncher.appGroups[i]
allApps = allApps.concat(appGroup.apps)
}
console.debug("AppGrid | Number of apps: " + allApps.length)
return allApps
}
function getGroupedApps(apps) {
var groupedApps = new Array
if (settings.useGroupedApps) {
apps.sort(function(a, b) { return b["statistic"] - a["statistic"] })
if (apps.length > appLauncher.maxAppCount) {
groupedApps.push( { "groupLabel": qsTr("Most used"), "apps": apps.slice(0, appLauncher.maxAppCount) } )
if (settings.useCategories) {
var remainingApps = apps.slice(appLauncher.maxAppCount)
remainingApps.sort(function(a, b) {
if (a.category > b.category) return 1
else if (a.category < b.category) return -1
else return 0
})
var groupLabel
var someApps
for (var i = 0; i < remainingApps.length; i++) {
var app = remainingApps[i]
var category = app.category !== "" ? app.category : qsTr("Other apps")
if (category !== groupLabel) {
if (groupLabel !== undefined) groupedApps.push({"groupLabel": groupLabel, "apps": someApps})
groupLabel = category
someApps = new Array
}
someApps.push(app)
}
groupedApps.push({"groupLabel": groupLabel, "apps": someApps})
} else {
groupedApps.push( { "groupLabel": qsTr("apps"), "apps": apps.slice(appLauncher.maxAppCount) } )
}
} else {
groupedApps.push( { "groupLabel": qsTr("Most used"), "apps": apps.slice(0,) } )
}
} else {
groupedApps.push( { "groupLabel": qsTr("Most used"), "apps": apps } )
}
return groupedApps
}
function createAppGroups(groupedApps) {
console.log("AppGrid | Will create app grids for " + groupedApps.length + " groups.")
groupedApps.forEach(function(appGroupInfos, index) {
console.log("AppGrid | Will create group " + index)
var component = Qt.createComponent("/AppGroup.qml", appLauncherColumn)
var properties = { "groupLabel": appGroupInfos["groupLabel"],
"groupIndex": index,
"selectedGroupIndex": appLauncher.selectedGroup,
"textInput": appLauncher.textInput,
"iconMap": mainView.iconMap,
"labelMap": mainView.labelMap,
"phoneApp": mainView.phoneApp,
"messageApp": mainView.messageApp,
"labelPointSize": appLauncher.labelPointSize,
"headerPointSize": mainView.mediumFontSize,
"innerSpacing": mainView.innerSpacing,
"componentSpacing" : mainView.componentSpacing,
"backgroundOpacity": mainView.backgroundOpacity,
"accentColor": mainView.accentColor,
"desaturation": settings.useColoredIcons ? 0.0 : 1.0,
"pinnedShortcuts": index === 0 ? appLauncher.pinnedShortcuts : new Array,
"apps": appGroupInfos["apps"]}
if (component.status !== Component.Ready) {
if (component.status === Component.Error)
console.debug("AppGrid | Error: "+ component.errorString() );
}
var object = component.createObject(appLauncherColumn, properties)
appLauncher.appGroups.push(object)
})
}
function destroyAppGroups() {
for (var i = 0; i < appLauncher.appGroups.length; i++) {
var appGroup = appLauncher.appGroups[i]
appGroup.destroy()
}
appLauncher.appGroups = new Array
}
Flickable {
id: appLauncherFlickable
anchors.fill: parent
contentWidth: parent.width
contentHeight: appLauncherColumn.height
Column {
id: appLauncherColumn
width: parent.width// - 2 * mainView.outerSpacing
Label {
id: headerTitle
topPadding: mainView.innerSpacing * 2
x: mainView.innerSpacing
text: qsTr("Apps")
font.pointSize: mainView.headerFontSize
font.weight: Font.Black
}
TextField {
id: headerTextField
topPadding: mainView.componentSpacing
bottomPadding: mainView.componentSpacing
leftPadding: 0.0
rightPadding: 0.0
x: mainView.innerSpacing
width: parent.width - mainView.innerSpacing * 2
placeholderText: qsTr("Filter apps")
color: mainView.fontColor
placeholderTextColor: "darkgrey"
font.pointSize: mainView.largeFontSize
background: Rectangle {
color: "transparent"
border.color: "transparent"
}
Binding {
target: appLauncher
property: "textInput"
value: headerTextField.displayText.toLowerCase()
}
Button {
id: headerDeleteButton
text: "<font color='#808080'>×</font>"
font.pointSize: mainView.largeFontSize * 2
flat: true
topPadding: 0.0
anchors.top: parent.top
anchors.right: parent.right
visible: headerTextField.displayText !== ""
onClicked: {
headerTextField.text = ""
headerTextField.focus = false
}
}
}
Component.onCompleted: {
console.log("AppGrid | Column completed")
AN.SystemDispatcher.dispatch("volla.launcher.getShortcuts", {})
//AN.SystemDispatcher.dispatch("volla.launcher.appCountAction", {})
}
function showGroup(groupIndex) {
console.log("AppGrid | Will show group " + groupIndex)
appLauncher.selectedGroup = groupIndex
for (var i = 0; i < appGroups.length; i++) {
var appGroup = appGroups[i]
appGroup.showApps(i === groupIndex)
appGroup.selectedGroupIndex = groupIndex
}
}
function openContextMenu(app, gridCell, gridView) {
contextMenu.app = app
contextMenu.gridView = gridView
contextMenu.isPinnedShortcut = app.shortcutId !== undefined && app.shortcutId.length > 0
contextMenu.popup(gridCell)
}
function closeContextMenu() {
contextMenu.dismiss()
}
}
}
Menu {
id: contextMenu
implicitHeight: addShortCutItem.height + openAppItem.height + removeAppItem.height + removePinnedShortcutItem.height + mainView.innerSpacing
topPadding: mainView.innerSpacing / 2
property double menuWidth: 250.0
property var app
property var gridView
property bool isPinnedShortcut: false
property bool canBeDeleted: false
property int menuItemHeight: 40
background: Rectangle {
id: menuBackground
implicitWidth: contextMenu.menuWidth
color: mainView.accentColor
radius: mainView.innerSpacing
}
MenuItem {
id: addShortCutItem
anchors.margins: mainView.innerSpacing
text: qsTr("Add to shortcuts")
font.pointSize: appLauncher.labelPointSize
contentItem: Label {
width: contextMenu.menuWidth
text: addShortCutItem.text
font: addShortCutItem.font
horizontalAlignment: Text.AlignHCenter
}
background: Rectangle {
anchors.fill: parent
color: "transparent"
}
onClicked: {
console.log("AppGrid | App " + contextMenu.app["label"] + " selected for shortcuts");
contextMenu.gridView.currentIndex = -1
mainView.updateAction(contextMenu.app["itemId"],
true,
mainView.settingsAction.CREATE,
{"id": contextMenu.app["itemId"],
"name": qsTr("Open") + " " + contextMenu.app["label"],
"activated": true} )
}
}
MenuItem {
id: openAppItem
anchors.margins: mainView.innerSpacing
font.pointSize: appLauncher.labelPointSize
contentItem: Label {
width: contextMenu.menuWidth
text: contextMenu.isPinnedShortcut ? qsTr("Open Shortcut") : qsTr("Open App")
horizontalAlignment: Text.AlignHCenter
}
background: Rectangle {
anchors.fill: parent
color: "transparent"
}
onClicked: {
console.log("AppGrid | App " + contextMenu.
app["label"] + " selected to open");
contextMenu.gridView.currentIndex = -1
if (contextMenu.isPinnedShortcut) {
AN.SystemDispatcher.dispatch("volla.launcher.launchShortcut",
{"shortcutId": contextMenu.app["itemId"], "package": contextMenu.app["package"]})
} else if (contextMenu.app.package === mainView.phoneApp) {
if (appLauncher.newCalls) {
AN.SystemDispatcher.dispatch("volla.launcher.dialerAction", {"app": mainView.phoneApp, "action": "log"})
AN.SystemDispatcher.dispatch("volla.launcher.updateCallsAsRead", { })
} else {
AN.SystemDispatcher.dispatch("volla.launcher.dialerAction", {"app": mainView.phoneApp})
}
} else {
AN.SystemDispatcher.dispatch("volla.launcher.runAppAction", {"appId": contextMenu.app["package"]})
}
}
}
MenuItem {
id: removeAppItem
anchors.margins: mainView.innerSpacing
height: removeAppItem.visible ? contextMenu.menuItemHeight : 0
font.pointSize: appLauncher.labelPointSize
contentItem: Label {
width: contextMenu.menuWidth
text: qsTr("Remove App")
horizontalAlignment: Text.AlignHCenter
}
background: Rectangle {
anchors.fill: parent
color: "transparent"
}
visible: contextMenu.canBeDeleted
onClicked: {
AN.SystemDispatcher.dispatch("volla.launcher.deleteAppAction", {"appId": contextMenu.app["package"]})
}
}
MenuItem {
id: removePinnedShortcutItem
anchors.margins: mainView.innerSpacing
height: removePinnedShortcutItem.visible ? contextMenu.menuItemHeight : 0
font.pointSize: appLauncher.labelPointSize
contentItem: Label {
width: contextMenu.menuWidth
text: qsTr("Remove Bookmark")
horizontalAlignment: Text.AlignHCenter
}
background: Rectangle {
anchors.fill: parent
color: "transparent"
}
visible: contextMenu.isPinnedShortcut
onClicked: {
console.log("AppGrid | App " + contextMenu.app.shortcutId + " selected to remove a shortcut");
contextMenu.gridView.currentIndex = -1
var shortcutId = contextMenu.app["itemId"]
for (var i = 0; i < appLauncher.appGroups.length; i++) {
var appGroup = appLauncher.appGroups[i]
appGroup.removePinnedShortcut(shortcutId)
}
AN.SystemDispatcher.dispatch("volla.launcher.removeShortcut", {"shortcutId": shortcutId})
disabledPinnedShortcuts.disableShortcut(shortcutId)
}
}
onAboutToShow: {
AN.SystemDispatcher.dispatch("volla.launcher.canDeleteAppAction", {"appId": contextMenu.app["package"]})
}
Connections {
target: AN.SystemDispatcher
onDispatched: {
if (type === "volla.launcher.canDeleteAppResponce") {
contextMenu.canBeDeleted = message["canDeleteApp"]
}
}
}
}
Connections {
target: AN.SystemDispatcher
onDispatched: {
if (type === "volla.launcher.appCountResponse") {
if (message["appCount"] !== settings.appCount) {
console.log("AppGrid | Number of apps: " + message["appCount"], ", " + settings.appCount)
settings.appCount = message["appCount"]
mainView.updateSpinner(true)
AN.SystemDispatcher.dispatch("volla.launcher.appAction", new Object)
} else if (new Date().valueOf() - settings.lastAppCountCheck > 3600000) {
console.debug("AppGrid | Will update statistics")
AN.SystemDispatcher.dispatch("volla.launcher.appAction", new Object)
} else if (settings.appCount !== appLauncher.getAllApps().length) {
var appsString = appsCache.readPrivate()
if (appsString.length > 0) {
console.debug("AppLauncher | Will read app cache")
var appsArray = JSON.parse(appsString)
var groupedApps = appLauncher.getGroupedApps(appsArray)
appLauncher.destroyAppGroups()
appLauncher.createAppGroups(groupedApps)
// Reflect different OS versions and devices
mainView.checkDefaultApps(appsArray)
//if (!appsString.includes("org.fossify.gallery")) mainView.galleryApp = "com.simplemobiletools.gallery.pro"
//if (!appsString.includes("org.fossify.calendar")) mainView.galleryApp = "com.simplemobiletools.calendar.pro"
} else {
console.log("AppLauncher | Need to retrieve apps from system")
mainView.updateSpinner(true)
AN.SystemDispatcher.dispatch("volla.launcher.appAction", new Object)
}
}
} else if (type === "volla.launcher.appResponse") {
console.log("AppGrid | " + message["appsCount"] + " app infos received")
if (settings.sync) {
settings.sync()
}
settings.lastAppCountCheck = new Date().valueOf()
console.log("App Launcher | Did store apps: " + appsCache.writePrivate(JSON.stringify(message["apps"])))
groupedApps = appLauncher.getGroupedApps(message["apps"])
if (appLauncher.appGroups.length !== groupedApps.lemgth) {
for (var i = 0; i < appLauncher.appGroups.length; i++) {
var appGroup = appLauncher.appGroups[i]
appGroup.destroy()
}
appLauncher.appGroups = new Array
appLauncher.createAppGroups(groupedApps)
} else {
for (i = 0; i < appLauncher.appGroups.length; i++) {
appLauncher.appGroup = appLauncher.appGroups[i]
appLauncher.appGroup.apps = groupedApps[i]["apps"]
}
}
mainView.updateSpinner(false)
mainView.checkDefaultApps(message["apps"])
} else if (type === "volla.launcher.receivedShortcut") {
console.log("AppGrid | New pinned shortcut: " + message["shortcutId"])
var shortcut = {"shortcutId": message["shortcutId"],
"package": message["package"],
"label": message["label"],
"icon": message["icon"] }
if (shortcut["shortcutId"] === undefined) {
mainView.showToast("ERROR: Undefined Shortcut Id")
return
}
if (shortcut["icon"] === undefined) {
mainView.showToast("ERROR: Undefined Shortcut Icin")
return
}
var disabledShortcutIds = disabledPinnedShortcuts.getShortcutIds()
i = disabledShortcutIds.indexOf(shortcut["shortcutId"])
if (i >= 0) {
disabledShortcutIds.splice(i, 1)
disabledPinnedShortcuts.saveShortcutIds(disabledShortcutIds)
}
var isExistingShortcut = appLauncher.pinnedShortcuts.some( function(app) {
return app.shorcutId !== undefined && app.shortcutId === message.shortcutId && app.package === message.package
})
if (!isExistingShortcut) {
mainView.showToast(qsTr("New pinned shortcut") + ": " + message["label"])
appLauncher.pinnedShortcuts.push(shortcut)
appGroup = appGroups[0]
if (appGroup !== undefined) {
appGroup.pinnedShortcuts = appLauncher.pinnedShortcuts
}
} else {
mainView.showToast(qsTr("Pinned shortcut already exists") + ": " + message["label"])
}
} else if (type === "volla.launcher.gotShortcuts") {
console.log("AppGrid | Pinned shortcuts received: " + message["pinnedShortcuts"].length)
if (message["pinnedShortcuts"].length > 0) {
disabledShortcutIds = disabledPinnedShortcuts.getShortcutIds()
console.debug("AppGrid | Disabled shortcuts " + disabledShortcutIds.length)
var pinnedShortcuts = new Array
for (i = 0; i < message["pinnedShortcuts"].length; i++) {
shortcut = message["pinnedShortcuts"][i]
if (disabledShortcutIds.indexOf(shortcut["shortcutId"]) < 0) {
pinnedShortcuts.push(shortcut)
}
}
appLauncher.pinnedShortcuts = pinnedShortcuts
appGroup = appGroups[0]
if (appGroup !== undefined) {
appGroup.pinnedShortcuts = appLauncher.pinnedShortcuts
}
}
}
}
}
Settings {
id: settings
property bool useColoredIcons: false
property bool useGroupedApps: true
property bool useCategories: false
property int appCount: 0
property double lastAppCountCheck: 0.0
}
FileIO {
id: disabledPinnedShortcuts
source: "dusabledShorcuts.json"
onError: {
console.log("AppGrid | Disabled shortcuts store error: " + msg)
}
function getShortcutIds() {
var shortcutIds = readPrivate()
if (shortcutIds !== undefined && shortcutIds.length > 0) {
return JSON.parse(shortcutIds)
} else {
return new Array
}
}
function saveShortcutIds(shortcutIds) {
writePrivate(JSON.stringify(shortcutIds))
}
function disableShortcut(shortcutId) {
var shortcutIds = readPrivate()
if (shortcutIds !== undefined && shortcutIds.length > 0) {
shortcutIds = JSON.parse(shortcutIds)
} else {
shortcutIds = new Array
}
shortcutIds.push(shortcutId)
saveShortcutIds(shortcutIds)
}
}
FileIO {
id: appsCache
source: "apps.json"
onError: {
console.log("AppLaunchr | Apps cache error: " + msg)
}
}
}