Add a 1x4 widget

This commit is contained in:
Christopher Eby 2010-05-26 22:25:00 -05:00
parent cf34354bba
commit 3e186a2307
10 changed files with 356 additions and 5 deletions

View File

@ -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" />

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

View File

@ -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>

View File

@ -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>

View File

@ -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>

View File

@ -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"
/>

View File

@ -128,6 +128,7 @@ public class ContextApplication extends Application {
public static void broadcast(Intent intent)
{
OneCellWidget.receive(intent);
FourLongWidget.receive(intent);
ArrayList<Activity> list = mActivities;
if (list != null) {

View File

@ -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);
}
}

View File

@ -90,7 +90,10 @@ public class OneCellWidget extends AppWidgetProvider {
*/
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);
@ -101,12 +104,14 @@ public class OneCellWidget extends AppWidgetProvider {
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) {