1/* 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import Log from '../../../../../../../../common/src/main/ets/default/Log'; 17import createOrGet from '../../../../../../../../common/src/main/ets/default/SingleInstanceHelper'; 18import SwitchUserManager, { UserInfo } from '../../../../../../../../common/src/main/ets/default/SwitchUserManager'; 19import CheckEmptyUtils from '../../../../../../../../common/src/main/ets/default/CheckEmptyUtils'; 20import SettingsUtil from '../../../../../../../../common/src/main/ets/default/SettingsUtil'; 21import AbilityManager from '../../../../../../../../common/src/main/ets/default/abilitymanager/abilityManager'; 22import PluginDataSourceAdapter from '../../../../../../../../common/src/main/ets/plugindatasource/PluginDataSourceAdapter'; 23import { ItemComponentData, FilterData 24} from '../../../../../../../../common/src/main/ets/plugindatasource/common/Constants'; 25import Constants, { ControlComponentData, ControlCenterConfig } from '../common/Constants'; 26 27export interface ControlCenterListener { 28 setComplexToggleLayout(layout: string[]): void; 29 30 setSimpleToggleLayout(layout: string[]): void; 31 32 setItemData(id: string, itemData: ControlComponentData): void; 33} 34 35const TAG = 'ControlCenterModel'; 36const SETTINGS_CONTROL_SIMPLE_TOGGLE_LAYOUT = 'settings.control.simpleToggleLayout'; 37const CONTROLCENTER_SOURCE_CONFIG = { 38 action: 'com.ohos.systemui.action.TOGGLE', 39 filterDatas: new Array<FilterData>(), 40 loaderConfig: { 41 MetaSource: { 42 action: 'com.ohos.systemui.action.TOGGLE', 43 permission: null, 44 }, 45 PluginSourceLoader: { 46 action: 'com.ohos.systemui.action.TOGGLE', 47 permission: null, 48 }, 49 }, 50}; 51 52function parseData(itemData: ItemComponentData): ControlComponentData { 53 let { toggleType, ...other } = itemData.actionData?.extra; 54 return { 55 ...itemData, 56 toggleType: toggleType ?? Constants.TOGGLE_TYPE_SIMPLE, 57 actionData: { ...itemData.actionData, extra: undefined }, 58 }; 59} 60 61export class ControlCenterService { 62 mIsStart = false; 63 mAdapter: PluginDataSourceAdapter; 64 mListener: ControlCenterListener | undefined; 65 mConfig: ControlCenterConfig; 66 mComplexToggleLayoutTemplate: string[]; 67 mSimpleToggleLayoutTemplate: string[]; 68 mAllComplexToggles: string[] = []; 69 mAllSimpleToggles: string[] = []; 70 mComplexToggleLayout: string[]; 71 mSimpleToggleLayout: string[]; 72 73 constructor() { 74 Log.showDebug(TAG, 'constructor'); 75 } 76 77 startService(config: ControlCenterConfig, moduleName: string): void { 78 if (this.mIsStart) { 79 return; 80 } 81 Log.showInfo(TAG, 'start ControlCenterService.'); 82 this.mIsStart = true; 83 84 this.parseConfig(config); 85 86 SwitchUserManager.getInstance().registerListener(this); 87 CONTROLCENTER_SOURCE_CONFIG.filterDatas = config.MetaToggles; 88 this.mAdapter = new PluginDataSourceAdapter(TAG, AbilityManager.getContext(AbilityManager.ABILITY_NAME_CONTROL_PANEL), this, moduleName); 89 this.mAdapter.setWant(globalThis[Constants.PLUGIN_COMPONENT_OWNER_WANT_KEY]); 90 this.mAdapter.initDataSource(CONTROLCENTER_SOURCE_CONFIG); 91 } 92 93 stopService(): void { 94 if (!this.mIsStart) { 95 return; 96 } 97 Log.showInfo(TAG, 'stop ControlCenterService.'); 98 this.mIsStart = false; 99 this.mAdapter.clearAll(); 100 } 101 102 parseConfig(config: ControlCenterConfig): void { 103 Log.showDebug(TAG, `parseConfig, config: ${JSON.stringify(config)}`); 104 this.mConfig = config; 105 106 this.mComplexToggleLayoutTemplate = []; 107 config.ComplexToggleLayout.forEach((name: string) => { 108 this.mComplexToggleLayoutTemplate.push(name); 109 }); 110 this.loadSimpleToggleLayoutTemplate(); 111 Log.showDebug(TAG, 112 `parseConfig, ComplexToggleLayoutTemplate: ${JSON.stringify(this.mComplexToggleLayoutTemplate)}`); 113 Log.showDebug(TAG, 114 `parseConfig, SimpleToggleLayoutTemplate: ${JSON.stringify(this.mSimpleToggleLayoutTemplate)}`); 115 116 config.LocalToggles.ComplexToggles.forEach((name: string) => { 117 this.mAllComplexToggles.push(name); 118 }); 119 Log.showDebug(TAG, `parseConfig, mAllComplexToggles: ${JSON.stringify(this.mAllComplexToggles)}`); 120 this.calcComplexToggleLayout(); 121 config.LocalToggles.SimpleToggles.forEach((name: string) => { 122 typeof name === 'string' && this.mAllSimpleToggles.push(name); 123 }); 124 Log.showDebug(TAG, `parseConfig, allSimpleToggles: ${JSON.stringify(this.mAllSimpleToggles)}`); 125 this.calcSimpleToggleLayout(); 126 } 127 128 loadSimpleToggleLayoutTemplate(): void{ 129 Log.showDebug(TAG, 'loadSimpleToggleLayoutTemplate'); 130 this.mSimpleToggleLayoutTemplate = []; 131 let simpleToggleLayout = this.getSimpleToggleLayoutFromSettings(); 132 Log.showDebug(TAG, `simpleToggleLayout: ${JSON.stringify(simpleToggleLayout)}`); 133 if (simpleToggleLayout) { 134 this.mSimpleToggleLayoutTemplate = simpleToggleLayout; 135 } else { 136 this.mConfig.DefaultSimpleToggleLayout.forEach((name: string) => { 137 this.mSimpleToggleLayoutTemplate.push(name); 138 }); 139 this.setSimpleToggleLayoutToSettings(this.mSimpleToggleLayoutTemplate); 140 } 141 Log.showDebug(TAG, `loadSimpleToggleLayoutTemplate, mSimpleToggleLayoutTemplate: ${JSON.stringify(this.mSimpleToggleLayoutTemplate)}`); 142 } 143 144 calcComplexToggleLayout(): void { 145 Log.showDebug(TAG, 'calcComplexToggleLayout'); 146 147 let complexToggleLayout: string[] = []; 148 this.mComplexToggleLayoutTemplate.forEach((name) => { 149 if (this.mAllComplexToggles.indexOf(name) >= 0) { 150 complexToggleLayout.push(name); 151 } 152 }); 153 154 Log.showDebug(TAG, `calcComplexToggleLayout, complexToggleLayout: ${JSON.stringify(complexToggleLayout)}`); 155 this.mComplexToggleLayout = complexToggleLayout; 156 this.mListener?.setComplexToggleLayout(complexToggleLayout); 157 } 158 159 calcSimpleToggleLayout(): void { 160 Log.showDebug(TAG, 'calcSimpleToggleLayout'); 161 162 let simpleToggleLayout: string[] = []; 163 this.mSimpleToggleLayoutTemplate.forEach((name) => { 164 if (this.mAllSimpleToggles.indexOf(name) >= 0) { 165 simpleToggleLayout.push(name); 166 } 167 }); 168 169 Log.showDebug(TAG, `calcSimpleToggleLayout, simpleToggleLayout: ${JSON.stringify(simpleToggleLayout)}`); 170 this.mSimpleToggleLayout = simpleToggleLayout; 171 this.mListener?.setSimpleToggleLayout(simpleToggleLayout); 172 } 173 174 userChange(userInfo: UserInfo): void { 175 Log.showInfo(TAG, `userChange userInfo ${userInfo}`); 176 this.mAdapter.loadData(userInfo.userId); 177 } 178 179 registerListener(listener: ControlCenterListener): void { 180 Log.showInfo(TAG, `registerListener, listener: ${listener}`); 181 this.mListener = listener; 182 } 183 184 initFinish(): void { 185 Log.showDebug(TAG, 'initFinish'); 186 SwitchUserManager.getInstance() 187 .getCurrentUserInfo() 188 .then((userInfo) => { 189 this.mAdapter.loadData(userInfo.userId); 190 }).catch((err) => { 191 }); 192 } 193 194 onItemAdd(itemData: ItemComponentData): void { 195 Log.showDebug(TAG, `onItemAdd, itemData: ${JSON.stringify(itemData)}`); 196 let controlData: ControlComponentData = parseData(itemData); 197 let id: string = controlData.id; 198 this.mListener?.setItemData(id, controlData); 199 if (controlData.toggleType == Constants.TOGGLE_TYPE_COMPLEX) { 200 if (this.mAllComplexToggles.indexOf(id) < 0) { 201 this.mAllComplexToggles.push(id); 202 Log.showDebug(TAG, `onItemAdd, mAllComplexToggles: ${JSON.stringify(this.mAllComplexToggles)}`); 203 this.calcComplexToggleLayout(); 204 } 205 } else { 206 if (this.mAllSimpleToggles.indexOf(id) < 0) { 207 this.mAllSimpleToggles.push(id); 208 Log.showDebug(TAG, `onItemAdd, mAllSimpleToggles: ${JSON.stringify(this.mAllSimpleToggles)}`); 209 this.calcSimpleToggleLayout(); 210 } 211 } 212 } 213 214 onItemRemove(itemData: ItemComponentData): void { 215 Log.showDebug(TAG, `onItemRemove, itemData: ${JSON.stringify(itemData)}`); 216 let id: string = itemData.id; 217 if (this.mAllComplexToggles.indexOf(id) >= 0) { 218 this.mAllComplexToggles.splice(this.mAllComplexToggles.indexOf(id), 1); 219 Log.showDebug(TAG, `onItemRemove, mAllComplexToggles: ${JSON.stringify(this.mAllComplexToggles)}`); 220 this.calcComplexToggleLayout(); 221 } else if (this.mAllSimpleToggles.indexOf(id) >= 0) { 222 this.mAllSimpleToggles.splice(this.mAllSimpleToggles.indexOf(id), 1); 223 Log.showDebug(TAG, `onItemRemove, mAllSimpleToggles: ${JSON.stringify(this.mAllSimpleToggles)}`); 224 this.calcSimpleToggleLayout(); 225 } 226 this.mListener?.setItemData(id, undefined); 227 } 228 229 getSimpleToggleLayoutFromSettings(): string[] { 230 Log.showDebug(TAG, 'getSimpleToggleLayoutFromSettings'); 231 let value = SettingsUtil.getValue(SETTINGS_CONTROL_SIMPLE_TOGGLE_LAYOUT); 232 let simpleToggleLayout: string[] = null; 233 if (!CheckEmptyUtils.isEmpty(value)) { 234 simpleToggleLayout = JSON.parse(value) as string[]; 235 } 236 Log.showDebug(TAG, `getSimpleToggleLayoutFromSettings, simpleToggleLayout: ${JSON.stringify(simpleToggleLayout)}`); 237 return simpleToggleLayout; 238 } 239 240 setSimpleToggleLayoutToSettings(simpleToggleLayout: string[]): void { 241 Log.showDebug(TAG, `setSimpleToggleLayoutToSettings, simpleToggleLayout: ${JSON.stringify(simpleToggleLayout)}`); 242 let value: string = JSON.stringify(simpleToggleLayout); 243 SettingsUtil.setValue(SETTINGS_CONTROL_SIMPLE_TOGGLE_LAYOUT, value); 244 } 245 246 getHidingSimpleToggles(): string[] { 247 Log.showDebug(TAG, 'getHidingSimpleToggles'); 248 let hidingSimpleToggles: string[] = []; 249 this.mAllSimpleToggles.forEach((toggleName) => { 250 if (this.mSimpleToggleLayout.indexOf(toggleName) < 0) { 251 hidingSimpleToggles.push(toggleName); 252 } 253 }); 254 Log.showDebug(TAG, `getHidingSimpleToggles, hidingSimpleToggles: ${JSON.stringify(hidingSimpleToggles)}`); 255 return hidingSimpleToggles; 256 } 257 258 getDefaultSimpleToggleLayout(): string[] { 259 Log.showDebug(TAG, 'getDefaultSimpleToggleLayout'); 260 let defaultToggles: string[] = []; 261 this.mConfig.DefaultSimpleToggleLayout.forEach((toggleName: string) => { 262 if (this.mAllSimpleToggles.indexOf(toggleName) >= 0) { 263 defaultToggles.push(toggleName); 264 } 265 }); 266 Log.showDebug(TAG, `getDefaultSimpleToggleLayout, defaultToggles: ${JSON.stringify(defaultToggles)}`); 267 return defaultToggles; 268 } 269 270 saveSimpleToggleLayout(layout: string[]): void{ 271 Log.showDebug(TAG, `saveSimpleToggleLayout, layout: ${JSON.stringify(layout)}`); 272 this.setSimpleToggleLayoutToSettings(layout); 273 this.mSimpleToggleLayoutTemplate = []; 274 layout.forEach((name) => { 275 this.mSimpleToggleLayoutTemplate.push(name); 276 }); 277 Log.showDebug(TAG, `saveSimpleToggleLayout, mSimpleToggleLayoutTemplate: ${JSON.stringify(this.mSimpleToggleLayoutTemplate)}`); 278 this.calcSimpleToggleLayout(); 279 } 280} 281 282let sControlCenterService = createOrGet(ControlCenterService, TAG); 283 284export default sControlCenterService;