1 /**
2  * Copyright (C) 2008 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.phone.settings;
18 
19 import android.os.Message;
20 import android.util.Log;
21 
22 import com.android.internal.telephony.CallForwardInfo;
23 import com.android.internal.telephony.CommandsInterface;
24 import com.android.internal.telephony.Phone;
25 import com.android.phone.PhoneGlobals;
26 
27 public class CallForwardInfoUtil {
28     private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
29     private static final String LOG_TAG = CallForwardInfoUtil.class.getSimpleName();
30 
31     /**
32      * @see CallForwardInfo#status
33      */
34     private static final int CALL_FORWARD_INFO_INACTIVE_STATUS = 0;
35     private static final int CALL_FORWARD_INFO_ACTIVE_STATUS = 1;
36 
37     /**
38      * Returns the first CallForwardInfo in infos which has the specified reason.
39      * @param infos array of CallForwardInfo objects.
40      * @param reason The reason we want to find a CallForwardInfo for.
41      */
infoForReason(CallForwardInfo[] infos, int reason)42     public static CallForwardInfo infoForReason(CallForwardInfo[] infos, int reason) {
43         if (infos == null) {
44             return null;
45         }
46 
47         CallForwardInfo result = null;
48         for (int i = 0; i < infos.length; i++) {
49             if (infos[i].reason == reason) {
50                 return infos[i];
51             }
52         }
53 
54         return null;
55     }
56 
57     /**
58      * Update, unless we're disabling a type of forwarding and it's already disabled.
59      */
isUpdateRequired(CallForwardInfo oldInfo, CallForwardInfo newInfo)60     public static boolean isUpdateRequired(CallForwardInfo oldInfo, CallForwardInfo newInfo) {
61         if (oldInfo == null) {
62             return true;
63         }
64 
65         if (newInfo.status == CALL_FORWARD_INFO_INACTIVE_STATUS
66                 && oldInfo.status == CALL_FORWARD_INFO_INACTIVE_STATUS) {
67             return false;
68         }
69 
70         return true;
71     }
72 
73     /**
74      * Sets the call forwarding option on the phone, with the command interface action set to the
75      * appropriate value depending on whether the CallForwardInfo is active or inactive.
76      */
setCallForwardingOption(Phone phone, CallForwardInfo info, Message message)77     public static void setCallForwardingOption(Phone phone, CallForwardInfo info, Message message) {
78         int commandInterfaceCfAction = info.status == CALL_FORWARD_INFO_ACTIVE_STATUS
79                 ? CommandsInterface.CF_ACTION_REGISTRATION
80                 : CommandsInterface.CF_ACTION_DISABLE;
81 
82         phone.setCallForwardingOption(commandInterfaceCfAction,
83                 info.reason,
84                 info.number,
85                 info.serviceClass,
86                 info.timeSeconds,
87                 message);
88     }
89 
90     /**
91      * Retrieves a CallForwardInfo object of type {@link CommandInterface.SERVICE_CLASS_VOICE} from
92      * the array of CallForwardInfo objects. If one does not exist, instantiates an CallForwardInfo
93      * object which disables the specified reason.
94      */
getCallForwardInfo(CallForwardInfo[] infos, int reason)95     public static CallForwardInfo getCallForwardInfo(CallForwardInfo[] infos, int reason) {
96         CallForwardInfo info = null;
97         if (infos != null) {
98             for (int i = 0 ; i < infos.length; i++) {
99                 if (isServiceClassVoice(infos[i])) {
100                     info = infos[i];
101                     break;
102                 }
103             }
104         }
105 
106         if (info == null) {
107             // If there is  no info, create a CallForwardInfo to disable this reason.
108             info = new CallForwardInfo();
109             info.status = CALL_FORWARD_INFO_INACTIVE_STATUS;
110             info.reason = reason;
111             info.serviceClass = CommandsInterface.SERVICE_CLASS_VOICE;
112 
113             if (DBG) Log.d(LOG_TAG, "Created default info for reason: " + reason);
114         } else {
115             if (!hasForwardingNumber(info)) {
116                 info.status = CALL_FORWARD_INFO_INACTIVE_STATUS;
117             }
118 
119             if (DBG) Log.d(LOG_TAG, "Retrieved  " + info.toString() + " for " + reason);
120         }
121 
122         return info;
123     }
124 
isServiceClassVoice(CallForwardInfo info)125     private static boolean isServiceClassVoice(CallForwardInfo info) {
126         return (info.serviceClass & CommandsInterface.SERVICE_CLASS_VOICE) != 0;
127     }
128 
hasForwardingNumber(CallForwardInfo info)129     private static boolean hasForwardingNumber(CallForwardInfo info) {
130         return info.number != null && info.number.length() > 0;
131     }
132 }
133