1 /*
2  * Copyright (C) 2006 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.internal.telephony;
18 
19 import android.telephony.Rlog;
20 import java.lang.Comparable;
21 import android.telephony.PhoneNumberUtils;
22 
23 /**
24  * {@hide}
25  */
26 public class DriverCall implements Comparable<DriverCall> {
27     static final String LOG_TAG = "DriverCall";
28 
29     public enum State {
30         ACTIVE,
31         HOLDING,
32         DIALING,    // MO call only
33         ALERTING,   // MO call only
34         INCOMING,   // MT call only
35         WAITING;    // MT call only
36         // If you add a state, make sure to look for the switch()
37         // statements that use this enum
38     }
39 
40     public int index;
41     public boolean isMT;
42     public State state;     // May be null if unavail
43     public boolean isMpty;
44     public String number;
45     public int TOA;
46     public boolean isVoice;
47     public boolean isVoicePrivacy;
48     public int als;
49     public int numberPresentation;
50     public String name;
51     public int namePresentation;
52     public UUSInfo uusInfo;
53 
54     /** returns null on error */
55     static DriverCall
fromCLCCLine(String line)56     fromCLCCLine(String line) {
57         DriverCall ret = new DriverCall();
58 
59         //+CLCC: 1,0,2,0,0,\"+18005551212\",145
60         //     index,isMT,state,mode,isMpty(,number,TOA)?
61         ATResponseParser p = new ATResponseParser(line);
62 
63         try {
64             ret.index = p.nextInt();
65             ret.isMT = p.nextBoolean();
66             ret.state = stateFromCLCC(p.nextInt());
67 
68             ret.isVoice = (0 == p.nextInt());
69             ret.isMpty = p.nextBoolean();
70 
71             // use ALLOWED as default presentation while parsing CLCC
72             ret.numberPresentation = PhoneConstants.PRESENTATION_ALLOWED;
73 
74             if (p.hasMore()) {
75                 // Some lame implementations return strings
76                 // like "NOT AVAILABLE" in the CLCC line
77                 ret.number = PhoneNumberUtils.extractNetworkPortionAlt(p.nextString());
78 
79                 if (ret.number.length() == 0) {
80                     ret.number = null;
81                 }
82 
83                 ret.TOA = p.nextInt();
84 
85                 // Make sure there's a leading + on addresses with a TOA
86                 // of 145
87 
88                 ret.number = PhoneNumberUtils.stringFromStringAndTOA(
89                                 ret.number, ret.TOA);
90 
91             }
92         } catch (ATParseEx ex) {
93             Rlog.e(LOG_TAG,"Invalid CLCC line: '" + line + "'");
94             return null;
95         }
96 
97         return ret;
98     }
99 
100     public
DriverCall()101     DriverCall() {
102     }
103 
104     @Override
105     public String
toString()106     toString() {
107         return "id=" + index + ","
108                 + state + ","
109                 + "toa=" + TOA + ","
110                 + (isMpty ? "conf" : "norm") + ","
111                 + (isMT ? "mt" : "mo") + ","
112                 + als + ","
113                 + (isVoice ? "voc" : "nonvoc") + ","
114                 + (isVoicePrivacy ? "evp" : "noevp") + ","
115                 /*+ "number=" + number */ + ",cli=" + numberPresentation + ","
116                 /*+ "name="+ name */ + "," + namePresentation;
117     }
118 
119     public static State
stateFromCLCC(int state)120     stateFromCLCC(int state) throws ATParseEx {
121         switch(state) {
122             case 0: return State.ACTIVE;
123             case 1: return State.HOLDING;
124             case 2: return State.DIALING;
125             case 3: return State.ALERTING;
126             case 4: return State.INCOMING;
127             case 5: return State.WAITING;
128             default:
129                 throw new ATParseEx("illegal call state " + state);
130         }
131     }
132 
133     public static int
presentationFromCLIP(int cli)134     presentationFromCLIP(int cli) throws ATParseEx
135     {
136         switch(cli) {
137             case 0: return PhoneConstants.PRESENTATION_ALLOWED;
138             case 1: return PhoneConstants.PRESENTATION_RESTRICTED;
139             case 2: return PhoneConstants.PRESENTATION_UNKNOWN;
140             case 3: return PhoneConstants.PRESENTATION_PAYPHONE;
141             default:
142                 throw new ATParseEx("illegal presentation " + cli);
143         }
144     }
145 
146     //***** Comparable Implementation
147 
148     /** For sorting by index */
149     @Override
150     public int
compareTo(DriverCall dc)151     compareTo(DriverCall dc) {
152 
153         if (index < dc.index) {
154             return -1;
155         } else if (index == dc.index) {
156             return 0;
157         } else { /*index > dc.index*/
158             return 1;
159         }
160     }
161 }
162