Example usage for android.os Messenger Messenger

List of usage examples for android.os Messenger Messenger

Introduction

In this page you can find the example usage for android.os Messenger Messenger.

Prototype

public Messenger(IBinder target) 

Source Link

Document

Create a Messenger from a raw IBinder, which had previously been retrieved with #getBinder .

Usage

From source file:org.sufficientlysecure.keychain.ui.dialog.DeleteFileDialogFragment.java

/**
 * Creates dialog//  w w w  . j  a v  a2  s.c om
 */
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final FragmentActivity activity = getActivity();

    final String deleteFile = getArguments().getString(ARG_DELETE_FILE);

    AlertDialog.Builder alert = new AlertDialog.Builder(activity);

    alert.setIcon(android.R.drawable.ic_dialog_alert);
    alert.setTitle(R.string.warning);
    alert.setMessage(this.getString(R.string.fileDeleteConfirmation, deleteFile));

    alert.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int id) {
            dismiss();

            // Send all information needed to service to edit key in other thread
            Intent intent = new Intent(activity, KeychainIntentService.class);

            // fill values for this action
            Bundle data = new Bundle();

            intent.putExtra(KeychainIntentService.EXTRA_ACTION,
                    KeychainIntentService.ACTION_DELETE_FILE_SECURELY);
            data.putString(KeychainIntentService.DELETE_FILE, deleteFile);
            intent.putExtra(KeychainIntentService.EXTRA_DATA, data);

            ProgressDialogFragment deletingDialog = ProgressDialogFragment
                    .newInstance(R.string.progress_deletingSecurely, ProgressDialog.STYLE_HORIZONTAL);

            // Message is received after deleting is done in ApgService
            KeychainIntentServiceHandler saveHandler = new KeychainIntentServiceHandler(activity,
                    deletingDialog) {
                public void handleMessage(Message message) {
                    // handle messages by standard ApgHandler first
                    super.handleMessage(message);

                    if (message.arg1 == KeychainIntentServiceHandler.MESSAGE_OKAY) {
                        Toast.makeText(activity, R.string.fileDeleteSuccessful, Toast.LENGTH_SHORT).show();
                    }
                };
            };

            // Create a new Messenger for the communication back
            Messenger messenger = new Messenger(saveHandler);
            intent.putExtra(KeychainIntentService.EXTRA_MESSENGER, messenger);

            // show progress dialog
            deletingDialog.show(activity.getSupportFragmentManager(), "deletingDialog");

            // start service with intent
            activity.startService(intent);
        }
    });
    alert.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dismiss();
        }
    });
    alert.setCancelable(true);

    return alert.create();
}

From source file:org.thialfihar.android.apg.ui.dialog.DeleteFileDialogFragment.java

/**
 * Creates dialog//from  w  w w.j a v a2 s . c o m
 */
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final FragmentActivity activity = getActivity();

    final String deleteFile = getArguments().getString(ARG_DELETE_FILE);

    AlertDialog.Builder alert = new AlertDialog.Builder(activity);

    alert.setIcon(R.drawable.ic_dialog_alert_holo_light);
    alert.setTitle(R.string.warning);
    alert.setMessage(this.getString(R.string.file_delete_confirmation, deleteFile));

    alert.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int id) {
            dismiss();

            // Send all information needed to service to edit key in other thread
            Intent intent = new Intent(activity, ApgIntentService.class);

            // fill values for this action
            Bundle data = new Bundle();

            intent.setAction(ApgIntentService.ACTION_DELETE_FILE_SECURELY);
            data.putString(ApgIntentService.DELETE_FILE, deleteFile);
            intent.putExtra(ApgIntentService.EXTRA_DATA, data);

            ProgressDialogFragment deletingDialog = ProgressDialogFragment.newInstance(
                    getString(R.string.progress_deleting_securely), ProgressDialog.STYLE_HORIZONTAL, false,
                    null);

            // Message is received after deleting is done in ApgService
            ApgIntentServiceHandler saveHandler = new ApgIntentServiceHandler(activity, deletingDialog) {
                public void handleMessage(Message message) {
                    // handle messages by standard ApgHandler first
                    super.handleMessage(message);

                    if (message.arg1 == ApgIntentServiceHandler.MESSAGE_OKAY) {
                        Toast.makeText(activity, R.string.file_delete_successful, Toast.LENGTH_SHORT).show();
                    }
                }
            };

            // Create a new Messenger for the communication back
            Messenger messenger = new Messenger(saveHandler);
            intent.putExtra(ApgIntentService.EXTRA_MESSENGER, messenger);

            // show progress dialog
            deletingDialog.show(activity.getSupportFragmentManager(), "deletingDialog");

            // start service with intent
            activity.startService(intent);
        }
    });
    alert.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dismiss();
        }
    });
    alert.setCancelable(true);

    return alert.create();
}

