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.timeSeconds,
86                 message);
87     }
88 
89     /**
90      * Retrieves a CallForwardInfo object of type {@link CommandInterface.SERVICE_CLASS_VOICE} from
91      * the array of CallForwardInfo objects. If one does not exist, instantiates an CallForwardInfo
92      * object which disables the specified reason.
93      */
getCallForwardInfo(CallForwardInfo[] infos, int reason)94     public static CallForwardInfo getCallForwardInfo(CallForwardInfo[] infos, int reason) {
95         CallForwardInfo info = null;
96         for (int i = 0 ; i < infos.length; i++) {
97             if (isServiceClassVoice(infos[i])) {
98                 info = infos[i];
99                 break;
100             }
101         }
102 
103         if (info == null) {
104             // If there is  no info, create a CallForwardInfo to disable this reason.
105             info = new CallForwardInfo();
106             info.status = CALL_FORWARD_INFO_INACTIVE_STATUS;
107             info.reason = reason;
108             info.serviceClass = CommandsInterface.SERVICE_CLASS_VOICE;
109 
110             if (DBG) Log.d(LOG_TAG, "Created default info for reason: " + reason);
111         } else {
112             if (!hasForwardingNumber(info)) {
113                 info.status = CALL_FORWARD_INFO_INACTIVE_STATUS;
114             }
115 
116             if (DBG) Log.d(LOG_TAG, "Retrieved  " + info.toString() + " for " + reason);
117         }
118 
119         return info;
120     }
121 
isServiceClassVoice(CallForwardInfo info)122     private static boolean isServiceClassVoice(CallForwardInfo info) {
123         return (info.serviceClass & CommandsInterface.SERVICE_CLASS_VOICE) != 0;
124     }
125 
hasForwardingNumber(CallForwardInfo info)126     private static boolean hasForwardingNumber(CallForwardInfo info) {
127         return info.number != null && info.number.length() > 0;
128     }
129 }
130