Example usage for android.widget RemoteViews RemoteViews

List of usage examples for android.widget RemoteViews RemoteViews

Introduction

In this page you can find the example usage for android.widget RemoteViews RemoteViews.

Prototype

public RemoteViews(RemoteViews landscape, RemoteViews portrait) 

Source Link

Document

Create a new RemoteViews object that will inflate as the specified landspace or portrait RemoteViews, depending on the current configuration.

Usage

From source file:com.amaze.filemanager.asynchronous.services.ZipService.java

@Override
public int onStartCommand(Intent intent, int flags, final int startId) {
    String mZipPath = intent.getStringExtra(KEY_COMPRESS_PATH);

    ArrayList<HybridFileParcelable> baseFiles = intent.getParcelableArrayListExtra(KEY_COMPRESS_FILES);

    File zipFile = new File(mZipPath);

    mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    if (!zipFile.exists()) {
        try {/*from w  w  w  .  ja v a  2  s  . com*/
            zipFile.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    accentColor = ((AppConfig) getApplication()).getUtilsProvider().getColorPreference()
            .getCurrentUserColorPreferences(this, sharedPreferences).accent;

    Intent notificationIntent = new Intent(this, MainActivity.class);
    notificationIntent.putExtra(MainActivity.KEY_INTENT_PROCESS_VIEWER, true);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    customSmallContentViews = new RemoteViews(getPackageName(), R.layout.notification_service_small);
    customBigContentViews = new RemoteViews(getPackageName(), R.layout.notification_service_big);

    Intent stopIntent = new Intent(KEY_COMPRESS_BROADCAST_CANCEL);
    PendingIntent stopPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1234, stopIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Action action = new NotificationCompat.Action(R.drawable.ic_zip_box_grey600_36dp,
            getString(R.string.stop_ftp), stopPendingIntent);

    mBuilder = new NotificationCompat.Builder(this, NotificationConstants.CHANNEL_NORMAL_ID)
            .setSmallIcon(R.drawable.ic_zip_box_grey600_36dp).setContentIntent(pendingIntent)
            .setCustomContentView(customSmallContentViews).setCustomBigContentView(customBigContentViews)
            .setCustomHeadsUpContentView(customSmallContentViews)
            .setStyle(new NotificationCompat.DecoratedCustomViewStyle()).addAction(action).setOngoing(true)
            .setColor(accentColor);

    NotificationConstants.setMetadata(this, mBuilder, NotificationConstants.TYPE_NORMAL);
    startForeground(NotificationConstants.ZIP_ID, mBuilder.build());
    initNotificationViews();

    super.onStartCommand(intent, flags, startId);
    super.progressHalted();
    asyncTask = new CompressAsyncTask(this, baseFiles, mZipPath);
    asyncTask.execute();
    // If we get killed, after returning from here, restart
    return START_STICKY;
}

From source file:net.sourceforge.servestream.service.AppWidgetOneProvider.java

/**
 * Update all active widget instances by pushing changes 
 *///from ww  w  .  j  a v a  2s .co  m
public void performUpdate(MediaPlaybackService service, int[] appWidgetIds, String what) {
    final Resources res = service.getResources();
    final RemoteViews views = new RemoteViews(service.getPackageName(), R.layout.appwidget_one);

    if (what.equals(MediaPlaybackService.PLAYER_CLOSED)) {
        defaultAppWidget(service, appWidgetIds);
    } else {
        CharSequence trackName = service.getTrackName();
        CharSequence artistName = service.getArtistName();
        //CharSequence errorState = null;

        if (trackName == null || trackName.equals(Media.UNKNOWN_STRING)) {
            trackName = res.getText(R.string.widget_one_track_info_unavailable);
        }

        if (artistName == null || artistName.equals(Media.UNKNOWN_STRING)) {
            artistName = service.getMediaUri();
        }

        // Show media info
        views.setViewVisibility(R.id.title, View.VISIBLE);
        views.setTextViewText(R.id.title, trackName);
        views.setViewVisibility(R.id.artist, View.VISIBLE);
        views.setTextViewText(R.id.artist, artistName);

        // Set correct drawable for pause state
        final boolean playing = service.isPlaying();
        if (playing) {
            views.setImageViewResource(R.id.control_play, android.R.drawable.ic_media_pause);
        } else {
            views.setImageViewResource(R.id.control_play, android.R.drawable.ic_media_play);
        }

        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap b = BitmapFactory.decodeStream(
                service.getResources().openRawResource(R.drawable.albumart_mp_unknown_widget), null, opts);

        views.setImageViewBitmap(R.id.coverart, b);

        if (service.getAudioId() >= 0) {
            views.setImageViewBitmap(R.id.coverart, service.getAlbumArt(true));
        }

        // Link actions buttons to intents
        linkButtons(service, views, true);
        pushUpdate(service, appWidgetIds, views);
    }
}

From source file:com.android.talkbacktests.testsession.NotificationTest.java

private void showCustomNotification() {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(getContext()).setAutoCancel(true)
            .setSmallIcon(android.R.drawable.ic_notification_overlay)
            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));

    RemoteViews contentView = new RemoteViews(getContext().getPackageName(), R.layout.custom_notification);
    contentView.setImageViewResource(R.id.notification_image, android.R.drawable.ic_dialog_email);
    contentView.setTextViewText(R.id.notification_title, getString(R.string.custom_notification_title));
    contentView.setTextViewText(R.id.notification_text, getString(R.string.custom_notification_text));
    builder.setContent(contentView);//from  ww  w  . ja  v  a2  s  .  com

    Intent resultIntent = new Intent(getContext(), MainActivity.class);
    resultIntent.setAction(Intent.ACTION_MAIN);
    resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);

    PendingIntent pendingIntent = PendingIntent.getActivity(getContext(), 0, resultIntent, 0);
    builder.setContentIntent(pendingIntent);

    NotificationManager notificationManager = (NotificationManager) getContext()
            .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIFICATION_ID_LAST_VIEW, builder.build());
}

