Solved

What is EventBus in Android

Posted on 2014-11-24
1
347 Views
Last Modified: 2014-11-30
Hi,
I have a following piece of Code :
package kujo.app.application;

import android.content.Context;
import android.media.AudioManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

import de.greenrobot.event.EventBus;
import kujo.app.SoundManager;
import to.talk.logging.Logger;
import to.talk.logging.LoggerFactory;

public class Device {
    private static final String DEVICE_NOT_CREATED_EXCEPTION_MSG = "Create Device before use!";
    private static Context _context;
    private static Device _this;
    private static final Object _lock = new Object();
    private Logger _logger = LoggerFactory.getTrimmer(Device.class.getSimpleName());

    private Device(Context context) {
        _context = context;
    }

    public static Device create(Context context) {
        if (_this == null) {
            synchronized (_lock) {
                if (_this == null) {
                    _this = new Device(context);
                    EventBus.getDefault().register(_this);
                }
            }
        }
        return _this;
    }

    public void onEvent(SoundManager.SpeakerSettingChangedEvent event) {
        boolean isSpeakerEnabled = event.isSpeakerOn();
        setSpeakerPhoneOn(isSpeakerEnabled);
    }

    //not throwing exception to avoid exceptional handling
    public static Device getInstance() {
        return _this;
    }

    public void setSpeakerPhoneOn(boolean val) {
        AudioManager audioManager = getAudioManager();
        audioManager.setSpeakerphoneOn(val);
    }

    public void setMuteStatus(boolean val) {
        _logger.debug("set mute status: {}", val);
        AudioManager audioManager = getAudioManager();
        audioManager.setMicrophoneMute(val);
        _logger.debug("set mute status check (fetching): mic mute: {}, music active: {}",
                audioManager.isMicrophoneMute(), audioManager.isMusicActive());
    }

    public AudioManager getAudioManager() {
        return (AudioManager) _context.getSystemService(Context.AUDIO_SERVICE);
    }

    public boolean canPerformNumberVerificationFromCurrentDevice() {
        return hasCallingAbility();
    }

    public boolean hasCallingAbility() {
        TelephonyManager telephonyManager = (TelephonyManager) _context.getSystemService(Context.TELEPHONY_SERVICE);
        return telephonyManager.getPhoneType() != TelephonyManager.PHONE_TYPE_NONE;

    }

    public String getNetworkType() {
        ConnectivityManager connectivityManager = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        if (networkInfo != null) {
            String subtypeName = networkInfo.getSubtypeName();
            if (subtypeName != null && subtypeName.length() > 0) {
                return networkInfo.getTypeName() + "-" + subtypeName;
            } else {
                return networkInfo.getTypeName();
            }
        } else {
            return "no network";
        }

    }
}

Open in new window



I dont know what an EventBus exactly is. Please help me in finding out what does the following lines in above code do :
public static Device create(Context context) {
        if (_this == null) {
            synchronized (_lock) {
                if (_this == null) {
                    _this = new Device(context);
                    EventBus.getDefault().register(_this);
                }
            }
        }
        return _this;
    }

Thanks
0
Comment
Question by:Rohit Bajaj
1 Comment
 
LVL 20

Accepted Solution

by:
Amitkumar Panchal earned 500 total points
ID: 40463673
EventBus is used to simplify the communication between components.

You can get the complete details about EventBus on its project site (https://github.com/greenrobot/EventBus).

Hope this helps.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

In this article we will discuss all things related to StageFright bug, the most vulnerable bug of android devices.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now