Search in sources :

Example 31 with AppWidgetManager

use of android.appwidget.AppWidgetManager in project AntennaPod by AntennaPod.

the class PlayerWidgetService method updateViews.

private void updateViews() {
    isUpdating = true;
    ComponentName playerWidget = new ComponentName(this, PlayerWidget.class);
    AppWidgetManager manager = AppWidgetManager.getInstance(this);
    RemoteViews views = new RemoteViews(getPackageName(), R.layout.player_widget);
    PendingIntent startMediaplayer = PendingIntent.getActivity(this, 0, PlaybackService.getPlayerActivityIntent(this), 0);
    Intent startApp = new Intent(getBaseContext(), MainActivity.class);
    startApp.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startApp.putExtra(MainActivity.EXTRA_FRAGMENT_TAG, QueueFragment.TAG);
    PendingIntent startAppPending = PendingIntent.getActivity(getBaseContext(), 0, startApp, PendingIntent.FLAG_UPDATE_CURRENT);
    boolean nothingPlaying = false;
    if (playbackService != null) {
        final Playable media = playbackService.getPlayable();
        if (media != null) {
            PlayerStatus status = playbackService.getStatus();
            views.setOnClickPendingIntent(R.id.layout_left, startMediaplayer);
            views.setTextViewText(R.id.txtvTitle, media.getEpisodeTitle());
            String progressString = getProgressString();
            if (progressString != null) {
                views.setViewVisibility(R.id.txtvProgress, View.VISIBLE);
                views.setTextViewText(R.id.txtvProgress, progressString);
            }
            if (status == PlayerStatus.PLAYING) {
                views.setImageViewResource(R.id.butPlay, R.drawable.ic_pause_white_24dp);
                if (Build.VERSION.SDK_INT >= 15) {
                    views.setContentDescription(R.id.butPlay, getString(R.string.pause_label));
                }
            } else {
                views.setImageViewResource(R.id.butPlay, R.drawable.ic_play_arrow_white_24dp);
                if (Build.VERSION.SDK_INT >= 15) {
                    views.setContentDescription(R.id.butPlay, getString(R.string.play_label));
                }
            }
            views.setOnClickPendingIntent(R.id.butPlay, createMediaButtonIntent());
        } else {
            nothingPlaying = true;
        }
    } else {
        nothingPlaying = true;
    }
    if (nothingPlaying) {
        // start the app if they click anything
        views.setOnClickPendingIntent(R.id.layout_left, startAppPending);
        views.setOnClickPendingIntent(R.id.butPlay, startAppPending);
        views.setViewVisibility(R.id.txtvProgress, View.INVISIBLE);
        views.setTextViewText(R.id.txtvTitle, this.getString(R.string.no_media_playing_label));
        views.setImageViewResource(R.id.butPlay, R.drawable.ic_play_arrow_white_24dp);
    }
    manager.updateAppWidget(playerWidget, views);
    isUpdating = false;
}
Also used : RemoteViews(android.widget.RemoteViews) Playable(de.danoeh.antennapod.core.util.playback.Playable) AppWidgetManager(android.appwidget.AppWidgetManager) PlayerStatus(de.danoeh.antennapod.core.service.playback.PlayerStatus) ComponentName(android.content.ComponentName) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent)

Example 32 with AppWidgetManager

use of android.appwidget.AppWidgetManager in project android_frameworks_base by DirtyUnicorns.

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);
        }
    }
}
Also used : RemoteViews(android.widget.RemoteViews) SharedPreferences(android.content.SharedPreferences) Bundle(android.os.Bundle) AppWidgetManager(android.appwidget.AppWidgetManager) ComponentName(android.content.ComponentName)

Example 33 with AppWidgetManager

use of android.appwidget.AppWidgetManager in project NetGuard by M66B.

the class WidgetLockdown method updateWidgets.

public static void updateWidgets(Context context) {
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    int[] appWidgetIds = AppWidgetManager.getInstance(context).getAppWidgetIds(new ComponentName(context, WidgetLockdown.class));
    update(appWidgetIds, appWidgetManager, context);
}
Also used : AppWidgetManager(android.appwidget.AppWidgetManager) ComponentName(android.content.ComponentName)

Example 34 with AppWidgetManager

use of android.appwidget.AppWidgetManager in project NetGuard by M66B.

the class WidgetMain method updateWidgets.

public static void updateWidgets(Context context) {
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    int[] appWidgetIds = AppWidgetManager.getInstance(context).getAppWidgetIds(new ComponentName(context, WidgetMain.class));
    update(appWidgetIds, appWidgetManager, context);
}
Also used : AppWidgetManager(android.appwidget.AppWidgetManager) ComponentName(android.content.ComponentName)

Example 35 with AppWidgetManager

use of android.appwidget.AppWidgetManager in project muzei by romannurik.

the class AppWidgetUpdateTask method doInBackground.

