/*
* Copyright (c) 2022 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 {AsyncCallback, Callback} from './basic';
import Context from "./application/BaseContext";
/**
* Provides interfaces to obtain and modify preferences data.
*
* @name preferences
* @since 9
* @syscap SystemCapability.DistributedDataManager.Preferences.Core
*
*/
declare namespace preferences {
/**
* Obtains a {@link Preferences} instance matching a specified preferences file name.
*
*
The {@link references} instance loads all data of the preferences file and
* resides in the memory. You can use removePreferencesFromCache to remove the instance from the memory.
*
* @param {Context} context - Indicates the context of application or capability.
* @param {string} name - Indicates the preferences file name.
* @param {AsyncCallback} callback - the {@link Preferences} instance matching the specified
* preferences file name.
* @throws {BusinessError} 401 - if the parameter type is incorrect.
* @since 9
*/
function getPreferences(context: Context, name: string, callback: AsyncCallback): void;
/**
* Obtains a {@link Preferences} instance matching a specified preferences file name.
*
*
The {@link references} instance loads all data of the preferences file and
* resides in the memory. You can use removePreferencesFromCache to remove the instance from the memory.
*
* @param {Context} context - Indicates the context of application or capability.
* @param {string} name - Indicates the preferences file name.
* @returns {Promise} the {@link Preferences} instance matching the specified preferences file name.
* @throws {BusinessError} 401 - if the parameter type is incorrect.
* @since 9
*/
function getPreferences(context: Context, name: string): Promise;
/**
* Deletes a {@link Preferences} instance matching a specified preferences file name
* from the cache which is performed by removePreferencesFromCache and deletes the
* preferences file.
*
*
When deleting the {@link Preferences} instance, you must release all references
* of the instance. In addition, do not use the instance to perform data operations. Otherwise, data inconsistency
* will occur.
*
* @param {Context} context - Indicates the context of application or capability.
* @param {string} name - Indicates the preferences file name.
* @param {AsyncCallback} callback - Indicates the callback function.
* @throws {BusinessError} 401 - if the parameter type is incorrect.
* @throws {BusinessError} 15500010 - if failed to delete preferences file.
* @since 9
*/
function deletePreferences(context: Context, name: string, callback: AsyncCallback): void;
/**
* Deletes a {@link Preferences} instance matching a specified preferences file name
* from the cache which is performed by removePreferencesFromCache and deletes the
* preferences file.
*
*
When deleting the {@link Preferences} instance, you must release all references
* of the instance. In addition, do not use the instance to perform data operations. Otherwise, data inconsistency
* will occur.
*
* @param {Context} context - Indicates the context of application or capability.
* @param {string} name - Indicates the preferences file name.
* @returns {Promise} a promise object.
* @throws {BusinessError} 401 - if the parameter type is incorrect.
* @throws {BusinessError} 15500010 - if failed to delete preferences file.
* @since 9
*/
function deletePreferences(context: Context, name: string): Promise;
/**
* Deletes a {@link Preferences} instance matching a specified preferences file name
* from the cache.
*
*
When deleting the {@link Preferences} instance, you must release all references
* of the instance. In addition, do not use the instance to perform data operations. Otherwise, data inconsistency
* will occur.
*
* @param {Context} context - Indicates the context of application or capability.
* @param {string} name - Indicates the preferences file name.
* @param {AsyncCallback} callback - Indicates the callback function.
* @throws {BusinessError} 401 - if the parameter type is incorrect.
* @since 9
*/
function removePreferencesFromCache(context: Context, name: string, callback: AsyncCallback): void;
/**
* Deletes a {@link Preferences} instance matching a specified preferences file name
* from the cache.
*
*
When deleting the {@link Preferences} instance, you must release all references
* of the instance. In addition, do not use the instance to perform data operations. Otherwise, data inconsistency
* will occur.
*
* @param {Context} context - Indicates the context of application or capability.
* @param {string} name - Indicates the preferences file name.
* @returns {Promise} a promise object.
* @throws {BusinessError} 401 - if the parameter type is incorrect.
* @since 9
*/
function removePreferencesFromCache(context: Context, name: string): Promise;
/**
* Provides interfaces to obtain and modify preferences data.
*
*
The preferences data is stored in a file, which matches only one {@link Preferences} instance in the memory.
* You can use getPreferences to obtain the {@link Preferences} instance matching
* the file that stores preferences data, and use movePreferencesFromCache
* to remove the {@link Preferences} instance from the memory.
*
* @syscap SystemCapability.DistributedDataManager.Preferences.Core
*
* @since 9
*/
interface Preferences {
/**
* Obtains the value of a preferences in the int format.
*
*
If the value is {@code null} or not in the int format, the default value is returned.
*
* @param {string} key - Indicates the key of the preferences. It cannot be {@code null} or empty.
* @param {ValueType} defValue - Indicates the default value to return.
* @param {AsyncCallback} callback - the value matching the specified key if it is found;
* returns the default value otherwise.
* @throws {BusinessError} 401 - if the parameter type is incorrect.
* @since 9
*/
get(key: string, defValue: ValueType, callback: AsyncCallback): void;
/**
* Obtains the value of a preferences in the int format.
*
*
If the value is {@code null} or not in the int format, the default value is returned.
*
* @param {string} key - Indicates the key of the preferences. It cannot be {@code null} or empty.
* @param {ValueType} defValue - Indicates the default value to return.
* @returns {Promise} the value matching the specified key if it is found;
* returns the default value otherwise.
* @throws {BusinessError} 401 - if the parameter type is incorrect.
* @since 9
*/
get(key: string, defValue: ValueType): Promise;
/**
* Obtains all the keys and values of a preferences in an object.
*
* @param {AsyncCallback