Skip to content

Commit

Permalink
更新README文件,优化功能,修复已知问题。Updated README file, optimized functionality, …
Browse files Browse the repository at this point in the history
…and fixed known issues.

中文:
- 更新 README文件,按照统一格式编写。
- 优化 versionName,使用三位数字表示版本号。
- 优化 使用信号量(Semaphore)来处理线程间的同步问题,而不是使用忙等待(Thread.sleep()),优化应用程序的性能。
- 优化 使用lambda表达式替换对匿名内部类的方法重写(Override)。
- 修复 若歌曲文件的ID3标签本身就有问题,获取该歌曲的标签会导致应用程序闪退的问题。(感谢酷友 @路还要走 发现该问题!)
- 优化 添加、修改翻译文件。

English:
- Updated README file to follow a unified format.
- Optimized versionName to use a three-digit number for versioning.
- Optimized the use of Semaphores to handle thread synchronization issues instead of using busy waiting (Thread.sleep()), improving application performance.
- Optimized the use of lambda expressions to replace method overriding with anonymous inner classes.
- Fixed an issue where retrieving tags for a song would cause the application to crash if the ID3 tags of the song file itself were problematic. (Thanks to @路还要走 for spotting the problem!)
- Optimized the addition and modification of translation files.
  • Loading branch information
Winnie0408 committed Oct 24, 2023
1 parent 7e84be3 commit c2c3b25
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 96 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<h3 align="right">中文 / <a href="README_ENG.md">English</a></h3>
<p align="center">
<img src="app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp" width = "150" height = "150" alt="cover" align=center />
</p>
<h2 align="center"> 欢迎使用 获取ID3标签 小工具</h2>
<h3 align="center">本工具主要用来配合 <a href="https://github.com/Winnie0408/SaltPlayerConverter">椒盐歌单转换助手</a> 使用,当然也可独立使用。</h3>
</p>

---

Expand Down Expand Up @@ -66,10 +66,13 @@

## 赞助与支持

如果这个项目对您有所帮助,您可以给我一颗⭐,或者请我喝杯咖啡!<br>
🥰🥰🥰

如果这个项目对您有所帮助,您可以给我一颗免费的⭐,或者请我喝杯咖啡!<br>
非常感谢您的支持! <br>
<a href="markdownResources/Alipay WeChatPay.jpg">
<img src="markdownResources/Sponsorship.png" width = "150" height = "90" alt="alipay.jpg" align=center />
⬇️⬇️⬇️<br>
<a href="markdownResources/Alipay WeChatPay.jpg">
<img src="markdownResources/Sponsorship.png" width = "150" height = "90" alt="Sponsorship.jpg" align=center />
</a>


7 changes: 5 additions & 2 deletions README_ENG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<h3 align="right"><a href="README.md">中文</a> / English</h3>
<p align="center">
<img src="app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp" width = "150" height = "150" alt="cover" align=center />
</p>
<h2 align="center"> Welcome to the Get ID3 Tag Tool APP</h2>
<h3 align="center">This tool is mainly used in conjunction with the <a href="https://github.com/Winnie0408/SaltPlayerConverter">SaltPlayer Playlist Converter</a>, but it can also be used independently.</h3>
</p>

---

Expand Down Expand Up @@ -65,8 +65,11 @@

## Sponsorship & Support

🥰🥰🥰

You can give me a ⭐ or buy me a cup of coffee if my project is helpful to you!<br>
Thank you very much for your support!<br>
<a href="markdownResources/Alipay WeChatPay.jpg">
⬇️⬇️⬇️<br>
<a href="markdownResources/Alipay WeChatPay.jpg">
<img src="markdownResources/Sponsorship.png" width = "150" height = "90" alt="alipay.jpg" align=center />
</a>
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
applicationId "com.hwinzniej.getmusicid3info"
minSdk 23
targetSdk 33
versionCode 1
versionName "1.0"
versionCode 2
versionName "1.0.1"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
128 changes: 40 additions & 88 deletions app/src/main/java/com/hwinzniej/getmusicid3info/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;
Expand Down Expand Up @@ -40,6 +38,7 @@
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

