/* * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the License); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an AS IS BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using System.Collections.Generic; namespace Tizen.System { /// /// The Information class provides functions to obtain various system information. /// /// 4 public static class Information { internal const string HttpPrefix = "http://"; internal const string RuntimeInfoStringKeyPrefix = "tizen.org/runtimefeature/"; internal const string RuntimeInfoStringKeyBluetooth = "bluetooth"; internal const string RuntimeInfoStringKeyTetheringWiFi = "tethering.wifi"; internal const string RuntimeInfoStringKeyTetheringBluetooth = "tethering.bluetooth"; internal const string RuntimeInfoStringKeyTetheringUsb = "tethering.usb"; internal const string RuntimeInfoStringKeyPacketData = "packetdata"; internal const string RuntimeInfoStringKeyDataRoaming = "dataroaming"; internal const string RuntimeInfoStringKeyVibration = "vibration"; internal const string RuntimeInfoStringKeyAudioJackConnected = "audiojack.connected"; internal const string RuntimeInfoStringKeyBatteryCharging = "battery.charging"; internal const string RuntimeInfoStringKeyTvOut = "tvout"; internal const string RuntimeInfoStringKeyCharger = "charger"; internal const string RuntimeInfoStringKeyAutoRotation = "autorotation"; internal const string RuntimeInfoStringKeyGps = "gps"; internal const string RuntimeInfoStringKeyAudioJackType = "audiojack.type"; private static readonly Dictionary StringEnumMapping = new Dictionary { [RuntimeInfoStringKeyPrefix + RuntimeInfoStringKeyBluetooth] = RuntimeInfoKey.Bluetooth, [RuntimeInfoStringKeyPrefix + RuntimeInfoStringKeyTetheringWiFi] = RuntimeInfoKey.WifiHotspot, [RuntimeInfoStringKeyPrefix + RuntimeInfoStringKeyTetheringBluetooth] = RuntimeInfoKey.BluetoothTethering, [RuntimeInfoStringKeyPrefix + RuntimeInfoStringKeyTetheringUsb] = RuntimeInfoKey.UsbTethering, [RuntimeInfoStringKeyPrefix + RuntimeInfoStringKeyPacketData] = RuntimeInfoKey.PacketData, [RuntimeInfoStringKeyPrefix + RuntimeInfoStringKeyDataRoaming] = RuntimeInfoKey.DataRoaming, [RuntimeInfoStringKeyPrefix + RuntimeInfoStringKeyVibration] = RuntimeInfoKey.Vibration, [RuntimeInfoStringKeyPrefix + RuntimeInfoStringKeyAudioJackConnected] = RuntimeInfoKey.AudioJack, [RuntimeInfoStringKeyPrefix + RuntimeInfoStringKeyBatteryCharging] = RuntimeInfoKey.BatteryIsCharging, [RuntimeInfoStringKeyPrefix + RuntimeInfoStringKeyTvOut] = RuntimeInfoKey.TvOut, [RuntimeInfoStringKeyPrefix + RuntimeInfoStringKeyCharger] = RuntimeInfoKey.Charger, [RuntimeInfoStringKeyPrefix + RuntimeInfoStringKeyAutoRotation] = RuntimeInfoKey.AutoRotation, [RuntimeInfoStringKeyPrefix + RuntimeInfoStringKeyGps] = RuntimeInfoKey.Gps, [RuntimeInfoStringKeyPrefix + RuntimeInfoStringKeyAudioJackType] = RuntimeInfoKey.AudioJackConnector }; internal static readonly Dictionary EnumStringMapping = new Dictionary { [RuntimeInfoKey.Bluetooth] = RuntimeInfoStringKeyBluetooth, [RuntimeInfoKey.WifiHotspot] = RuntimeInfoStringKeyTetheringWiFi, [RuntimeInfoKey.BluetoothTethering] = RuntimeInfoStringKeyTetheringBluetooth, [RuntimeInfoKey.UsbTethering] = RuntimeInfoStringKeyTetheringUsb, [RuntimeInfoKey.PacketData] = RuntimeInfoStringKeyPacketData, [RuntimeInfoKey.DataRoaming] = RuntimeInfoStringKeyDataRoaming, [RuntimeInfoKey.Vibration] = RuntimeInfoStringKeyVibration, [RuntimeInfoKey.AudioJack] = RuntimeInfoStringKeyAudioJackConnected, [RuntimeInfoKey.BatteryIsCharging] = RuntimeInfoStringKeyBatteryCharging, [RuntimeInfoKey.TvOut] = RuntimeInfoStringKeyTvOut, [RuntimeInfoKey.Charger] = RuntimeInfoStringKeyCharger, [RuntimeInfoKey.AutoRotation] = RuntimeInfoStringKeyAutoRotation, [RuntimeInfoKey.Gps] = RuntimeInfoStringKeyGps, [RuntimeInfoKey.AudioJackConnector] = RuntimeInfoStringKeyAudioJackType }; private static bool ConvertStringToRuntimeInfoKey(string key, out RuntimeInfoKey feature) { string filteredKey = key.StartsWith(HttpPrefix) ? key.Substring(HttpPrefix.Length) : key; feature = default(RuntimeInfoKey); return StringEnumMapping.TryGetValue(filteredKey, out feature); } private static bool TryGetRuntimeInfoValue(RuntimeInfoKey key, out T value) { value = default(T); if (!RuntimeInfo.Is(key)) { Log.Error(InformationErrorFactory.LogTag, "Invalid return type"); return false; } return RuntimeInfo.TryGetValue(key, out value); } #pragma warning disable CS0618 // Type or member is obsolete private static bool TryGetSystemInfoValue(string key, out T value) { value = default(T); if (!SystemInfo.IsValidKey(key)) { Log.Error(InformationErrorFactory.LogTag, "Invalid key"); return false; } if (!SystemInfo.Is(key)) { Log.Error(InformationErrorFactory.LogTag, "Invalid return type"); return false; } return SystemInfo.TryGetValue(key, out value); } #pragma warning restore CS0618 // Type or member is obsolete /// /// Gets the value of the feature. /// /// 4 /// The type of . /// The name of the feature. /// The value of the given feature. /// Returns true on success, otherwise false. public static bool TryGetValue(string key, out T value) { RuntimeInfoKey runtimeFeature; if (ConvertStringToRuntimeInfoKey(key, out runtimeFeature)) { return TryGetRuntimeInfoValue(runtimeFeature, out value); } else { return TryGetSystemInfoValue(key, out value); } } /// /// Registers a change event callback for given runtime feature key. /// /// /// This function is only for runtime feature. /// /// 4 /// The name of runtime feature which wants to register callback. /// The callback function to subscribe. /// Thrown when the is invalid. /// Thrown when the feature related is not supported. public static void SetCallback(string key, EventHandler callback) { RuntimeInfoKey runtimeFeature; if (!ConvertStringToRuntimeInfoKey(key, out runtimeFeature)) { Log.Error(InformationErrorFactory.LogTag, "Invalid key"); InformationErrorFactory.ThrowException(InformationError.InvalidParameter); } RuntimeInfo.SetCallback(runtimeFeature, callback); } /// /// Unregisters a change event callback for given runtime feature key. /// /// /// This function is only for runtime feature. /// /// 4 /// The name of runtime feature which wants to unregister callback. /// The callback function to unsubscribe. /// Thrown when the is invalid. /// Thrown when the feature related is not supported. public static void UnsetCallback(string key, EventHandler callback) { RuntimeInfoKey runtimeFeature; if (!ConvertStringToRuntimeInfoKey(key, out runtimeFeature)) { Log.Error(InformationErrorFactory.LogTag, "Invalid key"); InformationErrorFactory.ThrowException(InformationError.InvalidParameter); } RuntimeInfo.UnsetCallback(runtimeFeature, callback); } } }