]> git.basschouten.com Git - openhab-addons.git/blob
c533a9ccfc83b0f5d8187b4a471e448c2f857c34
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.lametrictime.internal;
14
15 import java.util.HashMap;
16 import java.util.Map;
17
18 import org.openhab.binding.lametrictime.api.local.model.Application;
19 import org.openhab.binding.lametrictime.api.local.model.Widget;
20 import org.openhab.binding.lametrictime.api.model.CoreApps;
21
22 import com.google.gson.JsonPrimitive;
23
24 /**
25  * Utility class providing miscellaneous functionality based on the LaMetric Time API to support the binding.
26  *
27  * @author Gregory Moyer - Initial contribution
28  */
29 public class LaMetricTimeUtil {
30     private static final Map<String, String> CORE_APP_LABELS = new HashMap<>();
31     static {
32         CORE_APP_LABELS.put(CoreApps.clock().getPackageName(), "Clock");
33         CORE_APP_LABELS.put(CoreApps.countdown().getPackageName(), "Timer");
34         CORE_APP_LABELS.put(CoreApps.radio().getPackageName(), "Radio");
35         CORE_APP_LABELS.put(CoreApps.stopwatch().getPackageName(), "Stopwatch");
36         CORE_APP_LABELS.put(CoreApps.weather().getPackageName(), "Weather");
37     }
38
39     public static String getAppLabel(Application app, Widget widget) {
40         Map<String, JsonPrimitive> settings = widget.getSettings();
41         if (settings != null && settings.containsKey("_title")) {
42             String title = settings.get("_title").getAsString();
43             if (title != null && !title.isEmpty()) {
44                 return title;
45             }
46         }
47
48         String coreAppLabel = CORE_APP_LABELS.get(app.getPackageName());
49         if (coreAppLabel != null) {
50             return coreAppLabel;
51         }
52
53         return app.getVendor() + "'s App";
54     }
55 }