Skip to content

Commit

Permalink
Add a 1x4 widget
Browse files Browse the repository at this point in the history
  • Loading branch information
kreed committed May 27, 2010
1 parent cf34354 commit 3e186a2
Show file tree
Hide file tree
Showing 10 changed files with 356 additions and 5 deletions.
10 changes: 10 additions & 0 deletions AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
<receiver
android:name=".FourLongWidget"
android:label="Vanilla Music 1x4">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/four_long_widget" />
</receiver>
<receiver android:name="MediaButtonReceiver" >
<intent-filter android:priority="1000">
<action android:name="android.intent.action.MEDIA_BUTTON" />
Expand Down
Binary file added res/drawable/appwidget_bg.9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable/appwidget_divider.9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions res/drawable/button_background.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2010 Christopher Eby <kreed@kreed.org>
This file is part of Vanilla Music Player.
Vanilla Music Player is free software; you can redistribute it and/or modify
it under the terms of the GNU Library General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
Vanilla Music Player is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="@drawable/button_background_focused" />
<item
android:state_focused="true"
android:drawable="@drawable/button_background_focused" />
<item android:drawable="@android:color/transparent" />
</selector>
25 changes: 25 additions & 0 deletions res/drawable/button_background_focused.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2010 Christopher Eby <kreed@kreed.org>
This file is part of Vanilla Music Player.
Vanilla Music Player is free software; you can redistribute it and/or modify
it under the terms of the GNU Library General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
Vanilla Music Player is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:endColor="#A9D2E2"
android:startColor="#274162"
android:angle="90" />
</shape>
112 changes: 112 additions & 0 deletions res/layout/four_long_widget.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2010 Christopher Eby <kreed@kreed.org>
This file is part of Vanilla Music Player.
Vanilla Music Player is free software; you can redistribute it and/or modify
it under the terms of the GNU Library General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
Vanilla Music Player is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:clickable="true"
android:background="@drawable/appwidget_bg">
<ImageView
android:id="@+id/cover"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:padding="4dip"
android:scaleType="fitCenter" />
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:layout_gravity="center"
android:orientation="vertical"
android:paddingLeft="8dip"
android:paddingRight="8dip">
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textStyle="bold"
android:textSize="13dip"
android:singleLine="true"
android:fadingEdge="horizontal"
android:fadingEdgeLength="10dip"
android:ellipsize="none" />
<TextView
android:id="@+id/artist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="13dip"
android:singleLine="true"
android:fadingEdge="horizontal"
android:fadingEdgeLength="10dip"
android:ellipsize="none" />
</LinearLayout>
<ImageView
android:layout_height="1dip"
android:layout_width="fill_parent"
android:background="@drawable/appwidget_divider" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1">
<ImageButton
android:id="@+id/previous"
android:background="@drawable/button_background"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent"
android:src="@drawable/previous"
android:scaleType="fitCenter" />
<ImageView
android:layout_width="1dip"
android:layout_height="fill_parent"
android:background="@drawable/appwidget_divider" />
<ImageButton
android:id="@+id/play"
android:background="@drawable/button_background"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent"
android:src="@drawable/play"
android:scaleType="fitCenter" />
<ImageView
android:layout_width="1dip"
android:layout_height="fill_parent"
android:background="@drawable/appwidget_divider" />
<ImageButton
android:id="@+id/next"
android:background="@drawable/button_background"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent"
android:src="@drawable/next"
android:scaleType="fitCenter" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
24 changes: 24 additions & 0 deletions res/xml/four_long_widget.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2010 Christopher Eby <kreed@kreed.org>
This file is part of Vanilla Music Player.
Vanilla Music Player is free software; you can redistribute it and/or modify
it under the terms of the GNU Library General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
Vanilla Music Player is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="294dip"
android:minHeight="72dip"
android:initialLayout="@layout/four_long_widget"
/>
1 change: 1 addition & 0 deletions src/org/kreed/vanilla/ContextApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ public static void removeActivity(Activity activity)
public static void broadcast(Intent intent)
{
OneCellWidget.receive(intent);
FourLongWidget.receive(intent);

ArrayList<Activity> list = mActivities;
if (list != null) {
Expand Down
147 changes: 147 additions & 0 deletions src/org/kreed/vanilla/FourLongWidget.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
* Copyright (C) 2010 Christopher Eby <kreed@kreed.org>
*
* This file is part of Vanilla Music Player.
*
* Vanilla Music Player is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Vanilla Music Player is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package org.kreed.vanilla;

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.content.res.Resources;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.RemoteViews;

/**
* Simple 1x4 widget to show currently playing song, album art, and play/pause,
* next, and previous buttons. Uses artwork from the default Android music
* widget.
*/
public class FourLongWidget extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager manager, int[] ids)
{
Song song;
int state;

if (ContextApplication.hasService()) {
PlaybackService service = ContextApplication.getService();
song = service.getSong(0);
state = service.getState();
} else {
SongTimeline timeline = new SongTimeline();
timeline.loadState(context);
song = timeline.getSong(0);
state = 0;

// If we generated a new current song (because the PlaybackService has
// never been started), then we need to save the state.
timeline.saveState(context, 0);
}

updateWidget(context, manager, ids, song, state);
}

/**
* Receive a broadcast sent by the PlaybackService and update the widget
* accordingly.
*
* @param intent The intent that was broadcast.
*/
public static void receive(Intent intent)
{
String action = intent.getAction();
if (PlaybackService.EVENT_CHANGED.equals(action) || PlaybackService.EVENT_REPLACE_SONG.equals(action)) {
Context context = ContextApplication.getContext();
Song song = intent.getParcelableExtra("song");
int state = intent.getIntExtra("state", -1);

AppWidgetManager manager = AppWidgetManager.getInstance(context);
int[] ids = manager.getAppWidgetIds(new ComponentName(context, FourLongWidget.class));
updateWidget(context, manager, ids, song, state);
}
}

/**
* Populate the widgets with the given ids with the given info.
*
* @param context A Context to use.
* @param manager The AppWidgetManager that will be used to update the
* widget.
* @param ids An array containing the ids of all the widgets to update.
* @param song The current Song in PlaybackService.
* @param state The current PlaybackService state.
*/
public static void updateWidget(Context context, AppWidgetManager manager, int[] ids, Song song, int state)
{
if (ids == null || ids.length == 0)
return;

Resources res = context.getResources();
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.four_long_widget);

Bitmap cover = null;

if (song == null) {
views.setViewVisibility(R.id.title, View.GONE);
views.setTextViewText(R.id.artist, res.getText(R.string.no_songs));
} else {
views.setViewVisibility(R.id.title, View.VISIBLE);
views.setTextViewText(R.id.title, song.title);
views.setTextViewText(R.id.artist, song.artist);
cover = song.getCover();
}

if (cover == null) {
views.setViewVisibility(R.id.cover, View.GONE);
} else {
views.setViewVisibility(R.id.cover, View.VISIBLE);
views.setImageViewBitmap(R.id.cover, cover);
}

if (state != -1) {
boolean playing = (state & PlaybackService.FLAG_PLAYING) != 0;
views.setImageViewResource(R.id.play, playing ? R.drawable.pause : R.drawable.play);
}

Intent intent;
PendingIntent pendingIntent;

ComponentName service = new ComponentName(context, PlaybackService.class);

intent = new Intent(context, LaunchActivity.class);
pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.layout, pendingIntent);

