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.aniruddhc.acemusic.player.NowPlayingQueueActivity.NowPlayingQueueFragment.java

@Override
public void onStop() {
    LocalBroadcastManager.getInstance(mContext).unregisterReceiver(receiver);
    super.onStop();

}

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

@Override
@TargetApi(Build.VERSION_CODES.M)/* w  w  w  . j av a2 s  .  co  m*/
public void onSharedPreferenceChanged(SharedPreferences prefs, String name) {
    // Pro features
    if ("theme".equals(name)) {
        if (!"teal".equals(prefs.getString(name, "teal"))) {
            prefs.edit().putString(name, "teal").apply();
            ((ListPreference) getPreferenceScreen().findPreference(name)).setValue("teal");
            return;
        }
    } else if ("install".equals(name)) {
        if (prefs.getBoolean(name, false)) {
            prefs.edit().putBoolean(name, false).apply();
            ((TwoStatePreference) getPreferenceScreen().findPreference(name)).setChecked(false);
            return;
        }
    } else if ("show_stats".equals(name)) {
        if (prefs.getBoolean(name, false)) {
            prefs.edit().putBoolean(name, false).apply();
            return;
        }
        ((TwoStatePreference) getPreferenceScreen().findPreference(name))
                .setChecked(prefs.getBoolean(name, false));
    }

    Object value = prefs.getAll().get(name);
    if (value instanceof String && "".equals(value))
        prefs.edit().remove(name).apply();

    // Dependencies
    if ("screen_on".equals(name))
        ServiceSinkhole.reload("changed " + name, this);

    else if ("whitelist_wifi".equals(name) || "screen_wifi".equals(name))
        ServiceSinkhole.reload("changed " + name, this);

    else if ("whitelist_other".equals(name) || "screen_other".equals(name))
        ServiceSinkhole.reload("changed " + name, this);

    else if ("whitelist_roaming".equals(name)) {
        if (prefs.getBoolean(name, false)) {
            if (Util.hasPhoneStatePermission(this))
                ServiceSinkhole.reload("changed " + name, this);
            else
                requestPermissions(new String[] { Manifest.permission.READ_PHONE_STATE },
                        REQUEST_ROAMING_INTERNATIONAL);
        } else
            ServiceSinkhole.reload("changed " + name, this);

    } else if ("auto_enable".equals(name))
        getPreferenceScreen().findPreference(name)
                .setTitle(getString(R.string.setting_auto, prefs.getString(name, "0")));

    else if ("screen_delay".equals(name))
        getPreferenceScreen().findPreference(name)
                .setTitle(getString(R.string.setting_delay, prefs.getString(name, "0")));

    else if ("theme".equals(name) || "dark_theme".equals(name))
        recreate();

    else if ("subnet".equals(name))
        ServiceSinkhole.reload("changed " + name, this);

    else if ("tethering".equals(name))
        ServiceSinkhole.reload("changed " + name, this);

    else if ("lan".equals(name))
        ServiceSinkhole.reload("changed " + name, this);

    else if ("ip6".equals(name))
        ServiceSinkhole.reload("changed " + name, this);

    else if ("wifi_homes".equals(name)) {
        MultiSelectListPreference pref_wifi_homes = (MultiSelectListPreference) getPreferenceScreen()
                .findPreference(name);
        Set<String> ssid = prefs.getStringSet(name, new HashSet<String>());
        if (ssid.size() > 0)
            pref_wifi_homes.setTitle(getString(R.string.setting_wifi_home, TextUtils.join(", ", ssid)));
        else
            pref_wifi_homes.setTitle(getString(R.string.setting_wifi_home, "-"));
        ServiceSinkhole.reload("changed " + name, this);

    } else if ("use_metered".equals(name))
        ServiceSinkhole.reload("changed " + name, this);

    else if ("unmetered_2g".equals(name) || "unmetered_3g".equals(name) || "unmetered_4g".equals(name)) {
        if (prefs.getBoolean(name, false)) {
            if (Util.hasPhoneStatePermission(this))
                ServiceSinkhole.reload("changed " + name, this);
            else {
                if ("unmetered_2g".equals(name))
                    requestPermissions(new String[] { Manifest.permission.READ_PHONE_STATE }, REQUEST_METERED2);
                else if ("unmetered_3g".equals(name))
                    requestPermissions(new String[] { Manifest.permission.READ_PHONE_STATE }, REQUEST_METERED3);
                else if ("unmetered_4g".equals(name))
                    requestPermissions(new String[] { Manifest.permission.READ_PHONE_STATE }, REQUEST_METERED4);
            }
        } else
            ServiceSinkhole.reload("changed " + name, this);

    } else if ("national_roaming".equals(name)) {
        if (prefs.getBoolean(name, false)) {
            if (Util.hasPhoneStatePermission(this))
                ServiceSinkhole.reload("changed " + name, this);
            else
                requestPermissions(new String[] { Manifest.permission.READ_PHONE_STATE },
                        REQUEST_ROAMING_NATIONAL);
        } else
            ServiceSinkhole.reload("changed " + name, this);

    } else if ("manage_system".equals(name)) {
        boolean manage = prefs.getBoolean(name, true);
        if (!manage)
            prefs.edit().putBoolean("show_user", true).apply();
        prefs.edit().putBoolean("show_system", manage).apply();
        ServiceSinkhole.reload("changed " + name, this);

    } else if ("log_app".equals(name)) {
        Intent ruleset = new Intent(MainActivity.ACTION_RULES_CHANGED);
        LocalBroadcastManager.getInstance(this).sendBroadcast(ruleset);

    } else if ("filter".equals(name)) {
        // Show dialog
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && prefs.getBoolean(name, false)) {
            LayoutInflater inflater = LayoutInflater.from(ActivitySettings.this);
            View view = inflater.inflate(R.layout.filter, null, false);
            dialogFilter = new AlertDialog.Builder(ActivitySettings.this).setView(view).setCancelable(false)
                    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // Do nothing
                        }
                    }).setOnDismissListener(new DialogInterface.OnDismissListener() {
                        @Override
                        public void onDismiss(DialogInterface dialogInterface) {
                            dialogFilter = null;
                        }
                    }).create();
            dialogFilter.show();
        } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP && !prefs.getBoolean(name, false)) {
            prefs.edit().putBoolean(name, true).apply();
            Toast.makeText(ActivitySettings.this, R.string.msg_filter4, Toast.LENGTH_SHORT).show();
        }

        ((TwoStatePreference) getPreferenceScreen().findPreference(name))
                .setChecked(prefs.getBoolean(name, false));

        ServiceSinkhole.reload("changed " + name, this);

    } else if ("use_hosts".equals(name))
        ServiceSinkhole.reload("changed " + name, this);

    else if ("vpn4".equals(name)) {
        String vpn4 = prefs.getString(name, null);
        try {
            checkAddress(vpn4);
        } catch (Throwable ex) {
            prefs.edit().remove(name).apply();
            ((EditTextPreference) getPreferenceScreen().findPreference(name)).setText(null);
            if (!TextUtils.isEmpty(vpn4))
                Toast.makeText(ActivitySettings.this, ex.toString(), Toast.LENGTH_LONG).show();
        }
        getPreferenceScreen().findPreference(name)
                .setTitle(getString(R.string.setting_vpn4, prefs.getString(name, "10.1.10.1")));
        ServiceSinkhole.reload("changed " + name, this);

    } else if ("vpn6".equals(name)) {
        String vpn6 = prefs.getString(name, null);
        try {
            checkAddress(vpn6);
        } catch (Throwable ex) {
            prefs.edit().remove(name).apply();
            ((EditTextPreference) getPreferenceScreen().findPreference(name)).setText(null);
            if (!TextUtils.isEmpty(vpn6))
                Toast.makeText(ActivitySettings.this, ex.toString(), Toast.LENGTH_LONG).show();
        }
        getPreferenceScreen().findPreference(name).setTitle(
                getString(R.string.setting_vpn6, prefs.getString(name, "fd00:1:fd00:1:fd00:1:fd00:1")));
        ServiceSinkhole.reload("changed " + name, this);

    } else if ("dns".equals(name) || "dns2".equals(name)) {
        String dns = prefs.getString(name, null);
        try {
            checkAddress(dns);
        } catch (Throwable ex) {
            prefs.edit().remove(name).apply();
            ((EditTextPreference) getPreferenceScreen().findPreference(name)).setText(null);
            if (!TextUtils.isEmpty(dns))
                Toast.makeText(ActivitySettings.this, ex.toString(), Toast.LENGTH_LONG).show();
        }
        getPreferenceScreen().findPreference(name).setTitle(getString(R.string.setting_dns,
                prefs.getString(name, Util.getDefaultDNS(this).get("dns".equals(name) ? 0 : 1))));
        ServiceSinkhole.reload("changed " + name, this);

    } else if ("ttl".equals(name))
        getPreferenceScreen().findPreference(name)
                .setTitle(getString(R.string.setting_ttl, prefs.getString(name, "259200")));

    else if ("rcode".equals(name)) {
        getPreferenceScreen().findPreference(name)
                .setTitle(getString(R.string.setting_rcode, prefs.getString(name, "3")));
        ServiceSinkhole.reload("changed " + name, this);

    } else if ("socks5_enabled".equals(name))
        ServiceSinkhole.reload("changed " + name, this);

    else if ("socks5_addr".equals(name)) {
        String socks5_addr = prefs.getString(name, null);
        try {
            if (!TextUtils.isEmpty(socks5_addr) && !Util.isNumericAddress(socks5_addr))
                throw new IllegalArgumentException("Bad address");
        } catch (Throwable ex) {
            prefs.edit().remove(name).apply();
            ((EditTextPreference) getPreferenceScreen().findPreference(name)).setText(null);
            if (!TextUtils.isEmpty(socks5_addr))
                Toast.makeText(ActivitySettings.this, ex.toString(), Toast.LENGTH_LONG).show();
        }
        getPreferenceScreen().findPreference(name)
                .setTitle(getString(R.string.setting_socks5_addr, prefs.getString(name, "-")));
        ServiceSinkhole.reload("changed " + name, this);

    } else if ("socks5_port".equals(name)) {
        getPreferenceScreen().findPreference(name)
                .setTitle(getString(R.string.setting_socks5_port, prefs.getString(name, "-")));
        ServiceSinkhole.reload("changed " + name, this);

    } else if ("socks5_username".equals(name)) {
        getPreferenceScreen().findPreference(name)
                .setTitle(getString(R.string.setting_socks5_username, prefs.getString(name, "-")));
        ServiceSinkhole.reload("changed " + name, this);

    } else if ("socks5_password".equals(name)) {
        getPreferenceScreen().findPreference(name).setTitle(getString(R.string.setting_socks5_password,
                TextUtils.isEmpty(prefs.getString(name, "")) ? "-" : "*****"));
        ServiceSinkhole.reload("changed " + name, this);

    } else if ("pcap_record_size".equals(name) || "pcap_file_size".equals(name)) {
        if ("pcap_record_size".equals(name))
            getPreferenceScreen().findPreference(name)
                    .setTitle(getString(R.string.setting_pcap_record_size, prefs.getString(name, "64")));
        else
            getPreferenceScreen().findPreference(name)
                    .setTitle(getString(R.string.setting_pcap_file_size, prefs.getString(name, "2")));

        ServiceSinkhole.setPcap(false, this);

        File pcap_file = new File(getDir("data", MODE_PRIVATE), "netguard.pcap");
        if (pcap_file.exists() && !pcap_file.delete())
            Log.w(TAG, "Delete PCAP failed");

        if (prefs.getBoolean("pcap", false))
            ServiceSinkhole.setPcap(true, this);

    } else if ("watchdog".equals(name)) {
        getPreferenceScreen().findPreference(name)
                .setTitle(getString(R.string.setting_watchdog, prefs.getString(name, "0")));
        ServiceSinkhole.reload("changed " + name, this);

    } else if ("show_stats".equals(name))
        ServiceSinkhole.reloadStats("changed " + name, this);

    else if ("stats_frequency".equals(name))
        getPreferenceScreen().findPreference(name)
                .setTitle(getString(R.string.setting_stats_frequency, prefs.getString(name, "1000")));

    else if ("stats_samples".equals(name))
        getPreferenceScreen().findPreference(name)
                .setTitle(getString(R.string.setting_stats_samples, prefs.getString(name, "90")));

    else if ("hosts_url".equals(name))
        getPreferenceScreen().findPreference(name)
                .setSummary(prefs.getString(name, "http://www.netguard.me/hosts"));

    else if ("loglevel".equals(name))
        ServiceSinkhole.reload("changed " + name, this);
}