From source file:com.android.wfds.printservice.WPrintService.java

@Override
public void onCreate() {
    mJNI = new WprintJNI();
    mServiceHandler = new ServiceHandler(this);
    mServiceMessenger = new Messenger(mServiceHandler);
    WPrintService.loadLibraries(getApplicationInfo());
    super.onCreate();
    HandlerThread jobThread = new HandlerThread(MobilePrintConstants.PRINT_SERVICE_JOB_THREAD_ID);
    jobThread.start();// w w w .j  a  v a 2  s  . c  o  m
    mJobHandler = new JobHandler(this, jobThread.getLooper(), mJNI);
    mJobHandler.sendEmptyMessage(MobilePrintConstants.WPRINT_SERVICE_MSG__INIT);
}

From source file:org.birthdayadapter.util.AccountHelper.java

/**
 * Force a manual sync now!//  w  w w.  j a  v  a 2s.  co  m
 */
public void manualSync() {
    Log.d(Constants.TAG, "Force manual sync...");

    // Disabled: Force resync in Android OS
    // Bundle extras = new Bundle();
    // extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
    // ContentResolver.requestSync(Constants.ACCOUNT, Constants.CONTENT_AUTHORITY, extras);

    // Enabled: Force resync in own thread:
    // Send all information needed to service to do in other thread
    Intent intent = new Intent(mContext, MainIntentService.class);

    // Create a new Messenger for the communication back
    if (mBackgroundStatusHandler != null) {
        Messenger messenger = new Messenger(mBackgroundStatusHandler);
        intent.putExtra(MainIntentService.EXTRA_MESSENGER, messenger);
    }
    intent.setAction(MainIntentService.ACTION_MANUAL_COMPLETE_SYNC);

    // start service with intent
    mContext.startService(intent);
}

From source file:org.thialfihar.android.apg.ui.UploadKeyActivity.java

private void uploadKey() {
    // Send all information needed to service to upload key in other thread
    Intent intent = new Intent(this, ApgIntentService.class);

    intent.setAction(ApgIntentService.ACTION_UPLOAD_KEYRING);

    // set data uri as path to keyring
    Uri blobUri = ApgContract.KeyRingData.buildPublicKeyRingUri(mDataUri);
    intent.setData(blobUri);/*from  ww  w  . j a v a  2 s  .  c  om*/

    // fill values for this action
    Bundle data = new Bundle();

    String server = (String) mKeyserverSpinner.getSelectedItem();
    data.putString(ApgIntentService.UPLOAD_KEY_SERVER, server);

    intent.putExtra(ApgIntentService.EXTRA_DATA, data);

    // Message is received after uploading is done in ApgService
    ApgIntentServiceHandler saveHandler = new ApgIntentServiceHandler(this,
            getString(R.string.progress_exporting), ProgressDialog.STYLE_HORIZONTAL) {
        public void handleMessage(Message message) {
            // handle messages by standard ApgHandler first
            super.handleMessage(message);

            if (message.arg1 == ApgIntentServiceHandler.MESSAGE_OKAY) {

                Toast.makeText(UploadKeyActivity.this, R.string.key_send_success, Toast.LENGTH_SHORT).show();
                finish();
            }
        }
    };

    // Create a new Messenger for the communication back
    Messenger messenger = new Messenger(saveHandler);
    intent.putExtra(ApgIntentService.EXTRA_MESSENGER, messenger);

    // show progress dialog
    saveHandler.showProgressDialog(this);

    // start service with intent
    startService(intent);
}

From source file:org.sufficientlysecure.keychain.service.PassphraseCacheService.java

/**
 * Gets a cached passphrase from memory by sending an intent to the service. This method is
 * designed to wait until the service returns the passphrase.
 *
 * @param context//from ww w . j a v  a  2 s.  c o  m
 * @param keyId
 * @return passphrase or null (if no passphrase is cached for this keyId)
 */
