use of android.widget.RemoteViews in project remusic by aa112901.
the class MediaService method getNotification.
private Notification getNotification() {
RemoteViews remoteViews;
final int PAUSE_FLAG = 0x1;
final int NEXT_FLAG = 0x2;
final int STOP_FLAG = 0x3;
final String albumName = getAlbumName();
final String artistName = getArtistName();
final boolean isPlaying = isPlaying();
remoteViews = new RemoteViews(this.getPackageName(), R.layout.notification);
String text = TextUtils.isEmpty(albumName) ? artistName : artistName + " - " + albumName;
remoteViews.setTextViewText(R.id.title, getTrackName());
remoteViews.setTextViewText(R.id.text, text);
//此处action不能是一样的 如果一样的 接受的flag参数只是第一个设置的值
Intent pauseIntent = new Intent(TOGGLEPAUSE_ACTION);
pauseIntent.putExtra("FLAG", PAUSE_FLAG);
PendingIntent pausePIntent = PendingIntent.getBroadcast(this, 0, pauseIntent, 0);
remoteViews.setImageViewResource(R.id.iv_pause, isPlaying ? R.drawable.note_btn_pause : R.drawable.note_btn_play);
remoteViews.setOnClickPendingIntent(R.id.iv_pause, pausePIntent);
Intent nextIntent = new Intent(NEXT_ACTION);
nextIntent.putExtra("FLAG", NEXT_FLAG);
PendingIntent nextPIntent = PendingIntent.getBroadcast(this, 0, nextIntent, 0);
remoteViews.setOnClickPendingIntent(R.id.iv_next, nextPIntent);
Intent preIntent = new Intent(STOP_ACTION);
preIntent.putExtra("FLAG", STOP_FLAG);
PendingIntent prePIntent = PendingIntent.getBroadcast(this, 0, preIntent, 0);
remoteViews.setOnClickPendingIntent(R.id.iv_stop, prePIntent);
// PendingIntent pendingIntent = PendingIntent.getActivity(this.getApplicationContext(), 0,
// new Intent(this.getApplicationContext(), PlayingActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
final Intent nowPlayingIntent = new Intent();
//nowPlayingIntent.setAction("com.wm.remusic.LAUNCH_NOW_PLAYING_ACTION");
nowPlayingIntent.setComponent(new ComponentName("com.wm.remusic", "com.wm.remusic.activity.PlayingActivity"));
nowPlayingIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent clickIntent = PendingIntent.getBroadcast(this, 0, nowPlayingIntent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent click = PendingIntent.getActivity(this, 0, nowPlayingIntent, PendingIntent.FLAG_UPDATE_CURRENT);
final Bitmap bitmap = ImageUtils.getArtworkQuick(this, getAlbumId(), 160, 160);
if (bitmap != null) {
remoteViews.setImageViewBitmap(R.id.image, bitmap);
// remoteViews.setImageViewUri(R.id.image, MusicUtils.getAlbumUri(this, getAudioId()));
mNoBit = null;
} else if (!isTrackLocal()) {
if (mNoBit != null) {
remoteViews.setImageViewBitmap(R.id.image, mNoBit);
mNoBit = null;
} else {
Uri uri = null;
if (getAlbumPath() != null) {
try {
uri = Uri.parse(getAlbumPath());
} catch (Exception e) {
e.printStackTrace();
}
}
if (getAlbumPath() == null || uri == null) {
mNoBit = BitmapFactory.decodeResource(getResources(), R.drawable.placeholder_disk_210);
updateNotification();
} else {
ImageRequest imageRequest = ImageRequestBuilder.newBuilderWithSource(uri).setProgressiveRenderingEnabled(true).build();
ImagePipeline imagePipeline = Fresco.getImagePipeline();
DataSource<CloseableReference<CloseableImage>> dataSource = imagePipeline.fetchDecodedImage(imageRequest, MediaService.this);
dataSource.subscribe(new BaseBitmapDataSubscriber() {
@Override
public void onNewResultImpl(@Nullable Bitmap bitmap) {
// No need to do any cleanup.
if (bitmap != null) {
mNoBit = bitmap;
}
updateNotification();
}
@Override
public void onFailureImpl(DataSource dataSource) {
// No cleanup required here.
mNoBit = BitmapFactory.decodeResource(getResources(), R.drawable.placeholder_disk_210);
updateNotification();
}
}, CallerThreadExecutor.getInstance());
}
}
} else {
remoteViews.setImageViewResource(R.id.image, R.drawable.placeholder_disk_210);
}
if (mNotificationPostTime == 0) {
mNotificationPostTime = System.currentTimeMillis();
}
if (mNotification == null) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setContent(remoteViews).setSmallIcon(R.drawable.ic_notification).setContentIntent(click).setWhen(mNotificationPostTime);
if (CommonUtils.isJellyBeanMR1()) {
builder.setShowWhen(false);
}
mNotification = builder.build();
} else {
mNotification.contentView = remoteViews;
}
return mNotification;
}
use of android.widget.RemoteViews in project remusic by aa112901.
the class SimpleWidgetProvider method pushUpdate.
// 更新所有的 widget
private synchronized void pushUpdate(final Context context, AppWidgetManager appWidgetManager, boolean updateProgress) {
pushAction(context, MediaService.SEND_PROGRESS);
RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.simple_control_widget_layout);
//将按钮与点击事件绑定
remoteView.setOnClickPendingIntent(R.id.widget_play, getPendingIntent(context, R.id.widget_play));
remoteView.setOnClickPendingIntent(R.id.widget_pre, getPendingIntent(context, R.id.widget_pre));
remoteView.setOnClickPendingIntent(R.id.widget_next, getPendingIntent(context, R.id.widget_next));
remoteView.setOnClickPendingIntent(R.id.widget_love, getPendingIntent(context, R.id.widget_love));
remoteView.setTextViewText(R.id.widget_content, trackname == null && art == null ? "" : trackname + "-" + art);
remoteView.setProgressBar(R.id.widget_progress, (int) duration, (int) position, false);
isFav = false;
long[] favlists = PlaylistsManager.getInstance(context).getPlaylistIds(IConstants.FAV_PLAYLIST);
for (long i : favlists) {
if (currentId == i) {
isFav = true;
break;
}
}
if (isFav) {
remoteView.setImageViewResource(R.id.widget_love, R.drawable.widget_unstar_selector);
} else {
remoteView.setImageViewResource(R.id.widget_love, R.drawable.widget_star_selector);
}
if (isPlaying) {
remoteView.setImageViewResource(R.id.widget_play, R.drawable.widget_pause_selector);
} else {
remoteView.setImageViewResource(R.id.widget_play, R.drawable.widget_play_selector);
}
if (updateProgress) {
if (albumuri == null) {
remoteView.setImageViewResource(R.id.widget_image, R.drawable.placeholder_disk_210);
} else {
if (isTrackLocal) {
Bitmap bitmap = ImageUtils.getArtworkQuick(context, Uri.parse(albumuri), 160, 160);
if (bitmap != null) {
remoteView.setImageViewBitmap(R.id.widget_image, bitmap);
} else {
remoteView.setImageViewResource(R.id.widget_image, R.drawable.placeholder_disk_210);
}
} else {
Bitmap bitmap = albumMap.get(albumuri);
if (bitmap != null)
remoteView.setImageViewBitmap(R.id.widget_image, bitmap);
}
}
} else {
if (albumuri == null) {
remoteView.setImageViewResource(R.id.widget_image, R.drawable.placeholder_disk_210);
} else {
if (isTrackLocal) {
final Bitmap bitmap = ImageUtils.getArtworkQuick(context, Uri.parse(albumuri), 160, 160);
if (bitmap != null) {
remoteView.setImageViewBitmap(R.id.widget_image, bitmap);
} else {
remoteView.setImageViewResource(R.id.widget_image, R.drawable.placeholder_disk_210);
}
albumMap.clear();
} else {
if (albumMap.get(albumuri) != null) {
remoteView.setImageViewBitmap(R.id.widget_image, albumMap.get(albumuri));
//noBit = null;
} else {
Uri uri = Uri.parse(albumuri);
if (uri == null) {
noBit = BitmapFactory.decodeResource(context.getResources(), R.drawable.placeholder_disk_210);
albumMap.put(albumuri, noBit);
pushUpdate(context, AppWidgetManager.getInstance(context), false);
} else {
ImageRequest imageRequest = ImageRequestBuilder.newBuilderWithSource(uri).setProgressiveRenderingEnabled(true).build();
ImagePipeline imagePipeline = Fresco.getImagePipeline();
DataSource<CloseableReference<CloseableImage>> dataSource = imagePipeline.fetchDecodedImage(imageRequest, this);
dataSource.subscribe(new BaseBitmapDataSubscriber() {
@Override
public void onNewResultImpl(@Nullable Bitmap bitmap) {
// No need to do any cleanup.
if (bitmap != null) {
noBit = bitmap.copy(bitmap.getConfig(), true);
albumMap.put(albumuri, noBit);
}
pushUpdate(context, AppWidgetManager.getInstance(context), false);
}
@Override
public void onFailureImpl(DataSource dataSource) {
// No cleanup required here.
noBit = BitmapFactory.decodeResource(context.getResources(), R.drawable.placeholder_disk_210);
albumMap.put(albumuri, noBit);
pushUpdate(context, AppWidgetManager.getInstance(context), false);
}
}, CallerThreadExecutor.getInstance());
}
}
}
}
}
// 相当于获得所有本程序创建的appwidget
ComponentName componentName = new ComponentName(context, SimpleWidgetProvider.class);
appWidgetManager.updateAppWidget(componentName, remoteView);
}
use of android.widget.RemoteViews in project SeriesGuide by UweTrottmann.
the class ListWidgetConfigure method onUpdateWidget.
private void onUpdateWidget() {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
RemoteViews views = ListWidgetProvider.buildRemoteViews(this, appWidgetManager, mAppWidgetId);
appWidgetManager.updateAppWidget(mAppWidgetId, views);
appWidgetManager.notifyAppWidgetViewDataChanged(mAppWidgetId, R.id.list_view);
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
setResult(RESULT_OK, resultValue);
finish();
}
use of android.widget.RemoteViews in project SeriesGuide by UweTrottmann.
the class ListWidgetProvider method buildRemoteViews.
public static RemoteViews buildRemoteViews(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
// setup intent pointing to RemoteViewsService providing the views for the collection
Intent intent = new Intent(context, ListWidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
// When intents are compared, the extras are ignored, so we need to
// embed the extras into the data so that the extras will not be
// ignored.
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
// determine layout (current size) and theme (user pref)
final boolean isCompactLayout = isCompactLayout(appWidgetManager, appWidgetId);
final boolean isLightTheme = WidgetSettings.isLightTheme(context, appWidgetId);
int layoutResId;
if (isLightTheme) {
layoutResId = isCompactLayout ? R.layout.appwidget_v11_light_compact : R.layout.appwidget_v11_light;
} else {
layoutResId = isCompactLayout ? R.layout.appwidget_v11_compact : R.layout.appwidget_v11;
}
// build widget views
RemoteViews rv = new RemoteViews(context.getPackageName(), layoutResId);
rv.setRemoteAdapter(R.id.list_view, intent);
// The empty view is displayed when the collection has no items. It
// should be a sibling of the collection view.
rv.setEmptyView(R.id.list_view, R.id.empty_view);
// set the background colors of...
// ...the header
boolean isDarkTheme = WidgetSettings.isDarkTheme(context, appWidgetId);
rv.setInt(R.id.containerWidgetHeader, "setBackgroundColor", isDarkTheme ? Color.TRANSPARENT : ContextCompat.getColor(context, R.color.accent_primary));
// ...the whole widget
int bgColor = WidgetSettings.getWidgetBackgroundColor(context, appWidgetId, isLightTheme);
rv.setInt(R.id.container, "setBackgroundColor", bgColor);
// determine type specific values
final int widgetType = WidgetSettings.getWidgetListType(context, appWidgetId);
int showsTabIndex;
int titleResId;
int emptyResId;
if (widgetType == WidgetSettings.Type.UPCOMING) {
// upcoming
showsTabIndex = ShowsActivity.InitBundle.INDEX_TAB_UPCOMING;
titleResId = R.string.upcoming;
emptyResId = R.string.noupcoming;
} else if (widgetType == WidgetSettings.Type.RECENT) {
// recent
showsTabIndex = ShowsActivity.InitBundle.INDEX_TAB_RECENT;
titleResId = R.string.recent;
emptyResId = R.string.norecent;
} else {
// shows
showsTabIndex = ShowsActivity.InitBundle.INDEX_TAB_SHOWS;
titleResId = R.string.shows;
emptyResId = R.string.no_nextepisode;
}
// change title and empty view based on type
rv.setTextViewText(R.id.empty_view, context.getString(emptyResId));
if (!isCompactLayout) {
// only regular layout has text title
rv.setTextViewText(R.id.widgetTitle, context.getString(titleResId));
}
// app launch button
final Intent appLaunchIntent = new Intent(context, ShowsActivity.class).putExtra(ShowsActivity.InitBundle.SELECTED_TAB, showsTabIndex);
PendingIntent pendingIntent = TaskStackBuilder.create(context).addNextIntent(appLaunchIntent).getPendingIntent(appWidgetId, PendingIntent.FLAG_UPDATE_CURRENT);
rv.setOnClickPendingIntent(R.id.widget_title, pendingIntent);
// item intent template, launches episode detail view
TaskStackBuilder builder = TaskStackBuilder.create(context);
builder.addNextIntent(appLaunchIntent);
builder.addNextIntent(new Intent(context, EpisodesActivity.class));
rv.setPendingIntentTemplate(R.id.list_view, builder.getPendingIntent(1, PendingIntent.FLAG_UPDATE_CURRENT));
// settings button
Intent settingsIntent = new Intent(context, ListWidgetConfigure.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS).putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
rv.setOnClickPendingIntent(R.id.widget_settings, PendingIntent.getActivity(context, appWidgetId, settingsIntent, PendingIntent.FLAG_UPDATE_CURRENT));
return rv;
}
use of android.widget.RemoteViews in project android_frameworks_base by ParanoidAndroid.
the class TestAppWidgetProvider method onReceive.
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.d(TAG, "intent=" + intent);
if (AppWidgetManager.ACTION_APPWIDGET_ENABLED.equals(action)) {
Log.d(TAG, "ENABLED");
} else if (AppWidgetManager.ACTION_APPWIDGET_DISABLED.equals(action)) {
Log.d(TAG, "DISABLED");
} else if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)) {
Log.d(TAG, "UPDATE");
// BEGIN_INCLUDE(getExtra_EXTRA_APPWIDGET_IDS)
Bundle extras = intent.getExtras();
int[] appWidgetIds = extras.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS);
// END_INCLUDE(getExtra_EXTRA_APPWIDGET_IDS)
SharedPreferences prefs = context.getSharedPreferences(TestAppWidgetProvider.PREFS_NAME, 0);
String prefix = prefs.getString(PREF_PREFIX_KEY, "hai");
AppWidgetManager gm = AppWidgetManager.getInstance(context);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.test_appwidget);
views.setTextViewText(R.id.oh_hai_text, prefix + ": " + SystemClock.elapsedRealtime());
if (false) {
gm.updateAppWidget(appWidgetIds, views);
} else {
gm.updateAppWidget(new ComponentName("com.android.tests.appwidgethost", "com.android.tests.appwidgethost.TestAppWidgetProvider"), views);
}
}
}
Aggregations