Added big app widget.

This commit is contained in:
Karim Abou Zeid 2016-04-04 15:35:12 +02:00
commit 14204d1789
14 changed files with 381 additions and 18 deletions

View file

@ -126,6 +126,19 @@
<activity android:name=".ui.activities.SearchActivity" />
<receiver
android:name=".appwidgets.AppWidgetBig"
android:exported="false"
android:label="@string/app_widget_big_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/app_widget_big_info" />
</receiver>
<receiver
android:name=".appwidgets.AppWidgetClassic"
android:exported="false"

View file

@ -0,0 +1,203 @@
package com.kabouzeid.gramophone.appwidgets;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.RemoteViews;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.kabouzeid.appthemehelper.util.MaterialValueHelper;
import com.kabouzeid.gramophone.R;
import com.kabouzeid.gramophone.glide.SongGlideRequest;
import com.kabouzeid.gramophone.model.Song;
import com.kabouzeid.gramophone.service.MusicService;
import com.kabouzeid.gramophone.ui.activities.MainActivity;
import com.kabouzeid.gramophone.util.Util;
/**
* @author Karim Abou Zeid (kabouzeid)
*/
public class AppWidgetBig extends BaseAppWidget {
public static final String NAME = "app_widget_big";
private static AppWidgetBig mInstance;
public static synchronized AppWidgetBig getInstance() {
if (mInstance == null) {
mInstance = new AppWidgetBig();
}
return mInstance;
}
/**
* {@inheritDoc}
*/
@Override
public void onUpdate(final Context context, final AppWidgetManager appWidgetManager,
final int[] appWidgetIds) {
defaultAppWidget(context, appWidgetIds);
final Intent updateIntent = new Intent(MusicService.APP_WIDGET_UPDATE);
updateIntent.putExtra(MusicService.EXTRA_APP_WIDGET_NAME, NAME);
updateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
updateIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
context.sendBroadcast(updateIntent);
}
/**
* Initialize given widgets to default state, where we launch Music on
* default click and hide actions if service not running.
*/
private void defaultAppWidget(final Context context, final int[] appWidgetIds) {
final RemoteViews appWidgetView = new RemoteViews(context.getPackageName(), R.layout.app_widget_big);
appWidgetView.setViewVisibility(R.id.media_titles, View.INVISIBLE);
appWidgetView.setViewVisibility(R.id.image, View.INVISIBLE);
appWidgetView.setImageViewBitmap(R.id.button_next, createBitmap(Util.getTintedDrawable(context, R.drawable.ic_skip_next_white_24dp, MaterialValueHelper.getPrimaryTextColor(context, false)), 1f));
appWidgetView.setImageViewBitmap(R.id.button_prev, createBitmap(Util.getTintedDrawable(context, R.drawable.ic_skip_previous_white_24dp, MaterialValueHelper.getPrimaryTextColor(context, false)), 1f));
appWidgetView.setImageViewBitmap(R.id.button_toggle_play_pause, createBitmap(Util.getTintedDrawable(context, R.drawable.ic_play_arrow_white_24dp, MaterialValueHelper.getPrimaryTextColor(context, false)), 1f));
linkButtons(context, appWidgetView);
pushUpdate(context, appWidgetIds, appWidgetView);
}
private void pushUpdate(final Context context, final int[] appWidgetIds, final RemoteViews views) {
final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
if (appWidgetIds != null) {
appWidgetManager.updateAppWidget(appWidgetIds, views);
} else {
appWidgetManager.updateAppWidget(new ComponentName(context, getClass()), views);
}
}
/**
* Check against {@link AppWidgetManager} if there are any instances of this
* widget.
*/
private boolean hasInstances(final Context context) {
final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
final int[] mAppWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context,
getClass()));
return mAppWidgetIds.length > 0;
}
/**
* Handle a change notification coming over from
* {@link MusicService}
*/
public void notifyChange(final MusicService service, final String what) {
if (hasInstances(service)) {
if (MusicService.META_CHANGED.equals(what) || MusicService.PLAY_STATE_CHANGED.equals(what)) {
performUpdate(service, null);
}
}
}
/**
* Update all active widget instances by pushing changes
*/
public void performUpdate(final MusicService service, final int[] appWidgetIds) {
final RemoteViews appWidgetView = new RemoteViews(service.getPackageName(), R.layout.app_widget_big);
final boolean isPlaying = service.isPlaying();
final Song song = service.getCurrentSong();
// Set the titles and artwork
if (TextUtils.isEmpty(song.title) && TextUtils.isEmpty(song.artistName)) {
appWidgetView.setViewVisibility(R.id.media_titles, View.INVISIBLE);
} else {
appWidgetView.setViewVisibility(R.id.media_titles, View.VISIBLE);
appWidgetView.setTextViewText(R.id.title, song.title);
appWidgetView.setTextViewText(R.id.text, song.artistName);
}
// Set correct drawable for pause state
int playPauseRes = isPlaying ? R.drawable.ic_pause_white_24dp : R.drawable.ic_play_arrow_white_24dp;
appWidgetView.setImageViewBitmap(R.id.button_toggle_play_pause, createBitmap(Util.getTintedDrawable(service, playPauseRes, MaterialValueHelper.getPrimaryTextColor(service, false)), 1f));
// set prev/next button drawables
appWidgetView.setImageViewBitmap(R.id.button_next, createBitmap(Util.getTintedDrawable(service, R.drawable.ic_skip_next_white_24dp, MaterialValueHelper.getPrimaryTextColor(service, false)), 1f));
appWidgetView.setImageViewBitmap(R.id.button_prev, createBitmap(Util.getTintedDrawable(service, R.drawable.ic_skip_previous_white_24dp, MaterialValueHelper.getPrimaryTextColor(service, false)), 1f));
// Link actions buttons to intents
linkButtons(service, appWidgetView);
// load the album cover async and push the update on completion
Point p = Util.getScreenSize(service);
final int widgetImageSize = Math.min(p.x, p.y);
final Context appContext = service.getApplicationContext();
service.runOnUiThread(new Runnable() {
@Override
public void run() {
if (target != null) {
Glide.clear(target);
}
target = SongGlideRequest.Builder.from(Glide.with(appContext), song)
.checkIgnoreMediaStore(appContext)
.asBitmap().build()
.into(new SimpleTarget<Bitmap>(widgetImageSize, widgetImageSize) {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
update(resource);
}
@Override
public void onLoadFailed(Exception e, Drawable errorDrawable) {
super.onLoadFailed(e, errorDrawable);
update(null);
}
private void update(@Nullable Bitmap bitmap) {
appWidgetView.setViewVisibility(R.id.image, View.VISIBLE);
if (bitmap == null) {
appWidgetView.setImageViewResource(R.id.image, R.drawable.default_album_art);
} else {
appWidgetView.setImageViewBitmap(R.id.image, bitmap);
}
pushUpdate(appContext, appWidgetIds, appWidgetView);
}
});
}
});
}
private Target<Bitmap> target; // for cancellation
/**
* Link up various button actions using {@link PendingIntent}.
*/
private void linkButtons(final Context context, final RemoteViews views) {
Intent action;
PendingIntent pendingIntent;
final ComponentName serviceName = new ComponentName(context, MusicService.class);
// Home
action = new Intent(context, MainActivity.class);
action.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
pendingIntent = PendingIntent.getActivity(context, 0, action, 0);
views.setOnClickPendingIntent(R.id.clickable_area, pendingIntent);
// Previous track
pendingIntent = buildPendingIntent(context, MusicService.ACTION_REWIND, serviceName);
views.setOnClickPendingIntent(R.id.button_prev, pendingIntent);
// Play and pause
pendingIntent = buildPendingIntent(context, MusicService.ACTION_TOGGLE_PAUSE, serviceName);
views.setOnClickPendingIntent(R.id.button_toggle_play_pause, pendingIntent);
// Next track
pendingIntent = buildPendingIntent(context, MusicService.ACTION_SKIP, serviceName);
views.setOnClickPendingIntent(R.id.button_next, pendingIntent);
}
}