public static String getCachedPassphrase(Context context, long keyId) {
    Log.d(TAG, "getCachedPassphrase() get masterKeyId for " + keyId);

    Intent intent = new Intent(context, PassphraseCacheService.class);
    intent.setAction(ACTION_PASSPHRASE_CACHE_GET);

    final Object mutex = new Object();
    final Bundle returnBundle = new Bundle();

    HandlerThread handlerThread = new HandlerThread("getPassphraseThread");
    handlerThread.start();
    Handler returnHandler = new Handler(handlerThread.getLooper()) {
        @Override
        public void handleMessage(Message message) {
            if (message.obj != null) {
                String passphrase = ((Bundle) message.obj).getString(EXTRA_PASSPHRASE);
                returnBundle.putString(EXTRA_PASSPHRASE, passphrase);
            }
            synchronized (mutex) {
                mutex.notify();
            }
            // quit handlerThread
            getLooper().quit();
        }
    };

    // Create a new Messenger for the communication back
    Messenger messenger = new Messenger(returnHandler);
    intent.putExtra(EXTRA_KEY_ID, keyId);
    intent.putExtra(EXTRA_MESSENGER, messenger);
    // send intent to this service
    context.startService(intent);

    // Wait on mutex until passphrase is returned to handlerThread
    synchronized (mutex) {
        try {
            mutex.wait(3000);
        } catch (InterruptedException e) {
        }
    }

    if (returnBundle.containsKey(EXTRA_PASSPHRASE)) {
        return returnBundle.getString(EXTRA_PASSPHRASE);
    } else {
        return null;
    }
}

From source file:fr.julienvermet.bugdroid.ui.BugCommentsFragment.java

