aboutsummaryrefslogtreecommitdiffstats
path: root/cm/lib/main/java/org/cyanogenmod/platform/internal/CMTelephonyManagerService.java
blob: 35e5395668c5a6831f1d37ed90970aaed1b02c2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/**
 * Copyright (c) 2015, The CyanogenMod Project
 *
 * 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.
 */

package org.cyanogenmod.platform.internal;

import com.android.server.SystemService;

import android.content.Context;
import android.os.IBinder;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.util.Log;

import java.util.List;

import cyanogenmod.app.CMContextConstants;
import cyanogenmod.app.CMTelephonyManager;
import cyanogenmod.app.ICMTelephonyManager;

/**
 * Internal service which manages interactions with the phone and data connection
 *
 * @hide
 */
public class CMTelephonyManagerService extends SystemService {
    private static final String TAG = "CMTelephonyManagerSrv";
    private static boolean localLOGD = Log.isLoggable(TAG, Log.DEBUG);

    private TelephonyManager mTelephonyManager;
    private Context mContext;
    private final IBinder mService = new ICMTelephonyManager.Stub() {

        /**
         * Returns the available SIM subscription information.
         *
         * @return The list of SIM subscriptions. The returning list can be null or empty.
         * @hide
         */
        @Override
        public List<SubscriptionInfo> getSubInformation() {
            enforceTelephonyReadPermission();
            return getActiveSubscriptionInfoList();
        }

        /**
         * Returns the state of the SIM by subscription ID.
         *
         * If the subscription ID is not valid the method will return {@code false}.
         *
         * @param subId The subscription ID to query.
         * @return {@code true} if the SIM is activated (even without signal or requesting the
         * PIN/PUK), {@code false} otherwise.
         * @hide
         */
        @Override
        public boolean isSubActive(int subId) {
            enforceTelephonyReadPermission();
            return CMTelephonyManagerService.this.isSubActive(subId);
        }

        /**
         * Sets the state of one of the SIMs by subscription ID.
         *
         * If the subscription ID is not valid or the SIM already
         * is in the desired state the method will do nothing.
         *
         * @param subId The subscription ID to set.
         * @param state {@code true} to activate the SIM, {@code false} to disable.
         * @hide
         */
        @Override
        public void setSubState(int subId, boolean state) {
            enforceTelephonyModifyPermission();
            CMTelephonyManagerService.this.setSubState(subId, state);
        }

        /**
         * Checks if the received subscription received has the data
         * connection enabled.
         *
         * This method will return {@code true} (or {@code false} if inactive on the SIM)
         * even when an internet connection is active through Wifi/BT.
         *
         * If the subscription ID is not valid the method will return {@code false}.
         *
         * @param subId The subscription ID to query.
         * @return {@code true} if the data connection is enabled on the SIM, {@code false} otherwise.
         * @hide
         */
        public boolean isDataConnectionSelectedOnSub(int subId) {
            enforceTelephonyReadPermission();
            return CMTelephonyManagerService.this.isDataConnectionSelectedOnSub(subId);
        }

        /**
         * Checks if the network data connection is enabled.
         *
         * This method will return {@code true} (or {@code false} if inactive)
         * even when an internet connection is active through Wifi/BT.
         *
         * @return {@code true} if the network data connection is enabled, {@code false} otherwise.
         * @hide
         */
        public boolean isDataConnectionEnabled() {
            enforceTelephonyReadPermission();
            return CMTelephonyManagerService.this.isDataConnectionEnabled();
        }

        /**
         * Sets the network data conection active or inactive.
         *
         * @param state If {@code true} enables the network data connection, if {@code false} disables it.
         * @hide
         */
        public void setDataConnectionState(boolean state) {
            enforceTelephonyModifyPermission();
            CMTelephonyManagerService.this.setDataConnectionState(state);
        }

        /**
         * Sets the data connection state on one of the SIMs by subscription ID.
         *
         * If the subscription ID is not valid or the data connection is already
         * enabled on the SIM the method will do nothing.
         *
         * @param subId The subscription ID to set the network data connection.
         * @hide
         */
        public void setDataConnectionSelectedOnSub(int subId) {
            enforceTelephonyModifyPermission();
            CMTelephonyManagerService.this.setDataConnectionSelectedOnSub(subId);
        }

        /**
         * Sets the default phone used to make phone calls as the one received on subId.
         *
         * If 0 is used as a parameter, then the option to choose what SIM to use is
         * selected.
         *
         * @param subId The subscription to set as default for phone calls.
         *              To select SIM when calling use 0.
         * @hide
         */
        public void setDefaultPhoneSub(int subId) {
            enforceTelephonyModifyPermission();
            CMTelephonyManagerService.this.setDefaultPhoneSub(subId);
        }

        /**
         * Sets the default phone used to send SMS as the one received on subId.
         *
         * If 0 is used as a parameter, then the option to choose what SIM to use is
         * selected.
         *
         * @param subId The subscription to set as default for sending SMS.
         *              To select SIM when sending SMS use 0.
         * @hide
         */
        public void setDefaultSmsSub(int subId) {
            enforceTelephonyModifyPermission();
            CMTelephonyManagerService.this.setDefaultSmsSub(subId);
        }
    };

    public CMTelephonyManagerService(Context context) {
        super(context);
        mContext = context;
    }

    @Override
    public void onStart() {
        if (localLOGD) {
            Log.d(TAG, "CM telephony manager service start: " + this);
        }
        if (mContext.getPackageManager().hasSystemFeature(
                CMContextConstants.Features.TELEPHONY)) {
            publishBinderService(CMContextConstants.CM_TELEPHONY_MANAGER_SERVICE, mService);
        } else {
            Log.wtf(TAG, "CM telephony service started by system server but feature xml not" +
                    " declared. Not publishing binder service!");
        }
        mTelephonyManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
    }

