From 5c4a55de5c9a9f2427e18334c5d13224d96d547d Mon Sep 17 00:00:00 2001 From: zwtmichael Date: Mon, 30 Sep 2024 15:57:57 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E6=96=B0=E5=A2=9Epreference=20ndk=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zwtmichael --- distributeddatamgr/preferences/BUILD.gn | 40 +++ .../preferences/include/oh_preferences.h | 271 ++++++++++++++++++ .../include/oh_preferences_err_code.h | 77 +++++ .../include/oh_preferences_option.h | 118 ++++++++ .../include/oh_preferences_value.h | 174 +++++++++++ .../preferences/libpreferences.ndk.json | 94 ++++++ 6 files changed, 774 insertions(+) create mode 100644 distributeddatamgr/preferences/BUILD.gn create mode 100644 distributeddatamgr/preferences/include/oh_preferences.h create mode 100644 distributeddatamgr/preferences/include/oh_preferences_err_code.h create mode 100644 distributeddatamgr/preferences/include/oh_preferences_option.h create mode 100644 distributeddatamgr/preferences/include/oh_preferences_value.h create mode 100644 distributeddatamgr/preferences/libpreferences.ndk.json diff --git a/distributeddatamgr/preferences/BUILD.gn b/distributeddatamgr/preferences/BUILD.gn new file mode 100644 index 000000000..90671c646 --- /dev/null +++ b/distributeddatamgr/preferences/BUILD.gn @@ -0,0 +1,40 @@ +# Copyright (c) 2024 Huawei Device Co., Ltd. +# 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. + +import("//build/ohos.gni") +import("//build/ohos/ndk/ndk.gni") +import("//foundation/distributeddatamgr/preferences/preferences.gni") + +ohos_ndk_headers("preferences_ndk_header") { + dest_dir = "$ndk_headers_out_dir/database/preferences/" + sources = [ + "./include/oh_preferences.h", + "./include/oh_preferences_err_code.h", + "./include/oh_preferences_option.h", + "./include/oh_preferences_values.h", + ] +} + +ohos_ndk_library("libohpreferences") { + output_name = "ohpreferences" + output_extension = "so" + system_capability = "SystemCapability.DistributedDataManager.Preferences.Core" + ndk_description_file = "./libpreferences.ndk.json" + min_compact_version = "13" + system_capability_headers = [ + "$ndk_headers_out_dir/database/preferences/oh_preferences.h", + "$ndk_headers_out_dir/database/preferences/oh_preferences_err_code.h", + "$ndk_headers_out_dir/database/preferences/oh_preferences_values.h", + "$ndk_headers_out_dir/database/preferences/oh_preferences_option.h", + ] +} diff --git a/distributeddatamgr/preferences/include/oh_preferences.h b/distributeddatamgr/preferences/include/oh_preferences.h new file mode 100644 index 000000000..b3ee03cb2 --- /dev/null +++ b/distributeddatamgr/preferences/include/oh_preferences.h @@ -0,0 +1,271 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * 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. + */ + +/** + * @addtogroup PREFERENCES + * @{ + * + * @brief Provides APIs for processing data in the form of key-value (KV) pairs. + * You can use the APIs provided by the Preferences module to query, modify, and persist KV pairs. + * The key is of the string type, and the value can be a number, a string, a boolean value. + * + * @since 13 + */ + +/** + * @file oh_preferences.h + * + * @brief Defines the APIS and enums of Preference. + * + * @kit ArkData + * @library libohpreferences.so + * @syscap SystemCapability.DistributedDataManager.Preferences.Core + * + * @since 13 + */ + +#ifndef OH_PREFERENCES_H +#define OH_PREFERENCES_H + +#include + +#include "oh_preferences_value.h" +#include "oh_preferences_option.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Represents a Preferences instance. + * + * @since 13 + */ +typedef struct OH_Preferences OH_Preferences; + +/** + * @brief Call to return the change in KV pairs. + * + * @param context Pointer to the context of data observer. + * @param pairs Pointer to the KV pairs to be observed. + * @param count Number of KV pairs to be observed. + * @see OH_PreferencesPair. + * @since 13 + */ +typedef void (*OH_PreferencesDataObserver)(void *context, const OH_PreferencesPair *pairs, uint32_t count); + +/** + * @brief Opens a Preferences object. + * + * @param option Pointer to an {@Link OH_PreferencesOption} instance. + * @param errCode Pointer to the status code of the execution. For details, See {@link OH_Preferences_ErrCode}. + * @return Returns an pointer to the Preferences object in {@Link OH_Preferences} if the operation is successful, + * returns nullptr otherwise. + * {@link PREFERENCES_OK} indicates the operation is successful. + * {@link PREFERENCES_ERROR_INVALID_PARAM} indicates invalid args are passed in. + * {@link PREFERENCES_ERROR_NOT_SUPPORTED} indicates the capability is not supported. + * {@link PREFERENCES_ERROR_DELETE_FILE} indicates delete file failed. + * {@link PREFERENCES_ERROR_STORAGE} indicates an storage error. + * {@link PREFERENCES_ERROR_MALLOC} indicates an malloc memory error. + * @see OH_Preferences OH_PreferencesOption. + * @since 13 + */ +OH_Preferences *OH_Preferences_Open(OH_PreferencesOption *option, int *errCode); + +/** + * @brief Closes a Preferences object. + * + * @param preference Pointer to the {@Link OH_Preferences} instance to close. + * @param option Pointer to an {@Link OH_PreferencesOption} instance. + * @return Returns the status code of the execution. For details, see {@Link OH_Preferences_ErrCode}. + * {@link PREFERENCES_OK} indicates the operation is successful. + * {@link PREFERENCES_ERROR_INVALID_PARAM} indicates invalid args are passed in. + * {@link PREFERENCES_ERROR_STORAGE} indicates an storage error. + * {@link PREFERENCES_ERROR_MALLOC} indicates an malloc memory error. + * @see OH_Preferences. + * @since 13 + */ +int OH_Preferences_Close(OH_Preferences *preference); + +/** + * @brief Obtains the integer value in a Preferences object based on the given key. + * + * @param preference Pointer to the target {@Link OH_Preferences} instance. + * @param key Pointer to the key of the value to obtain. + * @param value Pointer to the value obtained. + * @return Returns the status code of the execution. + * {@link PREFERENCES_OK} indicates the operation is successful. + * {@link PREFERENCES_ERROR_INVALID_PARAM} indicates invalid args are passed in. + * {@link PREFERENCES_ERROR_STORAGE} indicates an storage error. + * {@link PREFERENCES_ERROR_MALLOC} indicates an malloc memory error. + * {@link PREFERENCES_ERROR_KEY_NOT_FOUND} indicates the key does not exist. + * @see OH_Preferences. + * @since 13 + */ +int OH_Preferences_GetInt(OH_Preferences *preference, const char *key, int *value); + +/** + * @brief Obtains the Boolean value in a Preferences object based on the given key. + * + * @param preference Pointer to the target {@Link OH_Preferences} instance. + * @param key Pointer to the key of the value to obtain. + * @param value Pointer to the Boolean value obtained. + * @return Returns the status code of the execution. + * {@link PREFERENCES_OK} indicates the operation is successful. + * {@link PREFERENCES_ERROR_INVALID_PARAM} indicates invalid args are passed in. + * {@link PREFERENCES_ERROR_STORAGE} indicates an storage error. + * {@link PREFERENCES_ERROR_MALLOC} indicates an malloc memory error. + * {@link PREFERENCES_ERROR_KEY_NOT_FOUND} indicates the key does not exist. + * @see OH_Preferences. + * @since 13 + */ +int OH_Preferences_GetBool(OH_Preferences *preference, const char *key, bool *value); + +/** + * @brief Obtains the string value in a Preferences object based on the given key. + * + * @param preference Pointer to the target {@Link OH_Preferences} instance. + * @param key Pointer to the key of the value to obtain. + * @param value Double pointer to the value obtained in an char * array. Release {@Link OH_Preferences_FreeString} the + * memory by user when this parameter is no longer required. + * @param valueLen Pointer to the length of the string obtained. + * @return Returns the status code of the execution. + * {@link PREFERENCES_OK} indicates the operation is successful. + * {@link PREFERENCES_ERROR_INVALID_PARAM} indicates invalid args are passed in. + * {@link PREFERENCES_ERROR_STORAGE} indicates an storage error. + * {@link PREFERENCES_ERROR_MALLOC} indicates an malloc memory error. + * {@link PREFERENCES_ERROR_KEY_NOT_FOUND} indicates the key does not exist. + * @see OH_Preferences. + * @since 13 + */ +int OH_Preferences_GetString(OH_Preferences *preference, const char *key, char **value, uint32_t *valueLen); + +/** + * @brief Free a string got by Preferences object. + * + * @param string Point to string need to free. + * @see OH_Preferences. + * @since 13 + */ +void OH_Preferences_FreeString(char *string); + +/** + * @brief Sets an integer in a Preferences object. + * + * @param preference Pointer to the target {@Link OH_Preferences} instance. + * @param key Pointer to the key to set. + * @param value Value to set. + * @return Returns the status code of the execution. + * {@link PREFERENCES_OK} indicates the operation is successful. + * {@link PREFERENCES_ERROR_INVALID_PARAM} indicates invalid args are passed in. + * {@link PREFERENCES_ERROR_STORAGE} indicates an storage error. + * {@link PREFERENCES_ERROR_MALLOC} indicates an malloc memory error. + * @see OH_Preferences. + * @since 13 + */ +int OH_Preferences_SetInt(OH_Preferences *preference, const char *key, int value); + +/** + * @brief Sets a Boolean value in a Preferences object. + * + * @param preference Pointer to the target {@Link OH_Preferences} instance. + * @param key Pointer to the key to set. + * @param value Boolean value to set. + * @return Returns the status code of the execution. + * {@link PREFERENCES_OK} indicates the operation is successful. + * {@link PREFERENCES_ERROR_INVALID_PARAM} indicates invalid args are passed in. + * {@link PREFERENCES_ERROR_STORAGE} indicates an storage error. + * {@link PREFERENCES_ERROR_MALLOC} indicates an malloc memory error. + * @see OH_Preferences. + * @since 13 + */ +int OH_Preferences_SetBool(OH_Preferences *preference, const char *key, bool value); + +/** + * @brief Sets a string in a Preferences object. + * + * @param preference Pointer to the target {@Link OH_Preferences} instance. + * @param key Pointer to the key to set. + * @param value Point to string to set. + * @return Returns the status code of the execution. + * {@link PREFERENCES_OK} indicates the operation is successful. + * {@link PREFERENCES_ERROR_INVALID_PARAM} indicates invalid args are passed in. + * {@link PREFERENCES_ERROR_STORAGE} indicates an storage error. + * {@link PREFERENCES_ERROR_MALLOC} indicates an malloc memory error. + * @see OH_Preferences. + * @since 13 + */ +int OH_Preferences_SetString(OH_Preferences *preference, const char *key, const char *value); + +/** + * @brief Deletes a KV pair from a Preferences object. + * + * @param preference Pointer to the target {@Link OH_Preferences} instance. + * @param key Pointer to the key of the data to delete. + * @return Returns the status code of the execution. + * {@link PREFERENCES_OK} indicates the operation is successful. + * {@link PREFERENCES_ERROR_INVALID_PARAM} indicates invalid args are passed in. + * {@link PREFERENCES_ERROR_STORAGE} indicates an storage error. + * {@link PREFERENCES_ERROR_MALLOC} indicates an malloc memory error. + * @see OH_Preferences. + * @since 13 + */ +int OH_Preferences_Delete(OH_Preferences *preference, const char *key); + +/** + * @brief Registers a data observer for a Preferences object. + * + * @param preference Pointer to the target {@Link OH_Preferences} instance. + * @param context Pointer to the context of the data observer. + * @param observer Pointer to the {@Link OH_PreferencesDataObserver} to register. + * @param keys Pointer to the keys to observe. + * @param keyCount Number of keys to observe. + * @return Returns the status code of the execution. + * {@link PREFERENCES_OK} indicates the operation is successful. + * {@link PREFERENCES_ERROR_INVALID_PARAM} indicates invalid args are passed in. + * {@link PREFERENCES_ERROR_STORAGE} indicates an storage error. + * {@link PREFERENCES_ERROR_MALLOC} indicates an malloc memory error. + * {@link PREFERENCES_ERROR_GET_DATAOBSMGRCLIENT} indicates get dataObsMgrClient error. + * @see OH_Preferences OH_PreferencesDataObserver. + * @since 13 + */ +int OH_Preferences_RegisterDataObserver(OH_Preferences *preference, void *context, + const OH_PreferencesDataObserver *observer, const char *keys[], uint32_t keyCount); + +/** + * @brief Unregisters a data observer for a Preferences object. + * + * @param preference Pointer to the target {@Link OH_Preferences} instance. + * @param context Pointer to the context of the data observer. + * @param observer Pointer to the {@Link OH_PreferencesDataObserver} to unregister. + * @param keys Pointer to the keys observed. If this parameter is null, this API unregisters the listening for all keys. + * @param keyCount Number of the keys. + * @return Returns the status code of the execution. + * {@link PREFERENCES_OK} indicates the operation is successful. + * {@link PREFERENCES_ERROR_INVALID_PARAM} indicates invalid args are passed in. + * {@link PREFERENCES_ERROR_STORAGE} indicates an storage error. + * {@link PREFERENCES_ERROR_MALLOC} indicates an malloc memory error. + * @see OH_Preferences OH_PreferencesDataObserver. + * @since 13 + */ +int OH_Preferences_UnregisterDataObserver(OH_Preferences *preference, void *context, + const OH_PreferencesDataObserver *observer, const char *keys[], uint32_t keyCount); + +#ifdef __cplusplus +}; +#endif + +/** @} */ +#endif // OH_PREFERENCES_H \ No newline at end of file diff --git a/distributeddatamgr/preferences/include/oh_preferences_err_code.h b/distributeddatamgr/preferences/include/oh_preferences_err_code.h new file mode 100644 index 000000000..5b77219b9 --- /dev/null +++ b/distributeddatamgr/preferences/include/oh_preferences_err_code.h @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * 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. + */ + +/** + * @addtogroup PREFERENCES + * @{ + * + * @brief Provides APIs for processing data in the form of key-value (KV) pairs. + * You can use the APIs provided by the Preferences module to query, modify, and persist KV pairs. + * The key is of the string type, and the value can be a number, a string, a boolean value. + * + * @since 13 + */ + +/** + * @file preferences_err_code.h + * + * @brief Defines the status codes used in the Preferences module. + * + * @kit ArkData + * @library libohpreferences.so + * @syscap SystemCapability.DistributedDataManager.Preferences.Core + * + * @since 13 + */ + + +#ifndef OH_PREFERENCES_ERR_CODE_H +#define OH_PREFERENCES_ERR_CODE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Enumerates the status codes. + * + * @since 13 + */ +typedef enum OH_Preferences_ErrCode { + /** @error Operation successful. */ + PREFERENCES_OK = 0, + /* @error Invalid args. */ + PREFERENCES_ERROR_INVALID_PARAM = 401, + /* @error Capability not supported. */ + PREFERENCES_ERROR_NOT_SUPPORTED = 801, + /* @error Base error code. */ + PREFERENCES_ERROR_BASE = 15500000, + /* @error Failed to delete a file. */ + PREFERENCES_ERROR_DELETE_FILE = 15500010, + /* @error Storage error. */ + PREFERENCES_ERROR_STORAGE = 15500011, + /* @error Failed to malloc memory. */ + PREFERENCES_ERROR_MALLOC = 15500012, + /* @error Key not found error. */ + PREFERENCES_ERROR_KEY_NOT_FOUND = 15500013, + /* @error Failed to get DataObsMgrClient. */ + PREFERENCES_ERROR_GET_DATAOBSMGRCLIENT = 15500019, +} OH_Preferences_ErrCode; + +#ifdef __cplusplus +}; +#endif + +#endif // OH_PREFERENCES_ERR_CODE_H diff --git a/distributeddatamgr/preferences/include/oh_preferences_option.h b/distributeddatamgr/preferences/include/oh_preferences_option.h new file mode 100644 index 000000000..4b2d1d953 --- /dev/null +++ b/distributeddatamgr/preferences/include/oh_preferences_option.h @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * 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. + */ + +/** + * @addtogroup PREFERENCES + * @{ + * + * @brief Provides APIs for processing data in the form of key-value (KV) pairs. + * You can use the APIs provided by the Preferences module to query, modify, and persist KV pairs. + * The key is of the string type, and the value can be a number, a string, a boolean value. + * + * @since 13 + */ + +/** + * @file oh_preferences_option.h + * + * @brief Defines the APIs and enums related to preferences option. + * + * @kit ArkData + * @library libohpreferences.so + * @syscap SystemCapability.DistributedDataManager.Preferences.Core + * + * @since 13 + */ + +#ifndef OH_PREFERENCES_OPTION_H +#define OH_PREFERENCES_OPTION_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Represents an OH_PreferencesOption instance. + * + * @since 13 + */ +typedef struct OH_PreferencesOption OH_PreferencesOption; + +/** + * @brief Creates an {@Link OH_PreferencesOption} instance. + * + * @return Returns a pointer to the {@Link OH_PreferencesOption} instance created if the operation is successful; + * returns nullptr otherwise while malloc memory failed. + * @see OH_PreferencesOption. + * @since 13 + */ +OH_PreferencesOption *OH_PreferencesOption_Create(void); + +/** + * @brief Sets the file path in an {@Link OH_PreferencesOption} instance. + * + * @param option Pointer to the target {@Link OH_PreferencesOption} instance. + * @param filePath Pointer to the file path to set. + * @return Returns the status code of the execution. + * {@link PREFERENCES_OK} success. + * {@link PREFERENCES_ERROR_INVALID_PARAM} indicates invalid args are passed in. + * @see OH_PreferencesOption. + * @since 13 + */ +int OH_PreferencesOption_SetFilePath(OH_PreferencesOption *option, const char *filePath); + +/** + * @brief Sets the bundle name in an {@Link OH_PreferencesOption} instance. + * + * @param option Pointer to the target {@Link OH_PreferencesOption} instance. + * @param bundleName Pointer to the bundle name to set. + * @return Returns the status code of the execution. + * {@link PREFERENCES_OK} success. + * {@link PREFERENCES_ERROR_INVALID_PARAM} indicates invalid args are passed in. + * @see OH_PreferencesOption. + * @since 13 + */ +int OH_PreferencesOption_SetBundleName(OH_PreferencesOption *option, const char *bundleName); + +/** + * @brief Sets the data group ID in an {@Link OH_PreferencesOption} instance. + * + * @param option Represents a pointer to an {@link OH_PreferencesOption} instance. + * @param dataGroupId Represents preferences data group id param. + * @return Returns the status code of the execution. + * {@link PREFERENCES_OK} success. + * {@link PREFERENCES_ERROR_INVALID_PARAM} indicates invalid args are passed in. + * @see OH_PreferencesOption. + * @since 13 + */ +int OH_PreferencesOption_SetDataGroupId(OH_PreferencesOption *option, const char *dataGroupId); + +/** + * @brief Destroys an {@Link OH_PreferencesOption} instance. + * + * @param option Pointer to the {@Link OH_PreferencesOption} instance to destroy. + * @return Returns the status code of the execution. + * {@link PREFERENCES_OK} indicates the operation is successful. + * {@link PREFERENCES_ERROR_INVALID_PARAM} indicates invalid args are passed in. + * @see OH_PreferencesOption. + * @since 13 + */ +int OH_PreferencesOption_Destroy(OH_PreferencesOption *option); +#ifdef __cplusplus +}; +#endif +#endif // OH_PREFERENCES_OPTION_H \ No newline at end of file diff --git a/distributeddatamgr/preferences/include/oh_preferences_value.h b/distributeddatamgr/preferences/include/oh_preferences_value.h new file mode 100644 index 000000000..5fd358f32 --- /dev/null +++ b/distributeddatamgr/preferences/include/oh_preferences_value.h @@ -0,0 +1,174 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * 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. + */ + +/** + * @addtogroup PREFERENCES + * @{ + * + * @brief Provides APIs for processing data in the form of key-value (KV) pairs. + * You can use the APIs provided by the Preferences module to query, modify, and persist KV pairs. + * The key is of the string type, and the value can be a number, a string, a boolean value. + * + * @since 13 + */ + +/** + * @file oh_preferences_value.h + * + * @brief Defines the APIs and enums related to preferences values. + * + * @kit ArkData + * @library libohpreferences.so + * @syscap SystemCapability.DistributedDataManager.Preferences.Core + * + * @since 13 + */ + +#ifndef OH_PREFERENCES_VALUE_H +#define OH_PREFERENCES_VALUE_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Enumerates the Preference value types. + * + * @since 13 + */ +typedef enum Preference_ValueType { + /** + * @brief Null. + */ + TYPE_NULL = 0, + /** + * @brief Int. + */ + TYPE_INT, + /** + * @brief boolean. + */ + TYPE_BOOL, + /** + * @brief String. + */ + TYPE_STRING, + /** + * @brief end butt. + */ + TYPE_BUTT +} Preference_ValueType; + +/** + * @brief Represents a KV pair in a Preferences instance. + * + * @since 13 + */ +typedef struct OH_PreferencesPair OH_PreferencesPair; + +/** + * @brief Represents the value in a KV pair of a Preferences instance. + * + * @since 13 + */ +typedef struct OH_PreferencesValue OH_PreferencesValue; + +/** + * @brief Obtains a key from an {@Link OH_PreferencesPair} instance. + * + * @param pairs Pointer to the target {@Link OH_PreferencesPair} instance. + * @param index Represents a target index of the pairs + * @return Returns preferences pointer to the key that when input parameters valid, + * return nullptr otherwise while invalid args are passed in. + * @see OH_PreferencesPair. + * @since 13 + */ +const char *OH_PreferencesPair_GetKey(const OH_PreferencesPair *pairs, uint32_t index); + +/** + * @brief Obtains a value from an {@Link OH_PreferencesPair} instance. + * + * @param pairs Pointer to the target {@Link OH_PreferencesPair} instance. + * @param index Index of the value to obtain. + * @return Returns a pointer to the {@Link OH_PreferencesValue} obtained if the operation is successful, + * returns nullptr otherwise while invalid args are passed in. + * @see OH_PreferencesValue. + * @since 13 + */ +const OH_PreferencesValue *OH_PreferencesPair_GetPreferencesValue(const OH_PreferencesPair *pairs, uint32_t index); + +/** + * @brief Obtains the type of a preferences value. + * + * @param object Pointer to the target {@Link OH_PreferencesValue} instance. + * @return Returns the value type obtained. + * {@link TYPE_NULL} indicates invalid args are passed in. + * @see OH_PreferencesValue. + * @since 13 + */ +Preference_ValueType OH_PreferencesValue_GetValueType(const OH_PreferencesValue *object); + +/** + * @brief Obtains the int value of an {@Link OH_PreferencesValue} instance. + * + * @param object Pointer to the target {@Link OH_PreferencesValue} instance. + * @param value Pointer to the value obtained. + * @return Returns the status code of the execution. + * {@link PREFERENCES_OK} indicates the operation is successful. + * {@link PREFERENCES_ERROR_INVALID_PARAM} indicates invalid args are passed in. + * {@link PREFERENCES_ERROR_STORAGE} indicates an storage error. + * {@link PREFERENCES_ERROR_MALLOC} indicates an malloc memory error. + * @see OH_PreferencesValue. + * @since 13 + */ +int OH_PreferencesValue_GetInt(const OH_PreferencesValue *object, int *value); + +/** + * @brief Obtains the Boolean value of an {@Link OH_PreferencesValue} instance. + * + * @param object Pointer to the target {@Link OH_PreferencesValue} instance. + * @param value Pointer to the Boolean value obtained. + * @return Returns the status code of the execution. + * {@link PREFERENCES_OK} indicates the operation is successful. + * {@link PREFERENCES_ERROR_INVALID_PARAM} indicates invalid args are passed in. + * {@link PREFERENCES_ERROR_STORAGE} indicates an storage error. + * {@link PREFERENCES_ERROR_MALLOC} indicates an malloc memory error. + * @see OH_PreferencesValue. + * @since 13 + */ +int OH_PreferencesValue_GetBool(const OH_PreferencesValue *object, bool *value); + +/** + * @brief Obtains the string value of an {@Link OH_PreferencesValue} instance. + * + * @param object Pointer to target {@Link OH_PreferencesValue} instance. + * @param value Double pointer to the value obtained in an char * array. Release {@Link OH_Preferences_FreeString} the + * memory by user when this parameter is no longer required. + * @param valueLen Pointer to the string length. + * @return Returns the status code of the execution. + * {@link PREFERENCES_OK} indicates the operation is successful. + * {@link PREFERENCES_ERROR_INVALID_PARAM} indicates invalid args are passed in. + * {@link PREFERENCES_ERROR_STORAGE} indicates an storage error. + * {@link PREFERENCES_ERROR_MALLOC} indicates an malloc memory error. + * @see OH_PreferencesValue. + * @since 13 + */ +int OH_PreferencesValue_GetString(const OH_PreferencesValue *object, char **value, uint32_t *valueLen); +#ifdef __cplusplus +}; +#endif +#endif // OH_PREFERENCES_VALUE_H \ No newline at end of file diff --git a/distributeddatamgr/preferences/libpreferences.ndk.json b/distributeddatamgr/preferences/libpreferences.ndk.json new file mode 100644 index 000000000..fa4a164ed --- /dev/null +++ b/distributeddatamgr/preferences/libpreferences.ndk.json @@ -0,0 +1,94 @@ +[ + { + "first_introduced": "13", + "name": "OH_Preferences_Open" + }, + { + "first_introduced": "13", + "name": "OH_Preferences_Close" + }, + { + "first_introduced": "13", + "name": "OH_Preferences_GetInt" + }, + { + "first_introduced": "13", + "name": "OH_Preferences_GetBool" + }, + { + "first_introduced": "13", + "name": "OH_Preferences_GetString" + }, + { + "first_introduced": "13", + "name": "OH_Preferences_FreeString" + }, + { + "first_introduced": "13", + "name": "OH_Preferences_SetInt" + }, + { + "first_introduced": "13", + "name": "OH_Preferences_SetBool" + }, + { + "first_introduced": "13", + "name": "OH_Preferences_SetString" + }, + { + "first_introduced": "13", + "name": "OH_Preferences_Delete" + }, + { + "first_introduced": "13", + "name": "OH_Preferences_RegisterDataObserver" + }, + { + "first_introduced": "13", + "name": "OH_Preferences_UnregisterDataObserver" + }, + { + "first_introduced": "13", + "name": "OH_PreferencesOption_Create" + }, + { + "first_introduced": "13", + "name": "OH_PreferencesOption_SetFilePath" + }, + { + "first_introduced": "13", + "name": "OH_PreferencesOption_SetBundleName" + }, + { + "first_introduced": "13", + "name": "OH_PreferencesOption_SetDataGroupId" + }, + { + "first_introduced": "13", + "name": "OH_PreferencesOption_Destroy" + }, + { + "first_introduced": "13", + "name": "OH_PreferencesPair_GetKey" + }, + { + "first_introduced": "13", + "name": "OH_PreferencesPair_GetPreferencesValue" + }, + { + "first_introduced": "13", + "name": "OH_PreferencesValue_GetValueType" + }, + { + "first_introduced": "13", + "name": "OH_PreferencesValue_GetInt" + }, + { + "first_introduced": "13", + "name": "OH_PreferencesValue_GetBool" + }, + { + "first_introduced": "13", + "name": "OH_PreferencesValue_GetString" + } +] \ No newline at end of file -- Gitee From 9db6669da1f4666f0bd8c1a4e65bcc362142eb41 Mon Sep 17 00:00:00 2001 From: zwtmichael Date: Mon, 30 Sep 2024 16:02:27 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9build.gn=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=91=BD=E5=90=8D=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zwtmichael --- distributeddatamgr/preferences/BUILD.gn | 10 +++++----- .../preferences/include/oh_preferences.h | 3 ++- .../preferences/include/oh_preferences_option.h | 2 +- .../preferences/include/oh_preferences_value.h | 3 ++- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/distributeddatamgr/preferences/BUILD.gn b/distributeddatamgr/preferences/BUILD.gn index 90671c646..77c03331c 100644 --- a/distributeddatamgr/preferences/BUILD.gn +++ b/distributeddatamgr/preferences/BUILD.gn @@ -21,7 +21,7 @@ ohos_ndk_headers("preferences_ndk_header") { "./include/oh_preferences.h", "./include/oh_preferences_err_code.h", "./include/oh_preferences_option.h", - "./include/oh_preferences_values.h", + "./include/oh_preferences_value.h", ] } @@ -32,9 +32,9 @@ ohos_ndk_library("libohpreferences") { ndk_description_file = "./libpreferences.ndk.json" min_compact_version = "13" system_capability_headers = [ - "$ndk_headers_out_dir/database/preferences/oh_preferences.h", - "$ndk_headers_out_dir/database/preferences/oh_preferences_err_code.h", - "$ndk_headers_out_dir/database/preferences/oh_preferences_values.h", - "$ndk_headers_out_dir/database/preferences/oh_preferences_option.h", + "database/preferences/oh_preferences.h", + "database/preferences/oh_preferences_err_code.h", + "database/preferences/oh_preferences_value.h", + "database/preferences/oh_preferences_option.h", ] } diff --git a/distributeddatamgr/preferences/include/oh_preferences.h b/distributeddatamgr/preferences/include/oh_preferences.h index b3ee03cb2..f7531f81c 100644 --- a/distributeddatamgr/preferences/include/oh_preferences.h +++ b/distributeddatamgr/preferences/include/oh_preferences.h @@ -39,7 +39,8 @@ #ifndef OH_PREFERENCES_H #define OH_PREFERENCES_H -#include +#include +#include #include "oh_preferences_value.h" #include "oh_preferences_option.h" diff --git a/distributeddatamgr/preferences/include/oh_preferences_option.h b/distributeddatamgr/preferences/include/oh_preferences_option.h index 4b2d1d953..2c616d055 100644 --- a/distributeddatamgr/preferences/include/oh_preferences_option.h +++ b/distributeddatamgr/preferences/include/oh_preferences_option.h @@ -39,7 +39,7 @@ #ifndef OH_PREFERENCES_OPTION_H #define OH_PREFERENCES_OPTION_H -#include +#include #ifdef __cplusplus extern "C" { diff --git a/distributeddatamgr/preferences/include/oh_preferences_value.h b/distributeddatamgr/preferences/include/oh_preferences_value.h index 5fd358f32..c1a0e3579 100644 --- a/distributeddatamgr/preferences/include/oh_preferences_value.h +++ b/distributeddatamgr/preferences/include/oh_preferences_value.h @@ -39,7 +39,8 @@ #ifndef OH_PREFERENCES_VALUE_H #define OH_PREFERENCES_VALUE_H -#include +#include +#include #ifdef __cplusplus extern "C" { -- Gitee From 9d531b7cdd87ba2feedc58805721b6b987055b63 Mon Sep 17 00:00:00 2001 From: zwtmichael Date: Mon, 30 Sep 2024 16:10:30 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E6=B3=A8=E5=86=8C=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zwtmichael --- distributeddatamgr/preferences/include/oh_preferences.h | 8 ++++---- .../preferences/include/oh_preferences_option.h | 4 ++-- distributeddatamgr/preferences/libpreferences.ndk.json | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/distributeddatamgr/preferences/include/oh_preferences.h b/distributeddatamgr/preferences/include/oh_preferences.h index f7531f81c..c3a56fd95 100644 --- a/distributeddatamgr/preferences/include/oh_preferences.h +++ b/distributeddatamgr/preferences/include/oh_preferences.h @@ -230,7 +230,7 @@ int OH_Preferences_Delete(OH_Preferences *preference, const char *key); * * @param preference Pointer to the target {@Link OH_Preferences} instance. * @param context Pointer to the context of the data observer. - * @param observer Pointer to the {@Link OH_PreferencesDataObserver} to register. + * @param observer the {@Link OH_PreferencesDataObserver} to register. * @param keys Pointer to the keys to observe. * @param keyCount Number of keys to observe. * @return Returns the status code of the execution. @@ -243,14 +243,14 @@ int OH_Preferences_Delete(OH_Preferences *preference, const char *key); * @since 13 */ int OH_Preferences_RegisterDataObserver(OH_Preferences *preference, void *context, - const OH_PreferencesDataObserver *observer, const char *keys[], uint32_t keyCount); + OH_PreferencesDataObserver observer, const char *keys[], uint32_t keyCount); /** * @brief Unregisters a data observer for a Preferences object. * * @param preference Pointer to the target {@Link OH_Preferences} instance. * @param context Pointer to the context of the data observer. - * @param observer Pointer to the {@Link OH_PreferencesDataObserver} to unregister. + * @param observer the {@Link OH_PreferencesDataObserver} to unregister. * @param keys Pointer to the keys observed. If this parameter is null, this API unregisters the listening for all keys. * @param keyCount Number of the keys. * @return Returns the status code of the execution. @@ -262,7 +262,7 @@ int OH_Preferences_RegisterDataObserver(OH_Preferences *preference, void *contex * @since 13 */ int OH_Preferences_UnregisterDataObserver(OH_Preferences *preference, void *context, - const OH_PreferencesDataObserver *observer, const char *keys[], uint32_t keyCount); + OH_PreferencesDataObserver observer, const char *keys[], uint32_t keyCount); #ifdef __cplusplus }; diff --git a/distributeddatamgr/preferences/include/oh_preferences_option.h b/distributeddatamgr/preferences/include/oh_preferences_option.h index 2c616d055..a8a25af9f 100644 --- a/distributeddatamgr/preferences/include/oh_preferences_option.h +++ b/distributeddatamgr/preferences/include/oh_preferences_option.h @@ -66,14 +66,14 @@ OH_PreferencesOption *OH_PreferencesOption_Create(void); * @brief Sets the file path in an {@Link OH_PreferencesOption} instance. * * @param option Pointer to the target {@Link OH_PreferencesOption} instance. - * @param filePath Pointer to the file path to set. + * @param fileName Pointer to the file name to set. * @return Returns the status code of the execution. * {@link PREFERENCES_OK} success. * {@link PREFERENCES_ERROR_INVALID_PARAM} indicates invalid args are passed in. * @see OH_PreferencesOption. * @since 13 */ -int OH_PreferencesOption_SetFilePath(OH_PreferencesOption *option, const char *filePath); +int OH_PreferencesOption_SetFileName(OH_PreferencesOption *option, const char *fileName); /** * @brief Sets the bundle name in an {@Link OH_PreferencesOption} instance. diff --git a/distributeddatamgr/preferences/libpreferences.ndk.json b/distributeddatamgr/preferences/libpreferences.ndk.json index fa4a164ed..9c41566f5 100644 --- a/distributeddatamgr/preferences/libpreferences.ndk.json +++ b/distributeddatamgr/preferences/libpreferences.ndk.json @@ -53,7 +53,7 @@ }, { "first_introduced": "13", - "name": "OH_PreferencesOption_SetFilePath" + "name": "OH_PreferencesOption_SetFileName" }, { "first_introduced": "13", -- Gitee From 7dc3d20d1b14b43c6a40ab0e166372556ee42f42 Mon Sep 17 00:00:00 2001 From: zwtmichael Date: Mon, 30 Sep 2024 16:12:09 +0800 Subject: [PATCH 4/4] Preferences: change enum name Signed-off-by: zwtmichael --- .../preferences/include/oh_preferences_value.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/distributeddatamgr/preferences/include/oh_preferences_value.h b/distributeddatamgr/preferences/include/oh_preferences_value.h index c1a0e3579..945e28fc6 100644 --- a/distributeddatamgr/preferences/include/oh_preferences_value.h +++ b/distributeddatamgr/preferences/include/oh_preferences_value.h @@ -55,23 +55,23 @@ typedef enum Preference_ValueType { /** * @brief Null. */ - TYPE_NULL = 0, + PREFERENCE_TYPE_NULL = 0, /** * @brief Int. */ - TYPE_INT, + PREFERENCE_TYPE_INT, /** * @brief boolean. */ - TYPE_BOOL, + PREFERENCE_TYPE_BOOL, /** * @brief String. */ - TYPE_STRING, + PREFERENCE_TYPE_STRING, /** * @brief end butt. */ - TYPE_BUTT + PREFERENCE_TYPE_BUTT } Preference_ValueType; /** @@ -117,7 +117,7 @@ const OH_PreferencesValue *OH_PreferencesPair_GetPreferencesValue(const OH_Prefe * * @param object Pointer to the target {@Link OH_PreferencesValue} instance. * @return Returns the value type obtained. - * {@link TYPE_NULL} indicates invalid args are passed in. + * {@link PREFERENCE_TYPE_NULL} indicates invalid args are passed in. * @see OH_PreferencesValue. * @since 13 */ -- Gitee