From source file:ir.besteveryeverapp.telegram.MusicPlayerService.java

@SuppressLint("NewApi")
private void createNotification(MessageObject messageObject) {
    String songName = messageObject.getMusicTitle();
    String authorName = messageObject.getMusicAuthor();
    AudioInfo audioInfo = MediaController.getInstance().getAudioInfo();

    RemoteViews simpleContentView = new RemoteViews(getApplicationContext().getPackageName(),
            R.layout.player_small_notification);
    RemoteViews expandedView = null;/*w  ww  . j  av a2s.  c om*/
    if (supportBigNotifications) {
        expandedView = new RemoteViews(getApplicationContext().getPackageName(),
                R.layout.player_big_notification);
    }

    Intent intent = new Intent(ApplicationLoader.applicationContext, LaunchActivity.class);
    intent.setAction("com.tmessages.openplayer");
    intent.setFlags(32768);
    PendingIntent contentIntent = PendingIntent.getActivity(ApplicationLoader.applicationContext, 0, intent, 0);

    Notification notification = new NotificationCompat.Builder(getApplicationContext())
            .setSmallIcon(R.drawable.player).setContentIntent(contentIntent).setContentTitle(songName).build();

    notification.contentView = simpleContentView;
    if (supportBigNotifications) {
        notification.bigContentView = expandedView;
    }

    setListeners(simpleContentView);
    if (supportBigNotifications) {
        setListeners(expandedView);
    }

    Bitmap albumArt = audioInfo != null ? audioInfo.getSmallCover() : null;
    if (albumArt != null) {
        notification.contentView.setImageViewBitmap(R.id.player_album_art, albumArt);
        if (supportBigNotifications) {
            notification.bigContentView.setImageViewBitmap(R.id.player_album_art, albumArt);
        }
    } else {
        notification.contentView.setImageViewResource(R.id.player_album_art, R.drawable.nocover_small);
        if (supportBigNotifications) {
            notification.bigContentView.setImageViewResource(R.id.player_album_art, R.drawable.nocover_big);
        }
    }
    if (MediaController.getInstance().isDownloadingCurrentMessage()) {
        notification.contentView.setViewVisibility(R.id.player_pause, View.GONE);
        notification.contentView.setViewVisibility(R.id.player_play, View.GONE);
        notification.contentView.setViewVisibility(R.id.player_next, View.GONE);
        notification.contentView.setViewVisibility(R.id.player_previous, View.GONE);
        notification.contentView.setViewVisibility(R.id.player_progress_bar, View.VISIBLE);
        if (supportBigNotifications) {
            notification.bigContentView.setViewVisibility(R.id.player_pause, View.GONE);
            notification.bigContentView.setViewVisibility(R.id.player_play, View.GONE);
            notification.bigContentView.setViewVisibility(R.id.player_next, View.GONE);
            notification.bigContentView.setViewVisibility(R.id.player_previous, View.GONE);
            notification.bigContentView.setViewVisibility(R.id.player_progress_bar, View.VISIBLE);
        }
    } else {
        notification.contentView.setViewVisibility(R.id.player_progress_bar, View.GONE);
        notification.contentView.setViewVisibility(R.id.player_next, View.VISIBLE);
        notification.contentView.setViewVisibility(R.id.player_previous, View.VISIBLE);
        if (supportBigNotifications) {
            notification.bigContentView.setViewVisibility(R.id.player_next, View.VISIBLE);
            notification.bigContentView.setViewVisibility(R.id.player_previous, View.VISIBLE);
            notification.bigContentView.setViewVisibility(R.id.player_progress_bar, View.GONE);
        }

        if (MediaController.getInstance().isAudioPaused()) {
            notification.contentView.setViewVisibility(R.id.player_pause, View.GONE);
            notification.contentView.setViewVisibility(R.id.player_play, View.VISIBLE);
            if (supportBigNotifications) {
                notification.bigContentView.setViewVisibility(R.id.player_pause, View.GONE);
                notification.bigContentView.setViewVisibility(R.id.player_play, View.VISIBLE);
            }
        } else {
            notification.contentView.setViewVisibility(R.id.player_pause, View.VISIBLE);
            notification.contentView.setViewVisibility(R.id.player_play, View.GONE);
            if (supportBigNotifications) {
                notification.bigContentView.setViewVisibility(R.id.player_pause, View.VISIBLE);
                notification.bigContentView.setViewVisibility(R.id.player_play, View.GONE);
            }
        }
    }

    notification.contentView.setTextViewText(R.id.player_song_name, songName);
    notification.contentView.setTextViewText(R.id.player_author_name, authorName);
    if (supportBigNotifications) {
        notification.bigContentView.setTextViewText(R.id.player_song_name, songName);
        notification.bigContentView.setTextViewText(R.id.player_author_name, authorName);
    }
    notification.flags |= Notification.FLAG_ONGOING_EVENT;
    startForeground(5, notification);

    if (remoteControlClient != null) {
        RemoteControlClient.MetadataEditor metadataEditor = remoteControlClient.editMetadata(true);
        metadataEditor.putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, authorName);
        metadataEditor.putString(MediaMetadataRetriever.METADATA_KEY_TITLE, songName);
        if (audioInfo != null && audioInfo.getCover() != null) {
            try {
                metadataEditor.putBitmap(RemoteControlClient.MetadataEditor.BITMAP_KEY_ARTWORK,
                        audioInfo.getCover());
            } catch (Throwable e) {
                FileLog.e("tmessages", e);
            }
        }
        metadataEditor.apply();
    }
}

