Link to home
Start Free TrialLog in
Avatar of Rohit Bajaj
Rohit BajajFlag for India

asked on

What is EventBus in Android

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
ASKER CERTIFIED SOLUTION
Avatar of Am P
Am P
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial