Skip to content

Commit

Permalink
new ChanSwitch view
Browse files Browse the repository at this point in the history
fix playback failed because of substring
display time when pausing
  • Loading branch information
lz233 committed Oct 23, 2021
1 parent 2477fc0 commit 40f708c
Show file tree
Hide file tree
Showing 18 changed files with 160 additions and 57 deletions.
2 changes: 1 addition & 1 deletion neteasecloudmusic/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ android {
minSdkVersion 19
targetSdkVersion 31
versionCode = date.format("yyyyMMdd").toInteger()
versionName "1.3"
versionName "1.3.1"
multiDexEnabled true
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
ndkVersion '22.1.7171670'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package moe.lz233.meizugravity.cloudmusic.logic.dao
import moe.lz233.meizugravity.cloudmusic.App

object BaseDao {
var soundQuality: Long
get() = App.sp.getLong("soundQuality", 999000)
set(value) = App.editor.putLong("soundQuality", value).apply()
var soundQuality: Int
get() = App.sp.getInt("soundQuality", 999000)
set(value) = App.editor.putInt("soundQuality", value).apply()
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package moe.lz233.meizugravity.cloudmusic.logic.network

import moe.lz233.meizugravity.cloudmusic.logic.network.interceptor.ExoPlayerInterceptor
import moe.lz233.meizugravity.cloudmusic.logic.network.interceptor.RequestInterceptor
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

object ServiceCreator {
val BASE_HOST = "music.163.com"
val BASE_URL = "https://$BASE_HOST"
const val BASE_HOST = "music.163.com"
const val BASE_URL = "https://$BASE_HOST"

private val okHttpClient = OkHttpClient.Builder()
.addInterceptor(RequestInterceptor())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package moe.lz233.meizugravity.cloudmusic.logic.network
package moe.lz233.meizugravity.cloudmusic.logic.network.interceptor

import moe.lz233.meizugravity.cloudmusic.utils.LogUtil
import moe.lz233.meizugravity.cloudmusic.utils.ktx.processEapi
Expand All @@ -15,9 +15,9 @@ class ExoPlayerInterceptor : Interceptor {
.getJSONArray("data")
.getJSONObject(0)
.getString("url")
LogUtil.d(url.substring(0, url.indexOf("?authSecret=")))
LogUtil.d("music url: $url")
chain.proceed(chain.request().newBuilder()
.url(url.substring(0, url.indexOf("?authSecret=")))
.url(url)
.get()
.build())
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package moe.lz233.meizugravity.cloudmusic.logic.network
package moe.lz233.meizugravity.cloudmusic.logic.network.interceptor

import moe.lz233.meizugravity.cloudmusic.logic.dao.UserDao
import moe.lz233.meizugravity.cloudmusic.utils.ktx.processEapi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import retrofit2.http.Query

interface SongService {
@GET("/eapi/song/enhance/player/url")
fun getSongUrl(@Query("id") musicId: Long, @Query("br") bitRate: Long = BaseDao.soundQuality): Call<SongUrlResponse>
fun getSongUrl(@Query("id") musicId: Long, @Query("br") bitRate: Int = BaseDao.soundQuality): Call<SongUrlResponse>

@GET("/eapi/song/lyric")
fun getSongLyric(@Query("id") musicId: Long, @Query("lv") lv: Int = -1, @Query("kv") kv: Int = -1, @Query("tv") tv: Int = -1): Call<SongLyricResponse>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import android.view.KeyEvent
import moe.lz233.meizugravity.cloudmusic.App
import moe.lz233.meizugravity.cloudmusic.BuildConfig
import moe.lz233.meizugravity.cloudmusic.databinding.ActivityAboutBinding
import moe.lz233.meizugravity.cloudmusic.logic.dao.BaseDao
import moe.lz233.meizugravity.cloudmusic.ui.BaseActivity
import moe.lz233.meizugravity.cloudmusic.utils.LogUtil
import moe.lz233.meizugravity.cloudmusic.utils.QRCodeUtil
import kotlin.system.exitProcess

Expand All @@ -21,22 +19,6 @@ class AboutActivity : BaseActivity() {
setContentView(viewBuilding.root)
viewBuilding.aboutImageView.setImageBitmap(QRCodeUtil.createQRCodeBitmap("https://github.com/lz233/MeizuGravity/blob/master/NETEASECLOUDMUSIC.md", 100, 100))
viewBuilding.versionTextView.text = "${BuildConfig.VERSION_NAME} (${BuildConfig.VERSION_CODE})"
viewBuilding.soundQualityButton.setOnClickListener {
when (BaseDao.soundQuality) {
999000L -> {
BaseDao.soundQuality = 128000
LogUtil.toast("当前音质:低音质")
}
320000L -> {
BaseDao.soundQuality = 999000
LogUtil.toast("当前音质:无损")
}
128000L -> {
BaseDao.soundQuality = 320000
LogUtil.toast("当前音质:高音质")
}
}
}
viewBuilding.logoutButton.setOnClickListener {
App.editor.remove("userLogin").commit()
exitProcess(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class PlayingActivity : BaseActivity() {
clipToPadding = false
}
if (MediaManager.playlistItemList.isNotEmpty()) onMediaChange()
refreshTextClock()
MediaManager.addMediaSwitchChange(mediaTrackChangeListener)
MediaManager.addProgressListener(mediaProgressListener)
}
Expand Down Expand Up @@ -130,15 +131,18 @@ class PlayingActivity : BaseActivity() {
when (viewBuilding.mainViewPager2.currentItem) {
0 -> {
}
1 -> when (MediaManager.isPlaying()) {
true -> {
MediaManager.pause()
setScreenBrightnessValue(0.1f)
}
false -> {
MediaManager.play()
setScreenBrightnessValue(1.0f)
1 -> {
when (MediaManager.isPlaying()) {
true -> {
MediaManager.pause()
setScreenBrightnessValue(0.1f)
}
false -> {
MediaManager.play()
setScreenBrightnessValue(1.0f)
}
}
refreshTextClock()
}
2 -> launch {
val likeResponse = MediaManager.currentId()!!.toLong().like()
Expand All @@ -164,6 +168,16 @@ class PlayingActivity : BaseActivity() {
return super.onKeyDown(keyCode, event)
}

private fun refreshTextClock() = if (MediaManager.isPlaying()) {
viewBuilding.timeTextClock.visibility = View.GONE
viewBuilding.dateTextClock.visibility = View.GONE
viewBuilding.coverImageView.visibility = View.VISIBLE
} else {
viewBuilding.timeTextClock.visibility = View.VISIBLE
viewBuilding.dateTextClock.visibility = View.VISIBLE
viewBuilding.coverImageView.visibility = View.GONE
}

private fun showMenu() {
adapter.notifyDataSetChanged()
viewBuilding.lrcView.visibility = View.GONE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.os.Handler
import android.os.Looper
import android.widget.Toast
import moe.lz233.meizugravity.cloudmusic.App
import moe.lz233.meizugravity.cloudmusic.BuildConfig
import android.util.Log as ALog

object LogUtil {
Expand Down Expand Up @@ -32,8 +31,8 @@ object LogUtil {
}
}
} else {
f("MEIZUGRAVITYCLOUDMUSIC", str)
toast(str,force = false)
f("CLOUDMUSIC", str)
toast(str, force = false)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package moe.lz233.meizugravity.cloudmusic.utils.ktx

fun Boolean.toInt() = if (this) 1 else 0
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fun Interceptor.Chain.processEapi(): Response {
val params = "$path-$nobodyKnowThis-$json-$nobodyKnowThis-$nobodyUseMD5"
LogUtil.d(params)
return this.proceed(Request.Builder()
.addHeader("Cookie", "MUSIC_U=${UserDao.cookie}")
.addHeader("Cookie", "MUSIC_U=${UserDao.cookie}; appver=6.5.0; versioncode=164; buildver=${System.currentTimeMillis()}; os=android")
.url(url)
.post(FormBody.Builder()
.add("params", AESUtil.encrypt(params))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ package moe.lz233.meizugravity.cloudmusic.utils.ktx
import moe.lz233.meizugravity.cloudmusic.logic.dao.BaseDao
import moe.lz233.meizugravity.cloudmusic.logic.network.ServiceCreator

fun Long.getSongUrl(bitRate: Long = BaseDao.soundQuality) = "${ServiceCreator.BASE_URL}/eapi/song/enhance/player/url?ids=[$this]&br=$bitRate"
fun Long.getSongUrl(bitRate: Int = BaseDao.soundQuality) = "${ServiceCreator.BASE_URL}/eapi/song/enhance/player/url?ids=[$this]&br=$bitRate"
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package moe.lz233.meizugravity.cloudmusic.view

import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatButton
import androidx.core.content.ContextCompat
import moe.lz233.meizugravity.cloudmusic.App
import moe.lz233.meizugravity.cloudmusic.R
import moe.lz233.meizugravity.cloudmusic.utils.ktx.toInt

class ChanSwitch @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : AppCompatButton(context, attrs) {

private var valueType = -1
private var key: String?
private var value: List<String>
private var valueDefault: String
private var defaultBoolean = false
private var baseText: String?
private var optionText: List<String>

init {
background = ContextCompat.getDrawable(getContext(), R.drawable.button_background)
val typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.ChanSwitch)
key = typedArray.getString(R.styleable.ChanSwitch_key)
value = typedArray.getValueAndType().split('|')
valueDefault = value[typedArray.getInteger(R.styleable.ChanSwitch_valueDefault, 0)]
baseText = typedArray.getString(R.styleable.ChanSwitch_baseText)
optionText = typedArray.getString(R.styleable.ChanSwitch_optionText).toString().split('|')
refreshText()
setOnClickListener {
when (valueType) {
0 -> App.editor.putBoolean(key, !App.sp.getBoolean(key, defaultBoolean)).commit()
1 -> App.editor.putString(key, value[(value.indexOf(App.sp.getString(key, valueDefault) + 1)) % value.size]).commit()
2 -> App.editor.putInt(key, value[(value.indexOf(App.sp.getInt(key, valueDefault.toInt()).toString()) + 1) % value.size].toInt()).commit()
}
refreshText()
}
typedArray.recycle()
}

private fun refreshText() {
text = when (valueType) {
0 -> "$baseText${optionText[1 - App.sp.getBoolean(key, defaultBoolean).toInt()]}"
2 -> "$baseText${optionText[value.indexOf(App.sp.getInt(key, valueDefault.toInt()).toString())]}"
else -> "$baseText${optionText[value.indexOf(App.sp.getString(key, valueDefault))]}"
}
}

private fun TypedArray.getValueAndType(): String = when {
getBoolean(R.styleable.ChanSwitch_valueIsBoolean, false) -> {
valueType = 0
defaultBoolean = getBoolean(R.styleable.ChanSwitch_valueDefaultBoolean, false)
""
}
getString(R.styleable.ChanSwitch_valueString) != null -> {
valueType = 1
getString(R.styleable.ChanSwitch_valueString).toString()
}
getString(R.styleable.ChanSwitch_valueInteger) != null -> {
valueType = 2
getString(R.styleable.ChanSwitch_valueInteger).toString()
}
else -> {
valueType = -1
""
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@
import android.text.style.ScaleXSpan;
import android.util.AttributeSet;

// Decompile from A8Speaker by jadx

public class ChanTextView extends androidx.appcompat.widget.AppCompatTextView {
private static Typeface mTypeface = null;
private String TAG;
private CharSequence originalText;
private float spacing;
private CharSequence text;

public ChanTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.TAG = "ChanTextView";
this.spacing = 0.0f;
this.originalText = "";
setFont(context);
Expand Down Expand Up @@ -75,10 +74,4 @@ public void onDraw(Canvas canvas) {
setHorizontalFadingEdgeEnabled(false);
}

public class Spacing {
public static final float NORMAL = 0.0f;

public Spacing() {
}
}
}
13 changes: 8 additions & 5 deletions neteasecloudmusic/src/main/res/layout/activity_about.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand Down Expand Up @@ -58,14 +59,16 @@
android:text="@string/aboutSummary"
android:textSize="@dimen/text_20" />

<Button
android:id="@+id/soundQualityButton"
<moe.lz233.meizugravity.cloudmusic.view.ChanSwitch
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10px"
android:background="@drawable/button_background"
android:text="音质选择"
android:textSize="@dimen/text_20" />
android:textSize="@dimen/text_20"
app:baseText="音质选择"
app:key="soundQuality"
app:optionText="128K|256K|320K|FLAC"
app:valueDefault="3"
app:valueInteger="128000|256000|320000|999000" />

<Button
android:id="@+id/logoutButton"
Expand Down
26 changes: 26 additions & 0 deletions neteasecloudmusic/src/main/res/layout/activity_playing.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,32 @@
android:animateLayoutChanges="true"
android:clipChildren="false">

<TextClock
android:id="@+id/timeTextClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="40px"
android:layout_marginStart="10px"
android:format12Hour="HH:mm"
android:format24Hour="HH:mm"
android:gravity="center"
android:textSize="@dimen/text_50" />

<TextClock
android:id="@+id/dateTextClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginTop="40px"
android:layout_marginStart="10px"
android:format12Hour="EEEE M月d日"
android:format24Hour="EEEE M月d日"
android:gravity="center"
android:textSize="@dimen/text_20" />

<ImageView
android:id="@+id/coverImageView"
android:layout_width="100px"
Expand Down
13 changes: 12 additions & 1 deletion neteasecloudmusic/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<resources></resources>
<resources>
<declare-styleable name="ChanSwitch">
<attr name="key" format="string" />
<attr name="valueIsBoolean" format="boolean" />
<attr name="valueString" format="string" />
<attr name="valueInteger" format="string" />
<attr name="valueDefault" format="integer" />
<attr name="valueDefaultBoolean" format="boolean" />
<attr name="baseText" format="string" />
<attr name="optionText" format="string" />
</declare-styleable>
</resources>
1 change: 1 addition & 0 deletions neteasecloudmusic/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<dimen name="text_80">80px</dimen>
<dimen name="text_60">60px</dimen>
<dimen name="text_30">30px</dimen>
<dimen name="text_50">50px</dimen>
<dimen name="text_26">26px</dimen>
<dimen name="text_21">21px</dimen>
<dimen name="text_20">20px</dimen>
Expand Down

0 comments on commit 40f708c

Please sign in to comment.