├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── drawable
│ │ │ │ ├── normal.png
│ │ │ │ ├── checked.png
│ │ │ │ ├── music_center_bg.xml
│ │ │ │ ├── supportmusicplayer_checkbox.xml
│ │ │ │ └── ic_launcher_background.xml
│ │ │ ├── mipmap-hdpi
│ │ │ │ ├── pause.png
│ │ │ │ ├── play.png
│ │ │ │ ├── nextsong.png
│ │ │ │ ├── ic_launcher.png
│ │ │ │ ├── icon_music.png
│ │ │ │ ├── previoussong.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── values-zh
│ │ │ │ └── strings.xml
│ │ │ ├── values
│ │ │ │ ├── colors.xml
│ │ │ │ ├── strings.xml
│ │ │ │ └── styles.xml
│ │ │ ├── mipmap-anydpi-v26
│ │ │ │ ├── ic_launcher.xml
│ │ │ │ └── ic_launcher_round.xml
│ │ │ ├── layout
│ │ │ │ ├── activity_allsupportaudioapp.xml
│ │ │ │ ├── activity_remotecontroller.xml
│ │ │ │ ├── activity_testmusiccontrolview.xml
│ │ │ │ ├── item_supportmusicplayer.xml
│ │ │ │ ├── activity_main.xml
│ │ │ │ └── layout_musiccontrol.xml
│ │ │ └── drawable-v24
│ │ │ │ └── ic_launcher_foreground.xml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── baina
│ │ │ │ └── androidremotecontroller
│ │ │ │ ├── utils
│ │ │ │ ├── Constants.java
│ │ │ │ └── SharedPreferenceUtil.java
│ │ │ │ ├── RemoteControllerApplication.java
│ │ │ │ ├── activity
│ │ │ │ ├── TestMusicControlViewActivity.java
│ │ │ │ ├── MainActivity.java
│ │ │ │ ├── AllSupportAudioAppActivity.java
│ │ │ │ └── RemoteControllerActivity.java
│ │ │ │ ├── model
│ │ │ │ ├── Music.java
│ │ │ │ └── AppInfo.java
│ │ │ │ ├── adapter
│ │ │ │ └── SupportMusicPlayerAdapter.java
│ │ │ │ ├── service
│ │ │ │ └── MusicNotificationListenerService.java
│ │ │ │ └── view
│ │ │ │ ├── MusicControlView.java
│ │ │ │ └── NewMusicControlView.java
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── baina
│ │ │ └── androidremotecontroller
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── baina
│ │ └── androidremotecontroller
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── settings.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .idea
├── vcs.xml
├── modules.xml
├── runConfigurations.xml
├── gradle.xml
└── misc.xml
├── Screenshots
├── Screenshot_2018-01-07-14-23-24-904_com.example.baina.androidremotecontroller.png
├── Screenshot_2018-01-07-14-23-35-305_com.example.baina.androidremotecontroller.png
└── Screenshot_2018-01-07-14-23-41-900_com.example.baina.androidremotecontroller.png
├── gradle.properties
├── .gitignore
├── README.md
├── gradlew.bat
├── gradlew
└── LICENSE
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YoungBill/Android-RemoteController/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/drawable/normal.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YoungBill/Android-RemoteController/HEAD/app/src/main/res/drawable/normal.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/checked.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YoungBill/Android-RemoteController/HEAD/app/src/main/res/drawable/checked.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/pause.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YoungBill/Android-RemoteController/HEAD/app/src/main/res/mipmap-hdpi/pause.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/play.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YoungBill/Android-RemoteController/HEAD/app/src/main/res/mipmap-hdpi/play.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/nextsong.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YoungBill/Android-RemoteController/HEAD/app/src/main/res/mipmap-hdpi/nextsong.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YoungBill/Android-RemoteController/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/icon_music.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YoungBill/Android-RemoteController/HEAD/app/src/main/res/mipmap-hdpi/icon_music.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YoungBill/Android-RemoteController/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/previoussong.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YoungBill/Android-RemoteController/HEAD/app/src/main/res/mipmap-hdpi/previoussong.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YoungBill/Android-RemoteController/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YoungBill/Android-RemoteController/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YoungBill/Android-RemoteController/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YoungBill/Android-RemoteController/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YoungBill/Android-RemoteController/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YoungBill/Android-RemoteController/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YoungBill/Android-RemoteController/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YoungBill/Android-RemoteController/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/Screenshots/Screenshot_2018-01-07-14-23-24-904_com.example.baina.androidremotecontroller.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YoungBill/Android-RemoteController/HEAD/Screenshots/Screenshot_2018-01-07-14-23-24-904_com.example.baina.androidremotecontroller.png
--------------------------------------------------------------------------------
/Screenshots/Screenshot_2018-01-07-14-23-35-305_com.example.baina.androidremotecontroller.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YoungBill/Android-RemoteController/HEAD/Screenshots/Screenshot_2018-01-07-14-23-35-305_com.example.baina.androidremotecontroller.png
--------------------------------------------------------------------------------
/Screenshots/Screenshot_2018-01-07-14-23-41-900_com.example.baina.androidremotecontroller.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/YoungBill/Android-RemoteController/HEAD/Screenshots/Screenshot_2018-01-07-14-23-41-900_com.example.baina.androidremotecontroller.png
--------------------------------------------------------------------------------
/app/src/main/res/values-zh/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 请授予通知权限
5 | 应用未安装,启动失败
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/music_center_bg.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/java/com/baina/androidremotecontroller/utils/Constants.java:
--------------------------------------------------------------------------------
1 | package com.baina.androidremotecontroller.utils;
2 |
3 | /**
4 | * Created by baina on 18-1-3.
5 | */
6 |
7 | public class Constants {
8 | public static final String MUSICPLAYER = "musicplayer";
9 | }
10 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Jan 03 10:00:42 CST 2018
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
7 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | AndroidRemoteController
3 |
4 |
5 | Please grant notifications the use of authority
6 | Application is not installed, startup failure
7 |
8 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/supportmusicplayer_checkbox.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/test/java/com/baina/androidremotecontroller/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.baina.androidremotecontroller;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_allsupportaudioapp.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/app/src/main/java/com/baina/androidremotecontroller/RemoteControllerApplication.java:
--------------------------------------------------------------------------------
1 | package com.baina.androidremotecontroller;
2 |
3 | import android.app.Application;
4 | import android.content.Context;
5 |
6 | /**
7 | *
8 | *
9 | * Created by baina on 18-1-3.
10 | */
11 |
12 | public class RemoteControllerApplication extends Application {
13 |
14 | private static Context mContext;
15 |
16 | @Override
17 | public void onCreate() {
18 | super.onCreate();
19 | mContext = this;
20 | }
21 |
22 | public static Context getInstance() {
23 | return mContext;
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_remotecontroller.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_testmusiccontrolview.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/app/src/main/java/com/baina/androidremotecontroller/activity/TestMusicControlViewActivity.java:
--------------------------------------------------------------------------------
1 | package com.baina.androidremotecontroller.activity;
2 |
3 | import android.app.Activity;
4 | import android.os.Bundle;
5 | import android.support.annotation.Nullable;
6 |
7 | import com.baina.androidremotecontroller.R;
8 |
9 | /**
10 | * 测试高度集成音乐控制view的Activity
11 | * Created by baina on 18-1-5.
12 | */
13 |
14 | public class TestMusicControlViewActivity extends Activity {
15 |
16 | @Override
17 | protected void onCreate(@Nullable Bundle savedInstanceState) {
18 | super.onCreate(savedInstanceState);
19 | setContentView(R.layout.activity_testmusiccontrolview);
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/app/src/main/java/com/baina/androidremotecontroller/model/Music.java:
--------------------------------------------------------------------------------
1 | package com.baina.androidremotecontroller.model;
2 |
3 | import android.graphics.Bitmap;
4 |
5 | /**
6 | * Created by baina on 18-1-3.
7 | */
8 |
9 | public class Music {
10 |
11 | private String artist;
12 | private String title;
13 | private Bitmap cover;
14 |
15 | public String getArtist() {
16 | return artist;
17 | }
18 |
19 | public void setArtist(String artist) {
20 | this.artist = artist;
21 | }
22 |
23 | public String getTitle() {
24 | return title;
25 | }
26 |
27 | public void setTitle(String title) {
28 | this.title = title;
29 | }
30 |
31 | public Bitmap getCover() {
32 | return cover;
33 | }
34 |
35 | public void setCover(Bitmap cover) {
36 | this.cover = cover;
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/app/src/main/java/com/baina/androidremotecontroller/model/AppInfo.java:
--------------------------------------------------------------------------------
1 | package com.baina.androidremotecontroller.model;
2 |
3 | import android.graphics.drawable.Drawable;
4 |
5 | /**
6 | * Created by baina on 18-1-2.
7 | */
8 |
9 | public class AppInfo {
10 | private String appLabel;
11 | private String appPkg;
12 | private Drawable appIcon;
13 |
14 | public String getAppLabel() {
15 | return appLabel;
16 | }
17 |
18 | public void setAppLabel(String appLabel) {
19 | this.appLabel = appLabel;
20 | }
21 |
22 | public String getAppPkg() {
23 | return appPkg;
24 | }
25 |
26 | public void setAppPkg(String appPkg) {
27 | this.appPkg = appPkg;
28 | }
29 |
30 | public Drawable getAppIcon() {
31 | return appIcon;
32 | }
33 |
34 | public void setAppIcon(Drawable appIcon) {
35 | this.appIcon = appIcon;
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/baina/androidremotecontroller/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.baina.androidremotecontroller;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumented test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.baina.androidremotecontroller", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Built application files
2 | *.apk
3 | *.ap_
4 |
5 | # Files for the ART/Dalvik VM
6 | *.dex
7 |
8 | # Java class files
9 | *.class
10 |
11 | # Generated files
12 | bin/
13 | gen/
14 | out/
15 |
16 | # Gradle files
17 | .gradle/
18 | build/
19 |
20 | # Local configuration file (sdk path, etc)
21 | local.properties
22 |
23 | # Proguard folder generated by Eclipse
24 | proguard/
25 |
26 | # Log Files
27 | *.log
28 |
29 | # Android Studio Navigation editor temp files
30 | .navigation/
31 |
32 | # Android Studio captures folder
33 | captures/
34 |
35 | # Intellij
36 | *.iml
37 | .idea/workspace.xml
38 | .idea/tasks.xml
39 | .idea/gradle.xml
40 | .idea/dictionaries
41 | .idea/libraries
42 |
43 | # Keystore files
44 | *.jks
45 |
46 | # External native build folder generated in Android Studio 2.2 and later
47 | .externalNativeBuild
48 |
49 | # Google Services (e.g. APIs or Firebase)
50 | google-services.json
51 |
52 | # Freeline
53 | freeline.py
54 | freeline/
55 | freeline_project_description.json
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 26
5 | defaultConfig {
6 | applicationId "com.baina.androidremotecontroller"
7 | minSdkVersion 19
8 | targetSdkVersion 26
9 | versionCode 1
10 | versionName "1.0"
11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | }
20 |
21 | dependencies {
22 | implementation fileTree(dir: 'libs', include: ['*.jar'])
23 | implementation 'com.android.support:appcompat-v7:26.1.0'
24 | implementation 'com.android.support.constraint:constraint-layout:1.0.2'
25 | testImplementation 'junit:junit:4.12'
26 | androidTestImplementation 'com.android.support.test:runner:1.0.1'
27 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
28 | compile 'de.hdodenhof:circleimageview:2.2.0'
29 | }
30 |
--------------------------------------------------------------------------------
/app/src/main/java/com/baina/androidremotecontroller/activity/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.baina.androidremotecontroller.activity;
2 |
3 | import android.app.Activity;
4 | import android.content.Intent;
5 | import android.os.Bundle;
6 | import android.view.View;
7 |
8 | import com.baina.androidremotecontroller.R;
9 |
10 | public class MainActivity extends Activity {
11 |
12 | private static final String TAG = MainActivity.class.getSimpleName();
13 |
14 | @Override
15 | protected void onCreate(Bundle savedInstanceState) {
16 | super.onCreate(savedInstanceState);
17 | setContentView(R.layout.activity_main);
18 | }
19 |
20 | public void OnClick(View view) {
21 | switch (view.getId()) {
22 | case R.id.showAllSupportAudioAppsBt:
23 | startActivity(new Intent(MainActivity.this, AllSupportAudioAppActivity.class));
24 | break;
25 | case R.id.remoteControllerBt:
26 | startActivity(new Intent(MainActivity.this, RemoteControllerActivity.class));
27 | break;
28 | case R.id.NewMusicControlViewBt:
29 | startActivity(new Intent(MainActivity.this, TestMusicControlViewActivity.class));
30 | break;
31 | }
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/item_supportmusicplayer.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
14 |
15 |
24 |
25 |
35 |
36 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
9 |
19 |
20 |
31 |
32 |
43 |
44 |
45 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # AndroidRemoteController
2 |
3 | ## readme-中文
4 | AndroidRemoteController是一个远程控制第三方音乐播放器的项目。
5 |
6 | 在了解项目之前我们需要弄清楚2个知识点:
7 |
8 | 1、所有音乐播放App,它们都有注册耳机插拔的广播,还有就是你可以通过控制耳机按键来控制音乐播放,而耳机按键事件是可以模拟的,这就为控制第三方音乐播放器提供可能。
9 |
10 | 2、所有的音乐播放App,它们都会在通知栏有常驻的Notification来显示当前一些歌曲的信息,关于接收音乐信息的问题,这里指的是接收专辑、歌手专辑封面等等,那如何获取呢,一种方式是通过反射,但是普遍性比较差。在Android API 19中,谷歌为我们提供了RemoteController。
11 |
12 | 通过以上知识点,我们就能拿到第三方音乐播放App的各种音乐信息,并且控制第三方音乐播放。
13 |
14 | ## readme-en
15 | AndroidRemoteController Is a project to remotely control third-party music players.
16 |
17 | Before we understand the project we need to find out two knowledge points:
18 |
19 | 1, all music player App, they have registered headphones plug the radio, there is that you can control the headphone button to control music playback, and headphone button events can be simulated, which is to control the third-party music player may.
20 |
21 | 2, all the music player App, they will have a permanent notice Notification column to display some of the current song information on the reception of music information, here refers to receiving albums, artist album covers, etc., then how to get it One way is by reflection, but less universal. In Android API 19, Google provided us RemoteController.
22 |
23 | Through the above knowledge point, we can get a variety of music information of third-party music player App, and control the third-party music player.
24 |
25 | ## preview
26 | ### playing
27 | 
28 | ### pause
29 | 
30 | ### change
31 | 
32 | ## 结束
33 |
34 |
35 |
--------------------------------------------------------------------------------
/app/src/main/java/com/baina/androidremotecontroller/utils/SharedPreferenceUtil.java:
--------------------------------------------------------------------------------
1 | package com.baina.androidremotecontroller.utils;
2 |
3 | import android.content.Context;
4 | import android.content.SharedPreferences;
5 |
6 | import com.baina.androidremotecontroller.RemoteControllerApplication;
7 |
8 | public class SharedPreferenceUtil {
9 |
10 | private static Context mContext = RemoteControllerApplication.getInstance();
11 |
12 | public static void putKeyInt(String key, int putValue) {
13 | SharedPreferences settingPre = mContext.getSharedPreferences(key, Context.MODE_PRIVATE);
14 | settingPre.edit().putInt(key, putValue).apply();
15 | }
16 |
17 | public static int getKeyInt(String key, int defaultValue) {
18 | SharedPreferences intPreference = mContext.getSharedPreferences(key, Context.MODE_PRIVATE);
19 | return intPreference.getInt(key, defaultValue);
20 | }
21 |
22 |
23 | public static void putKeyLong(String key, long putValue) {
24 | SharedPreferences settingPre = mContext.getSharedPreferences(key, Context.MODE_PRIVATE);
25 | settingPre.edit().putLong(key, putValue).apply();
26 |
27 | }
28 |
29 | public static long getKeyLong(String key, long defaultValue) {
30 | SharedPreferences longPreference = mContext.getSharedPreferences(key, Context.MODE_PRIVATE);
31 | return longPreference.getLong(key, defaultValue);
32 | }
33 |
34 | public static void putKeyBoolean(String key, boolean putValue) {
35 | SharedPreferences settingPre = mContext.getSharedPreferences(key, Context.MODE_PRIVATE);
36 | settingPre.edit().putBoolean(key, putValue).apply();
37 | }
38 |
39 | public static boolean getKeyBoolean(String key, boolean defaultValue) {
40 | SharedPreferences booleanPreference = mContext.getSharedPreferences(key, Context.MODE_PRIVATE);
41 | return booleanPreference.getBoolean(key, defaultValue);
42 | }
43 |
44 | public static void putKeyString(String key, String putValue) {
45 | SharedPreferences settingPre = mContext.getSharedPreferences(key, Context.MODE_PRIVATE);
46 | settingPre.edit().putString(key, putValue).apply();
47 | }
48 |
49 | public static String getKeyString(String key, String defaultValue) {
50 | SharedPreferences stringPreference = mContext.getSharedPreferences(key, Context.MODE_PRIVATE);
51 | return stringPreference.getString(key, defaultValue);
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/app/src/main/java/com/baina/androidremotecontroller/adapter/SupportMusicPlayerAdapter.java:
--------------------------------------------------------------------------------
1 | package com.baina.androidremotecontroller.adapter;
2 |
3 | import android.content.Context;
4 | import android.view.LayoutInflater;
5 | import android.view.View;
6 | import android.view.ViewGroup;
7 | import android.widget.BaseAdapter;
8 | import android.widget.CheckBox;
9 | import android.widget.ImageView;
10 | import android.widget.TextView;
11 |
12 | import com.baina.androidremotecontroller.R;
13 | import com.baina.androidremotecontroller.model.AppInfo;
14 | import com.baina.androidremotecontroller.utils.Constants;
15 | import com.baina.androidremotecontroller.utils.SharedPreferenceUtil;
16 |
17 | import java.util.List;
18 |
19 | /**
20 | * Created by baina on 18-1-2.
21 | */
22 |
23 | public class SupportMusicPlayerAdapter extends BaseAdapter {
24 |
25 | private Context mContext;
26 | private List mAppInfoList;
27 | private LayoutInflater mInflater;
28 | private int mCheckedPosition = -1;
29 |
30 | public SupportMusicPlayerAdapter(Context context, List appInfos) {
31 | mContext = context;
32 | mAppInfoList = appInfos;
33 | mInflater = LayoutInflater.from(mContext);
34 | }
35 |
36 | @Override
37 | public int getCount() {
38 | return mAppInfoList.size();
39 | }
40 |
41 | @Override
42 | public Object getItem(int position) {
43 | return mAppInfoList.get(position);
44 | }
45 |
46 | @Override
47 | public long getItemId(int position) {
48 | return 0;
49 | }
50 |
51 | @Override
52 | public View getView(int position, View convertView, ViewGroup parent) {
53 | ViewHolder holder = null;
54 | if (convertView == null) {
55 | convertView = mInflater.inflate(R.layout.item_supportmusicplayer, parent, false);
56 | holder = new ViewHolder();
57 | holder.appIconIv = convertView.findViewById(R.id.appIconIv);
58 | holder.appLabelTv = convertView.findViewById(R.id.appLabelTv);
59 | holder.checkBox = convertView.findViewById(R.id.checkBox);
60 | convertView.setTag(holder);
61 | } else {
62 | holder = (ViewHolder) convertView.getTag();
63 | }
64 | AppInfo appInfo = mAppInfoList.get(position);
65 | holder.appIconIv.setImageDrawable(appInfo.getAppIcon());
66 | holder.appLabelTv.setText(appInfo.getAppLabel());
67 | if (appInfo.getAppPkg().equals(SharedPreferenceUtil.getKeyString(Constants.MUSICPLAYER, null)))
68 | mCheckedPosition = position;
69 | if (mCheckedPosition == position) {
70 | holder.checkBox.setChecked(true);
71 | } else {
72 | holder.checkBox.setChecked(false);
73 | }
74 | return convertView;
75 | }
76 |
77 | public void setCheckedPosition(int checkedPosition) {
78 | mCheckedPosition = checkedPosition;
79 | }
80 |
81 | private class ViewHolder {
82 | ImageView appIconIv;
83 | TextView appLabelTv;
84 | CheckBox checkBox;
85 | }
86 | }
87 |
--------------------------------------------------------------------------------
/app/src/main/java/com/baina/androidremotecontroller/activity/AllSupportAudioAppActivity.java:
--------------------------------------------------------------------------------
1 | package com.baina.androidremotecontroller.activity;
2 |
3 | import android.app.Activity;
4 | import android.content.Intent;
5 | import android.content.pm.PackageManager;
6 | import android.content.pm.ResolveInfo;
7 | import android.os.Bundle;
8 | import android.util.Log;
9 | import android.view.View;
10 | import android.widget.AdapterView;
11 | import android.widget.ListView;
12 |
13 | import com.baina.androidremotecontroller.model.AppInfo;
14 | import com.baina.androidremotecontroller.R;
15 | import com.baina.androidremotecontroller.adapter.SupportMusicPlayerAdapter;
16 | import com.baina.androidremotecontroller.utils.Constants;
17 | import com.baina.androidremotecontroller.utils.SharedPreferenceUtil;
18 |
19 | import java.util.ArrayList;
20 | import java.util.List;
21 |
22 | /**
23 | * Created by baina on 18-1-3.
24 | * 显示所有支持audio相关app
25 | */
26 | public class AllSupportAudioAppActivity extends Activity {
27 |
28 | private static final String TAG = AllSupportAudioAppActivity.class.getSimpleName();
29 |
30 | private List mAppInfoList;
31 | private SupportMusicPlayerAdapter mAdapter;
32 | private ListView mAppListView;
33 |
34 | @Override
35 | protected void onCreate(Bundle savedInstanceState) {
36 | super.onCreate(savedInstanceState);
37 | setContentView(R.layout.activity_allsupportaudioapp);
38 | mAppListView = findViewById(R.id.appListView);
39 | mAppInfoList = new ArrayList<>();
40 | }
41 |
42 | @Override
43 | protected void onResume() {
44 | super.onResume();
45 | //耳机控制播放器的intent action
46 | Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
47 | List infoList = getPackageManager().queryBroadcastReceivers(intent, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
48 | if (infoList.size() > 0) {
49 | mAppInfoList.clear();
50 | for (ResolveInfo resolveInfo : infoList) {
51 | AppInfo appInfo = new AppInfo();
52 | //set Icon
53 | appInfo.setAppIcon(resolveInfo.loadIcon(getPackageManager()));
54 | //set Application Name
55 | appInfo.setAppLabel(resolveInfo.loadLabel(getPackageManager()).toString());
56 | //set Package Name
57 | appInfo.setAppPkg(resolveInfo.activityInfo.packageName);
58 | mAppInfoList.add(appInfo);
59 | }
60 | mAdapter = new SupportMusicPlayerAdapter(AllSupportAudioAppActivity.this, mAppInfoList);
61 | mAppListView.setAdapter(mAdapter);
62 | mAppListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
63 | @Override
64 | public void onItemClick(AdapterView> parent, View view, int position, long id) {
65 | mAdapter.setCheckedPosition(position);
66 | mAdapter.notifyDataSetChanged();
67 | AppInfo appInfo = mAppInfoList.get(position);
68 | Log.d(TAG, "你选择了:" + appInfo.getAppLabel());
69 | SharedPreferenceUtil.putKeyString(Constants.MUSICPLAYER, appInfo.getAppPkg());
70 | }
71 | });
72 | }
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/layout_musiccontrol.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
14 |
15 |
21 |
22 |
31 |
32 |
40 |
41 |
50 |
51 |
52 |
58 |
59 |
65 |
66 |
74 |
75 |
83 |
84 |
85 |
86 |
--------------------------------------------------------------------------------
/app/src/main/java/com/baina/androidremotecontroller/service/MusicNotificationListenerService.java:
--------------------------------------------------------------------------------
1 | package com.baina.androidremotecontroller.service;
2 |
3 | import android.content.Intent;
4 | import android.media.AudioManager;
5 | import android.media.RemoteController;
6 | import android.os.Binder;
7 | import android.os.IBinder;
8 | import android.service.notification.NotificationListenerService;
9 | import android.service.notification.StatusBarNotification;
10 | import android.util.Log;
11 | import android.view.KeyEvent;
12 |
13 | /**
14 | * Created by baina on 18-1-2.
15 | * 音乐通知相关服务
16 | */
17 |
18 | public class MusicNotificationListenerService extends NotificationListenerService implements RemoteController.OnClientUpdateListener {
19 |
20 |
21 | private String TAG = MusicNotificationListenerService.class.getSimpleName();
22 |
23 | private RemoteController mRemoteController;
24 | private RCBinder mBinder = new RCBinder();
25 | private RemoteController.OnClientUpdateListener mExternalClientUpdateListener;
26 |
27 | @Override
28 | public IBinder onBind(Intent intent) {
29 | return mBinder;
30 | }
31 |
32 | public void setExternalClientUpdateListener(RemoteController.OnClientUpdateListener externalClientUpdateListener) {
33 | mExternalClientUpdateListener = externalClientUpdateListener;
34 | }
35 |
36 | public void registerRemoteController() {
37 | mRemoteController = new RemoteController(this, this);
38 | boolean registered;
39 | try {
40 | registered = ((AudioManager) getSystemService(AUDIO_SERVICE)).registerRemoteController(mRemoteController);
41 | } catch (NullPointerException | SecurityException e) {
42 | registered = false;
43 | }
44 | if (registered) {
45 | try {
46 | mRemoteController.setArtworkConfiguration(100, 100);
47 | mRemoteController.setSynchronizationMode(RemoteController.POSITION_SYNCHRONIZATION_CHECK);
48 | } catch (IllegalArgumentException e) {
49 | e.printStackTrace();
50 | }
51 | }
52 | }
53 |
54 | @Override
55 |
56 | public void onNotificationPosted(StatusBarNotification sbn) {
57 | Log.e(TAG, "onNotificationPosted...");
58 | }
59 |
60 | @Override
61 |
62 | public void onNotificationRemoved(StatusBarNotification sbn) {
63 | Log.e(TAG, "onNotificationRemoved...");
64 | }
65 |
66 | @Override
67 | public void onClientChange(boolean clearing) {
68 | if (mExternalClientUpdateListener != null)
69 | mExternalClientUpdateListener.onClientChange(clearing);
70 | }
71 |
72 | @Override
73 | public void onClientPlaybackStateUpdate(int state) {
74 | if (mExternalClientUpdateListener != null)
75 | mExternalClientUpdateListener.onClientPlaybackStateUpdate(state);
76 | }
77 |
78 | @Override
79 | public void onClientPlaybackStateUpdate(int state, long stateChangeTimeMs, long currentPosMs, float speed) {
80 | if (mExternalClientUpdateListener != null)
81 | mExternalClientUpdateListener.onClientPlaybackStateUpdate(state, stateChangeTimeMs, currentPosMs, speed);
82 | }
83 |
84 | @Override
85 | public void onClientTransportControlUpdate(int transportControlFlags) {
86 | if (mExternalClientUpdateListener != null)
87 | mExternalClientUpdateListener.onClientTransportControlUpdate(transportControlFlags);
88 | }
89 |
90 | @Override
91 | public void onClientMetadataUpdate(RemoteController.MetadataEditor metadataEditor) {
92 | if (mExternalClientUpdateListener != null)
93 | mExternalClientUpdateListener.onClientMetadataUpdate(metadataEditor);
94 | }
95 |
96 | public boolean sendMusicKeyEvent(int keyCode) {
97 | if (mRemoteController != null) {
98 | KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
99 | boolean down = mRemoteController.sendMediaKeyEvent(keyEvent);
100 | keyEvent = new KeyEvent(KeyEvent.ACTION_UP, keyCode);
101 | boolean up = mRemoteController.sendMediaKeyEvent(keyEvent);
102 | return down && up;
103 | }
104 | return false;
105 | }
106 |
107 | public class RCBinder extends Binder {
108 | public MusicNotificationListenerService getService() {
109 | return MusicNotificationListenerService.this;
110 | }
111 | }
112 | }
113 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/app/src/main/java/com/baina/androidremotecontroller/view/MusicControlView.java:
--------------------------------------------------------------------------------
1 | package com.baina.androidremotecontroller.view;
2 |
3 | import android.content.Context;
4 | import android.content.pm.ApplicationInfo;
5 | import android.util.AttributeSet;
6 | import android.view.LayoutInflater;
7 | import android.view.View;
8 | import android.widget.ImageButton;
9 | import android.widget.ImageView;
10 | import android.widget.RelativeLayout;
11 | import android.widget.TextView;
12 |
13 | import com.baina.androidremotecontroller.R;
14 | import com.baina.androidremotecontroller.model.Music;
15 |
16 | /**
17 | * Created by baina on 18-1-3.
18 | * 音乐控制view
19 | * 给调用者预留有接口
20 | */
21 |
22 | public class MusicControlView extends RelativeLayout {
23 |
24 | //STATE_XX是音乐控制中心对应的三种状态
25 | //状态1,没有选择播放器
26 | public static final int STATE_NOMUSICPLAYER = 0x001;
27 | //状态2,已经选择播放器,但是没有音乐播放信息
28 | public static final int STATE_NOMUSICDATA = 0x002;
29 | //状态3,已经选择播放器,并且有音乐播放信息
30 | public static final int STATE_MUSICDATA = 0x003;
31 |
32 | private int mMusicControlState;
33 | private Context mContext;
34 | private ImageView mMusicCoverIv;
35 | private TextView mMusicPlayerTv;
36 | private TextView mMusicTitleTv;
37 | private TextView mMusicArtistTv;
38 | private ImageButton mPlayPauseIb;
39 | private ImageButton mPreviousIb;
40 | private ImageButton mNextIb;
41 | private OnMusicControlClickListener mMusicControlClickListener;
42 |
43 | public MusicControlView(Context context) {
44 | super(context);
45 | }
46 |
47 | public MusicControlView(Context context, AttributeSet attrs) {
48 | super(context, attrs);
49 | }
50 |
51 | public void onCreate(final OnMusicControlClickListener musicControlClickListener) {
52 | mContext = getContext();
53 | LayoutInflater.from(mContext).inflate(R.layout.layout_musiccontrol, this);
54 | mMusicCoverIv = findViewById(R.id.musicCoverIv);
55 | mMusicPlayerTv = findViewById(R.id.musicPlayerTv);
56 | mMusicTitleTv = findViewById(R.id.musicTitleTv);
57 | mMusicArtistTv = findViewById(R.id.musicArtistTv);
58 | mPlayPauseIb = findViewById(R.id.playPauseIb);
59 | mPreviousIb = findViewById(R.id.previousIb);
60 | mNextIb = findViewById(R.id.nextIb);
61 | mMusicControlClickListener = musicControlClickListener;
62 | //初始化点击事件
63 | initListener();
64 | }
65 |
66 | private void initListener() {
67 | mMusicCoverIv.setOnClickListener(new OnClickListener() {
68 | @Override
69 | public void onClick(View v) {
70 | if (mMusicControlClickListener != null) {
71 | mMusicControlClickListener.OnClickMusicCover(mMusicControlState);
72 | }
73 | }
74 | });
75 | mPreviousIb.setOnClickListener(new OnClickListener() {
76 | @Override
77 | public void onClick(View v) {
78 | if (mMusicControlClickListener != null) {
79 | mMusicControlClickListener.OnClickPrevious(mMusicControlState);
80 | }
81 | }
82 | });
83 | mPlayPauseIb.setOnClickListener(new OnClickListener() {
84 | @Override
85 | public void onClick(View v) {
86 | if (mMusicControlClickListener != null)
87 | mMusicControlClickListener.OnClickPlayPause(mMusicControlState);
88 | }
89 | });
90 | mNextIb.setOnClickListener(new OnClickListener() {
91 | @Override
92 | public void onClick(View v) {
93 | if (mMusicControlClickListener != null)
94 | mMusicControlClickListener.OnClickNext(mMusicControlState);
95 | }
96 | });
97 | }
98 |
99 | //没有选择播放器
100 | public void onNoMusicPlayer() {
101 | mMusicPlayerTv.setVisibility(VISIBLE);
102 | mMusicPlayerTv.setText("choose a music player");
103 | mMusicTitleTv.setVisibility(GONE);
104 | mMusicArtistTv.setVisibility(GONE);
105 | mMusicControlState = STATE_NOMUSICPLAYER;
106 | }
107 |
108 | //没有歌曲信息,此时只会显示播放器名称
109 | public void onNoMusicData(ApplicationInfo applicationInfo) {
110 | //这里放这句判断,主要是RemoteControllerActivity在onResume的时候,有个状态操作,如果此时有歌曲信息,则不走后面的逻辑
111 | if (mMusicControlState == STATE_MUSICDATA)
112 | return;
113 | mMusicCoverIv.setImageDrawable(applicationInfo.loadIcon(mContext.getPackageManager()));
114 | mMusicPlayerTv.setVisibility(VISIBLE);
115 | mMusicPlayerTv.setText(applicationInfo.loadLabel(mContext.getPackageManager()));
116 | mMusicTitleTv.setVisibility(GONE);
117 | mMusicArtistTv.setVisibility(GONE);
118 | mMusicControlState = STATE_NOMUSICDATA;
119 | }
120 |
121 | //更新音乐信息
122 | public void onClientMetadataUpdate(Music music) {
123 | mMusicCoverIv.setImageBitmap(music.getCover());
124 | mMusicPlayerTv.setVisibility(GONE);
125 | mMusicTitleTv.setVisibility(VISIBLE);
126 | mMusicArtistTv.setVisibility(VISIBLE);
127 | mMusicTitleTv.setText(music.getTitle());
128 | mMusicArtistTv.setText(music.getArtist());
129 | mMusicControlState = STATE_MUSICDATA;
130 | }
131 |
132 | //更新播放/暂停按钮状态
133 | public void onClientPlaybackStateUpdate(int state) {
134 | mMusicControlState = STATE_MUSICDATA;
135 | switch (state) {
136 | case 2:
137 | //paused
138 | mPlayPauseIb.setBackgroundResource(R.mipmap.play);
139 | break;
140 | case 3:
141 | //playing
142 | mPlayPauseIb.setBackgroundResource(R.mipmap.pause);
143 | break;
144 |
145 | }
146 | }
147 |
148 | public interface OnMusicControlClickListener {
149 |
150 | //点击音乐封面
151 | void OnClickMusicCover(int musicControlState);
152 |
153 | //点击播放/暂停
154 | void OnClickPlayPause(int musicControlState);
155 |
156 | //点击上一曲
157 | void OnClickPrevious(int musicControlState);
158 |
159 | //点击下一曲
160 | void OnClickNext(int musicControlState);
161 | }
162 | }
163 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/app/src/main/java/com/baina/androidremotecontroller/activity/RemoteControllerActivity.java:
--------------------------------------------------------------------------------
1 | package com.baina.androidremotecontroller.activity;
2 |
3 | import android.app.Activity;
4 | import android.content.ComponentName;
5 | import android.content.Context;
6 | import android.content.Intent;
7 | import android.content.ServiceConnection;
8 | import android.content.pm.ApplicationInfo;
9 | import android.content.pm.PackageManager;
10 | import android.graphics.Bitmap;
11 | import android.graphics.BitmapFactory;
12 | import android.media.MediaMetadataRetriever;
13 | import android.media.RemoteController;
14 | import android.os.Bundle;
15 | import android.os.IBinder;
16 | import android.support.v4.app.NotificationManagerCompat;
17 | import android.text.TextUtils;
18 | import android.util.Log;
19 | import android.view.KeyEvent;
20 | import android.widget.Toast;
21 |
22 | import com.baina.androidremotecontroller.model.Music;
23 | import com.baina.androidremotecontroller.service.MusicNotificationListenerService;
24 | import com.baina.androidremotecontroller.R;
25 | import com.baina.androidremotecontroller.utils.Constants;
26 | import com.baina.androidremotecontroller.utils.SharedPreferenceUtil;
27 | import com.baina.androidremotecontroller.view.MusicControlView;
28 |
29 | import java.util.Set;
30 |
31 | /**
32 | * Created by baina on 18-1-3.
33 | * 远程获取第三方音乐信息及控制第三方音乐
34 | */
35 | public class RemoteControllerActivity extends Activity implements RemoteController.OnClientUpdateListener {
36 |
37 | private static final String TAG = RemoteControllerActivity.class.getSimpleName();
38 |
39 | private MusicNotificationListenerService mNotificationListenerService;
40 | private MusicControlView mMusicControlView;
41 |
42 | @Override
43 | protected void onCreate(Bundle savedInstanceState) {
44 | super.onCreate(savedInstanceState);
45 | setContentView(R.layout.activity_remotecontroller);
46 | mMusicControlView = findViewById(R.id.controlView);
47 | mMusicControlView.onCreate(new MusicControlView.OnMusicControlClickListener() {
48 |
49 | @Override
50 | public void OnClickMusicCover(int musicControlState) {
51 | switch (musicControlState) {
52 | case MusicControlView.STATE_NOMUSICPLAYER:
53 | startActivity(new Intent("com.baina.allsupportaudioapp"));
54 | break;
55 | case MusicControlView.STATE_NOMUSICDATA:
56 | case MusicControlView.STATE_MUSICDATA:
57 | String appPkg = SharedPreferenceUtil.getKeyString(Constants.MUSICPLAYER, null);
58 | startApp(appPkg);
59 | break;
60 | }
61 | }
62 |
63 | @Override
64 | public void OnClickPlayPause(int musicControlState) {
65 | switch (musicControlState) {
66 | case MusicControlView.STATE_NOMUSICPLAYER:
67 | startActivity(new Intent("com.baina.allsupportaudioapp"));
68 | break;
69 | case MusicControlView.STATE_NOMUSICDATA:
70 | String appPkg = SharedPreferenceUtil.getKeyString(Constants.MUSICPLAYER, null);
71 | startApp(appPkg);
72 | break;
73 | case MusicControlView.STATE_MUSICDATA:
74 | if (mNotificationListenerService != null) {
75 | mNotificationListenerService.sendMusicKeyEvent(KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE);
76 | }
77 | break;
78 | }
79 | }
80 |
81 | @Override
82 | public void OnClickPrevious(int musicControlState) {
83 | switch (musicControlState) {
84 | case MusicControlView.STATE_MUSICDATA:
85 | if (mNotificationListenerService != null) {
86 | mNotificationListenerService.sendMusicKeyEvent(KeyEvent.KEYCODE_MEDIA_PREVIOUS);
87 | }
88 | break;
89 | }
90 | }
91 |
92 | @Override
93 | public void OnClickNext(int musicControlState) {
94 | switch (musicControlState) {
95 | case MusicControlView.STATE_MUSICDATA:
96 | if (mNotificationListenerService != null) {
97 | mNotificationListenerService.sendMusicKeyEvent(KeyEvent.KEYCODE_MEDIA_NEXT);
98 | }
99 | break;
100 | }
101 | }
102 | });
103 | }
104 |
105 | @Override
106 | protected void onResume() {
107 | super.onResume();
108 | //获取通知相关权限
109 | if (!isNotificationListenerServiceEnabled(this)) {
110 | startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
111 | Toast.makeText(RemoteControllerActivity.this, "请授予通知使用权限", Toast.LENGTH_SHORT).show();
112 | } else {
113 | bindService(new Intent(RemoteControllerActivity.this, MusicNotificationListenerService.class), new ServiceConnection() {
114 | @Override
115 | public void onServiceConnected(ComponentName name, IBinder service) {
116 | MusicNotificationListenerService.RCBinder rcBinder = (MusicNotificationListenerService.RCBinder) service;
117 | mNotificationListenerService = rcBinder.getService();
118 | mNotificationListenerService.registerRemoteController();
119 | mNotificationListenerService.setExternalClientUpdateListener(RemoteControllerActivity.this);
120 | }
121 |
122 | @Override
123 | public void onServiceDisconnected(ComponentName name) {
124 |
125 | }
126 | }, Context.BIND_AUTO_CREATE);
127 | }
128 | //初始化MusicControlView相关
129 | String appPkg = SharedPreferenceUtil.getKeyString(Constants.MUSICPLAYER, null);
130 | if (TextUtils.isEmpty(appPkg)) {
131 | mMusicControlView.onNoMusicPlayer();
132 | } else {
133 | ApplicationInfo info = null;
134 | try {
135 | info = getPackageManager().getApplicationInfo(appPkg, 0);
136 | mMusicControlView.onNoMusicData(info);
137 | } catch (PackageManager.NameNotFoundException e) {
138 | e.printStackTrace();
139 | }
140 | }
141 | }
142 |
143 | @Override
144 | public void onClientChange(boolean clearing) {
145 |
146 | }
147 |
148 | @Override
149 | public void onClientPlaybackStateUpdate(int state) {
150 |
151 | }
152 |
153 | @Override
154 | public void onClientPlaybackStateUpdate(int state, long stateChangeTimeMs, long currentPosMs, float speed) {
155 | mMusicControlView.onClientPlaybackStateUpdate(state);
156 | }
157 |
158 | @Override
159 | public void onClientTransportControlUpdate(int transportControlFlags) {
160 |
161 | }
162 |
163 | @Override
164 | public void onClientMetadataUpdate(RemoteController.MetadataEditor metadataEditor) {
165 | String artist = metadataEditor.getString(MediaMetadataRetriever.METADATA_KEY_ARTIST, "null");
166 | String album = metadataEditor.getString(MediaMetadataRetriever.METADATA_KEY_ALBUM, "null");
167 | String title = metadataEditor.getString(MediaMetadataRetriever.METADATA_KEY_TITLE, "null");
168 | Long duration = metadataEditor.getLong(MediaMetadataRetriever.METADATA_KEY_DURATION, -1);
169 | Bitmap defaultCover = BitmapFactory.decodeResource(getResources(), android.R.drawable.ic_menu_compass);
170 | Bitmap bitmap = metadataEditor.getBitmap(RemoteController.MetadataEditor.BITMAP_KEY_ARTWORK, defaultCover);
171 | Log.e(TAG, "artist:" + artist + "album:" + album + "title:" + title + "duration:" + duration);
172 | Music music = new Music();
173 | music.setCover(bitmap);
174 | music.setTitle(title);
175 | music.setArtist(artist);
176 | mMusicControlView.onClientMetadataUpdate(music);
177 | }
178 |
179 | private void startApp(String appPkg) {
180 | try {
181 | Intent intent = this.getPackageManager().getLaunchIntentForPackage(appPkg);
182 | startActivity(intent);
183 | } catch (Exception e) {
184 | Toast.makeText(this, "应用未安装,启动失败", Toast.LENGTH_LONG).show();
185 | }
186 | }
187 |
188 | /**
189 | * 是否已经授予通知相关权限
190 | *
191 | * @param context,上下文对象
192 | * @return
193 | */
194 | private boolean isNotificationListenerServiceEnabled(Context context) {
195 | Set packageNames = NotificationManagerCompat.getEnabledListenerPackages(context);
196 | if (packageNames.contains(context.getPackageName())) {
197 | return true;
198 | }
199 | return false;
200 | }
201 | }
202 |
203 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/app/src/main/java/com/baina/androidremotecontroller/view/NewMusicControlView.java:
--------------------------------------------------------------------------------
1 | package com.baina.androidremotecontroller.view;
2 |
3 | import android.content.ComponentName;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import android.content.ServiceConnection;
7 | import android.content.SharedPreferences;
8 | import android.content.pm.ApplicationInfo;
9 | import android.content.pm.PackageManager;
10 | import android.graphics.Bitmap;
11 | import android.graphics.BitmapFactory;
12 | import android.media.MediaMetadataRetriever;
13 | import android.media.RemoteController;
14 | import android.os.IBinder;
15 | import android.support.v4.app.NotificationManagerCompat;
16 | import android.text.TextUtils;
17 | import android.util.AttributeSet;
18 | import android.util.Log;
19 | import android.view.KeyEvent;
20 | import android.view.LayoutInflater;
21 | import android.view.View;
22 | import android.widget.ImageButton;
23 | import android.widget.ImageView;
24 | import android.widget.RelativeLayout;
25 | import android.widget.TextView;
26 | import android.widget.Toast;
27 |
28 | import com.baina.androidremotecontroller.R;
29 | import com.baina.androidremotecontroller.model.Music;
30 | import com.baina.androidremotecontroller.service.MusicNotificationListenerService;
31 | import com.baina.androidremotecontroller.utils.Constants;
32 | import com.baina.androidremotecontroller.utils.SharedPreferenceUtil;
33 |
34 | import java.util.Set;
35 |
36 | /**
37 | * Created by baina on 18-1-5.
38 | * 音乐控制view,高度集成
39 | */
40 |
41 | public class NewMusicControlView extends RelativeLayout implements SharedPreferences.OnSharedPreferenceChangeListener {
42 |
43 | //STATE_XX是音乐控制中心对应的三种状态
44 | //状态1,没有选择播放器
45 | private static final int STATE_NOMUSICPLAYER = 0x001;
46 | //状态2,已经选择播放器,但是没有音乐播放信息
47 | private static final int STATE_NOMUSICDATA = 0x002;
48 | //状态3,已经选择播放器,并且有音乐播放信息
49 | private static final int STATE_MUSICDATA = 0x003;
50 |
51 | private static final String TAG = NewMusicControlView.class.getSimpleName();
52 |
53 | private int mMusicControlState;
54 | private Context mContext;
55 | private ImageView mMusicCoverIv;
56 | private TextView mMusicPlayerTv;
57 | private TextView mMusicTitleTv;
58 | private TextView mMusicArtistTv;
59 | private ImageButton mPlayPauseIb;
60 | private ImageButton mPreviousIb;
61 | private ImageButton mNextIb;
62 | private RemoteController.OnClientUpdateListener mOnClientUpdateListener;
63 | private ServiceConnection mServiceConnection;
64 | private MusicNotificationListenerService mNotificationListenerService;
65 | private SharedPreferences mPreferences;
66 |
67 | public NewMusicControlView(Context context) {
68 | super(context);
69 | }
70 |
71 | public NewMusicControlView(Context context, AttributeSet attrs) {
72 | super(context, attrs);
73 | }
74 |
75 | @Override
76 | protected void onFinishInflate() {
77 | super.onFinishInflate();
78 | Log.d(TAG, "onFinishInflate");
79 | onCreate();
80 | }
81 |
82 | @Override
83 | protected void onAttachedToWindow() {
84 | super.onAttachedToWindow();
85 | Log.d(TAG, "onAttachedToWindow");
86 | //绑定通知相关service
87 | mServiceConnection = new ServiceConnection() {
88 | @Override
89 | public void onServiceConnected(ComponentName name, IBinder service) {
90 | MusicNotificationListenerService.RCBinder rcBinder = (MusicNotificationListenerService.RCBinder) service;
91 | mNotificationListenerService = rcBinder.getService();
92 | mNotificationListenerService.registerRemoteController();
93 | mNotificationListenerService.setExternalClientUpdateListener(mOnClientUpdateListener);
94 | }
95 |
96 | @Override
97 | public void onServiceDisconnected(ComponentName name) {
98 |
99 | }
100 | };
101 | mContext.bindService(new Intent(mContext, MusicNotificationListenerService.class), mServiceConnection, Context.BIND_AUTO_CREATE);
102 | mPreferences = mContext.getSharedPreferences(Constants.MUSICPLAYER, Context.MODE_PRIVATE);
103 | mPreferences.registerOnSharedPreferenceChangeListener(this);
104 | onRestoreInstanceState();
105 | }
106 |
107 | @Override
108 | protected void onDetachedFromWindow() {
109 | super.onDetachedFromWindow();
110 | Log.d(TAG, "onDetachedFromWindow");
111 | //解绑通知相关service
112 | // mContext.unbindService(mServiceConnection);
113 | mPreferences.unregisterOnSharedPreferenceChangeListener(this);
114 | }
115 |
116 | @Override
117 | public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
118 | if (key.equals(Constants.MUSICPLAYER)) {
119 | onRestoreInstanceState();
120 | }
121 | }
122 |
123 | private void onCreate() {
124 | mContext = getContext();
125 | LayoutInflater.from(mContext).inflate(R.layout.layout_musiccontrol, this);
126 | mMusicCoverIv = findViewById(R.id.musicCoverIv);
127 | mMusicPlayerTv = findViewById(R.id.musicPlayerTv);
128 | mMusicTitleTv = findViewById(R.id.musicTitleTv);
129 | mMusicArtistTv = findViewById(R.id.musicArtistTv);
130 | mPlayPauseIb = findViewById(R.id.playPauseIb);
131 | mPreviousIb = findViewById(R.id.previousIb);
132 | mNextIb = findViewById(R.id.nextIb);
133 | //初始化点击事件
134 | initListener();
135 | }
136 |
137 | private void initListener() {
138 | mMusicCoverIv.setOnClickListener(new OnClickListener() {
139 | @Override
140 | public void onClick(View v) {
141 | switch (mMusicControlState) {
142 | case STATE_NOMUSICPLAYER:
143 | selectMusicPlayer();
144 | break;
145 | case STATE_NOMUSICDATA:
146 | case STATE_MUSICDATA:
147 | String appPkg = SharedPreferenceUtil.getKeyString(Constants.MUSICPLAYER, null);
148 | startApp(appPkg);
149 | break;
150 | }
151 | }
152 | });
153 | mPreviousIb.setOnClickListener(new OnClickListener() {
154 | @Override
155 | public void onClick(View v) {
156 | switch (mMusicControlState) {
157 | case MusicControlView.STATE_MUSICDATA:
158 | if (mNotificationListenerService != null) {
159 | mNotificationListenerService.sendMusicKeyEvent(KeyEvent.KEYCODE_MEDIA_PREVIOUS);
160 | }
161 | break;
162 | }
163 | }
164 | });
165 | mPlayPauseIb.setOnClickListener(new OnClickListener() {
166 | @Override
167 | public void onClick(View v) {
168 | switch (mMusicControlState) {
169 | case STATE_NOMUSICPLAYER:
170 | selectMusicPlayer();
171 | break;
172 | case STATE_NOMUSICDATA:
173 | String appPkg = SharedPreferenceUtil.getKeyString(Constants.MUSICPLAYER, null);
174 | startApp(appPkg);
175 | break;
176 | case STATE_MUSICDATA:
177 | if (mNotificationListenerService != null) {
178 | //可能存在,正在播放音乐,并且此时本地有音乐信息,但是音乐app被用户杀死的情况,此时应该启动app
179 | if (!mNotificationListenerService.sendMusicKeyEvent(KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE)) {
180 | String appPkg1 = SharedPreferenceUtil.getKeyString(Constants.MUSICPLAYER, null);
181 | startApp(appPkg1);
182 | }
183 | }
184 | break;
185 | }
186 | }
187 | });
188 | mNextIb.setOnClickListener(new OnClickListener() {
189 | @Override
190 | public void onClick(View v) {
191 | switch (mMusicControlState) {
192 | case STATE_MUSICDATA:
193 | if (mNotificationListenerService != null) {
194 | mNotificationListenerService.sendMusicKeyEvent(KeyEvent.KEYCODE_MEDIA_NEXT);
195 | }
196 | break;
197 | }
198 | }
199 | });
200 | mOnClientUpdateListener = new RemoteController.OnClientUpdateListener() {
201 | @Override
202 | public void onClientChange(boolean clearing) {
203 |
204 | }
205 |
206 | @Override
207 | public void onClientPlaybackStateUpdate(int state) {
208 | onPlaybackStateUpdate(state);
209 | }
210 |
211 | @Override
212 | public void onClientPlaybackStateUpdate(int state, long stateChangeTimeMs, long currentPosMs, float speed) {
213 | onPlaybackStateUpdate(state);
214 | }
215 |
216 | @Override
217 | public void onClientTransportControlUpdate(int transportControlFlags) {
218 |
219 | }
220 |
221 | @Override
222 | public void onClientMetadataUpdate(RemoteController.MetadataEditor metadataEditor) {
223 | String artist = metadataEditor.getString(MediaMetadataRetriever.METADATA_KEY_ARTIST, "null");
224 | String album = metadataEditor.getString(MediaMetadataRetriever.METADATA_KEY_ALBUM, "null");
225 | String title = metadataEditor.getString(MediaMetadataRetriever.METADATA_KEY_TITLE, "null");
226 | Long duration = metadataEditor.getLong(MediaMetadataRetriever.METADATA_KEY_DURATION, -1);
227 | Bitmap defaultCover = BitmapFactory.decodeResource(getResources(), android.R.drawable.ic_menu_compass);
228 | Bitmap bitmap = metadataEditor.getBitmap(RemoteController.MetadataEditor.BITMAP_KEY_ARTWORK, defaultCover);
229 | Log.d(TAG, "artist:" + artist + "album:" + album + "title:" + title + "duration:" + duration);
230 | Music music = new Music();
231 | music.setCover(bitmap);
232 | music.setTitle(title);
233 | music.setArtist(artist);
234 | onMetadataUpdate(music);
235 | }
236 | };
237 | }
238 |
239 | //初始化MusicControlView状态
240 | private void onRestoreInstanceState() {
241 | String appPkg = SharedPreferenceUtil.getKeyString(Constants.MUSICPLAYER, null);
242 | if (TextUtils.isEmpty(appPkg)) {
243 | onNoMusicPlayer();
244 | } else {
245 | ApplicationInfo info = null;
246 | try {
247 | info = mContext.getPackageManager().getApplicationInfo(appPkg, 0);
248 | onNoMusicData(info);
249 | } catch (PackageManager.NameNotFoundException e) {
250 | e.printStackTrace();
251 | }
252 | }
253 | }
254 |
255 | //没有选择播放器
256 | private void onNoMusicPlayer() {
257 | mMusicPlayerTv.setVisibility(VISIBLE);
258 | mMusicPlayerTv.setText("choose a music player");
259 | mMusicTitleTv.setVisibility(GONE);
260 | mMusicArtistTv.setVisibility(GONE);
261 | mMusicControlState = STATE_NOMUSICPLAYER;
262 | }
263 |
264 | //没有歌曲信息,此时只会显示播放器名称
265 | private void onNoMusicData(ApplicationInfo applicationInfo) {
266 | //这里放这句判断,主要是RemoteControllerActivity在onResume的时候,有个状态操作,如果此时有歌曲信息,则不走后面的逻辑
267 | if (mMusicControlState == STATE_MUSICDATA)
268 | return;
269 | mMusicCoverIv.setImageDrawable(applicationInfo.loadIcon(mContext.getPackageManager()));
270 | mMusicPlayerTv.setVisibility(VISIBLE);
271 | mMusicPlayerTv.setText(applicationInfo.loadLabel(mContext.getPackageManager()));
272 | mMusicTitleTv.setVisibility(GONE);
273 | mMusicArtistTv.setVisibility(GONE);
274 | mMusicControlState = STATE_NOMUSICDATA;
275 | }
276 |
277 | //更新音乐信息
278 | private void onMetadataUpdate(Music music) {
279 | if (mMusicCoverIv == null || mMusicPlayerTv == null || mMusicTitleTv == null || mMusicArtistTv == null)
280 | return;
281 | mMusicCoverIv.setImageBitmap(music.getCover());
282 | mMusicPlayerTv.setVisibility(GONE);
283 | mMusicTitleTv.setVisibility(VISIBLE);
284 | mMusicArtistTv.setVisibility(VISIBLE);
285 | mMusicTitleTv.setText(music.getTitle());
286 | mMusicArtistTv.setText(music.getArtist());
287 | mMusicControlState = STATE_MUSICDATA;
288 | }
289 |
290 | //更新播放/暂停按钮状态
291 | private void onPlaybackStateUpdate(int state) {
292 | if (mPlayPauseIb == null)
293 | return;
294 | mMusicControlState = STATE_MUSICDATA;
295 | switch (state) {
296 | case 2:
297 | //paused
298 | mPlayPauseIb.setBackgroundResource(R.mipmap.play);
299 | break;
300 | case 3:
301 | //playing
302 | mPlayPauseIb.setBackgroundResource(R.mipmap.pause);
303 | break;
304 |
305 | }
306 | }
307 |
308 | private void selectMusicPlayer() {
309 | if (!isNotificationListenerServiceEnabled(mContext)) {
310 | mContext.startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
311 | Toast.makeText(mContext, R.string.grant_notifications_access, Toast.LENGTH_SHORT).show();
312 | } else {
313 | mContext.startActivity(new Intent("com.baina.allsupportaudioapp"));
314 | }
315 | }
316 |
317 | private void startApp(String appPkg) {
318 | if (!isNotificationListenerServiceEnabled(mContext)) {
319 | mContext.startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
320 | Toast.makeText(mContext, R.string.grant_notifications_access, Toast.LENGTH_SHORT).show();
321 | } else {
322 | try {
323 | Intent intent = mContext.getPackageManager().getLaunchIntentForPackage(appPkg);
324 | mContext.startActivity(intent);
325 | } catch (Exception e) {
326 | Toast.makeText(mContext, R.string.fail_startup_app, Toast.LENGTH_LONG).show();
327 | }
328 | }
329 | }
330 |
331 | /**
332 | * 是否已经授予通知相关权限
333 | *
334 | * @param context,上下文对象
335 | * @return
336 | */
337 | private boolean isNotificationListenerServiceEnabled(Context context) {
338 | Set packageNames = NotificationManagerCompat.getEnabledListenerPackages(context);
339 | if (packageNames.contains(context.getPackageName())) {
340 | return true;
341 | }
342 | return false;
343 | }
344 | }
345 |
--------------------------------------------------------------------------------