diff --git a/features/brightnesscomponent/src/main/ets/default/brightnessManager.ts b/features/brightnesscomponent/src/main/ets/default/brightnessManager.ts index ef66d3e8c0c2a3fb7f0e49649ae82005a503df2c..ae6bbed52234b4dc4754c323698721abdcc623dd 100644 --- a/features/brightnesscomponent/src/main/ets/default/brightnessManager.ts +++ b/features/brightnesscomponent/src/main/ets/default/brightnessManager.ts @@ -26,6 +26,7 @@ import Constants from "../../../../../../common/src/main/ets/default/Constants"; import createOrGet from '../../../../../../common/src/main/ets/default/SingleInstanceHelper'; const TAG = 'Control-brightnessManager'; +const UPDATE_INTERVAL = 1000; var mBrightnessValue = AppStorage.SetAndLink('BrightnessValue', 100); export class brightnessManager { @@ -34,6 +35,8 @@ export class brightnessManager { context: Context; SLIDER_CHANG_MODE_MOVING = 1; private sliderChangeMode: number; + private retry: number = 0; + private MAX_RETRY: number = 60; constructor() { this.uri = Constants.getUriSync(Constants.KEY_BRIGHTNESS_STATUS); @@ -44,25 +47,33 @@ export class brightnessManager { async init(): Promise { Log.showInfo(TAG, 'init'); - this.createDataShare() + this.createDataShare(); Log.showInfo(TAG, `init helper ${this.helper}`); } public createDataShare() { - if (this.context == undefined || this.context == null) { - Log.showInfo(TAG, `constructor, this.context is null`); + if( this.retry > this.MAX_RETRY) { + Log.showError(TAG, `create context is null`); return; } - Log.showInfo(TAG, `createDataShare, this.context ${this.context}`); - const UPDATE_INTERVAL = 500; + this.retry = this.retry + 1; + const timer = setInterval(() => { + if (this.context == undefined || this.context == null) { + Log.showInfo(TAG, `Exceeded the maxiretry of retries`); + this.context = AbilityManager.getContext(AbilityManager.ABILITY_NAME_CONTROL_PANEL); + this.createDataShare(); + return; + } else { + clearInterval(timer); + } + Log.showInfo(TAG, `createDataShare, this.context ${this.context}`); dataShare.createDataShareHelper(this.context, this.uri) .then((dataHelper) => { Log.showInfo(TAG, `createDataShareHelper success.`); this.helper = dataHelper; this.registerBrightness(); this.getValue(); - clearInterval(timer); }) .catch((err: BusinessError) => { Log.showError(TAG, `createDataShare fail. ${JSON.stringify(err)}`);