Bug 1093014 - Part 1: Refactoring. r=hsinyi

This commit is contained in:
Szu-Yu Chen [:aknow] 2014-12-11 16:27:44 +08:00
parent fc989d5fd2
commit 4330523a3b

View File

@ -222,10 +222,9 @@ const TELEPHONY_REQUESTS = [
REQUEST_UDUB
];
function TelephonyRequestEntry(request, action, options) {
function TelephonyRequestEntry(request, callback) {
this.request = request;
this.action = action;
this.options = options;
this.callback = callback;
}
function TelephonyRequestQueue(ril) {
@ -274,7 +273,7 @@ TelephonyRequestQueue.prototype = {
_executeEntry: function(entry) {
if (DEBUG) this.debug("execute " + this._getRequestName(entry.request));
entry.action.call(this.ril, entry.options);
entry.callback();
},
_getRequestName: function(request) {
@ -290,7 +289,7 @@ TelephonyRequestQueue.prototype = {
return TELEPHONY_REQUESTS.indexOf(request) !== -1;
},
push: function(request, action, options) {
push: function(request, callback) {
if (!this.isValidRequest(request)) {
if (DEBUG) {
this.debug("Error: " + this._getRequestName(request) +
@ -300,7 +299,7 @@ TelephonyRequestQueue.prototype = {
}
if (DEBUG) this.debug("push " + this._getRequestName(request));
let entry = new TelephonyRequestEntry(request, action, options);
let entry = new TelephonyRequestEntry(request, callback);
let queue = this._getQueue(request);
queue.push(entry);
@ -1545,18 +1544,6 @@ RilObject.prototype = {
Buf.sendParcel();
},
/**
* Get current calls.
*/
getCurrentCalls: function() {
this.telephonyRequestQueue.push(REQUEST_GET_CURRENT_CALLS,
this.sendRilRequestGetCurrentCalls, null);
},
sendRilRequestGetCurrentCalls: function() {
this.context.Buf.simpleRequest(REQUEST_GET_CURRENT_CALLS);
},
/**
* Get the signal strength.
*/
@ -1631,20 +1618,24 @@ RilObject.prototype = {
let isRadioOff = (this.radioState === GECKO_RADIOSTATE_DISABLED);
if (options.isEmergency) {
options.request = RILQUIRKS_REQUEST_USE_DIAL_EMERGENCY_CALL ?
REQUEST_DIAL_EMERGENCY_CALL : REQUEST_DIAL;
if (isRadioOff) {
if (DEBUG) {
this.context.debug("Automatically enable radio for an emergency call.");
}
this.cachedDialRequest = {
callback: this.dialEmergencyNumber.bind(this, options),
callback: this.dialInternal.bind(this, options),
onerror: onerror
};
this.setRadioEnabled({enabled: true});
return;
}
this.dialEmergencyNumber(options);
this.dialInternal(options);
} else {
// Notify error in establishing the call without radio.
if (isRadioOff) {
@ -1658,70 +1649,39 @@ RilObject.prototype = {
return;
}
this.dialNonEmergencyNumber(options);
// Exit emergency callback mode when user dial a non-emergency call.
if (this._isInEmergencyCbMode) {
this.exitEmergencyCbMode();
}
options.request = REQUEST_DIAL;
this.dialInternal(options);
}
},
/**
* Dial a non-emergency number.
*
* @param number
* String containing the number to dial.
* @param clirMode
* Integer for showing/hidding the caller Id to the called party.
* @param uusInfo
* Integer doing something XXX TODO
*/
dialNonEmergencyNumber: function(options) {
// Exit emergency callback mode when user dial a non-emergency call.
if (this._isInEmergencyCbMode) {
this.exitEmergencyCbMode();
}
options.request = REQUEST_DIAL;
this.sendDialRequest(options);
},
/**
* Dial an emergency number.
*
* @param number
* String containing the number to dial.
* @param clirMode
* Integer for showing/hidding the caller Id to the called party.
* @param uusInfo
* Integer doing something XXX TODO
*/
dialEmergencyNumber: function(options) {
options.request = RILQUIRKS_REQUEST_USE_DIAL_EMERGENCY_CALL ?
REQUEST_DIAL_EMERGENCY_CALL : REQUEST_DIAL;
this.sendDialRequest(options);
},
sendDialRequest: function(options) {
dialInternal: function(options) {
// Make a Cdma 3way call.
if (this._isCdma && Object.keys(this.currentCalls).length == 1) {
// Make a Cdma 3way call.
options.featureStr = options.number;
this.sendCdmaFlashCommand(options);
} else {
this.telephonyRequestQueue.push(options.request, this.sendRilRequestDial,
options);
this.cdmaFlash(options);
return;
}
this.telephonyRequestQueue.push(options.request, () => {
let Buf = this.context.Buf;
Buf.newParcel(options.request, options);
Buf.writeString(options.number);
Buf.writeInt32(options.clirMode || 0);
Buf.writeInt32(options.uusInfo || 0);
// TODO Why do we need this extra 0? It was put it in to make this
// match the format of the binary message.
Buf.writeInt32(0);
Buf.sendParcel();
});
},
sendRilRequestDial: function(options) {
let Buf = this.context.Buf;
Buf.newParcel(options.request, options);
Buf.writeString(options.number);
Buf.writeInt32(options.clirMode || 0);
Buf.writeInt32(options.uusInfo || 0);
// TODO Why do we need this extra 0? It was put it in to make this
// match the format of the binary message.
Buf.writeInt32(0);
Buf.sendParcel();
},
sendCdmaFlashCommand: function(options) {
cdmaFlash: function(options) {
let Buf = this.context.Buf;
options.isCdma = true;
options.request = REQUEST_CDMA_FLASH;
@ -1753,58 +1713,43 @@ RilObject.prototype = {
call.hangUpLocal = true;
if (call.state === CALL_STATE_HOLDING) {
this.sendHangUpBackgroundRequest();
this.hangUpBackground(options);
} else {
this.sendHangUpRequest(options);
this.telephonyRequestQueue.push(REQUEST_HANGUP, () => {
let Buf = this.context.Buf;
Buf.newParcel(REQUEST_HANGUP, options);
Buf.writeInt32(1);
Buf.writeInt32(options.callIndex);
Buf.sendParcel();
});
}
},
sendHangUpRequest: function(options) {
this.telephonyRequestQueue.push(REQUEST_HANGUP, this.sendRilRequestHangUp,
options);
hangUpForeground: function(options) {
this.telephonyRequestQueue.push(REQUEST_HANGUP_FOREGROUND_RESUME_BACKGROUND, () => {
this.context.Buf.simpleRequest(REQUEST_HANGUP_FOREGROUND_RESUME_BACKGROUND,
options);
});
},
sendRilRequestHangUp: function(options) {
let Buf = this.context.Buf;
Buf.newParcel(REQUEST_HANGUP, options);
Buf.writeInt32(1);
Buf.writeInt32(options.callIndex);
Buf.sendParcel();
hangUpBackground: function(options) {
this.telephonyRequestQueue.push(REQUEST_HANGUP_WAITING_OR_BACKGROUND, () => {
this.context.Buf.simpleRequest(REQUEST_HANGUP_WAITING_OR_BACKGROUND,
options);
});
},
sendHangUpForegroundRequest: function(options) {
this.telephonyRequestQueue.push(REQUEST_HANGUP_FOREGROUND_RESUME_BACKGROUND,
this.sendRilRequestHangUpForeground,
options);
switchActiveCall: function(options) {
this.telephonyRequestQueue.push(REQUEST_SWITCH_WAITING_OR_HOLDING_AND_ACTIVE, () => {
this.context.Buf.simpleRequest(REQUEST_SWITCH_WAITING_OR_HOLDING_AND_ACTIVE,
options);
});
},
sendRilRequestHangUpForeground: function(options) {
this.context.Buf.simpleRequest(REQUEST_HANGUP_FOREGROUND_RESUME_BACKGROUND,
options);
},
sendHangUpBackgroundRequest: function(options) {
this.telephonyRequestQueue.push(REQUEST_HANGUP_WAITING_OR_BACKGROUND,
this.sendRilRequestHangUpWaiting, options);
},
sendRilRequestHangUpWaiting: function(options) {
this.context.Buf.simpleRequest(REQUEST_HANGUP_WAITING_OR_BACKGROUND,
options);
},
/**
* Mute or unmute the radio.
*
* @param mute
* Boolean to indicate whether to mute or unmute the radio.
*/
setMute: function(options) {
let Buf = this.context.Buf;
Buf.newParcel(REQUEST_SET_MUTE);
Buf.writeInt32(1);
Buf.writeInt32(options.muted ? 1 : 0);
Buf.sendParcel();
udub: function(options) {
this.telephonyRequestQueue.push(REQUEST_UDUB, () => {
this.context.Buf.simpleRequest(REQUEST_UDUB, options);
});
},
/**
@ -1814,40 +1759,28 @@ RilObject.prototype = {
* Call index of the call to answer.
*/
answerCall: function(options) {
// Check for races. Since we dispatched the incoming/waiting call
// notification the incoming/waiting call may have changed. The main
// thread thinks that it is answering the call with the given index,
// so only answer if that is still incoming/waiting.
let call = this.currentCalls[options.callIndex];
if (!call) {
return;
}
// Check for races. Since we dispatched the incoming/waiting call
// notification the incoming/waiting call may have changed. The main
// thread thinks that it is answering the call with the given index,
// so only answer if that is still incoming/waiting.
switch (call.state) {
case CALL_STATE_INCOMING:
this.telephonyRequestQueue.push(REQUEST_ANSWER, this.sendRilRequestAnswer,
null);
this.telephonyRequestQueue.push(REQUEST_ANSWER, () => {
this.context.Buf.simpleRequest(REQUEST_ANSWER);
});
break;
case CALL_STATE_WAITING:
// Answer the waiting (second) call, and hold the first call.
this.sendSwitchWaitingRequest();
this.switchActiveCall(options);
break;
}
},
sendRilRequestAnswer: function() {
this.context.Buf.simpleRequest(REQUEST_ANSWER);
},
sendSwitchWaitingRequest: function() {
this.telephonyRequestQueue.push(REQUEST_SWITCH_WAITING_OR_HOLDING_AND_ACTIVE,
this.sendRilRequestSwitch, null);
},
sendRilRequestSwitch: function() {
this.context.Buf.simpleRequest(REQUEST_SWITCH_WAITING_OR_HOLDING_AND_ACTIVE);
},
/**
* Reject an incoming/waiting call.
*
@ -1868,26 +1801,21 @@ RilObject.prototype = {
if (this._isCdma) {
// AT+CHLD=0 means "release held or UDUB."
this.sendHangUpBackgroundRequest();
this.hangUpBackground(options);
return;
}
switch (call.state) {
case CALL_STATE_INCOMING:
this.telephonyRequestQueue.push(REQUEST_UDUB, this.sendRilRequestUdub,
null);
this.udub(options);
break;
case CALL_STATE_WAITING:
// Reject the waiting (second) call, and remain the first call.
this.sendHangUpBackgroundRequest();
this.hangUpBackground(options);
break;
}
},
sendRilRequestUdub: function() {
this.context.Buf.simpleRequest(REQUEST_UDUB);
},
holdCall: function(options) {
let call = this.currentCalls[options.callIndex];
if (!call) {
@ -1900,9 +1828,9 @@ RilObject.prototype = {
let Buf = this.context.Buf;
if (this._isCdma) {
options.featureStr = "";
this.sendCdmaFlashCommand(options);
this.cdmaFlash(options);
} else if (call.state == CALL_STATE_ACTIVE) {
this.sendSwitchWaitingRequest();
this.switchActiveCall(options);
}
},
@ -1918,24 +1846,22 @@ RilObject.prototype = {
let Buf = this.context.Buf;
if (this._isCdma) {
options.featureStr = "";
this.sendCdmaFlashCommand(options);
this.cdmaFlash(options);
} else if (call.state == CALL_STATE_HOLDING) {
this.sendSwitchWaitingRequest();
this.switchActiveCall(options);
}
},
conferenceCall: function(options) {
if (this._isCdma) {
options.featureStr = "";
this.sendCdmaFlashCommand(options);
} else {
this.telephonyRequestQueue.push(REQUEST_CONFERENCE,
this.sendRilRequestConference, options);
this.cdmaFlash(options);
return;
}
},
sendRilRequestConference: function(options) {
this.context.Buf.simpleRequest(REQUEST_CONFERENCE, options);
this.telephonyRequestQueue.push(REQUEST_CONFERENCE, () => {
this.context.Buf.simpleRequest(REQUEST_CONFERENCE, options);
});
},
separateCall: function(options) {
@ -1950,20 +1876,17 @@ RilObject.prototype = {
if (this._isCdma) {
options.featureStr = "";
this.sendCdmaFlashCommand(options);
} else {
this.telephonyRequestQueue.push(REQUEST_SEPARATE_CONNECTION,
this.sendRilRequestSeparateConnection,
options);
this.cdmaFlash(options);
return;
}
},
sendRilRequestSeparateConnection: function(options) {
let Buf = this.context.Buf;
Buf.newParcel(REQUEST_SEPARATE_CONNECTION, options);
Buf.writeInt32(1);
Buf.writeInt32(options.callIndex);
Buf.sendParcel();
this.telephonyRequestQueue.push(REQUEST_SEPARATE_CONNECTION, () => {
let Buf = this.context.Buf;
Buf.newParcel(REQUEST_SEPARATE_CONNECTION, options);
Buf.writeInt32(1);
Buf.writeInt32(options.callIndex);
Buf.sendParcel();
});
},
hangUpConference: function(options) {
@ -1976,31 +1899,56 @@ RilObject.prototype = {
this.sendChromeMessage(options);
return;
}
call.hangUpLocal = true;
this.sendHangUpRequest(1);
options.callIndex = 1;
this.hangUp(options);
return;
}
if (this.currentConferenceState === CALL_STATE_ACTIVE) {
this.hangUpForeground(options);
} else {
if (this.currentConferenceState === CALL_STATE_ACTIVE) {
this.sendHangUpForegroundRequest(options);
} else {
this.sendHangUpBackgroundRequest(options);
}
this.hangUpBackground(options);
}
},
holdConference: function() {
holdConference: function(options) {
if (this._isCdma) {
return;
}
this.sendSwitchWaitingRequest();
this.switchActiveCall(options);
},
resumeConference: function() {
resumeConference: function(options) {
if (this._isCdma) {
return;
}
this.sendSwitchWaitingRequest();
this.switchActiveCall(options);
},
/**
* Get current calls.
*/
getCurrentCalls: function() {
this.telephonyRequestQueue.push(REQUEST_GET_CURRENT_CALLS, () => {
this.context.Buf.simpleRequest(REQUEST_GET_CURRENT_CALLS);
});
},
/**
* Mute or unmute the radio.
*
* @param mute
* Boolean to indicate whether to mute or unmute the radio.
*/
setMute: function(options) {
let Buf = this.context.Buf;
Buf.newParcel(REQUEST_SET_MUTE);
Buf.writeInt32(1);
Buf.writeInt32(options.muted ? 1 : 0);
Buf.sendParcel();
},
/**
@ -2687,10 +2635,6 @@ RilObject.prototype = {
return;
}
this.sendRilRequestSendUSSD(options);
},
sendRilRequestSendUSSD: function(options) {
let Buf = this.context.Buf;
Buf.newParcel(REQUEST_SEND_USSD, options);
Buf.writeString(options.ussd);
@ -3936,6 +3880,8 @@ RilObject.prototype = {
* Helpers for processing call state changes.
*/
_processCalls: function(newCalls, failCause) {
if (DEBUG) this.context.debug("_processCalls: " + JSON.stringify(newCalls));
// Let's get the failCause first if there are removed calls. Otherwise, we
// need to trigger another async request when removing call and it cause
// the order of callDisconnected and conferenceCallStateChanged
@ -5464,7 +5410,7 @@ RilObject.prototype[REQUEST_GET_IMSI] = function REQUEST_GET_IMSI(length, option
this.sendChromeMessage(options);
};
RilObject.prototype[REQUEST_HANGUP] = function REQUEST_HANGUP(length, options) {
options.success = options.rilRequestError === 0;
options.success = (options.rilRequestError === 0);
options.errorMsg = RIL_ERROR_TO_GECKO_ERROR[options.rilRequestError];
this.sendChromeMessage(options);
};
@ -5475,6 +5421,9 @@ RilObject.prototype[REQUEST_HANGUP_FOREGROUND_RESUME_BACKGROUND] = function REQU
RilObject.prototype[REQUEST_HANGUP].call(this, length, options);
};
RilObject.prototype[REQUEST_SWITCH_WAITING_OR_HOLDING_AND_ACTIVE] = function REQUEST_SWITCH_WAITING_OR_HOLDING_AND_ACTIVE(length, options) {
options.success = (options.rilRequestError === 0);
options.errorMsg = RIL_ERROR_TO_GECKO_ERROR[options.rilRequestError];
this.sendChromeMessage(options);
};
RilObject.prototype[REQUEST_CONFERENCE] = function REQUEST_CONFERENCE(length, options) {
options.success = (options.rilRequestError === 0);