summaryrefslogtreecommitdiff
path: root/rockworkd/libpebble/appmanager.cpp
blob: 04a99c7032bcb1fbbe52ce6a721716387546debf (plain)
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
#include <QDir>
#include <QSettings>

#include "appmanager.h"
#include "pebble.h"

#include "watchconnection.h"
#include "watchdatareader.h"
#include "watchdatawriter.h"
#include "uploadmanager.h"

#include <libintl.h>

#define SETTINGS_APP_UUID "07e0d9cb-8957-4bf7-9d42-35bf47caadfe"

AppManager::AppManager(Pebble *pebble, WatchConnection *connection)
    : QObject(pebble),
      m_pebble(pebble),
      m_connection(connection)
{
    QDir dataDir(m_pebble->storagePath() + "/apps/");
    if (!dataDir.exists() && !dataDir.mkpath(dataDir.absolutePath())) {
        qWarning() << "could not create apps dir" << dataDir.absolutePath();
    }
    qDebug() << "install apps in" << dataDir.absolutePath();

    m_connection->registerEndpointHandler(WatchConnection::EndpointAppFetch, this, "handleAppFetchMessage");
    m_connection->registerEndpointHandler(WatchConnection::EndpointSorting, this, "sortingReply");
}

QList<QUuid> AppManager::appUuids() const
{
    return m_appList;
}

//QList<QString> AppManager::appIds() const
//{
//    return m_appsIds.keys();
//}

AppInfo AppManager::info(const QUuid &uuid) const
{
    return m_apps.value(uuid);
}

//AppInfo AppManager::info(const QString &id) const
//{
//    return m_appsUuids.value(m_appsIds.value(id));
//}

void AppManager::rescan()
{
    m_appList.clear();
    m_apps.clear();

    AppInfo settingsApp(QUuid(SETTINGS_APP_UUID), false, gettext("Settings"), gettext("System app"));
    m_appList.append(settingsApp.uuid());
    m_apps.insert(settingsApp.uuid(), settingsApp);
    AppInfo watchfaces(QUuid("18e443ce-38fd-47c8-84d5-6d0c775fbe55"), false, gettext("Watchfaces"), gettext("System app"));
    m_appList.append(watchfaces.uuid());
    m_apps.insert(watchfaces.uuid(), watchfaces);
    if (m_pebble->capabilities().testFlag(CapabilityHealth)) {
        AppInfo health(QUuid("36d8c6ed-4c83-4fa1-a9e2-8f12dc941f8c"), false, gettext("Health"), gettext("System app"), true);
        m_appList.append(health.uuid());
        m_apps.insert(health.uuid(), health);
    }
    AppInfo music(QUuid("1f03293d-47af-4f28-b960-f2b02a6dd757"), false, gettext("Music"), gettext("System app"));
    m_appList.append(music.uuid());
    m_apps.insert(music.uuid(), music);
    AppInfo notifications(QUuid("b2cae818-10f8-46df-ad2b-98ad2254a3c1"), false, gettext("Notifications"), gettext("System app"));
    m_appList.append(notifications.uuid());
    m_apps.insert(notifications.uuid(), notifications);
    AppInfo alarms(QUuid("67a32d95-ef69-46d4-a0b9-854cc62f97f9"), false, gettext("Alarms"), gettext("System app"));
    m_appList.append(alarms.uuid());
    m_apps.insert(alarms.uuid(), alarms);
    AppInfo ticToc(QUuid("8f3c8686-31a1-4f5f-91f5-01600c9bdc59"), true, "Tic Toc", gettext("Default watchface"));
    m_appList.append(ticToc.uuid());
    m_apps.insert(ticToc.uuid(), ticToc);

    QDir dir(m_pebble->storagePath() + "/apps/");
    qDebug() << "Scanning Apps dir" << dir.absolutePath();
    Q_FOREACH(const QString &path, dir.entryList(QDir::Dirs | QDir::Readable)) {
        QString appPath = dir.absoluteFilePath(path);
        if (dir.exists(path + "/appinfo.json")) {
            scanApp(appPath);
        } else if (QFileInfo(appPath).isFile()) {
            scanApp(appPath);
        }
    }

    QSettings settings(m_pebble->storagePath() + "/apps.conf", QSettings::IniFormat);
    QStringList storedList = settings.value("appList").toStringList();
    if (storedList.isEmpty()) {
        // User did not manually sort the app list yet... We can stop here.
        return;
    }
    // Run some sanity checks
    if (storedList.count() != m_appList.count()) {
        qWarning() << "Installed apps not matching order config. App sort order might be wrong.";
        return;
    }
    foreach (const QUuid &uuid, m_appList) {
        if (!storedList.contains(uuid.toString())) {
            qWarning() << "Installed apps and stored config order cannot be matched. App sort order might be wrong.";
            return;
        }
    }
    // All seems fine, repopulate m_appList
    m_appList.clear();
    foreach (const QString &storedId, storedList) {
        m_appList.append(QUuid(storedId));
    }
}

