1/**
2 * Copyright (c) 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 */
15import LooseObject from '../../../data/LooseObject'
16import HiLog from '../../../utils/HiLog';
17import router from '@system.router';
18import prompt from '@system.prompt';
19import settingService from '../../../service/SettingService'
20import common from '../../../data/commonData';
21
22const TAG = 'AdvancedSettingsController';
23
24export default class AdvancedSettingsController {
25    private static sInstance: AdvancedSettingsController;
26    // Delivery Report SMS
27    checkedValueOfSms: boolean = false;
28    // Delivery Report MMS
29    checkedValueOfMms: boolean = false;
30    // Value of Delivery Report Switch
31    deliveryReportSwitch: string = common.DELIVERY_REPORTS.DISABLED;
32    // Delivery Report Text
33    deliveryReportSwitchInText: Resource = $r('app.string.disabled');
34    // Automatic MM Download Switch
35    autoRetrieveMmsSwitch: string = common.AUTO_RETRIEVE_MMS.OFF;
36    // Text for automatically downloading MMS messages
37    autoRetrieveMmsSwitchInText: Resource | string = $r('app.string.off');
38    // Cancel Sending Switch
39    recallMessageSwitch: string = common.bool.FALSE;
40    // Automatic Notification Deletion Switch
41    autoDeleteInfoSwitch: string = common.bool.FALSE;
42    // Indicates whether to automatically delete notification information. This parameter is a temporary variable.
43    tempAutoDeleteInfoSwitch: string = common.bool.FALSE;
44    // China Telecom
45    snpNameOfChinaTelecom: string = '';
46    // Number of SIM cards
47    simCount: number = 0;
48    // For one card, the name of the carrier.
49    spnNameOfOneSimCard: string = '';
50    // If there are two cards, the carrier name of card 1 is the name of card 1.
51    firstSpnNameOfTwoSimCard: string = '';
52    // If there are two cards, the carrier name of card 2 is the name of card 2.
53    secondSpnNameOfTwoSimCard: string = '';
54    refresh: boolean = false;
55
56    static getInstance() {
57        if (AdvancedSettingsController.sInstance == null) {
58            AdvancedSettingsController.sInstance = new AdvancedSettingsController();
59        }
60        return AdvancedSettingsController.sInstance;
61    }
62
63    constructor() {
64        HiLog.i(TAG, 'constructor, start');
65        this.getAdvancedPageSwitchValue();
66    }
67
68    onInit() {
69        HiLog.i(TAG, 'onInit, start');
70    }
71
72    onShow() {
73        HiLog.i(TAG, 'onShow, start');
74    }
75
76    getAdvancedPageSwitchValue() {
77        HiLog.i(TAG, 'getAdvancedPageSwitchValue, start');
78        let that = this;
79        settingService.getAdvancedPageSwitchValue(function (result) {
80            if (result.code === common.int.SUCCESS) {
81                let switchValue = result.abilityResult;
82                that.deliveryReportSwitch = switchValue.deliveryReportSwitch;
83                that.returnDeliveryReportResultInText(that.deliveryReportSwitch);
84                that.autoRetrieveMmsSwitch = switchValue.autoRetrieveMmsSwitch;
85                that.returnAutoRetrieveMmsResultInText(that.autoRetrieveMmsSwitch);
86                that.recallMessageSwitch = switchValue.recallMessageSwitch;
87                that.autoDeleteInfoSwitch = switchValue.autoDeleteInfoSwitch;
88                that.tempAutoDeleteInfoSwitch = that.autoDeleteInfoSwitch;
89                // Change the SIM card to switchValue.simCount after the SIM card functions properly.
90                that.simCount = switchValue.simCount;
91                if (that.simCount === 2) {
92                    that.firstSpnNameOfTwoSimCard = switchValue.firstSpnNameOfTwoSimCard;
93                    that.secondSpnNameOfTwoSimCard = switchValue.secondSpnNameOfTwoSimCard;
94                } else {
95                    that.spnNameOfOneSimCard = switchValue.spnNameOfOneSimCard;
96                }
97            } else {
98                HiLog.w(TAG, 'getAdvancedPageSwitchValue, failed');
99            }
100        });
101    }
102    // Returns the text version of the delivery report result based on an integer value
103    returnDeliveryReportResultInText(intValue) {
104        let tempValue;
105        if (intValue == common.DELIVERY_REPORTS.DISABLED || intValue == '') {
106            tempValue = $r('app.string.disabled');
107            this.checkedValueOfSms = false;
108            this.checkedValueOfMms = false;
109        } else if (intValue == common.DELIVERY_REPORTS.SMS) {
110            tempValue = $r('app.string.sms');
111            this.checkedValueOfSms = true;
112            this.checkedValueOfMms = false;
113        } else if (intValue == common.DELIVERY_REPORTS.MMS) {
114            tempValue = $r('app.string.mms');
115            this.checkedValueOfSms = false;
116            this.checkedValueOfMms = true;
117        } else {
118            tempValue = $r('app.string.sms_and_mms');
119            this.checkedValueOfSms = true;
120            this.checkedValueOfMms = true;
121        }
122        this.deliveryReportSwitchInText = tempValue;
123        this.refresh = !this.refresh;
124    }
125    // Returns the text version of the delivery report result based on an integer value
126    returnAutoRetrieveMmsResultInText(intValue) {
127        let tempValue;
128        if (intValue == common.AUTO_RETRIEVE_MMS.OFF) {
129            tempValue = $r('app.string.off');
130        } else if (intValue == common.AUTO_RETRIEVE_MMS.NOT_WHEN_ROAMING) {
131            tempValue = $r('app.string.not_when_roaming');
132        } else {
133            tempValue = $r('app.string.always');
134        }
135        this.autoRetrieveMmsSwitchInText = tempValue;
136    }
137    // Back button
138    back() {
139        router.back();
140    }
141    // Restore the default values on the settings page.
142    restoreSettingPageSwitchValue() {
143        HiLog.i(TAG, 'restoreSettingPageSwitchValue');
144        let that = this;
145        settingService.restoreSwitchValue(function (result) {
146            if (result.code === common.int.SUCCESS) {
147                that.deliveryReportSwitch = common.DELIVERY_REPORTS.DISABLED;
148                that.returnDeliveryReportResultInText(common.DELIVERY_REPORTS.DISABLED);
149                that.autoRetrieveMmsSwitch = common.AUTO_RETRIEVE_MMS.NOT_WHEN_ROAMING;
150                that.returnAutoRetrieveMmsResultInText(common.AUTO_RETRIEVE_MMS.NOT_WHEN_ROAMING);
151                that.recallMessageSwitch = common.bool.FALSE;
152                that.autoDeleteInfoSwitch = common.bool.FALSE;
153                that.tempAutoDeleteInfoSwitch = common.bool.FALSE;
154                HiLog.i(TAG, 'restoreSettingPageSwitchValue, success');
155            } else {
156                HiLog.w(TAG, 'restoreSettingPageSwitchValue, failed');
157            }
158        });
159    }
160    // Click the text line.
161    clickSmsDiv() {
162        this.checkedValueOfSms = !this.checkedValueOfSms;
163    }
164    // Click Checkbox in the line of the message.
165    clickSmsCheckbox(e) {
166        this.checkedValueOfSms = e.checked;
167    }
168    // Click the MMS line.
169    clickMmsDiv() {
170        this.checkedValueOfMms = !this.checkedValueOfMms;
171    }
172    // Click Checkbox in the MMS line.
173    clickMmsCheckbox(e) {
174        this.checkedValueOfMms = e.checked;
175    }
176    // Cancel the configuration restoration dialog box.
177    cancelRestore() {
178        this.returnDeliveryReportResultInText(this.deliveryReportSwitch);
179    }
180    // delivery report dialog, OK
181    setRestore(isOnOfSms: boolean, isOnOfMms: boolean) {
182        this.checkedValueOfSms = isOnOfSms;
183        this.checkedValueOfMms = isOnOfMms;
184        this.deliveryReportSwitch = common.string.EMPTY_STR;
185        if (this.checkedValueOfSms && this.checkedValueOfMms) {
186            this.deliveryReportSwitch = common.DELIVERY_REPORTS.SMS_AND_MMS;
187        } else if (this.checkedValueOfSms) {
188            this.deliveryReportSwitch = common.DELIVERY_REPORTS.SMS;
189        } else if (this.checkedValueOfMms) {
190            this.deliveryReportSwitch = common.DELIVERY_REPORTS.MMS;
191        } else {
192            this.deliveryReportSwitch = common.DELIVERY_REPORTS.DISABLED;
193        }
194        this.returnDeliveryReportResultInText(this.deliveryReportSwitch);
195        this.autoHandleDeliveryReportValueChange(this.deliveryReportSwitch);
196    }
197    // When the value of deliveryReportSwitch changes, this method is used to process the change.
198    autoHandleDeliveryReportValueChange(newValue) {
199        HiLog.i(TAG, 'autoHandleDeliveryReportValueChange, newValue = ' + newValue);
200        let messageCode = common.route.MESSAGE_CODE_UPDATE_DELIVERY_REPORTS_VALUE;
201        let actionData: LooseObject = {};
202        actionData.intValue = newValue;
203        this.updateAdvancedPageSwitchValue(messageCode, actionData);
204    }
205    // Displaying the 'Automatic Download MMS' dialog
206    showAutoRetrieveMmsDialog() {
207        //    this.$element('auto-retrieve-mms-dialog').show();
208    }
209    // Click the corresponding option in the dialog box for automatically downloading MMs.
210    clickDiv(idx) {
211        this.autoRetrieveMmsSwitch = idx + common.string.EMPTY_STR;
212        this.returnAutoRetrieveMmsResultInText(idx);
213        //    this.$element('auto-retrieve-mms-dialog').close();
214        this.autoHandleAutoRetrieveMmsValueChange(this.autoRetrieveMmsSwitch);
215    }
216    // Disable the automatic MM download dialog box.
217    closeAutoRetrieveMmsDialog() {
218        //    this.$element('auto-retrieve-mms-dialog').close();
219    }
220    // This method is used when the value of autoRetrieveMmsSwitch changes.
221    autoHandleAutoRetrieveMmsValueChange(newValue) {
222        HiLog.i(TAG, 'autoHandleAutoRetrieveMmsValueChange, newValue = ' + newValue);
223        let messageCode = common.route.MESSAGE_CODE_UPDATE_AUTO_RETRIEVE_MMS_VALUE;
224        let actionData: LooseObject = {};
225        actionData.intValue = newValue;
226        this.updateAdvancedPageSwitchValue(messageCode, actionData);
227    }
228    // Cancel Sending
229    recallMsg(e) {
230        let messageCode = common.route.MESSAGE_CODE_UPDATE_RECALL_MESSAGES_VALUE;
231        let actionData: LooseObject = {};
232        this.recallMessageSwitch = e.checked;
233        if (this.recallMessageSwitch) {
234            actionData.booleanValue = common.bool.TRUE;
235        } else {
236            actionData.booleanValue = common.bool.FALSE;
237        }
238        this.updateAdvancedPageSwitchValue(messageCode, actionData);
239    }
240    // Automatically delete notification information
241    autoDeleteInfo(e) {
242        let that = this;
243        that.tempAutoDeleteInfoSwitch = e.checked;
244        if (e.checked) {
245            prompt.showDialog({
246                title: $r('app.string.enable_auto_delete') + '',
247                message: $r('app.string.enable_auto_delete_hint') + '',
248                buttons: [
249                    {
250                        text: $r('app.string.cancel') + '', color: '#007DFF'
251                    },
252                    {
253                        text: $r('app.string.enable') + '', color: '#007DFF'
254                    }
255                ],
256                success: function (data) {
257                    that.autoDeleteSuccess(data);
258                },
259                cancel: function () {
260                    HiLog.i(TAG, 'autoDeleteInfo, cancel');
261                    that.tempAutoDeleteInfoSwitch = common.bool.FALSE;
262                    that.autoDeleteInfoSwitch = common.bool.FALSE;
263                }
264            });
265        } else {
266            that.autoDeleteInfoSwitch = common.bool.FALSE;
267            that.autoHandleAutoDeleteInfoValueChange(common.bool.FALSE);
268        }
269    }
270
271    autoDeleteSuccess(data) {
272        if (data.index == 0) {
273            this.tempAutoDeleteInfoSwitch = common.bool.FALSE;
274        } else {
275            this.autoDeleteInfoSwitch = common.bool.TRUE;
276            this.autoHandleAutoDeleteInfoValueChange(common.bool.TRUE);
277        }
278    }
279    // This method is used when the value of autoDeleteInfoSwitch changes.
280    autoHandleAutoDeleteInfoValueChange(newValue) {
281        HiLog.i(TAG, 'autoHandleAutoDeleteInfoValueChange, newValue = ' + newValue);
282        let messageCode = common.route.MESSAGE_CODE_UPDATE_AUTO_DELETE_INFO_MESSAGES_VALUE;
283        let actionData: LooseObject = {};
284        actionData.booleanValue = newValue;
285        this.updateAdvancedPageSwitchValue(messageCode, actionData);
286    }
287    // The SMSC page is displayed.
288    jumpToSmsCenterPage(index) {
289        if (this.simCount == 0) {
290            return;
291        }
292        router.push({
293            uri: 'pages/sms_center/sms_center',
294            params: {
295                idx: index,
296                countOfSim: this.simCount
297            }
298        });
299    }
300    // The Manage SIM Card page is displayed.
301    jumpToManageSimPage(index) {
302        if (this.simCount == 0) {
303            return;
304        }
305        router.push({
306            uri: 'pages/manage_sim/manage_sim',
307            params: {
308                idx: index,
309                countOfSim: this.simCount
310            }
311        });
312    }
313    // Update Switch Value
314    updateAdvancedPageSwitchValue(messageCode, actionData) {
315        settingService.updateSettingValue(messageCode, actionData, function (result) {
316            if (result.code == common.int.SUCCESS) {
317                HiLog.i(TAG, 'updateAdvancedPageSwitchValue, success');
318            } else {
319                HiLog.w(TAG, 'updateAdvancedPageSwitchValue, failed');
320            }
321        });
322    }
323}