1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16
17package com.android.phone.vvm;
18
19import android.annotation.Nullable;
20import android.telecom.PhoneAccountHandle;
21import android.telephony.SubscriptionManager;
22import com.android.internal.telephony.Phone;
23import com.android.internal.telephony.PhoneFactory;
24import com.android.phone.PhoneUtils;
25import com.android.phone.vvm.VvmLog;
26
27/**
28 * Utility to convert between PhoneAccountHandle and subId, which is a common operation in OMTP
29 * client
30 *
31 * TODO(b/28977379): remove dependency on PhoneUtils and use public APIs
32 */
33public class PhoneAccountHandleConverter {
34
35    private static final String TAG = "PhoneAccountHndCvtr";
36
37    @Nullable
38    public static PhoneAccountHandle fromSubId(int subId) {
39        if (!SubscriptionManager.isValidSubscriptionId(subId)) {
40            VvmLog.e(TAG, "invalid subId " + subId);
41            return null;
42        }
43        // Calling PhoneUtils.makePstnPhoneAccountHandle() with a phoneId might throw a NPE if the
44        // phone object cannot be found, so the Phone object should be created and checked here.
45        Phone phone = PhoneFactory.getPhone(SubscriptionManager.getPhoneId(subId));
46        if (phone == null) {
47            VvmLog.e(TAG, "Unable to find Phone for subId " + subId);
48            return null;
49        }
50        return PhoneUtils.makePstnPhoneAccountHandle(phone);
51    }
52
53    public static int toSubId(PhoneAccountHandle handle) {
54        return PhoneUtils.getSubIdForPhoneAccountHandle(handle);
55    }
56
57    private PhoneAccountHandleConverter() {
58    }
59}
60