View file

@ -29,6 +29,6 @@ public class BitmapPaletteTranscoder implements ResourceTranscoder<Bitmap, Bitma
@Override
public String getId() {
return "";
return "BitmapPaletteTranscoder.com.kabouzeid.gramophone.glide.palette";
}
}

View file

@ -37,6 +37,7 @@ import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.kabouzeid.gramophone.R;
import com.kabouzeid.gramophone.appwidgets.AppWidgetBig;
import com.kabouzeid.gramophone.appwidgets.AppWidgetClassic;
import com.kabouzeid.gramophone.appwidgets.AppWidgetSmall;
import com.kabouzeid.gramophone.glide.BlurTransformation;
@ -113,8 +114,9 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
private final IBinder musicBind = new MusicBinder();
private AppWidgetSmall appWidgetSmall = AppWidgetSmall.getInstance();
private AppWidgetBig appWidgetBig = AppWidgetBig.getInstance();
private AppWidgetClassic appWidgetClassic = AppWidgetClassic.getInstance();
private AppWidgetSmall appWidgetSmall = AppWidgetSmall.getInstance();
private Playback playback;
private ArrayList<Song> playingQueue = new ArrayList<>();
@ -938,6 +940,7 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
private void sendChangeInternal(final String what) {
sendBroadcast(new Intent(what));
appWidgetBig.notifyChange(this, what);
appWidgetClassic.notifyChange(this, what);
appWidgetSmall.notifyChange(this, what);
}
@ -1162,11 +1165,14 @@ public class MusicService extends Service implements SharedPreferences.OnSharedP
final String command = intent.getStringExtra(EXTRA_APP_WIDGET_NAME);
if (AppWidgetClassic.NAME.equals(command)) {
final int[] small = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
appWidgetClassic.performUpdate(MusicService.this, small);
final int[] ids = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
appWidgetClassic.performUpdate(MusicService.this, ids);
} else if (AppWidgetSmall.NAME.equals(command)) {
final int[] small = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
appWidgetSmall.performUpdate(MusicService.this, small);
final int[] ids = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
appWidgetSmall.performUpdate(MusicService.this, ids);
} else if (AppWidgetBig.NAME.equals(command)) {
final int[] ids = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
appWidgetBig.performUpdate(MusicService.this, ids);
}
}
};