From source file:com.justmoon.glass.bitcoin.price.BitcoinPriceService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (mLiveCard == null) {
        Log.d(TAG, "Publishing LiveCard");
        mLiveCard = mTimelineManager.getLiveCard(LIVE_CARD_ID);

        // Keep track of the callback to remove it before unpublishing.
        //mCallback = new PriceDrawer(this);
        //mLiveCard.enableDirectRendering(true).getSurfaceHolder().addCallback(mCallback);
        mViews = new RemoteViews(getPackageName(), R.layout.card_price);

        RetrievePriceTask get = new RetrievePriceTask();
        get.execute();// w ww  .  j a  va  2 s  .co m

        mViews.setTextViewText(R.id.price, "???");
        mLiveCard.setViews(mViews);
        mLiveCard.setNonSilent(true);

        Intent menuIntent = new Intent(this, MenuActivity.class);
        mLiveCard.setAction(PendingIntent.getActivity(this, 0, menuIntent, 0));

        mLiveCard.publish();
        Log.d(TAG, "Done publishing LiveCard");
    } else {
        // TODO(alainv): Jump to the LiveCard when API is available.
    }

    return START_STICKY;
}

From source file:com.todotxt.todotxttouch.widget.ListWidgetProvider.java

private RemoteViews buildLayout(Context context, int appWidgetId, boolean showProgress) {
    RemoteViews rv;//from ww  w  .java 2s  .co m
    // Specify the service to provide data for the collection widget. Note
    // that we need to
    // embed the appWidgetId via the data otherwise it will be ignored.
    final Intent intent = new Intent(context, ListWidgetService.class);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
    rv = new RemoteViews(context.getPackageName(), R.layout.listwidget);
    rv.setRemoteAdapter(R.id.widget_list, intent);

    // Set the empty view to be displayed if the collection is empty. It
    // must be a sibling
    // view of the collection view.
    rv.setEmptyView(R.id.widget_list, R.id.empty_view);

    // Set click listener for the logo
    //        Intent clickIntent = new Intent(context, LoginScreen.class);
    //        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, clickIntent, 0);
    //        rv.setOnClickPendingIntent(R.id.listwidget_header, pendingIntent);

    // Set click listener for the 'add' button
    //        if (isAuthenticated(context)) {
    //            PendingIntent taskStackBuilderPendingIntent = TaskStackBuilder
    //                    .create(context)
    //                    .addNextIntent(new Intent(context, TodoTxtTouch.class))
    //                    .addNextIntent(new Intent(context, AddTask.class))
    //                    .getPendingIntent(0, 0);
    //            rv.setOnClickPendingIntent(R.id.listwidget_additem,
    //                    taskStackBuilderPendingIntent);
    //        } else {
    //            // if not logged in, just go to login screen
    //            rv.setOnClickPendingIntent(R.id.listwidget_additem, pendingIntent);
    //        }

    // Bind a click listener template for the contents of the list.
    // Note that we
    // need to update the intent's data if we set an extra, since the extras
    // will be
    // ignored otherwise.
    final Intent onClickIntent = new Intent(context, TodoTxtTouch.class);
    onClickIntent.setAction(ListWidgetProvider.ITEM_ACTION);
    onClickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    onClickIntent.setData(Uri.parse(onClickIntent.toUri(Intent.URI_INTENT_SCHEME)));
    final PendingIntent onClickPendingIntent = PendingIntent.getActivity(context, 0, onClickIntent, 0);
    rv.setPendingIntentTemplate(R.id.widget_list, onClickPendingIntent);

    // Bind the click intent for the refresh button on the widget
    final Intent refreshIntent = new Intent(context, ListWidgetProvider.class);
    refreshIntent.setAction(ListWidgetProvider.REFRESH_ACTION);
    refreshIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    final PendingIntent refreshPendingIntent = PendingIntent.getBroadcast(context, 0, refreshIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    rv.setOnClickPendingIntent(R.id.listwidget_refresh, refreshPendingIntent);

    if (showProgress) {
        rv.setViewVisibility(R.id.listwidget_progress, View.VISIBLE);
        rv.setViewVisibility(R.id.listwidget_refresh, View.INVISIBLE);
    } else {
        rv.setViewVisibility(R.id.listwidget_progress, View.INVISIBLE);
        rv.setViewVisibility(R.id.listwidget_refresh, View.VISIBLE);
    }

    return rv;
}

From source file:org.gnucash.android.ui.widget.WidgetConfigurationActivity.java

/**
 * Updates the widget with id <code>appWidgetId</code> with information from the 
 * account with record ID <code>accountId</code>
  * If the account has been deleted, then a notice is posted in the widget
  * @param appWidgetId ID of the widget to be updated
  * @param accountId Database ID of the account tied to the widget
 *///  ww w . j  a va2s. c  o  m
public static void updateWidget(Context context, int appWidgetId, long accountId) {
    Log.i("WidgetConfiguration", "Updating widget: " + appWidgetId);
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

    AccountsDbAdapter accountsDbAdapter = new AccountsDbAdapter(context);
    Account account = accountsDbAdapter.getAccount(accountId);

    if (account == null) {
        Log.i("WidgetConfiguration", "Account not found, resetting widget " + appWidgetId);
        //if account has been deleted, let the user know
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_4x1);
        views.setTextViewText(R.id.account_name, context.getString(R.string.toast_account_deleted));
        views.setTextViewText(R.id.transactions_summary, "");
        //set it to simply open the app
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                new Intent(context, AccountsActivity.class), 0);
        views.setOnClickPendingIntent(R.id.widget_layout, pendingIntent);
        views.setOnClickPendingIntent(R.id.btn_new_transaction, pendingIntent);
        appWidgetManager.updateAppWidget(appWidgetId, views);
        Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();
        editor.remove(UxArgument.SELECTED_ACCOUNT_ID + appWidgetId);
        editor.commit();
        return;
    }

    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_4x1);
    views.setTextViewText(R.id.account_name, account.getName());
    Money accountBalance = accountsDbAdapter.getAccountBalance(accountId);

    views.setTextViewText(R.id.transactions_summary, accountBalance.formattedString(Locale.getDefault()));
    int color = account.getBalance().isNegative() ? R.color.debit_red : R.color.credit_green;
    views.setTextColor(R.id.transactions_summary, context.getResources().getColor(color));

    Intent accountViewIntent = new Intent(context, TransactionsActivity.class);
    accountViewIntent.setAction(Intent.ACTION_VIEW);
    accountViewIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    accountViewIntent.putExtra(UxArgument.SELECTED_ACCOUNT_ID, accountId);
    PendingIntent accountPendingIntent = PendingIntent.getActivity(context, appWidgetId, accountViewIntent, 0);
    views.setOnClickPendingIntent(R.id.widget_layout, accountPendingIntent);

    Intent newTransactionIntent = new Intent(context, TransactionsActivity.class);
    newTransactionIntent.setAction(Intent.ACTION_INSERT_OR_EDIT);
    newTransactionIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    newTransactionIntent.putExtra(UxArgument.SELECTED_ACCOUNT_ID, accountId);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, appWidgetId, newTransactionIntent, 0);
    views.setOnClickPendingIntent(R.id.btn_new_transaction, pendingIntent);

    appWidgetManager.updateAppWidget(appWidgetId, views);
}

