]> git.basschouten.com Git - openhab-addons.git/blob
5eff62b7e1d27e85f3759daefb7071c45165802a
[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.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.lametrictime.internal.api.dto.CoreApps;
20 import org.openhab.binding.lametrictime.internal.api.local.dto.Application;
21 import org.openhab.binding.lametrictime.internal.api.local.dto.Widget;
22
23 import com.google.gson.JsonPrimitive;
24
25 /**
26  * Utility class providing miscellaneous functionality based on the LaMetric Time API to support the binding.
27  *
28  * @author Gregory Moyer - Initial contribution
29  */
30 @NonNullByDefault
31 public class LaMetricTimeUtil {
32     private static final Map<String, String> CORE_APP_LABELS = new HashMap<>();
33     static {
34         CORE_APP_LABELS.put(CoreApps.clock().getPackageName(), "Clock");
35         CORE_APP_LABELS.put(CoreApps.countdown().getPackageName(), "Timer");
36         CORE_APP_LABELS.put(CoreApps.radio().getPackageName(), "Radio");
37         CORE_APP_LABELS.put(CoreApps.stopwatch().getPackageName(), "Stopwatch");
38         CORE_APP_LABELS.put(CoreApps.weather().getPackageName(), "Weather");
39     }
40
41     public static String getAppLabel(Application app, Widget widget) {
42         Map<String, JsonPrimitive> settings = widget.getSettings();
43         if (settings != null && settings.containsKey("_title")) {
44             String title = settings.get("_title").getAsString();
45             if (title != null && !title.isEmpty()) {
46                 return title;
47             }
48         }
49
50         String coreAppLabel = CORE_APP_LABELS.get(app.getPackageName());
51         if (coreAppLabel != null) {
52             return coreAppLabel;
53         }
54
55         return app.getVendor() + "'s App";
56     }
57 }