intent = new Intent(PlaybackService.ACTION_PREVIOUS_SONG).setComponent(service);
pendingIntent = PendingIntent.getService(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.previous, pendingIntent);

intent = new Intent(PlaybackService.ACTION_TOGGLE_PLAYBACK).setComponent(service);
pendingIntent = PendingIntent.getService(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.play, pendingIntent);

intent = new Intent(PlaybackService.ACTION_NEXT_SONG).setComponent(service);
pendingIntent = PendingIntent.getService(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.next, pendingIntent);

manager.updateAppWidget(ids, views);
}
}
15 changes: 10 additions & 5 deletions src/org/kreed/vanilla/OneCellWidget.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ public static void receive(Intent intent)
*/
public static void updateWidget(Context context, AppWidgetManager manager, int[] ids, Song song, int state)
{
for (int i = 0; i != ids.length; ++i) {
if (ids == null || ids.length == 0)
return;

for (int i = ids.length; --i != -1; ) {
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
boolean doubleTap = settings.getBoolean("double_tap_" + ids[i], false);

Expand All @@ -101,12 +104,14 @@ public static void updateWidget(Context context, AppWidgetManager manager, int[]
views.setImageViewResource(R.id.play_pause, playing ? R.drawable.hidden_pause : R.drawable.hidden_play);
}

Intent playPause = new Intent(context, PlaybackService.class);
playPause.setAction(doubleTap ? PlaybackService.ACTION_TOGGLE_PLAYBACK_DELAYED : PlaybackService.ACTION_TOGGLE_PLAYBACK);
ComponentName service = new ComponentName(context, PlaybackService.class);

Intent playPause = new Intent(doubleTap ? PlaybackService.ACTION_TOGGLE_PLAYBACK_DELAYED : PlaybackService.ACTION_TOGGLE_PLAYBACK);
playPause.setComponent(service);
views.setOnClickPendingIntent(R.id.play_pause, PendingIntent.getService(context, 0, playPause, 0));

Intent next = new Intent(context, PlaybackService.class);
next.setAction(doubleTap ? PlaybackService.ACTION_NEXT_SONG_DELAYED : PlaybackService.ACTION_NEXT_SONG);
Intent next = new Intent(doubleTap ? PlaybackService.ACTION_NEXT_SONG_DELAYED : PlaybackService.ACTION_NEXT_SONG);
next.setComponent(service);
views.setOnClickPendingIntent(R.id.next, PendingIntent.getService(context, 0, next, 0));

if (song == null) {
Expand Down

0 comments on commit 3e186a2

Please sign in to comment.