Home | History | Annotate | Download | only in sip
      1 /*
      2  * Copyright (C) 2010 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.services.telephony.sip;
     18 
     19 import android.content.BroadcastReceiver;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.net.sip.SipManager;
     23 import android.os.Bundle;
     24 import android.os.UserHandle;
     25 import android.telecom.PhoneAccount;
     26 import android.telecom.PhoneAccountHandle;
     27 import android.telecom.TelecomManager;
     28 import android.util.Log;
     29 
     30 import com.android.phone.PhoneGlobals;
     31 import com.android.server.sip.SipService;
     32 
     33 /**
     34  * Broadcast receiver that handles SIP-related intents.
     35  */
     36 public class SipBroadcastReceiver extends BroadcastReceiver {
     37     private static final String PREFIX = "[SipBroadcastReceiver] ";
     38     private static final boolean VERBOSE = false; /* STOP SHIP if true */
     39 
     40     @Override
     41     public void onReceive(Context context, final Intent intent) {
     42         String action = intent.getAction();
     43 
     44         if (!isRunningInSystemUser()) {
     45             if (VERBOSE) log("SipBroadcastReceiver only run in system user, ignore " + action);
     46             return;
     47         }
     48 
     49         if (!SipUtil.isVoipSupported(context)) {
     50             if (VERBOSE) log("SIP VOIP not supported: " + action);
     51             return;
     52         }
     53 
     54         SipAccountRegistry sipAccountRegistry = SipAccountRegistry.getInstance();
     55         if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
     56             SipUtil.startSipService();
     57         } else if (action.equals(SipManager.ACTION_SIP_INCOMING_CALL)) {
     58             takeCall(context, intent);
     59         } else if (action.equals(SipManager.ACTION_SIP_SERVICE_UP) ||
     60                 action.equals(SipManager.ACTION_SIP_CALL_OPTION_CHANGED)) {
     61             sipAccountRegistry.setup(context);
     62         } else if (action.equals(SipManager.ACTION_SIP_REMOVE_PHONE)) {
     63             if (VERBOSE) log("SIP_REMOVE_PHONE " +
     64                             intent.getStringExtra(SipManager.EXTRA_LOCAL_URI));
     65             sipAccountRegistry.removeSipProfile(intent.getStringExtra(SipManager.EXTRA_LOCAL_URI));
     66         } else {
     67             if (VERBOSE) log("onReceive, action not processed: " + action);
     68         }
     69     }
     70 
     71     private void takeCall(Context context, Intent intent) {
     72         if (VERBOSE) log("takeCall, intent: " + intent);
     73         PhoneAccountHandle accountHandle = null;
     74         try {
     75             accountHandle = intent.getParcelableExtra(SipUtil.EXTRA_PHONE_ACCOUNT);
     76         } catch (ClassCastException e) {
     77             log("takeCall, Bad account handle detected. Bailing!");
     78             return;
     79         }
     80         if (accountHandle != null) {
     81             Bundle extras = new Bundle();
     82             extras.putParcelable(SipUtil.EXTRA_INCOMING_CALL_INTENT, intent);
     83             TelecomManager tm = TelecomManager.from(context);
     84             PhoneAccount phoneAccount = tm.getPhoneAccount(accountHandle);
     85             if(phoneAccount != null && phoneAccount.isEnabled()) {
     86                 tm.addNewIncomingCall(accountHandle, extras);
     87             } else {
     88                 log("takeCall, PhoneAccount is disabled. Not accepting incoming call...");
     89             }
     90         }
     91     }
     92 
     93     private boolean isRunningInSystemUser() {
     94         return UserHandle.myUserId() == UserHandle.USER_SYSTEM;
     95     }
     96 
     97     private static void log(String msg) {
     98         Log.d(SipUtil.LOG_TAG, PREFIX + msg);
     99     }
    100 }
    101