Home | History | Annotate | Download | only in managedprofile
      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 
     17 package com.android.cts.managedprofile;
     18 
     19 import android.net.Uri;
     20 import android.telecom.Connection;
     21 import android.telecom.ConnectionRequest;
     22 import android.telecom.ConnectionService;
     23 import android.telecom.DisconnectCause;
     24 import android.telecom.PhoneAccountHandle;
     25 import android.telecom.RemoteConference;
     26 import android.telecom.RemoteConnection;
     27 import android.telecom.TelecomManager;
     28 
     29 /**
     30  * A simple connection service that hangs up automatically for incoming and outgoing call.
     31  */
     32 public class DummyConnectionService extends ConnectionService {
     33 
     34     @Override
     35     public Connection onCreateOutgoingConnection(PhoneAccountHandle connectionManagerPhoneAccount,
     36             ConnectionRequest request) {
     37         final DummyConnection connection = new DummyConnection();
     38         connection.setAddress(request.getAddress(), TelecomManager.PRESENTATION_ALLOWED);
     39         connection.setVideoState(request.getVideoState());
     40         hangUpAsync(connection);
     41         return connection;
     42     }
     43 
     44     @Override
     45     public Connection onCreateIncomingConnection(PhoneAccountHandle connectionManagerPhoneAccount,
     46             ConnectionRequest request) {
     47         final DummyConnection connection = new DummyConnection();
     48         connection.setVideoState(request.getVideoState());
     49         final Uri address =
     50                 request.getExtras().getParcelable(TelecomManager.EXTRA_INCOMING_CALL_ADDRESS);
     51         connection.setAddress(address, TelecomManager.PRESENTATION_ALLOWED);
     52         hangUpAsync(connection);
     53         return connection;
     54     }
     55 
     56     @Override
     57     public void onConference(Connection connection1, Connection connection2) {
     58     }
     59 
     60     @Override
     61     public void onRemoteExistingConnectionAdded(RemoteConnection connection) {
     62     }
     63 
     64     @Override
     65     public void onRemoteConferenceAdded(RemoteConference conference) {
     66     }
     67 
     68     public static class DummyConnection extends Connection {
     69 
     70         @Override
     71         public void onAnswer() {
     72             super.onAnswer();
     73         }
     74 
     75         @Override
     76         public void onAnswer(int videoState) {
     77             super.onAnswer(videoState);
     78             setActive();
     79         }
     80 
     81         @Override
     82         public void onReject() {
     83             super.onReject();
     84             setDisconnected(new DisconnectCause(DisconnectCause.REJECTED));
     85             destroy();
     86         }
     87 
     88         @Override
     89         public void onHold() {
     90             super.onHold();
     91             setOnHold();
     92         }
     93 
     94         @Override
     95         public void onUnhold() {
     96             super.onUnhold();
     97             setActive();
     98         }
     99 
    100         @Override
    101         public void onDisconnect() {
    102             super.onDisconnect();
    103             setDisconnected(new DisconnectCause(DisconnectCause.LOCAL));
    104             destroy();
    105         }
    106 
    107         @Override
    108         public void onAbort() {
    109             super.onAbort();
    110             setDisconnected(new DisconnectCause(DisconnectCause.UNKNOWN));
    111             destroy();
    112         }
    113     }
    114 
    115     /**
    116      * Hang up the call after 1 second in a background thread.
    117      * TODO: It is better if we could have a callback to know when we can disconnect the call.
    118      */
    119     private static void hangUpAsync(final Connection connection) {
    120         new Thread(new Runnable() {
    121             @Override
    122             public void run() {
    123                 try {
    124                     Thread.sleep(1000);
    125                     connection.onDisconnect();
    126                 } catch (InterruptedException ex) {
    127                     // let it be
    128                 }
    129             }
    130         }).start();
    131     }
    132 }