    private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
        SubscriptionManager subscriptionManager = SubscriptionManager.from(mContext);
        List<SubscriptionInfo> subInfoList = subscriptionManager.getActiveSubscriptionInfoList();
        if (localLOGD) {
            Log.d(TAG, "The active subscriptions where obtained from the subscription manager.");
        }
        return subInfoList;
    }

    private boolean isSubActive(int subId) {
        boolean validSubscriptionId = SubscriptionManager.isValidSubscriptionId(subId);

        if (validSubscriptionId) {
            int simState = SubscriptionManager.getSimStateForSlotIdx(
                    SubscriptionManager.getSlotId(subId));
            switch (simState) {
                case TelephonyManager.SIM_STATE_ABSENT:
                case TelephonyManager.SIM_STATE_CARD_IO_ERROR:
                case TelephonyManager.SIM_STATE_PERM_DISABLED:
                case TelephonyManager.SIM_STATE_NOT_READY:
                    if (localLOGD) {
                        Log.d(TAG, "The subscription " + subId + " is NOT active: " + simState);
                    }
                    return false;
                default:
                    if (localLOGD) {
                        Log.d(TAG, "The subscription " + subId + " is active: " + simState);
                    }
                    return true;
            }
        } else {
            Log.w(TAG, "Invalid subscription identifier: " + subId);
            return false;
        }
    }

    private void setSubState(int subId, boolean state) {
        if (localLOGD) {
            Log.d(TAG, "Setting the subscription " + subId + " to inactive (false) or active (true): " + state);
        }

        if (state) {
            SubscriptionManager.activateSubId(subId);
        } else {
            SubscriptionManager.deactivateSubId(subId);
        }
    }

    private boolean isDataConnectionSelectedOnSub(int subId) {
        boolean validSubscriptionId = SubscriptionManager.isValidSubscriptionId(subId);

        if (validSubscriptionId) {
            if (subId == SubscriptionManager.getDefaultDataSubId()) {
                if (localLOGD) {
                    Log.d(TAG, "Data connection selected for subscription " + subId);
                }
                return true;
            } else {
                if (localLOGD) {
                    Log.d(TAG, "Data connection not selected for subscription " + subId);
                }
                return false;
            }
        } else {
            Log.w(TAG, "Invalid subscription identifier: " + subId);
            return false;
        }
    }

    private boolean isDataConnectionEnabled() {
        if (localLOGD) {
            Log.d(TAG, "Checking if the network data connection is active");
        }

        boolean dataEnabled = mTelephonyManager.getDataEnabled();

        if (localLOGD) {
            Log.d(TAG, "Data network connection is inactive (false) or active (true): " + dataEnabled);
        }

        return dataEnabled;
    }

    private void setDataConnectionState(boolean state) {
        if (localLOGD) {
            Log.d(TAG, "Setting the network data connection inactive (false) or active (true): " + state);
        }

        if (state) {
            mTelephonyManager.enableDataConnectivity();
        } else {
            mTelephonyManager.disableDataConnectivity();
        }
    }

    private void setDataConnectionSelectedOnSub(int subId) {
        if (localLOGD) {
            Log.d(TAG, "Setting the network data connection for subscription " + subId);
        }

        SubscriptionManager subscriptionManager = SubscriptionManager.from(mContext);
        subscriptionManager.setDefaultDataSubId(subId);
    }

    private void setDefaultPhoneSub(int subId) {
        if (localLOGD) {
            Log.d(TAG, "Setting the SIM for phone calls on subscription " + subId);
        }

        SubscriptionManager subscriptionManager = SubscriptionManager.from(mContext);
        /*if (subId == CMTelephonyManager.ASK_FOR_SUBSCRIPTION_ID) {
            if (localLOGD) {
                Log.d(TAG, "Activates the prompt for phone calls");
            }
            SubscriptionManager.setVoicePromptEnabled(true);
        } else {
            SubscriptionManager.setVoicePromptEnabled(false);
            subscriptionManager.setDefaultVoiceSubId(subId);
        }*/
        subscriptionManager.setDefaultVoiceSubId(subId);
    }

    private void setDefaultSmsSub(int subId) {
        if (localLOGD) {
            Log.d(TAG, "Setting the SIM for phone calls on subscription " + subId);
        }

        SubscriptionManager subscriptionManager = SubscriptionManager.from(mContext);
        /*if (subId == CMTelephonyManager.ASK_FOR_SUBSCRIPTION_ID) {
            if (localLOGD) {
                Log.d(TAG, "Activates the prompt for SMS");
            }
            SubscriptionManager.setSMSPromptEnabled(true);
        } else {
            SubscriptionManager.setSMSPromptEnabled(false);
            subscriptionManager.setDefaultSmsSubId(subId);
        }*/
        subscriptionManager.setDefaultSmsSubId(subId);
    }

    private void enforceTelephonyReadPermission() {
        mContext.enforceCallingOrSelfPermission(
                cyanogenmod.platform.Manifest.permission.READ_MSIM_PHONE_STATE,
                "CMTelephonyManagerService");
    }

    private void enforceTelephonyModifyPermission() {
        mContext.enforceCallingOrSelfPermission(
                cyanogenmod.platform.Manifest.permission.MODIFY_MSIM_PHONE_STATE,
                "CMTelephonyManagerService");
    }
}