• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 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 telephonyData from '@ohos.telephony.data';
16import radio from '@ohos.telephony.radio';
17import LogUtils from '../common/utils/LogUtils';
18import call from '@ohos.telephony.call';
19
20const TAG = 'mobileDataStatus';
21
22/**
23 * Get cellular data status
24 *
25 * @return {Promise} promise object
26 */
27export function isCellularDataEnabled() {
28  if (telephonyData.isCellularDataEnabled()) {
29    return telephonyData.isCellularDataEnabled();
30  }
31}
32
33export function getOperatorName(slotId = 0) {
34  return new Promise((resolve, reject) => {
35    try {
36      radio.getOperatorName(slotId, (error, value) => {
37        if (error) {
38          LogUtils.i(TAG, 'getOperatorName error:' + JSON.stringify(error))
39          reject(error);
40        } else {
41          LogUtils.i(TAG, 'getOperatorName value:' + JSON.stringify(value))
42          resolve(value);
43        }
44      });
45    } catch (error) {
46      LogUtils.i(TAG, 'getOperatorName catch:' + JSON.stringify(error))
47      reject(error);
48    }
49  });
50};
51
52/**
53 * Enable mobile data
54 *
55 * @return {Promise} promise object
56 */
57export function enableCellularData() {
58  if (telephonyData.enableCellularData) {
59    return telephonyData.enableCellularData();
60  }
61}
62
63/**
64 * disableCellularData mobile data
65 *
66 * @return {Promise} promise object
67 */
68export function disableCellularData() {
69  if (telephonyData.disableCellularData) {
70    return telephonyData.disableCellularData();
71  }
72}
73
74/**
75 * Whether to enable cellular data roaming
76 *
77 * @return {Promise}
78 */
79export function getCellularDataRoaming(slotId = 0) {
80  return telephonyData.isCellularDataRoamingEnabled(slotId);
81}
82
83/**
84 * Enabling Data Roaming
85 *
86 * @return {Promise}
87 */
88export function enableCellularDataRoamingCardOne(slotId = 0) {
89  return new Promise((resolve, reject) => {
90    try {
91      telephonyData.enableCellularDataRoaming(slotId, (error, value) => {
92        if (error) {
93          LogUtils.i(TAG, 'enableCellularDataRoaming card one error :' + JSON.stringify(error));
94          reject(error);
95        } else {
96          LogUtils.i(TAG, 'enableCellularDataRoaming card one success : then' + JSON.stringify(value));
97          resolve(value);
98        }
99      });
100    } catch (error) {
101      LogUtils.i(TAG, 'enableCellularDataRoaming card one catch: ' + JSON.stringify(error));
102      reject(error);
103    }
104  });
105}
106
107export function enableCellularDataRoamingCardTwo(slotId = 1) {
108  return new Promise((resolve, reject) => {
109    try {
110      telephonyData.enableCellularDataRoaming(slotId, (error, value) => {
111        if (error) {
112          LogUtils.i(TAG, 'enableCellularDataRoaming card two error :' + JSON.stringify(error));
113          reject(error);
114        } else {
115          LogUtils.i(TAG, 'enableCellularDataRoaming card two success :then' + JSON.stringify(value));
116          resolve(value);
117        }
118      });
119    } catch (error) {
120      LogUtils.i(TAG, 'enableCellularDataRoaming card two catch: ' + JSON.stringify(error));
121      reject(error);
122    }
123  });
124}
125
126/**
127 * Enabling Data Roaming
128 *
129 * @return {Promise}
130 */
131export function disableCellularDataRoamingCardOne(slotId = 0) {
132  return new Promise((resolve, reject) => {
133    try {
134      telephonyData.disableCellularDataRoaming(slotId, (error, value) => {
135        if (error) {
136          LogUtils.i(TAG, 'disableCellularDataRoaming card one error : ' + JSON.stringify(error));
137          reject(error);
138        } else {
139          LogUtils.i(TAG, 'disableCellularDataRoaming card one success : then' + JSON.stringify(value));
140          resolve(value);
141        }
142      });
143    } catch (error) {
144      LogUtils.i(TAG, 'disableCellularDataRoaming card one catch:' + JSON.stringify(error));
145      reject(error);
146    }
147  });
148}
149
150export function disableCellularDataRoamingCardTwo(slotId = 1) {
151  return new Promise((resolve, reject) => {
152    try {
153      telephonyData.disableCellularDataRoaming(slotId, (error, value) => {
154        if (error) {
155          LogUtils.i(TAG, 'disableCellularDataRoaming card two error :' + JSON.stringify(error));
156          reject(error);
157        } else {
158          LogUtils.i(TAG, 'disableCellularDataRoaming card two success :then' + JSON.stringify(value));
159          resolve(value);
160        }
161      });
162    } catch (error) {
163      LogUtils.i(TAG, 'disableCellularDataRoaming card two catch:' + JSON.stringify(error));
164      reject(error);
165    }
166  });
167}
168
169
170export function enableImsSwitchCardOne() {
171  call.enableImsSwitch(0).then((data) => {
172    AppStorage.SetOrCreate('imsOne', true);
173    LogUtils.i(TAG, 'enableImsSwitch card one : success then ' + JSON.stringify(data));
174  }).catch((error) => {
175    LogUtils.i(TAG, 'enableImsSwitch card one : catch' + JSON.stringify(error));
176  });
177}
178
179export function enableImsSwitchCardTwo() {
180  call.enableImsSwitch(1).then((data) => {
181    LogUtils.i(TAG, 'enableImsSwitch card two : success then ' + JSON.stringify(data));
182  }).catch((error) => {
183    LogUtils.i(TAG, 'enableImsSwitch card two : catch' + JSON.stringify(error));
184  });
185}
186
187export function disableImsSwitchCardOne() {
188  call.disableImsSwitch(0).then((data) => {
189    AppStorage.SetOrCreate('imsOne', false);
190    LogUtils.i(TAG, 'disableImsSwitch card one : success then ' + JSON.stringify(data));
191  }).catch((error) => {
192    LogUtils.i(TAG, 'disableImsSwitch card one : catch' + JSON.stringify(error));
193  });
194}
195
196export function disableImsSwitchCardTwo() {
197  call.disableImsSwitch(1).then((data) => {
198    LogUtils.i(TAG, 'disableImsSwitch card two : success then ' + JSON.stringify(data));
199  }).catch((error) => {
200    LogUtils.i(TAG, 'disableImsSwitch card two : catch' + JSON.stringify(error));
201  });
202}
203