Search in sources :

Example 56 with Messenger

use of android.os.Messenger in project android_frameworks_base by ResurrectionRemix.

the class ScreenrecordTile method takeScreenRecord.

private void takeScreenRecord() {
    synchronized (mScreenrecordLock) {
        if (mScreenrecordConnection != null) {
            return;
        }
        Intent intent = new Intent(mContext, TakeScreenrecordService.class);
        ServiceConnection conn = new ServiceConnection() {

            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                synchronized (mScreenrecordLock) {
                    if (mScreenrecordConnection != this) {
                        return;
                    }
                    Messenger messenger = new Messenger(service);
                    Message msg = Message.obtain(null, 1);
                    final ServiceConnection myConn = this;
                    Handler h = new Handler(mHandler.getLooper()) {

                        @Override
                        public void handleMessage(Message msg) {
                            synchronized (mScreenrecordLock) {
                                if (mScreenrecordConnection == myConn) {
                                    mContext.unbindService(mScreenrecordConnection);
                                    mScreenrecordConnection = null;
                                    mRecording = false;
                                    mHandler.removeCallbacks(mScreenrecordTimeout);
                                }
                            }
                        }
                    };
                    msg.replyTo = new Messenger(h);
                    msg.arg1 = msg.arg2 = 0;
                    // Take the screenrecord
                    try {
                        messenger.send(msg);
                    } catch (RemoteException e) {
                    // Do nothing here
                    }
                }
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
            // Do nothing here
            }
        };
        if (mContext.bindService(intent, conn, mContext.BIND_AUTO_CREATE)) {
            mScreenrecordConnection = conn;
            mRecording = true;
            mHandler.postDelayed(mScreenrecordTimeout, 100000);
        }
    }
}
Also used : ServiceConnection(android.content.ServiceConnection) IBinder(android.os.IBinder) Message(android.os.Message) Handler(android.os.Handler) Intent(android.content.Intent) ComponentName(android.content.ComponentName) Messenger(android.os.Messenger) RemoteException(android.os.RemoteException)

Example 57 with Messenger

use of android.os.Messenger in project k-9 by k9mail.

the class OpenPgpApi method executeApiAsync.

public <T> CancelableBackgroundOperation executeApiAsync(Intent data, OpenPgpDataSource dataSource, OpenPgpDataSink<T> dataSink, final IOpenPgpSinkResultCallback<T> callback) {
    Messenger messenger = new Messenger(new Handler(new Handler.Callback() {

        @Override
        public boolean handleMessage(Message message) {
            callback.onProgress(message.arg1, message.arg2);
            return true;
        }
    }));
    data.putExtra(EXTRA_PROGRESS_MESSENGER, messenger);
    OpenPgpSourceSinkAsyncTask<T> task = new OpenPgpSourceSinkAsyncTask<>(data, dataSource, dataSink, callback);
    // http://commonsware.com/blog/2012/04/20/asynctask-threading-regression-confirmed.html
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[]) null);
    } else {
        task.execute((Void[]) null);
    }
    return task;
}
Also used : Message(android.os.Message) Handler(android.os.Handler) Messenger(android.os.Messenger)

Example 58 with Messenger

use of android.os.Messenger in project cw-omnibus by commonsguy.

the class Downloader method onHandleIntent.

@Override
public void onHandleIntent(Intent i) {
    HttpGet getMethod = new HttpGet(i.getData().toString());
    int result = Activity.RESULT_CANCELED;
    try {
        ResponseHandler<byte[]> responseHandler = new ByteArrayResponseHandler();
        byte[] responseBody = client.execute(getMethod, responseHandler);
        File output = new File(Environment.getExternalStorageDirectory(), i.getData().getLastPathSegment());
        if (output.exists()) {
            output.delete();
        }
        FileOutputStream fos = new FileOutputStream(output.getPath());
        fos.write(responseBody);
        fos.close();
        result = Activity.RESULT_OK;
    } catch (IOException e2) {
        Log.e(getClass().getName(), "Exception in download", e2);
    }
    Bundle extras = i.getExtras();
    if (extras != null) {
        Messenger messenger = (Messenger) extras.get(EXTRA_MESSENGER);
        Message msg = Message.obtain();
        msg.arg1 = result;
        try {
            messenger.send(msg);
        } catch (android.os.RemoteException e1) {
            Log.w(getClass().getName(), "Exception sending message", e1);
        }
    }
}
Also used : Message(android.os.Message) Bundle(android.os.Bundle) HttpGet(org.apache.http.client.methods.HttpGet) IOException(java.io.IOException) Messenger(android.os.Messenger) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 59 with Messenger

use of android.os.Messenger in project cw-android by commonsguy.

the class Downloader method onHandleIntent.

@Override
public void onHandleIntent(Intent i) {
    HttpGet getMethod = new HttpGet(i.getData().toString());
    int result = Activity.RESULT_CANCELED;
    try {
        ResponseHandler<byte[]> responseHandler = new ByteArrayResponseHandler();
        byte[] responseBody = client.execute(getMethod, responseHandler);
        File output = new File(Environment.getExternalStorageDirectory(), i.getData().getLastPathSegment());
        if (output.exists()) {
            output.delete();
        }
        FileOutputStream fos = new FileOutputStream(output.getPath());
        fos.write(responseBody);
        fos.close();
        result = Activity.RESULT_OK;
    } catch (IOException e2) {
        Log.e(getClass().getName(), "Exception in download", e2);
    }
    Bundle extras = i.getExtras();
    if (extras != null) {
        Messenger messenger = (Messenger) extras.get(EXTRA_MESSENGER);
        Message msg = Message.obtain();
        msg.arg1 = result;
        try {
            messenger.send(msg);
        } catch (android.os.RemoteException e1) {
            Log.w(getClass().getName(), "Exception sending message", e1);
        }
    }
}
Also used : Message(android.os.Message) Bundle(android.os.Bundle) HttpGet(org.apache.http.client.methods.HttpGet) IOException(java.io.IOException) Messenger(android.os.Messenger) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 60 with Messenger

use of android.os.Messenger in project cw-android by commonsguy.

the class DownloaderDemo method doTheDownload.

public void doTheDownload(View v) {
    b.setEnabled(false);
    Intent i = new Intent(this, Downloader.class);
    i.setData(Uri.parse("http://commonsware.com/Android/excerpt.pdf"));
    i.putExtra(Downloader.EXTRA_MESSENGER, new Messenger(handler));
    startService(i);
}
Also used : Intent(android.content.Intent) Messenger(android.os.Messenger)

Aggregations

Messenger (android.os.Messenger)108 RemoteException (android.os.RemoteException)44 Message (android.os.Message)42 Intent (android.content.Intent)38 Handler (android.os.Handler)27 ComponentName (android.content.ComponentName)23 IBinder (android.os.IBinder)23 ServiceConnection (android.content.ServiceConnection)19 DataUsageRequest (android.net.DataUsageRequest)9 Looper (android.os.Looper)9 ConditionVariable (android.os.ConditionVariable)8 AsyncChannel (com.android.internal.util.AsyncChannel)8 PendingIntent (android.app.PendingIntent)7 Bundle (android.os.Bundle)7 HandlerThread (android.os.HandlerThread)7 Binder (android.os.Binder)6 File (java.io.File)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 NetworkTemplate (android.net.NetworkTemplate)5 RecognizerIntent (android.speech.RecognizerIntent)5