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.extractNetworkPortion(
77                                    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            Log.e(LOG_TAG,"Invalid CLCC line: '" + line + "'");
94            return null;
95        }
96
97        return ret;
98    }
99
100    public
101    DriverCall() {
102    }
103
104    public String
105    toString() {
106        return "id=" + index + ","
107                + state + ","
108                + "toa=" + TOA + ","
109                + (isMpty ? "conf" : "norm") + ","
110                + (isMT ? "mt" : "mo") + ","
111                + als + ","
112                + (isVoice ? "voc" : "nonvoc") + ","
113                + (isVoicePrivacy ? "evp" : "noevp") + ","
114                /*+ "number=" + number */ + ",cli=" + numberPresentation + ","
115                /*+ "name="+ name */ + "," + namePresentation;
116    }
117
118    public static State
119    stateFromCLCC(int state) throws ATParseEx {
120        switch(state) {
121            case 0: return State.ACTIVE;
122            case 1: return State.HOLDING;
123            case 2: return State.DIALING;
124            case 3: return State.ALERTING;
125            case 4: return State.INCOMING;
126            case 5: return State.WAITING;
127            default:
128                throw new ATParseEx("illegal call state " + state);
129        }
130    }
131
132    public static int
133    presentationFromCLIP(int cli) throws ATParseEx
134    {
135        switch(cli) {
136            case 0: return Connection.PRESENTATION_ALLOWED;
137            case 1: return Connection.PRESENTATION_RESTRICTED;
138            case 2: return Connection.PRESENTATION_UNKNOWN;
139            case 3: return Connection.PRESENTATION_PAYPHONE;
140            default:
141                throw new ATParseEx("illegal presentation " + cli);
142        }
143    }
144
145    //***** Comparable Implementation
146
147    /** For sorting by index */
148    public int
149    compareTo (Object o) {
150        DriverCall dc;
151
152        dc = (DriverCall)o;
153
154        if (index < dc.index) {
155            return -1;
156        } else if (index == dc.index) {
157            return 0;
158        } else { /*index > dc.index*/
159            return 1;
160        }
161    }
162}
163