1/*
2 * Copyright (C) 2017 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 */
16package com.android.dialer.app.calllog;
17
18import android.content.ComponentName;
19import android.content.Context;
20import android.support.annotation.NonNull;
21import android.support.annotation.Nullable;
22import android.telecom.PhoneAccount;
23import android.telecom.PhoneAccountHandle;
24import com.android.dialer.app.calllog.CallLogNotificationsQueryHelper.NewCall;
25import com.android.dialer.common.LogUtil;
26import com.android.dialer.telecom.TelecomUtil;
27import java.util.List;
28
29/** Methods to help extract {@link PhoneAccount} information from database and Telecomm sources. */
30class PhoneAccountHandles {
31
32  @Nullable
33  public static PhoneAccountHandle getAccount(@NonNull Context context, @Nullable NewCall call) {
34    PhoneAccountHandle handle;
35    if (call == null || call.accountComponentName == null || call.accountId == null) {
36      LogUtil.v(
37          "PhoneAccountUtils.getAccount",
38          "accountComponentName == null || callToNotify.accountId == null");
39      handle = TelecomUtil.getDefaultOutgoingPhoneAccount(context, PhoneAccount.SCHEME_TEL);
40      if (handle == null) {
41        List<PhoneAccountHandle> callCapablePhoneAccounts =
42            TelecomUtil.getCallCapablePhoneAccounts(context);
43        if (!callCapablePhoneAccounts.isEmpty()) {
44          return callCapablePhoneAccounts.get(0);
45        }
46        return null;
47      }
48    } else {
49      handle =
50          new PhoneAccountHandle(
51              ComponentName.unflattenFromString(call.accountComponentName), call.accountId);
52    }
53    if (handle.getComponentName() != null) {
54      LogUtil.v(
55          "PhoneAccountUtils.getAccount",
56          "PhoneAccountHandle.ComponentInfo:" + handle.getComponentName());
57    } else {
58      LogUtil.i("PhoneAccountUtils.getAccount", "PhoneAccountHandle.ComponentInfo: null");
59    }
60    return handle;
61  }
62}
63