diff --git a/.gitignore b/.gitignore index 649affc..47333d9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,50 +1,50 @@ -### Android ### -# Built application files -*.apk -*.ap_ - -# Files for the ART/Dalvik VM -*.dex - -# Java class files -*.class - -# Generated files -bin/ -gen/ -out/ - -# Gradle files -.gradle/ -build/ - -# Local configuration file (sdk path, etc) -local.properties - -# Proguard folder generated by Eclipse -proguard/ - -# Log Files -*.log - -# Android Studio Navigation editor temp files -.navigation/ - -# Android Studio captures folder -captures/ - -# Intellij -*.iml -.idea/ - -# Keystore files -*.jks - -# External native build folder generated in Android Studio 2.2 and later -.externalNativeBuild - -# Google Services (e.g. APIs or Firebase) -google-services.json - -### Android Patch ### -gen-external-apklibs +### Android ### +# Built application files +*.apk +*.ap_ + +# Files for the ART/Dalvik VM +*.dex + +# Java class files +*.class + +# Generated files +bin/ +gen/ +out/ + +# Gradle files +.gradle/ +build/ + +# Local configuration file (sdk path, etc) +local.properties + +# Proguard folder generated by Eclipse +proguard/ + +# Log Files +*.log + +# Android Studio Navigation editor temp files +.navigation/ + +# Android Studio captures folder +captures/ + +# Intellij +*.iml +.idea/ + +# Keystore files +*.jks + +# External native build folder generated in Android Studio 2.2 and later +.externalNativeBuild + +# Google Services (e.g. APIs or Firebase) +google-services.json + +### Android Patch ### +gen-external-apklibs diff --git a/CoolDudesAssignment/.gitignore b/CoolDudesAssignment/.gitignore new file mode 100644 index 0000000..47333d9 --- /dev/null +++ b/CoolDudesAssignment/.gitignore @@ -0,0 +1,50 @@ +### Android ### +# Built application files +*.apk +*.ap_ + +# Files for the ART/Dalvik VM +*.dex + +# Java class files +*.class + +# Generated files +bin/ +gen/ +out/ + +# Gradle files +.gradle/ +build/ + +# Local configuration file (sdk path, etc) +local.properties + +# Proguard folder generated by Eclipse +proguard/ + +# Log Files +*.log + +# Android Studio Navigation editor temp files +.navigation/ + +# Android Studio captures folder +captures/ + +# Intellij +*.iml +.idea/ + +# Keystore files +*.jks + +# External native build folder generated in Android Studio 2.2 and later +.externalNativeBuild + +# Google Services (e.g. APIs or Firebase) +google-services.json + +### Android Patch ### +gen-external-apklibs diff --git a/CoolDudesAssignment/app/.gitignore b/CoolDudesAssignment/app/.gitignore new file mode 100644 index 0000000..3543521 --- /dev/null +++ b/CoolDudesAssignment/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/app/build.gradle b/CoolDudesAssignment/app/build.gradle similarity index 78% rename from app/build.gradle rename to CoolDudesAssignment/app/build.gradle index fe0a5c9..0566196 100644 --- a/app/build.gradle +++ b/CoolDudesAssignment/app/build.gradle @@ -1,29 +1,30 @@ -apply plugin: 'com.android.application' - -android { - compileSdkVersion 25 - buildToolsVersion "25.0.2" - defaultConfig { - applicationId "com.sdsmdg.hareshkh.lectureassignment" - minSdkVersion 19 - targetSdkVersion 25 - versionCode 1 - versionName "1.0" - testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" - } - buildTypes { - release { - minifyEnabled false - proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' - } - } -} - -dependencies { - compile fileTree(dir: 'libs', include: ['*.jar']) - androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { - exclude group: 'com.android.support', module: 'support-annotations' - }) - compile 'com.android.support:appcompat-v7:25.1.0' - testCompile 'junit:junit:4.12' -} +apply plugin: 'com.android.application' + +android { + compileSdkVersion 25 + buildToolsVersion "25.0.2" + defaultConfig { + applicationId "com.rohan.musicplayer" + minSdkVersion 21 + targetSdkVersion 25 + versionCode 1 + versionName "1.0" + testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + compile fileTree(dir: 'libs', include: ['*.jar']) + androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { + exclude group: 'com.android.support', module: 'support-annotations' + }) + compile 'com.android.support:appcompat-v7:25.2.0' + compile 'com.github.d-max:spots-dialog:0.7@aar' + testCompile 'junit:junit:4.12' +} diff --git a/CoolDudesAssignment/app/proguard-rules.pro b/CoolDudesAssignment/app/proguard-rules.pro new file mode 100644 index 0000000..8a75ce1 --- /dev/null +++ b/CoolDudesAssignment/app/proguard-rules.pro @@ -0,0 +1,25 @@ +# Add project specific ProGuard rules here. +# By default, the flags in this file are appended to flags specified +# in C:\Users\Rohan\AppData\Local\Android\Sdk/tools/proguard/proguard-android.txt +# You can edit the include path and order by changing the proguardFiles +# directive in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# Add any project specific keep options here: + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile diff --git a/CoolDudesAssignment/app/src/androidTest/java/com/rohan/musicplayer/ExampleInstrumentedTest.java b/CoolDudesAssignment/app/src/androidTest/java/com/rohan/musicplayer/ExampleInstrumentedTest.java new file mode 100644 index 0000000..a5973e6 --- /dev/null +++ b/CoolDudesAssignment/app/src/androidTest/java/com/rohan/musicplayer/ExampleInstrumentedTest.java @@ -0,0 +1,26 @@ +package com.rohan.musicplayer; + +import android.content.Context; +import android.support.test.InstrumentationRegistry; +import android.support.test.runner.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.*; + +/** + * Instrumentation test, which will execute on an Android device. + * + * @see Testing documentation + */ +@RunWith(AndroidJUnit4.class) +public class ExampleInstrumentedTest { + @Test + public void useAppContext() throws Exception { + // Context of the app under test. + Context appContext = InstrumentationRegistry.getTargetContext(); + + assertEquals("com.rohan.musicplayer", appContext.getPackageName()); + } +} diff --git a/CoolDudesAssignment/app/src/main/AndroidManifest.xml b/CoolDudesAssignment/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..5df532e --- /dev/null +++ b/CoolDudesAssignment/app/src/main/AndroidManifest.xml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/CoolDudesAssignment/app/src/main/java/com/rohan/musicplayer/MainActivity.java b/CoolDudesAssignment/app/src/main/java/com/rohan/musicplayer/MainActivity.java new file mode 100644 index 0000000..7f75021 --- /dev/null +++ b/CoolDudesAssignment/app/src/main/java/com/rohan/musicplayer/MainActivity.java @@ -0,0 +1,108 @@ +package com.rohan.musicplayer; + +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.content.ServiceConnection; +import android.os.Bundle; +import android.os.IBinder; +import android.support.v7.app.AppCompatActivity; +import android.view.View; +import android.widget.Button; + +import dmax.dialog.SpotsDialog; + +public class MainActivity extends AppCompatActivity { + + MyService mService; + boolean mBound = false; + Button playButton; + Button pauseButton; + Button stopButton; + public SpotsDialog dialog; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + playButton = (Button) findViewById(R.id.play); + pauseButton = (Button) findViewById(R.id.pause); + stopButton = (Button) findViewById(R.id.stop); + + dialog = new SpotsDialog(this); + dialog.show(); + + Intent intent = new Intent(this, MyService.class); + bindService(intent, mConnection, Context.BIND_AUTO_CREATE); + + playButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (mBound) + mService.startPlayer(); + } + }); + pauseButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (mBound) + mService.pausePlayer(); + } + }); + stopButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (mBound) + mService.stopPlayer(); + } + }); + + } + + @Override + protected void onPause() { + super.onPause(); + if (mBound) + mService.showNotification(); + } + + @Override + protected void onPostResume() { + super.onPostResume(); + if (mBound) + mService.removeNotification(); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + if (mBound) { + unbindService(mConnection); + mBound = false; + } + } + + private ServiceConnection mConnection = new ServiceConnection() { + + @Override + public void onServiceConnected(ComponentName className, IBinder service) { + MyService.MyBinder binder = (MyService.MyBinder) service; + mService = binder.getService(); + binder.setListener(new MyService.BoundServiceListener() { + + @Override + public void finishedDownloading() { + dialog.dismiss(); + } + }); + mBound = true; + } + + @Override + public void onServiceDisconnected(ComponentName arg0) { + mBound = false; + } + }; + +} diff --git a/CoolDudesAssignment/app/src/main/java/com/rohan/musicplayer/MessageReceiver.java b/CoolDudesAssignment/app/src/main/java/com/rohan/musicplayer/MessageReceiver.java new file mode 100644 index 0000000..8168b47 --- /dev/null +++ b/CoolDudesAssignment/app/src/main/java/com/rohan/musicplayer/MessageReceiver.java @@ -0,0 +1,36 @@ +package com.rohan.musicplayer; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.os.Build; +import android.os.Bundle; +import android.telephony.SmsMessage; + +public class MessageReceiver extends BroadcastReceiver { + public void onReceive(Context context, Intent intent) { + Bundle myBundle = intent.getExtras(); + SmsMessage[] messages = null; + + if (myBundle != null) { + Object[] pdus = (Object[]) myBundle.get("pdus"); + + //String strMessage[] = new String[pdus.length]; + messages = new SmsMessage[pdus.length]; + + for (int i = 0; i < messages.length; i++) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + String format = myBundle.getString("format"); + messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i], format); + } else { + messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]); + } + Intent x = new Intent(context, MyService.class); + if (messages[i].getMessageBody().toLowerCase().trim().equals("play media")) { + context.startService(x); + } + + } + } + } +} \ No newline at end of file diff --git a/CoolDudesAssignment/app/src/main/java/com/rohan/musicplayer/MyService.java b/CoolDudesAssignment/app/src/main/java/com/rohan/musicplayer/MyService.java new file mode 100644 index 0000000..75e8af5 --- /dev/null +++ b/CoolDudesAssignment/app/src/main/java/com/rohan/musicplayer/MyService.java @@ -0,0 +1,146 @@ +package com.rohan.musicplayer; + +import android.app.Notification; +import android.app.PendingIntent; +import android.app.Service; +import android.content.Intent; +import android.media.AudioManager; +import android.media.MediaPlayer; +import android.os.AsyncTask; +import android.os.Binder; +import android.os.IBinder; +import android.support.annotation.Nullable; +import android.widget.Toast; + +import java.io.IOException; + + +public class MyService extends Service { + + private MediaPlayer mPlayer = new MediaPlayer(); + private boolean playerIsSet = false; + + public interface BoundServiceListener { + + public void finishedDownloading(); + } + + public class MyBinder extends Binder { + MyService getService() { + return MyService.this; + } + + public void setListener(BoundServiceListener listener) { + mListener = listener; + } + } + + private final IBinder mBinder = new MyBinder(); + private BoundServiceListener mListener; + + @Nullable + @Override + public IBinder onBind(Intent intent) { + if (!playerIsSet) { + new LoadDataInBackground().execute(); + } + return mBinder; + } + + @Override + public boolean onUnbind(Intent intent) { + mPlayer.stop(); + mPlayer.release(); + playerIsSet = false; + stopSelf(); + return super.onUnbind(intent); + } + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + if (!playerIsSet) { + setUpPlayer(); + playerIsSet = true; + } + if (!mPlayer.isPlaying()) + mPlayer.start(); + return super.onStartCommand(intent, flags, startId); + } + + private class LoadDataInBackground extends AsyncTask { + + @Override + protected Void doInBackground(Void... params) { + setUpPlayer(); + return null; + } + + @Override + protected void onPostExecute(Void aVoid) { + super.onPostExecute(aVoid); + playerIsSet = true; + if (mListener != null) + mListener.finishedDownloading(); + } + } + + public void setUpPlayer() { + mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); + try { + String URL = "http://dot.890m.com/shapeofyou.mp3"; + mPlayer.setDataSource(URL); + } catch (IllegalArgumentException e) { + Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show(); + } catch (SecurityException e) { + Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show(); + } catch (IllegalStateException e) { + Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show(); + } catch (IOException e) { + e.printStackTrace(); + } + try { + mPlayer.prepare(); + } catch (IllegalStateException e) { + Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show(); + } catch (IOException e) { + Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show(); + } + } + + public void startPlayer() { + mPlayer.start(); + } + + public void pausePlayer() { + mPlayer.pause(); + } + + public void stopPlayer() { + mPlayer.stop(); + try { + mPlayer.prepare(); + } catch (IllegalStateException e) { + Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show(); + } catch (IOException e) { + Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show(); + } + } + + public void showNotification() { + Intent notificationIntent = new Intent(this, MainActivity.class); + PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); + + Notification notification = new Notification.Builder(this) + .setContentTitle("Shape of You") + .setContentText("Tap to open app") + .setSmallIcon(R.mipmap.ic_launcher) + .setContentIntent(pendingIntent) + .setTicker("Music Player is running") + .build(); + startForeground(101, notification); + } + + public void removeNotification() { + stopForeground(true); + } +} diff --git a/CoolDudesAssignment/app/src/main/res/layout/activity_main.xml b/CoolDudesAssignment/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..0fc7e4c --- /dev/null +++ b/CoolDudesAssignment/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,33 @@ + + + +