View file

@ -0,0 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/ripple_material_dark" />

View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="90"
android:endColor="#88000000"
android:startColor="#00000000" />
</shape>

View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="270"
android:endColor="#88000000"
android:startColor="#00000000" />
</shape>

View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/ripple_material_dark" android:state_activated="true" android:state_pressed="true" />
<item android:drawable="@color/ripple_material_dark" android:state_activated="true" />
<item android:drawable="@color/ripple_material_dark" android:state_pressed="true" />
<item android:drawable="@android:color/transparent" />
</selector>

View file

@ -0,0 +1,97 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black">
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
tools:ignore="ContentDescription"
tools:src="@drawable/default_album_art" />
<LinearLayout
android:id="@+id/media_actions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@drawable/shadow_up_strong"
android:orientation="horizontal"
android:padding="16dp">
<ImageButton
android:id="@+id/button_prev"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/widget_selector"
tools:ignore="ContentDescription"
tools:src="@drawable/ic_skip_previous_white_24dp"
tools:tint="@color/ate_primary_text_dark" />
<ImageButton
android:id="@+id/button_toggle_play_pause"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/widget_selector"
tools:ignore="ContentDescription"
tools:src="@drawable/ic_play_arrow_white_24dp"
tools:tint="@color/ate_primary_text_dark" />
<ImageButton
android:id="@+id/button_next"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/widget_selector"
tools:ignore="ContentDescription"
tools:src="@drawable/ic_skip_next_white_24dp"
tools:tint="@color/ate_primary_text_dark" />
</LinearLayout>
<FrameLayout
android:id="@+id/clickable_area"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/media_actions"
android:layout_alignParentTop="true" />
<LinearLayout
android:id="@+id/media_titles"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="@drawable/shadow_down_strong"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:singleLine="true"
android:textAppearance="@style/TextAppearance.AppCompat.Headline"
android:textColor="@color/ate_primary_text_dark"
tools:text="Title" />
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:singleLine="true"
android:textAppearance="@style/TextAppearance.AppCompat.Subhead"
android:textColor="@color/ate_secondary_text_dark"
tools:text="Text" />
</LinearLayout>
</RelativeLayout>