From source file:biz.wiz.android.wallet.service.BlockchainServiceImpl.java

private void broadcastBlockchainState() {
    final Intent broadcast = new Intent(ACTION_BLOCKCHAIN_STATE);
    broadcast.setPackage(getPackageName());
    getBlockchainState().putExtras(broadcast);
    LocalBroadcastManager.getInstance(this).sendBroadcast(broadcast);
}

From source file:com.anp.bdmt.MainActivity.java

/**
 * ? ? ? ?? LocalBoardcast ? ./*ww  w  .  jav  a2s  . c om*/
 */
@Override
protected void onPause() {
    LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver);
    super.onPause();
}

From source file:ch.bfh.evoting.alljoyn.BusHandler.java

/**
 * Method processing the message received
 * @param messageObject object containing the message transmitted over the newtork
 *///from w ww .j  ava 2 s .c om
private void processMessage(AllJoynMessage messageObject) {

    /*
     * First we check if decrypter is ready. If not, only Salt message can go further
     */
    //if messageEncrypter isn't ready to decrypt and message is encrypted
    if (!messageEncrypter.isReady() && messageObject.isEncrypted()) {
        //requeue the message in order to process it later
        Message msg = this.obtainMessage(BusHandler.REPROCESS_MESSAGE);
        Bundle data = new Bundle();
        data.putSerializable("message", messageObject);
        msg.setData(data);
        this.sendMessage(msg);
        Log.d(TAG, "Requeueing message received");
        return;
    }

    /*
     * Second, we check if the message contains the salt and extract it
     */
    //Check if the message contains the salt
    if (messageObject.getType().equals(Type.SALT)) {
        if (messageEncrypter.getSalt() == null) {
            this.saltReceived(messageObject);
            return;
        }
    }

    /*
     * Third, we check if the message contains an identity
     */
    //Check if message contain an identity
    if (messageObject.getType().equals(Type.IDENTITY)) {
        extractIdentity(messageObject);
        return;
    }

    /*
     * Fourth, we verify the signature, if we know the sender, otherwise we set a flag
     */
    if (identityMap.containsKey(messageObject.getSender())) {
        boolean result = messageObject.verifyMessage(identityMap.get(messageObject.getSender()).getPublicKey());//messageAuthenticater.verifySignature(identityMap.get(sender).getPublicKey(), Base64.decode(signature,Base64.DEFAULT), Base64.decode(message,Base64.DEFAULT));
        if (!result) {
            //signature verification failed
            //ignoring message
            Log.e(TAG, "Wrong signature");
            return;
        } else {
            Log.d(TAG, "Signature correct");
        }
    } else {
        //message not containing a salt nor an identity coming from an unknown person => ignore 
        Log.d(TAG, "Ignoring message since sender is not known");
        //try to decrypt in order to detect if it could be a password error
        messageObject.getMessage();
        return;
    }

    /*
     * Fifth, we decrypt the message
     */
    String decryptedString = messageObject.getMessage();
    if (decryptedString == null || decryptedString.equals("")) {
        //decryption failed
        Log.d(TAG, "Message decryption failed");
        return;
    }

    /*
     * Sixth, we transmit message to the application
     */
    //Send the message to the app
    Intent i = new Intent("messageArrived");
    i.putExtra("senderId", messageObject.getSender());
    i.putExtra("senderName", identityMap.get(messageObject.getSender()).getName());
    i.putExtra("message", decryptedString);
    LocalBroadcastManager.getInstance(context).sendBroadcast(i);
}