@Override
protected Boolean doInBackground(Void... params) {
    ComponentName widget = new ComponentName(mContext, MuzeiAppWidgetProvider.class);
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(mContext);
    int[] appWidgetIds = appWidgetManager.getAppWidgetIds(widget);
    if (appWidgetIds.length == 0) {
        // No app widgets, nothing to do
        Log.i(TAG, "No AppWidgets found");
        return true;
    }
    String[] projection = new String[] { BaseColumns._ID, MuzeiContract.Artwork.COLUMN_NAME_TITLE, MuzeiContract.Artwork.COLUMN_NAME_BYLINE, MuzeiContract.Sources.COLUMN_NAME_SUPPORTS_NEXT_ARTWORK_COMMAND };
    ContentResolver contentResolver = mContext.getContentResolver();
    Cursor artwork = contentResolver.query(MuzeiContract.Artwork.CONTENT_URI, projection, null, null, null);
    if (artwork == null || !artwork.moveToFirst()) {
        Log.w(TAG, "No current artwork found");
        if (artwork != null) {
            artwork.close();
        }
        return false;
    }
    String title = artwork.getString(artwork.getColumnIndex(MuzeiContract.Artwork.COLUMN_NAME_TITLE));
    String byline = artwork.getString(artwork.getColumnIndex(MuzeiContract.Artwork.COLUMN_NAME_BYLINE));
    String contentDescription = !TextUtils.isEmpty(title) ? title : byline;
    Uri imageUri = ContentUris.withAppendedId(MuzeiContract.Artwork.CONTENT_URI, artwork.getLong(artwork.getColumnIndex(BaseColumns._ID)));
    boolean supportsNextArtwork = artwork.getInt(artwork.getColumnIndex(MuzeiContract.Sources.COLUMN_NAME_SUPPORTS_NEXT_ARTWORK_COMMAND)) != 0;
    artwork.close();
    // Update the widget(s) with the new artwork information
    PackageManager packageManager = mContext.getPackageManager();
    Intent launchIntent = packageManager.getLaunchIntentForPackage(mContext.getPackageName());
    PendingIntent launchPendingIntent = PendingIntent.getActivity(mContext, 0, launchIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    Intent nextArtworkIntent = new Intent(mContext, MuzeiAppWidgetProvider.class);
    nextArtworkIntent.setAction(MuzeiAppWidgetProvider.ACTION_NEXT_ARTWORK);
    PendingIntent nextArtworkPendingIntent = PendingIntent.getBroadcast(mContext, 0, nextArtworkIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics();
    int smallWidgetHeight = mContext.getResources().getDimensionPixelSize(R.dimen.widget_small_height_breakpoint);
    for (int widgetId : appWidgetIds) {
        Bundle extras = appWidgetManager.getAppWidgetOptions(widgetId);
        int widgetWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, extras.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH), displayMetrics);
        int widgetHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, extras.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT), displayMetrics);
        Bitmap image;
        try {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(contentResolver.openInputStream(imageUri), null, options);
            int width = options.outWidth;
            int height = options.outHeight;
            options.inJustDecodeBounds = false;
            options.inSampleSize = Math.min(ImageUtil.calculateSampleSize(width, widgetWidth), ImageUtil.calculateSampleSize(height, widgetHeight));
            image = BitmapFactory.decodeStream(contentResolver.openInputStream(imageUri), null, options);
        } catch (FileNotFoundException e) {
            Log.e(TAG, "Could not find current artwork image", e);
            return false;
        }
        // Even after using sample size to scale an image down, it might be larger than the
        // maximum bitmap memory usage for widgets
        Bitmap scaledImage = scaleBitmap(image, widgetWidth, widgetHeight);
        @LayoutRes int widgetLayout = widgetHeight < smallWidgetHeight ? R.layout.widget_small : R.layout.widget;
        RemoteViews remoteViews = new RemoteViews(mContext.getPackageName(), widgetLayout);
        remoteViews.setContentDescription(R.id.widget_background, contentDescription);
        remoteViews.setImageViewBitmap(R.id.widget_background, scaledImage);
        remoteViews.setOnClickPendingIntent(R.id.widget_background, launchPendingIntent);
        remoteViews.setOnClickPendingIntent(R.id.widget_next_artwork, nextArtworkPendingIntent);
        if (supportsNextArtwork) {
            remoteViews.setViewVisibility(R.id.widget_next_artwork, View.VISIBLE);
        } else {
            remoteViews.setViewVisibility(R.id.widget_next_artwork, View.GONE);
        }
        appWidgetManager.updateAppWidget(widgetId, remoteViews);
    }
    return true;
}
Also used : Bundle(android.os.Bundle) AppWidgetManager(android.appwidget.AppWidgetManager) FileNotFoundException(java.io.FileNotFoundException) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) Cursor(android.database.Cursor) Uri(android.net.Uri) DisplayMetrics(android.util.DisplayMetrics) ContentResolver(android.content.ContentResolver) Bitmap(android.graphics.Bitmap) RemoteViews(android.widget.RemoteViews) LayoutRes(android.support.annotation.LayoutRes) PackageManager(android.content.pm.PackageManager) ComponentName(android.content.ComponentName) PendingIntent(android.app.PendingIntent) BitmapFactory(android.graphics.BitmapFactory)

Aggregations

AppWidgetManager (android.appwidget.AppWidgetManager)53 ComponentName (android.content.ComponentName)33 RemoteViews (android.widget.RemoteViews)26 Intent (android.content.Intent)21 PendingIntent (android.app.PendingIntent)12 Bundle (android.os.Bundle)12 SharedPreferences (android.content.SharedPreferences)7 Context (android.content.Context)5 View (android.view.View)4 Uri (android.net.Uri)3 AppWidgetProvider (android.appwidget.AppWidgetProvider)2 PackageManager (android.content.pm.PackageManager)2 Cursor (android.database.Cursor)2 Button (android.widget.Button)2 EditText (android.widget.EditText)2 TextView (android.widget.TextView)2 Account (android.accounts.Account)1 Service (android.app.Service)1 AppWidgetProviderInfo (android.appwidget.AppWidgetProviderInfo)1 ContentResolver (android.content.ContentResolver)1