Commit e34508af authored by Edward Wang's avatar Edward Wang Committed by Jean-Baptiste Kempf

Add preliminary widget feature

Signed-off-by: Jean-Baptiste Kempf's avatarJean-Baptiste Kempf <jb@videolan.org>
parent dbe4cf6a
......@@ -75,6 +75,19 @@
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
<receiver
android:name=".widget.VLCAppWidgetProvider"
android:label="VLC mini player" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="org.videolan.vlc.android.widget.VLCAppWidgetProvider.ACTION_WIDGET_RECEIVER"/>
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/vlcwidget" />
</receiver>
</application>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
......
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg_with_shadow"
android:orientation="vertical"
android:layout_margin="10dp"
android:layout_gravity="center" android:clipChildren="true" android:padding="10dp" android:gravity="center" android:clickable="true">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="50dip"
android:layout_height="50dp"
android:layout_marginRight="10dp"
android:background="@drawable/cone" />
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="14.50"
android:orientation="vertical" >
<TextView
android:id="@+id/songName"
android:layout_width="269dp"
android:layout_height="wrap_content"
android:text="Song Name"
android:textSize="22sp" android:ellipsize="end"/>
<TextView
android:id="@+id/artist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Artist"
android:textSize="16sp" android:ellipsize="end" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" android:layout_marginTop="6dp">
<ImageButton
android:id="@+id/backward"
android:layout_width="50dip"
android:layout_height="50dip"
android:clickable="true"
android:scaleType="fitXY"
android:src="@drawable/ic_backward" />
<ImageButton
android:id="@+id/play_pause"
android:layout_width="50dip"
android:layout_height="50dip"
android:src="@drawable/ic_play"
android:clickable="true"
android:scaleType="fitXY" />
<ImageButton
android:id="@+id/stop"
android:layout_width="50dip"
android:layout_height="50dip"
android:clickable="true"
android:scaleType="fitXY"
android:src="@drawable/ic_stop" />
<ImageButton
android:id="@+id/forward"
android:layout_width="50dip"
android:layout_height="50dip"
android:src="@drawable/ic_forward"
android:clickable="true"
android:scaleType="fitXY" />
</LinearLayout>
</LinearLayout>
\ No newline at end of file
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="260dp"
android:minHeight="110dp"
android:updatePeriodMillis="86400000"
android:initialLayout="@layout/vlcwidget">
</appwidget-provider>
\ No newline at end of file
......@@ -4,8 +4,10 @@ import java.util.ArrayList;
import java.util.List;
import org.videolan.vlc.android.AudioPlayer.AudioPlayerControl;
import org.videolan.vlc.android.widget.VLCAppWidgetProvider;
import org.videolan.vlc.android.AudioPlayer;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
......@@ -14,6 +16,7 @@ import android.graphics.Bitmap;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.widget.RemoteViews;
public class AudioServiceController implements AudioPlayerControl {
public static final String TAG = "VLC/AudioServiceContoller";
......@@ -31,10 +34,13 @@ public class AudioServiceController implements AudioPlayerControl {
}
};
private AudioServiceController() {
private AudioServiceController(Context c) {
// Get context from MainActivity
mContext = MainActivity.getInstance();
if(c != null)
mContext = c;
else
mContext = MainActivity.getInstance();
mAudioPlayer = new ArrayList<AudioPlayer>();
......@@ -63,8 +69,12 @@ public class AudioServiceController implements AudioPlayerControl {
}
public static AudioServiceController getInstance() {
return getInstance(null);
}
public static AudioServiceController getInstance(Context context) {
if (mInstance == null) {
mInstance = new AudioServiceController();
mInstance = new AudioServiceController(context);
}
if (!mIsBound) {
mInstance.bindAudioService();
......@@ -135,6 +145,12 @@ public class AudioServiceController implements AudioPlayerControl {
* Update all AudioPlayer
*/
private void updateAudioPlayer() {
/* Update widget */
ComponentName componentName = new ComponentName(mContext, VLCAppWidgetProvider.class);
RemoteViews views = new RemoteViews("org.videolan.vlc.android", R.layout.vlcwidget);
AppWidgetManager.getInstance(mContext).updateAppWidget(componentName, views);
/* Update audio players */
for (int i = 0; i < mAudioPlayer.size(); i++)
mAudioPlayer.get(i).update();
}
......
package org.videolan.vlc.android.widget;
import org.videolan.vlc.android.AudioPlayer;
import org.videolan.vlc.android.AudioServiceController;
import org.videolan.vlc.android.MainActivity;
import org.videolan.vlc.android.R;
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.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;
public class VLCAppWidgetProvider extends AppWidgetProvider {
public static final String TAG = "VLC/VLCAppWidgetProvider";
public static String ACTION_WIDGET_BACKWARD = "ActionReceiverBack";
public static String ACTION_WIDGET_PLAY = "ActionReceiverPlaypause";
public static String ACTION_WIDGET_STOP = "ActionReceiverStop";
public static String ACTION_WIDGET_FORWARD = "ActionReceiverForward";
private RemoteViews views = new RemoteViews("org.videolan.vlc.android", R.layout.vlcwidget);
private AudioServiceController controller = AudioServiceController.getInstance();
@Override
public void onEnabled(Context context) {
AppWidgetManager a = AppWidgetManager.getInstance(context);
this.onUpdate(context, a, a.getAppWidgetIds(new ComponentName(context, org.videolan.vlc.android.widget.VLCAppWidgetProvider.class)));
super.onEnabled(context);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
ComponentName cn = new ComponentName(context, VLCAppWidgetProvider.class);
appWidgetManager.updateAppWidget(cn, buildUpdate(context, appWidgetIds));
}
private RemoteViews buildUpdate(Context context, int[] appWidgetIds) {
Intent iBackward = new Intent(context, VLCAppWidgetProvider.class);
iBackward.setAction(ACTION_WIDGET_BACKWARD);
Intent iPlay = new Intent(context, VLCAppWidgetProvider.class);
iPlay.setAction(ACTION_WIDGET_PLAY);
Intent iStop = new Intent(context, VLCAppWidgetProvider.class);
iStop.setAction(ACTION_WIDGET_STOP);
Intent iForward = new Intent(context, VLCAppWidgetProvider.class);
iForward.setAction(ACTION_WIDGET_FORWARD);
PendingIntent piBackward = PendingIntent.getBroadcast(context, 0, iBackward, 0);
PendingIntent piPlay = PendingIntent.getBroadcast(context, 0, iPlay, 0);
PendingIntent piStop = PendingIntent.getBroadcast(context, 0, iStop, 0);
PendingIntent piForward = PendingIntent.getBroadcast(context, 0, iForward, 0);
views.setOnClickPendingIntent(R.id.backward, piBackward);
views.setOnClickPendingIntent(R.id.play_pause, piPlay);
views.setOnClickPendingIntent(R.id.stop, piStop);
views.setOnClickPendingIntent(R.id.forward, piForward);
return views;
}
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if(action.equals(ACTION_WIDGET_BACKWARD)) {
controller.previous();
} else if(action.equals(ACTION_WIDGET_STOP)) {
controller.stop();
views.setImageViewResource(R.id.play_pause, R.drawable.ic_play);
} else if(action.equals(ACTION_WIDGET_FORWARD)) {
controller.next();
} else if(action.equals(ACTION_WIDGET_PLAY)) {
if(controller.isPlaying()) {
controller.pause();
views.setImageViewResource(R.id.play_pause, R.drawable.ic_play);
} else {
if(!controller.hasMedia()) {
/* Causes a NDK crash, don't un-comment yet
* This is because the LibVLC pointer is not initalised properly */
/*updateTexts();
ComponentName cn = new ComponentName(context, VLCAppWidgetProvider.class);
AppWidgetManager.getInstance(context).updateAppWidget(cn, views);
Intent x = new Intent(context, MainActivity.class);
x.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
context.startActivity(x);
return;*/
Toast.makeText(context, "No media loaded, open VLC first", Toast.LENGTH_SHORT).show();
} else {
controller.play();
views.setImageViewResource(R.id.play_pause, R.drawable.ic_pause);
}
}
}
updateTexts();
ComponentName cn = new ComponentName(context, VLCAppWidgetProvider.class);
AppWidgetManager.getInstance(context).updateAppWidget(cn, views);
super.onReceive(context, intent);
}
private void updateTexts() {
Log.d(TAG, "Updating texts in widget");
if(controller.hasMedia()) {
views.setTextViewText(R.id.songName, controller.getTitle());
views.setTextViewText(R.id.artist, controller.getArtist());
if(controller.isPlaying()) {
views.setImageViewResource(R.id.play_pause, R.drawable.ic_pause);
} else {
views.setImageViewResource(R.id.play_pause, R.drawable.ic_play);
}
} else {
views.setTextViewText(R.id.songName, "VLC mini player");
views.setTextViewText(R.id.artist, "");
views.setImageViewResource(R.id.play_pause, R.drawable.ic_play);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment