wangzhibo
4 天以前 ae6f40460dcd56af6c5f60ba52c883854c3bac55
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
/*  Copyright (C) 2021-2024 Arjan Schrijver
 
    This file is part of Gadgetbridge.
 
    Gadgetbridge is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published
    by the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
    Gadgetbridge is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.
 
    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.devices.qhybrid;
 
import android.content.Context;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
 
import nodomain.freeyourgadget.gadgetbridge.util.CRC32C;
import nodomain.freeyourgadget.gadgetbridge.util.StringUtils;
 
/**
 * Writes watch apps to a file in the Fossil Hybrid HR .wapp format.
 */
public class FossilAppWriter {
    private final Logger LOG = LoggerFactory.getLogger(FossilAppWriter.class);
    private Context mContext;
    private String version;
    private LinkedHashMap<String, InputStream> code;
    private LinkedHashMap<String, InputStream> icons;
    private LinkedHashMap<String, InputStream> layout;
    private LinkedHashMap<String, String> displayName;
    private LinkedHashMap<String, String> config;
 
    public FossilAppWriter(Context context, String version, LinkedHashMap<String, InputStream> code, LinkedHashMap<String, InputStream> icons, LinkedHashMap<String, InputStream> layout, LinkedHashMap<String, String> displayName, LinkedHashMap<String, String> config) {
        this.mContext = context;
        if (this.mContext == null) throw new AssertionError("context cannot be null");
        this.version = version;
        if (!this.version.matches("^[0-9]\\.[0-9]+$")) throw new AssertionError("Version must be in x.x format");
        this.code = code;
        if (this.code.size() == 0) throw new AssertionError("At least one code file InputStream must be supplied");
        this.icons = icons;
        if (this.icons == null) throw new AssertionError("icons cannot be null");
        this.layout = layout;
        if (this.layout == null) throw new AssertionError("layout cannot be null");
        this.displayName = displayName;
        if (this.displayName == null) throw new AssertionError("displayName cannot be null");
        this.config = config;
        if (this.config == null) throw new AssertionError("config cannot be null");
    }
 
    public byte[] getWapp() throws IOException {
        byte[] codeData = loadFiles(code, false);
        byte[] iconsData = loadFiles(icons, false);
        byte[] layoutData = loadFiles(layout, true);
        byte[] displayNameData = loadStringFiles(displayName);
        byte[] configData = loadStringFiles(config);
 
        int offsetCode = 88;
        int offsetIcons = offsetCode + codeData.length;
        int offsetLayout = offsetIcons + iconsData.length;
        int offsetDisplayName = offsetLayout + layoutData.length;
        int offsetConfig = offsetDisplayName + displayNameData.length;
        int offsetFileEnd = offsetConfig + configData.length;
 
        ByteArrayOutputStream filePart = new ByteArrayOutputStream();
        filePart.write(0x1);  // 1 = watchface, 2 = app
        String[] versionParts = this.version.split("\\.");
        for (String versionPart : versionParts) {
            filePart.write(Integer.valueOf(versionPart).byteValue());
        }
        filePart.write(0x0);
        filePart.write(intToLEBytes(0));
        filePart.write(intToLEBytes(0));
        filePart.write(intToLEBytes(offsetCode));
        filePart.write(intToLEBytes(offsetIcons));
        filePart.write(intToLEBytes(offsetLayout));
        filePart.write(intToLEBytes(offsetDisplayName));
        filePart.write(intToLEBytes(offsetDisplayName));
        filePart.write(intToLEBytes(offsetConfig));
        filePart.write(intToLEBytes(offsetFileEnd));
        filePart.write(intToLEBytes(0));
        filePart.write(intToLEBytes(0));
        filePart.write(intToLEBytes(0));
        filePart.write(intToLEBytes(0));
        filePart.write(intToLEBytes(0));
        filePart.write(intToLEBytes(0));
        filePart.write(intToLEBytes(0));
        filePart.write(intToLEBytes(0));
        filePart.write(intToLEBytes(0));
        filePart.write(codeData);
        filePart.write(iconsData);
        filePart.write(layoutData);
        filePart.write(displayNameData);
        filePart.write(configData);
        byte[] filePartBytes = filePart.toByteArray();
 
        ByteArrayOutputStream wapp = new ByteArrayOutputStream();
        wapp.write(new byte[]{(byte)0xFE, (byte)0x15});  // file handle
        wapp.write(new byte[]{(byte)0x03, (byte)0x00});  // file version
        wapp.write(intToLEBytes(0));  // file offset
        wapp.write(intToLEBytes(filePartBytes.length));
        wapp.write(filePartBytes);
 
        CRC32C crc = new CRC32C();
        crc.update(filePartBytes,0,filePartBytes.length);
        wapp.write(intToLEBytes((int)crc.getValue()));
 
        return wapp.toByteArray();
    }
 
    public byte[] loadFiles(LinkedHashMap<String, InputStream> filesMap, boolean appendNull) throws IOException {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        for (String filename : filesMap.keySet()) {
            InputStream in = filesMap.get(filename);
            output.write((byte)filename.length() + 1);
            output.write(StringUtils.terminateNull(filename).getBytes(StandardCharsets.UTF_8));
            int fileLength = in.available();
            if(appendNull){
                fileLength++;
            }
            output.write(shortToLEBytes((short)fileLength));
            byte[] fileBytes = new byte[in.available()];
            in.read(fileBytes);
            output.write(fileBytes);
            if(appendNull){
                output.write(0x00);
            }
        }
        return output.toByteArray();
    }
 
    public byte[] loadStringFiles(LinkedHashMap<String, String> stringsMap) throws IOException {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        for (String filename : stringsMap.keySet()) {
            output.write((byte)filename.length() + 1);
            output.write(StringUtils.terminateNull(filename).getBytes(StandardCharsets.UTF_8));
            output.write(shortToLEBytes((short)(stringsMap.get(filename).getBytes(StandardCharsets.UTF_8).length + 1)));
            output.write(StringUtils.terminateNull(stringsMap.get(filename)).getBytes(StandardCharsets.UTF_8));
        }
        return output.toByteArray();
    }
 
    private static byte[] intToLEBytes(int number) {
        byte[] b = new byte[4];
        b[0] = (byte) (number & 0xFF);
        b[1] = (byte) ((number >> 8) & 0xFF);
        b[2] = (byte) ((number >> 16) & 0xFF);
        b[3] = (byte) ((number >> 24) & 0xFF);
        return b;
    }
 
    private static byte[] shortToLEBytes(short number) {
        byte[] b = new byte[2];
        b[0] = (byte) (number & 0xFF);
        b[1] = (byte) ((number >> 8) & 0xFF);
        return b;
    }
}