Skip to content

Commit

Permalink
1.0.0-alpha15
Browse files Browse the repository at this point in the history
- 'Schedule Shutdown' feature
- Pause button will abandon audio focus if it is clicked by user
- Other stability fixes
  • Loading branch information
Senorsen committed Sep 30, 2017
1 parent d731677 commit 9b80d8f
Show file tree
Hide file tree
Showing 15 changed files with 227 additions and 19 deletions.
4 changes: 2 additions & 2 deletions wukong/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ android {
applicationId "com.senorsen.wukong"
minSdkVersion 21
targetSdkVersion 26
versionCode 9012
versionName "1.0.0-alpha8.4"
versionCode 9015
versionName "1.0.0-alpha15"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
Expand Down
73 changes: 66 additions & 7 deletions wukong/src/main/java/com/senorsen/wukong/service/WukongService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,55 @@ class WukongService : Service() {
LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
}

private var mShutdownTimer: Timer? = null
private var mShutdownTask: TimerTask? = null
private var shutdownAfterSongFinished = false
private var shouldShutdownNow = false
var shutdownScheduleAt: Date? = null
get() = field
private set(value) {
field = value
}

fun enableScheduleShutdown(after: Long, afterSongFinished: Boolean) {
if (mShutdownTimer != null) {
mShutdownTask?.cancel()
mShutdownTimer!!.purge()
}
if (after > 0) {
mShutdownTimer = Timer()
mShutdownTask = ShutdownTask()
val calendar = Calendar.getInstance()
calendar.add(Calendar.MILLISECOND, after.toInt())
shutdownScheduleAt = calendar.time
mShutdownTimer!!.schedule(mShutdownTask, after)
shutdownAfterSongFinished = afterSongFinished
} else {
shutdownScheduleAt = null
shutdownAfterSongFinished = false
}
}

private fun doScheduleShutdown() {
val isDay = Calendar.getInstance().get(Calendar.HOUR_OF_DAY) in (6..20)
handler.post {
Toast.makeText(this, if (isDay) R.string.notification_schedule_shutdown_day else R.string.notification_schedule_shutdown_night, Toast.LENGTH_LONG).show()
}
stopSelf()
}

private inner class ShutdownTask : TimerTask() {
override fun run() {
if (shutdownAfterSongFinished) {
Log.i(TAG, "shutdown task triggered, will stop after song finish")
shouldShutdownNow = true
} else {
Log.i(TAG, "shutdown task triggered, stop")
doScheduleShutdown()
}
}
}

fun fetchConfiguration(http: HttpClient = this.http): Configuration? {
try {
configuration = http.getConfiguration()
Expand Down Expand Up @@ -276,11 +325,11 @@ class WukongService : Service() {
if (intent != null) {
Log.d(TAG, intent.action)
when (intent.action) {
ACTION_TURN_OFF -> stopService(Intent(this@WukongService, WukongService::class.java))
ACTION_TURN_OFF -> stopSelf()

ACTION_DOWNVOTE -> sendDownvote(intent.getStringExtra("song").toRequestSong())

ACTION_PAUSE -> switchPause()
ACTION_PAUSE -> switchPause(true)

ACTION_PLAY -> switchPlay()
}
Expand Down Expand Up @@ -504,6 +553,11 @@ class WukongService : Service() {
}

Protocol.PLAY -> {
if (shouldShutdownNow) {
doScheduleShutdown()
return
}

if (protocol.song == null) {
setNotification("Channel $channelId: play null")
return
Expand Down Expand Up @@ -564,6 +618,7 @@ class WukongService : Service() {
} catch (e: Exception) {
e.printStackTrace()
}
if (shouldShutdownNow) doScheduleShutdown()
}
}
} catch (e: Exception) {
Expand Down Expand Up @@ -639,7 +694,7 @@ class WukongService : Service() {
} catch (e: Exception) {
handler.post {
Toast.makeText(applicationContext, "Error: " + e.message, Toast.LENGTH_LONG).show()
stopService(Intent(this, WukongService::class.java))
stopSelf()
}
}

Expand Down Expand Up @@ -680,14 +735,16 @@ class WukongService : Service() {
}
}

fun switchPause() {
fun switchPause(triggeredByUser: Boolean = false) {
try {
Log.d(TAG, "switchPause")
mediaPlayer.pause()
isPaused = true
setNotification()
updateMediaSessionState()
onUpdateSongInfo()
if (triggeredByUser)
am.abandonAudioFocus(afChangeListener)
} catch (e: Exception) {
Log.e(TAG, "switchPause", e)
}
Expand All @@ -700,6 +757,7 @@ class WukongService : Service() {
mediaPlayer.start()
mediaPlayer.setVolume(1.0f, 1.0f)
isPaused = false
requestAudioFocus()
setNotification()
updateMediaSessionState()
onUpdateSongInfo()
Expand All @@ -715,20 +773,21 @@ class WukongService : Service() {
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {

if (intent != null) {
startConnect()
}

return START_REDELIVER_INTENT
}

override fun onDestroy() {

stopPrevConnect()
onServiceStopped()
onUpdateChannelInfo(null, null)
unregisterReceiver(receiver)
mTask?.cancel()
mTimer?.purge()
mShutdownTask?.cancel()
mShutdownTimer?.purge()

super.onDestroy()
Log.d(TAG, "onDestroy")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class MainFragment : Fragment() {
if (wukongService.isPaused)
wukongService.switchPlay()
else
wukongService.switchPause()
wukongService.switchPause(true)
}
}

Expand Down
70 changes: 69 additions & 1 deletion wukong/src/main/java/com/senorsen/wukong/ui/WukongActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import android.os.Bundle
import android.os.Handler
import android.os.IBinder
import android.preference.PreferenceManager
import android.support.design.widget.CoordinatorLayout
import android.support.design.widget.NavigationView
import android.support.design.widget.Snackbar
import android.support.v4.content.LocalBroadcastManager
Expand Down Expand Up @@ -123,13 +124,22 @@ class WukongActivity : AppCompatActivity() {
mLrcTask = LrcTask()
mTimer!!.scheduleAtFixedRate(mLrcTask, 1000, 500)
}
if (mScheduleShutdownPullTimer == null) {
mScheduleShutdownPullTimer = Timer()
mScheduleShutdownPullTask = ScheduleShutdownPullTask()
mScheduleShutdownPullTimer!!.scheduleAtFixedRate(mScheduleShutdownPullTask, 0, 1000)
}
}

override fun onStop() {
handler.removeCallbacks(bindRunnable)
if (connected) unbindService(serviceConnection)
mTimer?.cancel()
mLrcTask?.cancel()
mTimer?.purge()
mTimer = null
mScheduleShutdownPullTask?.cancel()
mScheduleShutdownPullTimer?.purge()
mScheduleShutdownPullTimer = null
super.onStop()
}

Expand Down Expand Up @@ -272,6 +282,9 @@ class WukongActivity : AppCompatActivity() {
}
}

R.id.nav_schedule_shutdown ->
showScheduleShutdownDialog()

R.id.nav_start_service ->
startWukongService()

Expand Down Expand Up @@ -471,6 +484,43 @@ class WukongActivity : AppCompatActivity() {
}
}

private var mScheduleShutdownPullTimer: Timer? = null
private var mScheduleShutdownPullTask: TimerTask? = null

private inner class ScheduleShutdownPullTask : TimerTask() {
override fun run() {
if (wukongService != null) {
updateScheduleShutdownTimeLeft(wukongService!!.shutdownScheduleAt)
} else {
updateScheduleShutdownTimeLeft(null)
}
}
}

fun updateScheduleShutdownTimeLeft(scheduleShutdownAt: Date?) {
if (scheduleShutdownAt != null) {
val millis = scheduleShutdownAt.time - Date().time
val text = when {
millis < 1000 -> resources.getString(R.string.schedule_shutdown_shortly)
millis > 60000 -> resources.getQuantityString(R.plurals.time_minute, ((millis + 30000) / 60000).toInt(), ((millis + 30000) / 60000).toInt())
else -> resources.getQuantityString(R.plurals.time_second, (millis / 1000 % 60).toInt(), (millis / 1000 % 60).toInt())
}
handler.post {
findViewById<NavigationView>(R.id.left_drawer)
?.menu
?.findItem(R.id.nav_schedule_shutdown)
?.setTitle(getString(R.string.schedule_shutdown) + ": " + text)
}
} else {
handler.post {
findViewById<NavigationView>(R.id.left_drawer)
?.menu
?.findItem(R.id.nav_schedule_shutdown)
?.setTitle(R.string.schedule_shutdown)
}
}
}

private val defaultArtwork = R.mipmap.ic_default_art
private val wukongArtwork = R.mipmap.ic_launcher

Expand Down Expand Up @@ -560,6 +610,24 @@ class WukongActivity : AppCompatActivity() {
builder.show()
}

private fun showScheduleShutdownDialog() {
val builder = AlertDialog.Builder(this)
builder.setTitle(R.string.schedule_shutdown)
.setItems(R.array.schedule_shutdown_entries) { dialog, which ->
val millis = resources.getIntArray(R.array.schedule_shutdown_values)[which].toLong()
if (wukongService != null) {
wukongService!!.enableScheduleShutdown(millis, true)
if (millis > 0)
Snackbar.make(findViewById<CoordinatorLayout>(R.id.mainCoordinatorLayout), getString(R.string.will_schedule_shutdown_at, resources.getStringArray(R.array.schedule_shutdown_hints)[which]), Snackbar.LENGTH_SHORT).show()
else
Snackbar.make(findViewById<CoordinatorLayout>(R.id.mainCoordinatorLayout), getString(R.string.cancel_schedule_shutdown), Snackbar.LENGTH_SHORT).show()
} else {
Snackbar.make(findViewById<CoordinatorLayout>(R.id.mainCoordinatorLayout), R.string.start_service_first, Snackbar.LENGTH_SHORT).show()
}
}
builder.show()
}

private fun startWukongService() {
val pref = getSharedPreferences("wukong", Context.MODE_PRIVATE)
val cookies = pref.getString("cookies", "")
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion wukong/src/main/res/layout/nav_header.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="#eee"
android:background="#FFF3E0"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:padding="10dp">

Expand Down
4 changes: 4 additions & 0 deletions wukong/src/main/res/menu/drawer_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
android:id="@+id/nav_settings"
android:icon="@drawable/ic_settings"
android:title="@string/settings" />
<item
android:id="@+id/nav_schedule_shutdown"
android:icon="@drawable/ic_schedule_shutdown"
android:title="@string/schedule_shutdown"/>
<item
android:id="@+id/nav_start_service"
android:icon="@drawable/ic_drawer_start_service"
Expand Down
24 changes: 24 additions & 0 deletions wukong/src/main/res/values-zh-rCN/arrays.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
<string-array name="pref_preferAudioQuality_entries">
<item>极佳(无损) > 600kbps</item>
<item>高 ~ 320kbps</item>
<item>中 ~ 192kbps</item>
<item>低 ~ 128kbps</item>
</string-array>

<string-array name="schedule_shutdown_entries">
<item>15分钟后</item>
<item>30分钟后</item>
<item>1小时后</item>
<item>2小时后</item>
<item>不关闭</item>
</string-array>

<string-array name="schedule_shutdown_hints">
<item>15分钟</item>
<item>半小时</item>
<item>一小时</item>
<item>2小时</item>
</string-array>
</resources>
14 changes: 14 additions & 0 deletions wukong/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,18 @@
<string name="sync_playlists_empty_or_error">同步结果为空或错误</string>
<string name="sync_playlists_error">同步错误:%1$s 。震惊!</string>
<string name="sync_playlists_configuration_error">同步前配置错误</string>
<string name="pref_dialog_title_max_audio_quality">音质偏好(数据)</string>
<string name="schedule_shutdown">定时关闭</string>
<plurals name="time_minute">
<item quantity="other">%1$d 分钟</item>
</plurals>
<plurals name="time_second">
<item quantity="other">%1$d 秒</item>
</plurals>
<string name="schedule_shutdown_shortly">即将</string>
<string name="start_service_first">请先打开悟空音乐服务。</string>
<string name="will_schedule_shutdown_at">悟空音乐将在%1$s后自动关闭。</string>
<string name="cancel_schedule_shutdown">定时关闭已取消。</string>
<string name="notification_schedule_shutdown_night">悟空音乐已关闭。晚安好梦!</string>
<string name="notification_schedule_shutdown_day">悟空音乐已关闭。祝您度过美满的一天!</string>
</resources>
35 changes: 29 additions & 6 deletions wukong/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="MissingTranslation">
<string-array name="pref_preferAudioQuality_values">
<resources xmlns:tools="http://schemas.android.com/tools">
<string-array name="pref_preferAudioQuality_values" tools:ignore="MissingTranslation">
<item>lossless</item>
<item>high</item>
<item>medium</item>
<item>low</item>
</string-array>

<string-array name="pref_preferAudioQuality_entries">
<item>Lossless</item>
<item>High</item>
<item>Medium</item>
<item>Low</item>
<item>Lossless > 600kbps</item>
<item>High ~ 320kbps</item>
<item>Medium ~ 192kbps</item>
<item>Low ~ 128kbps</item>
</string-array>

<string-array name="schedule_shutdown_entries">
<item>After 15 minutes</item>
<item>After half an hour</item>
<item>After one hour</item>
<item>After two hours</item>
<item>Remove schedule</item>
</string-array>

<string-array name="schedule_shutdown_hints">
<item>15 minutes</item>
<item>half an hour</item>
<item>one hour</item>
<item>two hours</item>
</string-array>

<integer-array name="schedule_shutdown_values" tools:ignore="MissingTranslation">
<item>900000</item>
<item>1800000</item>
<item>3600000</item>
<item>7200000</item>
<item>0</item>
</integer-array>
</resources>
Loading

0 comments on commit 9b80d8f

Please sign in to comment.