From 2bb4349cc47f020deeaa7b6adff42d2c2bd49ddb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=B9=E5=9D=97=E5=90=9B?= Date: Tue, 30 Nov 2021 00:57:51 +0800 Subject: [PATCH 01/12] =?UTF-8?q?sp=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=20?= =?UTF-8?q?(Test)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/miui/statusbar/lyric/Api.java | 39 +--- .../java/miui/statusbar/lyric/Config.java | 211 +++++------------- .../statusbar/lyric/QuickTitleService.java | 6 +- .../lyric/activity/SettingsActivity.java | 59 +++-- .../miui/statusbar/lyric/hook/MainHook.java | 6 +- .../java/miui/statusbar/lyric/hook/migu.java | 2 +- .../miui/statusbar/lyric/hook/netease.java | 2 +- .../miui/statusbar/lyric/hook/qqmusic.java | 2 +- .../statusbar/lyric/utils/ActivityUtils.java | 37 +-- .../statusbar/lyric/utils/ConfigUtils.java | 151 +++++++++++++ .../miui/statusbar/lyric/utils/Utils.java | 21 +- app/src/main/res/values-zh-rCN/strings.xml | 3 + app/src/main/res/values-zh-rTW/strings.xml | 3 + app/src/main/res/values/strings.xml | 3 + settings.gradle | 2 +- 15 files changed, 313 insertions(+), 234 deletions(-) create mode 100644 app/src/main/java/miui/statusbar/lyric/utils/ConfigUtils.java diff --git a/app/src/main/java/miui/statusbar/lyric/Api.java b/app/src/main/java/miui/statusbar/lyric/Api.java index 6ee0574c..4b21ee01 100644 --- a/app/src/main/java/miui/statusbar/lyric/Api.java +++ b/app/src/main/java/miui/statusbar/lyric/Api.java @@ -11,6 +11,8 @@ import java.io.FileOutputStream; import java.io.IOException; +import miui.statusbar.lyric.utils.ActivityUtils; + /** * 状态栏歌词Api * @author FKj @@ -34,7 +36,7 @@ public class Api { * @param useSystemMusicActive detect your music service running status via system. 是否使用系统检测音乐是否播放 */ public void sendLyric(Context context, String lyric, String icon, String packName, boolean useSystemMusicActive) { - if (new Config().isFileLyric()) { + if (ActivityUtils.getConfig(context).getFileLyric()) { setLyricFile(packName, lyric, icon, useSystemMusicActive); } else { context.sendBroadcast(new Intent() @@ -53,7 +55,7 @@ public void sendLyric(Context context, String lyric, String icon, String packNam * @param context */ public void stopLyric(Context context) { - if (new Config().isFileLyric()) { + if (ActivityUtils.getConfig(context).getFileLyric()) { setLyricFile_stop(); } else { context.sendBroadcast(new Intent().setAction("Lyric_Server").putExtra("Lyric_Type", "app_stop")); @@ -89,37 +91,4 @@ public void setLyricFile_stop() { } catch (Exception ignored) { } } - - public class Config { - JSONObject config = new JSONObject(); - - public Config() { - try { - if (getConfig().equals("")) { - config = new JSONObject(); - return; - } - config = new JSONObject(getConfig()); - } catch (JSONException ignored) { - } - } - - public String getConfig() { - String str = ""; - try { - FileInputStream fileInputStream = new FileInputStream(PATH + "Config.json"); - byte[] bArr = new byte[fileInputStream.available()]; - fileInputStream.read(bArr); - str = new String(bArr); - fileInputStream.close(); - } catch (IOException e) { - e.printStackTrace(); - } - return str; - } - - public boolean isFileLyric() { - return config.optBoolean("FileLyric", false); - } - } } \ No newline at end of file diff --git a/app/src/main/java/miui/statusbar/lyric/Config.java b/app/src/main/java/miui/statusbar/lyric/Config.java index 14f58250..bc395fc5 100644 --- a/app/src/main/java/miui/statusbar/lyric/Config.java +++ b/app/src/main/java/miui/statusbar/lyric/Config.java @@ -1,8 +1,12 @@ package miui.statusbar.lyric; import android.annotation.SuppressLint; +import android.content.SharedPreferences; import android.os.Environment; import android.util.Log; + +import de.robv.android.xposed.XSharedPreferences; +import miui.statusbar.lyric.utils.ConfigUtils; import miui.statusbar.lyric.utils.Utils; import org.json.JSONException; import org.json.JSONObject; @@ -17,18 +21,18 @@ @SuppressLint("LongLogTag") public class Config { static String old_Json = ""; - JSONObject config = new JSONObject(); + ConfigUtils config; public Config() { - try { - if (getConfig().equals("")) { - Log.d("StatusBarLyric: Config()", "cfg isEmpty!"); - config = new JSONObject(); - return; - } - config = new JSONObject(getConfig()); - } catch (JSONException ignored) { - } + config = new ConfigUtils(getConfig()); + } + + public Config(XSharedPreferences xSharedPreferences) { + config = new ConfigUtils(xSharedPreferences); + } + + public Config(SharedPreferences sharedPreferences) { + config = new ConfigUtils(sharedPreferences); } public static String getConfig() { @@ -40,38 +44,29 @@ public static String getConfig() { FileInputStream fileInputStream = new FileInputStream(Utils.PATH + "Config.json"); byte[] bArr = new byte[fileInputStream.available()]; fileInputStream.read(bArr); - old_Json = new String(bArr); str = new String(bArr); + old_Json = str; fileInputStream.close(); } catch (IOException e) { - if (!old_Json.equals("")) { - str = new String(old_Json); - } + str = old_Json; } return str; } - public static void setConfig(String str) { - try { - FileOutputStream fileOutputStream = new FileOutputStream(Utils.PATH + "Config.json"); - fileOutputStream.write(str.getBytes()); - fileOutputStream.close(); - } catch (IOException e) { - e.printStackTrace(); + public void update() { + if (config.hasJson()) { + config.update(getConfig()); + } else { + config.update(); } } - public int getId() { return config.optInt("id", 0); } public void setId(int i) { - try { - config.put("id", i); - setConfig(config.toString()); - } catch (JSONException ignored) { - } + config.put("id", i); } @@ -80,11 +75,7 @@ public Boolean getLyricService() { } public void setLyricService(Boolean bool) { - try { - config.put("LyricService", bool); - setConfig(config.toString()); - } catch (JSONException ignored) { - } + config.put("LyricService", bool); } public int getLyricWidth() { @@ -92,11 +83,7 @@ public int getLyricWidth() { } public void setLyricWidth(int i) { - try { - config.put("LyricWidth", i); - setConfig(config.toString()); - } catch (JSONException ignored) { - } + config.put("LyricWidth", i); } public int getLyricMaxWidth() { @@ -104,11 +91,7 @@ public int getLyricMaxWidth() { } public void setLyricMaxWidth(int i) { - try { - config.put("LyricMaxWidth", i); - setConfig(config.toString()); - } catch (JSONException ignored) { - } + config.put("LyricMaxWidth", i); } public int getLyricPosition() { @@ -116,11 +99,7 @@ public int getLyricPosition() { } public void setLyricPosition(int i) { - try { - config.put("LyricPosition", i); - setConfig(config.toString()); - } catch (JSONException ignored) { - } + config.put("LyricPosition", i); } public Boolean getLyricAutoOff() { @@ -128,11 +107,7 @@ public Boolean getLyricAutoOff() { } public void setLyricAutoOff(Boolean bool) { - try { - config.put("LyricAutoOff", bool); - setConfig(config.toString()); - } catch (JSONException ignored) { - } + config.put("LyricAutoOff", bool); } public Boolean getLockScreenOff() { @@ -140,19 +115,11 @@ public Boolean getLockScreenOff() { } public void setLockScreenOff(Boolean bool) { - try { - config.put("lockScreenOff", bool); - setConfig(config.toString()); - } catch (JSONException ignored) { - } + config.put("lockScreenOff", bool); } public void sethNoticeIcon(Boolean bool) { - try { - config.put("hNoticeIcon", bool); - setConfig(config.toString()); - } catch (JSONException ignored) { - } + config.put("hNoticeIcon", bool); } public String getLyricColor() { @@ -160,11 +127,7 @@ public String getLyricColor() { } public void setLyricColor(String str) { - try { - config.put("LyricColor", str); - setConfig(config.toString()); - } catch (JSONException ignored) { - } + config.put("LyricColor", str); } public Boolean getLyricSwitch() { @@ -172,11 +135,7 @@ public Boolean getLyricSwitch() { } public void setLyricSwitch(Boolean bool) { - try { - config.put("LyricSwitch", bool); - setConfig(config.toString()); - } catch (JSONException ignored) { - } + config.put("LyricSwitch", bool); } public Boolean getHNoticeIco() { @@ -188,11 +147,7 @@ public Boolean getHAlarm() { } public void setHAlarm(Boolean bool) { - try { - config.put("hAlarm", bool); - setConfig(config.toString()); - } catch (JSONException ignored) { - } + config.put("hAlarm", bool); } public Boolean getHNetSpeed() { @@ -200,11 +155,7 @@ public Boolean getHNetSpeed() { } public void setHNetSpeed(Boolean bool) { - try { - config.put("hNetSpeed", bool); - setConfig(config.toString()); - } catch (JSONException ignored) { - } + config.put("hNetSpeed", bool); } public Boolean getHCUK() { @@ -212,11 +163,7 @@ public Boolean getHCUK() { } public void setHCUK(Boolean bool) { - try { - config.put("hCUK", bool); - setConfig(config.toString()); - } catch (JSONException ignored) { - } + config.put("hCUK", bool); } public Boolean getDebug() { @@ -224,11 +171,7 @@ public Boolean getDebug() { } public void setDebug(Boolean bool) { - try { - config.put("debug", bool); - setConfig(config.toString()); - } catch (JSONException ignored) { - } + config.put("debug", bool); } public Boolean getisUsedCount() { @@ -236,11 +179,7 @@ public Boolean getisUsedCount() { } public void setisUsedCount(Boolean bool) { - try { - config.put("isusedcount", bool); - setConfig(config.toString()); - } catch (JSONException ignored) { - } + config.put("isusedcount", bool); } public boolean getIcon() { @@ -248,23 +187,15 @@ public boolean getIcon() { } public void setIcon(Boolean bool) { - try { - config.put("Icon", bool); - setConfig(config.toString()); - } catch (JSONException ignored) { - } + config.put("Icon", bool); } - public String getLyricSpeed() { - return config.optString("LyricSpeed", "1.0"); + public Float getLyricSpeed() { + return config.optFloat("LyricSpeed", 1f); } - public void setLyricSpeed(String str) { - try { - config.put("LyricSpeed", str); - setConfig(config.toString()); - } catch (JSONException ignored) { - } + public void setLyricSpeed(Float f) { + config.put("LyricSpeed", f); } public Boolean getIconAutoColor() { @@ -272,11 +203,7 @@ public Boolean getIconAutoColor() { } public void setIconAutoColor(Boolean bool) { - try { - config.put("IconAutoColor", bool); - setConfig(config.toString()); - } catch (JSONException ignored) { - } + config.put("IconAutoColor", bool); } public String getIconPath() { @@ -284,11 +211,7 @@ public String getIconPath() { } public void setIconPath(String str) { - try { - config.put("IconPath", str); - setConfig(config.toString()); - } catch (JSONException ignored) { - } + config.put("IconPath", str); } public boolean getAntiBurn() { @@ -296,11 +219,7 @@ public boolean getAntiBurn() { } public void setAntiBurn(Boolean bool) { - try { - config.put("antiburn", bool); - setConfig(config.toString()); - } catch (JSONException ignored) { - } + config.put("antiburn", bool); } public int getUsedCount() { @@ -308,11 +227,7 @@ public int getUsedCount() { } public void setUsedCount(int i) { - try { - config.put("usedcount", i); - setConfig(config.toString()); - } catch (JSONException ignored) { - } + config.put("usedcount", i); } public String getAnim() { @@ -320,11 +235,7 @@ public String getAnim() { } public void setAnim(String str) { - try { - config.put("Anim", str); - setConfig(config.toString()); - } catch (JSONException ignored) { - } + config.put("Anim", str); } public String getHook() { @@ -332,11 +243,7 @@ public String getHook() { } public void setHook(String str) { - try { - config.put("Hook", str); - setConfig(config.toString()); - } catch (JSONException ignored) { - } + config.put("Hook", str); } public boolean getFileLyric() { @@ -344,11 +251,7 @@ public boolean getFileLyric() { } public void setFileLyric(boolean bool) { - try { - config.put("FileLyric", bool); - setConfig(config.toString()); - } catch (JSONException ignored) { - } + config.put("FileLyric", bool); } public boolean getLyricStyle() { @@ -356,11 +259,7 @@ public boolean getLyricStyle() { } public void setLyricStyle(boolean bool) { - try { - config.put("LyricStyle", bool); - setConfig(config.toString()); - } catch (JSONException ignored) { - } + config.put("LyricStyle", bool); } public boolean getLShowOnce() { @@ -368,11 +267,7 @@ public boolean getLShowOnce() { } public void setLShowOnce(boolean bool) { - try { - config.put("LShowOnce", bool); - setConfig(config.toString()); - } catch (JSONException ignored) { - } + config.put("LShowOnce", bool); } public boolean getMeizuLyric() { @@ -380,10 +275,6 @@ public boolean getMeizuLyric() { } public void setMeizuLyric(boolean bool) { - try { - config.put("tMeizuLyric", bool); - setConfig(config.toString()); - } catch (JSONException ignored) { - } + config.put("tMeizuLyric", bool); } } \ No newline at end of file diff --git a/app/src/main/java/miui/statusbar/lyric/QuickTitleService.java b/app/src/main/java/miui/statusbar/lyric/QuickTitleService.java index 5758c29d..5881176a 100644 --- a/app/src/main/java/miui/statusbar/lyric/QuickTitleService.java +++ b/app/src/main/java/miui/statusbar/lyric/QuickTitleService.java @@ -5,6 +5,8 @@ import android.os.Build; import android.service.quicksettings.Tile; import android.service.quicksettings.TileService; + +import miui.statusbar.lyric.utils.ActivityUtils; import miui.statusbar.lyric.utils.Utils; @TargetApi(Build.VERSION_CODES.N) @@ -14,7 +16,7 @@ public class QuickTitleService extends TileService { public void onClick() { super.onClick(); Tile tile = getQsTile(); - Config config = new Config(); + Config config = ActivityUtils.getConfig(getApplicationContext()); config.setLyricService(!config.getLyricService()); set(tile, config); } @@ -29,7 +31,7 @@ private void set(Tile tile, Config config) { @Override public void onStartListening() { - Config config = new Config(); + Config config = ActivityUtils.getConfig(getApplicationContext()); Tile tile = getQsTile(); set(tile, config); } diff --git a/app/src/main/java/miui/statusbar/lyric/activity/SettingsActivity.java b/app/src/main/java/miui/statusbar/lyric/activity/SettingsActivity.java index ef31d7f3..43841f3c 100644 --- a/app/src/main/java/miui/statusbar/lyric/activity/SettingsActivity.java +++ b/app/src/main/java/miui/statusbar/lyric/activity/SettingsActivity.java @@ -49,10 +49,37 @@ public class SettingsActivity extends PreferenceActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.root_preferences); - ActivityUtils.checkPermissions(activity); - config = new Config(); + try { + config = new Config(ActivityUtils.getSP(activity)); + setTitle(String.format("%s (%s)", getString(R.string.AppName), getString(R.string.SPConfigMode))); + } catch (SecurityException ignored) { + if (!activity.getSharedPreferences("isFile", 0).getBoolean("file", false)) { + new AlertDialog.Builder(activity) + .setTitle(getString(R.string.Tips)) + .setIcon(R.mipmap.ic_launcher) + .setMessage(getString(R.string.AppTips)) + .setNegativeButton(getString(R.string.UseFileConfig), (dialog, which) -> { + activity.getSharedPreferences("isFile", 0).edit().putBoolean("file", true).apply(); + config = new Config(); + setTitle(String.format("%s (%s)", getString(R.string.AppName), getString(R.string.FileConfigMode))); + init(); + }) + .setPositiveButton(getString(R.string.Quit), (dialog, which) -> activity.finish()) + .setCancelable(false) + .create() + .show(); + } else { + config = new Config(); + setTitle(String.format("%s (%s)", getString(R.string.AppName), getString(R.string.FileConfigMode))); + init(); + } + } // Utils.log("Debug On"); + } + public void init() { + ActivityUtils.checkPermissions(activity, config); + String tips = "Tips1"; SharedPreferences preferences = activity.getSharedPreferences(tips, 0); if (!preferences.getBoolean(tips, false)) { @@ -61,9 +88,7 @@ protected void onCreate(Bundle savedInstanceState) { .setIcon(R.mipmap.ic_launcher) .setMessage(getString(R.string.AppTips)) .setNegativeButton(getString(R.string.TipsIDone), (dialog, which) -> { - SharedPreferences.Editor a = preferences.edit(); - a.putBoolean(tips, true); - a.apply(); + preferences.edit().putBoolean(tips, true).apply(); }) .setPositiveButton(getString(R.string.Quit), (dialog, which) -> activity.finish()) .setCancelable(false) @@ -257,9 +282,9 @@ protected void onCreate(Bundle savedInstanceState) { EditTextPreference lyricSpeed = (EditTextPreference) findPreference("lyricSpeed"); assert lyricSpeed != null; lyricSpeed.setEnabled(config.getLyricStyle()); - lyricSpeed.setSummary(config.getLyricSpeed()); + lyricSpeed.setSummary(config.getLyricSpeed().toString()); lyricSpeed.setOnPreferenceChangeListener((preference, newValue) -> { - config.setLyricSpeed(newValue.toString()); + config.setLyricSpeed(Float.parseFloat(newValue.toString())); lyricSpeed.setSummary(newValue.toString()); return true; }); @@ -297,7 +322,7 @@ protected void onCreate(Bundle savedInstanceState) { .setNegativeButton(getString(R.string.RestoreDefaultPath), (dialog, which) -> { iconPath.setSummary(getString(R.string.DefaultPath)); config.setIconPath(Utils.PATH); - ActivityUtils.initIcon(activity); + ActivityUtils.initIcon(activity, config); }) .setPositiveButton(getString(R.string.NewPath), (dialog, which) -> { ChooseFileUtils chooseFileUtils = new ChooseFileUtils(activity); @@ -310,7 +335,7 @@ public void onSuccess(String filePath, Uri uri, Intent intent) { if (config.getIconPath().equals(Utils.PATH)) { iconPath.setSummary(getString(R.string.DefaultPath)); } - ActivityUtils.initIcon(activity); + ActivityUtils.initIcon(activity, config); } }); }) @@ -545,7 +570,7 @@ public void onSuccess(String filePath, Uri uri, Intent intent) { } Handler titleUpdate = new Handler(Looper.getMainLooper(), message -> { Log.i("asaaaaa", config.getisUsedCount() + ""); - setTitle(String.format("%s%s", getString(R.string.GetLyricNum), new Config().getUsedCount())); + setTitle(String.format("%s%s", getString(R.string.GetLyricNum), config.getUsedCount())); return false; }); new Thread(() -> new Timer().schedule( @@ -565,14 +590,17 @@ public void run() { @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); + if (config == null) { + config = ActivityUtils.getConfig(getApplicationContext()); + } if (grantResults[0] == 0) { - ActivityUtils.init(activity); - ActivityUtils.initIcon(activity); + ActivityUtils.init(activity, config); + ActivityUtils.initIcon(activity, config); } else { new AlertDialog.Builder(activity) .setTitle(getString(R.string.GetStorageFailed)) .setMessage(getString(R.string.GetStorageFaildTips)) - .setNegativeButton(getString(R.string.ReAppy), (dialog, which) -> ActivityUtils.checkPermissions(activity)) + .setNegativeButton(getString(R.string.ReAppy), (dialog, which) -> ActivityUtils.checkPermissions(activity, config)) .setPositiveButton(getString(R.string.Quit), (dialog, which) -> finish()) .setNeutralButton(getString(R.string.GetPermission), (dialog, which) -> { Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) @@ -591,7 +619,10 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 13131) { - ActivityUtils.checkPermissions(activity); + if (config == null) { + config = ActivityUtils.getConfig(getApplicationContext()); + } + ActivityUtils.checkPermissions(activity, config); } } diff --git a/app/src/main/java/miui/statusbar/lyric/hook/MainHook.java b/app/src/main/java/miui/statusbar/lyric/hook/MainHook.java index dae65c28..386a7b62 100644 --- a/app/src/main/java/miui/statusbar/lyric/hook/MainHook.java +++ b/app/src/main/java/miui/statusbar/lyric/hook/MainHook.java @@ -90,7 +90,7 @@ protected void afterHookedMethod(MethodHookParam param) { case "com.android.systemui": Utils.log("正在hook系统界面"); MeiZuStatusBarLyricService.FlymeNotificationService(lpparam); - config = new Config(); + config = Utils.getConfig(); // 状态栏歌词 XposedHelpers.findAndHookMethod("com.android.systemui.statusbar.phone.CollapsedStatusBarFragment", lpparam.classLoader, "onViewCreated", View.class, Bundle.class, new XC_MethodHook() { @Override @@ -285,7 +285,7 @@ protected void afterHookedMethod(MethodHookParam param) throws Throwable { public void run() { try { if (count == 50) { - config = new Config(); + config.update(); } if (config.getLyricService()) { if (count == 25) { @@ -301,7 +301,7 @@ public void run() { } else if (count == 50) { // 滚动速度 if (config.getLyricStyle()) { - lyricTextView.setSpeed(Float.parseFloat(config.getLyricSpeed())); + lyricTextView.setSpeed(config.getLyricSpeed()); } // 设置动画 diff --git a/app/src/main/java/miui/statusbar/lyric/hook/migu.java b/app/src/main/java/miui/statusbar/lyric/hook/migu.java index 7ad0ffff..50e06b4b 100644 --- a/app/src/main/java/miui/statusbar/lyric/hook/migu.java +++ b/app/src/main/java/miui/statusbar/lyric/hook/migu.java @@ -15,7 +15,7 @@ public class migu { public static class Hook { public Hook(XC_LoadPackage.LoadPackageParam lpparam) { - Config config = new Config(); + Config config = Utils.getConfig(); if (config.getMeizuLyric()) { MeiZuStatusBarLyric.guiseFlyme(lpparam); } else { diff --git a/app/src/main/java/miui/statusbar/lyric/hook/netease.java b/app/src/main/java/miui/statusbar/lyric/hook/netease.java index 1033794c..200664df 100644 --- a/app/src/main/java/miui/statusbar/lyric/hook/netease.java +++ b/app/src/main/java/miui/statusbar/lyric/hook/netease.java @@ -16,7 +16,7 @@ public class netease { public static class Hook { public Hook(XC_LoadPackage.LoadPackageParam lpparam) { - Config config = new Config(); + Config config = Utils.getConfig(); if (config.getMeizuLyric()) { MeiZuStatusBarLyric.guiseFlyme(lpparam); } else { diff --git a/app/src/main/java/miui/statusbar/lyric/hook/qqmusic.java b/app/src/main/java/miui/statusbar/lyric/hook/qqmusic.java index ed6fcb86..b2a9f650 100644 --- a/app/src/main/java/miui/statusbar/lyric/hook/qqmusic.java +++ b/app/src/main/java/miui/statusbar/lyric/hook/qqmusic.java @@ -17,7 +17,7 @@ public class qqmusic { public static class Hook { public Hook(XC_LoadPackage.LoadPackageParam lpparam) { - Config config = new Config(); + Config config = Utils.getConfig(); if (config.getMeizuLyric()) { MeiZuStatusBarLyric.guiseFlyme(lpparam); } else { diff --git a/app/src/main/java/miui/statusbar/lyric/utils/ActivityUtils.java b/app/src/main/java/miui/statusbar/lyric/utils/ActivityUtils.java index ebc3408f..e0278b7a 100644 --- a/app/src/main/java/miui/statusbar/lyric/utils/ActivityUtils.java +++ b/app/src/main/java/miui/statusbar/lyric/utils/ActivityUtils.java @@ -41,15 +41,15 @@ public static String getLocalVersion(Context context) { return localVersion; } - public static void checkPermissions(Activity activity) { + public static void checkPermissions(Activity activity, Config config) { if (checkSelfPermission(activity) == -1) { activity.requestPermissions(new String[]{ "android.permission.WRITE_EXTERNAL_STORAGE" }, 1); } else { - init(activity); - initIcon(activity); - ActivityUtils.checkConfig(activity, new Config().getId()); + init(activity, config); + initIcon(activity, config); + ActivityUtils.checkConfig(activity, config); } } @@ -58,7 +58,7 @@ private static int checkSelfPermission(Context context) { } @SuppressWarnings("ResultOfMethodCallIgnored") - public static void init(Activity activity) { + public static void init(Activity activity, Config config) { File file = new File(Utils.PATH); if (!file.exists()) { file.mkdirs(); @@ -66,7 +66,6 @@ public static void init(Activity activity) { file = new File(Utils.PATH + "Config.json"); if (!file.exists()) { try { - Config config = new Config(); file.createNewFile(); config.setId(configId); config.setUsedCount(0); @@ -78,7 +77,7 @@ public static void init(Activity activity) { config.setAnim("off"); config.setLyricColor("off"); config.setIcon(true); - config.setLyricSpeed("1.0"); + config.setLyricSpeed(1f); config.setLyricPosition(2); config.setIconPath(Utils.PATH); config.setIconAutoColor(true); @@ -98,9 +97,8 @@ public static void init(Activity activity) { } @SuppressWarnings("ResultOfMethodCallIgnored") - public static void initIcon(Activity activity) { + public static void initIcon(Activity activity, Config config) { String[] IconList = {"kugou.webp", "netease.webp", "qqmusic.webp", "myplayer.webp", "migu.webp"}; - Config config = new Config(); for (String s : IconList) { if (!new File(config.getIconPath(), s).exists()) { copyAssets(activity, "icon/" + s, config.getIconPath() + s); @@ -190,15 +188,15 @@ public static void checkUpdate(Activity activity) { }).start(); } - public static void checkConfig(Activity activity, int id) { - if (id != configId) { + public static void checkConfig(Activity activity, Config config) { + if (config.getId() != configId) { try { new AlertDialog.Builder(activity) .setTitle(activity.getString(R.string.Warn)) .setMessage(activity.getString(R.string.ConfigError)) .setNegativeButton(activity.getString(R.string.ResetNow), (dialog, which) -> cleanConfig(activity)) .setPositiveButton(activity.getString(R.string.NoReset), null) - .setNeutralButton(activity.getString(R.string.TryFix), (dialog, which) -> fixConfig(activity)) + .setNeutralButton(activity.getString(R.string.TryFix), (dialog, which) -> fixConfig(activity, config)) .setCancelable(false) .create() .show(); @@ -222,8 +220,7 @@ public static void cleanConfig(Activity activity) { activity.finishAffinity(); } - public static void fixConfig(Activity activity) { - Config config = new Config(); + public static void fixConfig(Activity activity, Config config) { config.setId(configId); config.setUsedCount(config.getUsedCount()); config.setLyricService(config.getLyricService()); @@ -250,5 +247,17 @@ public static void fixConfig(Activity activity) { activity.finishAffinity(); } + public static Config getConfig(Context context) { + try { + return new Config(getSP(context)); + } catch (SecurityException ignored) { + return new Config(); + } + } + + public static SharedPreferences getSP(Context context) { + return context.createDeviceProtectedStorageContext().getSharedPreferences("Lyric_Config", Context.MODE_WORLD_READABLE); + } + } \ No newline at end of file diff --git a/app/src/main/java/miui/statusbar/lyric/utils/ConfigUtils.java b/app/src/main/java/miui/statusbar/lyric/utils/ConfigUtils.java new file mode 100644 index 00000000..c468fbd2 --- /dev/null +++ b/app/src/main/java/miui/statusbar/lyric/utils/ConfigUtils.java @@ -0,0 +1,151 @@ +package miui.statusbar.lyric.utils; + +import android.content.SharedPreferences; +import android.text.TextUtils; + +import org.json.JSONException; +import org.json.JSONObject; + +import java.io.FileOutputStream; +import java.io.IOException; + +import de.robv.android.xposed.XSharedPreferences; + +public class ConfigUtils { + + XSharedPreferences xSP; + SharedPreferences SP; + SharedPreferences.Editor SPEditor; + JSONObject jsonObject; + boolean hasJson = false; + + public ConfigUtils(XSharedPreferences xSharedPreferences) { + xSP = xSharedPreferences; + SP = xSharedPreferences; + } + + public ConfigUtils(String str) { + hasJson = true; + try { + jsonObject = new JSONObject(str); + } catch (JSONException e) { + e.printStackTrace(); + jsonObject = new JSONObject(); + } + } + + public ConfigUtils(SharedPreferences SharedPreferences) { + SP = SharedPreferences; + SPEditor = SharedPreferences.edit(); + } + + public void update() { + xSP.reload(); + } + + public void update(String str) { + if (TextUtils.isEmpty(str)) { + return; + } + try { + jsonObject = new JSONObject(str); + } catch (JSONException e) { + e.printStackTrace(); + } + } + + void setConfig(String str) { + try { + FileOutputStream fileOutputStream = new FileOutputStream(Utils.PATH + "Config.json"); + fileOutputStream.write(str.getBytes()); + fileOutputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public boolean hasJson() { + return hasJson; + } + + public void put(String key, Object value) { + if (value instanceof Integer) { + if (hasJson) { + try { + jsonObject.put(key, Integer.parseInt(value.toString())); + } catch (JSONException ignored) { + } + } else { + SPEditor.putInt(key, Integer.parseInt(value.toString())).apply(); + } + } else if (value instanceof String) { + if (hasJson) { + try { + jsonObject.put(key, value.toString()); + } catch (JSONException ignored) { + } + } else { + SPEditor.putString(key, value.toString()).apply(); + } + } else if (value instanceof Boolean) { + if (hasJson) { + try { + jsonObject.put(key, (boolean) value); + } catch (JSONException ignored) { + } + } else { + SPEditor.putBoolean(key, (boolean) value).apply(); + } + } else if (value instanceof Float) { + if (hasJson) { + try { + jsonObject.put(key, value.toString()); + } catch (JSONException ignored) { + } + } else { + SPEditor.putFloat(key, Float.parseFloat(value.toString())).apply(); + } + } + if (hasJson) { + setConfig(jsonObject.toString()); + } + } + + public int optInt(String key, int i) { + if (hasJson) { + return jsonObject.optInt(key, i); + } else { + return SP.getInt(key, i); + } + } + + public Boolean optBoolean(String key, boolean bool) { + if (hasJson) { + return jsonObject.optBoolean(key, bool); + } else { + return SP.getBoolean(key, bool); + } + } + + public String optString(String key, String str) { + if (hasJson) { + return jsonObject.optString(key, str); + } else { + return SP.getString(key, str); + } + } + + public Float optFloat(String key, float f) { + if (hasJson) { + String f_Str = jsonObject.optString(key, ""); + if (TextUtils.isEmpty(f_Str)) { + return f; + } else { + return Float.parseFloat(f_Str); + } + } else { + return SP.getFloat(key, f); + } + } +} + diff --git a/app/src/main/java/miui/statusbar/lyric/utils/Utils.java b/app/src/main/java/miui/statusbar/lyric/utils/Utils.java index b2f3c35b..adce3a00 100644 --- a/app/src/main/java/miui/statusbar/lyric/utils/Utils.java +++ b/app/src/main/java/miui/statusbar/lyric/utils/Utils.java @@ -23,7 +23,10 @@ import android.view.animation.AnimationSet; import android.view.animation.TranslateAnimation; import android.widget.Toast; + +import de.robv.android.xposed.XSharedPreferences; import de.robv.android.xposed.XposedBridge; +import miui.statusbar.lyric.BuildConfig; import miui.statusbar.lyric.Config; import miui.statusbar.lyric.hook.MainHook; import org.json.JSONArray; @@ -168,7 +171,7 @@ public static void addLyricCount(Config config) { } public static void sendLyric(Context context, String lyric, String icon) { - if (new Config().getFileLyric()) { + if (Utils.getConfig().getFileLyric()) { setLyricFile(icon, lyric); } else { context.sendBroadcast(new Intent().setAction("Lyric_Server").putExtra("Lyric_Data", lyric).putExtra("Lyric_Icon", icon).putExtra("Lyric_Type", "hook")); @@ -376,11 +379,25 @@ public static boolean isPresent(String name) { // log public static void log(String text) { - if (new Config().getDebug()) { + if (Utils.getConfig().getDebug()) { XposedBridge.log("MIUI状态栏歌词: " + text); Log.d("MIUI状态栏歌词", text); } } + public static XSharedPreferences getPref() { + XSharedPreferences pref = new XSharedPreferences(BuildConfig.APPLICATION_ID, "Lyric_Config"); + return pref.getFile().canRead() ? pref : null; + } + + public static Config getConfig() { + XSharedPreferences xSharedPreferences = getPref(); + if (xSharedPreferences == null) { + return new Config(); + } else { + return new Config(xSharedPreferences); + } + } + } diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index 5d18f0a1..9ed9c70a 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -192,4 +192,7 @@ 使用魅族方式获取歌词 使用魅族方法无法获取歌词请关闭此开关 *歌词统计 + 使用文件保存配置 + SP 配置文件 + 文件配置文件 \ No newline at end of file diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index a6041eed..c93771c9 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -178,4 +178,7 @@ 使用魅族方式獲取歌詞 使用魅族方法無法獲取歌詞請關閉此開關 *歌词统计 + 使用文件保存配置 + 文件配置文件 + SP 配置文件 \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9b4e780a..c9859e21 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -189,4 +189,7 @@ Use Meizu Way to Get Lyrics Cannot get lyrics using Meizu method, please turn off this switch *Lyric statistics + Use file to save configuration + SP configuration file + File configuration file \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index 23358f76..ff40c336 100644 --- a/settings.gradle +++ b/settings.gradle @@ -6,5 +6,5 @@ dependencyResolutionManagement { jcenter() // Warning: this repository is going to shut down soon } } -rootProject.name = "状态栏歌词" +rootProject.name = "MIUI状态栏歌词" include ':app' From 91053ecdfbbf39248f329a78d3b80c889f8c3423 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=B9=E5=9D=97=E5=90=9B?= Date: Tue, 30 Nov 2021 01:14:07 +0800 Subject: [PATCH 02/12] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E7=82=B9=E5=87=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复无法点击 --- .../java/miui/statusbar/lyric/Config.java | 4 ++ .../lyric/activity/SettingsActivity.java | 51 +++++++++++++------ .../miui/statusbar/lyric/utils/Utils.java | 6 ++- app/src/main/res/values-zh-rCN/strings.xml | 2 +- app/src/main/res/values-zh-rTW/strings.xml | 2 +- app/src/main/res/values/strings.xml | 2 +- app/src/main/res/xml/root_preferences.xml | 2 +- 7 files changed, 47 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/miui/statusbar/lyric/Config.java b/app/src/main/java/miui/statusbar/lyric/Config.java index bc395fc5..9e753678 100644 --- a/app/src/main/java/miui/statusbar/lyric/Config.java +++ b/app/src/main/java/miui/statusbar/lyric/Config.java @@ -61,6 +61,10 @@ public void update() { } } + public boolean hasJson() { + return config.hasJson(); + } + public int getId() { return config.optInt("id", 0); } diff --git a/app/src/main/java/miui/statusbar/lyric/activity/SettingsActivity.java b/app/src/main/java/miui/statusbar/lyric/activity/SettingsActivity.java index 43841f3c..368b6e52 100644 --- a/app/src/main/java/miui/statusbar/lyric/activity/SettingsActivity.java +++ b/app/src/main/java/miui/statusbar/lyric/activity/SettingsActivity.java @@ -52,19 +52,23 @@ protected void onCreate(Bundle savedInstanceState) { try { config = new Config(ActivityUtils.getSP(activity)); setTitle(String.format("%s (%s)", getString(R.string.AppName), getString(R.string.SPConfigMode))); + init(); } catch (SecurityException ignored) { if (!activity.getSharedPreferences("isFile", 0).getBoolean("file", false)) { new AlertDialog.Builder(activity) .setTitle(getString(R.string.Tips)) .setIcon(R.mipmap.ic_launcher) - .setMessage(getString(R.string.AppTips)) + .setMessage(getString(R.string.NotSupport)) .setNegativeButton(getString(R.string.UseFileConfig), (dialog, which) -> { activity.getSharedPreferences("isFile", 0).edit().putBoolean("file", true).apply(); config = new Config(); setTitle(String.format("%s (%s)", getString(R.string.AppName), getString(R.string.FileConfigMode))); init(); }) - .setPositiveButton(getString(R.string.Quit), (dialog, which) -> activity.finish()) + .setPositiveButton(getString(R.string.Quit), (dialog, which) -> { + activity.finish(); + System.exit(0); + }) .setCancelable(false) .create() .show(); @@ -505,6 +509,16 @@ public void onSuccess(String filePath, Uri uri, Intent intent) { return true; }); + // 统计次数 + SwitchPreference usedCount = (SwitchPreference) findPreference("usedCount"); + assert usedCount != null; + usedCount.setChecked(config.getisUsedCount()); + usedCount.setOnPreferenceChangeListener((preference, newValue) -> { + config.setisUsedCount((Boolean) newValue); + usedCount.setEnabled(!(Boolean) newValue); + return true; + }); + // 重启SystemUI Preference reSystemUI = findPreference("restartUI"); assert reSystemUI != null; @@ -568,22 +582,27 @@ public void onSuccess(String filePath, Uri uri, Intent intent) { hCUK.setSummary(String.format("%s%s", hCUK.getSummary(), getString(R.string.YouNotMIUI))); config.sethNoticeIcon(false); } - Handler titleUpdate = new Handler(Looper.getMainLooper(), message -> { - Log.i("asaaaaa", config.getisUsedCount() + ""); - setTitle(String.format("%s%s", getString(R.string.GetLyricNum), config.getUsedCount())); - return false; - }); - new Thread(() -> new Timer().schedule( - new TimerTask() { - @Override - public void run() { - if (config.getisUsedCount()) { - titleUpdate.sendEmptyMessage(0); - } - } - }, 0, 1000)).start(); + if (config.hasJson()) { + Handler titleUpdate = new Handler(Looper.getMainLooper(), message -> { + Log.i("asaaaaa", config.getisUsedCount() + ""); + setTitle(String.format("%s%s", getString(R.string.GetLyricNum), config.getUsedCount())); + return false; + }); + new Thread(() -> new Timer().schedule( + new TimerTask() { + @Override + public void run() { + if (config.getisUsedCount()) { + titleUpdate.sendEmptyMessage(0); + } + } + }, 0, 1000)).start(); + } else { + usedCount.setChecked(false); + usedCount.setEnabled(false); + } //ActivityUtils.setData(activity); } diff --git a/app/src/main/java/miui/statusbar/lyric/utils/Utils.java b/app/src/main/java/miui/statusbar/lyric/utils/Utils.java index adce3a00..0711d306 100644 --- a/app/src/main/java/miui/statusbar/lyric/utils/Utils.java +++ b/app/src/main/java/miui/statusbar/lyric/utils/Utils.java @@ -165,8 +165,10 @@ public static boolean isDark(int color) { //歌词磁获取统计 public static void addLyricCount(Config config) { - if (config.getisUsedCount()) { - config.setUsedCount(config.getUsedCount() + 1); + if (config.hasJson()) { + if (config.getisUsedCount()) { + config.setUsedCount(config.getUsedCount() + 1); + } } } diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index 9ed9c70a..e23261d8 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -191,7 +191,7 @@ 使用魅族方式获取歌词 使用魅族方法无法获取歌词请关闭此开关 - *歌词统计 + * 歌词统计 使用文件保存配置 SP 配置文件 文件配置文件 diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index c93771c9..450ff3fd 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -177,7 +177,7 @@ 不支持的Xposed版本\n或未激活本模塊\n激活後強制停止本軟件 使用魅族方式獲取歌詞 使用魅族方法無法獲取歌詞請關閉此開關 - *歌词统计 + * 歌词统计 使用文件保存配置 文件配置文件 SP 配置文件 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c9859e21..1edcb543 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -188,7 +188,7 @@ Xposed not supported\n or the module is not activated\nforce stop the software after activation Use Meizu Way to Get Lyrics Cannot get lyrics using Meizu method, please turn off this switch - *Lyric statistics + * Lyric statistics Use file to save configuration SP configuration file File configuration file diff --git a/app/src/main/res/xml/root_preferences.xml b/app/src/main/res/xml/root_preferences.xml index 69087d87..7fb5f5d2 100644 --- a/app/src/main/res/xml/root_preferences.xml +++ b/app/src/main/res/xml/root_preferences.xml @@ -170,7 +170,7 @@ android:title="@string/DebugMode"/> Date: Tue, 30 Nov 2021 21:20:38 +0800 Subject: [PATCH 03/12] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=B7=B2=E7=9F=A5?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 4 +- .../main/java/miui/statusbar/lyric/Api.java | 5 +- .../java/miui/statusbar/lyric/Config.java | 7 - .../statusbar/lyric/QuickTitleService.java | 1 - .../lyric/activity/SettingsActivity.java | 17 +- .../miui/statusbar/lyric/hook/MainHook.java | 1 - .../lyric/hook/MeiZuStatusBarLyric.java | 30 +++ .../hook/MeiZuStatusBarLyricService.java | 79 ------ .../java/miui/statusbar/lyric/hook/migu.java | 43 +--- .../miui/statusbar/lyric/hook/netease.java | 243 +++++++++--------- .../statusbar/lyric/hook/neteaseLite.java | 1 + .../miui/statusbar/lyric/hook/qqmusic.java | 37 +-- .../statusbar/lyric/utils/ActivityUtils.java | 9 +- .../miui/statusbar/lyric/utils/Utils.java | 21 +- .../lyric/view/LyricTextSwitchView.java | 4 +- .../statusbar/lyric/view/LyricTextView.java | 14 + app/src/main/res/xml/root_preferences.xml | 6 - 17 files changed, 187 insertions(+), 335 deletions(-) delete mode 100644 app/src/main/java/miui/statusbar/lyric/hook/MeiZuStatusBarLyricService.java diff --git a/app/build.gradle b/app/build.gradle index c4f00c8b..3b10b959 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -12,8 +12,8 @@ android { minSdkVersion 27 //noinspection OldTargetApi targetSdkVersion 29 - versionCode 55 - versionName "3.32.3" + versionCode 56 + versionName "3.33.3-dev" aaptOptions.cruncherEnabled = false aaptOptions.useNewCruncher = false } diff --git a/app/src/main/java/miui/statusbar/lyric/Api.java b/app/src/main/java/miui/statusbar/lyric/Api.java index 4b21ee01..0d29506f 100644 --- a/app/src/main/java/miui/statusbar/lyric/Api.java +++ b/app/src/main/java/miui/statusbar/lyric/Api.java @@ -3,13 +3,10 @@ import android.content.Context; import android.content.Intent; import android.os.Environment; + import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; -import java.io.FileInputStream; import java.io.FileOutputStream; -import java.io.IOException; import miui.statusbar.lyric.utils.ActivityUtils; diff --git a/app/src/main/java/miui/statusbar/lyric/Config.java b/app/src/main/java/miui/statusbar/lyric/Config.java index 9e753678..3923f9af 100644 --- a/app/src/main/java/miui/statusbar/lyric/Config.java +++ b/app/src/main/java/miui/statusbar/lyric/Config.java @@ -274,11 +274,4 @@ public void setLShowOnce(boolean bool) { config.put("LShowOnce", bool); } - public boolean getMeizuLyric() { - return config.optBoolean("tMeizuLyric", true); - } - - public void setMeizuLyric(boolean bool) { - config.put("tMeizuLyric", bool); - } } \ No newline at end of file diff --git a/app/src/main/java/miui/statusbar/lyric/QuickTitleService.java b/app/src/main/java/miui/statusbar/lyric/QuickTitleService.java index 5881176a..27ae46d6 100644 --- a/app/src/main/java/miui/statusbar/lyric/QuickTitleService.java +++ b/app/src/main/java/miui/statusbar/lyric/QuickTitleService.java @@ -7,7 +7,6 @@ import android.service.quicksettings.TileService; import miui.statusbar.lyric.utils.ActivityUtils; -import miui.statusbar.lyric.utils.Utils; @TargetApi(Build.VERSION_CODES.N) public class QuickTitleService extends TileService { diff --git a/app/src/main/java/miui/statusbar/lyric/activity/SettingsActivity.java b/app/src/main/java/miui/statusbar/lyric/activity/SettingsActivity.java index 368b6e52..93f0d051 100644 --- a/app/src/main/java/miui/statusbar/lyric/activity/SettingsActivity.java +++ b/app/src/main/java/miui/statusbar/lyric/activity/SettingsActivity.java @@ -91,9 +91,7 @@ public void init() { .setTitle(getString(R.string.Tips)) .setIcon(R.mipmap.ic_launcher) .setMessage(getString(R.string.AppTips)) - .setNegativeButton(getString(R.string.TipsIDone), (dialog, which) -> { - preferences.edit().putBoolean(tips, true).apply(); - }) + .setNegativeButton(getString(R.string.TipsIDone), (dialog, which) -> preferences.edit().putBoolean(tips, true).apply()) .setPositiveButton(getString(R.string.Quit), (dialog, which) -> activity.finish()) .setCancelable(false) .create() @@ -147,6 +145,7 @@ public void init() { // 歌词总开关 SwitchPreference lyricService = (SwitchPreference) findPreference("lyricService"); assert lyricService != null; + Log.d("LSPosed", config.getLyricService().toString()); lyricService.setChecked(config.getLyricService()); lyricService.setOnPreferenceChangeListener((preference, newValue) -> { config.setLyricService((Boolean) newValue); @@ -498,17 +497,6 @@ public void onSuccess(String filePath, Uri uri, Intent intent) { return true; }); - // 魅族方式 - SwitchPreference meizuLyric = (SwitchPreference) findPreference("meizuLyric"); - assert meizuLyric != null; - fileLyric.setEnabled(!config.getMeizuLyric()); - meizuLyric.setChecked(config.getMeizuLyric()); - meizuLyric.setOnPreferenceChangeListener((preference, newValue) -> { - config.setMeizuLyric((Boolean) newValue); - fileLyric.setEnabled(!(Boolean) newValue); - return true; - }); - // 统计次数 SwitchPreference usedCount = (SwitchPreference) findPreference("usedCount"); assert usedCount != null; @@ -633,7 +621,6 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis } - @SuppressWarnings("deprecation") @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); diff --git a/app/src/main/java/miui/statusbar/lyric/hook/MainHook.java b/app/src/main/java/miui/statusbar/lyric/hook/MainHook.java index 386a7b62..59f40f34 100644 --- a/app/src/main/java/miui/statusbar/lyric/hook/MainHook.java +++ b/app/src/main/java/miui/statusbar/lyric/hook/MainHook.java @@ -89,7 +89,6 @@ protected void afterHookedMethod(MethodHookParam param) { switch (lpparam.packageName) { case "com.android.systemui": Utils.log("正在hook系统界面"); - MeiZuStatusBarLyricService.FlymeNotificationService(lpparam); config = Utils.getConfig(); // 状态栏歌词 XposedHelpers.findAndHookMethod("com.android.systemui.statusbar.phone.CollapsedStatusBarFragment", lpparam.classLoader, "onViewCreated", View.class, Bundle.class, new XC_MethodHook() { diff --git a/app/src/main/java/miui/statusbar/lyric/hook/MeiZuStatusBarLyric.java b/app/src/main/java/miui/statusbar/lyric/hook/MeiZuStatusBarLyric.java index e0900b16..d7861b6c 100644 --- a/app/src/main/java/miui/statusbar/lyric/hook/MeiZuStatusBarLyric.java +++ b/app/src/main/java/miui/statusbar/lyric/hook/MeiZuStatusBarLyric.java @@ -4,9 +4,12 @@ import android.app.Application; import android.content.Context; import android.os.Build; +import android.app.Notification; import de.robv.android.xposed.XC_MethodHook; +import de.robv.android.xposed.XposedBridge; import de.robv.android.xposed.XposedHelpers; import de.robv.android.xposed.callbacks.XC_LoadPackage; +import miui.statusbar.lyric.utils.Utils; public class MeiZuStatusBarLyric { @@ -68,6 +71,33 @@ protected void beforeHookedMethod(MethodHookParam param) throws Throwable { } } + @Override + protected void afterHookedMethod(MethodHookParam param) throws Throwable { + super.afterHookedMethod(param); + } + }); + XposedHelpers.findAndHookMethod("android.app.NotificationManager", lpparam.classLoader, "notify", int.class, Notification.class, new XC_MethodHook() { + @Override + protected void beforeHookedMethod(MethodHookParam param) throws Throwable { + super.beforeHookedMethod(param); + Notification notification = ((Notification) param.args[1]); + CharSequence charSequence = notification.tickerText; + XposedBridge.log(notification.toString()); + XposedBridge.log("Flags: " + notification.flags); + boolean isLyric = false; + if ((notification.flags & MeiZuNotification.FLAG_ALWAYS_SHOW_TICKER) != 0) { + isLyric = true; + } + if ((notification.flags & MeiZuNotification.FLAG_ONLY_UPDATE_TICKER) != 0) { + isLyric = true; + } + if (charSequence == null || !isLyric) { + return; + } +// XposedBridge.log("1: " + charSequence); + Utils.sendLyric(context, charSequence.toString(), Utils.packName_GetIconName(lpparam.packageName)); + } + @Override protected void afterHookedMethod(MethodHookParam param) throws Throwable { super.afterHookedMethod(param); diff --git a/app/src/main/java/miui/statusbar/lyric/hook/MeiZuStatusBarLyricService.java b/app/src/main/java/miui/statusbar/lyric/hook/MeiZuStatusBarLyricService.java deleted file mode 100644 index 92e344b6..00000000 --- a/app/src/main/java/miui/statusbar/lyric/hook/MeiZuStatusBarLyricService.java +++ /dev/null @@ -1,79 +0,0 @@ -package miui.statusbar.lyric.hook; - -import android.annotation.SuppressLint; -import android.app.Application; -import android.app.Notification; -import android.content.Context; -import android.service.notification.NotificationListenerService; -import android.service.notification.StatusBarNotification; -import de.robv.android.xposed.XC_MethodHook; -import de.robv.android.xposed.XposedHelpers; -import de.robv.android.xposed.callbacks.XC_LoadPackage; -import miui.statusbar.lyric.utils.Utils; - -public class MeiZuStatusBarLyricService { - @SuppressLint("StaticFieldLeak") - static Context context; - - public static void FlymeNotificationService(XC_LoadPackage.LoadPackageParam lpparam) { - // 获取Context - XposedHelpers.findAndHookMethod(Application.class, "attach", Context.class, new XC_MethodHook() { - @Override - protected void afterHookedMethod(MethodHookParam param) { - context = (Context) param.args[0]; - } - }); - try { - XposedHelpers.findAndHookMethod("com.android.systemui.statusbar.notification.NotificationEntryManager", lpparam.classLoader, "updateNotificationInternal", StatusBarNotification.class, NotificationListenerService.RankingMap.class, new XC_MethodHook() { - @Override - protected void beforeHookedMethod(MethodHookParam param) throws Throwable { - super.beforeHookedMethod(param); - StatusBarNotification statusBarNotification = (StatusBarNotification) param.args[0]; - Notification n = statusBarNotification.getNotification(); - String lyric; - - if (n.tickerText != null) { - lyric = n.tickerText.toString(); - Utils.log(lyric); - Utils.log(statusBarNotification.getPackageName()); - Utils.log(n.flags + " | " + n.when); - Utils.log((n.flags & MeiZuNotification.FLAG_INSISTENT) + ""); - } else { - lyric = ""; - } - - if (n.flags == 0 || n.flags == 8 || n.flags == 16777330) { - Utils.log("n.flags = " + n.flags + " lyric:" + lyric); - Utils.setLocalLyric("", statusBarNotification.getPackageName()); - return; - } - - if ((n.flags & MeiZuNotification.FLAG_ONLY_UPDATE_TICKER) != 0) { - Utils.log("dnotifi"); - param.setResult(null); - } - - boolean isLyric = (n.flags & MeiZuNotification.FLAG_ONLY_UPDATE_TICKER) != 0; - if (n.flags == 16777314) { - isLyric = true; - } - Utils.log("isLyric = " + isLyric); - if (isLyric) { - Utils.log(lyric); - Utils.log(statusBarNotification.getPackageName()); - Utils.setLocalLyric(lyric, statusBarNotification.getPackageName()); - } - } - - @Override - protected void afterHookedMethod(MethodHookParam param) throws Throwable { - super.afterHookedMethod(param); - } - }); - } catch (Exception e) { - Utils.log("无法使用魅族方式获取歌词,请切换传统Hook模式"); - Utils.log(e.toString()); - } - - } -} diff --git a/app/src/main/java/miui/statusbar/lyric/hook/migu.java b/app/src/main/java/miui/statusbar/lyric/hook/migu.java index 50e06b4b..b4836125 100644 --- a/app/src/main/java/miui/statusbar/lyric/hook/migu.java +++ b/app/src/main/java/miui/statusbar/lyric/hook/migu.java @@ -1,53 +1,12 @@ package miui.statusbar.lyric.hook; -import android.annotation.SuppressLint; -import android.app.Application; -import android.content.Context; -import de.robv.android.xposed.XC_MethodHook; -import de.robv.android.xposed.XposedHelpers; import de.robv.android.xposed.callbacks.XC_LoadPackage; -import miui.statusbar.lyric.Config; -import miui.statusbar.lyric.utils.Utils; public class migu { - @SuppressLint("StaticFieldLeak") - static Context context; public static class Hook { public Hook(XC_LoadPackage.LoadPackageParam lpparam) { - Config config = Utils.getConfig(); - if (config.getMeizuLyric()) { - MeiZuStatusBarLyric.guiseFlyme(lpparam); - } else { - // 获取Context - XposedHelpers.findAndHookMethod(Application.class, "attach", Context.class, new XC_MethodHook() { - @Override - protected void afterHookedMethod(MethodHookParam param) { - context = (Context) param.args[0]; - } - }); - XposedHelpers.findAndHookMethod("android.support.v4.media.MediaMetadataCompat$Builder", lpparam.classLoader, "putString", String.class, String.class, new XC_MethodHook() { - @Override - protected void beforeHookedMethod(MethodHookParam param) throws Throwable { - super.beforeHookedMethod(param); - } - - @Override - protected void afterHookedMethod(MethodHookParam param) throws Throwable { - super.afterHookedMethod(param); - if (param.args[0] != null) { - if (param.args[0].toString().equals("android.media.metadata.TITLE")) { - if (param.args[1] != null) { - if (context != null) { - Utils.sendLyric(context, param.args[1].toString(), "migu"); - } - Utils.log("咪咕音乐: " + param.args[1].toString()); - } - } - } - } - }); - } + MeiZuStatusBarLyric.guiseFlyme(lpparam); } } } diff --git a/app/src/main/java/miui/statusbar/lyric/hook/netease.java b/app/src/main/java/miui/statusbar/lyric/hook/netease.java index 200664df..93c9b13e 100644 --- a/app/src/main/java/miui/statusbar/lyric/hook/netease.java +++ b/app/src/main/java/miui/statusbar/lyric/hook/netease.java @@ -4,10 +4,10 @@ import android.content.Context; import android.graphics.Bitmap; import android.text.TextUtils; + import de.robv.android.xposed.XC_MethodHook; import de.robv.android.xposed.XposedHelpers; import de.robv.android.xposed.callbacks.XC_LoadPackage; -import miui.statusbar.lyric.Config; import miui.statusbar.lyric.utils.Utils; public class netease { @@ -16,105 +16,121 @@ public class netease { public static class Hook { public Hook(XC_LoadPackage.LoadPackageParam lpparam) { - Config config = Utils.getConfig(); - if (config.getMeizuLyric()) { - MeiZuStatusBarLyric.guiseFlyme(lpparam); - } else { - XposedHelpers.findAndHookMethod(XposedHelpers.findClass("com.netease.cloudmusic.NeteaseMusicApplication", lpparam.classLoader), "attachBaseContext", Context.class, new XC_MethodHook() { - @Override - protected void afterHookedMethod(MethodHookParam param) { - Context context = (Context) param.thisObject; - String enableBTLyric_Class; - String enableBTLyric_Method; - String getMusicName_Class; - String getMusicName_Method; - Object[] getMusicName_ClsArr; - XC_MethodHook getMusicName_Hook = new XC_MethodHook() { - @Override - protected void beforeHookedMethod(MethodHookParam param) throws Throwable { - super.beforeHookedMethod(param); - } + XposedHelpers.findAndHookMethod(XposedHelpers.findClass("com.netease.cloudmusic.NeteaseMusicApplication", lpparam.classLoader), "attachBaseContext", Context.class, new XC_MethodHook() { + @Override + protected void afterHookedMethod(MethodHookParam param) { + Context context = (Context) param.thisObject; + String enableBTLyric_Class; + String enableBTLyric_Method; + String getMusicName_Class; + String getMusicName_Method; + Object[] getMusicName_ClsArr; + XC_MethodHook getMusicName_Hook = new XC_MethodHook() { + @Override + protected void beforeHookedMethod(MethodHookParam param) throws Throwable { + super.beforeHookedMethod(param); + } - @Override - protected void afterHookedMethod(MethodHookParam param) throws Throwable { - super.afterHookedMethod(param); - if (param.args[0] != null) { - Utils.sendLyric(context, param.args[0].toString(), "netease"); - musicName = param.args[0].toString(); - Utils.log("网易云歌名: " + param.args[0].toString()); - } + @Override + protected void afterHookedMethod(MethodHookParam param) throws Throwable { + super.afterHookedMethod(param); + if (param.args[0] != null) { + Utils.sendLyric(context, param.args[0].toString(), "netease"); + musicName = param.args[0].toString(); + Utils.log("网易云歌名: " + param.args[0].toString()); } - }; - String getMusicLyric_Class; - String getMusicLyric_Method; - Object[] getMusicLyric_ClsArr; - XC_MethodHook getMusicLyric_Hook = new XC_MethodHook() { - @Override - protected void beforeHookedMethod(MethodHookParam param) throws Throwable { - super.beforeHookedMethod(param); - if (param.args[0] != null) { - Utils.sendLyric(context, param.args[0].toString(), "netease"); - Utils.log("网易云: " + param.args[0].toString()); - } - if (!TextUtils.isEmpty(musicName)) { - param.args[0] = musicName; - param.setResult(param.args); - } + } + }; + String getMusicLyric_Class; + String getMusicLyric_Method; + Object[] getMusicLyric_ClsArr; + XC_MethodHook getMusicLyric_Hook = new XC_MethodHook() { + @Override + protected void beforeHookedMethod(MethodHookParam param) throws Throwable { + super.beforeHookedMethod(param); + if (param.args[0] != null) { + Utils.sendLyric(context, param.args[0].toString(), "netease"); + Utils.log("网易云: " + param.args[0].toString()); } - - @Override - protected void afterHookedMethod(MethodHookParam param) throws Throwable { - super.afterHookedMethod(param); + if (!TextUtils.isEmpty(musicName)) { + param.args[0] = musicName; + param.setResult(param.args); } - }; - try { - int cloudmusicVer = context.getPackageManager().getPackageInfo("com.netease.cloudmusic", 0).versionCode; - if (cloudmusicVer >= 8006000) { - enableBTLyric_Class = "com.netease.cloudmusic.module.player.w.h"; - enableBTLyric_Method = "o"; + } - getMusicName_Class = "com.netease.cloudmusic.module.player.w.h"; - getMusicName_Method = "B"; - getMusicName_ClsArr = new Object[]{ - String.class, String.class, String.class, long.class, Boolean.class, getMusicName_Hook - }; + @Override + protected void afterHookedMethod(MethodHookParam param) throws Throwable { + super.afterHookedMethod(param); + } + }; + try { + int cloudmusicVer = context.getPackageManager().getPackageInfo("com.netease.cloudmusic", 0).versionCode; + if (cloudmusicVer >= 8006000) { + enableBTLyric_Class = "com.netease.cloudmusic.module.player.w.h"; + enableBTLyric_Method = "o"; + + getMusicName_Class = "com.netease.cloudmusic.module.player.w.h"; + getMusicName_Method = "B"; + getMusicName_ClsArr = new Object[]{ + String.class, String.class, String.class, long.class, Boolean.class, getMusicName_Hook + }; - getMusicLyric_Class = "com.netease.cloudmusic.module.player.w.h"; - getMusicLyric_Method = "F"; - getMusicLyric_ClsArr = new Object[]{ - java.lang.String.class, java.lang.String.class, getMusicLyric_Hook - }; - } else if (cloudmusicVer > 7002022) { - enableBTLyric_Class = "com.netease.cloudmusic.module.player.t.e"; - enableBTLyric_Method = "o"; + getMusicLyric_Class = "com.netease.cloudmusic.module.player.w.h"; + getMusicLyric_Method = "F"; + getMusicLyric_ClsArr = new Object[]{ + java.lang.String.class, java.lang.String.class, getMusicLyric_Hook + }; + } else if (cloudmusicVer > 7002022) { + enableBTLyric_Class = "com.netease.cloudmusic.module.player.t.e"; + enableBTLyric_Method = "o"; - getMusicName_Class = "com.netease.cloudmusic.module.player.t.e"; - getMusicName_Method = "B"; - getMusicName_ClsArr = new Object[]{ - String.class, String.class, String.class, Long.TYPE, Boolean.class, getMusicName_Hook - }; + getMusicName_Class = "com.netease.cloudmusic.module.player.t.e"; + getMusicName_Method = "B"; + getMusicName_ClsArr = new Object[]{ + String.class, String.class, String.class, Long.TYPE, Boolean.class, getMusicName_Hook + }; - getMusicLyric_Class = "com.netease.cloudmusic.module.player.t.e"; - getMusicLyric_Method = "F"; - getMusicLyric_ClsArr = new Object[]{ - java.lang.String.class, java.lang.String.class, getMusicLyric_Hook - }; - } else { - enableBTLyric_Class = "com.netease.cloudmusic.module.player.f.e"; - enableBTLyric_Method = "b"; + getMusicLyric_Class = "com.netease.cloudmusic.module.player.t.e"; + getMusicLyric_Method = "F"; + getMusicLyric_ClsArr = new Object[]{ + java.lang.String.class, java.lang.String.class, getMusicLyric_Hook + }; + } else { + enableBTLyric_Class = "com.netease.cloudmusic.module.player.f.e"; + enableBTLyric_Method = "b"; - getMusicName_Class = ""; - getMusicName_Method = ""; - getMusicName_ClsArr = new Object[]{}; + getMusicName_Class = ""; + getMusicName_Method = ""; + getMusicName_ClsArr = new Object[]{}; - getMusicLyric_Class = "com.netease.cloudmusic.module.player.f.e"; - getMusicLyric_Method = "a"; - getMusicLyric_ClsArr = new Object[]{ - String.class, String.class, String.class, long.class, Bitmap.class, String.class, getMusicLyric_Hook - }; + getMusicLyric_Class = "com.netease.cloudmusic.module.player.f.e"; + getMusicLyric_Method = "a"; + getMusicLyric_ClsArr = new Object[]{ + String.class, String.class, String.class, long.class, Bitmap.class, String.class, getMusicLyric_Hook + }; + } + try { + XposedHelpers.findAndHookMethod(enableBTLyric_Class, lpparam.classLoader, enableBTLyric_Method, new XC_MethodHook() { + @Override + protected void beforeHookedMethod(MethodHookParam param) throws Throwable { + super.beforeHookedMethod(param); + } + + @Override + protected void afterHookedMethod(MethodHookParam param) throws Throwable { + super.afterHookedMethod(param); + param.setResult(true); + } + }); + if (!getMusicName_Class.equals("")) { + XposedHelpers.findAndHookMethod(getMusicName_Class, lpparam.classLoader, getMusicName_Method, getMusicName_ClsArr); } + XposedHelpers.findAndHookMethod(getMusicLyric_Class, lpparam.classLoader, getMusicLyric_Method, getMusicLyric_ClsArr); + } catch (NoSuchMethodError | IllegalArgumentException e) { + Utils.log("网易云Hook失败: " + e); + Utils.log("正在尝试通用Hook"); try { - XposedHelpers.findAndHookMethod(enableBTLyric_Class, lpparam.classLoader, enableBTLyric_Method, new XC_MethodHook() { + XposedHelpers.findAndHookMethod("android.support.v4.media.MediaMetadataCompat$Builder", lpparam.classLoader, "putString", String.class, String.class, new XC_MethodHook() { @Override protected void beforeHookedMethod(MethodHookParam param) throws Throwable { super.beforeHookedMethod(param); @@ -123,47 +139,26 @@ protected void beforeHookedMethod(MethodHookParam param) throws Throwable { @Override protected void afterHookedMethod(MethodHookParam param) throws Throwable { super.afterHookedMethod(param); - param.setResult(true); - } - }); - if (!getMusicName_Class.equals("")) { - XposedHelpers.findAndHookMethod(getMusicName_Class, lpparam.classLoader, getMusicName_Method, getMusicName_ClsArr); - } - XposedHelpers.findAndHookMethod(getMusicLyric_Class, lpparam.classLoader, getMusicLyric_Method, getMusicLyric_ClsArr); - } catch (NoSuchMethodError e) { - Utils.log("网易云Hook失败: " + e); - Utils.log("正在尝试通用Hook"); - try { - XposedHelpers.findAndHookMethod("android.support.v4.media.MediaMetadataCompat$Builder", lpparam.classLoader, "putString", String.class, String.class, new XC_MethodHook() { - @Override - protected void beforeHookedMethod(MethodHookParam param) throws Throwable { - super.beforeHookedMethod(param); - } - - @Override - protected void afterHookedMethod(MethodHookParam param) throws Throwable { - super.afterHookedMethod(param); - if (param.args[0].toString().equals("android.media.metadata.TITLE")) { - if (param.args[1] != null) { - Utils.sendLyric(context, param.args[1].toString(), "netease"); - Utils.log("网易云通用: " + param.args[1].toString()); - } + if (param.args[0].toString().equals("android.media.metadata.TITLE")) { + if (param.args[1] != null) { + Utils.sendLyric(context, param.args[1].toString(), "netease"); + Utils.log("网易云通用: " + param.args[1].toString()); } } - }); - } catch (NoSuchMethodError mE) { - Utils.log("网易云通用Hook失败: " + mE); - Utils.log("未知版本: " + cloudmusicVer); - Utils.showToastOnLooper(context, "MIUI状态栏歌词 未知版本: " + cloudmusicVer); - } + } + }); + } catch (NoSuchMethodError mE) { + Utils.log("网易云通用Hook失败: " + mE); + Utils.log("未知版本: " + cloudmusicVer); + Utils.showToastOnLooper(context, "MIUI状态栏歌词 未知版本: " + cloudmusicVer); } - } catch (Exception e) { - Utils.log(Utils.dumpException(e)); - Utils.showToastOnLooper(context, "hook网易云失败: " + e); } + } catch (Exception e) { + Utils.log(Utils.dumpException(e)); + Utils.showToastOnLooper(context, "hook网易云失败: " + e); } - }); - } + } + }); } } } diff --git a/app/src/main/java/miui/statusbar/lyric/hook/neteaseLite.java b/app/src/main/java/miui/statusbar/lyric/hook/neteaseLite.java index 907706c2..7ca0f975 100644 --- a/app/src/main/java/miui/statusbar/lyric/hook/neteaseLite.java +++ b/app/src/main/java/miui/statusbar/lyric/hook/neteaseLite.java @@ -16,6 +16,7 @@ public class neteaseLite { public static class Hook { public Hook(XC_LoadPackage.LoadPackageParam lpparam) { + MeiZuStatusBarLyric.guiseFlyme(lpparam); XposedHelpers.findAndHookMethod("com.netease.cloudmusic.utils.u", lpparam.classLoader, "R", new XC_MethodHook() { @Override protected void beforeHookedMethod(MethodHookParam param) throws Throwable { diff --git a/app/src/main/java/miui/statusbar/lyric/hook/qqmusic.java b/app/src/main/java/miui/statusbar/lyric/hook/qqmusic.java index b2a9f650..8a8ee4e2 100644 --- a/app/src/main/java/miui/statusbar/lyric/hook/qqmusic.java +++ b/app/src/main/java/miui/statusbar/lyric/hook/qqmusic.java @@ -12,44 +12,9 @@ import java.lang.reflect.Field; public class qqmusic { - @SuppressLint("StaticFieldLeak") - static Context context; public static class Hook { - public Hook(XC_LoadPackage.LoadPackageParam lpparam) { - Config config = Utils.getConfig(); - if (config.getMeizuLyric()) { - MeiZuStatusBarLyric.guiseFlyme(lpparam); - } else { - // 获取Context - XposedHelpers.findAndHookMethod(Application.class, "attach", Context.class, new XC_MethodHook() { - @Override - protected void afterHookedMethod(MethodHookParam param) { - context = (Context) param.args[0]; - } - }); - XposedHelpers.findAndHookMethod("com.tencent.qqmusicplayerprocess.servicenew.mediasession.d$d", lpparam.classLoader, "run", new XC_MethodHook() { - @Override - protected void beforeHookedMethod(MethodHookParam param) throws Throwable { - super.beforeHookedMethod(param); - } - - @Override - protected void afterHookedMethod(MethodHookParam param) throws Throwable { - super.afterHookedMethod(param); - - Class findClass = XposedHelpers.findClass("com.lyricengine.base.h", lpparam.classLoader); - Field declaredField = findClass.getDeclaredField("a"); - declaredField.setAccessible(true); - - Object obj = XposedHelpers.findField(param.thisObject.getClass(), "b").get(param.thisObject); - String str = (String) declaredField.get(obj); - - Utils.log("qq音乐: " + str); - Utils.sendLyric(context, str, "qqmusic"); - } - }); - } + MeiZuStatusBarLyric.guiseFlyme(lpparam); } } } diff --git a/app/src/main/java/miui/statusbar/lyric/utils/ActivityUtils.java b/app/src/main/java/miui/statusbar/lyric/utils/ActivityUtils.java index e0278b7a..8d3fdfe8 100644 --- a/app/src/main/java/miui/statusbar/lyric/utils/ActivityUtils.java +++ b/app/src/main/java/miui/statusbar/lyric/utils/ActivityUtils.java @@ -49,7 +49,9 @@ public static void checkPermissions(Activity activity, Config config) { } else { init(activity, config); initIcon(activity, config); - ActivityUtils.checkConfig(activity, config); + if (config.hasJson()) { + ActivityUtils.checkConfig(activity, config); + } } } @@ -63,6 +65,9 @@ public static void init(Activity activity, Config config) { if (!file.exists()) { file.mkdirs(); } + if (!config.hasJson()) { + return; + } file = new File(Utils.PATH + "Config.json"); if (!file.exists()) { try { @@ -98,7 +103,7 @@ public static void init(Activity activity, Config config) { @SuppressWarnings("ResultOfMethodCallIgnored") public static void initIcon(Activity activity, Config config) { - String[] IconList = {"kugou.webp", "netease.webp", "qqmusic.webp", "myplayer.webp", "migu.webp"}; + String[] IconList = {"kugou.webp", "netease.webp", "qqmusic.webp", "myplayer.webp", "migu.webp", "kuwo.webp"}; for (String s : IconList) { if (!new File(config.getIconPath(), s).exists()) { copyAssets(activity, "icon/" + s, config.getIconPath() + s); diff --git a/app/src/main/java/miui/statusbar/lyric/utils/Utils.java b/app/src/main/java/miui/statusbar/lyric/utils/Utils.java index 0711d306..9c3cf543 100644 --- a/app/src/main/java/miui/statusbar/lyric/utils/Utils.java +++ b/app/src/main/java/miui/statusbar/lyric/utils/Utils.java @@ -24,11 +24,6 @@ import android.view.animation.TranslateAnimation; import android.widget.Toast; -import de.robv.android.xposed.XSharedPreferences; -import de.robv.android.xposed.XposedBridge; -import miui.statusbar.lyric.BuildConfig; -import miui.statusbar.lyric.Config; -import miui.statusbar.lyric.hook.MainHook; import org.json.JSONArray; import java.io.FileInputStream; @@ -40,6 +35,11 @@ import java.util.List; import java.util.Objects; +import de.robv.android.xposed.XSharedPreferences; +import de.robv.android.xposed.XposedBridge; +import miui.statusbar.lyric.BuildConfig; +import miui.statusbar.lyric.Config; + public class Utils { public static String PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/media/miui.statusbar.lyric/"; @@ -57,15 +57,8 @@ public class Utils { packName_Name.put("cn.kuwo", "kuwo"); } - public static String packName_GetIconPath(String packName) { - return MainHook.config.getIconPath() + Utils.packName_Name.get(packName) + ".webp"; - } - - public static void setLocalLyric(String lyric, String packName) { - MainHook.icon[0] = "hook"; - MainHook.icon[1] = Utils.packName_GetIconPath(packName); - MainHook.lyric = lyric; -// addLyricCount(); + public static String packName_GetIconName(String packName) { + return Utils.packName_Name.get(packName); } public static int getLocalVersionCode(Context context) { diff --git a/app/src/main/java/miui/statusbar/lyric/view/LyricTextSwitchView.java b/app/src/main/java/miui/statusbar/lyric/view/LyricTextSwitchView.java index 946a88e7..04562e9f 100644 --- a/app/src/main/java/miui/statusbar/lyric/view/LyricTextSwitchView.java +++ b/app/src/main/java/miui/statusbar/lyric/view/LyricTextSwitchView.java @@ -50,14 +50,14 @@ public void setText(String str) { // 设置歌词文本 if (switchLyric) { if (hasMeizu) { - lyricTextView.setText(str); + lyricTextView.setTextT(str); } else { autoMarqueeTextView.setText(str); } switchLyric = false; } else { if (hasMeizu) { - lyricTextView2.setText(str); + lyricTextView2.setTextT(str); } else { autoMarqueeTextView2.setText(str); } diff --git a/app/src/main/java/miui/statusbar/lyric/view/LyricTextView.java b/app/src/main/java/miui/statusbar/lyric/view/LyricTextView.java index 1bbdc876..c9d3f217 100644 --- a/app/src/main/java/miui/statusbar/lyric/view/LyricTextView.java +++ b/app/src/main/java/miui/statusbar/lyric/view/LyricTextView.java @@ -29,6 +29,7 @@ public class LyricTextView extends TextView { private float textLength = 0f; private float viewWidth = 0f; private float speed = 4f; + private long time; private float x = 0f; private String text; @@ -87,6 +88,14 @@ public void setTextColor(int color) { @Override protected void onDraw(Canvas canvas) { + if (canvas != null && text != null) { + if ((System.currentTimeMillis() - time) <= 1500) { + float y = getHeight() / 2 + Math.abs(mPaint.ascent() + mPaint.descent()) / 2; + canvas.drawText(text, x, y, mPaint); + invalidateAfter(invalidateDelay); + return; + } + } if (canvas != null && text != null) { float y = getHeight() / 2 + Math.abs(mPaint.ascent() + mPaint.descent()) / 2; canvas.drawText(text, x, y, mPaint); @@ -128,4 +137,9 @@ private float getTextLength() { public void setSpeed(float speed) { this.speed = speed; } + + public void setTextT(CharSequence charSequence) { + super.setText(charSequence); + this.time = System.currentTimeMillis(); + } } diff --git a/app/src/main/res/xml/root_preferences.xml b/app/src/main/res/xml/root_preferences.xml index 7fb5f5d2..c884a14e 100644 --- a/app/src/main/res/xml/root_preferences.xml +++ b/app/src/main/res/xml/root_preferences.xml @@ -144,12 +144,6 @@ android:summary="@string/AutoHideCarrierName" android:title="@string/HideCarrierName"/> - - Date: Fri, 3 Dec 2021 10:33:40 +0800 Subject: [PATCH 04/12] =?UTF-8?q?=E6=9B=B4=E5=A5=BD=E7=9A=84Api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更好的Api --- app/src/main/AndroidManifest.xml | 2 + .../miui/statusbar/lyric/ApiListConfig.java | 72 +++++++++++++++++ .../lyric/activity/AboutActivity.java | 9 --- .../lyric/activity/ApiAPPListActivity.java | 53 +++++++++++++ .../lyric/activity/SettingsActivity.java | 50 ++++++++---- .../lyric/activity/TestActivity.java | 53 ------------- .../miui/statusbar/lyric/hook/HookApi.java | 78 +++++++++++++++++++ .../miui/statusbar/lyric/hook/MainHook.java | 25 +++--- .../lyric/utils/APiAPPListUtils.java | 20 +++++ .../statusbar/lyric/utils/ActivityUtils.java | 16 +++- .../miui/statusbar/lyric/utils/Utils.java | 48 +++++++++++- app/src/main/res/values-zh-rCN/strings.xml | 7 +- app/src/main/res/values-zh-rTW/strings.xml | 7 +- app/src/main/res/values/strings.xml | 7 +- app/src/main/res/xml/about_preferences.xml | 6 -- app/src/main/res/xml/api_preferences.xml | 6 ++ app/src/main/res/xml/root_preferences.xml | 11 ++- app/src/main/res/xml/test_preferences.xml | 21 ----- 18 files changed, 350 insertions(+), 141 deletions(-) create mode 100644 app/src/main/java/miui/statusbar/lyric/ApiListConfig.java create mode 100644 app/src/main/java/miui/statusbar/lyric/activity/ApiAPPListActivity.java delete mode 100644 app/src/main/java/miui/statusbar/lyric/activity/TestActivity.java create mode 100644 app/src/main/java/miui/statusbar/lyric/hook/HookApi.java create mode 100644 app/src/main/java/miui/statusbar/lyric/utils/APiAPPListUtils.java create mode 100644 app/src/main/res/xml/api_preferences.xml delete mode 100644 app/src/main/res/xml/test_preferences.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index be3a5aed..f11ac71e 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -25,6 +25,8 @@ + + { - startActivity(new Intent(activity, TestActivity.class)); - return true; - }); - } } \ No newline at end of file diff --git a/app/src/main/java/miui/statusbar/lyric/activity/ApiAPPListActivity.java b/app/src/main/java/miui/statusbar/lyric/activity/ApiAPPListActivity.java new file mode 100644 index 00000000..d3146f75 --- /dev/null +++ b/app/src/main/java/miui/statusbar/lyric/activity/ApiAPPListActivity.java @@ -0,0 +1,53 @@ +package miui.statusbar.lyric.activity; + +import android.annotation.SuppressLint; +import android.app.Activity; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageInfo; +import android.os.Bundle; +import android.preference.PreferenceActivity; +import android.preference.SwitchPreference; +import android.util.Log; + +import java.util.List; + +import miui.statusbar.lyric.ApiListConfig; +import miui.statusbar.lyric.R; +import miui.statusbar.lyric.utils.APiAPPListUtils; +import miui.statusbar.lyric.utils.ActivityUtils; +import miui.statusbar.lyric.utils.Utils; + +@SuppressWarnings("deprecation") +@SuppressLint("ExportedPreferenceActivity") +public class ApiAPPListActivity extends PreferenceActivity { + private final Activity activity = this; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + addPreferencesFromResource(R.xml.api_preferences); + getActionBar().setDisplayHomeAsUpEnabled(true); + setTitle(getString(R.string.UseApiList)); + + ApiListConfig apiConfig = ActivityUtils.getAppList(activity); + + List packages = getPackageManager().getInstalledPackages(0); + for (int i = 0; i < packages.size(); i++) { + PackageInfo packageInfo = packages.get(i); + if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { + if (APiAPPListUtils.isApi(getPackageManager(), packageInfo.packageName)) { + SwitchPreference switchPreference = new SwitchPreference(this); + switchPreference.setChecked(apiConfig.hasEnable(packageInfo.packageName)); + switchPreference.setSummary(packageInfo.packageName); + switchPreference.setTitle(packageInfo.applicationInfo.loadLabel(getPackageManager())); + switchPreference.setIcon(packageInfo.applicationInfo.loadIcon(getPackageManager())); + switchPreference.setOnPreferenceChangeListener((preference, newValue) -> { + apiConfig.setEnable(preference.getSummary().toString(), (Boolean) newValue); + return true; + }); + getPreferenceScreen().addPreference(switchPreference); + } + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/miui/statusbar/lyric/activity/SettingsActivity.java b/app/src/main/java/miui/statusbar/lyric/activity/SettingsActivity.java index 93f0d051..4c1af070 100644 --- a/app/src/main/java/miui/statusbar/lyric/activity/SettingsActivity.java +++ b/app/src/main/java/miui/statusbar/lyric/activity/SettingsActivity.java @@ -50,7 +50,7 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.root_preferences); try { - config = new Config(ActivityUtils.getSP(activity)); + config = new Config(ActivityUtils.getSP(activity, "Lyric_Config")); setTitle(String.format("%s (%s)", getString(R.string.AppName), getString(R.string.SPConfigMode))); init(); } catch (SecurityException ignored) { @@ -198,13 +198,17 @@ public void init() { lyricMaxWidth.setDialogMessage(String.format("%s%s", getString(R.string.LyricMaxWidthTips), getString(R.string.Adaptive))); lyricMaxWidth.setSummary(getString(R.string.Adaptive)); config.setLyricMaxWidth(-1); - String value = newValue.toString().replaceAll(" ", "").replaceAll("\n", ""); - if (value.equals("-1")) { - return true; - } else if (Integer.parseInt(value) <= 100 && Integer.parseInt(value) >= 0) { - config.setLyricMaxWidth(Integer.parseInt(value)); - lyricMaxWidth.setSummary(value); - } else { + try { + String value = newValue.toString().replaceAll(" ", "").replaceAll("\n", "").replaceAll("\\+", ""); + if (value.equals("-1")) { + return true; + } else if (Integer.parseInt(value) <= 100 && Integer.parseInt(value) >= 0) { + config.setLyricMaxWidth(Integer.parseInt(value)); + lyricMaxWidth.setSummary(value); + } else { + Toast.makeText(activity, getString(R.string.RangeError), Toast.LENGTH_LONG).show(); + } + } catch (NumberFormatException ignored) { Toast.makeText(activity, getString(R.string.RangeError), Toast.LENGTH_LONG).show(); } @@ -222,19 +226,24 @@ public void init() { lyricWidth.setDefaultValue(String.valueOf(config.getLyricWidth())); lyricWidth.setDialogMessage(String.format("%s%s", getString(R.string.LyricWidthTips), lyricWidth.getSummary())); lyricWidth.setOnPreferenceChangeListener((preference, newValue) -> { - String value = newValue.toString().replaceAll(" ", "").replaceAll("\n", ""); lyricMaxWidth.setEnabled(true); lyricWidth.setSummary(getString(R.string.Adaptive)); lyricWidth.setDialogMessage(String.format("%s%s", getString(R.string.LyricWidthTips), getString(R.string.Adaptive))); config.setLyricWidth(-1); - if (value.equals("-1")) { - return true; - } else if (Integer.parseInt(value) <= 100 && Integer.parseInt(value) >= 0) { - config.setLyricWidth(Integer.parseInt(value)); - lyricWidth.setSummary(value); - lyricMaxWidth.setEnabled(false); - lyricWidth.setDialogMessage(String.format("%s%s", getString(R.string.LyricWidthTips), value)); - } else { + try { + String value = newValue.toString().replaceAll(" ", "").replaceAll("\n", "").replaceAll("\\+", ""); + config.setLyricWidth(-1); + if (value.equals("-1")) { + return true; + } else if (Integer.parseInt(value) <= 100 && Integer.parseInt(value) >= 0) { + config.setLyricWidth(Integer.parseInt(value)); + lyricWidth.setSummary(value); + lyricMaxWidth.setEnabled(false); + lyricWidth.setDialogMessage(String.format("%s%s", getString(R.string.LyricWidthTips), value)); + } else { + Toast.makeText(activity, getString(R.string.RangeError), Toast.LENGTH_LONG).show(); + } + } catch (NumberFormatException ignored) { Toast.makeText(activity, getString(R.string.RangeError), Toast.LENGTH_LONG).show(); } @@ -554,6 +563,13 @@ public void onSuccess(String filePath, Uri uri, Intent intent) { return true; }); + // Apiactivity + Preference apiAc = findPreference("apiAc"); + assert apiAc != null; + apiAc.setOnPreferenceClickListener((preference) -> { + startActivity(new Intent(activity, ApiAPPListActivity.class)); + return true; + }); // 非MIUI关闭功能 if (!Utils.hasMiuiSetting) { diff --git a/app/src/main/java/miui/statusbar/lyric/activity/TestActivity.java b/app/src/main/java/miui/statusbar/lyric/activity/TestActivity.java deleted file mode 100644 index 12268e23..00000000 --- a/app/src/main/java/miui/statusbar/lyric/activity/TestActivity.java +++ /dev/null @@ -1,53 +0,0 @@ -package miui.statusbar.lyric.activity; - -import android.annotation.SuppressLint; -import android.app.Activity; -import android.os.Bundle; -import android.preference.EditTextPreference; -import android.preference.Preference; -import android.preference.PreferenceActivity; -import miui.statusbar.lyric.Api; -import miui.statusbar.lyric.BuildConfig; -import miui.statusbar.lyric.R; - -@SuppressWarnings("deprecation") -@SuppressLint("ExportedPreferenceActivity") -public class TestActivity extends PreferenceActivity { - private final Activity activity = this; - private String icon = "iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAACXBIWXMAAC4jAAAuIwF4pT92AAAByElEQVRYhe1X0W2EMAx9VF2AFbICK1xHYAVWYAU6Ah3hOgIdgRuBG+E6wuvHOT3jJiFHUamqWoqQibFfnh0nFCSxpzzsGh0ASH4O0WuSxw39z/zpeCSDACZumBfrLwnAGysZ1IeO5CDvJ5IHNVeS7EleZLQ6uPa3BKAWByTZmCCTjIbkKDZO5jrRWxm1on/mb1UK5GN6QCQr0TvRPTPu3hTk7gLPhCPZAKhE989neU7CRpnpN5sBv0IrukYOyk5XfX4RJgD0MYoDCxpNwLtTcFZBa3n3AuAdwCgUtySPpghbSY8DcFrwN0NsGXC8bZ9O2VUmFaMB4Kt90ExZf5aBQrNdFMUSw98Wk939z4J/AH8bgOyIcTcAAEoZUfm921A1Hn/G92Z+kvmW5h5gbNKXG9sJAwAaabvUAVR38yeg12trE4sXPIwiIEvffgMAfDt2MZvVAIRef/shyWnBuT35FgGkaqAH0AF4A/AUpWcuJa6nZr4kasCuOMhAIAWDtYnFI4nHBLYTgEoKr0rYHUm+AmhEf07YfpUEA07yf+H1MjGSvNjV8XZbmni9kMDaxOJl74II8FU/MNlF+FOSqoElOW8BoFjB4qbyAVZ9MC+3yLbAAAAAAElFTkSuQmCC"; - private String lyric = "TestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTestTest"; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - addPreferencesFromResource(R.xml.test_preferences); - getActionBar().setDisplayHomeAsUpEnabled(true); - setTitle(getString(R.string.DevelopmentTest)); - - Preference preference = findPreference("testlyric"); - preference.setOnPreferenceClickListener((preference1) -> { - Api api = new Api(); - api.sendLyric(activity, ((int) (Math.random() * 10)) + lyric, icon, BuildConfig.APPLICATION_ID, false); - return true; - }); - - preference = findPreference("teststop"); - preference.setOnPreferenceClickListener((preference1) -> { - Api api = new Api(); - api.stopLyric(activity); - return true; - }); - - EditTextPreference editTextPreference = (EditTextPreference) findPreference("testicon"); - editTextPreference.setOnPreferenceChangeListener((preference1, newValue) -> { - icon = newValue.toString(); - return true; - }); - - editTextPreference = (EditTextPreference) findPreference("testlyric1"); - editTextPreference.setOnPreferenceChangeListener((preference1, newValue) -> { - lyric = newValue.toString(); - return true; - }); - } -} \ No newline at end of file diff --git a/app/src/main/java/miui/statusbar/lyric/hook/HookApi.java b/app/src/main/java/miui/statusbar/lyric/hook/HookApi.java new file mode 100644 index 00000000..daad7d5c --- /dev/null +++ b/app/src/main/java/miui/statusbar/lyric/hook/HookApi.java @@ -0,0 +1,78 @@ +package miui.statusbar.lyric.hook; + +import static miui.statusbar.lyric.utils.Utils.PATH; + +import android.content.Context; +import android.content.Intent; + +import org.json.JSONArray; + +import java.io.FileOutputStream; + +import de.robv.android.xposed.XC_MethodHook; +import de.robv.android.xposed.XposedBridge; +import de.robv.android.xposed.XposedHelpers; +import de.robv.android.xposed.callbacks.XC_LoadPackage; +import miui.statusbar.lyric.utils.Utils; + +public class HookApi { + + public static class Hook { + public Hook(XC_LoadPackage.LoadPackageParam lpparam) { + XposedHelpers.findAndHookMethod("statusbarsdk.statusbarlyric", lpparam.classLoader, "hasEnable", new XC_MethodHook() { + @Override + protected void beforeHookedMethod(MethodHookParam param) throws Throwable { + super.beforeHookedMethod(param); + param.setResult(true); + } + + @Override + protected void afterHookedMethod(MethodHookParam param) throws Throwable { + super.afterHookedMethod(param); + } + }); + XposedHelpers.findAndHookMethod("statusbarsdk.statusbarlyric", lpparam.classLoader, "sendLyric", Context.class, String.class, String.class, boolean.class, new XC_MethodHook() { + @Override + protected void beforeHookedMethod(MethodHookParam param) throws Throwable { + super.beforeHookedMethod(param); + XposedBridge.log((String) param.args[1]); + Utils.sendLyric((Context) param.args[0], (String) param.args[1], (String) param.args[2], (boolean) param.args[3], lpparam.packageName); + } + + @Override + protected void afterHookedMethod(MethodHookParam param) throws Throwable { + super.afterHookedMethod(param); + } + }); + XposedHelpers.findAndHookMethod("statusbarsdk.statusbarlyric", lpparam.classLoader, "stopLyric", Context.class, new XC_MethodHook() { + @Override + protected void beforeHookedMethod(MethodHookParam param) throws Throwable { + super.beforeHookedMethod(param); + if (Utils.getConfig().getFileLyric()) { + try { + FileOutputStream outputStream = new FileOutputStream(PATH + "lyric.txt"); + JSONArray jsonArray = new JSONArray(); + jsonArray.put("app_stop"); + String json = jsonArray.toString(); + outputStream.write(json.getBytes()); + outputStream.close(); + } catch (Exception ignored) { + } + } else { + ((Context) param.args[0]).sendBroadcast( + new Intent() + .setAction("Lyric_Server") + .putExtra("Lyric_Type", "app_stop") + ); + } + } + + @Override + protected void afterHookedMethod(MethodHookParam param) throws Throwable { + super.afterHookedMethod(param); + } + }); + } + } + +} diff --git a/app/src/main/java/miui/statusbar/lyric/hook/MainHook.java b/app/src/main/java/miui/statusbar/lyric/hook/MainHook.java index 59f40f34..033e4513 100644 --- a/app/src/main/java/miui/statusbar/lyric/hook/MainHook.java +++ b/app/src/main/java/miui/statusbar/lyric/hook/MainHook.java @@ -28,6 +28,7 @@ import de.robv.android.xposed.XC_MethodHook; import de.robv.android.xposed.XposedHelpers; import de.robv.android.xposed.callbacks.XC_LoadPackage; +import miui.statusbar.lyric.ApiListConfig; import miui.statusbar.lyric.Config; import miui.statusbar.lyric.utils.Utils; import miui.statusbar.lyric.view.LyricTextSwitchView; @@ -131,15 +132,15 @@ protected void afterHookedMethod(MethodHookParam param) throws Throwable { } } else { try { - clockField = XposedHelpers.findField(param.thisObject.getClass(), "mStatusClock"); - Utils.log("mStatusClock 反射成功"); + clockField = XposedHelpers.findField(param.thisObject.getClass(), "mClockView"); + Utils.log("尝试 mClockView 反射成功"); } catch (NoSuchFieldError e) { - Utils.log("mStatusClock 反射失败: " + e + "\n" + Utils.dumpNoSuchFieldError(e)); + Utils.log("尝试 mClockView 反射失败: " + e + "\n" + Utils.dumpNoSuchFieldError(e)); try { - clockField = XposedHelpers.findField(param.thisObject.getClass(), "mClockView"); - Utils.log("mClockView 反射成功"); + clockField = XposedHelpers.findField(param.thisObject.getClass(), "mStatusClock"); + Utils.log("mStatusClock 反射成功"); } catch (NoSuchFieldError mE) { - Utils.log("mClockView 反射失败: " + mE + "\n" + Utils.dumpNoSuchFieldError(mE)); + Utils.log("mStatusClock 反射失败: " + mE + "\n" + Utils.dumpNoSuchFieldError(mE)); return; } } @@ -410,7 +411,6 @@ public void run() { icon[0] = strArr[0]; switch (icon[0]) { case "hook": -// Utils.addLyricCount(); if (!strArr[2].equals("")) { lyric = strArr[2]; } @@ -419,7 +419,6 @@ public void run() { useSystemMusicActive = true; break; case "app": -// Utils.addLyricCount(); if (!strArr[2].equals("")) { lyric = strArr[2]; } @@ -650,6 +649,12 @@ protected void afterHookedMethod(MethodHookParam param) throws Throwable { break; case "com.meizu.media.music": MeiZuStatusBarLyric.guiseFlyme(lpparam); + break; + default: + ApiListConfig apiConfig = Utils.getAppList(); + if (apiConfig.hasEnable(lpparam.packageName)) { + new HookApi.Hook(lpparam); + } } } @@ -676,14 +681,13 @@ public void onReceive(Context context, Intent intent) { if (intent.getAction().equals("Lyric_Server")) { switch (intent.getStringExtra("Lyric_Type")) { case "hook": -// Utils.addLyricCount(); lyric = intent.getStringExtra("Lyric_Data"); icon[0] = "hook"; icon[1] = config.getIconPath() + intent.getStringExtra("Lyric_Icon") + ".webp"; Utils.log("收到广播hook: lyric:" + lyric + " icon:" + icon[1]); + useSystemMusicActive = true; break; case "app": -// Utils.addLyricCount(); lyric = intent.getStringExtra("Lyric_Data"); icon[0] = "app"; String icon_data = intent.getStringExtra("Lyric_Icon"); @@ -706,6 +710,7 @@ public void onReceive(Context context, Intent intent) { musicServer = Utils.stringsListAdd(musicServer, packName); } } + useSystemMusicActive = intent.getBooleanExtra("Lyric_UseSystemMusicActive", false); musicOffStatus = true; Utils.log("收到广播app: lyric:" + lyric + " icon:" + icon[1] + "packName:" + packName + " isPackName: " + isPackName); break; diff --git a/app/src/main/java/miui/statusbar/lyric/utils/APiAPPListUtils.java b/app/src/main/java/miui/statusbar/lyric/utils/APiAPPListUtils.java new file mode 100644 index 00000000..5caeca37 --- /dev/null +++ b/app/src/main/java/miui/statusbar/lyric/utils/APiAPPListUtils.java @@ -0,0 +1,20 @@ +package miui.statusbar.lyric.utils; + +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageManager; + +public class APiAPPListUtils { + public static boolean isApi(PackageManager packageManager, String packName) { + try { + ApplicationInfo appInfo = packageManager.getApplicationInfo(packName, PackageManager.GET_META_DATA); + if (appInfo.metaData != null) { + return appInfo.metaData.getBoolean("XStatusBarLyric", false); + } else { + return false; + } + } catch (PackageManager.NameNotFoundException e) { + e.printStackTrace(); + return false; + } + } +} diff --git a/app/src/main/java/miui/statusbar/lyric/utils/ActivityUtils.java b/app/src/main/java/miui/statusbar/lyric/utils/ActivityUtils.java index 8d3fdfe8..b57da799 100644 --- a/app/src/main/java/miui/statusbar/lyric/utils/ActivityUtils.java +++ b/app/src/main/java/miui/statusbar/lyric/utils/ActivityUtils.java @@ -15,6 +15,8 @@ import android.os.Message; import android.os.Process; import android.widget.Toast; + +import miui.statusbar.lyric.ApiListConfig; import miui.statusbar.lyric.Config; import miui.statusbar.lyric.R; import org.json.JSONException; @@ -254,15 +256,21 @@ public static void fixConfig(Activity activity, Config config) { public static Config getConfig(Context context) { try { - return new Config(getSP(context)); + return new Config(getSP(context, "Lyric_Config")); } catch (SecurityException ignored) { return new Config(); } } - public static SharedPreferences getSP(Context context) { - return context.createDeviceProtectedStorageContext().getSharedPreferences("Lyric_Config", Context.MODE_WORLD_READABLE); + public static SharedPreferences getSP(Context context, String key) { + return context.createDeviceProtectedStorageContext().getSharedPreferences(key, Context.MODE_WORLD_READABLE); } - + public static ApiListConfig getAppList(Context context) { + try { + return new ApiListConfig(getSP(context, "AppList_Config")); + } catch (SecurityException ignored) { + return new ApiListConfig(); + } + } } \ No newline at end of file diff --git a/app/src/main/java/miui/statusbar/lyric/utils/Utils.java b/app/src/main/java/miui/statusbar/lyric/utils/Utils.java index 9c3cf543..7dd01f65 100644 --- a/app/src/main/java/miui/statusbar/lyric/utils/Utils.java +++ b/app/src/main/java/miui/statusbar/lyric/utils/Utils.java @@ -37,6 +37,7 @@ import de.robv.android.xposed.XSharedPreferences; import de.robv.android.xposed.XposedBridge; +import miui.statusbar.lyric.ApiListConfig; import miui.statusbar.lyric.BuildConfig; import miui.statusbar.lyric.Config; @@ -173,6 +174,38 @@ public static void sendLyric(Context context, String lyric, String icon) { } } + public static void sendLyric(Context context, String lyric, String icon, boolean useSystemMusicActive, String packName) { + if (Utils.getConfig().getFileLyric()) { + setLyricFile(packName, lyric, icon, useSystemMusicActive); + } else { + context.sendBroadcast(new Intent() + .setAction("Lyric_Server") + .putExtra("Lyric_Data", lyric) + .putExtra("Lyric_Type", "app") + .putExtra("Lyric_PackName", packName) + .putExtra("Lyric_Icon", icon) + .putExtra("Lyric_UseSystemMusicActive", useSystemMusicActive) + ); + } + } + + // 写入歌词文件 + public static void setLyricFile(String PackName, String lyric, String icon, boolean useSystemMusicActive) { + try { + FileOutputStream outputStream = new FileOutputStream(PATH + "lyric.txt"); + JSONArray jsonArray = new JSONArray(); + jsonArray.put("app"); + jsonArray.put(PackName); + jsonArray.put(lyric); + jsonArray.put(icon); + jsonArray.put(useSystemMusicActive); + String json = jsonArray.toString(); + outputStream.write(json.getBytes()); + outputStream.close(); + } catch (Exception ignored) { + } + } + //获取歌词文件 public static String[] getLyricFile() { String[] res = {"", ""}; @@ -380,13 +413,13 @@ public static void log(String text) { } } - public static XSharedPreferences getPref() { - XSharedPreferences pref = new XSharedPreferences(BuildConfig.APPLICATION_ID, "Lyric_Config"); + public static XSharedPreferences getPref(String key) { + XSharedPreferences pref = new XSharedPreferences(BuildConfig.APPLICATION_ID, key); return pref.getFile().canRead() ? pref : null; } public static Config getConfig() { - XSharedPreferences xSharedPreferences = getPref(); + XSharedPreferences xSharedPreferences = getPref("Lyric_Config"); if (xSharedPreferences == null) { return new Config(); } else { @@ -394,5 +427,14 @@ public static Config getConfig() { } } + public static ApiListConfig getAppList() { + XSharedPreferences xSharedPreferences = getPref("AppList_Config"); + if (xSharedPreferences == null) { + return new ApiListConfig(); + } else { + return new ApiListConfig(xSharedPreferences); + } + } + } diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index e23261d8..3d05837c 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -52,11 +52,6 @@ 觉得好用的话,支持一下呗 联系我们 项目地址 - 开发测试 - 显示测试歌词 - 自定义图标 - 自定义歌词 - 停止 https://github.com/577fkj/MIUIStatusBarLyric#%E6%84%9F%E8%B0%A2%E5%90%8D%E5%8D%95%E4%B8%8D%E5%88%86%E5%85%88%E5%90%8E @@ -195,4 +190,6 @@ 使用文件保存配置 SP 配置文件 文件配置文件 + 使用 API 的软件 + 软件列表 \ No newline at end of file diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index 450ff3fd..e16c9d22 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -52,11 +52,6 @@ 覺得好用的話,支持一下唄 聯系我們 項目地址 - 開發測試 - 顯示測試歌詞 - 自定義圖標 - 自定義歌詞 - 停止 https://github.com/577fkj/MIUIStatusBarLyric#%E6%84%9F%E8%B0%A2%E5%90%8D%E5%8D%95%E4%B8%8D%E5%88%86%E5%85%88%E5%90%8E @@ -181,4 +176,6 @@ 使用文件保存配置 文件配置文件 SP 配置文件 + 使用 API 的軟件 + 軟件列表 \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1edcb543..bf03faae 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -55,11 +55,6 @@ If you find it easy to use, please support me contact us Project Address - Development test - Show test lyrics - Custom icon - Custom lyrics - Stop https://github.com/577fkj/MIUIStatusBarLyric/blob/main/README_EN.md#thanks-listin-no-particular-order @@ -192,4 +187,6 @@ Use file to save configuration SP configuration file File configuration file + Software using API + Software list \ No newline at end of file diff --git a/app/src/main/res/xml/about_preferences.xml b/app/src/main/res/xml/about_preferences.xml index 76b6fd8f..d12e6f3a 100644 --- a/app/src/main/res/xml/about_preferences.xml +++ b/app/src/main/res/xml/about_preferences.xml @@ -63,10 +63,4 @@ android:data="https://github.com/577fkj/MIUIStatusBarLyric"/> - - - diff --git a/app/src/main/res/xml/api_preferences.xml b/app/src/main/res/xml/api_preferences.xml new file mode 100644 index 00000000..2d43d2c5 --- /dev/null +++ b/app/src/main/res/xml/api_preferences.xml @@ -0,0 +1,6 @@ + + + + diff --git a/app/src/main/res/xml/root_preferences.xml b/app/src/main/res/xml/root_preferences.xml index c884a14e..0d881885 100644 --- a/app/src/main/res/xml/root_preferences.xml +++ b/app/src/main/res/xml/root_preferences.xml @@ -31,7 +31,7 @@ android:summary="@string/Adaptive" android:title="@string/LyricWidth" android:maxLength="4" - android:inputType="number"/> + android:inputType="numberSigned"/> + android:inputType="numberSigned"/> + android:inputType="numberSigned"/> + + - - - - - - - - - - - \ No newline at end of file From 342eac020d043a9a37c4263f70702594741233a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=B9=E5=9D=97=E5=90=9B?= Date: Fri, 3 Dec 2021 10:43:26 +0800 Subject: [PATCH 05/12] =?UTF-8?q?=E6=9B=B4=E6=96=B0api=20readme?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更新api readme --- Api/Api.java | 136 ------------------ Api/Api.kt | 131 ----------------- Api/README.md | 11 ++ Api/statusbarsdk/statusbarlyric.java | 80 +++++++++++ README.md | 3 +- README_EN.md | 3 +- .../main/java/miui/statusbar/lyric/Api.java | 91 ------------ 7 files changed, 93 insertions(+), 362 deletions(-) delete mode 100644 Api/Api.java delete mode 100644 Api/Api.kt create mode 100644 Api/README.md create mode 100644 Api/statusbarsdk/statusbarlyric.java delete mode 100644 app/src/main/java/miui/statusbar/lyric/Api.java diff --git a/Api/Api.java b/Api/Api.java deleted file mode 100644 index 7b091e36..00000000 --- a/Api/Api.java +++ /dev/null @@ -1,136 +0,0 @@ -import android.content.Context; -import android.content.Intent; -import android.os.Environment; -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; - -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; - -/** - * 状态栏歌词Api - * @author FKj - * @version 1.2 - */ -public class Api { - String PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/media/miui.statusbar.lyric/"; - - /** - * SendLyric / 发送歌词   - *

- * this function will broadcast a intent containing a single line lyrics, which would be displayed (and remained) on statusbar until you send another intent or you call {@link #stopLyric} manually or your app is killed. - *

- *

- * 发送单行歌词的广播Intent, 歌词将一直停留在状态栏! 调用{@link #stopLyric}清除(当然你的应用被杀死也会清除) - *

- * - * Example/例:
- * - * // for (....) {
- * // ...
- * String line = XXXXX(); // your method to get the current lyrics
- * // example of BASE64 code of notification icon, it should be like this:
- * String icon = "XEK+FoG3bhD/pK3u ..... ACTEFV98A";
- * sendLyric(mContext, line, icon, BuildConfig.APPLICATION_ID, true);
- * // ...
- * // } - *
- * - * @param context - * @param lyric A single line lyrics 单行歌词 - * @param icon (notification) icon (you can use your music service's notification icon), coded in BASE64, format should be webp. 通知栏图标(Webp格式,Base64编码) - * @param packName package name/id, actually music service's name in manifest(or s string containing service's name) 包名,实际为音乐服务名(或包含该服务名的字符串) - * @param useSystemMusicActive detect your music service running status via system. 是否使用系统检测音乐是否播放 - */ - public void sendLyric(Context context, String lyric, String icon, String packName, boolean useSystemMusicActive) { - if (new Config().isFileLyric()) { - setLyricFile(packName, lyric, icon, useSystemMusicActive); - } else { - context.sendBroadcast(new Intent() - .setAction("Lyric_Server") - .putExtra("Lyric_Data", lyric) - .putExtra("Lyric_Type", "app") - .putExtra("Lyric_PackName", packName) - .putExtra("Lyric_Icon", icon) - .putExtra("Lyric_UseSystemMusicActive", useSystemMusicActive) - ); - } - } - - /** - * StopLyric (useSystemMusicActive for 'true' No need to use) / 停止播放 (useSystemMusicActive 为 'true' 无需使用) - * @param context - */ - public void stopLyric(Context context) { - if (new Config().isFileLyric()) { - setLyricFile_stop(); - } else { - context.sendBroadcast(new Intent().setAction("Lyric_Server").putExtra("Lyric_Type", "app_stop")); - } - } - - // 写入歌词文件 - public void setLyricFile(String PackName, String lyric, String icon, boolean useSystemMusicActive) { - try { - FileOutputStream outputStream = new FileOutputStream(PATH + "lyric.txt"); - JSONArray jsonArray = new JSONArray(); - jsonArray.put("app"); - jsonArray.put(PackName); - jsonArray.put(lyric); - jsonArray.put(icon); - jsonArray.put(useSystemMusicActive); - String json = jsonArray.toString(); - outputStream.write(json.getBytes()); - outputStream.close(); - } catch (Exception ignored) { - } - } - - // 写入暂停文件 - public void setLyricFile_stop() { - try { - FileOutputStream outputStream = new FileOutputStream(PATH + "lyric.txt"); - JSONArray jsonArray = new JSONArray(); - jsonArray.put("app_stop"); - String json = jsonArray.toString(); - outputStream.write(json.getBytes()); - outputStream.close(); - } catch (Exception ignored) { - } - } - - public class Config { - JSONObject config = new JSONObject(); - - public Config() { - try { - if (getConfig().equals("")) { - config = new JSONObject(); - return; - } - config = new JSONObject(getConfig()); - } catch (JSONException ignored) { - } - } - - public String getConfig() { - String str = ""; - try { - FileInputStream fileInputStream = new FileInputStream(PATH + "Config.json"); - byte[] bArr = new byte[fileInputStream.available()]; - fileInputStream.read(bArr); - str = new String(bArr); - fileInputStream.close(); - } catch (IOException e) { - e.printStackTrace(); - } - return str; - } - - public boolean isFileLyric() { - return config.optBoolean("FileLyric", false); - } - } -} \ No newline at end of file diff --git a/Api/Api.kt b/Api/Api.kt deleted file mode 100644 index 0f726cd9..00000000 --- a/Api/Api.kt +++ /dev/null @@ -1,131 +0,0 @@ -import android.content.Context -import android.content.Intent -import android.os.Environment -import org.json.JSONArray -import org.json.JSONException -import org.json.JSONObject -import java.io.FileInputStream -import java.io.FileOutputStream -import java.io.IOException - -/** - * 状态栏歌词Api - * @author FKj - * @version 1.2 - */ -class Api { - var PATH: String = Environment.getExternalStorageDirectory().getAbsolutePath().toString() + "/Android/media/miui.statusbar.lyric/" - - /** - * SendLyric / 发送歌词 - * - - * this function will broadcast a intent containing a single line lyrics, which would be displayed (and remained) on statusbar until you send another intent or you call [stopLyric] manually or your app is killed. - * - - * 发送单行歌词的广播Intent, 歌词将一直停留在状态栏! 调用[stopLyric]清除(当然你的应用被杀死也会清除) - * - - * Example/例: - * ``` - * // for ( XXX in XXXX) { - * // ... - * val line: String = XXXXX() // your method to get the current lyrics - * // example of BASE64 code of notification icon, it should be like this: - * val icon: String = "XEK+FoG3bhD/pK3u ..... ACTEFV98A" - * sendLyric(mContext, line, icon, BuildConfig.APPLICATION_ID, true) - * // ... - * // } - * ``` - * - * @param context - * @param lyric A **single** line lyrics **单行**歌词 - * @param icon (notification) icon (you can use your music service's notification icon), coded in BASE64, format should be webp. 通知栏图标(Webp格式,Base64编码) - * @param packName package name/id, actually music service's name in manifest(or s string containing service's name) 包名, 实际为音乐服务名(或包含该服务名的字符串) - * @param useSystemMusicActive detect your music service running status via system. 是否使用系统检测音乐是否播放 - */ - fun sendLyric(context: Context, lyric: String?, icon: String?, packName: String?, useSystemMusicActive: Boolean) { - if (Config().isFileLyric()) { - setLyricFile(packName, lyric, icon, useSystemMusicActive) - } else { - context.sendBroadcast(Intent() - .setAction("Lyric_Server") - .putExtra("Lyric_Data", lyric) - .putExtra("Lyric_Type", "app") - .putExtra("Lyric_PackName", packName) - .putExtra("Lyric_Icon", icon) - .putExtra("Lyric_UseSystemMusicActive", useSystemMusicActive) - ) - } - } - - /** - * StopLyric (useSystemMusicActive for 'true' No need to use) / 停止播放 (useSystemMusicActive 为 'true' 无需使用) - * @param context - */ - fun stopLyric(context: Context) { - if (Config().isFileLyric()) { - setLyricFile_stop() - } else { - context.sendBroadcast(Intent().setAction("Lyric_Server").putExtra("Lyric_Type", "app_stop")) - } - } - - // 写入歌词文件 - fun setLyricFile(PackName: String?, lyric: String?, icon: String?, useSystemMusicActive: Boolean) { - try { - val outputStream = FileOutputStream(PATH + "lyric.txt") - val jsonArray = JSONArray() - jsonArray.put("app") - jsonArray.put(PackName) - jsonArray.put(lyric) - jsonArray.put(icon) - jsonArray.put(useSystemMusicActive) - val json: String = jsonArray.toString() - outputStream.write(json.getBytes()) - outputStream.close() - } catch (ignored: Exception) { - } - } - - // 写入暂停文件 - fun setLyricFile_stop() { - try { - val outputStream = FileOutputStream(PATH + "lyric.txt") - val jsonArray = JSONArray() - jsonArray.put("app_stop") - val json: String = jsonArray.toString() - outputStream.write(json.getBytes()) - outputStream.close() - } catch (ignored: Exception) { - } - } - - inner class Config { - var config: JSONObject = JSONObject() - fun getConfig(): String { - var str = "" - try { - val fileInputStream = FileInputStream(PATH + "Config.json") - val bArr = ByteArray(fileInputStream.available()) - fileInputStream.read(bArr) - str = String(bArr) - fileInputStream.close() - } catch (e: IOException) { - e.printStackTrace() - } - return str - } - - val isFileLyric: Boolean - get() = config.optBoolean("FileLyric", false) - - init { - try { - if (getConfig().equals("")) { - config = JSONObject() - return - } - config = JSONObject(getConfig()) - } catch (ignored: JSONException) { - } - } - } -} \ No newline at end of file diff --git a/Api/README.md b/Api/README.md new file mode 100644 index 00000000..9baab16a --- /dev/null +++ b/Api/README.md @@ -0,0 +1,11 @@ +# StatusBarAPi + +(StatusBarApiExample)[https://github.com/577fkj/StatusBarApiExample] + +To use SDK, please increase the package statusbarsdk and ignore this package in the confusion + +# StatusBarAPi + +(StatusBarApiExample)[https://github.com/577fkj/StatusBarApiExample] + +使用sdk请增加软件包 statusbarsdk 并在混淆里忽略此包 \ No newline at end of file diff --git a/Api/statusbarsdk/statusbarlyric.java b/Api/statusbarsdk/statusbarlyric.java new file mode 100644 index 00000000..8fd5f048 --- /dev/null +++ b/Api/statusbarsdk/statusbarlyric.java @@ -0,0 +1,80 @@ +package statusbarsdk; + +import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.drawable.BitmapDrawable; +import android.graphics.drawable.Drawable; +import android.util.Base64; + +import java.io.ByteArrayOutputStream; + +public class statusbarlyric { + + String icon; + Context context; + boolean useSystemMusicActive; + + /** + * InitStatusBarLyric / 初始化状态栏歌词   + *

+ * Call {@link #sendLyric} to send lyrics, Call {@link #stopLyric} to clear (of course, your app will be cleared if killed), Call {@link #hasEnable} to determine whether to activate the Xposed module. + *

+ * 调用{@link #sendLyric}发送歌词, 调用{@link #stopLyric}清除(当然你的应用被杀死也会清除), 调用{@link #hasEnable}判断是否激活模块. + *

+ * + * @param context context + * @param drawable (notification) icon (you can use your music service's notification icon), Drawable, format should be webp. 通知栏图标(Webp格式,Drawable) + * @param useSystemMusicActive detect your music service running status via system. 是否使用系统检测音乐是否播放 + */ + public statusbarlyric(Context context, Drawable drawable, boolean useSystemMusicActive) { + icon = drawableToBase64(drawable); + this.context = context; + this.useSystemMusicActive = useSystemMusicActive; + } + + /** + * SendLyric / 发送歌词   + *

+ * this function will broadcast a intent containing a single line lyrics, which would be displayed (and remained) on statusbar until you send another intent or you call {@link #stopLyric} manually or your app is killed. + *

+ * 发送单行歌词的广播Intent, 歌词将一直停留在状态栏! 调用{@link #stopLyric}清除(当然你的应用被杀死也会清除) + *

+ * + * @param lyric A single line lyrics 单行歌词 + */ + public void updateLyric(String lyric) { + sendLyric(context, lyric, icon, useSystemMusicActive); + } + + /** + * Whether to activate the Xposed module / 是否激活模块   + *

+ * Get whether the Xposed module is activated for this software + *

+ * 获取模块是否对本软件激活 + *

+ */ + public boolean hasEnable() { + return false; + } + + /** + * StopLyric (useSystemMusicActive for 'true' No need to use) / 停止播放 (useSystemMusicActive 为 'true' 无需使用) + */ + public void stopLyric() { + stopLyric(context); + } + + protected String drawableToBase64(Drawable drawable) { + Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + bitmap.compress(Bitmap.CompressFormat.PNG,100,baos); + byte[] bytes = baos.toByteArray(); + return Base64.encodeToString(bytes, Base64.DEFAULT); + } + + protected void sendLyric(Context context, String lyric, String icon, boolean useSystemMusicActive) {} + + protected void stopLyric(Context context) {} + +} diff --git a/README.md b/README.md index fa66c3bb..6ef22f91 100644 --- a/README.md +++ b/README.md @@ -67,5 +67,4 @@ ## 音乐主动适配 -请查看 Java [Api.java](https://github.com/577fkj/MIUIStatusBarLyric/blob/main/Api/Api.java) -请查看 Kotlin [Api.kt](https://github.com/577fkj/MIUIStatusBarLyric/blob/main/Api/Api.kt) +请查看 [Api](./Api) diff --git a/README_EN.md b/README_EN.md index dba67813..a7a125bd 100644 --- a/README_EN.md +++ b/README_EN.md @@ -65,5 +65,4 @@ ## App active adaptation -please check Java [Api.java](https://github.com/577fkj/MIUIStatusBarLyric/blob/main/Api/Api.java) -please check Kotlin [Api.kt](https://github.com/577fkj/MIUIStatusBarLyric/blob/main/Api/Api.kt) +please check [Api](./Api) diff --git a/app/src/main/java/miui/statusbar/lyric/Api.java b/app/src/main/java/miui/statusbar/lyric/Api.java deleted file mode 100644 index 0d29506f..00000000 --- a/app/src/main/java/miui/statusbar/lyric/Api.java +++ /dev/null @@ -1,91 +0,0 @@ -package miui.statusbar.lyric; - -import android.content.Context; -import android.content.Intent; -import android.os.Environment; - -import org.json.JSONArray; - -import java.io.FileOutputStream; - -import miui.statusbar.lyric.utils.ActivityUtils; - -/** - * 状态栏歌词Api - * @author FKj - * @version 1.2 - */ -public class Api { - String PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/media/miui.statusbar.lyric/"; - - /** - * SendLyric / 发送歌词   - *

- * this function will broadcast a intent containing a single line lyrics, which would be displayed (and remained) on statusbar until you send another intent or you call {@link #stopLyric} manually or your app is killed. - *

- * 发送单行歌词的广播Intent, 歌词将一直停留在状态栏! 调用{@link #stopLyric}清除(当然你的应用被杀死也会清除) - *

- * - * @param context context - * @param lyric A single line lyrics 单行歌词 - * @param icon (notification) icon (you can use your music service's notification icon), coded in BASE64, format should be webp. 通知栏图标(Webp格式,Base64编码) - * @param packName package name/id, actually music service's name in manifest(or s string containing service's name) 包名,实际为音乐服务名(或包含该服务名的字符串) - * @param useSystemMusicActive detect your music service running status via system. 是否使用系统检测音乐是否播放 - */ - public void sendLyric(Context context, String lyric, String icon, String packName, boolean useSystemMusicActive) { - if (ActivityUtils.getConfig(context).getFileLyric()) { - setLyricFile(packName, lyric, icon, useSystemMusicActive); - } else { - context.sendBroadcast(new Intent() - .setAction("Lyric_Server") - .putExtra("Lyric_Data", lyric) - .putExtra("Lyric_Type", "app") - .putExtra("Lyric_PackName", packName) - .putExtra("Lyric_Icon", icon) - .putExtra("Lyric_UseSystemMusicActive", useSystemMusicActive) - ); - } - } - - /** - * StopLyric (useSystemMusicActive for 'true' No need to use) / 停止播放 (useSystemMusicActive 为 'true' 无需使用) - * @param context - */ - public void stopLyric(Context context) { - if (ActivityUtils.getConfig(context).getFileLyric()) { - setLyricFile_stop(); - } else { - context.sendBroadcast(new Intent().setAction("Lyric_Server").putExtra("Lyric_Type", "app_stop")); - } - } - - // 写入歌词文件 - public void setLyricFile(String PackName, String lyric, String icon, boolean useSystemMusicActive) { - try { - FileOutputStream outputStream = new FileOutputStream(PATH + "lyric.txt"); - JSONArray jsonArray = new JSONArray(); - jsonArray.put("app"); - jsonArray.put(PackName); - jsonArray.put(lyric); - jsonArray.put(icon); - jsonArray.put(useSystemMusicActive); - String json = jsonArray.toString(); - outputStream.write(json.getBytes()); - outputStream.close(); - } catch (Exception ignored) { - } - } - - // 写入暂停文件 - public void setLyricFile_stop() { - try { - FileOutputStream outputStream = new FileOutputStream(PATH + "lyric.txt"); - JSONArray jsonArray = new JSONArray(); - jsonArray.put("app_stop"); - String json = jsonArray.toString(); - outputStream.write(json.getBytes()); - outputStream.close(); - } catch (Exception ignored) { - } - } -} \ No newline at end of file From baa3dac373f3a24ca816fe9cd4ba7e15d00ad64f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=B9=E5=9D=97=E5=90=9B?= Date: Fri, 3 Dec 2021 10:44:30 +0800 Subject: [PATCH 06/12] Update README.md --- Api/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Api/README.md b/Api/README.md index 9baab16a..70994447 100644 --- a/Api/README.md +++ b/Api/README.md @@ -1,11 +1,11 @@ # StatusBarAPi -(StatusBarApiExample)[https://github.com/577fkj/StatusBarApiExample] +[StatusBarApiExample](https://github.com/577fkj/StatusBarApiExample) To use SDK, please increase the package statusbarsdk and ignore this package in the confusion # StatusBarAPi -(StatusBarApiExample)[https://github.com/577fkj/StatusBarApiExample] +[StatusBarApiExample](https://github.com/577fkj/StatusBarApiExample) 使用sdk请增加软件包 statusbarsdk 并在混淆里忽略此包 \ No newline at end of file From bc6c4b3ebf9f8eb3edc0a15cb06a59bee910d8ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=B9=E5=9D=97=E5=90=9B?= Date: Fri, 3 Dec 2021 10:50:46 +0800 Subject: [PATCH 07/12] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=82=B9=E6=8F=90?= =?UTF-8?q?=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加点提示 --- app/build.gradle | 2 +- app/src/main/res/values-zh-rCN/strings.xml | 2 +- app/src/main/res/values-zh-rTW/strings.xml | 2 +- app/src/main/res/values/strings.xml | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 3b10b959..f51c7cb1 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -13,7 +13,7 @@ android { //noinspection OldTargetApi targetSdkVersion 29 versionCode 56 - versionName "3.33.3-dev" + versionName "3.35.3-dev" aaptOptions.cruncherEnabled = false aaptOptions.useNewCruncher = false } diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index 3d05837c..44df0aa0 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -190,6 +190,6 @@ 使用文件保存配置 SP 配置文件 文件配置文件 - 使用 API 的软件 + 使用 API 的软件 (需要在EDXposed或LSPosed勾选开启的软件) 软件列表 \ No newline at end of file diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index e16c9d22..f7df765e 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -176,6 +176,6 @@ 使用文件保存配置 文件配置文件 SP 配置文件 - 使用 API 的軟件 + 使用 API 的軟件 (需要在EDXposed或LSPosed勾選開啟的軟件) 軟件列表 \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index bf03faae..492a1d3b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -187,6 +187,6 @@ Use file to save configuration SP configuration file File configuration file - Software using API + Software using API (The software that needs to be checked in EDXposed or LSPosed) Software list \ No newline at end of file From d8a52b15d4540506cf5edde88ce30fa9849cccbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=B9=E5=9D=97=E5=90=9B?= Date: Fri, 3 Dec 2021 10:59:06 +0800 Subject: [PATCH 08/12] =?UTF-8?q?=E5=AE=8C=E5=96=84api=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Api/README.md | 18 ++++++++++++++++-- app/src/main/AndroidManifest.xml | 2 -- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/Api/README.md b/Api/README.md index 70994447..38e618f8 100644 --- a/Api/README.md +++ b/Api/README.md @@ -2,10 +2,24 @@ [StatusBarApiExample](https://github.com/577fkj/StatusBarApiExample) -To use SDK, please increase the package statusbarsdk and ignore this package in the confusion +To use API, please increase the package statusbarsdk and ignore this package in the confusion + +Add under application in AndroidManifest.xml +'''xml + +''' # StatusBarAPi [StatusBarApiExample](https://github.com/577fkj/StatusBarApiExample) -使用sdk请增加软件包 statusbarsdk 并在混淆里忽略此包 \ No newline at end of file +使用 API 请增加软件包 statusbarsdk 并在混淆里忽略此包 + +在AndroidManifest.xml 中的 application 下添加 +'''xml + +''' \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index f11ac71e..c15f35f8 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -23,8 +23,6 @@ - - Date: Fri, 3 Dec 2021 10:59:59 +0800 Subject: [PATCH 09/12] Update README.md --- Api/README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Api/README.md b/Api/README.md index 38e618f8..c11a0f6e 100644 --- a/Api/README.md +++ b/Api/README.md @@ -5,11 +5,11 @@ To use API, please increase the package statusbarsdk and ignore this package in the confusion Add under application in AndroidManifest.xml -'''xml +```xml -''' +``` # StatusBarAPi @@ -18,8 +18,8 @@ android:value="true" /> 使用 API 请增加软件包 statusbarsdk 并在混淆里忽略此包 在AndroidManifest.xml 中的 application 下添加 -'''xml +```xml -''' \ No newline at end of file +``` \ No newline at end of file From d8526d1570c1ffe4321136e3045632338ba63139 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=B9=E5=9D=97=E5=90=9B?= Date: Fri, 3 Dec 2021 11:15:39 +0800 Subject: [PATCH 10/12] =?UTF-8?q?=E5=85=81=E8=AE=B8=E4=B8=8D=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E5=9B=BE=E6=A0=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Api/statusbarsdk/statusbarlyric.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Api/statusbarsdk/statusbarlyric.java b/Api/statusbarsdk/statusbarlyric.java index 8fd5f048..195fac7c 100644 --- a/Api/statusbarsdk/statusbarlyric.java +++ b/Api/statusbarsdk/statusbarlyric.java @@ -66,6 +66,9 @@ public void stopLyric() { } protected String drawableToBase64(Drawable drawable) { + if (drawable == null) { + return ""; + } Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG,100,baos); From 039d5fb413caee1e46f99840324af9997947c55c Mon Sep 17 00:00:00 2001 From: 577fkj Date: Fri, 3 Dec 2021 13:49:50 +0800 Subject: [PATCH 11/12] Update build.gradle --- app/build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index f51c7cb1..e260c194 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -12,8 +12,8 @@ android { minSdkVersion 27 //noinspection OldTargetApi targetSdkVersion 29 - versionCode 56 - versionName "3.35.3-dev" + versionCode 57 + versionName "3.35.3" aaptOptions.cruncherEnabled = false aaptOptions.useNewCruncher = false } From 705e5fe0e4d1fc287e361c0cec611ee14a3d65f4 Mon Sep 17 00:00:00 2001 From: 577fkj Date: Fri, 3 Dec 2021 13:51:24 +0800 Subject: [PATCH 12/12] Update build.gradle --- app/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/build.gradle b/app/build.gradle index e260c194..75909a90 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -13,7 +13,7 @@ android { //noinspection OldTargetApi targetSdkVersion 29 versionCode 57 - versionName "3.35.3" + versionName "3.34.5" aaptOptions.cruncherEnabled = false aaptOptions.useNewCruncher = false }