forked from Pra-San/Music-Player
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from samimshoaib01/widget
Home Screen Widget #2
- Loading branch information
Showing
8 changed files
with
149 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,4 +81,3 @@ public int getItemCount() { | |
return localdataset.size(); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -169,4 +169,3 @@ protected void onDestroy() { | |
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
app/src/main/java/com/example/musicplayer/MusicWidgetProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" /> |