use of android.os.HandlerThread in project android_frameworks_base by ResurrectionRemix.
the class MediaPlayer method addTimedTextSource.
/**
* Adds an external timed text file (FileDescriptor).
*
* It is the caller's responsibility to close the file descriptor.
* It is safe to do so as soon as this call returns.
*
* Currently supported format is SubRip. Note that a single external timed text source may
* contain multiple tracks in it. One can find the total number of available tracks
* using {@link #getTrackInfo()} to see what additional tracks become available
* after this method call.
*
* @param fd the FileDescriptor for the file you want to play
* @param offset the offset into the file where the data to be played starts, in bytes
* @param length the length in bytes of the data to be played
* @param mime The mime type of the file. Must be one of the mime types listed above.
* @throws IllegalArgumentException if the mimeType is not supported.
* @throws IllegalStateException if called in an invalid state.
*/
public void addTimedTextSource(FileDescriptor fd, long offset, long length, String mime) throws IllegalArgumentException, IllegalStateException {
if (!availableMimeTypeForExternalSource(mime)) {
throw new IllegalArgumentException("Illegal mimeType for timed text source: " + mime);
}
final FileDescriptor dupedFd;
try {
dupedFd = Libcore.os.dup(fd);
} catch (ErrnoException ex) {
Log.e(TAG, ex.getMessage(), ex);
throw new RuntimeException(ex);
}
final MediaFormat fFormat = new MediaFormat();
fFormat.setString(MediaFormat.KEY_MIME, mime);
fFormat.setInteger(MediaFormat.KEY_IS_TIMED_TEXT, 1);
// A MediaPlayer created by a VideoView should already have its mSubtitleController set.
if (mSubtitleController == null) {
setSubtitleAnchor();
}
if (!mSubtitleController.hasRendererFor(fFormat)) {
// test and add not atomic
Context context = ActivityThread.currentApplication();
mSubtitleController.registerRenderer(new SRTRenderer(context, mEventHandler));
}
final SubtitleTrack track = mSubtitleController.addTrack(fFormat);
synchronized (mIndexTrackPairs) {
mIndexTrackPairs.add(Pair.<Integer, SubtitleTrack>create(null, track));
}
getMediaTimeProvider();
final long offset2 = offset;
final long length2 = length;
final HandlerThread thread = new HandlerThread("TimedTextReadThread", Process.THREAD_PRIORITY_BACKGROUND + Process.THREAD_PRIORITY_MORE_FAVORABLE);
thread.start();
Handler handler = new Handler(thread.getLooper());
handler.post(new Runnable() {
private int addTrack() {
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
Libcore.os.lseek(dupedFd, offset2, OsConstants.SEEK_SET);
byte[] buffer = new byte[4096];
for (long total = 0; total < length2; ) {
int bytesToRead = (int) Math.min(buffer.length, length2 - total);
int bytes = IoBridge.read(dupedFd, buffer, 0, bytesToRead);
if (bytes < 0) {
break;
} else {
bos.write(buffer, 0, bytes);
total += bytes;
}
}
Handler h = mTimeProvider.mEventHandler;
int what = TimeProvider.NOTIFY;
int arg1 = TimeProvider.NOTIFY_TRACK_DATA;
Pair<SubtitleTrack, byte[]> trackData = Pair.create(track, bos.toByteArray());
Message m = h.obtainMessage(what, arg1, 0, trackData);
h.sendMessage(m);
return MEDIA_INFO_EXTERNAL_METADATA_UPDATE;
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
return MEDIA_INFO_TIMED_TEXT_ERROR;
} finally {
try {
Libcore.os.close(dupedFd);
} catch (ErrnoException e) {
Log.e(TAG, e.getMessage(), e);
}
}
}
public void run() {
int res = addTrack();
if (mEventHandler != null) {
Message m = mEventHandler.obtainMessage(MEDIA_INFO, res, 0, null);
mEventHandler.sendMessage(m);
}
thread.getLooper().quitSafely();
}
});
}
use of android.os.HandlerThread in project android_frameworks_base by ResurrectionRemix.
the class KeyboardUI method start.
@Override
public void start() {
mContext = super.mContext;
HandlerThread thread = new HandlerThread("Keyboard", Process.THREAD_PRIORITY_BACKGROUND);
thread.start();
mHandler = new KeyboardHandler(thread.getLooper());
mHandler.sendEmptyMessage(MSG_INIT);
}
use of android.os.HandlerThread in project android_frameworks_base by ResurrectionRemix.
the class SeekBarVolumizer method start.
public void start() {
// already started
if (mHandler != null)
return;
HandlerThread thread = new HandlerThread(TAG + ".CallbackHandler");
thread.start();
mHandler = new Handler(thread.getLooper(), this);
mHandler.sendEmptyMessage(MSG_INIT_SAMPLE);
mVolumeObserver = new Observer(mHandler);
mContext.getContentResolver().registerContentObserver(System.getUriFor(System.VOLUME_SETTINGS[mStreamType]), false, mVolumeObserver);
mReceiver.setListening(true);
}
use of android.os.HandlerThread in project android_frameworks_base by ResurrectionRemix.
the class StateMachineTest method testStateMachineSharedThread.
@MediumTest
public void testStateMachineSharedThread() throws Exception {
if (DBG)
tlog("testStateMachineSharedThread E");
// Create and start the handler thread
HandlerThread smThread = new HandlerThread("testStateMachineSharedThread");
smThread.start();
// Create the state machines
StateMachineSharedThread[] sms = new StateMachineSharedThread[10];
for (int i = 0; i < sms.length; i++) {
sms[i] = new StateMachineSharedThread("smSharedThread", smThread.getLooper(), sms.length);
sms[i].start();
}
synchronized (waitObject) {
// Send messages to each of the state machines
for (StateMachineSharedThread sm : sms) {
for (int i = 1; i <= 4; i++) {
sm.sendMessage(i);
}
}
// Wait for the last state machine to notify its done
try {
waitObject.wait();
} catch (InterruptedException e) {
tloge("testStateMachineSharedThread: exception while waiting " + e.getMessage());
}
}
for (StateMachineSharedThread sm : sms) {
assertEquals(4, sm.getLogRecCount());
for (int i = 0; i < sm.getLogRecSize(); i++) {
LogRec lr = sm.getLogRec(i);
assertEquals(i + 1, lr.getWhat());
assertEquals(sm.mS1, lr.getState());
assertEquals(sm.mS1, lr.getOriginalState());
}
}
if (DBG)
tlog("testStateMachineSharedThread X");
}
use of android.os.HandlerThread in project android_frameworks_base by ResurrectionRemix.
the class BugreportProgressService method newLooper.
private static Looper newLooper(String name) {
final HandlerThread thread = new HandlerThread(name, THREAD_PRIORITY_BACKGROUND);
thread.start();
return thread.getLooper();
}
Aggregations