Example usage for android.support.v4.content LocalBroadcastManager getInstance

List of usage examples for android.support.v4.content LocalBroadcastManager getInstance

Introduction

In this page you can find the example usage for android.support.v4.content LocalBroadcastManager getInstance.

Prototype

public static LocalBroadcastManager getInstance(Context context) 

Source Link

Usage

From source file:com.android.dragonkeyboardfirmwareupdater.KeyboardFirmwareUpdateService.java

public void abortDfu() {
    if (mDfuStatus != DFU_STATE_UPDATING)
        return;/*w  ww .  ja v  a  2 s.  com*/
    final Intent pauseAction = new Intent(DfuService.BROADCAST_ACTION);
    pauseAction.putExtra(DfuService.EXTRA_ACTION, DfuService.ACTION_ABORT);
    LocalBroadcastManager.getInstance(this).sendBroadcast(pauseAction);
}

From source file:com.android.messaging.ui.conversation.ConversationFragment.java

@Override
public void onPause() {
    super.onPause();
    if (mComposeMessageView != null && !mSuppressWriteDraft) {
        mComposeMessageView.writeDraftMessage();
    }/*from   w w  w . jav a2s .c o  m*/
    mSuppressWriteDraft = false;
    mBinding.getData().unsetFocus();
    mListState = mRecyclerView.getLayoutManager().onSaveInstanceState();

    LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mConversationSelfIdChangeReceiver);
}

From source file:com.aniruddhc.acemusic.player.EqualizerActivity.EqualizerActivity.java

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

    //Initialize the broadcast manager that will listen for track changes.
    LocalBroadcastManager.getInstance(mContext).registerReceiver((mReceiver),
            new IntentFilter(Common.UPDATE_UI_BROADCAST));

}

From source file:com.aniruddhc.acemusic.player.EqualizerActivity.EqualizerActivity.java

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

    //Unregister the broadcast receivers.
    LocalBroadcastManager.getInstance(mContext).unregisterReceiver(mReceiver);

}

From source file:com.android.contacts.ContactSaveService.java

private void splitContact(Intent intent) {
    final long rawContactIds[][] = (long[][]) intent.getSerializableExtra(EXTRA_RAW_CONTACT_IDS);
    final ResultReceiver receiver = intent.getParcelableExtra(EXTRA_RESULT_RECEIVER);
    final boolean hardSplit = intent.getBooleanExtra(EXTRA_HARD_SPLIT, false);
    if (rawContactIds == null) {
        Log.e(TAG, "Invalid argument for splitContact request");
        if (receiver != null) {
            receiver.send(BAD_ARGUMENTS, new Bundle());
        }//from  w w w  .j a v a2  s  . com
        return;
    }
    final int batchSize = MAX_CONTACTS_PROVIDER_BATCH_SIZE;
    final ContentResolver resolver = getContentResolver();
    final ArrayList<ContentProviderOperation> operations = new ArrayList<>(batchSize);
    for (int i = 0; i < rawContactIds.length; i++) {
        for (int j = 0; j < rawContactIds.length; j++) {
            if (i != j) {
                if (!buildSplitTwoContacts(operations, rawContactIds[i], rawContactIds[j], hardSplit)) {
                    if (receiver != null) {
                        receiver.send(CP2_ERROR, new Bundle());
                        return;
                    }
                }
            }
        }
    }
    if (operations.size() > 0 && !applyOperations(resolver, operations)) {
        if (receiver != null) {
            receiver.send(CP2_ERROR, new Bundle());
        }
        return;
    }
    LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(BROADCAST_UNLINK_COMPLETE));
    if (receiver != null) {
        receiver.send(CONTACTS_SPLIT, new Bundle());
    } else {
        showToast(R.string.contactUnlinkedToast);
    }
}

From source file:com.aimfire.gallery.GalleryActivity.java

