summaryrefslogtreecommitdiffstats
path: root/android/src/org/rockbox/widgets/RockboxWidgetProvider.java
blob: beeb81c13b951e5763932721f4fcb052fe58a212 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2011 Antoine Cellerier <dionoea at videolan dot org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

package org.rockbox.widgets;

import java.io.File;
import org.rockbox.R;
import org.rockbox.RockboxActivity;
import org.rockbox.RockboxService;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.appwidget.AppWidgetProviderInfo;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RemoteViews;

public class RockboxWidgetProvider extends AppWidgetProvider
{
    static RockboxWidgetProvider mInstance;
    public RockboxWidgetProvider()
    {
        super();
        mInstance = this;
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
    {
        final int N = appWidgetIds.length;
        for (int i = 0; i < N; i++)
        {
            int appWidgetId = appWidgetIds[i];
            updateAppWidget(context, appWidgetManager, appWidgetId, null);

        }
    }
    
    public static RockboxWidgetProvider getInstance()
    {   
        /* no new instance here, instanced by android */
        return mInstance;
    }

    @Override
    public void onDeleted(Context context, int[] appWidgetIds)
    {
    }

    @Override
    public void onEnabled(Context context)
    {
    }

    @Override
    public void onDisabled(Context context)
    {
    }

    @Override
    public void onReceive(Context context, Intent intent)
    {
        String action = intent.getAction();
        if (action.equals("org.rockbox.TrackUpdateInfo") ||
            action.equals("org.rockbox.TrackFinish") ||
            action.equals("org.rockbox.UpdateState"))
        {
            AppWidgetManager gm = AppWidgetManager.getInstance(context);
            int[] appWidgetIds = gm.getAppWidgetIds(new ComponentName(context, this.getClass()));
            final int N = appWidgetIds.length;
            for (int i = 0; i < N; i++)
            {
                updateAppWidget(context, gm, appWidgetIds[i], intent);
            }
        }
        else
        {
            super.onReceive(context, intent);
        }
    }

     public void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Intent args)
     {
        AppWidgetProviderInfo provider = appWidgetManager.getAppWidgetInfo(appWidgetId);
        RemoteViews views = null;
        views = new RemoteViews(context.getPackageName(), provider.initialLayout);

        Intent intent = new Intent(context, RockboxActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
        views.setOnClickPendingIntent(R.id.infoDisplay, pendingIntent);
        views.setOnClickPendingIntent(R.id.logo, pendingIntent);

        RockboxWidgetConfigure.WidgetPref state = RockboxWidgetConfigure.loadWidgetPref(context, appWidgetId);

        /* enable/disable PREV */
        if (state.enablePrev)
        {
            views.setOnClickPendingIntent(R.id.prev, 
                                          newPendingIntent(context,
                                            KeyEvent.KEYCODE_MEDIA_PREVIOUS));
        }
        else
            views.setViewVisibility(R.id.prev, View.GONE);

        /* enable/disable PLAY/PAUSE */
        if (state.enablePlayPause)
        {
            views.setOnClickPendingIntent(R.id.playPause, 
                                          newPendingIntent(context,
                                            KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE));
        }
        else
            views.setViewVisibility(R.id.playPause, View.GONE);

        /* enable/disable NEXT */
        if (state.enableNext)
        {
            views.setOnClickPendingIntent(R.id.next, 
                                          newPendingIntent(context,
                                            KeyEvent.KEYCODE_MEDIA_NEXT));
        }
        else
            views.setViewVisibility(R.id.next, View.GONE);

        /* enable/disable STOP */
        if (state.enableStop)
        {
            views.setOnClickPendingIntent(R.id.stop, 
                                          newPendingIntent(context,
                                            KeyEvent.KEYCODE_MEDIA_STOP));
        }
        else
            views.setViewVisibility(R.id.stop, View.GONE);

        if (!state.enableAA)
            views.setViewVisibility(R.id.logo, View.GONE);

        if (args != null)
        {
            if (args.getAction().equals("org.rockbox.TrackUpdateInfo"))
            {
                CharSequence title = args.getCharSequenceExtra("title");
                CharSequence artist = args.getCharSequenceExtra("artist");
                CharSequence album = args.getCharSequenceExtra("album");
                views.setTextViewText(R.id.infoDisplay, title+"\n"+artist+"\n"+album);
                CharSequence albumart = args.getCharSequenceExtra("albumart");
                if (albumart != null)
                    views.setImageViewUri(R.id.logo, Uri.fromFile(new File(albumart.toString())));
                else
                    views.setImageViewResource(R.id.logo, R.drawable.rockbox);
            }
            else if (args.getAction().equals("org.rockbox.TrackFinish"))
            {
                // FIXME: looks like this event is always fired earlier than
                // the actual track change (a few seconds)
                views.setTextViewText(R.id.infoDisplay, context.getString(R.string.appwidget_infoDisplay));
                views.setImageViewResource(R.id.logo, R.drawable.rockbox);
            }
            else if (args.getAction().equals("org.rockbox.UpdateState"))
            {
                CharSequence playstate = args.getCharSequenceExtra("state");
                if (playstate.equals("play"))
                    views.setImageViewResource(R.id.playPause, R.drawable.appwidget_pause);
                else /* pause or stop */
                    views.setImageViewResource(R.id.playPause, R.drawable.appwidget_play);
            }
        }

        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
     
    public static PendingIntent newPendingIntent(Context context, int keycode)
    {
        /* Use keycode as request to code to prevent successive
         * PendingIntents  from overwritting one another.
         * This seems hackish but at least it works.
         * see: http://code.google.com/p/android/issues/detail?id=863
         */
        Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON, Uri.EMPTY,
                                   context, RockboxService.class);
        intent.putExtra(Intent.EXTRA_KEY_EVENT,
                        new KeyEvent(KeyEvent.ACTION_UP, keycode));
        return PendingIntent.getService(context, keycode, intent, 0);
    }
}