From source file:ch.bfh.evoting.alljoyn.BusHandler.java

/**
 * Helper method extracting an identity from a received message
 * @param messageObject the original message received
 *///w ww.  j a  v  a 2s  . co  m
private void extractIdentity(AllJoynMessage messageObject) {
    Log.d(TAG, "Exctracting identity " + messageObject.getMessage());

    //Get the name and its corresponding key key
    StringTokenizer tokenizer = new StringTokenizer(messageObject.getMessage(), MESSAGE_PARTS_SEPARATOR);
    if (tokenizer.countTokens() != 2) {
        //malformed string
        Log.d(TAG, "String was not composed of 2 parts");
        return;
    }

    String peerId = messageObject.getSender();
    //then peerName
    String peerName = (String) tokenizer.nextElement();
    //and finally peerKey
    String peerKey = (String) tokenizer.nextElement();

    Identity newIdentity = new Identity(peerName, messageAuthenticater.decodePublicKey(peerKey));

    //Check if identity is already known
    if (identityMap.containsKey(peerId)) {
        //if yes, check if the same identity as received before
        if (!identityMap.get(peerId).equals(newIdentity)) {
            //If not someone is trying to impersonate somebody else
            Intent intent = new Intent("attackDetected");
            intent.putExtra("type", 1);
            LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
            Log.e(TAG, "Two different data received for peer " + peerId);
            return;
        }
    } else {
        boolean verification = messageObject.verifyMessage(newIdentity.getPublicKey());
        if (verification) {
            //Save the new identity
            Log.d(TAG, "identity received " + newIdentity.getName());
            identityMap.put(peerId, newIdentity);
            this.sendMyIdentity();
            //Update the UI
            LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent("participantStateUpdate"));

            if (peerId.equals(signatureVerificationTask.getSender())) {
                verifySignatureSalt();
            }
        } else {
            Log.e(TAG, "Wrong signature for identiy message from " + peerId);
            return;
        }
    }
}

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

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

    if (mIncomingDraft == null) {
        mComposeMessageView.requestDraftMessage(mClearLocalDraft);
    } else {//from ww  w. j  a  v a2s.  c o  m
        mComposeMessageView.setDraftMessage(mIncomingDraft);
        mIncomingDraft = null;
    }
    mClearLocalDraft = false;

    // On resume, check if there's a pending request for resuming message compose. This
    // may happen when the user commits the contact selection for a group conversation and
    // goes from compose back to the conversation fragment.
    if (mHost.shouldResumeComposeMessage()) {
        mComposeMessageView.resumeComposeMessage();
    }

    setConversationFocus();

    // On resume, invalidate all message views to show the updated timestamp.
    mAdapter.notifyDataSetChanged();

    LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mConversationSelfIdChangeReceiver,
            new IntentFilter(UIIntents.CONVERSATION_SELF_ID_CHANGE_BROADCAST_ACTION));
}

