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     /**
41      * Audio information
42      */
43     /** Unspecified audio codec */
44     public static final int AUDIO_QUALITY_UNSPECIFIED = 0;
45     /** AMR (Narrowband) audio codec */
46     public static final int AUDIO_QUALITY_AMR = 1;
47     /** AMR (Wideband) audio codec */
48     public static final int AUDIO_QUALITY_AMR_WB = 2;
49     /** GSM Enhanced Full-Rate audio codec */
50     public static final int AUDIO_QUALITY_GSM_EFR = 3;
51     /** GSM Full-Rate audio codec */
52     public static final int AUDIO_QUALITY_GSM_FR = 4;
53     /** GSM Half-Rate audio codec */
54     public static final int AUDIO_QUALITY_GSM_HR = 5;
55     /** Enhanced Variable rate codec */
56     public static final int AUDIO_QUALITY_EVRC = 6;
57     /** Enhanced Variable rate codec revision B */
58     public static final int AUDIO_QUALITY_EVRC_B = 7;
59     /** Enhanced Variable rate codec (Wideband) */
60     public static final int AUDIO_QUALITY_EVRC_WB = 8;
61     /** Enhanced Variable rate codec (Narrowband) */
62     public static final int AUDIO_QUALITY_EVRC_NW = 9;
63 
64     public int index;
65     public boolean isMT;
66     public State state;     // May be null if unavail
67     public boolean isMpty;
68     public String number;
69     public int TOA;
70     public boolean isVoice;
71     public boolean isVoicePrivacy;
72     public int als;
73     public int numberPresentation;
74     public String name;
75     public int namePresentation;
76     public UUSInfo uusInfo;
77     public int audioQuality = AUDIO_QUALITY_UNSPECIFIED;
78 
79     /** returns null on error */
80     static DriverCall
fromCLCCLine(String line)81     fromCLCCLine(String line) {
82         DriverCall ret = new DriverCall();
83 
84         //+CLCC: 1,0,2,0,0,\"+18005551212\",145
85         //     index,isMT,state,mode,isMpty(,number,TOA)?
86         ATResponseParser p = new ATResponseParser(line);
87 
88         try {
89             ret.index = p.nextInt();
90             ret.isMT = p.nextBoolean();
91             ret.state = stateFromCLCC(p.nextInt());
92 
93             ret.isVoice = (0 == p.nextInt());
94             ret.isMpty = p.nextBoolean();
95 
96             // use ALLOWED as default presentation while parsing CLCC
97             ret.numberPresentation = PhoneConstants.PRESENTATION_ALLOWED;
98 
99             if (p.hasMore()) {
100                 // Some lame implementations return strings
101                 // like "NOT AVAILABLE" in the CLCC line
102                 ret.number = PhoneNumberUtils.extractNetworkPortionAlt(p.nextString());
103 
104                 if (ret.number.length() == 0) {
105                     ret.number = null;
106                 }
107 
108                 ret.TOA = p.nextInt();
109 
110                 // Make sure there's a leading + on addresses with a TOA
111                 // of 145
112 
113                 ret.number = PhoneNumberUtils.stringFromStringAndTOA(
114                                 ret.number, ret.TOA);
115 
116             }
117         } catch (ATParseEx ex) {
118             Rlog.e(LOG_TAG,"Invalid CLCC line: '" + line + "'");
119             return null;
120         }
121 
122         return ret;
123     }
124 
125     public
DriverCall()126     DriverCall() {
127     }
128 
129     @Override
130     public String
toString()131     toString() {
132         return "id=" + index + ","
133                 + state + ","
134                 + "toa=" + TOA + ","
135                 + (isMpty ? "conf" : "norm") + ","
136                 + (isMT ? "mt" : "mo") + ","
137                 + als + ","
138                 + (isVoice ? "voc" : "nonvoc") + ","
139                 + (isVoicePrivacy ? "evp" : "noevp") + ","
140                 /*+ "number=" + number */ + ",cli=" + numberPresentation + ","
141                 /*+ "name="+ name */ + "," + namePresentation + ","
142                 + "audioQuality=" + audioQuality;
143     }
144 
145     public static State
stateFromCLCC(int state)146     stateFromCLCC(int state) throws ATParseEx {
147         switch(state) {
148             case 0: return State.ACTIVE;
149             case 1: return State.HOLDING;
150             case 2: return State.DIALING;
151             case 3: return State.ALERTING;
152             case 4: return State.INCOMING;
153             case 5: return State.WAITING;
154             default:
155                 throw new ATParseEx("illegal call state " + state);
156         }
157     }
158 
159     public static int
presentationFromCLIP(int cli)160     presentationFromCLIP(int cli) throws ATParseEx
161     {
162         switch(cli) {
163             case 0: return PhoneConstants.PRESENTATION_ALLOWED;
164             case 1: return PhoneConstants.PRESENTATION_RESTRICTED;
165             case 2: return PhoneConstants.PRESENTATION_UNKNOWN;
166             case 3: return PhoneConstants.PRESENTATION_PAYPHONE;
167             default:
168                 throw new ATParseEx("illegal presentation " + cli);
169         }
170     }
171 
172     //***** Comparable Implementation
173 
174     /** For sorting by index */
175     @Override
176     public int
compareTo(DriverCall dc)177     compareTo(DriverCall dc) {
178 
179         if (index < dc.index) {
180             return -1;
181         } else if (index == dc.index) {
182             return 0;
183         } else { /*index > dc.index*/
184             return 1;
185         }
186     }
187 }
188