private void postComment() {
    try {/*from  w w w  .j  a  v a 2s  .com*/
        String message = mCommentMessage.getText().toString();
        if (message.isEmpty()) {
            Toast.makeText(getActivity(), R.string.comment_empty, Toast.LENGTH_LONG).show();
            return;
        }

        mCommentSendProgress.setVisibility(View.VISIBLE);
        mCommentSend.setVisibility(View.GONE);

        JSONObject jsonData = new JSONObject();
        jsonData.put("text", message);

        Intent intent = CommentIntentService.getIntent(getActivity(), mInstance, mBug.bugId, jsonData);
        Messenger messenger = new Messenger(onCommentHandler);
        intent.putExtra(CommentIntentService.MESSENGER, messenger);
        getActivity().startService(intent);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:org.sufficientlysecure.keychain.ui.UploadKeyActivity.java

private void uploadKey() {
    // Send all information needed to service to upload key in other thread
    Intent intent = new Intent(this, KeychainIntentService.class);

    intent.setAction(KeychainIntentService.ACTION_UPLOAD_KEYRING);

    // set data uri as path to keyring
    Uri blobUri = KeychainContract.KeyRingData.buildPublicKeyRingUri(mDataUri);
    intent.setData(blobUri);/*  w w  w. j av  a 2  s.c om*/

    // fill values for this action
    Bundle data = new Bundle();

    String server = (String) mKeyServerSpinner.getSelectedItem();
    data.putString(KeychainIntentService.UPLOAD_KEY_SERVER, server);

    intent.putExtra(KeychainIntentService.EXTRA_DATA, data);

    // Message is received after uploading is done in KeychainIntentService
    KeychainIntentServiceHandler saveHandler = new KeychainIntentServiceHandler(this,
            getString(R.string.progress_exporting), ProgressDialog.STYLE_HORIZONTAL) {
        public void handleMessage(Message message) {
            // handle messages by standard KeychainIntentServiceHandler first
            super.handleMessage(message);

            if (message.arg1 == KeychainIntentServiceHandler.MESSAGE_OKAY) {

                Toast.makeText(UploadKeyActivity.this, R.string.key_send_success, Toast.LENGTH_SHORT).show();
                finish();
            }
        }
    };

    // Create a new Messenger for the communication back
    Messenger messenger = new Messenger(saveHandler);
    intent.putExtra(KeychainIntentService.EXTRA_MESSENGER, messenger);

    // show progress dialog
    saveHandler.showProgressDialog(this);

    // start service with intent
    startService(intent);
}

From source file:com.yangtsaosoftware.pebblemessenger.services.MessageProcessingService.java

@Override
public void onCreate() {
    super.onCreate();
    phone_is_ontalking = false;//ww w .  j  av a2 s.c om
    loadPrefs();
    Thread proceedthread = new ProcessThread();
    proceedthread.start();
    messageHandler = new MessageHandler(Looper.getMainLooper());
    mMessenger = new Messenger(messageHandler);
    MessageProcessingService._context = getApplicationContext();
    fdb = new FontDbHandler(_context);
    fdb.open();
    mdb = new MessageDbHandler(_context);
    mdb.open();
    bindService(new Intent(this, PebbleCenter.class), connToPebbleCenter, Context.BIND_AUTO_CREATE);
    BroadcastReceiver br = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            int command = intent.getIntExtra(Constants.BROADCAST_COMMAND, Constants.BROADCAST_PREFER_CHANGED);
            switch (command) {
            case Constants.BROADCAST_PREFER_CHANGED:
                loadPrefs();
                break;
            case Constants.BROADCAST_CALL_INCOMING:
                String number = intent.getStringExtra(Constants.BROADCAST_PHONE_NUM);
                String name = intent.getStringExtra(Constants.BROADCAST_NAME);

                if (callQuietEnable) {
                    Calendar c = Calendar.getInstance();
                    Calendar now = new GregorianCalendar(0, 0, 0, c.get(Calendar.HOUR_OF_DAY),
                            c.get(Calendar.MINUTE));
                    Constants.log(TAG_NAME, "Checking quiet hours. Now: " + now.toString() + " vs "
                            + quiet_hours_before.toString() + " and " + quiet_hours_after.toString());
                    if (now.before(quiet_hours_before) || now.after(quiet_hours_after)) {
                        Constants.log(TAG_NAME, "Time is before or after the quiet hours time. Returning.");
                        addNewCall(number, name, MessageDbHandler.NEW_ICON);
                    } else {
                        Bundle b = new Bundle();
                        b.putLong(MessageDbHandler.COL_CALL_ID,
                                addNewCall(number, name, MessageDbHandler.OLD_ICON));
                        b.putString(MessageDbHandler.COL_CALL_NUMBER, number);
                        b.putString(MessageDbHandler.COL_CALL_NAME, name);
                        Message innerMsg = processHandler.obtainMessage(INNER_CALL_PROCEED);
                        innerMsg.setData(b);
                        processHandler.sendMessage(innerMsg);
                    }
                } else {
                    Bundle b = new Bundle();
                    if (phone_is_ontalking) {
                        b.putString(MessageDbHandler.COL_MESSAGE_APP, "Call");
                        b.putString(MessageDbHandler.COL_MESSAGE_CONTENT, name + "\n" + number);
                        addNewCall(number, name, MessageDbHandler.NEW_ICON);
                        b.putLong(MessageDbHandler.COL_MESSAGE_ID, addNewMessage(b, MessageDbHandler.OLD_ICON));
                        Message innerMsg = processHandler.obtainMessage(INNER_MESSAGE_PROCEED);
                        innerMsg.setData(b);
                        processHandler.sendMessage(innerMsg);
                    } else {
                        b.putLong(MessageDbHandler.COL_CALL_ID,
                                addNewCall(number, name, MessageDbHandler.OLD_ICON));
                        b.putString(MessageDbHandler.COL_CALL_NUMBER, number);
                        b.putString(MessageDbHandler.COL_CALL_NAME, name);
                        Message innerMsg = processHandler.obtainMessage(INNER_CALL_PROCEED);
                        innerMsg.setData(b);
                        processHandler.sendMessage(innerMsg);
                    }
                }
                break;
            case Constants.BROADCAST_CALL_HOOK:
                phone_is_ontalking = true;
                break;
            case Constants.BROADCAST_CALL_IDLE:
                phone_is_ontalking = false;
                break;
            case Constants.BROADCAST_SMS_INCOMING: {
                Message msg = Message.obtain();
                msg.what = MSG_NEW_MESSAGE;
                Bundle b = new Bundle();
                b.putString(MessageDbHandler.COL_MESSAGE_APP, "SMS");
                b.putString(MessageDbHandler.COL_MESSAGE_CONTENT,
                        intent.getStringExtra(Constants.BROADCAST_SMS_BODY));
                msg.setData(b);
                messageHandler.sendMessage(msg);
            }
            }
        }
    };
    IntentFilter intentFilter = new IntentFilter(MessageProcessingService.class.getName());
    LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(br, intentFilter);

}

From source file:org.peercast.core.PeerCastService.java

@Override
public void onCreate() {
    super.onCreate();

    HandlerThread thread = new HandlerThread("PeerCastService", Thread.MIN_PRIORITY);
    thread.start();//from w  w w  .  ja v a 2s. co m

    serviceLooper = thread.getLooper();
    serviceHandler = new ServiceHandler(serviceLooper);
    serviceMessenger = new Messenger(serviceHandler);
}