Bug 1072868 - Part 2: Add TelephonyAudioService (implementation). r=hsinyi

This commit is contained in:
Szu-Yu Chen [:aknow] 2014-11-14 15:20:44 +08:00
parent 1f0892bc10
commit 84c39fb6a1
5 changed files with 174 additions and 62 deletions

View File

@ -449,6 +449,8 @@
@BINPATH@/components/RadioInterfaceLayer.js
@BINPATH@/components/RadioInterfaceLayer.manifest
@BINPATH@/components/RILContentHelper.js
@BINPATH@/components/TelephonyAudioService.js
@BINPATH@/components/TelephonyAudioService.manifest
@BINPATH@/components/TelephonyService.js
@BINPATH@/components/TelephonyService.manifest
@BINPATH@/components/VoicemailService.js

View File

@ -0,0 +1,151 @@
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
const nsIAudioManager = Ci.nsIAudioManager;
const nsITelephonyAudioService = Ci.nsITelephonyAudioService;
const NS_PREFBRANCH_PREFCHANGE_TOPIC_ID = "nsPref:changed";
const kPrefRilDebuggingEnabled = "ril.debugging.enabled";
const AUDIO_STATE_NAME = [
"PHONE_STATE_NORMAL",
"PHONE_STATE_RINGTONE",
"PHONE_STATE_IN_CALL"
];
let DEBUG;
function debug(s) {
dump("TelephonyAudioService: " + s + "\n");
}
XPCOMUtils.defineLazyGetter(this, "RIL", function () {
let obj = {};
Cu.import("resource://gre/modules/ril_consts.js", obj);
return obj;
});
XPCOMUtils.defineLazyGetter(this, "gAudioManager", function getAudioManager() {
try {
return Cc["@mozilla.org/telephony/audiomanager;1"]
.getService(nsIAudioManager);
} catch (ex) {
//TODO on the phone this should not fall back as silently.
// Fake nsIAudioManager implementation so that we can run the telephony
// code in a non-Gonk build.
if (DEBUG) debug("Using fake audio manager.");
return {
microphoneMuted: false,
masterVolume: 1.0,
masterMuted: false,
phoneState: nsIAudioManager.PHONE_STATE_CURRENT,
_forceForUse: {},
setForceForUse: function(usage, force) {
this._forceForUse[usage] = force;
},
getForceForUse: function(usage) {
return this._forceForUse[usage] || nsIAudioManager.FORCE_NONE;
}
};
}
});
function TelephonyAudioService() {
this._updateDebugFlag();
Services.prefs.addObserver(kPrefRilDebuggingEnabled, this, false);
}
TelephonyAudioService.prototype = {
classDescription: "TelephonyAudioService",
classID: Components.ID("{c8605499-cfec-4cb5-a082-5f1f56d42adf}"),
contractID: "@mozilla.org/telephony/audio-service;1",
QueryInterface: XPCOMUtils.generateQI([Ci.nsITelephonyAudioService,
Ci.nsIObserver]),
_updateDebugFlag: function() {
try {
DEBUG = RIL.DEBUG_RIL ||
Services.prefs.getBoolPref(kPrefRilDebuggingEnabled);
} catch (e) {}
},
/**
* nsITelephonyAudioService interface.
*/
get microphoneMuted() {
return gAudioManager.microphoneMuted;
},
set microphoneMuted(aMuted) {
if (aMuted == this.microphoneMuted) {
return;
}
gAudioManager.microphoneMuted = aMuted;
},
get speakerEnabled() {
let force = gAudioManager.getForceForUse(nsIAudioManager.USE_COMMUNICATION);
return (force == nsIAudioManager.FORCE_SPEAKER);
},
set speakerEnabled(aEnabled) {
if (aEnabled == this.speakerEnabled) {
return;
}
let force = aEnabled ? nsIAudioManager.FORCE_SPEAKER :
nsIAudioManager.FORCE_NONE;
gAudioManager.setForceForUse(nsIAudioManager.USE_COMMUNICATION, force);
},
setPhoneState: function(aState) {
switch (aState) {
case nsITelephonyAudioService.PHONE_STATE_NORMAL:
gAudioManager.phoneState = nsIAudioManager.PHONE_STATE_NORMAL;
break;
case nsITelephonyAudioService.PHONE_STATE_RINGTONE:
gAudioManager.phoneState = nsIAudioManager.PHONE_STATE_RINGTONE;
break;
case nsITelephonyAudioService.PHONE_STATE_IN_CALL:
gAudioManager.phoneState = nsIAudioManager.PHONE_STATE_IN_CALL;
if (this.speakerEnabled) {
gAudioManager.setForceForUse(nsIAudioManager.USE_COMMUNICATION,
nsIAudioManager.FORCE_SPEAKER);
}
break;
default:
throw new Error("Unknown audioPhoneState: " + aState);
}
if (DEBUG) {
debug("Put audio system into " + AUDIO_STATE_NAME[aState] + ": " +
aState + ", result is: " + gAudioManager.phoneState);
}
},
/**
* nsIObserver interface.
*/
observe: function(aSubject, aTopic, aData) {
switch (aTopic) {
case NS_PREFBRANCH_PREFCHANGE_TOPIC_ID:
if (aData === kPrefRilDebuggingEnabled) {
this._updateDebugFlag();
}
break;
}
}
};
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([TelephonyAudioService]);

View File

@ -0,0 +1,6 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
component {c8605499-cfec-4cb5-a082-5f1f56d42adf} TelephonyAudioService.js
contract @mozilla.org/telephony/audio-service;1 {c8605499-cfec-4cb5-a082-5f1f56d42adf}

View File

