/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.services.telephony;

import android.telecomm.CallInfo;
import android.telecomm.CallServiceDescriptor;
import android.telecomm.CallServiceSelector;

import java.util.ArrayList;
import java.util.List;

/**
 * Decides which call service should be used to place outgoing calls or to switch the call to.
 */
public class TelephonyCallServiceSelector extends CallServiceSelector {

    /** {@inheritDoc} */
    @Override
    protected void select(CallInfo callInfo, List<CallServiceDescriptor> descriptors) {
        ArrayList<CallServiceDescriptor> selectedDescriptors =
                new ArrayList<CallServiceDescriptor>();
        for (CallServiceDescriptor descriptor : descriptors) {
            if (!getPackageName().equals(descriptor.getServiceComponent().getPackageName())) {
                continue;
            }

            String name = descriptor.getServiceComponent().getClassName();
            if (name.equals(CdmaConnectionService.class.getName())) {
                if (CdmaConnectionService.canCall(this, callInfo.getHandle())) {
                    selectedDescriptors.add(descriptor);
                }
            } else if (name.equals(GsmConnectionService.class.getName())) {
                if (GsmConnectionService.canCall(this, callInfo.getHandle())) {
                    selectedDescriptors.add(descriptor);
                }
            } else if (name.equals(SipConnectionService.class.getName())) {
                if (SipConnectionService.canCall(this, callInfo.getHandle())) {
                    selectedDescriptors.add(descriptor);
                }
            }
        }
        getAdapter().setSelectedCallServices(callInfo.getId(), selectedDescriptors);
    }
}

@@ -1256,24 +1256,25 @@
 tor%3E();%0A
+%0A
         
 for (Cal
@@ -1269,13 +1269,8 @@
     
-for (
 Call
@@ -1302,35 +1302,69 @@
 tor 
-: descriptors) %7B%0A  
+= getDescriptor(descriptors, CdmaCallService.class);%0A
         
   if
@@ -1363,117 +1363,149 @@
     
-  
 if (
-!getPackageName().equals(descriptor.getServiceComponent().getPackageName())) %7B%0A                continue
+descriptor != null) %7B%0A            if (CdmaCallService.shouldSelect(this, callInfo)) %7B%0A                selectedDescriptors.add(descriptor)
 ;%0A  
@@ -1516,25 +1516,24 @@
       %7D%0A
-%0A
         
     Stri
@@ -1528,72 +1528,78 @@
     
+%7D%0A
     
-String name = descriptor.getServiceComponent().getClassName(
+    descriptor = getDescriptor(descriptors, GsmCallService.class
 );%0A 
@@ -1609,66 +1609,30 @@
     
- 
-   
 if (
-name.equals(CdmaConnectionService.class.getName())
+descriptor != null
 ) %7B%0A
@@ -1647,45 +1647,39 @@
     
- 
-   
 if (
-CdmaConnectionService.canCall
+GsmCallService.shouldSelect
 (thi
@@ -1689,37 +1689,21 @@
 callInfo
-.getHandle()
 )) %7B%0A
-    
         
@@ -1747,32 +1747,38 @@
 iptor);%0A        
+    %7D%0A
         %7D%0A      
@@ -1783,72 +1783,101 @@
     
-    %7D else if (name.equals(GsmConnectionService.class.getName())
+descriptor = getDescriptor(descriptors, SipCallService.class);%0A        if (descriptor != null
 ) %7B%0A
@@ -1892,44 +1892,39 @@
     
- 
-   
 if (
-GsmConnectionService.canCall
+SipCallService.shouldSelect
 (thi
@@ -1930,36 +1930,24 @@
 is, callInfo
-.getHandle()
 )) %7B%0A       
@@ -1947,36 +1947,32 @@
                 
-    
 selectedDescript
@@ -2004,34 +2004,37 @@
         
+%7D%0A
     
-%7D%0A
     
+%7D%0A%0A
         
 %7D else i
@@ -2029,160 +2029,221 @@
     
-%7D else if (name.equals(SipConnectionService.class.getName())) %7B%0A                if (SipConnectionService.canCall(this, callInfo.getHandle())
+getAdapter().setSelectedCallServices(callInfo.getId(), selectedDescriptors);%0A    %7D%0A%0A    private CallServiceDescriptor getDescriptor(%0A            List%3CCallServiceDescriptor%3E descriptors, Class%3C?%3E clazz
 ) %7B%0A
+%0A
         
     
@@ -2242,56 +2242,157 @@
     
-            selectedDescriptors.add(descriptor);
+for (CallServiceDescriptor descriptor : descriptors) %7B%0A            if (!getPackageName().equals(descriptor.getServiceComponent().getPackageName())) %7B
 %0A   
@@ -2396,33 +2396,41 @@
                 
-%7D
+continue;
 %0A            %7D%0A 
@@ -2424,32 +2424,33 @@
           %7D%0A
+%0A
         
 %7D%0A        ge
@@ -2441,93 +2441,170 @@
     
-%7D%0A
     
-    getAdapter().setSelectedCallServices(callInfo.getId(), selectedDescriptors)
+if (clazz.getName().equals(descriptor.getServiceComponent().getClassName())) %7B%0A                return descriptor;%0A            %7D%0A        %7D%0A%0A        return null
 ;%0A  

Order Call Services explicitly in TelephonySelector.