Home | History | Annotate | Download | only in impl
      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  */
     16 
     17 package com.android.dialer.simulator.impl;
     18 
     19 import android.content.ComponentName;
     20 import android.content.Context;
     21 import android.net.Uri;
     22 import android.telecom.Connection;
     23 import android.telecom.ConnectionRequest;
     24 import android.telecom.ConnectionService;
     25 import android.telecom.PhoneAccount;
     26 import android.telecom.PhoneAccountHandle;
     27 import android.telecom.TelecomManager;
     28 import android.telephony.TelephonyManager;
     29 import com.android.dialer.common.LogUtil;
     30 import java.util.ArrayList;
     31 import java.util.List;
     32 
     33 /** Simple connection provider to create an incoming call. This is useful for emulators. */
     34 public final class SimulatorConnectionService extends ConnectionService {
     35 
     36   private static final String PHONE_ACCOUNT_ID = "SIMULATOR_ACCOUNT_ID";
     37 
     38   public static void register(Context context) {
     39     LogUtil.enterBlock("SimulatorConnectionService.register");
     40     context.getSystemService(TelecomManager.class).registerPhoneAccount(buildPhoneAccount(context));
     41   }
     42 
     43   private static PhoneAccount buildPhoneAccount(Context context) {
     44     PhoneAccount.Builder builder =
     45         new PhoneAccount.Builder(
     46             getConnectionServiceHandle(context), "Simulator connection service");
     47     List<String> uriSchemes = new ArrayList<>();
     48     uriSchemes.add(PhoneAccount.SCHEME_TEL);
     49 
     50     return builder
     51         .setCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER)
     52         .setShortDescription("Simulator Connection Service")
     53         .setSupportedUriSchemes(uriSchemes)
     54         .build();
     55   }
     56 
     57   public static PhoneAccountHandle getConnectionServiceHandle(Context context) {
     58     return new PhoneAccountHandle(
     59         new ComponentName(context, SimulatorConnectionService.class), PHONE_ACCOUNT_ID);
     60   }
     61 
     62   private static Uri getPhoneNumber(ConnectionRequest request) {
     63     String phoneNumber = request.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
     64     return Uri.fromParts(PhoneAccount.SCHEME_TEL, phoneNumber, null);
     65   }
     66 
     67   @Override
     68   public Connection onCreateOutgoingConnection(
     69       PhoneAccountHandle phoneAccount, ConnectionRequest request) {
     70     LogUtil.i(
     71         "SimulatorConnectionService.onCreateOutgoingConnection",
     72         "outgoing calls not supported yet");
     73     return null;
     74   }
     75 
     76   @Override
     77   public Connection onCreateIncomingConnection(
     78       PhoneAccountHandle phoneAccount, ConnectionRequest request) {
     79     LogUtil.enterBlock("SimulatorConnectionService.onCreateIncomingConnection");
     80     SimulatorConnection connection = new SimulatorConnection();
     81     connection.setRinging();
     82     connection.setAddress(getPhoneNumber(request), TelecomManager.PRESENTATION_ALLOWED);
     83     connection.setConnectionCapabilities(
     84         Connection.CAPABILITY_MUTE | Connection.CAPABILITY_SUPPORT_HOLD);
     85     return connection;
     86   }
     87 }
     88