@ -33,7 +33,7 @@ const kPrefRilNumRadioInterfaces = "ril.numRadioInterfaces";
const kPrefRilDebuggingEnabled = "ril.debugging.enabled";
const kPrefDefaultServiceId = "dom.telephony.defaultServiceId";
const nsIAudioManager = Ci.nsIAudioManager;
const nsITelephonyAudioService = Ci.nsITelephonyAudioService;
const nsITelephonyService = Ci.nsITelephonyService;
const CALL_WAKELOCK_TIMEOUT = 5000;
@ -45,12 +45,6 @@ const DIAL_ERROR_INVALID_STATE_ERROR = "InvalidStateError";
const DIAL_ERROR_OTHER_CONNECTION_IN_USE = "OtherConnectionInUse";
const DIAL_ERROR_BAD_NUMBER = RIL.GECKO_CALL_ERROR_BAD_NUMBER;
const AUDIO_STATE_NAME = [
"PHONE_STATE_NORMAL",
"PHONE_STATE_RINGTONE",
"PHONE_STATE_IN_CALL"
];
const DEFAULT_EMERGENCY_NUMBERS = ["112", "911"];
// MMI match groups
@ -68,34 +62,6 @@ function debug(s) {
dump("TelephonyService: " + s + "\n");
}
XPCOMUtils.defineLazyGetter(this, "gAudioManager", function getAudioManager() {
try {
return Cc["@mozilla.org/telephony/audiomanager;1"]
.getService(nsIAudioManager);
} catch (ex) {
//TODO on the phone this should not fall back as silently.
// Fake nsIAudioManager implementation so that we can run the telephony
// code in a non-Gonk build.
if (DEBUG) debug("Using fake audio manager.");
return {
microphoneMuted: false,
masterVolume: 1.0,
masterMuted: false,
phoneState: nsIAudioManager.PHONE_STATE_CURRENT,
_forceForUse: {},
setForceForUse: function(usage, force) {
this._forceForUse[usage] = force;
},
getForceForUse: function(usage) {
return this._forceForUse[usage] || nsIAudioManager.FORCE_NONE;
}
};
}
});
XPCOMUtils.defineLazyServiceGetter(this, "gRadioInterfaceLayer",
"@mozilla.org/ril;1",
"nsIRadioInterfaceLayer");
@ -108,6 +74,10 @@ XPCOMUtils.defineLazyServiceGetter(this, "gSystemMessenger",
"@mozilla.org/system-message-internal;1",
"nsISystemMessagesInternal");
XPCOMUtils.defineLazyServiceGetter(this, "gAudioService",
"@mozilla.org/telephony/audio-service;1",
"nsITelephonyAudioService");
XPCOMUtils.defineLazyServiceGetter(this, "gGonkMobileConnectionService",
"@mozilla.org/mobileconnection/mobileconnectionservice;1",
"nsIGonkMobileConnectionService");
@ -279,26 +249,15 @@ TelephonyService.prototype = {
_updateAudioState: function(aAudioState) {
switch (aAudioState) {
case RIL.AUDIO_STATE_NO_CALL:
gAudioManager.phoneState = nsIAudioManager.PHONE_STATE_NORMAL;
gAudioService.setPhoneState(nsITelephonyAudioService.PHONE_STATE_NORMAL);
break;
case RIL.AUDIO_STATE_INCOMING:
gAudioManager.phoneState = nsIAudioManager.PHONE_STATE_RINGTONE;
gAudioService.setPhoneState(nsITelephonyAudioService.PHONE_STATE_RINGTONE);
break;
case RIL.AUDIO_STATE_IN_CALL:
gAudioManager.phoneState = nsIAudioManager.PHONE_STATE_IN_CALL;
if (this.speakerEnabled) {
gAudioManager.setForceForUse(nsIAudioManager.USE_COMMUNICATION,
nsIAudioManager.FORCE_SPEAKER);
}
gAudioService.setPhoneState(nsITelephonyAudioService.PHONE_STATE_IN_CALL);
break;
}
if (DEBUG) {
debug("Put audio system into " + AUDIO_STATE_NAME[aAudioState] + ": " +
aAudioState + ", result is: " + gAudioManager.phoneState);
}
},
_convertRILCallState: function(aState) {
@ -1090,28 +1049,19 @@ TelephonyService.prototype = {
},
get microphoneMuted() {
return gAudioManager.microphoneMuted;
return gAudioService.microphoneMuted;
},
set microphoneMuted(aMuted) {
if (aMuted == this.microphoneMuted) {
return;
}
gAudioManager.microphoneMuted = aMuted;
gAudioService.microphoneMuted = aMuted;
},
get speakerEnabled() {
let force = gAudioManager.getForceForUse(nsIAudioManager.USE_COMMUNICATION);
return (force == nsIAudioManager.FORCE_SPEAKER);
return gAudioService.speakerEnabled;
},
set speakerEnabled(aEnabled) {
if (aEnabled == this.speakerEnabled) {
return;
}
let force = aEnabled ? nsIAudioManager.FORCE_SPEAKER :
nsIAudioManager.FORCE_NONE;
gAudioManager.setForceForUse(nsIAudioManager.USE_COMMUNICATION, force);
gAudioService.speakerEnabled = aEnabled;
},
/**

View File

@ -53,8 +53,11 @@ if CONFIG['MOZ_WIDGET_TOOLKIT'] == 'gonk' and CONFIG['MOZ_B2G_RIL']:
XPCSHELL_TESTS_MANIFESTS += ['test/xpcshell/xpcshell.ini']
XPIDL_SOURCES += [
'nsIGonkTelephonyService.idl',
'nsITelephonyAudioService.idl',
]
EXTRA_COMPONENTS += [
'gonk/TelephonyAudioService.js',
'gonk/TelephonyAudioService.manifest',
'gonk/TelephonyService.js',
'gonk/TelephonyService.manifest',
]