public void onDownloadFileProgress(String name, final int downloadBytes, final int totalBytes) {
    if (MediaScanner.isMovie(name)) {
        if (totalBytes != -1) {
            int percentage = (int) ((float) downloadBytes * 100 / (float) totalBytes);

            if (BuildConfig.DEBUG)
                Log.d(TAG, "onDownloadFileProgress: " + percentage + "%");

            String path = MediaScanner.getSharedMediaPathFromOrigName(name);

            Intent messageIntent = new Intent(MainConsts.FILE_DOWNLOADER_MESSAGE);
            messageIntent.putExtra(MainConsts.EXTRA_WHAT, MainConsts.MSG_FILE_DOWNLOADER_PROGRESS);
            messageIntent.putExtra(MainConsts.EXTRA_PATH, path);
            messageIntent.putExtra(MainConsts.EXTRA_MSG, percentage);
            LocalBroadcastManager.getInstance(this).sendBroadcast(messageIntent);
        }//from   ww w .  j a v a  2  s  .c o m
    }
}

From source file:com.android.contacts.ContactSaveService.java

private void joinSeveralContacts(Intent intent) {
    final long[] contactIds = intent.getLongArrayExtra(EXTRA_CONTACT_IDS);

    final ResultReceiver receiver = intent.getParcelableExtra(EXTRA_RESULT_RECEIVER);

    // Load raw contact IDs for all contacts involved.
    final long rawContactIds[] = getRawContactIdsForAggregation(contactIds);
    final long[][] separatedRawContactIds = getSeparatedRawContactIds(contactIds);
    if (rawContactIds == null) {
        Log.e(TAG, "Invalid arguments for joinSeveralContacts request");
        if (receiver != null) {
            receiver.send(BAD_ARGUMENTS, new Bundle());
        }//from  w  ww .j a va2s.c  om
        return;
    }

    // For each pair of raw contacts, insert an aggregation exception
    final ContentResolver resolver = getContentResolver();
    // The maximum number of operations per batch (aka yield point) is 500. See b/22480225
    final int batchSize = MAX_CONTACTS_PROVIDER_BATCH_SIZE;
    final ArrayList<ContentProviderOperation> operations = new ArrayList<>(batchSize);
    for (int i = 0; i < rawContactIds.length; i++) {
        for (int j = 0; j < rawContactIds.length; j++) {
            if (i != j) {
                buildJoinContactDiff(operations, rawContactIds[i], rawContactIds[j]);
            }
            // Before we get to 500 we need to flush the operations list
            if (operations.size() > 0 && operations.size() % batchSize == 0) {
                if (!applyOperations(resolver, operations)) {
                    if (receiver != null) {
                        receiver.send(CP2_ERROR, new Bundle());
                    }
                    return;
                }
                operations.clear();
            }
        }
    }
    if (operations.size() > 0 && !applyOperations(resolver, operations)) {
        if (receiver != null) {
            receiver.send(CP2_ERROR, new Bundle());
        }
        return;
    }

    final String name = queryNameOfLinkedContacts(contactIds);
    if (name != null) {
        if (receiver != null) {
            final Bundle result = new Bundle();
            result.putSerializable(EXTRA_RAW_CONTACT_IDS, separatedRawContactIds);
            result.putString(EXTRA_DISPLAY_NAME, name);
            receiver.send(CONTACTS_LINKED, result);
        } else {
            if (TextUtils.isEmpty(name)) {
                showToast(R.string.contactsJoinedMessage);
            } else {
                showToast(R.string.contactsJoinedNamedMessage, name);
            }
        }
        LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(BROADCAST_LINK_COMPLETE));
    } else {
        if (receiver != null) {
            receiver.send(CP2_ERROR, new Bundle());
        }
        showToast(R.string.contactJoinErrorToast);
    }
}

From source file:com.android.contacts.ContactSaveService.java

