Home | History | Annotate | Download | only in testapps
      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.server.telecom.testapps;
     18 
     19 import android.content.ComponentName;
     20 import android.content.Context;
     21 import android.net.Uri;
     22 import android.os.Bundle;
     23 import android.telecom.ConnectionRequest;
     24 import android.telecom.Log;
     25 import android.telecom.PhoneAccount;
     26 import android.telecom.PhoneAccountHandle;
     27 import android.telecom.TelecomManager;
     28 import android.util.ArrayMap;
     29 
     30 import java.util.ArrayList;
     31 import java.util.List;
     32 import java.util.Map;
     33 import java.util.Optional;
     34 
     35 /**
     36  * Manages the list of {@link SelfManagedConnection} active in the sample third-party calling app.
     37  */
     38 public class SelfManagedCallList {
     39     public abstract static class Listener {
     40         public void onCreateIncomingConnectionFailed(ConnectionRequest request) {};
     41         public void onCreateOutgoingConnectionFailed(ConnectionRequest request) {};
     42         public void onConnectionListChanged() {};
     43     }
     44 
     45     public static String SELF_MANAGED_ACCOUNT_1 = "1";
     46     public static String SELF_MANAGED_ACCOUNT_2 = "2";
     47     public static String SELF_MANAGED_NAME_1 = "SuperCall";
     48     public static String SELF_MANAGED_NAME_2 = "Mega Call";
     49 
     50     private static SelfManagedCallList sInstance;
     51     private static ComponentName COMPONENT_NAME = new ComponentName(
     52             SelfManagedCallList.class.getPackage().getName(),
     53             SelfManagedConnectionService.class.getName());
     54     private static Uri SELF_MANAGED_ADDRESS_1 = Uri.fromParts(PhoneAccount.SCHEME_TEL, "555-1212",
     55             "");
     56     private static Uri SELF_MANAGED_ADDRESS_2 = Uri.fromParts(PhoneAccount.SCHEME_SIP,
     57             "me (at) test.org", "");
     58     private static Map<String, PhoneAccountHandle> mPhoneAccounts = new ArrayMap();
     59 
     60     public static SelfManagedCallList getInstance() {
     61         if (sInstance == null) {
     62             sInstance = new SelfManagedCallList();
     63         }
     64         return sInstance;
     65     }
     66 
     67     private Listener mListener;
     68 
     69     private List<SelfManagedConnection> mConnections = new ArrayList<>();
     70 
     71     private SelfManagedConnection.Listener mConnectionListener =
     72             new SelfManagedConnection.Listener() {
     73                 @Override
     74                 public void onConnectionStateChanged(SelfManagedConnection connection) {
     75                     notifyCallModified();
     76                 }
     77 
     78                 @Override
     79                 public void onConnectionRemoved(SelfManagedConnection connection) {
     80                     removeConnection(connection);
     81                     notifyCallModified();
     82                 }
     83     };
     84 
     85     public SelfManagedConnection.Listener getConnectionListener() {
     86         return mConnectionListener;
     87     }
     88 
     89 
     90     public void setListener(Listener listener) {
     91         mListener = listener;
     92     }
     93 
     94     public void registerPhoneAccounts(Context context) {
     95         registerPhoneAccount(context, SELF_MANAGED_ACCOUNT_1, SELF_MANAGED_ADDRESS_1,
     96                 SELF_MANAGED_NAME_1, true /* areCallsLogged */);
     97         registerPhoneAccount(context, SELF_MANAGED_ACCOUNT_2, SELF_MANAGED_ADDRESS_2,
     98                 SELF_MANAGED_NAME_2, false /* areCallsLogged */);
     99     }
    100 
    101     public void registerPhoneAccount(Context context, String id, Uri address, String name,
    102                                      boolean areCallsLogged) {
    103         PhoneAccountHandle handle = new PhoneAccountHandle(COMPONENT_NAME, id);
    104         mPhoneAccounts.put(id, handle);
    105         Bundle extras = new Bundle();
    106         extras.putBoolean(PhoneAccount.EXTRA_SUPPORTS_HANDOVER_TO, true);
    107         if (areCallsLogged) {
    108             extras.putBoolean(PhoneAccount.EXTRA_LOG_SELF_MANAGED_CALLS, true);
    109         }
    110         PhoneAccount.Builder builder = PhoneAccount.builder(handle, name)
    111                 .addSupportedUriScheme(PhoneAccount.SCHEME_TEL)
    112                 .addSupportedUriScheme(PhoneAccount.SCHEME_SIP)
    113                 .setAddress(address)
    114                 .setCapabilities(PhoneAccount.CAPABILITY_SELF_MANAGED |
    115                         PhoneAccount.CAPABILITY_VIDEO_CALLING |
    116                         PhoneAccount.CAPABILITY_SUPPORTS_VIDEO_CALLING)
    117                 .setExtras(extras)
    118                 .setShortDescription(name);
    119 
    120         TelecomManager.from(context).registerPhoneAccount(builder.build());
    121     }
    122 
    123     public PhoneAccountHandle getPhoneAccountHandle(String id) {
    124         return mPhoneAccounts.get(id);
    125     }
    126 
    127     public void notifyCreateIncomingConnectionFailed(ConnectionRequest request) {
    128         if (mListener != null) {
    129             mListener.onCreateIncomingConnectionFailed(request);
    130         }
    131     }
    132 
    133     public void notifyCreateOutgoingConnectionFailed(ConnectionRequest request) {
    134         if (mListener != null) {
    135             mListener.onCreateOutgoingConnectionFailed(request);
    136         }
    137     }
    138 
    139     public void addConnection(SelfManagedConnection connection) {
    140         Log.i(this, "addConnection %s", connection);
    141         mConnections.add(connection);
    142         if (mListener != null) {
    143             Log.i(this, "addConnection calling onConnectionListChanged %s", connection);
    144             mListener.onConnectionListChanged();
    145         }
    146     }
    147 
    148     public void removeConnection(SelfManagedConnection connection) {
    149         Log.i(this, "removeConnection %s", connection);
    150         mConnections.remove(connection);
    151         if (mListener != null) {
    152             Log.i(this, "removeConnection calling onConnectionListChanged %s", connection);
    153             mListener.onConnectionListChanged();
    154         }
    155     }
    156 
    157     public List<SelfManagedConnection> getConnections() {
    158         return mConnections;
    159     }
    160 
    161     public SelfManagedConnection getConnectionById(int callId) {
    162         Optional<SelfManagedConnection> foundOptional = mConnections.stream()
    163                 .filter((c) -> {return c.getCallId() == callId;})
    164                 .findFirst();
    165         return foundOptional.orElse(null);
    166     }
    167 
    168     public void notifyCallModified() {
    169         if (mListener != null) {
    170             mListener.onConnectionListChanged();
    171         }
    172     }
    173 }
    174