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
17package com.android.internal.telephony;
18//import com.android.internal.telephony.*;
19import android.util.Log;
20import java.lang.Comparable;
21import android.telephony.PhoneNumberUtils;
22
23/**
24 * {@hide}
25 */
26public class DriverCall implements Comparable {
27    static final String LOG_TAG = "RILB";
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
53    /** returns null on error */
54    static DriverCall
55    fromCLCCLine(String line) {
56        DriverCall ret = new DriverCall();
57
58        //+CLCC: 1,0,2,0,0,\"+18005551212\",145
59        //     index,isMT,state,mode,isMpty(,number,TOA)?
60        ATResponseParser p = new ATResponseParser(line);
61
62        try {
63            ret.index = p.nextInt();
64            ret.isMT = p.nextBoolean();
65            ret.state = stateFromCLCC(p.nextInt());
66
67            ret.isVoice = (0 == p.nextInt());
68            ret.isMpty = p.nextBoolean();
69
70            // use ALLOWED as default presentation while parsing CLCC
71            ret.numberPresentation = Connection.PRESENTATION_ALLOWED;
72
73            if (p.hasMore()) {
74                // Some lame implementations return strings
75                // like "NOT AVAILABLE" in the CLCC line
76                ret.number = PhoneNumberUtils.extractNetworkPortionAlt(p.nextString());
77
78                if (ret.number.length() == 0) {
79                    ret.number = null;
80                }
81
82                ret.TOA = p.nextInt();
83
84                // Make sure there's a leading + on addresses with a TOA
85                // of 145
86
87                ret.number = PhoneNumberUtils.stringFromStringAndTOA(
88                                ret.number, ret.TOA);
89
90            }
91        } catch (ATParseEx ex) {
92            Log.e(LOG_TAG,"Invalid CLCC line: '" + line + "'");
93            return null;
94        }
95
96        return ret;
97    }
98
99    public
100    DriverCall() {
101    }
102
103    public String
104    toString() {
105        return "id=" + index + ","
106                + state + ","
107                + "toa=" + TOA + ","
108                + (isMpty ? "conf" : "norm") + ","
109                + (isMT ? "mt" : "mo") + ","
110                + als + ","
111                + (isVoice ? "voc" : "nonvoc") + ","
112                + (isVoicePrivacy ? "evp" : "noevp") + ","
113                /*+ "number=" + number */ + ",cli=" + numberPresentation + ","
114                /*+ "name="+ name */ + "," + namePresentation;
115    }
116
117    public static State
118    stateFromCLCC(int state) throws ATParseEx {
119        switch(state) {
120            case 0: return State.ACTIVE;
121            case 1: return State.HOLDING;
122            case 2: return State.DIALING;
123            case 3: return State.ALERTING;
124            case 4: return State.INCOMING;
125            case 5: return State.WAITING;
126            default:
127                throw new ATParseEx("illegal call state " + state);
128        }
129    }
130
131    public static int
132    presentationFromCLIP(int cli) throws ATParseEx
133    {
134        switch(cli) {
135            case 0: return Connection.PRESENTATION_ALLOWED;
136            case 1: return Connection.PRESENTATION_RESTRICTED;
137            case 2: return Connection.PRESENTATION_UNKNOWN;
138            case 3: return Connection.PRESENTATION_PAYPHONE;
139            default:
140                throw new ATParseEx("illegal presentation " + cli);
141        }
142    }
143
144    //***** Comparable Implementation
145
146    /** For sorting by index */
147    public int
148    compareTo (Object o) {
149        DriverCall dc;
150
151        dc = (DriverCall)o;
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