diff --git a/app/build.gradle b/app/build.gradle index a55e3a4..70f374e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -19,7 +19,7 @@ android { minSdkVersion 23 targetSdkVersion 32 versionCode 1 - versionName "1.1" + versionName "1.2" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } @@ -44,12 +44,12 @@ android { } dependencies { - implementation 'com.google.android.material:material:1.6.0' + implementation 'com.google.android.material:material:1.6.1' implementation 'androidx.coordinatorlayout:coordinatorlayout:1.2.0' implementation fileTree(dir: "libs", include: ["*.aar"]) - implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" - implementation 'androidx.appcompat:appcompat:1.4.1' - implementation 'androidx.constraintlayout:constraintlayout:2.1.3' + implementation "org.jetbrains.kotlin:kotlin-stdlib:1.6.21" + implementation 'androidx.appcompat:appcompat:1.4.2' + implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'androidx.work:work-runtime:2.7.1' implementation 'androidx.recyclerview:recyclerview:1.2.1' testImplementation 'junit:junit:4.13.2' @@ -62,7 +62,7 @@ dependencies { implementation 'com.alibaba:fastjson:1.2.78' //noinspection GradleCompatible - implementation 'com.android.support:support-media-compat:28.0.0' + implementation "androidx.media:media:1.6.0" implementation 'androidx.palette:palette-ktx:1.0.0' implementation 'com.github.zrunker:ZTextView:v1.0.2' @@ -73,8 +73,8 @@ dependencies { implementation 'com.nostra13.universalimageloader:universal-image-loader:1.9.5' - implementation "androidx.room:room-runtime:2.2.6" - annotationProcessor "androidx.room:room-compiler:2.2.6" + implementation "androidx.room:room-runtime:2.4.3" + annotationProcessor "androidx.room:room-compiler:2.4.3" implementation("com.squareup.okhttp3:okhttp:4.9.3") diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 2bc61ce..08c6baf 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -13,6 +13,7 @@ android:supportsRtl="true" android:theme="@style/AppTheme" android:usesCleartextTraffic="true"> + { - if (width == -1 && height == -1) { - networkInterface.httpGetData(img, 0); - } else { - networkInterface.httpGetData(ThumbnailUtils.extractThumbnail(img, width, height), width + height); - } - }); + return bitmap; - } - - @Override - public void onError(Exception e) { - super.onError(e); - isDownloadImg = false; - Bitmap img = BitmapFactory.decodeResource(MyApplication.application.getResources(), R.mipmap.ic_launcher); - AppData.handler.post(() -> networkInterface.httpGetData(img, 0)); - } - }); - if (width == -1 && height == -1) { - return img; - } - return ThumbnailUtils.extractThumbnail(img, width, height); } diff --git a/app/src/main/java/com/yutou/nas_music_player/Interfaces/DownloadInterface.java b/app/src/main/java/com/yutou/nas_music_player/Interfaces/DownloadInterface.java index f914fe5..6dffbfc 100644 --- a/app/src/main/java/com/yutou/nas_music_player/Interfaces/DownloadInterface.java +++ b/app/src/main/java/com/yutou/nas_music_player/Interfaces/DownloadInterface.java @@ -11,5 +11,5 @@ public abstract class DownloadInterface { * @param oldJar */ public abstract void onDownloadOver(File oldJar); - public void onError(Exception e){}; + public void onError(Exception e){} } diff --git a/app/src/main/java/com/yutou/nas_music_player/MyApplication.java b/app/src/main/java/com/yutou/nas_music_player/MyApplication.java index 1923e72..cf6dc38 100644 --- a/app/src/main/java/com/yutou/nas_music_player/MyApplication.java +++ b/app/src/main/java/com/yutou/nas_music_player/MyApplication.java @@ -2,16 +2,34 @@ package com.yutou.nas_music_player; import android.app.Activity; import android.app.Application; +import android.content.res.AssetManager; +import android.os.Build; import android.os.Bundle; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import com.yutou.nas_music_player.views.OpenActivity; + import java.lang.ref.WeakReference; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.Field; +import java.util.Arrays; + +import dalvik.system.BaseDexClassLoader; +import dalvik.system.DexClassLoader; +import dalvik.system.DexFile; + + public class MyApplication extends Application { public static Application application; - private static WeakReference activityWeakReference=null; + public static DexClassLoader loader; + private static WeakReference activityWeakReference = null; public static Activity getActivity() { return activityWeakReference.get(); @@ -24,7 +42,7 @@ public class MyApplication extends Application { registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() { @Override public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) { - activityWeakReference=new WeakReference<>(activity); + activityWeakReference = new WeakReference<>(activity); } @Override @@ -57,5 +75,178 @@ public class MyApplication extends Application { } }); + // dexLoader(); + System.err.println("加载版本111:" + OpenActivity.version); + } + + private void dexLoader() { + File path = new File("/data/data/" + getPackageName() + "/plugs"); + if (!path.exists()) { + path.mkdirs(); + } + File jar = new File("/data/data/" + getPackageName() + "/plugs/classes.jar"); + System.err.println("jar = " + jar.getAbsolutePath()); + if (!jar.exists()) { + System.out.println("进这里了"); + saveAdJar("classes.jar"); + return; + } + try { + Object[] t1 = getDexElements(getClassLoader()); //获取系统类加载器的dexElements数组 + loader = new DexClassLoader(jar.getAbsolutePath(), "/data/data/" + getPackageName() + "/plugs/", null, getClassLoader()); + Object version = loader.loadClass("com.yutou.nas_music_player.views.OpenActivity").getField("version").get(null); + System.out.println("loader version = " + version); + Object[] t2 = getDexElements(loader); //获取jar包类加载器的dexElements数组 + saveLibPath(t1, t2); //合并两个数组,并将合并后的数组覆盖到系统类加载器 + removeLib(loader); //删掉jar包加载器的数组 + System.out.println("加载完成"); + getDexElements(getClassLoader()); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * 合并两个Elements数组并覆盖掉系统加载类的dexElements + * + * @param t1 合成的Elements + * @param t2 被合并的Elements + * @throws Exception 未知异常 + */ + private void saveLibPath(Object[] t1, Object[] t2) throws Exception { + //遍历BaseDexClassLoader,拿到pathList变量 源码参考: 46行 + //遍历BaseDexClassLoader源码:https://android.googlesource.com/platform/libcore/+/refs/heads/master/dalvik/src/main/java/dalvik/system/BaseDexClassLoader.java + //DexPathList类的源码: https://android.googlesource.com/platform/libcore/+/refs/heads/master/dalvik/src/main/java/dalvik/system/DexPathList.java + Field[] fields = BaseDexClassLoader.class.getDeclaredFields(); + Field pathListField = null; + for (Field field : fields) { + System.out.println("field.getName() = " + field.getName()); + if (field.getName().equals("pathList")) { + pathListField = field; + } + } + if (pathListField == null) + return; + pathListField.setAccessible(true); + Object pathList = pathListField.get(getClassLoader()); + Field dexElements = pathList.getClass().getDeclaredField("dexElements"); //获取到系统类加载器的dexElements + dexElements.setAccessible(true); + //合并两个获取到系统类加载器的dexElements + Object[] obj = Arrays.copyOf(t1, t1.length + t2.length); + int index = 0; + /* for (int i = 0; i < t2.length; i++) { + obj[t1.length+i]=obj[index]; + obj[index++]=t2[i]; + }*/ + /* for (int i=t1.length,j=0;i callbacks = new ArrayList<>(); + private final List callbacks = new ArrayList<>(); public void regPlayListener(MediaControllerCompat.Callback playListener) { callbacks.add(playListener); } + public void unPlayListener(MediaControllerCompat.Callback playListener) { + callbacks.remove(playListener); + } + public void play(MusicData item) { MusicContainer.getInstance().setNowPlayData(item); getTransportControls().play(); @@ -85,17 +98,13 @@ public class MediaBrowserHelper { @Override public void onConnected() { super.onConnected(); - try { - System.out.println("初始化:onConnected"); - controllerCompat = new MediaControllerCompat(context, mMediaBrowser.getSessionToken()); - controllerCompat.registerCallback(controllerCallback); - controllerCallback.onMetadataChanged(controllerCompat.getMetadata()); - controllerCallback.onPlaybackStateChanged(controllerCompat.getPlaybackState()); - mMediaBrowser.subscribe(mMediaBrowser.getRoot(), subscriptionCallback); + System.out.println("初始化:onConnected"); + controllerCompat = new MediaControllerCompat(context, mMediaBrowser.getSessionToken()); + controllerCompat.registerCallback(controllerCallback); + controllerCallback.onMetadataChanged(controllerCompat.getMetadata()); + controllerCallback.onPlaybackStateChanged(controllerCompat.getPlaybackState()); + mMediaBrowser.subscribe(mMediaBrowser.getRoot(), subscriptionCallback); - } catch (RemoteException e) { - e.printStackTrace(); - } } } diff --git a/app/src/main/java/com/yutou/nas_music_player/containers/MediaNotificationManager.java b/app/src/main/java/com/yutou/nas_music_player/containers/MediaNotificationManager.java index f5ae895..2f30ba9 100644 --- a/app/src/main/java/com/yutou/nas_music_player/containers/MediaNotificationManager.java +++ b/app/src/main/java/com/yutou/nas_music_player/containers/MediaNotificationManager.java @@ -12,12 +12,10 @@ import android.support.v4.media.MediaDescriptionCompat; import android.support.v4.media.MediaMetadataCompat; import android.support.v4.media.session.MediaSessionCompat; import android.support.v4.media.session.PlaybackStateCompat; -import android.util.Log; import androidx.annotation.NonNull; import androidx.annotation.RequiresApi; import androidx.core.app.NotificationCompat; -import androidx.core.graphics.drawable.IconCompat; import androidx.media.session.MediaButtonReceiver; import com.yutou.nas_music_player.MyApplication; @@ -29,22 +27,24 @@ public class MediaNotificationManager { public static MediaNotificationManager manager; public static final int NOTIFICATION_ID = 412; private static final String CHANNEL_ID = MyApplication.application.getPackageName() + ".music_channel"; - private MusicService mService; - private NotificationCompat.Action mPlayAction; - private NotificationCompat.Action mPauseAction; - private NotificationCompat.Action mNextAction; - private NotificationCompat.Action mPrevAction; - private NotificationManager mNotificationManager; + private final MusicService mService; + private final NotificationCompat.Action mPlayAction; + private final NotificationCompat.Action mPauseAction; + private final NotificationCompat.Action mNextAction; + private final NotificationCompat.Action mPrevAction; + private final NotificationManager mNotificationManager; - public static MediaNotificationManager getInstance(){ + public static MediaNotificationManager getInstance() { return manager; } - public MusicService getService(){ + + public MusicService getService() { return mService; } + public MediaNotificationManager(MusicService musicService) { this.mService = musicService; - manager=this; + manager = this; mNotificationManager = (NotificationManager) mService.getSystemService(Context.NOTIFICATION_SERVICE); @@ -85,6 +85,7 @@ public class MediaNotificationManager { public NotificationManager getManager() { return mNotificationManager; } + public Notification getNotification(MediaMetadataCompat metadata, @NonNull PlaybackStateCompat state, MediaSessionCompat.Token token) { @@ -105,12 +106,12 @@ public class MediaNotificationManager { NotificationCompat.Builder builder = new NotificationCompat.Builder(mService, CHANNEL_ID); builder.setStyle(new androidx.media.app.NotificationCompat.MediaStyle() .setMediaSession(token) - .setShowActionsInCompactView(0,1,2) + .setShowActionsInCompactView(0, 1, 2) .setShowCancelButton(true) - .setCancelButtonIntent(MediaButtonReceiver.buildMediaButtonPendingIntent(mService,PlaybackStateCompat.ACTION_STOP)) + .setCancelButtonIntent(MediaButtonReceiver.buildMediaButtonPendingIntent(mService, PlaybackStateCompat.ACTION_STOP)) ); builder.setColor(Color.BLUE); - if(metadata.getBitmap(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON)!=null){ + if (metadata.getBitmap(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON) != null) { builder.setLargeIcon(metadata.getBitmap(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON)); } builder.setOngoing(true); @@ -153,6 +154,7 @@ public class MediaNotificationManager { mNotificationManager.createNotificationChannel(mChannel); } } + private PendingIntent createContentIntent() { Intent openUI = new Intent(mService, MainActivity.class); openUI.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); diff --git a/app/src/main/java/com/yutou/nas_music_player/containers/MusicContainer.java b/app/src/main/java/com/yutou/nas_music_player/containers/MusicContainer.java index 71339af..5b37035 100644 --- a/app/src/main/java/com/yutou/nas_music_player/containers/MusicContainer.java +++ b/app/src/main/java/com/yutou/nas_music_player/containers/MusicContainer.java @@ -5,6 +5,7 @@ import static android.media.MediaPlayer.SEEK_PREVIOUS_SYNC; import android.content.Context; import android.content.DialogInterface; import android.media.MediaPlayer; +import android.net.Uri; import android.os.Build; import android.os.Handler; import android.os.Looper; @@ -48,19 +49,20 @@ public class MusicContainer { public static final int PLAY_MODEL_ONE_LOOP = 2;//单曲循环 public static final int PLAY_MODEL_LIST_RANDOM = 3;//列表随机 private static MusicContainer container; - private MusicLibs libs; - private MediaPlayer mediaPlayer; + private final MusicLibs libs; + private final MediaPlayer mediaPlayer; private int playIndex = -1; private MusicData nowPlayData = null; private MusicLibsInitInterface initInterface; private List completionListener; - private List browserHelpers; - private List playList, tmpList; - private LinkedHashMap> albumMapList; - private LinkedHashMap> artistMapList; - private LinkedHashMap> collectionMapList; + private final List browserHelpers; + private List playList; + private final List tmpList; + private final LinkedHashMap> albumMapList; + private final LinkedHashMap> artistMapList; + private final LinkedHashMap> collectionMapList; private int playModel = PLAY_MODEL_RANDOM; - private PreviousPlayerList previousPlayerList; + private final PreviousPlayerList previousPlayerList; private MusicContainer(MusicLibsInitInterface initInterface) { this.initInterface = initInterface; @@ -245,7 +247,7 @@ public class MusicContainer { mediaPlayer.stop(); } mediaPlayer.reset(); - mediaPlayer.setDataSource(url); + mediaPlayer.setDataSource(MyApplication.application, Uri.parse(url)); mediaPlayer.prepare(); mediaPlayer.start(); if (playTimer == null) { @@ -265,7 +267,7 @@ public class MusicContainer { ConfigTools.getPreferences().edit().putString(ConfigTools.previous_music, JSONObject.toJSONString(tmp)).apply(); MediaNotificationManager manager = MediaNotificationManager.getInstance(); if (manager != null) { - manager.getService().updateNotification(); + // manager.getService().updateNotification(); } tmp = null; } catch (IOException e) { @@ -326,8 +328,8 @@ public class MusicContainer { builder.putLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER, Long.parseLong(data.getTrack())); builder.putLong(MediaMetadataCompat.METADATA_KEY_DISC_NUMBER, Long.parseLong(data.getDisc_no())); builder.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, ((int) (data.getDurationAsDouble() * 1000))); - if (data.getImg(AppData.minImageWidth, AppData.minImageHigh) != null) { - builder.putBitmap(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON, data.getImg(AppData.minImageWidth, AppData.minImageHigh)); + if (data.getImg() != null) { + builder.putBitmap(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON, data.getImg()); } builder.putString("md5", data.getMd5()); metadataCompat = builder.build(); @@ -344,6 +346,10 @@ public class MusicContainer { isPause = true; mediaPlayer.pause(); } + public void play(){ + isPause=false; + mediaPlayer.start(); + } public void receiverPause() { for (MediaBrowserHelper helper : browserHelpers) { @@ -508,6 +514,11 @@ public class MusicContainer { this.playIndex = i; } + public void clearPlayList() { + playList.clear(); + tmpList.clear(); + } + public class MusicLibs { private MusicLibs() { @@ -531,7 +542,7 @@ public class MusicContainer { AlertDialog dialog = new AlertDialog.Builder(context) .setTitle("添加到收藏夹") - .setItems((CharSequence[]) titles.toArray(new CharSequence[list.size()]), new DialogInterface.OnClickListener() { + .setItems(titles.toArray(new CharSequence[list.size()]), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (which == 0) {//添加新收藏夹 diff --git a/app/src/main/java/com/yutou/nas_music_player/network/HttpManager.java b/app/src/main/java/com/yutou/nas_music_player/network/HttpManager.java index bd2a859..14d08a0 100644 --- a/app/src/main/java/com/yutou/nas_music_player/network/HttpManager.java +++ b/app/src/main/java/com/yutou/nas_music_player/network/HttpManager.java @@ -8,7 +8,6 @@ import androidx.annotation.NonNull; import com.alibaba.fastjson.JSONException; import com.alibaba.fastjson.JSONObject; -import com.bumptech.glide.load.HttpException; import com.yutou.nas_music_player.Interfaces.DownloadInterface; import com.yutou.nas_music_player.Interfaces.NetworkInterface; import com.yutou.nas_music_player.MyApplication; @@ -24,13 +23,8 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.net.HttpURLConnection; -import java.net.URL; -import java.time.Duration; -import java.time.temporal.TemporalUnit; import java.util.ArrayList; import java.util.List; -import java.util.Objects; import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.TimeUnit; @@ -39,7 +33,6 @@ import okhttp3.Call; import okhttp3.Callback; import okhttp3.FormBody; import okhttp3.MediaType; -import okhttp3.OkHttp; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; @@ -50,7 +43,7 @@ public class HttpManager { public static String downloadPath = AppData.dir + AppData.download_dir + File.separator; public static class NetworkAPI { - public static final String HTTP_KEY = "zIrsh9TUZP2lfRW753PannG49E7VJvor"; + public static final String HTTP_KEY = "OHreoKn2CSlDipWV6phEupAqPIX2LJFl"; public static String HOME = ""; public static String MUSIC_LIST = HOME + "/music/list.do"; public static String MUSIC_Album = HOME + "/music/getAlbum.do"; @@ -250,7 +243,7 @@ public class HttpManager { }); } - private List downloadImageList = new ArrayList<>(); + private final List downloadImageList = new ArrayList<>(); public synchronized void downloadImage(String url, JSONObject body, String saveName, String method, DownloadInterface downloadInterface) { if(saveName.contains("/")){ @@ -296,7 +289,7 @@ public class HttpManager { Log.i("donwloadImage", "下载文件:" + url + "?" + body + " 保存文件:" + saveName); Log.i("downloadImageCode", response.code() + ""); if (response.code() != 200) { - downloadInterface.onError(new HttpException("http error = " + response.code())); + downloadInterface.onError(new Exception("http error = " + response.code())); return; } diff --git a/app/src/main/java/com/yutou/nas_music_player/services/MusicService.java b/app/src/main/java/com/yutou/nas_music_player/services/MusicService.java index 096cdda..3b734d5 100644 --- a/app/src/main/java/com/yutou/nas_music_player/services/MusicService.java +++ b/app/src/main/java/com/yutou/nas_music_player/services/MusicService.java @@ -28,12 +28,13 @@ import java.util.ArrayList; import java.util.List; public class MusicService extends MediaBrowserServiceCompat { - private final static String TAG="MusicService"; + private final static String TAG = "MusicService"; public MediaSessionCompat mediaSession; private PlaybackStateCompat.Builder stateBuilder; private MediaNotificationManager notificationManager; private int mState = PlaybackStateCompat.STATE_PLAYING; - private String playMd5=""; + private String playMd5 = ""; + private MediaBrowserHelper helper; private static final long MEDIA_SESSION_ACTIONS = PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE @@ -43,6 +44,9 @@ public class MusicService extends MediaBrowserServiceCompat { | PlaybackStateCompat.ACTION_STOP | PlaybackStateCompat.ACTION_SEEK_TO; + MusicReceiver receiver = new MusicReceiver(); + IntentFilter intentFilter = new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY); + @Override public void onCreate() { super.onCreate(); @@ -57,25 +61,26 @@ public class MusicService extends MediaBrowserServiceCompat { mediaSession.setPlaybackState(stateBuilder.build()); mediaSession.setCallback(new MediaSessionCallback()); Intent intent = new Intent(this, MainActivity.class); - PendingIntent pi = PendingIntent.getActivity(this, 233, intent, PendingIntent.FLAG_UPDATE_CURRENT); + PendingIntent pi = PendingIntent.getActivity(this, 233, intent, PendingIntent.FLAG_MUTABLE); mediaSession.setSessionActivity(pi); setSessionToken(mediaSession.getSessionToken()); MusicContainer.getInstance().addCompletionListener(new PlayCompletionListener()); - MediaBrowserHelper helper = new MediaBrowserHelper(this); + helper = MediaBrowserHelper.getInstance(); helper.regPlayListener(new MediaControllerCompat.Callback() { - private String compat_md5=""; + private String compat_md5 = ""; private MediaMetadataCompat compat; + @Override public void onMetadataChanged(MediaMetadataCompat metadata) { super.onMetadataChanged(metadata); - if(metadata==null){ + if (metadata == null) { return; } - String md5=metadata.getString("md5"); - if(compat_md5.equals(md5)){ + String md5 = metadata.getString("md5"); + if (compat_md5.equals(md5)) { return; } - compat_md5=md5; + compat_md5 = md5; Notification notification = notificationManager.getNotification(metadata, builderState(0).build(), getSessionToken()); notificationManager.getManager() .notify(MediaNotificationManager.NOTIFICATION_ID, notification); @@ -84,10 +89,10 @@ public class MusicService extends MediaBrowserServiceCompat { @Override public void onPlaybackStateChanged(PlaybackStateCompat state) { super.onPlaybackStateChanged(state); - if(mState==state.getState()){ + if (mState == state.getState()) { return; } - mState=state.getState(); + mState = state.getState(); MusicData data = MusicContainer.getInstance().getNowPlayData(); MediaMetadataCompat metadataCompat = MusicContainer.getInstance().getMetadataCompat(data); Notification notification = notificationManager.getNotification(metadataCompat, builderState(state.getPosition()).build(), getSessionToken()); @@ -95,6 +100,14 @@ public class MusicService extends MediaBrowserServiceCompat { .notify(MediaNotificationManager.NOTIFICATION_ID, notification); } }); + System.out.println("注册receiver"); + registerReceiver(receiver, intentFilter); + } + + @Override + public void onDestroy() { + super.onDestroy(); + unregisterReceiver(receiver); } @Nullable @@ -117,8 +130,8 @@ public class MusicService extends MediaBrowserServiceCompat { public void seekToNotification(long pos) { MusicData data = MusicContainer.getInstance().getNowPlayData(); - if(!playMd5.equals(data.getMd5())){ - playMd5=data.getMd5(); + if (!playMd5.equals(data.getMd5())) { + playMd5 = data.getMd5(); mediaSession.setMetadata(MusicContainer.getInstance().getNowPlayMetadataCompat()); updateNotification(); } @@ -179,15 +192,16 @@ public class MusicService extends MediaBrowserServiceCompat { } public class MediaSessionCallback extends MediaSessionCompat.Callback { - MusicReceiver receiver = new MusicReceiver(); - IntentFilter intentFilter = new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY); + + boolean isRegReceiver = false; @Override public void onPlay() { super.onPlay(); System.out.println("接收到播放指令"); - - registerReceiver(receiver, intentFilter); + if (!isRegReceiver) { + isRegReceiver = true; + } mState = PlaybackStateCompat.STATE_PLAYING; MediaMetadataCompat metadataCompat = MusicContainer.getInstance().getNowPlayMetadataCompat(); mediaSession.setMetadata(metadataCompat); @@ -200,9 +214,15 @@ public class MusicService extends MediaBrowserServiceCompat { @Override public void onPause() { super.onPause(); - mState = PlaybackStateCompat.STATE_PAUSED; - MusicContainer.getInstance().pause(); - + System.out.println("点击暂停"); + if (helper.isPlayer()) { + mState = PlaybackStateCompat.STATE_PAUSED; + MusicContainer.getInstance().pause(); + } else { + mState = PlaybackStateCompat.STATE_PLAYING; + MusicContainer.getInstance().play(); + } + updateNotification(); //MusicService.this.onSeekTo(-1); } @@ -218,6 +238,7 @@ public class MusicService extends MediaBrowserServiceCompat { @Override public void onSkipToNext() { super.onSkipToNext(); + System.out.println("播放下一曲"); MusicContainer.getInstance().playNext(); updateNotification(); } @@ -234,7 +255,7 @@ public class MusicService extends MediaBrowserServiceCompat { super.onStop(); mState = PlaybackStateCompat.STATE_STOPPED; MusicContainer.getInstance().getMediaPlayer().stop(); - unregisterReceiver(receiver); + isRegReceiver = false; } } diff --git a/app/src/main/java/com/yutou/nas_music_player/tools/AppData.java b/app/src/main/java/com/yutou/nas_music_player/tools/AppData.java index 83fece4..023745a 100644 --- a/app/src/main/java/com/yutou/nas_music_player/tools/AppData.java +++ b/app/src/main/java/com/yutou/nas_music_player/tools/AppData.java @@ -13,8 +13,8 @@ public class AppData { public static String dir= MyApplication.application.getFilesDir() + File.separator; public static boolean isDebug=true; public static Handler handler=new Handler(Looper.getMainLooper()); - public static int imageWidth=400; - public static int imageHigh=400; - public static int minImageWidth=200; - public static int minImageHigh=200; + public static int imageWidth=200; + public static int imageHigh=200; + public static int minImageWidth=80; + public static int minImageHigh=80; } diff --git a/app/src/main/java/com/yutou/nas_music_player/tools/AppTools.java b/app/src/main/java/com/yutou/nas_music_player/tools/AppTools.java index 296fc27..6966954 100644 --- a/app/src/main/java/com/yutou/nas_music_player/tools/AppTools.java +++ b/app/src/main/java/com/yutou/nas_music_player/tools/AppTools.java @@ -24,6 +24,7 @@ import java.security.MessageDigest; public class AppTools { private static KProgressHUD hud; + private static ImageLoader imageLoader; public static synchronized void showHud(Context context) { hideHud(); @@ -88,7 +89,7 @@ public class AppTools { } private static String bytesToHexString(byte[] src) { - StringBuilder stringBuilder = new StringBuilder(""); + StringBuilder stringBuilder = new StringBuilder(); if (src == null || src.length <= 0) { return null; } @@ -113,8 +114,11 @@ public class AppTools { } public static ImageLoader initImageLoader(Context context) { + if(imageLoader!=null){ + return imageLoader; + } - ImageLoader imageLoader = ImageLoader.getInstance(); + imageLoader = ImageLoader.getInstance(); ImageLoaderConfiguration.Builder builder = new ImageLoaderConfiguration.Builder(context); File cachePath = new File(context.getFilesDir().getAbsolutePath() + "/cache/"); if (!cachePath.exists()) { diff --git a/app/src/main/java/com/yutou/nas_music_player/tools/Log.java b/app/src/main/java/com/yutou/nas_music_player/tools/Log.java index 1e84d3e..786b645 100644 --- a/app/src/main/java/com/yutou/nas_music_player/tools/Log.java +++ b/app/src/main/java/com/yutou/nas_music_player/tools/Log.java @@ -12,7 +12,7 @@ import java.io.StringWriter; */ public class Log { public static boolean isDebug = AppData.isDebug; - private static boolean LogPrint = false; + private static final boolean LogPrint = false; public static String getErrorLog(Exception e) { StringWriter writer = new StringWriter(); @@ -29,7 +29,7 @@ public class Log { + "SDK_Version = " + Build.VERSION.SDK_INT + "\n" + "files = " + files + "Error = \n"; - return str + writer.toString(); + return str + writer; } private static void printOrInfo(String string) { @@ -68,7 +68,7 @@ public class Log { StringWriter writer = new StringWriter(); PrintWriter printWriter = new PrintWriter(writer); e.printStackTrace(printWriter); - printOrError("[" + title + "]" + writer.toString()); + printOrError("[" + title + "]" + writer); } public static void toast(final Context context, final String string) { diff --git a/app/src/main/java/com/yutou/nas_music_player/tools/NetworkTool.java b/app/src/main/java/com/yutou/nas_music_player/tools/NetworkTool.java index 19e1394..b929d0b 100644 --- a/app/src/main/java/com/yutou/nas_music_player/tools/NetworkTool.java +++ b/app/src/main/java/com/yutou/nas_music_player/tools/NetworkTool.java @@ -34,7 +34,7 @@ import static com.yutou.nas_music_player.tools.NetworkTool.NetworkAPI.HTTP_KEY; public class NetworkTool { public static class NetworkAPI { - public static final String HTTP_KEY = "zIrsh9TUZP2lfRW753PannG49E7VJvor"; + public static final String HTTP_KEY = "OHreoKn2CSlDipWV6phEupAqPIX2LJFl"; public static String HOME = ""; public static String MUSIC_LIST = HOME + "/music/list.do"; public static String MUSIC_Album = HOME + "/music/getAlbum.do"; @@ -187,7 +187,7 @@ public class NetworkTool { } - private Map map = Collections.synchronizedMap(new HashMap()); + private final Map map = Collections.synchronizedMap(new HashMap()); public void httpPost(final String url, final JSONObject body, final NetworkInterface networkInterface) { if (!url.startsWith("http:") && !StringUtil.isEmpty(HOME)) { @@ -230,7 +230,7 @@ public class NetworkTool { while ((tmp = reader.readLine()) != null) { str.append(tmp); } - Log.i(TAG, "[域名]" + url + " = [body]" + body.toString() + " -> [接收] " + str); + Log.i(TAG, "[域名]" + url + " = [body]" + body + " -> [接收] " + str); final String finalStr = str.toString(); // Log.i(TAG + "[" + url + "?" + toGetSplice(body) + "]", "body:" + str + " (" + connection.getResponseCode() + ")"); @@ -316,7 +316,7 @@ public class NetworkTool { download(url, new JSONObject(), saveName, "GET", null); } - private List downloadImageList = new ArrayList<>(); + private final List downloadImageList = new ArrayList<>(); public synchronized void downloadImage(final String url, final JSONObject body, final String saveName, String method, final DownloadInterface downloadInterface) { System.out.println(">>>>>>>>>>" + url); @@ -342,7 +342,7 @@ public class NetworkTool { HttpURLConnection connection = (HttpURLConnection) new URL(url + "?" + toGetSplice(body)).openConnection(); // Log.i(TAG,"获取到网络请求:"+connection.getResponseCode()); - if (method.toLowerCase().equals("post")) { + if (method.equalsIgnoreCase("post")) { connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setDoInput(true); @@ -414,7 +414,7 @@ public class NetworkTool { HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); // Log.i(TAG,"获取到网络请求:"+connection.getResponseCode()); - if (method.toLowerCase().equals("post")) { + if (method.equalsIgnoreCase("post")) { connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setDoInput(true); diff --git a/app/src/main/java/com/yutou/nas_music_player/views/AlbumsActivity.java b/app/src/main/java/com/yutou/nas_music_player/views/AlbumsActivity.java index e4b0ad0..62a8596 100644 --- a/app/src/main/java/com/yutou/nas_music_player/views/AlbumsActivity.java +++ b/app/src/main/java/com/yutou/nas_music_player/views/AlbumsActivity.java @@ -5,6 +5,7 @@ import android.graphics.BitmapFactory; import android.graphics.Color; import android.os.Bundle; import android.support.v4.media.session.MediaControllerCompat; +import android.view.View; import android.view.Window; import android.widget.Button; import android.widget.ImageView; @@ -18,6 +19,7 @@ import androidx.recyclerview.widget.RecyclerView; import com.bumptech.glide.Glide; import com.bumptech.glide.request.RequestOptions; import com.google.android.material.appbar.AppBarLayout; +import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener; import com.yutou.nas_music_player.Adapters.AlbumsRecyclerAdapter; import com.yutou.nas_music_player.Datas.MusicData; import com.yutou.nas_music_player.Interfaces.NetworkInterface; @@ -41,6 +43,7 @@ public class AlbumsActivity extends AppCompatActivity { private TextView breakApp; private Button playAll; private int model;//0 专辑, 1 歌手, 2 收藏 + private PlayListener playListener; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { @@ -59,20 +62,29 @@ public class AlbumsActivity extends AppCompatActivity { default: finish(); } - Bitmap image=getIntent().getParcelableExtra("image"); - if(image!=null){ - album_image.setImageBitmap(image); - Glide.with(AlbumsActivity.this).load(image) - .apply(RequestOptions.bitmapTransform(new BlurTransformation(25, 3))) - .into(image_background); - }else{ + String imageUrl = getIntent().getStringExtra("image"); + if (imageUrl != null) { + AppTools.initImageLoader(this).displayImage(imageUrl, album_image); + AppTools.initImageLoader(this) + .loadImage(imageUrl, new SimpleImageLoadingListener() { + @Override + public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { + super.onLoadingComplete(imageUri, view, loadedImage); + Glide.with(AlbumsActivity.this).load(loadedImage) + .apply(RequestOptions.bitmapTransform(new BlurTransformation(25, 3))) + .into(image_background); + } + }); + + } else { album_image.setImageResource(R.mipmap.ic_launcher); - Glide.with(AlbumsActivity.this).load(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)) + Glide.with(AlbumsActivity.this).load(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)) .apply(RequestOptions.bitmapTransform(new BlurTransformation(25, 3))) .into(image_background); } - browserHelper = new MediaBrowserHelper(this); - browserHelper.regPlayListener(new PlayListener()); + playListener = new PlayListener(); + browserHelper = MediaBrowserHelper.getInstance(); + browserHelper.regPlayListener(playListener); adapter.setPlayListener(browserHelper); listView.setBackgroundColor(getIntent().getIntExtra("color", Color.parseColor("#bbbbbb"))); playAll.setBackgroundColor(getIntent().getIntExtra("color", Color.parseColor("#bbbbbb"))); @@ -93,7 +105,7 @@ public class AlbumsActivity extends AppCompatActivity { adapter.notifyDataSetChanged(); if (list.isEmpty()) return; - list.get(0).getImg(AppData.imageWidth,AppData.imageHigh, new NetworkInterface() { + list.get(0).getImg(AppData.imageWidth, AppData.imageHigh, new NetworkInterface() { @Override public void httpGetData(Object data, int state) { album_image.setImageBitmap((Bitmap) data); @@ -142,7 +154,7 @@ public class AlbumsActivity extends AppCompatActivity { listView = findViewById(R.id.album_list); breakApp = findViewById(R.id.breakApp); appBarLayout = findViewById(R.id.appbar); - playAll=findViewById(R.id.playAll); + playAll = findViewById(R.id.playAll); listView.setLayoutManager(new LinearLayoutManager(this)); adapter = new AlbumsRecyclerAdapter(AlbumsActivity.this, new ArrayList<>(), model); listView.setAdapter(adapter); @@ -164,6 +176,6 @@ public class AlbumsActivity extends AppCompatActivity { protected void onDestroy() { super.onDestroy(); adapter.onStop(); - browserHelper.onStop(); + browserHelper.unPlayListener(playListener); } } diff --git a/app/src/main/java/com/yutou/nas_music_player/views/Fragments/MusicLibsFragment.java b/app/src/main/java/com/yutou/nas_music_player/views/Fragments/MusicLibsFragment.java index 7e9b20c..1729198 100644 --- a/app/src/main/java/com/yutou/nas_music_player/views/Fragments/MusicLibsFragment.java +++ b/app/src/main/java/com/yutou/nas_music_player/views/Fragments/MusicLibsFragment.java @@ -20,6 +20,7 @@ import androidx.core.util.Pair; import androidx.fragment.app.Fragment; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; +import androidx.recyclerview.widget.SimpleItemAnimator; import com.yutou.nas_music_player.Adapters.MusicLibsAdapter; import com.yutou.nas_music_player.Datas.MusicData; @@ -94,9 +95,15 @@ public class MusicLibsFragment extends Fragment { getContext().startActivity(intent, bundle); }); adapter.notifyDataSetChanged(); - if(MusicContainer.getInstance().getNowPlayData()==null){ - return; - } + closeDefaultAnimator(); + } + /* * 关闭默认局部刷新动画 ,this代表recycleview*/ + public void closeDefaultAnimator() { + listView.getItemAnimator().setAddDuration(0); + listView.getItemAnimator().setChangeDuration(0); + listView.getItemAnimator().setMoveDuration(0); + listView.getItemAnimator().setRemoveDuration(0); + ((SimpleItemAnimator) listView.getItemAnimator()).setSupportsChangeAnimations(false); } public MusicLibsAdapter getAdapter() { @@ -122,7 +129,7 @@ public class MusicLibsFragment extends Fragment { } List list = new ArrayList<>(); - private String musicPath = "root"; + private final String musicPath = "root"; public void initData() { AppTools.showHud(context); diff --git a/app/src/main/java/com/yutou/nas_music_player/views/MainActivity.java b/app/src/main/java/com/yutou/nas_music_player/views/MainActivity.java index 5d01825..f20856d 100644 --- a/app/src/main/java/com/yutou/nas_music_player/views/MainActivity.java +++ b/app/src/main/java/com/yutou/nas_music_player/views/MainActivity.java @@ -15,8 +15,10 @@ import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; +import android.widget.Button; import android.widget.ImageButton; import android.widget.ImageView; +import android.widget.LinearLayout; import android.widget.SeekBar; import android.widget.TextView; import android.widget.Toast; @@ -33,6 +35,7 @@ import com.bumptech.glide.Glide; import com.bumptech.glide.request.RequestOptions; import com.yutou.nas_music_player.Adapters.MusicLibsAdapter; import com.yutou.nas_music_player.Datas.MusicData; +import com.yutou.nas_music_player.Interfaces.DefaultNetworkInterface; import com.yutou.nas_music_player.R; import com.yutou.nas_music_player.containers.MediaBrowserHelper; import com.yutou.nas_music_player.containers.MusicContainer; @@ -58,6 +61,7 @@ public class MainActivity extends AppCompatActivity { private TextView positionTime, durationTime, bitRate, playIndex; private SeekBar seekBar; private Handler handler; + private PlayListener playListener; private GestureDetectorCompat touch; @@ -108,9 +112,9 @@ public class MainActivity extends AppCompatActivity { } setPlayModelButton(model + 1, true); }); - - browserHelper = new MediaBrowserHelper(this); - browserHelper.regPlayListener(new PlayListener()); + playListener=new PlayListener(); + browserHelper = MediaBrowserHelper.getInstance(); + browserHelper.regPlayListener(playListener); initData(); } @@ -193,14 +197,28 @@ public class MainActivity extends AppCompatActivity { private void setImage(MusicData data) { // setBarColor(data.getImageColor()); - Glide.with(this).load(data.getImg(AppData.imageWidth, AppData.imageHigh)) - .placeholder(album_image.getDrawable()) - .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(25, 3))) - .into(album_image); - Glide.with(this).load(data.getImg(AppData.minImageWidth, AppData.minImageHigh)) - .placeholder(background_image.getDrawable()) - .apply(RequestOptions.bitmapTransform(new BlurTransformation(25, 3))) - .into(background_image); + data.getImg(AppData.imageWidth, AppData.imageHigh,new DefaultNetworkInterface(){ + @Override + public void httpGetData(Object bitmap, int state) { + super.httpGetData(data, state); + Glide.with(MainActivity.this).load(bitmap) + .placeholder(album_image.getDrawable()) + .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(25, 3))) + .into(album_image); + Glide.with(MainActivity.this).load(bitmap) + .placeholder(background_image.getDrawable()) + .apply(RequestOptions.bitmapTransform(new BlurTransformation(25, 3))) + .into(background_image); + } + }); + /* data.getImg(AppData.minImageWidth, AppData.minImageHigh,new DefaultNetworkInterface(){ + @Override + public void httpGetData(Object bitmap, int state) { + super.httpGetData(data, state); + + } + });*/ + } private void initView() { @@ -232,6 +250,7 @@ public class MainActivity extends AppCompatActivity { protected void onDestroy() { super.onDestroy(); if (browserHelper != null) { + browserHelper.unPlayListener(playListener); browserHelper.onStop(); browserHelper = null; } @@ -266,13 +285,13 @@ public class MainActivity extends AppCompatActivity { } private void toPlayList() { - Pair pImage = Pair.create((View) album_image, "main_album_image"); - Pair pPrevious = Pair.create((View) previous, "previous"); - Pair pPlay = Pair.create((View) play, "play"); - Pair pNext = Pair.create((View) next, "next"); - Pair pTitle = Pair.create((View) title, "title"); - Pair pArtist = Pair.create((View) artist, "artist"); - Pair pBar = Pair.create((View) seekBar, "bar"); + Pair pImage = Pair.create(album_image, "main_album_image"); + Pair pPrevious = Pair.create(previous, "previous"); + Pair pPlay = Pair.create(play, "play"); + Pair pNext = Pair.create(next, "next"); + Pair pTitle = Pair.create(title, "title"); + Pair pArtist = Pair.create(artist, "artist"); + Pair pBar = Pair.create(seekBar, "bar"); Bundle bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(MainActivity.this, pImage, pPrevious, @@ -296,6 +315,10 @@ public class MainActivity extends AppCompatActivity { List playList = MusicContainer.getInstance().getPlayList(); POPDialog dialog = new POPDialog(this); RecyclerView recyclerView = null; + Button clear=new Button(this); + LinearLayout layout=new LinearLayout(this); + layout.setOrientation(LinearLayout.VERTICAL); + clear.setText("清空列表"); dialog.enableExitButton(true); if (playList.isEmpty()) { dialog.setTitle("播放列表"); @@ -312,7 +335,14 @@ public class MainActivity extends AppCompatActivity { // touchHelper.attachToRecyclerView(recyclerView); adapter.setData(playList); adapter.setBrowserHelper(browserHelper); - dialog.setContentView(recyclerView); + clear.setOnClickListener(view -> { + MusicContainer.getInstance().clearPlayList(); + AppTools.toast("列表已清空"); + dialog.dismiss(); + }); + layout.addView(clear); + layout.addView(recyclerView); + dialog.setContentView(layout); } dialog.show(); } diff --git a/app/src/main/java/com/yutou/nas_music_player/views/MyItemLayout.java b/app/src/main/java/com/yutou/nas_music_player/views/MyItemLayout.java new file mode 100644 index 0000000..bb223aa --- /dev/null +++ b/app/src/main/java/com/yutou/nas_music_player/views/MyItemLayout.java @@ -0,0 +1,128 @@ +package com.yutou.nas_music_player.views; + +import android.content.Context; +import android.util.AttributeSet; +import android.view.GestureDetector; +import android.view.MotionEvent; +import android.widget.LinearLayout; + +import androidx.annotation.Nullable; +import androidx.core.view.GestureDetectorCompat; + +public class MyItemLayout extends LinearLayout { + private GestureDetectorCompat touch; + private int mTotalLength; + + public MyItemLayout(Context context) { + super(context); + initTouch(context); + } + + public MyItemLayout(Context context, @Nullable AttributeSet attrs) { + super(context, attrs); + initTouch(context); + } + + private void initTouch(Context context) { + touch = new GestureDetectorCompat(context, new GestureDetector.SimpleOnGestureListener() { + + @Override + public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { + System.out.println("MyItemLayout.onFling"); + return super.onFling(e1, e2, velocityX, velocityY); + } + + @Override + public boolean onDown(MotionEvent e) { + System.out.println("MyItemLayout.onDown"); + return true; + } + + @Override + public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { + System.out.println("MyItemLayout.onScroll"); + MyItemLayout.this.setX(MyItemLayout.this.getX()+e2.getX()); + return super.onScroll(e1, e2, distanceX, distanceY); + } + /* @Override + public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { + float y = e2.getY() - e1.getY(); + System.out.println("执行动画"); + if (Math.abs(y) < 200) { + float x = e2.getX() - e1.getX(); + ObjectAnimator animator = new ObjectAnimator(); + animator.setTarget(MyItemLayout.this); + animator.setPropertyName("translationX"); + animator.setDuration(300); + if (x < 0) { + animator.setFloatValues(-(mTotalLength - getRight())); + } else { + animator.setFloatValues(0); + } + animator.start(); + return true; + } + return super.onScroll(e1, e2, distanceX, distanceY); + }*/ + }); + } + + private final boolean down = false; + private final boolean up = false; + private final boolean move = false; + + @Override + public boolean onTouchEvent(MotionEvent ev) { + boolean tmp = super.onTouchEvent(ev); + /* int action = ev.getAction(); + if (action == MotionEvent.ACTION_DOWN) { + down = true; + move = false; + up = false; + } else if (action == MotionEvent.ACTION_UP) { + down = false; + move = false; + up = true; + } else if (action == MotionEvent.ACTION_MOVE) { + down = true; + move = true; + up = false; + } + System.out.println("action = " + action + " down = " + down + " move = " + move + " up = " + up); + if (down && !up && !move) { + return false; + } else if (down && move && !up) { + System.out.println("移动啦"); + boolean clickVal = touch.onTouchEvent(ev); + return true; + } else if (down && up && !move) { + return false; + } + AppTools.toast(tmp+"");*/ + tmp=true; + if (ev.getAction() == MotionEvent.ACTION_MOVE) { + System.out.println("进触摸"); + boolean clickVal = touch.onTouchEvent(ev); + return clickVal; + } + return tmp; + } + + + @Override + protected void onLayout(boolean changed, int _left, int top, int _right, int bottom) { + //right+=300; + super.onLayout(changed, _left, top, _right, bottom); + } + + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + super.onMeasure(widthMeasureSpec, heightMeasureSpec); + + for (int i = 0; i < getChildCount(); i++) { + mTotalLength += getChildAt(i).getWidth(); + } + System.out.println("mTotalLength = " + mTotalLength); + } + +} diff --git a/app/src/main/java/com/yutou/nas_music_player/views/OpenActivity.java b/app/src/main/java/com/yutou/nas_music_player/views/OpenActivity.java index 0fa82b8..9368416 100644 --- a/app/src/main/java/com/yutou/nas_music_player/views/OpenActivity.java +++ b/app/src/main/java/com/yutou/nas_music_player/views/OpenActivity.java @@ -2,6 +2,9 @@ package com.yutou.nas_music_player.views; import android.content.Intent; import android.os.Bundle; +import android.view.View; +import android.widget.ImageButton; +import android.widget.LinearLayout; import android.widget.TextView; import androidx.annotation.Nullable; @@ -9,22 +12,55 @@ import androidx.appcompat.app.AppCompatActivity; import com.yutou.nas_music_player.R; import com.yutou.nas_music_player.containers.MusicContainer; +import com.yutou.nas_music_player.tools.AppTools; + +import cc.ibooker.ztextviewlib.MarqueeTextView; public class OpenActivity extends AppCompatActivity { TextView textView; + public static String version="1.0"; + @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setContentView(R.layout.activity_open); - textView=findViewById(R.id.textView); - MusicContainer.getInstance(new MusicContainer.MusicLibsInitInterface() { - @Override - public void init() { - Intent intent=new Intent(OpenActivity.this,MainActivity.class); - intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); - startActivity(intent); - OpenActivity.this.finish(); - } - }); + if (false) { + setContentView(R.layout.layout_music_libs_item); + System.out.println("ok"); + ImageButton a,b,c; + MarqueeTextView textView=findViewById(R.id.title); + LinearLayout layout=findViewById(R.id.layout); + a=findViewById(R.id.dislike); + b=findViewById(R.id.collection); + c=findViewById(R.id.top); + layout.setTag("layout"); + a.setTag("a"); + b.setTag("b"); + c.setTag("c"); + textView.setText(version); + a.setOnClickListener(new Click()); + b.setOnClickListener(new Click()); + c.setOnClickListener(new Click()); + layout.setOnClickListener(new Click()); + System.out.println("version = " + version); + } else { + setContentView(R.layout.activity_open); + textView = findViewById(R.id.textView); + MusicContainer.getInstance(new MusicContainer.MusicLibsInitInterface() { + @Override + public void init() { + Intent intent = new Intent(OpenActivity.this, MainActivity.class); + intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); + startActivity(intent); + OpenActivity.this.finish(); + } + }); + } + } + private static class Click implements View.OnClickListener{ + + @Override + public void onClick(View view) { + AppTools.toast("click view = "+view.getTag()); + } } } diff --git a/app/src/main/java/com/yutou/nas_music_player/views/PlayLibsActivity.java b/app/src/main/java/com/yutou/nas_music_player/views/PlayLibsActivity.java index dea8fb8..207e454 100644 --- a/app/src/main/java/com/yutou/nas_music_player/views/PlayLibsActivity.java +++ b/app/src/main/java/com/yutou/nas_music_player/views/PlayLibsActivity.java @@ -50,14 +50,14 @@ public class PlayLibsActivity extends AppCompatActivity { private TabLayout tabLayout; private ViewPager2 viewPager; - private int index = 0; + private final int index = 0; + PlayListener playListener = new PlayListener(); @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_play_list); - PlayListener playListener = new PlayListener(); - browserHelper = new MediaBrowserHelper(this); + browserHelper = MediaBrowserHelper.getInstance(); browserHelper.regPlayListener(playListener); initView(); bar_pos = getIntent().getIntExtra("bar_pos", 0); @@ -107,6 +107,7 @@ public class PlayLibsActivity extends AppCompatActivity { } List fragments; + private void initView() { play = findViewById(R.id.collection); next = findViewById(R.id.top); @@ -117,11 +118,11 @@ public class PlayLibsActivity extends AppCompatActivity { progressBar = findViewById(R.id.progressBar); tabLayout = findViewById(R.id.tabLayout); viewPager = findViewById(R.id.viewPager); - fragments=new ArrayList<>(); - fragments.add(new MusicLibsFragment(this,MusicLibsAdapter.LIBS_MODEL_ALL)); - fragments.add(new MusicLibsFragment(this,MusicLibsAdapter.LIBS_MODEL_Album)); - fragments.add(new MusicLibsFragment(this,MusicLibsAdapter.LIBS_MODEL_PLAY_Collection)); - fragments.add(new MusicLibsFragment(this,MusicLibsAdapter.LIBS_MODEL_Tmp)); + fragments = new ArrayList<>(); + fragments.add(new MusicLibsFragment(this, MusicLibsAdapter.LIBS_MODEL_ALL)); + fragments.add(new MusicLibsFragment(this, MusicLibsAdapter.LIBS_MODEL_Album)); + fragments.add(new MusicLibsFragment(this, MusicLibsAdapter.LIBS_MODEL_PLAY_Collection)); + fragments.add(new MusicLibsFragment(this, MusicLibsAdapter.LIBS_MODEL_Tmp)); for (MusicLibsFragment fragment : fragments) { fragment.setPlayContainer(browserHelper); } @@ -152,10 +153,10 @@ public class PlayLibsActivity extends AppCompatActivity { fragments.get(tab.getPosition()).update(); } }); - TabLayoutMediator mediator=new TabLayoutMediator(tabLayout, viewPager, true, new TabLayoutMediator.TabConfigurationStrategy() { + TabLayoutMediator mediator = new TabLayoutMediator(tabLayout, viewPager, true, new TabLayoutMediator.TabConfigurationStrategy() { @Override public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) { - switch (position){ + switch (position) { case 0: tab.setText("全部"); break; @@ -191,10 +192,10 @@ public class PlayLibsActivity extends AppCompatActivity { artist.setText(data.getArtist()); progressBar.setMax((int) (data.getDurationAsDouble() * 1000)); progressBar.setProgress(bar_pos); - data.getImg(AppData.minImageWidth,AppData.minImageHigh, new NetworkInterface() { + data.getImg(AppData.minImageWidth, AppData.minImageHigh, new NetworkInterface() { @Override public void httpGetData(Object data, int state) { - setImage((Bitmap) data); + setImage((Bitmap) data); } @Override @@ -202,12 +203,12 @@ public class PlayLibsActivity extends AppCompatActivity { } }); - } + } @Override protected void onDestroy() { super.onDestroy(); - browserHelper.onStop(); + browserHelper.unPlayListener(playListener); browserHelper = null; finish(); } diff --git a/build.gradle b/build.gradle index abd4a05..31fd4d7 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,5 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { - ext.kotlin_version = "1.4.10" repositories { google()