From source file:bf.io.openshop.ux.MainActivity.java

@Override
protected void onResume() {
    super.onResume();
    // FB base events logging
    AppEventsLogger.activateApp(this);

    // GCM registration
    LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
            new IntentFilter(SettingsMy.REGISTRATION_COMPLETE));
}

From source file:ch.bfh.evoting.alljoyn.BusHandler.java

/**
 * Helper method extracting the salt from the recived message
 * @param message original message containing the salt
 *//*from  w  ww  .  jav a  2  s.c  o  m*/
private void saltReceived(AllJoynMessage message) {

    Log.d(TAG, "Exctracting salt " + message.getMessage());

    if (messageEncrypter.getSalt() == null) {
        messageEncrypter.setSalt(message.getMessage());
    } else {
        if (!messageEncrypter.getSalt().equals(message.getMessage())) {
            //someone is sending a false salt
            Intent intent = new Intent("attackDetected");
            intent.putExtra("type", 3);
            LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
            Log.e(TAG, "Different salts have been received!");
            return;
        }
    }

    //Add to verification task in order to check the signature later
    signatureVerificationTask = message;

    //send my identity
    if (connected) {
        this.sendMyIdentity();
    }
}

From source file:bf.io.openshop.ux.MainActivity.java

@Override
protected void onPause() {
    super.onPause();
    // FB base events logging
    AppEventsLogger.deactivateApp(this);
    MyApplication.getInstance().cancelPendingRequests(CONST.MAIN_ACTIVITY_REQUESTS_TAG);

    // GCM registration
    LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver);
}