package com.nf2m.provider; import android.appwidget.AppWidgetProvider; import android.content.Context; import android.content.Intent; import android.support.annotation.NonNull; import com.nf2m.service.MediaPlayerService; public class PlayerWidgetSmallProvider extends AppWidgetProvider { public static final String ACTION_SMALL_WIDGET_PLAY = "action_small_widget_play"; public static final String ACTION_SMALL_WIDGET_NEXT = "action_small_widget_next"; public static final String ACTION_SMALL_WIDGET_PREVIOUS = "action_small_widget_previous"; public static final String EXTRA_WIDGET = "extra_widget"; public static final String VALUE_PLAY = "value_play"; public static final String VALUE_PAUSE = "value_pause"; public static final String VALUE_NEXT = "value_next"; public static final String VALUE_PREVIOUS = "value_previous"; private Context context; @Override public void onEnabled(Context context) { send(VALUE_PAUSE, MediaPlayerService.ACTION_UPDATE); } private void send(String value, String action) { if (!MediaPlayerService.isServiceStart) { startMediaPlayerService(value); } else { Intent intent = new Intent(action); context.sendBroadcast(intent); } } private void startMediaPlayerService(String value) { Intent intent = new Intent(context, MediaPlayerService.class); intent.putExtra(EXTRA_WIDGET, value); context.startService(intent); } @Override public void onReceive(Context context, @NonNull Intent intent) { this.context = context; if (intent.getAction().equalsIgnoreCase(ACTION_SMALL_WIDGET_PLAY)) { send(PlayerWidgetSmallProvider.VALUE_PLAY, MediaPlayerService.ACTION_PLAY); } else if (intent.getAction().equalsIgnoreCase(ACTION_SMALL_WIDGET_NEXT)) { send(PlayerWidgetSmallProvider.VALUE_NEXT, MediaPlayerService.ACTION_NEXT); } else if (intent.getAction().equalsIgnoreCase(ACTION_SMALL_WIDGET_PREVIOUS)) { send(PlayerWidgetSmallProvider.VALUE_PREVIOUS, MediaPlayerService.ACTION_PREVIOUS); } else if (intent.getAction().equalsIgnoreCase("android.appwidget.action.APPWIDGET_ENABLED")) { onEnabled(context); } } }