From ab0f6d1e5da31d80839d8ac7b5d857ba07a6cca2 Mon Sep 17 00:00:00 2001 From: Albresky <78648318+Albresky@users.noreply.github.com> Date: Sun, 16 Jul 2023 23:32:35 +0800 Subject: [PATCH] fix bugs; clean code --- app/build.gradle | 2 +- app/src/main/AndroidManifest.xml | 24 +- .../splayer/Adapter/VideoAdapter.java | 10 +- .../cn/albresky/splayer/MainActivity.java | 55 +-- .../cn/albresky/splayer/UI/MusicActivity.java | 8 - .../splayer/UI/MusicPlayerActivity.java | 9 - .../albresky/splayer/UI/SettingsActivity.java | 20 +- .../cn/albresky/splayer/UI/VideoActivity.java | 18 +- .../splayer/UI/VideoPlayerActivity.java | 2 +- .../cn/albresky/splayer/Utils/Converter.java | 8 +- .../albresky/splayer/Utils/SuperScanner.java | 21 +- .../albresky/splayer/Utils/VideoScanner.java | 4 +- .../main/res/layout/activity_musiclist.xml | 326 +++++++++--------- app/src/main/res/values/strings.xml | 2 +- 14 files changed, 231 insertions(+), 278 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index aa040f1..cae7461 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -12,7 +12,7 @@ android { minSdk 24 targetSdk 33 versionCode 2 - versionName "0.0.3" + versionName "0.0.4_final" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index ac8e3f2..9fb24bb 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -23,26 +23,31 @@ + android:screenOrientation="portrait" + tools:ignore="LockedOrientationActivity" /> + android:screenOrientation="portrait" + tools:ignore="LockedOrientationActivity" /> + android:screenOrientation="portrait" + tools:ignore="LockedOrientationActivity" /> + android:screenOrientation="portrait" + tools:ignore="LockedOrientationActivity" /> + android:screenOrientation="portrait" + tools:ignore="LockedOrientationActivity"> @@ -52,15 +57,18 @@ + android:screenOrientation="portrait" + tools:ignore="LockedOrientationActivity" /> + android:screenOrientation="portrait" + tools:ignore="LockedOrientationActivity" /> + android:screenOrientation="portrait" + tools:ignore="LockedOrientationActivity" /> \ No newline at end of file diff --git a/app/src/main/java/cn/albresky/splayer/Adapter/VideoAdapter.java b/app/src/main/java/cn/albresky/splayer/Adapter/VideoAdapter.java index d61aae5..a347241 100644 --- a/app/src/main/java/cn/albresky/splayer/Adapter/VideoAdapter.java +++ b/app/src/main/java/cn/albresky/splayer/Adapter/VideoAdapter.java @@ -10,7 +10,6 @@ import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; -import android.widget.Toast; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; @@ -74,6 +73,12 @@ public void onBindViewHolder(@NonNull ViewHolder holder, int position) { if (thumbnail == null) { holder.videoWarning.setVisibility(View.VISIBLE); + holder.videoThumbnail.setScaleType(ImageView.ScaleType.FIT_CENTER); + holder.videoThumbnail.setImageResource(R.drawable.baseline_video_half); + } else { + holder.videoWarning.setVisibility(View.GONE); + holder.videoThumbnail.setScaleType(ImageView.ScaleType.CENTER_CROP); + holder.videoThumbnail.setImageBitmap(thumbnail); } holder.videoIndex.setText(String.valueOf(position + 1)); holder.videoName.setText(video.getName()); @@ -82,9 +87,8 @@ public void onBindViewHolder(@NonNull ViewHolder holder, int position) { holder.videoResolution.setText(Converter.resolutionConvert(video.getWidth(), video.getHeight())); holder.videoDuration.setText(duration); holder.videoType.setText(video.getType()); - holder.videoThumbnail.setImageBitmap(thumbnail); + holder.itemView.setOnClickListener(v -> { - Toast.makeText(mContext, "点击了第" + position + "项", Toast.LENGTH_SHORT).show(); onItemClickListener.onItemClick(v, position); }); } diff --git a/app/src/main/java/cn/albresky/splayer/MainActivity.java b/app/src/main/java/cn/albresky/splayer/MainActivity.java index 67d4ea1..dd0b471 100644 --- a/app/src/main/java/cn/albresky/splayer/MainActivity.java +++ b/app/src/main/java/cn/albresky/splayer/MainActivity.java @@ -45,16 +45,12 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) { @Override protected void onCreate(Bundle savedInstanceState) { + Log.d(TAG, "onCreate() called"); super.onCreate(savedInstanceState); binding = ActivityMainBinding.inflate(getLayoutInflater()); View view = binding.getRoot(); setContentView(view); -// setSupportActionBar(binding.toolbar); - - Log.d(TAG, "onCreate() called"); - - checkPermissions(); getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR); @@ -80,8 +76,6 @@ protected void onCreate(Bundle savedInstanceState) { startActivity(intent); return true; }); - - // test zone end } private void checkPermissionsAndLaunch(@NonNull ActivityResultLauncher launcher, @NonNull Intent intent) { @@ -128,56 +122,11 @@ private void checkPermissions() { Intent intent = new Intent(getApplicationContext(), MusicActivity.class); checkPermissionsAndLaunch(launcher, intent); }); -// if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q) { -// if (permissionGranted[PERMISSION_READ_EXTERNAL] && permissionGranted[PERMISSION_WRITE_EXTERNAL]) { -// startActivity(new Intent(getApplicationContext(), MusicActivity.class)); -// } else if (ContextCompat.checkSelfPermission( -// getApplicationContext(), android.Manifest.permission.READ_EXTERNAL_STORAGE -// ) != PackageManager.PERMISSION_GRANTED) { -// requestPermissions(new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSION_READ_EXTERNAL); -// } else if (ContextCompat.checkSelfPermission( -// getApplicationContext(), android.Manifest.permission.WRITE_EXTERNAL_STORAGE -// ) != PackageManager.PERMISSION_GRANTED) { -// requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_WRITE_EXTERNAL); -// } -// } else { -// if (!Environment.isExternalStorageManager()) { -// Toast.makeText(this, "请求所有文件访问权限", Toast.LENGTH_SHORT).show(); -// Intent intent = new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION); -// launcher.launch(intent); -// } else { -// Toast.makeText(this, "已获得所有文件访问权限", Toast.LENGTH_SHORT).show(); -// startActivity(new Intent(getApplicationContext(), MusicActivity.class)); -// } -// } binding.btnVideo.setOnClickListener(v -> { Intent intent = new Intent(getApplicationContext(), VideoActivity.class); checkPermissionsAndLaunch(launcher, intent); }); - -// if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q) { -// if (permissionGranted[PERMISSION_READ_EXTERNAL] && permissionGranted[PERMISSION_WRITE_EXTERNAL]) { -// startActivity(new Intent(getApplicationContext(), VideoActivity.class)); -// } else if (ContextCompat.checkSelfPermission( -// getApplicationContext(), android.Manifest.permission.READ_EXTERNAL_STORAGE -// ) != PackageManager.PERMISSION_GRANTED) { -// requestPermissions(new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSION_READ_EXTERNAL); -// } else if (ContextCompat.checkSelfPermission( -// getApplicationContext(), android.Manifest.permission.WRITE_EXTERNAL_STORAGE -// ) != PackageManager.PERMISSION_GRANTED) { -// requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_WRITE_EXTERNAL); -// } -// } else { -// if (!Environment.isExternalStorageManager()) { -// Toast.makeText(this, "请求所有文件访问权限", Toast.LENGTH_SHORT).show(); -// Intent intent = new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION); -// launcher.launch(intent); -// } else { -// Toast.makeText(this, "已获得所有文件访问权限", Toast.LENGTH_SHORT).show(); -// startActivity(new Intent(getApplicationContext(), VideoActivity.class)); -// } -// } } @@ -204,6 +153,4 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis } } } - - } \ No newline at end of file diff --git a/app/src/main/java/cn/albresky/splayer/UI/MusicActivity.java b/app/src/main/java/cn/albresky/splayer/UI/MusicActivity.java index 4815345..2dc08cc 100644 --- a/app/src/main/java/cn/albresky/splayer/UI/MusicActivity.java +++ b/app/src/main/java/cn/albresky/splayer/UI/MusicActivity.java @@ -83,10 +83,6 @@ private void initView() { binding.btnPlay.setEnabled(false); binding.btnPlay.setOnClickListener(v -> { if (mContorller != null) { -// if (!mContorller.isPrepared()) { -// Log.d(TAG, "initView:[btn_play clicked] [isPrepared=false] => playerStart(0)"); -// playerStart(0); -// } else if (mContorller.isPlaying()) { Log.d(TAG, "initView:[btn_play clicked] [isPlaying=true] => playerPause()"); playerPause(); @@ -139,8 +135,6 @@ private void initView() { Log.d(TAG, "initView: playerSongName clicked"); startMusicPlayerActivity(mIndex); }); - - } private void getMusicList() { @@ -306,6 +300,4 @@ public void onServiceDisconnected(ComponentName name) { Log.d(TAG, "onServiceDisconnected: "); } } - - } \ No newline at end of file diff --git a/app/src/main/java/cn/albresky/splayer/UI/MusicPlayerActivity.java b/app/src/main/java/cn/albresky/splayer/UI/MusicPlayerActivity.java index 8e88bf3..f41d73e 100644 --- a/app/src/main/java/cn/albresky/splayer/UI/MusicPlayerActivity.java +++ b/app/src/main/java/cn/albresky/splayer/UI/MusicPlayerActivity.java @@ -88,8 +88,6 @@ protected void onCreate(Bundle savedInstanceState) { } private void initView() { -// binding.ivMusicCover.setImageBitmap(Converter.createBitmapWithScale(Converter.createBitmapWithNoScale(this, R.drawable.record), 512, 512, false)); - String jList = (String) getIntent().getSerializableExtra("songList"); Gson gson = new Gson(); mList = gson.fromJson(jList, new TypeToken>() { @@ -154,13 +152,6 @@ private void initView() { binding.playNext.setOnClickListener(v -> { Log.d(TAG, "onClick: next"); playNext(); -// int nextIndex = songIndex >= mList.size() - 1 ? 0 : songIndex + 1; -// songIndex = nextIndex; -// song = mList.get(songIndex); -// mContorller.prepare(songIndex); -// mContorller.play(); -// resetUI(); -// startAnimation(); }); binding.playPrev.setOnClickListener(v -> { diff --git a/app/src/main/java/cn/albresky/splayer/UI/SettingsActivity.java b/app/src/main/java/cn/albresky/splayer/UI/SettingsActivity.java index 16e663a..0927831 100644 --- a/app/src/main/java/cn/albresky/splayer/UI/SettingsActivity.java +++ b/app/src/main/java/cn/albresky/splayer/UI/SettingsActivity.java @@ -19,9 +19,7 @@ public class SettingsActivity extends AppCompatActivity { private final String TAG = "loadSettings"; private ActivitySettingsBinding binding; - private boolean enableDeepScan = false; - private int scanDepth = 4; @@ -34,12 +32,14 @@ protected void onCreate(Bundle savedInstanceState) { initView(); } + @Override protected void onDestroy() { Log.d(TAG, "onDestroy: "); super.onDestroy(); } + private void initView() { Log.d(TAG, "initView: "); @@ -54,7 +54,6 @@ private void initView() { saveSettings(); }); - binding.txtCache.setText(getTotalCacheSize()); binding.btnClearCache.setOnClickListener(v -> { @@ -79,6 +78,7 @@ private void initView() { }); } + private void loadSettings() { Log.d(TAG, "loadSettings: "); SharedPreferences sp = getSharedPreferences("settings", MODE_PRIVATE); @@ -91,6 +91,7 @@ private void loadSettings() { binding.deepScan.setChecked(sp.getBoolean("enableDeepScan", false)); } + private void saveSettings() { Log.d(TAG, "saveSettings: "); SharedPreferences sp = getSharedPreferences("settings", MODE_PRIVATE); @@ -100,6 +101,7 @@ private void saveSettings() { editor.apply(); } + public String getTotalCacheSize() { long cacheSize; try { @@ -114,6 +116,7 @@ public String getTotalCacheSize() { return getFormatSize(cacheSize); } + public String getFormatSize(long size) { long kb = size / 1024; int m = (int) (kb / 1024); @@ -121,6 +124,7 @@ public String getFormatSize(long size) { return m + "." + kbs + "MB"; } + public long getFolderSize(File file) throws Exception { long size = 0; try { @@ -139,8 +143,8 @@ public long getFolderSize(File file) throws Exception { return size; } + public void clearAllCache() { - deleteDir(this.getCacheDir()); if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { if (deleteDir(this.getExternalCacheDir())) { Log.d(TAG, "clearAllCache: "); @@ -150,19 +154,21 @@ public void clearAllCache() { } } + private boolean deleteDir(File dir) { if (dir != null && dir.isDirectory()) { String[] children = dir.list(); - for (int i = 0; i < children.length; i++) { - boolean success = deleteDir(new File(dir, children[i])); + for (String child : children) { + boolean success = deleteDir(new File(dir, child)); if (!success) { return false; } } } - return dir.delete(); + return dir != null && dir.delete(); } + @Override public void finish() { Log.d(TAG, "finish: "); diff --git a/app/src/main/java/cn/albresky/splayer/UI/VideoActivity.java b/app/src/main/java/cn/albresky/splayer/UI/VideoActivity.java index 024e9be..9aabd4a 100644 --- a/app/src/main/java/cn/albresky/splayer/UI/VideoActivity.java +++ b/app/src/main/java/cn/albresky/splayer/UI/VideoActivity.java @@ -34,7 +34,6 @@ public class VideoActivity extends AppCompatActivity implements VideoAdapter.OnI private final String TAG = "VideoActivity"; private ActivityVideoBinding binding; private boolean enableDeepScan = false; - private int scanDepth = 4; private VideoAdapter mAdapter; @@ -69,12 +68,9 @@ private void initView() { binding.layRefresh.setOnRefreshListener( () -> { Log.i(TAG, "onRefresh called from SwipeRefreshLayout"); - runOnUiThread(new Runnable() { - public void run() { - // your code to update the UI thread here - getVideoList(false); - - } + runOnUiThread(() -> { + // your code to update the UI thread here + getVideoList(false); }); } ); @@ -98,11 +94,10 @@ private void getVideoList(boolean enableCache) { mList.clear(); // try to get video list from cache if (!enableCache || !loadCache()) { -// if (true) { if (enableDeepScan) { Log.d(TAG, "getVideoList: enableDeepScan|ScanType:{mp4,mkv,webm}|ScanDepth:" + scanDepth); SuperScanner sScanner = new SuperScanner(); - sScanner.setScanType(new String[]{"mp4", "mkv", "webm"}); + sScanner.setScanType(new String[]{"mp4", "mkv", "webm", "avi", "m2ts", "flv", "mov", "wmv", "3gp", "ts", "rmvb"}); sScanner.setScanDepth(scanDepth); sScanner.startScan(); mList = sScanner.getVideoData(); @@ -146,17 +141,20 @@ private void getVideoList(boolean enableCache) { }); } + @Override public void onItemClick(View view, int position) { startVideoPlayerActivity(position); } + private void startVideoPlayerActivity(int videoIndex) { Intent intent = new Intent(this, VideoPlayerActivity.class); intent.putExtra("videoInfo", mList.get(videoIndex)); startActivity(intent); } + private boolean loadCache() { Log.d(TAG, "loadCache: load cache from shared preference"); SharedPreferences sp = getSharedPreferences("videoListCache", MODE_PRIVATE); @@ -171,6 +169,7 @@ private boolean loadCache() { } } + private boolean writeCache() { Log.d(TAG, "writeCache: write cache to shared preference"); SharedPreferences sp = getSharedPreferences("videoListCache", MODE_PRIVATE); @@ -183,5 +182,4 @@ private boolean writeCache() { editor.putString("videoList", json); return editor.commit(); } - } diff --git a/app/src/main/java/cn/albresky/splayer/UI/VideoPlayerActivity.java b/app/src/main/java/cn/albresky/splayer/UI/VideoPlayerActivity.java index e02db84..3786d56 100644 --- a/app/src/main/java/cn/albresky/splayer/UI/VideoPlayerActivity.java +++ b/app/src/main/java/cn/albresky/splayer/UI/VideoPlayerActivity.java @@ -67,7 +67,7 @@ private void initPlayer() { binding.videoType.setText(video.getType()); binding.videoMDate.setText(DatetimeUtils.formatTime(Long.parseLong(video.getDate()))); -// add MediaController + // add MediaController MediaController mediaController = new MediaController(this); mediaController.setAnchorView(binding.videoPlayer); binding.videoPlayer.setMediaController(mediaController); diff --git a/app/src/main/java/cn/albresky/splayer/Utils/Converter.java b/app/src/main/java/cn/albresky/splayer/Utils/Converter.java index b7df912..54da67d 100644 --- a/app/src/main/java/cn/albresky/splayer/Utils/Converter.java +++ b/app/src/main/java/cn/albresky/splayer/Utils/Converter.java @@ -11,9 +11,9 @@ import android.util.Log; public class Converter { - private static final String TAG = "Converter"; + public static String resolutionConvert(int w, int h) { return w + "x" + h; } @@ -132,20 +132,23 @@ public static Bitmap createBitmapWithScale(Bitmap bitmap, int targetX, int targe return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, filter); } + public static Bitmap createBitmapWithNoScale(Context context, int resId) { BitmapFactory.Options mOptions = new BitmapFactory.Options(); mOptions.inScaled = false; return BitmapFactory.decodeResource(context.getResources(), resId, mOptions); } + public static Uri getAudioAlbumImageContentUri(long albumId) { - Uri imgUri = null; + Uri imgUri; Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart"); imgUri = ContentUris.withAppendedId(sArtworkUri, albumId); Log.d(TAG, "AudioCoverImgUri = " + imgUri.toString()); return imgUri; } + public static Uri getAudioAlbumImageContentUri(Context context, String filePath) { Uri audioUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; String selection = MediaStore.Audio.Media.DATA + "=? "; @@ -172,7 +175,6 @@ public static Uri getAudioAlbumImageContentUri(Context context, String filePath) } return imgUri; } - } diff --git a/app/src/main/java/cn/albresky/splayer/Utils/SuperScanner.java b/app/src/main/java/cn/albresky/splayer/Utils/SuperScanner.java index a4dc186..003d8d7 100644 --- a/app/src/main/java/cn/albresky/splayer/Utils/SuperScanner.java +++ b/app/src/main/java/cn/albresky/splayer/Utils/SuperScanner.java @@ -39,15 +39,16 @@ public void setThreadCount(int count) { threadCount = count; } + public List getAudioData() { - while (!scanFinished) { - } + while (!scanFinished) ; if (scannedFiles == null || scannedFiles.size() == 0) { return null; } List songs = new ArrayList<>(); + MediaMetadataRetriever mmr = new MediaMetadataRetriever(); for (FileScanner.FindItem item : scannedFiles) { try { @@ -84,13 +85,17 @@ public List getAudioData() { songs.add(song); } } + try { + mmr.release(); + } catch (Exception e) { + e.printStackTrace(); + } return songs; } public List