Search in sources :

Example 91 with HandlerThread

use of android.os.HandlerThread in project android_frameworks_base by DirtyUnicorns.

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();
        }
    });
}
Also used : MediaFormat(android.media.MediaFormat) Context(android.content.Context) Message(android.os.Message) Handler(android.os.Handler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AssetFileDescriptor(android.content.res.AssetFileDescriptor) FileDescriptor(java.io.FileDescriptor) ErrnoException(android.system.ErrnoException) IOException(java.io.IOException) ErrnoException(android.system.ErrnoException) HandlerThread(android.os.HandlerThread) Runnable(java.lang.Runnable) Pair(android.util.Pair)

Example 92 with HandlerThread

use of android.os.HandlerThread in project android_frameworks_base by DirtyUnicorns.

the class MediaPlayer method setSubtitleAnchor.

/**
     * The private version of setSubtitleAnchor is used internally to set mSubtitleController if
     * necessary when clients don't provide their own SubtitleControllers using the public version
     * {@link #setSubtitleAnchor(SubtitleController, Anchor)} (e.g. {@link VideoView} provides one).
     */
private synchronized void setSubtitleAnchor() {
    if (mSubtitleController == null) {
        final HandlerThread thread = new HandlerThread("SetSubtitleAnchorThread");
        thread.start();
        Handler handler = new Handler(thread.getLooper());
        handler.post(new Runnable() {

            @Override
            public void run() {
                Context context = ActivityThread.currentApplication();
                mSubtitleController = new SubtitleController(context, mTimeProvider, MediaPlayer.this);
                mSubtitleController.setAnchor(new Anchor() {

                    @Override
                    public void setSubtitleWidget(RenderingWidget subtitleWidget) {
                    }

                    @Override
                    public Looper getSubtitleLooper() {
                        return Looper.getMainLooper();
                    }
                });
                thread.getLooper().quitSafely();
            }
        });
        try {
            thread.join();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            Log.w(TAG, "failed to join SetSubtitleAnchorThread");
        }
    }
}
Also used : Context(android.content.Context) SubtitleController(android.media.SubtitleController) Anchor(android.media.SubtitleController.Anchor) HandlerThread(android.os.HandlerThread) Runnable(java.lang.Runnable) Handler(android.os.Handler) RenderingWidget(android.media.SubtitleTrack.RenderingWidget)

Example 93 with HandlerThread

use of android.os.HandlerThread in project android_frameworks_base by DirtyUnicorns.

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);
}
Also used : HandlerThread(android.os.HandlerThread)

Example 94 with HandlerThread

use of android.os.HandlerThread in project android_frameworks_base by DirtyUnicorns.

the class ConnectivityServiceTest method testMobileDataAlwaysOn.

@SmallTest
public void testMobileDataAlwaysOn() throws Exception {
    final TestNetworkCallback cellNetworkCallback = new TestNetworkCallback();
    final NetworkRequest cellRequest = new NetworkRequest.Builder().addTransportType(TRANSPORT_CELLULAR).build();
    mCm.registerNetworkCallback(cellRequest, cellNetworkCallback);
    final HandlerThread handlerThread = new HandlerThread("MobileDataAlwaysOnFactory");
    handlerThread.start();
    NetworkCapabilities filter = new NetworkCapabilities().addTransportType(TRANSPORT_CELLULAR).addCapability(NET_CAPABILITY_INTERNET);
    final MockNetworkFactory testFactory = new MockNetworkFactory(handlerThread.getLooper(), mServiceContext, "testFactory", filter);
    testFactory.setScoreFilter(40);
    // Register the factory and expect it to start looking for a network.
    testFactory.expectAddRequests(1);
    testFactory.register();
    testFactory.waitForNetworkRequests(1);
    assertTrue(testFactory.getMyStartRequested());
    // Bring up wifi. The factory stops looking for a network.
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    // Because the default request changes score twice.
    testFactory.expectAddRequests(2);
    mWiFiNetworkAgent.connect(true);
    testFactory.waitForNetworkRequests(1);
    assertFalse(testFactory.getMyStartRequested());
    ContentResolver cr = mServiceContext.getContentResolver();
    // Turn on mobile data always on. The factory starts looking again.
    testFactory.expectAddRequests(1);
    setMobileDataAlwaysOn(true);
    testFactory.waitForNetworkRequests(2);
    assertTrue(testFactory.getMyStartRequested());
    // Bring up cell data and check that the factory stops looking.
    assertEquals(1, mCm.getAllNetworks().length);
    mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
    // Because the cell request changes score twice.
    testFactory.expectAddRequests(2);
    mCellNetworkAgent.connect(true);
    cellNetworkCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    testFactory.waitForNetworkRequests(2);
    // Because the cell network outscores us.
    assertFalse(testFactory.getMyStartRequested());
    // Check that cell data stays up.
    mService.waitForIdle();
    verifyActiveNetwork(TRANSPORT_WIFI);
    assertEquals(2, mCm.getAllNetworks().length);
    // Turn off mobile data always on and expect the request to disappear...
    testFactory.expectRemoveRequests(1);
    setMobileDataAlwaysOn(false);
    testFactory.waitForNetworkRequests(1);
    // ...  and cell data to be torn down.
    cellNetworkCallback.expectCallback(CallbackState.LOST, mCellNetworkAgent);
    assertEquals(1, mCm.getAllNetworks().length);
    testFactory.unregister();
    mCm.unregisterNetworkCallback(cellNetworkCallback);
    handlerThread.quit();
}
Also used : HandlerThread(android.os.HandlerThread) NetworkRequest(android.net.NetworkRequest) NetworkCapabilities(android.net.NetworkCapabilities) ContentResolver(android.content.ContentResolver) MockContentResolver(android.test.mock.MockContentResolver) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 95 with HandlerThread

use of android.os.HandlerThread in project android_frameworks_base by DirtyUnicorns.

the class DrmManagerClient method createEventThreads.

private void createEventThreads() {
    if (mEventHandler == null && mInfoHandler == null) {
        mInfoThread = new HandlerThread("DrmManagerClient.InfoHandler");
        mInfoThread.start();
        mInfoHandler = new InfoHandler(mInfoThread.getLooper());
        mEventThread = new HandlerThread("DrmManagerClient.EventHandler");
        mEventThread.start();
        mEventHandler = new EventHandler(mEventThread.getLooper());
    }
}
Also used : HandlerThread(android.os.HandlerThread)

Aggregations

HandlerThread (android.os.HandlerThread)313 Handler (android.os.Handler)149 IntentFilter (android.content.IntentFilter)34 Message (android.os.Message)26 PowerManager (android.os.PowerManager)26 Intent (android.content.Intent)25 Context (android.content.Context)22 Test (org.junit.Test)20 Runnable (java.lang.Runnable)15 ComponentName (android.content.ComponentName)14 View (android.view.View)14 Looper (android.os.Looper)13 PendingIntent (android.app.PendingIntent)12 MediumTest (android.test.suitebuilder.annotation.MediumTest)12 RemoteException (android.os.RemoteException)11 MediaFormat (android.media.MediaFormat)10 Uri (android.net.Uri)10 Pair (android.util.Pair)10 SmallTest (android.test.suitebuilder.annotation.SmallTest)9 Bundle (android.os.Bundle)8