public class MainActivity extends AppCompatActivity {
private static final int PERMISSIONS_REQUEST_CODE = 123;
Expand All @@ -53,6 +52,7 @@ public class MainActivity extends AppCompatActivity {
ProgressDialog progressDialog;
AlertDialog.Builder alertDialog;
int userAction = 0;
Semaphore userActionSema = new Semaphore(0);
private ActivityResultLauncher<Intent> activityResultLauncher;


Expand Down Expand Up @@ -91,29 +91,22 @@ protected void onCreate(Bundle savedInstanceState) {
alertDialog.setTitle(getResources().getString(R.string.fileConflictAlertDialogTitle));
alertDialog.setMessage(getResources().getString(R.string.fileConflictAlertDialogMessage).replace("#n", "\n"));
alertDialog.setCancelable(false);
alertDialog.setPositiveButton(getResources().getString(R.string.fileConflictAlertDialogOptions1), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
userAction = 1;
}
alertDialog.setPositiveButton(getResources().getString(R.string.fileConflictAlertDialogOptions1), (dialogInterface, i) -> {
userAction = 1;
userActionSema.release();
});
alertDialog.setNegativeButton(getResources().getString(R.string.fileConflictAlertDialogOptions2), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
userAction = 2;
}
alertDialog.setNegativeButton(getResources().getString(R.string.fileConflictAlertDialogOptions2), (dialogInterface, i) -> {
userAction = 2;
userActionSema.release();
});

getId3Btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progressDialog.setTitle(getResources().getString(R.string.scanSongsDialogTitle));
progressDialog.setMessage(getResources().getString(R.string.scanSongsDialogMessage));
logPrint.setText(getResources().getString(R.string.initTextView));
progressPercent = 0;
executorPool = Executors.newFixedThreadPool(core);
manageAllFiles();
}
getId3Btn.setOnClickListener(v -> {
progressDialog.setTitle(getResources().getString(R.string.scanSongsDialogTitle));
progressDialog.setMessage(getResources().getString(R.string.scanSongsDialogMessage));
logPrint.setText(getResources().getString(R.string.initTextView));
progressPercent = 0;
executorPool = Executors.newFixedThreadPool(core);
manageAllFiles();
});
}

Expand All @@ -125,19 +118,9 @@ public void onActivityResult(ActivityResult result) {
if (data != null) {
Uri uri = data.getData();
logPrint.append("\n\n" + getResources().getString(R.string.youHaveSelected) + uri.getPathSegments().get(uri.getPathSegments().size() - 1).replace("primary:", "/storage/emulated/0/") + getResources().getString(R.string.directory) + "\n");
executorService.execute(new Runnable() {
@Override
public void run() {
checkFileExists();
}
});
executorService.execute(() -> checkFileExists());
progressDialog.show();
executorService.execute(new Runnable() {
@Override
public void run() {
listFilesInTree(uri);
}
});
executorService.execute(() -> listFilesInTree(uri));
}
}
}
Expand All @@ -150,37 +133,24 @@ private void checkFileExists() {
file.createNewFile();
} else {
if (progressPercent == 0) {
runOnUiThread(new Runnable() {
@Override
public void run() {
alertDialog.show();
}
});
runOnUiThread(() -> alertDialog.show());

while (true) {
try {
userActionSema.acquire();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}

if (userAction == 1) {
runOnUiThread(new Runnable() {
@Override
public void run() {
logPrint.append("\n" + getResources().getString(R.string.appendLogPring) + "\n");
}
});
runOnUiThread(() -> logPrint.append("\n" + getResources().getString(R.string.appendLogPring) + "\n"));
break;
}
if (userAction == 2) {
new FileWriter(file, false).close();
runOnUiThread(new Runnable() {
@Override
public void run() {
logPrint.append("\n" + getResources().getString(R.string.overwriteLogPrint) + "\n");
}
});
runOnUiThread(() -> logPrint.append("\n" + getResources().getString(R.string.overwriteLogPrint) + "\n"));
break;
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
Expand Down Expand Up @@ -229,21 +199,13 @@ private void listFilesInTree(Uri treeUri) {
DocumentFile root = DocumentFile.fromTreeUri(this, treeUri);
musicCounter(root);
if (max == 0) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, getResources().getString(R.string.toastThisDirectoryDoNotHaveMusicFile), Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
runOnUiThread(() -> {
Toast.makeText(MainActivity.this, getResources().getString(R.string.toastThisDirectoryDoNotHaveMusicFile), Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
});
return;
}
runOnUiThread(new Runnable() {
@Override
public void run() {
progressDialog.setTitle(getResources().getString(R.string.exportSongsDialogTitle));
}
});
runOnUiThread(() -> progressDialog.setTitle(getResources().getString(R.string.exportSongsDialogTitle)));
listFilesInTree(root);
}

Expand Down Expand Up @@ -282,12 +244,7 @@ private void listFilesInTree(DocumentFile root) {
ReadOnlyFileException | InvalidAudioFrameException e) {
continue;
}
executorPool.execute(new Runnable() {
@Override
public void run() {
handleFile(file.getUri().getPathSegments().get(file.getUri().getPathSegments().size() - 1).replace("primary:", "/storage/emulated/0/"));
}
});
executorPool.execute(() -> handleFile(file.getUri().getPathSegments().get(file.getUri().getPathSegments().size() - 1).replace("primary:", "/storage/emulated/0/")));
}
}
}
Expand All @@ -307,13 +264,10 @@ private void handleFile(String dfile) {
writeToFile(tag, dfile);

if (progressPercent == max) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, getResources().getString(R.string.toastExportComplete1) + progressPercent + getResources().getString(R.string.toastExportComplete2).replace("。", ""), Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
logPrint.append("\n" + getResources().getString(R.string.toastExportComplete1) + progressPercent + getResources().getString(R.string.toastExportComplete2) + "\n" + getResources().getString(R.string.exportCompleteLogPrint));
}
runOnUiThread(() -> {
Toast.makeText(MainActivity.this, getResources().getString(R.string.toastExportComplete1) + progressPercent + getResources().getString(R.string.toastExportComplete2).replace("。", ""), Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
logPrint.append("\n" + getResources().getString(R.string.toastExportComplete1) + progressPercent + getResources().getString(R.string.toastExportComplete2) + "\n" + getResources().getString(R.string.exportCompleteLogPrint));
});
executorPool.shutdown();
max = 0;
Expand All @@ -331,15 +285,13 @@ private synchronized void writeToFile(Tag tag, String dfile) {
} catch (IOException e) {
e.printStackTrace();
return;
} catch (Exception e) {
logPrint.append("\n" + dfile + getResources().getString(R.string.failReadTag) + e + "\n");
return;
}

increment();
runOnUiThread(new Runnable() {
@Override
public void run() {
progressDialog.setMessage(getResources().getString(R.string.progress) + progressPercent + " / " + max);
}
});
runOnUiThread(() -> progressDialog.setMessage(getResources().getString(R.string.progress) + progressPercent + " / " + max));
}

private synchronized void increment() {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@
<string name="appendLogPring">本次导出将追加到原有的“本地音乐导出.txt”文件。</string>
<string name="overwriteLogPrint">已删除旧的“本地音乐导出.txt”文件。</string>
<string name="progress">进度:</string>
<string name="failReadTag">" 获取标签失败。错误信息:"</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@
<string name="appendLogPring">This export will be added to the original \"Local Music Export.txt\" file.</string>
<string name="overwriteLogPrint">The old \"Local Music Export.txt\" file has been deleted.</string>
<string name="progress">"Progress: "</string>
<string name="failReadTag">" Get ID3 Tag failed. Error info:"</string>
</resources>

0 comments on commit c2c3b25

Please sign in to comment.