/* * 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. */ /** * @file * @kit ArkData */ import { Callback } from './@ohos.base'; import Context from './application/BaseContext'; import collections from '../arkts/@arkts.collections'; import lang from '../arkts/@arkts.lang'; /** * Provides interfaces to obtain and modify preferences data. * * @namespace sendablePreferences * @syscap SystemCapability.DistributedDataManager.Preferences.Core * @atomicservice * @since 12 * @name sendablePreferences */ declare namespace sendablePreferences { /** * Maximum length of a key. * * @syscap SystemCapability.DistributedDataManager.Preferences.Core * @atomicservice * @since 12 */ const MAX_KEY_LENGTH: number; /** * Maximum length of a value. * * @syscap SystemCapability.DistributedDataManager.Preferences.Core * @atomicservice * @since 12 */ const MAX_VALUE_LENGTH: number; /** * Defines the configuration of a preferences file. * * @interface Options * @syscap SystemCapability.DistributedDataManager.Preferences.Core * @atomicservice * @since 12 */ interface Options { /** * Name of the preferences file. * * @type { string } * @syscap SystemCapability.DistributedDataManager.Preferences.Core * @atomicservice * @since 12 */ name: string; /** * Application group ID. * * @type { ?(string | null) } * @syscap SystemCapability.DistributedDataManager.Preferences.Core * @StageModelOnly * @atomicservice * @since 12 */ dataGroupId?: string | null; } /** * Obtains a {@link Preferences} instance matching the specified preferences file name. *
The {@link references} instance loads all data of the preferences file and
* resides in the cache. You can use removePreferencesFromCache to remove the instance from the cache.
*
* @param { Context } context - Indicates the context of application or capability.
* @param { Options } options - Indicates information about the preferences file. For details, see {@link Options}.
* @returns { Promise The {@link references} instance loads all data of the preferences file and
* resides in the cache. You can use removePreferencesFromCache to remove the instance from the cache.
*
* @param { Context } context - Indicates the context of application or capability.
* @param { Options } options - Indicates information about the preferences file. For details, see {@link Options}.
* @returns { Preferences } return the {@link Preferences}.
* @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;
* 2. Incorrect parameter types;
* 3. Parameter verification failed.
* @throws { BusinessError } 801 - Capability not supported.
* @throws { BusinessError } 15500000 - Inner error.
* @throws { BusinessError } 15501001 - The operations is supported in stage mode only.
* @throws { BusinessError } 15501002 - Invalid dataGroupId.
* @syscap SystemCapability.DistributedDataManager.Preferences.Core
* @atomicservice
* @since 12
*/
function getPreferencesSync(context: Context, options: Options): Preferences;
/**
* Deletes a {@link Preferences} instance matching the specified preferences file name
* from the cache (which is equivalent to calling removePreferencesFromCache) and deletes
* the preferences file.
* When deleting a {@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 { Options } options - Indicates information about the preferences file. For details, see {@link Options}.
* @returns { Promise When removing a {@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 { Options } options - Indicates information about the preferences file. For details, see {@link Options}.
* @returns { Promise When removing a {@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 { Options } options - Indicates information about the preferences file. For details, see {@link Options}.
* @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;
* 2. Incorrect parameter types;
* 3. Parameter verification failed.
* @throws { BusinessError } 801 - Capability not supported.
* @throws { BusinessError } 15500000 - Inner error.
* @throws { BusinessError } 15501001 - The operations is supported in stage mode only.
* @throws { BusinessError } 15501002 - Invalid dataGroupId.
* @syscap SystemCapability.DistributedDataManager.Preferences.Core
* @atomicservice
* @since 12
*/
function removePreferencesFromCacheSync(context: Context, options: Options): void;
/**
* 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 cache.
* You can use getPreferences to obtain the {@link Preferences} instance matching
* the file that stores preferences data, and use removePreferencesFromCache
* to remove the {@link Preferences} instance from the cache.
*
* @interface Preferences
* @syscap SystemCapability.DistributedDataManager.Preferences.Core
* @atomicservice
* @since 12
*/
interface Preferences extends lang.ISendable {
/**
* Obtains the value of a preferences instance.
* If the value is {@code null} or not in the lang.ISendable format, the default value is returned.
*
* @param { string } key - Indicates the key of the preferences. It cannot be {@code null} or empty.
* MAX_KEY_LENGTH.
* @param { lang.ISendable } defValue - Indicates the default value to return.
* @returns { Promise If the value is {@code null} or not in the lang.ISendable format, the default value is returned.
*
* @param { string } key - Indicates the key of the preferences. It cannot be {@code null} or empty.
* MAX_KEY_LENGTH.
* @param { lang.ISendable } defValue - Indicates the default value to return.
* @returns { lang.ISendable } If a value matching the specified key is found, the value is returned. Otherwise,
* the default value is returned.
* @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;
* 2. Incorrect parameter types;
* 3. Parameter verification failed.
* @throws { BusinessError } 15500000 - Inner error.
* @syscap SystemCapability.DistributedDataManager.Preferences.Core
* @atomicservice
* @since 12
*/
getSync(key: string, defValue: lang.ISendable): lang.ISendable;
/**
* Obtains all the keys and values of a preferences instance in an object.
*
* @returns { Promise You can call the {@link #flush} method to save the {@link Preferences} object to the file.
*
* @param { string } key - Indicates the key of the preferences to set. It cannot be {@code null} or empty.
* MAX_KEY_LENGTH.
* @param { lang.ISendable } value - Indicates the value of the preferences.
* MAX_VALUE_LENGTH.
* @returns { Promise You can call the {@link #flush} method to save the {@link Preferences} object to the file.
*
* @param { string } key - Indicates the key of the preferences to set. It cannot be {@code null} or empty.
* MAX_KEY_LENGTH.
* @param { lang.ISendable } value - Indicates the value of the preferences.
* MAX_VALUE_LENGTH.
* @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;
* 2. Incorrect parameter types;
* 3. Parameter verification failed.
* @throws { BusinessError } 15500000 - Inner error.
* @syscap SystemCapability.DistributedDataManager.Preferences.Core
* @atomicservice
* @since 12
*/
putSync(key: string, value: lang.ISendable): void;
/**
* Deletes the preferences with a specified key from the {@link Preferences} object.
* You can call the {@link #flush} method to save the {@link Preferences} object to the file.
*
* @param { string } key - Indicates the key of the preferences to delete. It cannot be {@code null} or empty.
* MAX_KEY_LENGTH.
* @returns { Promise You can call the {@link #flush} method to save the {@link Preferences} object to the file.
*
* @param { string } key - Indicates the key of the preferences to delete. It cannot be {@code null} or empty.
* MAX_KEY_LENGTH.
* @throws { BusinessError } 401 - Parameter error. Possible causes: 1. Mandatory parameters are left unspecified;
* 2. Incorrect parameter types;
* 3. Parameter verification failed.
* @throws { BusinessError } 15500000 - Inner error.
* @syscap SystemCapability.DistributedDataManager.Preferences.Core
* @atomicservice
* @since 12
*/
deleteSync(key: string): void;
/**
* Clears all preferences from the {@link Preferences} object.
* You can call the {@link #flush} method to save the {@link Preferences} object to the file.
*
* @returns { Promise You can call the {@link #flush} method to save the {@link Preferences} object to the file.
*
* @throws { BusinessError } 15500000 - Inner error.
* @syscap SystemCapability.DistributedDataManager.Preferences.Core
* @atomicservice
* @since 12
*/
clearSync(): void;
/**
* Flushes the {@link Preferences} object to the file.
*
* @returns { Promise