Search in sources :

Example 46 with Handler

use of android.os.Handler in project platform_frameworks_base by android.

the class IconTest method testAsync.

@SmallTest
public void testAsync() throws Exception {
    final Bitmap bit1 = ((BitmapDrawable) getContext().getDrawable(R.drawable.landscape)).getBitmap();
    final File dir = getContext().getExternalFilesDir(null);
    final File file1 = new File(dir, "async-original.png");
    bit1.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(file1));
    final Icon im1 = Icon.createWithFilePath(file1.toString());
    final HandlerThread thd = new HandlerThread("testAsync");
    thd.start();
    final Handler h = new Handler(thd.getLooper());
    L(TAG, "asyncTest: dispatching load to thread: " + thd);
    im1.loadDrawableAsync(mContext, new Icon.OnDrawableLoadedListener() {

        @Override
        public void onDrawableLoaded(Drawable draw1) {
            L(TAG, "asyncTest: thread: loading drawable");
            L(TAG, "asyncTest: thread: loaded: %dx%d", draw1.getIntrinsicWidth(), draw1.getIntrinsicHeight());
            final Bitmap test1 = Bitmap.createBitmap(draw1.getIntrinsicWidth(), draw1.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
            draw1.setBounds(0, 0, test1.getWidth(), test1.getHeight());
            draw1.draw(new Canvas(test1));
            try {
                test1.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(new File(dir, "async-test.png")));
            } catch (java.io.FileNotFoundException ex) {
                fail("couldn't create test file: " + ex);
            }
            if (!equalBitmaps(bit1, test1)) {
                findBitmapDifferences(bit1, test1);
                fail("testAsync: file1 differs, check " + dir);
            }
        }
    }, h);
    L(TAG, "asyncTest: awaiting result");
    // ;_;
    Thread.sleep(500);
    assertTrue("async-test.png does not exist!", new File(dir, "async-test.png").exists());
    L(TAG, "asyncTest: done");
}
Also used : Canvas(android.graphics.Canvas) Handler(android.os.Handler) Bitmap(android.graphics.Bitmap) HandlerThread(android.os.HandlerThread) FileOutputStream(java.io.FileOutputStream) File(java.io.File) Override(java.lang.Override) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 47 with Handler

use of android.os.Handler in project platform_frameworks_base by android.

the class MidiManager method openBluetoothDevice.

/**
     * Opens a Bluetooth MIDI device for reading and writing.
     *
     * @param bluetoothDevice a {@link android.bluetooth.BluetoothDevice} to open as a MIDI device
     * @param listener a {@link MidiManager.OnDeviceOpenedListener} to be called to receive the
     * result
     * @param handler the {@link android.os.Handler Handler} that will be used for delivering
     *                the result. If handler is null, then the thread used for the
     *                listener is unspecified.
     */
public void openBluetoothDevice(BluetoothDevice bluetoothDevice, OnDeviceOpenedListener listener, Handler handler) {
    final OnDeviceOpenedListener listenerF = listener;
    final Handler handlerF = handler;
    IMidiDeviceOpenCallback callback = new IMidiDeviceOpenCallback.Stub() {

        @Override
        public void onDeviceOpened(IMidiDeviceServer server, IBinder deviceToken) {
            MidiDevice device = null;
            if (server != null) {
                try {
                    // fetch MidiDeviceInfo from the server
                    MidiDeviceInfo deviceInfo = server.getDeviceInfo();
                    device = new MidiDevice(deviceInfo, server, mService, mToken, deviceToken);
                } catch (RemoteException e) {
                    Log.e(TAG, "remote exception in getDeviceInfo()");
                }
            }
            sendOpenDeviceResponse(device, listenerF, handlerF);
        }
    };
    try {
        mService.openBluetoothDevice(mToken, bluetoothDevice, callback);
    } catch (RemoteException e) {
        throw e.rethrowFromSystemServer();
    }
}
Also used : IBinder(android.os.IBinder) Handler(android.os.Handler) RemoteException(android.os.RemoteException)

Example 48 with Handler

use of android.os.Handler in project platform_frameworks_base by android.

the class MediaSession method setCallback.

/**
     * Set the callback to receive updates for the MediaSession. This includes
     * media button events and transport controls.
     * <p>
     * Set the callback to null to stop receiving updates.
     *
     * @param callback The callback to receive updates on.
     * @param handler The handler that events should be posted on.
     */
