Search in sources :

Example 21 with RemoteViews

use of android.widget.RemoteViews in project Notes by MiCode.

the class NoteWidgetProvider method update.

private void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds, boolean privacyMode) {
    for (int i = 0; i < appWidgetIds.length; i++) {
        if (appWidgetIds[i] != AppWidgetManager.INVALID_APPWIDGET_ID) {
            int bgId = ResourceParser.getDefaultBgId(context);
            String snippet = "";
            Intent intent = new Intent(context, NoteEditActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            intent.putExtra(Notes.INTENT_EXTRA_WIDGET_ID, appWidgetIds[i]);
            intent.putExtra(Notes.INTENT_EXTRA_WIDGET_TYPE, getWidgetType());
            Cursor c = getNoteWidgetInfo(context, appWidgetIds[i]);
            if (c != null && c.moveToFirst()) {
                if (c.getCount() > 1) {
                    Log.e(TAG, "Multiple message with same widget id:" + appWidgetIds[i]);
                    c.close();
                    return;
                }
                snippet = c.getString(COLUMN_SNIPPET);
                bgId = c.getInt(COLUMN_BG_COLOR_ID);
                intent.putExtra(Intent.EXTRA_UID, c.getLong(COLUMN_ID));
                intent.setAction(Intent.ACTION_VIEW);
            } else {
                snippet = context.getResources().getString(R.string.widget_havenot_content);
                intent.setAction(Intent.ACTION_INSERT_OR_EDIT);
            }
            if (c != null) {
                c.close();
            }
            RemoteViews rv = new RemoteViews(context.getPackageName(), getLayoutId());
            rv.setImageViewResource(R.id.widget_bg_image, getBgResourceId(bgId));
            intent.putExtra(Notes.INTENT_EXTRA_BACKGROUND_ID, bgId);
            /**
                 * Generate the pending intent to start host for the widget
                 */
            PendingIntent pendingIntent = null;
            if (privacyMode) {
                rv.setTextViewText(R.id.widget_text, context.getString(R.string.widget_under_visit_mode));
                pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i], new Intent(context, NotesListActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
            } else {
                rv.setTextViewText(R.id.widget_text, snippet);
                pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i], intent, PendingIntent.FLAG_UPDATE_CURRENT);
            }
            rv.setOnClickPendingIntent(R.id.widget_text, pendingIntent);
            appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
        }
    }
}
Also used : RemoteViews(android.widget.RemoteViews) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent) Cursor(android.database.Cursor)

Example 22 with RemoteViews

use of android.widget.RemoteViews in project Signal-Android by WhisperSystems.

the class KeyCachingService method foregroundServiceICS.

private void foregroundServiceICS() {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.key_caching_notification);
    remoteViews.setOnClickPendingIntent(R.id.lock_cache_icon, buildLockIntent());
    builder.setSmallIcon(R.drawable.icon_cached);
    builder.setContent(remoteViews);
    builder.setContentIntent(buildLaunchIntent());
    stopForeground(true);
    startForeground(SERVICE_RUNNING_ID, builder.build());
}
Also used : RemoteViews(android.widget.RemoteViews) NotificationCompat(android.support.v4.app.NotificationCompat)

Example 23 with RemoteViews

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;
}
Also used : ImageRequestBuilder(com.facebook.imagepipeline.request.ImageRequestBuilder) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) CloseableImage(com.facebook.imagepipeline.image.CloseableImage) Uri(android.net.Uri) SuppressLint(android.annotation.SuppressLint) FileNotFoundException(java.io.FileNotFoundException) RemoteException(android.os.RemoteException) IOException(java.io.IOException) DataSource(com.facebook.datasource.DataSource) BaseBitmapDataSubscriber(com.facebook.imagepipeline.datasource.BaseBitmapDataSubscriber) RemoteViews(android.widget.RemoteViews) Bitmap(android.graphics.Bitmap) ImageRequest(com.facebook.imagepipeline.request.ImageRequest) NotificationCompat(android.support.v4.app.NotificationCompat) ComponentName(android.content.ComponentName) PendingIntent(android.app.PendingIntent) ImagePipeline(com.facebook.imagepipeline.core.ImagePipeline) Nullable(android.support.annotation.Nullable)