void AppManager::handleAppFetchMessage(const QByteArray &data)
{
    WatchDataReader reader(data);
    reader.read<quint8>();
    QUuid uuid = reader.readUuid();
    quint32 appFetchId = reader.read<quint32>();

    bool haveApp = m_apps.contains(uuid);

    AppFetchResponse response;
    if (haveApp) {
        response.setStatus(AppFetchResponse::StatusStart);
        m_connection->writeToPebble(WatchConnection::EndpointAppFetch, response.serialize());
    } else {
        qWarning() << "App with uuid" << uuid.toString() << "which is not installed.";
        response.setStatus(AppFetchResponse::StatusInvalidUUID);
        m_connection->writeToPebble(WatchConnection::EndpointAppFetch, response.serialize());
        emit idMismatchDetected();
        return;
    }

    AppInfo appInfo = m_apps.value(uuid);

    QString binaryFile = appInfo.file(AppInfo::FileTypeApplication, m_pebble->hardwarePlatform());
    quint32 crc = appInfo.crc(AppInfo::FileTypeApplication, m_pebble->hardwarePlatform());
    qDebug() << "opened binary" << binaryFile << "for hardware" << m_pebble->hardwarePlatform() << "crc" << crc;
    m_connection->uploadManager()->uploadAppBinary(appFetchId, binaryFile, crc, [this, appInfo, appFetchId](){
        qDebug() << "binary file uploaded successfully";

        QString resourcesFile = appInfo.file(AppInfo::FileTypeResources, m_pebble->hardwarePlatform());
        quint32 crc = appInfo.crc(AppInfo::FileTypeResources, m_pebble->hardwarePlatform());
        qDebug() << "uploadign resource file" << resourcesFile;
        m_connection->uploadManager()->uploadAppResources(appFetchId, resourcesFile, crc, [this, appInfo, appFetchId]() {
            qDebug() << "resource file uploaded successfully";

            QString workerFile = appInfo.file(AppInfo::FileTypeWorker, m_pebble->hardwarePlatform());
            if (!workerFile.isEmpty()) {
                quint32 crc = appInfo.crc(AppInfo::FileTypeWorker, m_pebble->hardwarePlatform());
                m_connection->uploadManager()->uploadAppWorker(appFetchId, workerFile, crc, [this]() {
                    qDebug() << "worker file uploaded successfully";
                });
            }
        });
    });
}

void AppManager::sortingReply(const QByteArray &data)
{
    qDebug() << "have sorting reply" << data.toHex();
}

void AppManager::insertAppInfo(const AppInfo &info)
{
    m_appList.append(info.uuid());
    m_apps.insert(info.uuid(), info);
//    m_appsIds.insert(info.id(), info.uuid());
    emit appsChanged();
}

QUuid AppManager::scanApp(const QString &path)
{
    qDebug() << "scanning app" << path;
    AppInfo info(path);
    if (info.isValid()) {
        insertAppInfo(info);
    }
    return info.uuid();
}

void AppManager::removeApp(const QUuid &uuid)
{
    m_appList.removeAll(uuid);
    AppInfo info = m_apps.take(uuid);
    if (!info.isValid() || info.path().isEmpty()) {
        qWarning() << "App UUID not found. not removing";
        return;

    }
    QDir dir(info.path());
    dir.removeRecursively();
    emit appsChanged();
}

void AppManager::setAppOrder(const QList<QUuid> &newList)
{
    // run some sanity checks
    if (newList.count() != m_appList.count()) {
        qWarning() << "Number of apps in order list is not matching installed apps.";
        return;
    }
    foreach (const QUuid &installedUuid, m_appList) {
        if (!newList.contains(installedUuid)) {
            qWarning() << "App ids in order list not matching with installed apps.";
            return;
        }
    }
    if (newList.first() != QUuid(SETTINGS_APP_UUID)) {
        qWarning() << "Settings app must be the first app.";
        return;
    }

    m_appList = newList;
    QSettings settings(m_pebble->storagePath() + "/apps.conf", QSettings::IniFormat);
    QStringList tmp;
    foreach (const QUuid &id, m_appList) {
        tmp << id.toString();
    }
    settings.setValue("appList", tmp);
    emit appsChanged();

    QByteArray data;
    WatchDataWriter writer(&data);
    writer.write<quint8>(0x01);
    writer.write<quint8>(m_appList.count());
    foreach (const QUuid &uuid, m_appList) {
        writer.writeUuid(uuid);
    }

    qDebug() << "writing" << data.toHex();
    m_connection->writeToPebble(WatchConnection::EndpointSorting, data);
}

AppFetchResponse::AppFetchResponse(Status status):
    m_status(status)
{

}

void AppFetchResponse::setStatus(AppFetchResponse::Status status)
{
    m_status = status;
}

QByteArray AppFetchResponse::serialize() const
{
    QByteArray ret;
    WatchDataWriter writer(&ret);
    writer.write<quint8>(m_command);
    writer.write<quint8>(m_status);
    return ret;
}