public void setCallback(@Nullable Callback callback, @Nullable Handler handler) {
    synchronized (mLock) {
        if (callback == null) {
            if (mCallback != null) {
                mCallback.mCallback.mSession = null;
            }
            mCallback = null;
            return;
        }
        if (mCallback != null) {
            // We're updating the callback, clear the session from the old
            // one.
            mCallback.mCallback.mSession = null;
        }
        if (handler == null) {
            handler = new Handler();
        }
        callback.mSession = this;
        CallbackMessageHandler msgHandler = new CallbackMessageHandler(handler.getLooper(), callback);
        mCallback = msgHandler;
    }
}
Also used : Handler(android.os.Handler)

Example 49 with Handler

use of android.os.Handler in project platform_frameworks_base by android.

the class CameraStressTest method setUp.

protected void setUp() throws Exception {
    final Semaphore sem = new Semaphore(0);
    mLooperThread = new Thread() {

        @Override
        public void run() {
            Log.v(TAG, "starting looper");
            Looper.prepare();
            mHandler = new Handler();
            sem.release();
            Looper.loop();
            Log.v(TAG, "quit looper");
        }
    };
    mLooperThread.start();
    if (!sem.tryAcquire(WAIT_TIMEOUT, TimeUnit.MILLISECONDS)) {
        fail("Failed to start the looper.");
    }
    getActivity();
    super.setUp();
    mCameraTestHelper = new CameraTestHelper();
    File stressOutFile = new File(String.format("%s/%s", Environment.getExternalStorageDirectory(), CAMERA_STRESS_OUTPUT));
    mOutput = new BufferedWriter(new FileWriter(stressOutFile, true));
    mOutput.write(this.getName() + "\n");
}
Also used : CameraTestHelper(com.android.mediaframeworktest.helpers.CameraTestHelper) FileWriter(java.io.FileWriter) Handler(android.os.Handler) Semaphore(java.util.concurrent.Semaphore) File(java.io.File) BufferedWriter(java.io.BufferedWriter)

Example 50 with Handler

use of android.os.Handler in project platform_frameworks_base by android.

the class MediaRecorderStressTest method setUp.

protected void setUp() throws Exception {
    final Semaphore sem = new Semaphore(0);
    mLooperThread = new Thread() {

        @Override
        public void run() {
            Log.v(TAG, "starting looper");
            Looper.prepare();
            mHandler = new Handler();
            sem.release();
            Looper.loop();
            Log.v(TAG, "quit looper");
        }
    };
    mLooperThread.start();
    if (!sem.tryAcquire(WAIT_TIMEOUT, TimeUnit.MILLISECONDS)) {
        fail("Failed to start the looper.");
    }
    //Insert a 2 second before launching the test activity. This is
    //the workaround for the race condition of requesting the updated surface.
    Thread.sleep(2000);
    getActivity();
    super.setUp();
    File stressOutFile = new File(String.format("%s/%s", Environment.getExternalStorageDirectory(), MEDIA_STRESS_OUTPUT));
    mOutput = new BufferedWriter(new FileWriter(stressOutFile, true));
    mOutput.write(this.getName() + "\n");
}
Also used : FileWriter(java.io.FileWriter) Handler(android.os.Handler) Semaphore(java.util.concurrent.Semaphore) File(java.io.File) BufferedWriter(java.io.BufferedWriter)

Aggregations

Handler (android.os.Handler)1906 Message (android.os.Message)254 View (android.view.View)207 Intent (android.content.Intent)174 HandlerThread (android.os.HandlerThread)156 TextView (android.widget.TextView)118 MediumTest (android.test.suitebuilder.annotation.MediumTest)116 ArrayList (java.util.ArrayList)97 Test (org.junit.Test)85 Context (android.content.Context)71 IntentFilter (android.content.IntentFilter)69 RemoteException (android.os.RemoteException)63 IOException (java.io.IOException)63 ComponentName (android.content.ComponentName)57 ImageView (android.widget.ImageView)57 Bundle (android.os.Bundle)56 RecyclerView (android.support.v7.widget.RecyclerView)54 Looper (android.os.Looper)53 File (java.io.File)46 ContentObserver (android.database.ContentObserver)44