2
\$\begingroup\$

I am using Kotlin from quite some time now, but I could not able to achieve not-null types for all the properties in Kotlin.

After what I learned by watching Kotlin in Google IO and a bit of research, I made all properties not nullable.

Please check my code and correct me if any mistakes.

Thanks in advance.

class MusicService : Service(), PlaybackManager.PlaybackServiceCallback {

    //NotNull
    private val mDelayedStopHandler = DelayedStopHandler(this)
    private val eventBus = EventBus.getDefault()

    //Lateinit
    lateinit var mSessionToken: MediaSessionCompat.Token
    lateinit var mTransportControls: MediaControllerCompat.TransportControls

    //Lazy
    private val mSession: MediaSessionCompat by lazy { MediaSessionCompat(this, "MusicService") }
    private val mMediaNotificationManager: MediaNotificationManager by lazy {
        try {
            MediaNotificationManager(this)
        } catch (e: RemoteException) {
            throw IllegalStateException("Could not create a MediaNotificationManager", e)
        }
    }
    val mPlaybackManager: PlaybackManager by lazy {
        PlaybackManager(mPlayback = LocalPlayer(this), mServiceCallback = this)
    }

    override fun onCreate() {
        LogHelper.d(TAG, "onCreate")
        super.onCreate()

        //Init MediaSessionCompat and TransportControls
        mSessionToken = mSession.sessionToken
        mSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS or MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS)
        mTransportControls = MediaControllerCompat(this, mSession).transportControls
        mSession.setCallback(mPlaybackManager.mMediaSessionCallback)

        //EventBus Reg
        eventBus.reg(this)
        eventBus.post(GetAllMediaEventRequest())

    }

    @Subscribe
    fun onGetAllMediaEventResponse(event: GetAllMediaEventResponse) {
        Timber.d("GetAllMediaEventResponse event.status = ", event.status)
        mPlaybackManager.mMediaData = event.mediaItems
    }
}
\$\endgroup\$
  • \$\begingroup\$ looks like everything is correctly converted! \$\endgroup\$ – dimsuz Jun 14 '17 at 22:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.