Example 24 with RemoteViews

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);
}
Also used : CloseableReference(com.facebook.common.references.CloseableReference) Uri(android.net.Uri) BaseBitmapDataSubscriber(com.facebook.imagepipeline.datasource.BaseBitmapDataSubscriber) DataSource(com.facebook.datasource.DataSource) RemoteViews(android.widget.RemoteViews) Bitmap(android.graphics.Bitmap) ImageRequest(com.facebook.imagepipeline.request.ImageRequest) ComponentName(android.content.ComponentName) ImagePipeline(com.facebook.imagepipeline.core.ImagePipeline)

Example 25 with RemoteViews

use of android.widget.RemoteViews in project Android-Developers-Samples by johnjohndoe.

the class MainActivity method createNotification.

/**
     * This sample demonstrates notifications with custom content views.
     *
     * <p>On API level 16 and above a big content view is also defined that is used for the
     * 'expanded' notification. The notification is created by the NotificationCompat.Builder.
     * The expanded content view is set directly on the {@link android.app.Notification} once it has been build.
     * (See {@link android.app.Notification#bigContentView}.) </p>
     *
     * <p>The content views are inflated as {@link android.widget.RemoteViews} directly from their XML layout
     * definitions using {@link android.widget.RemoteViews#RemoteViews(String, int)}.</p>
     */
private void createNotification() {
    // BEGIN_INCLUDE(notificationCompat)
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    // END_INCLUDE(notificationCompat)
    // BEGIN_INCLUDE(intent)
    //Create Intent to launch this Activity again if the notification is clicked.
    Intent i = new Intent(this, MainActivity.class);
    i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(intent);
    // END_INCLUDE(intent)
    // BEGIN_INCLUDE(ticker)
    // Sets the ticker text
    builder.setTicker(getResources().getString(R.string.custom_notification));
    // Sets the small icon for the ticker
    builder.setSmallIcon(R.drawable.ic_stat_custom);
    // END_INCLUDE(ticker)
    // BEGIN_INCLUDE(buildNotification)
    // Cancel the notification when clicked
    builder.setAutoCancel(true);
    // Build the notification
    Notification notification = builder.build();
    // END_INCLUDE(buildNotification)
    // BEGIN_INCLUDE(customLayout)
    // Inflate the notification layout as RemoteViews
    RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification);
    // Set text on a TextView in the RemoteViews programmatically.
    final String time = DateFormat.getTimeInstance().format(new Date()).toString();
    final String text = getResources().getString(R.string.collapsed, time);
    contentView.setTextViewText(R.id.textView, text);
    /* Workaround: Need to set the content view here directly on the notification.
         * NotificationCompatBuilder contains a bug that prevents this from working on platform
         * versions HoneyComb.
         * See https://code.google.com/p/android/issues/detail?id=30495
         */
    notification.contentView = contentView;
    // big content view set here is displayed.)
    if (Build.VERSION.SDK_INT >= 16) {
        // Inflate and set the layout for the expanded notification view
        RemoteViews expandedView = new RemoteViews(getPackageName(), R.layout.notification_expanded);
        notification.bigContentView = expandedView;
    }
    // END_INCLUDE(customLayout)
    // START_INCLUDE(notify)
    // Use the NotificationManager to show the notification
    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    nm.notify(0, notification);
// END_INCLUDE(notify)
}
Also used : RemoteViews(android.widget.RemoteViews) NotificationManager(android.app.NotificationManager) NotificationCompat(android.support.v4.app.NotificationCompat) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent) Notification(android.app.Notification) Date(java.util.Date)

Aggregations

RemoteViews (android.widget.RemoteViews)211 Intent (android.content.Intent)83 PendingIntent (android.app.PendingIntent)75 TextView (android.widget.TextView)35 Test (org.junit.Test)35 View (android.view.View)32 Notification (android.app.Notification)30 SmallTest (android.support.test.filters.SmallTest)30 ImageView (android.widget.ImageView)28 AppWidgetManager (android.appwidget.AppWidgetManager)25 ComponentName (android.content.ComponentName)24 Bitmap (android.graphics.Bitmap)24 Bundle (android.os.Bundle)16 NavigationBarView (com.android.systemui.statusbar.phone.NavigationBarView)15 RemoteInputView (com.android.systemui.statusbar.policy.RemoteInputView)15 NotificationCompat (android.support.v4.app.NotificationCompat)14 Resources (android.content.res.Resources)12 StatusBarNotification (android.service.notification.StatusBarNotification)12 SharedPreferences (android.content.SharedPreferences)11 NotificationPanelView (com.android.systemui.statusbar.phone.NotificationPanelView)11