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
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
/*  Copyright (C) 2024 Vitalii Tomin
 
    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.huawei;
 
import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.util.Arrays;
import java.util.List;
 
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.InstallActivity;
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
import nodomain.freeyourgadget.gadgetbridge.devices.InstallHandler;
 
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.GenericItem;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.HuaweiAppManager;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.HuaweiFwHelper;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.HuaweiMusicManager;
import nodomain.freeyourgadget.gadgetbridge.service.devices.huawei.HuaweiWatchfaceManager;
 
public class HuaweiInstallHandler implements InstallHandler {
    private static final Logger LOG = LoggerFactory.getLogger(HuaweiInstallHandler.class);
 
    private final Context context;
    protected final HuaweiFwHelper helper;
 
    boolean valid = false;
 
    public HuaweiInstallHandler(Uri uri, Context context) {
        this.context = context;
        this.helper = new HuaweiFwHelper(uri, context);
    }
 
    private HuaweiMusicUtils.FormatRestrictions getRestriction(HuaweiMusicUtils.MusicCapabilities capabilities, String ext) {
        List<HuaweiMusicUtils.FormatRestrictions> restrictions = capabilities.formatsRestrictions;
        if(restrictions == null)
            return null;
 
        for(HuaweiMusicUtils.FormatRestrictions r: restrictions) {
            if(ext.equals(r.getName())) {
                return r;
            }
        }
        return null;
    }
 
    //TODO: add proper checks
    private boolean checkMediaCompatibility(HuaweiMusicUtils.MusicCapabilities capabilities, HuaweiMusicManager.AudioInfo currentMusicInfo) {
        if(capabilities == null) {
            LOG.error("No media info from device");
            return false;
        }
        String ext = currentMusicInfo.getExtension();
 
        List<String> supportedFormats = capabilities.supportedFormats;
        if(supportedFormats == null) {
            LOG.error("Format not supported {}", ext);
            return false;
        }
        if(!supportedFormats.contains(ext)) {
            LOG.error("Format not supported {}", ext);
            return false;
        }
 
        HuaweiMusicUtils.FormatRestrictions restrictions = getRestriction(capabilities, ext);
        if(restrictions == null) {
            LOG.info("no restriction for: {}", ext);
            return true;
        }
 
        LOG.info("bitrate {}", restrictions.bitrate);
        LOG.info("channels {}", restrictions.channels);
        LOG.info("musicEncode {}", restrictions.musicEncode);
        LOG.info("sampleRate {}", Arrays.toString(restrictions.sampleRates));
        LOG.info("unknownBitrate {}", restrictions.unknownBitrate);
 
        if(currentMusicInfo.getChannels() > restrictions.channels) {
            LOG.error("Not supported channels count {} > {}", currentMusicInfo.getChannels(), restrictions.channels);
            return false;
        }
 
        //TODO: check other restrictions.
 
        return true;
    }
 
 
    @Override
    public void validateInstallation(InstallActivity installActivity, GBDevice device) {
 
        final DeviceCoordinator coordinator = device.getDeviceCoordinator();
        if (!(coordinator instanceof HuaweiCoordinatorSupplier)) {
            LOG.warn("Coordinator is not a HuaweiCoordinatorSupplier: {}", coordinator.getClass());
            installActivity.setInstallEnabled(false);
            return;
        }
 
        if (helper.isWatchface()) {
            final HuaweiCoordinatorSupplier huaweiCoordinatorSupplier = (HuaweiCoordinatorSupplier) coordinator;
 
            HuaweiWatchfaceManager.WatchfaceDescription description = helper.getWatchfaceDescription();
 
            HuaweiWatchfaceManager.Resolution resolution = new HuaweiWatchfaceManager.Resolution();
            String deviceScreen = String.format("%d*%d", huaweiCoordinatorSupplier.getHuaweiCoordinator().getHeight(),
                    huaweiCoordinatorSupplier.getHuaweiCoordinator().getWidth());
            this.valid = resolution.isValid(description.screen, deviceScreen);
 
            installActivity.setInstallEnabled(true);
 
            GenericItem installItem = new GenericItem();
 
            if (helper.getPreviewBitmap() != null) {
                installItem.setPreview(helper.getPreviewBitmap());
            }
 
            installItem.setName(description.title);
            installActivity.setInstallItem(installItem);
            if (device.isBusy()) {
                LOG.error("Watchface cannot be installed (device busy)");
                installActivity.setInfoText("Watchface cannot be installed (device busy)");
                installActivity.setInfoText(device.getBusyTask());
                installActivity.setInstallEnabled(false);
                return;
            }
 
            if (!device.isConnected()) {
                LOG.error("Watchface cannot be installed (not connected or wrong device)");
                installActivity.setInfoText("Watchface cannot be installed (not connected or wrong device)");
                installActivity.setInstallEnabled(false);
                return;
            }
 
            if (!this.valid) {
                LOG.error("Watchface cannot be installed");
                installActivity.setInfoText(context.getString(R.string.watchface_resolution_doesnt_match,
                        resolution.screenByThemeVersion(description.screen), deviceScreen));
                installActivity.setInstallEnabled(false);
                return;
            }
 
            installItem.setDetails(description.version);
 
            installItem.setIcon(R.drawable.ic_watchface);
            installActivity.setInfoText(context.getString(R.string.watchface_install_info, installItem.getName(), description.version, description.author));
 
            LOG.debug("Initialized HuaweiInstallHandler: Watchface");
        } else if (helper.isAPP()) {
            final HuaweiAppManager.AppConfig config = helper.getAppConfig();
 
            this.valid = true; //NOTE: nothing to verify for now
 
            installActivity.setInstallEnabled(true);
 
            GenericItem installItem = new GenericItem();
 
            if (helper.getPreviewBitmap() != null) {
                installItem.setPreview(helper.getPreviewBitmap());
            }
 
            installItem.setName(config.bundleName);
            installActivity.setInstallItem(installItem);
            if (device.isBusy()) {
                LOG.error("App cannot be installed (device busy)");
                installActivity.setInfoText("App cannot be installed (device busy)");
                installActivity.setInfoText(device.getBusyTask());
                installActivity.setInstallEnabled(false);
                return;
            }
 
            if (!device.isConnected()) {
                LOG.error("App cannot be installed (not connected or wrong device)");
                installActivity.setInfoText("App cannot be installed (not connected or wrong device)");
                installActivity.setInstallEnabled(false);
                return;
            }
 
            if (!this.valid) {
                LOG.error("App cannot be installed");
                installActivity.setInstallEnabled(false);
                return;
            }
 
            installItem.setDetails(config.version);
 
            installItem.setIcon(R.drawable.ic_watchapp);
 
            installActivity.setInfoText(context.getString(R.string.app_install_info, installItem.getName(), config.version, config.vendor));
 
            LOG.debug("Initialized HuaweiInstallHandler: App");
        } else if (helper.isMusic()) {
            final HuaweiCoordinatorSupplier huaweiCoordinatorSupplier = (HuaweiCoordinatorSupplier) coordinator;
 
            HuaweiMusicUtils.MusicCapabilities capabilities = huaweiCoordinatorSupplier.getHuaweiCoordinator().getExtendedMusicInfoParams();
            if(capabilities == null) {
                capabilities = huaweiCoordinatorSupplier.getHuaweiCoordinator().getMusicInfoParams();
            }
            HuaweiMusicManager.AudioInfo currentMusicInfo = helper.getMusicInfo();
 
            boolean isMediaCompatible = checkMediaCompatibility(capabilities, currentMusicInfo);
 
            this.valid = isMediaCompatible && !TextUtils.isEmpty(helper.getMusicInfo().getFileName()) && !TextUtils.isEmpty(helper.getMusicInfo().getArtist()) && !TextUtils.isEmpty(helper.getMusicInfo().getTitle());
 
            installActivity.setInstallEnabled(true);
 
            GenericItem installItem = new GenericItem();
 
            installItem.setName(helper.getFileName());
            installActivity.setInstallItem(installItem);
            if (device.isBusy()) {
                LOG.error("Music cannot be uploaded (device busy)");
                installActivity.setInfoText("Music cannot be uploaded (device busy)");
                installActivity.setInfoText(device.getBusyTask());
                installActivity.setInstallEnabled(false);
                return;
            }
 
            if (!device.isConnected()) {
                LOG.error("Music cannot be uploaded (not connected or wrong device)");
                installActivity.setInfoText("Music cannot be uploaded (not connected or wrong device)");
                installActivity.setInstallEnabled(false);
                return;
            }
 
            if (!this.valid) {
                LOG.error("Music cannot be uploaded");
                installActivity.setInfoText("Music cannot be uploaded");
                installActivity.setInstallEnabled(false);
                return;
            }
 
            installItem.setDetails(helper.getMusicInfo().getFileName());
 
            installItem.setIcon(R.drawable.ic_music);
 
            installActivity.setInfoText(context.getString(R.string.music_upload_info, helper.getMusicInfo().getFileName(), helper.getMusicInfo().getTitle(), helper.getMusicInfo().getArtist()));
 
            LOG.debug("Initialized HuaweiInstallHandler: Music");
        }
 
    }
 
    @Override
    public boolean isValid() {
        return helper.isValid();
    }
 
    @Override
    public void onStartInstall(GBDevice device) {
        helper.unsetFwBytes();
    }
}