[Java] Catch Thread Exception

Java has a annoying feature that if any Exception happens, you must report it as throws XXXException.

If you want to run something in a Thread, but still want to catch Exception outside that Thread.

1
2
3
4
5
6
7
8
9
10
    ExecutorService executor = Executors.newCachedThreadPool();
    try {
        executor.execute(new Runnable() {
            public void run () {
                // code might throw Throwable or any other Exceptions.
            }
        });
    } catch (Throwable e) {
            // handling exception here.
    }

[Android] Camera callback not be invoked

The Problem

今天用 android.hardware.Camera 写了个简单的拍照函数,代码大致如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
    private void doTakePicture(int cameraId) throws Exception {
        Camera camera = Camera.open(cameraId);
        camera.lock();

        camera.setPreviewDisplay(mActivity.getSurfaceHolder());
        camera.startPreview();

        mMonitor.reset();

        PictureCallback jpegCallback = new PictureCallback() {
            /* callback to save the picture we take */
            @Override
            public void onPictureTaken(byte[] data, Camera camera) {
                try {
                    FileOutputStream fos = new FileOutputStream(mOutFile);
                    fos.write(data);
                    fos.close();
                    Log.i(TAG, "notify take picture done");
                    mMonitor.signal();  // notify done
                } catch (FileNotFoundException e) {
                    Log.e(TAG, "File not found: " + e.getMessage());
                } catch (IOException e) {
                    Log.e(TAG, "IO Exception: " + e.getMessage());
                }
            }
        };

        camera.takePicture(null, null, jpegCallback);

        // wait at most 4s for picture taken done
        boolean success = mMonitor.waitForSignal(4000);
        assertTrue(success);

        camera.stopPreview();
        camera.unlock();
        camera.release();
        camera = null;
    }

[Android] Force route audio stream to wired headset

I want to force route audio stream to headphone for some tests, no matter headset is on or off.

API has been deprecrated

Andoird's AudioManager has such API before, called setRouting , but it has deprecated for quite a long time. Currently, the available APIs can't meet my demands.

public void setRouting (int mode, int routes, int mask)
This method was deprecated in API level 4. Do not set audio routing directly, use setSpeakerphoneOn(), setBluetoothScoOn() methods instead.

[Android] The Story of MediaPlayer.setDataSource [Part.1]

Android MediaPlayer is a big class for all purpose media type playing, including local file and streaming playing.

The story start from MediaPlayer.setDataSource.

android.media package

android.media package locates in frameworks/base/media, and it's Java source is java/android/media.

This package provide various of interface for media playing/recording/decoding etc, including the MediaPlayer we are going to talk about today.

MediaPlayer.setDataSource()

I would not talk much about MediaPlayer constrution, the part we are insterested is what happens after calling setDataSource()

From developer.android, MediaPlayer provide 4 types of method reloading for setDataSource:

1
2
3
4
5
void setDataSource(String path)
void setDataSource(Context context, Uri uri, Map<String, String> headers)
void setDataSource(Context context, Uri uri)
void setDataSource(FileDescriptor fd, long offset, long length)
void setDataSource(FileDescriptor fd)

[Android] Bypass SSL cert check when connecting to localhost through HTTPS

最近需要用HTTPS连接到localhost做一些测试,但是发现一个SSL证书的问题, 用Android的MediaPlayer播放Https内容的时候总是失败在SSL连接握手阶段,应该是localhost证书非法,验证不了,直接导致MediaPlayer拒绝连接。

1
2
javax.net.ssl.SSLProtocolException: SSL handshake terminated: ssl=0xef44d200: Failure in SSL library, usually a protocol error
error:14094416:SSL routines:SSL3_READ_BYTES:sslv3 alert certificate unknown (external/openssl/ssl/s3_pkt.c:1286 0xef40c640:0x00000003)

Level of Security

对于连接的安全级别,可以分为下列3个级别:

  • Level A – no encryption, no verification
  • Level B – encryption, no verification
  • Level C – encryption and verification

Level A 就是相当于http连接,没有任何保护;而Level C就相当于https连接,连接加密且验证连接方。

而对于媒体播放的需求其实更接近于是Level B,只需要传输数据加密,不让别人知道自己看过什么,而对播放源验证不会有那么严格的要求。