/*
* Copyright (c) 2016 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.ComponentModel;
namespace Tizen.System
{
[EditorBrowsable(EditorBrowsableState.Never)]
internal static class SystemInfo
{
[EditorBrowsable(EditorBrowsableState.Never)]
private static Interop.SystemInfo.SystemInfoType GetValueType(string key, out Interop.SystemInfo.SystemInfoValueType valueType)
{
InformationError err = Interop.SystemInfo.SystemInfoGetPlatformType(key, out valueType);
if (err == InformationError.None)
{
return Interop.SystemInfo.SystemInfoType.platform;
}
Log.Debug(InformationErrorFactory.LogTag, string.Format("Key {0} not in platform system info", key));
err = Interop.SystemInfo.SystemInfoGetCustomType(key, out valueType);
if (err == InformationError.None)
{
return Interop.SystemInfo.SystemInfoType.Custom;
}
Log.Debug(InformationErrorFactory.LogTag, string.Format("Key {0} not in custom system info", key));
return Interop.SystemInfo.SystemInfoType.None;
}
///
/// Checks if the type of value for the given feature is T.
///
/// Type of value for the feature key.
/// The name of the feature.
/// True if type of value for the given feature is T, otherwise false.
internal static bool Is(string key)
{
Interop.SystemInfo.SystemInfoValueType valueType;
Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
if (keyType == Interop.SystemInfo.SystemInfoType.None)
{
return false;
}
switch (valueType)
{
case Interop.SystemInfo.SystemInfoValueType.Bool:
return typeof(T) == typeof(bool);
case Interop.SystemInfo.SystemInfoValueType.Double:
return typeof(T) == typeof(double);
case Interop.SystemInfo.SystemInfoValueType.Int:
return typeof(T) == typeof(int);
case Interop.SystemInfo.SystemInfoValueType.String:
return typeof(T) == typeof(string);
}
return false;
}
///
/// Checks if the given key is a valid feature.
///
/// The name of the feature.
/// True if the key is valid, otherwise false.
internal static bool IsValidKey(string key)
{
Interop.SystemInfo.SystemInfoValueType valueType;
return GetValueType(key, out valueType) != Interop.SystemInfo.SystemInfoType.None;
}
///
/// Gets the value of the feature.
///
/// Type of key value.
/// The name of the feature.
/// The value of the given feature.
/// Returns true on success, otherwise false.
internal static bool TryGetValue(string key, out T value)
{
bool res = false;
if (typeof(T) == typeof(bool))
{
bool val;
res = TryGetValue(key, out val);
value = (T)(object)val;
}
else if (typeof(T) == typeof(int))
{
int val;
res = TryGetValue(key, out val);
value = (T)(object)val;
}
else if (typeof(T) == typeof(double))
{
double val;
res = TryGetValue(key, out val);
value = (T)(object)val;
}
else if (typeof(T) == typeof(string))
{
string val;
res = TryGetValue(key, out val);
value = (T)(object)val;
}
else
{
value = default(T);
}
return res;
}
///
/// Gets the bool value of the feature.
///
/// The name of the feature.
/// The value of the given feature.
/// Returns true on success, otherwise false.
internal static bool TryGetValue(string key, out bool value)
{
Interop.SystemInfo.SystemInfoValueType valueType;
Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
InformationError err = InformationError.InvalidParameter;
if (keyType == Interop.SystemInfo.SystemInfoType.platform)
{
err = Interop.SystemInfo.SystemInfoGetPlatformBool(key, out value);
}
else if (keyType == Interop.SystemInfo.SystemInfoType.Custom)
{
err = Interop.SystemInfo.SystemInfoGetCustomBool(key, out value);
} else
{
value = false;
}
if (err != InformationError.None)
{
Log.Warn(InformationErrorFactory.LogTag, string.Format("Failed to get value for key: {0}. err = {1}", key, err));
return false;
}
return true;
}
///
/// Gets the int value of the feature.
///
/// The name of the feature.
/// The value of the given feature.
/// Returns true on success, otherwise false.
internal static bool TryGetValue(string key, out int value)
{
Interop.SystemInfo.SystemInfoValueType valueType;
Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
InformationError err = InformationError.InvalidParameter;
if (keyType == Interop.SystemInfo.SystemInfoType.platform)
{
err = Interop.SystemInfo.SystemInfoGetPlatformInt(key, out value);
}
else if (keyType == Interop.SystemInfo.SystemInfoType.Custom)
{
err = Interop.SystemInfo.SystemInfoGetCustomInt(key, out value);
}
else
{
value = 0;
}
if (err != InformationError.None)
{
Log.Warn(InformationErrorFactory.LogTag, string.Format("Failed to get value for key: {0}. err = {1}", key, err));
return false;
}
return true;
}
///
/// Gets the double value of the feature.
///
/// The name of the feature.
/// The value of the given feature.
/// Returns true on success, otherwise false.
internal static bool TryGetValue(string key, out double value)
{
Interop.SystemInfo.SystemInfoValueType valueType;
Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
InformationError err = InformationError.InvalidParameter;
if (keyType == Interop.SystemInfo.SystemInfoType.platform)
{
err = Interop.SystemInfo.SystemInfoGetPlatformDouble(key, out value);
}
else if (keyType == Interop.SystemInfo.SystemInfoType.Custom)
{
err = Interop.SystemInfo.SystemInfoGetCustomDouble(key, out value);
}
else
{
value = 0;
}
if (err != InformationError.None)
{
Log.Warn(InformationErrorFactory.LogTag, string.Format("Failed to get value for key: {0}. err = {1}", key, err));
return false;
}
return true;
}
///
/// Gets the string value of the feature.
///
/// The name of the feature.
/// The value of the given feature.
/// Returns true on success, otherwise false.
internal static bool TryGetValue(string key, out string value)
{
Interop.SystemInfo.SystemInfoValueType valueType;
Interop.SystemInfo.SystemInfoType keyType = GetValueType(key, out valueType);
InformationError err = InformationError.InvalidParameter;
if (keyType == Interop.SystemInfo.SystemInfoType.platform)
{
err = Interop.SystemInfo.SystemInfoGetPlatformString(key, out value);
}
else if (keyType == Interop.SystemInfo.SystemInfoType.Custom)
{
err = Interop.SystemInfo.SystemInfoGetCustomString(key, out value);
}
else
{
value = string.Empty;
}
if (err != InformationError.None)
{
Log.Warn(InformationErrorFactory.LogTag, string.Format("Failed to get value for key: {0}. err = {1}", key, err));
return false;
}
return true;
}
}
}