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");
}
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();
}
}
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;
}
}
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");
}
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");
}
Aggregations