From source file:com.teclib.service.MQTTNotificationService.java

public void CustomNotificationNoConnected(String title) {
    RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification_mqtt_no_connect);

    String strtitle = getString(R.string.app_name);

    Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra("title", strtitle);
    intent.putExtra("text", title);

    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_white_stork).setTicker(getString(R.string.enrolement_string))
            .setOngoing(true).setContentIntent(pIntent).setContent(remoteViews);

    remoteViews.setImageViewResource(R.id.imagenotileft, R.mipmap.ic_notification_no_connect);

    remoteViews.setTextViewText(R.id.title, getString(R.string.app_name));
    remoteViews.setTextViewText(R.id.text, title);

    NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationmanager.notify(1, builder.build());

}

From source file:org.chromium.tools.audio_focus_grabber.AudioFocusGrabberListenerService.java

private void showNotification() {
    RemoteViews view = new RemoteViews(this.getPackageName(), R.layout.audio_focus_grabber_notification_bar);
    view.setOnClickPendingIntent(R.id.notification_button_gain, createPendingIntent(ACTION_GAIN));
    view.setOnClickPendingIntent(R.id.notification_button_transient_pause,
            createPendingIntent(ACTION_TRANSIENT_PAUSE));
    view.setOnClickPendingIntent(R.id.notification_button_transient_duck,
            createPendingIntent(ACTION_TRANSIENT_DUCK));
    view.setOnClickPendingIntent(R.id.notification_button_hide, createPendingIntent(ACTION_HIDE_NOTIFICATION));

    NotificationManagerCompat manager = NotificationManagerCompat.from(this);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setContent(view)
            .setSmallIcon(R.drawable.notification_icon);
    manager.notify(NOTIFICATION_ID, builder.build());
}

From source file:com.teclib.service.BootService.java

public void CustomNotification(int status) {
    if (status == 1) {
        //enrolement ok
    } else {/*from w  w w  . j a v a  2 s  .c  o m*/
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification_enrolment);

        String strTitle = getString(R.string.app_name);
        String strText = getString(R.string.enrolement_string);

        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra("title", strTitle);
        intent.putExtra("text", strText);

        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_white_stork).setTicker(getString(R.string.enrolement_string))
                .setOngoing(true).setContentIntent(pIntent).setContent(remoteViews);

        remoteViews.setImageViewResource(R.id.imagenotileft, R.mipmap.ic_notification_enrolment);
        remoteViews.setTextViewText(R.id.title, getString(R.string.app_name));
        remoteViews.setTextViewText(R.id.text, getString(R.string.enrolement_string));

        NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationmanager.notify(1, builder.build());
    }

}