private void joinContacts(Intent intent) {
    long contactId1 = intent.getLongExtra(EXTRA_CONTACT_ID1, -1);
    long contactId2 = intent.getLongExtra(EXTRA_CONTACT_ID2, -1);

    // Load raw contact IDs for all raw contacts involved - currently edited and selected
    // in the join UIs.
    long rawContactIds[] = getRawContactIdsForAggregation(contactId1, contactId2);
    if (rawContactIds == null) {
        Log.e(TAG, "Invalid arguments for joinContacts request");
        return;//from w w w.  ja v  a  2 s.  co  m
    }

    ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();

    // For each pair of raw contacts, insert an aggregation exception
    for (int i = 0; i < rawContactIds.length; i++) {
        for (int j = 0; j < rawContactIds.length; j++) {
            if (i != j) {
                buildJoinContactDiff(operations, rawContactIds[i], rawContactIds[j]);
            }
        }
    }

    final ContentResolver resolver = getContentResolver();

    // Use the name for contactId1 as the name for the newly aggregated contact.
    final Uri contactId1Uri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId1);
    final Uri entityUri = Uri.withAppendedPath(contactId1Uri, Contacts.Entity.CONTENT_DIRECTORY);
    Cursor c = resolver.query(entityUri, ContactEntityQuery.PROJECTION, ContactEntityQuery.SELECTION, null,
            null);
    if (c == null) {
        Log.e(TAG, "Unable to open Contacts DB cursor");
        showToast(R.string.contactSavedErrorToast);
        return;
    }
    long dataIdToAddSuperPrimary = -1;
    try {
        if (c.moveToFirst()) {
            dataIdToAddSuperPrimary = c.getLong(ContactEntityQuery.DATA_ID);
        }
    } finally {
        c.close();
    }

    // Mark the name from contactId1 IS_SUPER_PRIMARY to make sure that the contact
    // display name does not change as a result of the join.
    if (dataIdToAddSuperPrimary != -1) {
        Builder builder = ContentProviderOperation
                .newUpdate(ContentUris.withAppendedId(Data.CONTENT_URI, dataIdToAddSuperPrimary));
        builder.withValue(Data.IS_SUPER_PRIMARY, 1);
        builder.withValue(Data.IS_PRIMARY, 1);
        operations.add(builder.build());
    }

    // Apply all aggregation exceptions as one batch
    final boolean success = applyOperations(resolver, operations);

    final String name = queryNameOfLinkedContacts(new long[] { contactId1, contactId2 });
    Intent callbackIntent = intent.getParcelableExtra(EXTRA_CALLBACK_INTENT);
    if (success && name != null) {
        if (TextUtils.isEmpty(name)) {
            showToast(R.string.contactsJoinedMessage);
        } else {
            showToast(R.string.contactsJoinedNamedMessage, name);
        }
        Uri uri = RawContacts.getContactLookupUri(resolver,
                ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactIds[0]));
        callbackIntent.setData(uri);
        LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(BROADCAST_LINK_COMPLETE));
    }
    deliverCallback(callbackIntent);
}

From source file:android_network.hetnet.vpn_service.ServiceSinkhole.java

private void set(Intent intent) {
    // Get arguments
    int uid = intent.getIntExtra(EXTRA_UID, 0);
    String network = intent.getStringExtra(EXTRA_NETWORK);
    String pkg = intent.getStringExtra(EXTRA_PACKAGE);
    boolean blocked = intent.getBooleanExtra(EXTRA_BLOCKED, false);
    Log.i(TAG, "Set " + pkg + " " + network + "=" + blocked);

    // Get defaults
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(ServiceSinkhole.this);
    boolean default_wifi = settings.getBoolean("whitelist_wifi", true);
    boolean default_other = settings.getBoolean("whitelist_other", true);

    // Update setting
    SharedPreferences prefs = getSharedPreferences(network, Context.MODE_PRIVATE);
    if (blocked == ("wifi".equals(network) ? default_wifi : default_other))
        prefs.edit().remove(pkg).apply();
    else/* w  w w .j ava  2 s . c  o m*/
        prefs.edit().putBoolean(pkg, blocked).apply();

    // Apply rules
    ServiceSinkhole.reload("notification", ServiceSinkhole.this);

    // Update notification
    Receiver.notifyNewApplication(uid, ServiceSinkhole.this);

    // Update UI
    Intent ruleset = new Intent(MainActivity.ACTION_RULES_CHANGED);
    LocalBroadcastManager.getInstance(ServiceSinkhole.this).sendBroadcast(ruleset);
}