View file

@ -32,27 +32,27 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/notification_selector"
android:background="@drawable/widget_selector"
tools:src="@drawable/ic_skip_previous_white_24dp"
tools:tint="#fff" />
tools:tint="@color/ate_secondary_text_dark" />
<ImageButton
android:id="@+id/button_toggle_play_pause"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/notification_selector"
android:background="@drawable/widget_selector"
tools:src="@drawable/ic_play_arrow_white_24dp"
tools:tint="#fff" />
tools:tint="@color/ate_secondary_text_dark" />
<ImageButton
android:id="@+id/button_next"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/notification_selector"
android:background="@drawable/widget_selector"
tools:src="@drawable/ic_skip_next_white_24dp"
tools:tint="#fff" />
tools:tint="@color/ate_secondary_text_dark" />
</LinearLayout>

View file

@ -27,30 +27,30 @@
android:layout_width="0dp"
android:layout_height="@dimen/app_widget_small_button_height"
android:layout_weight="1"
android:background="@drawable/notification_selector"
android:background="@drawable/widget_selector"
tools:ignore="ContentDescription"
tools:src="@drawable/ic_skip_previous_white_24dp"
tools:tint="#fff" />
tools:tint="@color/ate_secondary_text_dark" />
<ImageButton
android:id="@+id/button_toggle_play_pause"
android:layout_width="0dp"
android:layout_height="@dimen/app_widget_small_button_height"
android:layout_weight="1"
android:background="@drawable/notification_selector"
android:background="@drawable/widget_selector"
tools:ignore="ContentDescription"
tools:src="@drawable/ic_play_arrow_white_24dp"
tools:tint="#fff" />
tools:tint="@color/ate_secondary_text_dark" />
<ImageButton
android:id="@+id/button_next"
android:layout_width="0dp"
android:layout_height="@dimen/app_widget_small_button_height"
android:layout_weight="1"
android:background="@drawable/notification_selector"
android:background="@drawable/widget_selector"
tools:ignore="ContentDescription"
tools:src="@drawable/ic_skip_next_white_24dp"
tools:tint="#fff" />
tools:tint="@color/ate_secondary_text_dark" />
</LinearLayout>

View file

@ -69,4 +69,7 @@ http://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout
<dimen name="app_widget_small_min_width">250dp</dimen>
<dimen name="app_widget_small_min_height">40dp</dimen>
<dimen name="app_widget_big_min_width">250dp</dimen>
<dimen name="app_widget_big_min_height">110dp</dimen>
</resources>

View file

@ -245,6 +245,7 @@
<string name="could_not_scan_files">Could not scan %d files.</string>
<string name="listing_files">Listing files</string>
<string name="new_start_directory">%s is the new start directory.</string>
<string name="app_widget_big_name">Phonograph - Big</string>
<string name="app_widget_classic_name">Phonograph - Classic</string>
<string name="app_widget_small_name">Phonograph - Small</string>
</resources>

View file

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:initialLayout="@layout/app_widget_big"
android:minHeight="@dimen/app_widget_big_min_height"
android:minWidth="@dimen/app_widget_big_min_width"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="0"
android:widgetCategory="keyguard|home_screen"
tools:ignore="UnusedAttribute" />
<!--android:previewImage="@drawable/app_widget_classic"-->