Skip to content

Commit

Permalink
Merge pull request #11 from samimshoaib01/widget
Browse files Browse the repository at this point in the history
Home Screen Widget #2
  • Loading branch information
Pra-San authored Nov 1, 2024
2 parents 4b098ea + 2837fe2 commit b12d96d
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 15 deletions.
30 changes: 21 additions & 9 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />


<application
android:allowBackup="true"
Expand All @@ -15,10 +17,26 @@
android:supportsRtl="true"
android:theme="@style/Theme.MusicPlayer"
tools:targetApi="31">

<!-- Music Service for background playback -->
<service
android:name=".MusicService"
android:enabled="true"
android:exported="true"></service>
android:exported="true" />

<!-- Widget Provider registration -->
<receiver
android:name=".MusicWidgetProvider"
android:exported="true">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="com.example.musicplayer.ACTION_WIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/music_widget_info" />
</receiver>


<activity
android:name=".PlaySong"
Expand All @@ -28,20 +46,14 @@
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.musicplayer.MainActivity" />
</activity>

<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
</application>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

</manifest>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,3 @@ public int getItemCount() {
return localdataset.size();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,3 @@ protected void onDestroy() {
}



43 changes: 40 additions & 3 deletions app/src/main/java/com/example/musicplayer/MusicService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
import android.widget.RemoteViews;

import androidx.core.app.NotificationCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.media.app.NotificationCompat.MediaStyle;
Expand All @@ -18,7 +23,6 @@
import java.util.ArrayList;

public class MusicService extends Service {

private MediaPlayer mediaPlayer;
private ArrayList<File> songList;
private int currentPosition = 0;
Expand All @@ -32,6 +36,7 @@ public class MusicService extends Service {
public static final String ACTION_PREVIOUS = "com.example.musicplayer.ACTION_PREVIOUS";
public static final String ACTION_STATUS_CHANGED = "com.example.musicplayer.ACTION_STATUS_CHANGED";
public static final String EXTRA_IS_PLAYING = "com.example.musicplayer.EXTRA_IS_PLAYING";
public static final String ACTION_WIDGET_UPDATE = "com.example.musicplayer.ACTION_WIDGET_UPDATE";

public class MusicBinder extends Binder {
public MusicService getService() {
Expand All @@ -50,6 +55,7 @@ public IBinder onBind(Intent intent) {
return binder;
}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null) {
Expand All @@ -72,9 +78,10 @@ public int onStartCommand(Intent intent, int flags, int startId) {
playSong();
}
}
return START_STICKY;
return START_NOT_STICKY;
}


private void playSong() {
if (mediaPlayer != null) {
mediaPlayer.release();
Expand All @@ -86,10 +93,12 @@ private void playSong() {
isPlaying = true;
sendSongChangedBroadcast();
showNotification();
updateWidgetPlayPauseStateDirectly();
});
mediaPlayer.setOnCompletionListener(mp -> playNext());
}


private void sendSongChangedBroadcast() {
Intent intent = new Intent(ACTION_SONG_CHANGED);
intent.putExtra(EXTRA_SONG_NAME, songList.get(currentPosition).getName().replace(".mp3", ""));
Expand All @@ -102,6 +111,12 @@ private void sendStatusChangedBroadcast() {
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}

private void updateWidgetPlayPauseState() {
Intent intent = new Intent(ACTION_WIDGET_UPDATE);
intent.putExtra(EXTRA_IS_PLAYING, isPlaying);
sendBroadcast(intent);
}

public void pauseOrResume() {
if (mediaPlayer != null) {
if (isPlaying) {
Expand All @@ -113,8 +128,21 @@ public void pauseOrResume() {
}
sendStatusChangedBroadcast();
showNotification();
updateWidgetPlayPauseStateDirectly();
}
}
private void updateWidgetPlayPauseStateDirectly() {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
ComponentName widget = new ComponentName(this, MusicWidgetProvider.class);
RemoteViews views = new RemoteViews(getPackageName(), R.layout.widget_layout);

views.setImageViewResource(R.id.btn_play_pause,
isPlaying ? R.drawable.ic_baseline_pause_24 : R.drawable.ic_baseline_play_arrow_24);

appWidgetManager.updateAppWidget(widget, views);
}



public void playNext() {
if (currentPosition < songList.size() - 1) {
Expand Down Expand Up @@ -184,11 +212,20 @@ private void createNotificationChannel() {
}
}

@Override
public void onTaskRemoved(Intent rootIntent) {
stopSelf();
super.onTaskRemoved(rootIntent);
}

@Override
public void onDestroy() {
super.onDestroy();
if (mediaPlayer != null) {
mediaPlayer.release();
mediaPlayer = null;
}
stopForeground(true);
super.onDestroy();
}

}
53 changes: 53 additions & 0 deletions app/src/main/java/com/example/musicplayer/MusicWidgetProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.example.musicplayer;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

public class MusicWidgetProvider extends AppWidgetProvider {
public static final String ACTION_WIDGET_PLAY_PAUSE = "com.example.musicplayer.ACTION_WIDGET_PLAY_PAUSE";
public static final String ACTION_WIDGET_NEXT = "com.example.musicplayer.ACTION_WIDGET_NEXT";
public static final String ACTION_WIDGET_PREVIOUS = "com.example.musicplayer.ACTION_WIDGET_PREVIOUS";
public static final String ACTION_WIDGET_UPDATE = "com.example.musicplayer.ACTION_WIDGET_UPDATE";
public static final String EXTRA_IS_PLAYING = "com.example.musicplayer.EXTRA_IS_PLAYING";


@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

Intent playPauseIntent = new Intent(context, MusicService.class).setAction(MusicService.ACTION_PLAY_PAUSE);
PendingIntent playPausePendingIntent = PendingIntent.getService(context, 0, playPauseIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
views.setOnClickPendingIntent(R.id.btn_play_pause, playPausePendingIntent);

Intent nextIntent = new Intent(context, MusicService.class).setAction(MusicService.ACTION_NEXT);
PendingIntent nextPendingIntent = PendingIntent.getService(context, 0, nextIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
views.setOnClickPendingIntent(R.id.btn_next, nextPendingIntent);

Intent previousIntent = new Intent(context, MusicService.class).setAction(MusicService.ACTION_PREVIOUS);
PendingIntent previousPendingIntent = PendingIntent.getService(context, 0, previousIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
views.setOnClickPendingIntent(R.id.btn_previous, previousPendingIntent);

appWidgetManager.updateAppWidget(appWidgetId, views);
}
}

@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (intent.getAction() != null) {
switch (intent.getAction()) {
case ACTION_WIDGET_PLAY_PAUSE:
Intent serviceIntent = new Intent(context, MusicService.class);
serviceIntent.setAction(MusicService.ACTION_PLAY_PAUSE);
context.startService(serviceIntent);
break;
}
}
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/notification_music_player.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
android:layout_width="48dp"
android:layout_height="48dp"
android:scaleType="centerCrop"
android:src="@drawable/baseline_album_24" />
android:src="@drawable/baseline_album_24" />

<LinearLayout
android:layout_width="0dp"
Expand Down
27 changes: 27 additions & 0 deletions app/src/main/res/layout/widget_layout.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!-- res/layout/widget_layout.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">

<ImageButton
android:id="@+id/btn_previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_baseline_skip_previous_24"
android:contentDescription="Previous" />

<ImageButton
android:id="@+id/btn_play_pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_baseline_play_arrow_24"
android:contentDescription="Play/Pause" />

<ImageButton
android:id="@+id/btn_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_baseline_skip_next_24"
android:contentDescription="Next" />
</LinearLayout>
7 changes: 7 additions & 0 deletions app/src/main/res/xml/music_widget_info.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!-- res/xml/music_widget_info.xml -->
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="250dp"
android:minHeight="50dp"
android:updatePeriodMillis="0"
android:initialLayout="@layout/widget_layout"
android:resizeMode="horizontal|vertical" />

0 comments on commit b12d96d

Please sign in to comment.