jirevwe
6/7/2018 - 1:06 PM

Some Fix for Unexpected Service Shutdown

Some Fix for Unexpected Service Shutdown

<!-- KitKat Fix Activity -->
<activity
    android:name=".player.KitKatFixActivity"
    android:allowTaskReparenting="true"
    android:alwaysRetainTaskState="true"
    android:clearTaskOnLaunch="true"
    android:enabled="true"
    android:excludeFromRecents="true"
    android:finishOnTaskLaunch="true"
    android:noHistory="true"
    android:stateNotNeeded="true"
    android:theme="@android:style/Theme.NoDisplay"/>
/**
 * Fix for KitKat error where the service is killed as soon
 * as the app is swiped away from the Recents menu.
 */
override fun onTaskRemoved(rootIntent: Intent) {
    val intent = Intent(this, KitKatFixActivity::class.java)
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
    startActivity(intent)
}

override fun onDestroy() {
    super.onDestroy()

    eventBus.cleanup()

    with(player!!)
    {
        queueManager.duration = currentPosition
        queueManager.isPlaying = isPlaying
        release()
    }

    isPlayerReleased.set(true)
    isServiceRunning = false

    L.d("AudioPlayerService: onDestroy()")

    if (killedSelf) return

    val intent = Intent(this, KitKatFixActivity::class.java)
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or
            Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
    startActivity(intent)
}
/*
 * Copyright (C) 2014 Saravan Pantham
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ouida.app.player

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import com.ouida.app.utils.others.L

/* KitKat introduced a new bug: swiping away the app from the
 * "Recent Apps" list causes all background services to shut
 * down. To circumvent this issue, this dummy activity will
 * launch momentarily and close (it will be invisible to the
 * user). This will fool the OS into thinking that the service
 * still has an Activity bound to it, and prevent it from being
 * killed.
 *
 * ISSUE #53313:
 * https://code.google.com/p/android/issues/detail?id=53313
 *
 * ISSUE #63618:
 * https://code.google.com/p/android/issues/detail?id=63618
 *
 * General discussion thread:
 * https://groups.google.com/forum/#!topic/android-developers/LtmA9xbrD5A
 */
class KitKatFixActivity : Activity() {

    public override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        L.d("KitKatFixActivity: onCreate()")
        finish()
    }

    override fun onDestroy() {
        L.d("KitKatFixActivity: onDestroy()")

        val serviceIntent = Intent(applicationContext, AudioPlayerService::class.java)
        startService(serviceIntent)
        super.onDestroy()
    }
}