Methods Summary |
---|
boolean | isRinging()
synchronized (this) {
return (isRingtonePlaying() || isVibrating());
}
|
private boolean | isRingtonePlaying()
synchronized (this) {
return (mRingtone != null && mRingtone.isPlaying()) ||
(mRingHandler != null && mRingHandler.hasMessages(PLAY_RING_ONCE));
}
|
private boolean | isVibrating()
synchronized (this) {
return (mVibratorThread != null);
}
|
private static void | log(java.lang.String msg)
Log.d(LOG_TAG, msg);
|
private void | makeLooper()
if (mRingThread == null) {
mRingThread = new Worker("ringer");
mRingHandler = new Handler(mRingThread.getLooper()) {
@Override
public void handleMessage(Message msg) {
Ringtone r = null;
switch (msg.what) {
case PLAY_RING_ONCE:
if (DBG) log("mRingHandler: PLAY_RING_ONCE...");
if (mRingtone == null && !hasMessages(STOP_RING)) {
// create the ringtone with the uri
if (DBG) log("creating ringtone: " + mCustomRingtoneUri);
r = RingtoneManager.getRingtone(mContext, mCustomRingtoneUri);
synchronized (Ringer.this) {
if (!hasMessages(STOP_RING)) {
mRingtone = r;
}
}
}
r = mRingtone;
if (r != null && !hasMessages(STOP_RING)) {
PhoneUtils.setAudioMode(mContext, AudioManager.MODE_RINGTONE);
r.play();
synchronized (Ringer.this) {
mRingPending = false;
if (mFirstRingStartTime < 0) {
mFirstRingStartTime = SystemClock.elapsedRealtime();
}
}
}
break;
case STOP_RING:
if (DBG) log("mRingHandler: STOP_RING...");
r = (Ringtone) msg.obj;
if (r != null) {
r.stop();
} else {
if (DBG) log("- STOP_RING with null ringtone! msg = " + msg);
}
getLooper().quit();
break;
}
}
};
}
|
void | ring()Starts the ringtone and/or vibrator
if (DBG) log("ring()...");
synchronized (this) {
try {
mHardwareService.setAttentionLight(true);
} catch (RemoteException ex) {
// the other end of this binder call is in the system process.
}
if (shouldVibrate() && mVibratorThread == null) {
mContinueVibrating = true;
mVibratorThread = new VibratorThread();
if (DBG) log("- starting vibrator...");
mVibratorThread.start();
}
AudioManager audioManager =
(AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
if (audioManager.getStreamVolume(AudioManager.STREAM_RING) == 0) {
if (DBG) log("skipping ring because volume is zero");
return;
}
if (!isRingtonePlaying() && !mRingPending) {
makeLooper();
mRingHandler.removeCallbacksAndMessages(null);
mRingPending = true;
if (mFirstRingEventTime < 0) {
mFirstRingEventTime = SystemClock.elapsedRealtime();
mRingHandler.sendEmptyMessage(PLAY_RING_ONCE);
} else {
// For repeat rings, figure out by how much to delay
// the ring so that it happens the correct amount of
// time after the previous ring
if (mFirstRingStartTime > 0) {
// Delay subsequent rings by the delta between event
// and play time of the first ring
if (DBG) {
log("delaying ring by " + (mFirstRingStartTime - mFirstRingEventTime));
}
mRingHandler.sendEmptyMessageDelayed(PLAY_RING_ONCE,
mFirstRingStartTime - mFirstRingEventTime);
} else {
// We've gotten two ring events so far, but the ring
// still hasn't started. Reset the event time to the
// time of this event to maintain correct spacing.
mFirstRingEventTime = SystemClock.elapsedRealtime();
}
}
} else {
if (DBG) log("skipping ring, already playing or pending: "
+ mRingtone + "/" + mRingHandler);
}
}
|
void | setCustomRingtoneUri(android.net.Uri uri)Sets the ringtone uri in preparation for ringtone creation
in makeLooper(). This uri is defaulted to the phone-wide
default ringtone.
if (uri != null) {
mCustomRingtoneUri = uri;
}
|
boolean | shouldVibrate()
AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
return audioManager.shouldVibrate(AudioManager.VIBRATE_TYPE_RINGER);
|
void | stopRing()Stops the ringtone and/or vibrator if any of these are actually
ringing/vibrating.
synchronized (this) {
if (DBG) log("stopRing()...");
try {
mHardwareService.setAttentionLight(false);
} catch (RemoteException ex) {
// the other end of this binder call is in the system process.
}
if (mRingHandler != null) {
mRingHandler.removeCallbacksAndMessages(null);
Message msg = mRingHandler.obtainMessage(STOP_RING);
msg.obj = mRingtone;
mRingHandler.sendMessage(msg);
PhoneUtils.setAudioMode(mContext, AudioManager.MODE_NORMAL);
mRingThread = null;
mRingHandler = null;
mRingtone = null;
mFirstRingEventTime = -1;
mFirstRingStartTime = -1;
mRingPending = false;
} else {
if (DBG) log("- stopRing: null mRingHandler!");
}
if (mVibratorThread != null) {
if (DBG) log("- stopRing: cleaning up vibrator thread...");
mContinueVibrating = false;
mVibratorThread = null;
}
// Also immediately cancel any vibration in progress.
mVibrator.cancel();
}
|