Skip to content
This repository has been archived by the owner on Jun 30, 2022. It is now read-only.

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
bin456789 committed Mar 28, 2019
1 parent 3f54c84 commit 9a6abbc
Show file tree
Hide file tree
Showing 14 changed files with 204 additions and 210 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ android {
defaultConfig {
applicationId "bin.xposed.Unblock163MusicClient"
minSdkVersion 14
// target Android P 之后,在安装过程中会对 APP 文件目录实行严格的 SELinux 限制,即使 makeWorkdReadable,其他 APP 也无法读取插件的任何配置文件
// target Android P 之后,在安装过程中会对 APP 文件目录实行严格的 SELinux 限制,即使 makeWorldReadable,其他 APP 也无法读取插件的任何配置文件
targetSdkVersion 27
versionCode 31
versionName = "0.0.${versionCode}"
Expand Down
2 changes: 1 addition & 1 deletion app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
-keep class bin.xposed.Unblock163MusicClient.Main

-keepclassmembers class bin.xposed.Unblock163MusicClient.HookerDispatcher* {
void dispatch(***);
void dispatch(**);
}

-keepclassmembernames class bin.xposed.Unblock163MusicClient.ui.SettingsActivity {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

import static bin.xposed.Unblock163MusicClient.CloudMusicPackage.ClassHelper.getFilteredClasses;
import static bin.xposed.Unblock163MusicClient.Utils.log;
import static de.robv.android.xposed.XposedBridge.invokeOriginalMethod;
import static de.robv.android.xposed.XposedHelpers.callMethod;
import static de.robv.android.xposed.XposedHelpers.callStaticMethod;
import static de.robv.android.xposed.XposedHelpers.findClass;
Expand Down Expand Up @@ -138,6 +139,7 @@ public static class MusicInfo {
private static Class clazz;

private final Object musicInfo;
private static Method hasCopyRightMethod;

public MusicInfo(Object musicInfo) {
this.musicInfo = musicInfo;
Expand All @@ -151,8 +153,24 @@ public static Class getClazz() {
return clazz;
}

public static Method getHasCopyRightMethod() {
if (hasCopyRightMethod == null) {
hasCopyRightMethod = findMethodExact(getClazz(), "hasCopyRight");
}
return hasCopyRightMethod;
}

public static boolean isStarred(long musicId) {
return (boolean) callStaticMethod(MusicInfo.getClazz(), "isStarred", musicId);
return (boolean) callStaticMethod(getClazz(), "isStarred", musicId);
}

public boolean hasCopyRight() throws InvocationTargetException, IllegalAccessException {
if (Settings.isPreventGrayEnabled()) {
return (boolean) invokeOriginalMethod(getHasCopyRightMethod(), musicInfo, null);
} else {
// workaround "method not hooked, cannot call original method"
return (boolean) getHasCopyRightMethod().invoke(musicInfo);
}
}

public long getMatchedMusicId() {
Expand All @@ -175,9 +193,9 @@ public String get3rdSourceString() throws JSONException, IOException {
song.parseMatchInfo(new JSONObject(jsonStr));
if (song.is3rdPartySong()) {
return String.format("(音源%s:%s - %s)",
song.matchedPlatform,
song.matchedArtistName,
song.matchedSongName);
song.getMatchedPlatform(),
song.getMatchedArtistName(),
song.getMatchedSongName());
}
}
}
Expand Down Expand Up @@ -359,7 +377,7 @@ public Uri getUri() throws IllegalAccessException {

public static class CookieUtil {
private static Class clazz;
private static Method getSingtonMethod;
private static Method getSingletonMethod;
private static Method getListMethod;

static Class getClazz() {
Expand Down Expand Up @@ -398,14 +416,14 @@ static Class getClazz() {


static String getDefaultCookie() throws InvocationTargetException, IllegalAccessException {
if (getSingtonMethod == null) {
getSingtonMethod = XposedHelpers.findMethodsByExactParameters(getClazz(), getClazz())[0];
if (getSingletonMethod == null) {
getSingletonMethod = XposedHelpers.findMethodsByExactParameters(getClazz(), getClazz())[0];
}
if (getListMethod == null) {
getListMethod = XposedHelpers.findMethodsByExactParameters(getClazz(), List.class)[0];
}

Object singleton = getSingtonMethod.invoke(null);
Object singleton = getSingletonMethod.invoke(null);
List cookieList = (List) getListMethod.invoke(singleton);
return Utils.serialCookies(cookieList, cookieMethodMap, "music.163.com");

Expand Down Expand Up @@ -503,15 +521,16 @@ public static Method getLikeButtonOnClickMethod() {
return m.find() ? Integer.parseInt(m.group(1)) : -1;
})
.filter(g -> g.getKey() > -1)
.max((x, y) -> {
int sizeDiff = x.getValue().size() - y.getValue().size();
if (sizeDiff != 0) {
return sizeDiff;
.reduce((x, y) -> {
if (x.getValue().size() > y.getValue().size()) {
return x;
}
int nameDiff = x.getKey() - y.getKey();
return -nameDiff;
}).get().getKey();

if (x.getKey() > y.getKey()) {
return x;
}
return y;
})
.get().getKey();

if (num >= 0) {
likeButtonOnClickMethod = findMethodExact(playerActivitySuperClass.getName() + "$" + num, getClassLoader(), "onClick", View.class);
Expand Down Expand Up @@ -585,7 +604,7 @@ static Class getClazz() {
return clazz;
}

static void showToast(final String text) {
static void showToast(String text) {
Utils.postDelayed(() -> {
try {
// Toast.makeText(NeteaseMusicApplication.getApplication(), text, Toast.LENGTH_SHORT).show();
Expand Down
67 changes: 31 additions & 36 deletions app/src/main/java/bin/xposed/Unblock163MusicClient/Handler.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package bin.xposed.Unblock163MusicClient;

import com.google.common.collect.Iterables;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand All @@ -27,7 +23,7 @@
import static bin.xposed.Unblock163MusicClient.Utils.log;

public class Handler {
private static final String XAPI = "http://xmusic.xmusic.top/xapi/v1/";
private static final String XAPI = "https://xmusic.xmusic.top/xapi/v1/";
private static final Pattern REX_PL = Pattern.compile("\"pl\":(?!999000)\\d+");
private static final Pattern REX_DL = Pattern.compile("\"dl\":(?!999000)\\d+");
private static final Pattern REX_SUBP = Pattern.compile("\"subp\":\\d+");
Expand All @@ -40,13 +36,14 @@ public static String modifyByRegex(String originalContent) {
return originalContent;
}

public static String modifyPlayerOrDownloadApi(String originalContent, HttpEapi eapi, final String from) throws JSONException {
public static String modifyPlayerOrDownloadApi(String originalContent, HttpEapi eapi, String from) throws JSONException {
JSONObject originalJson = new JSONObject(originalContent);

int expectBitrate = 320000;
int expectBitrate;
try {
expectBitrate = Integer.parseInt(eapi.getRequestData().get("br"));
} catch (Throwable ignored) {
expectBitrate = 320000;
}


Expand All @@ -56,13 +53,13 @@ public static String modifyPlayerOrDownloadApi(String originalContent, HttpEapi
Process.process(originalSong, expectBitrate, from);
} else {
JSONArray originalSongs = (JSONArray) data;
Set<Future> futureSet = new HashSet<>();
List<Future> futures = new ArrayList<>();
final int finalExpectBitrate = expectBitrate;
for (int i = 0; i < originalSongs.length(); i++) {
final JSONObject songJson = originalSongs.getJSONObject(i);
futureSet.add(handlerPool.submit(() -> Process.process(songJson, finalExpectBitrate, from)));
JSONObject songJson = originalSongs.getJSONObject(i);
futures.add(handlerPool.submit(() -> Process.process(songJson, finalExpectBitrate, from)));
}
for (Future future : futureSet) {
for (Future future : futures) {
try {
future.get();
} catch (Throwable t) {
Expand Down Expand Up @@ -124,7 +121,7 @@ public static String modifyPub(String originalContent, HttpEapi eapi) throws Thr
return originalContent;
}

private static List<Song> getSongByRemoteApi(final long songId, final int expectBitrate, final String apiName) {
private static List<Song> getSongByRemoteApi(long songId, int expectBitrate, String apiName) {
try {
Map<String, String> map = new LinkedHashMap<String, String>() {{
put("id", String.valueOf(songId));
Expand All @@ -137,9 +134,9 @@ private static List<Song> getSongByRemoteApi(final long songId, final int expect
if (json.getInt("code") == 200) {
Object obj = json.get("data");
if (obj instanceof JSONObject) {
return Collections.singletonList(Song.parseFromOther((JSONObject) obj));
return Collections.singletonList(Song.parse((JSONObject) obj));
} else if (obj instanceof JSONArray) {
return Song.parseFromOther((JSONArray) obj);
return Song.parse((JSONArray) obj);
}
}
} catch (Throwable t) {
Expand Down Expand Up @@ -172,13 +169,13 @@ static void process(JSONObject oldSongJson, int expectBr, String from) {
}

boolean isNeedToGetP1() {
return !preferSong.isAccessible() || preferSong.br < expectBr;
return !preferSong.isAccessible() || preferSong.getBr() < expectBr;
}

boolean isNeedToEnhance() {
boolean isNeed = false;

if (preferSong.br < expectBr && originalSong.isFee() && !originalSong.isPayed()) {
if (preferSong.getBr() < expectBr && originalSong.isFee() && !originalSong.isPayed()) {
isNeed = true;
}

Expand All @@ -191,7 +188,7 @@ boolean isNeedToEnhance() {
}

boolean isNeedToGet3rdParty() {
return !preferSong.isAccessible() || (preferSong.br < expectBr && isUpgradeBitrateFrom3rdParty());
return !preferSong.isAccessible() || (preferSong.getBr() < expectBr && isUpgradeBitrateFrom3rdParty());
}

boolean isNeedToProcess() {
Expand All @@ -203,18 +200,17 @@ boolean isNeedToProcess() {

return !originalSong.isAccessible() // 听不了
|| originalSong.isFreeTrialFile() // 试听
|| originalSong.br < expectBr; // 音质不够
|| originalSong.getBr() < expectBr; // 音质不够
}

private void addRC(Callable condition, Callable callable) {
private void addRC(Callable condition, Callable<List<Song>> getSongByApi) {
try {
if ((boolean) condition.call()) {
List<Song> songList = new ArrayList<>();
songList.add(preferSong);
songList.addAll((List<Song>) callable.call());
songList.addAll(getSongByApi.call());

Song[] songArray = Iterables.toArray(songList, Song.class);
Song tmp = Song.getPreferSong(songArray);
Song tmp = Song.getPreferSong(songList);
if (tmp != null) {
preferSong = tmp;
}
Expand All @@ -226,7 +222,7 @@ private void addRC(Callable condition, Callable callable) {

void doProcess() throws JSONException {

originalSong = Song.parseFromOther(oldSongJson);
originalSong = Song.parse(oldSongJson);

if (originalSong == null) {
return;
Expand All @@ -245,34 +241,33 @@ void doProcess() throws JSONException {

preferSong = originalSong;

addRC(this::isNeedToGetP1, () -> getSongByRemoteApi(originalSong.id, expectBr, "songs"));
addRC(this::isNeedToEnhance, () -> getSongByRemoteApi(originalSong.id, expectBr, "songx"));
addRC(this::isNeedToGet3rdParty, () -> getSongByRemoteApi(originalSong.id, expectBr, "match"));
addRC(this::isNeedToGetP1, () -> getSongByRemoteApi(originalSong.getId(), expectBr, "songs"));
addRC(this::isNeedToEnhance, () -> getSongByRemoteApi(originalSong.getId(), expectBr, "songx"));
addRC(this::isNeedToGet3rdParty, () -> getSongByRemoteApi(originalSong.getId(), expectBr, "match"));

if (preferSong.getPrefer() > originalSong.getPrefer()) {
oldSongJson.put("br", preferSong.br)
if (preferSong != originalSong) {
oldSongJson.put("br", preferSong.getBr())
.put("code", 200)
.put("gain", 0)
.put("md5", preferSong.md5)
.put("size", preferSong.size)
.put("type", preferSong.type)
.put("url", preferSong.url);
.put("md5", preferSong.getMd5())
.put("size", preferSong.getSize())
.put("type", preferSong.getType())
.put("url", preferSong.getUrl());

try {
File cacheDir = CloudMusicPackage.NeteaseMusicApplication.getMusicCacheDir();
if (preferSong.is3rdPartySong()) {
String fileName = String.format("%s-%s-%s.%s.xp!", preferSong.id, preferSong.br, preferSong.md5, preferSong.type);
String fileName = String.format("%s-%s-%s.%s.xp!", preferSong.getId(), preferSong.getBr(), preferSong.getMd5(), preferSong.getType());
File file = new File(cacheDir, fileName);
String str = preferSong.getMatchedJson().toString();
Utils.writeFile(file, str);
} else {
String start = String.format("%s-", preferSong.id);
String start = String.format("%s-", preferSong.getId());
String end = ".xp!";
File[] files = Utils.findFiles(cacheDir, start, end, null);
Utils.deleteFiles(files);
}
} catch (Throwable t) {
log("read 3rd party tips failed " + originalSong.id, t);
log("read 3rd party tips failed " + originalSong.getId(), t);
}
}
}
Expand Down
Loading

0 comments on commit 9a6abbc

Please sign in to comment.