add a widget config for setting the background transparency

This commit is contained in:
tibbi 2016-01-24 17:40:25 +01:00
parent a28f8cc53e
commit 3f98ab868d
13 changed files with 209 additions and 17 deletions

View file

@ -23,6 +23,15 @@
</intent-filter>
</activity>
<activity
android:name=".MyWidgetConfigure"
android:screenOrientation="portrait"
android:theme="@style/MyWidgetConfigTheme">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
</intent-filter>
</activity>
<service android:name="musicplayer.simplemobiletools.com.MusicService"/>
<receiver android:name=".MyWidgetProvider">

View file

@ -0,0 +1,6 @@
package musicplayer.simplemobiletools.com;
public class Constants {
public static final String PREFS = "prefs";
public static final String WIDGET_BG_COLOR = "widget_bg_color";
}

View file

@ -0,0 +1,86 @@
package musicplayer.simplemobiletools.com;
import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.RemoteViews;
import android.widget.SeekBar;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class MyWidgetConfigure extends Activity implements SeekBar.OnSeekBarChangeListener {
@Bind(R.id.config_seekbar) SeekBar seekBar;
@Bind(R.id.config_player) View background;
private int widgetId;
private int newBgColor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.widget_config);
ButterKnife.bind(this);
seekBar.setOnSeekBarChangeListener(this);
seekBar.setProgress(50);
final Intent intent = getIntent();
final Bundle extras = intent.getExtras();
if (extras != null)
widgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
if (widgetId == AppWidgetManager.INVALID_APPWIDGET_ID)
finish();
setResult(RESULT_CANCELED);
}
@OnClick(R.id.config_save)
public void saveConfig() {
final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
final RemoteViews views = new RemoteViews(getPackageName(), R.layout.widget);
views.setInt(R.id.widget_holder, "setBackgroundColor", newBgColor);
appWidgetManager.updateAppWidget(widgetId, views);
storeWidgetBackground();
requestWidgetUpdate();
final Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
setResult(RESULT_OK, resultValue);
finish();
}
private void storeWidgetBackground() {
final SharedPreferences prefs = getSharedPreferences(Constants.PREFS, Context.MODE_PRIVATE);
prefs.edit().putInt(Constants.WIDGET_BG_COLOR, newBgColor).apply();
}
private void requestWidgetUpdate() {
final Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, this, MyWidgetProvider.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[]{widgetId});
sendBroadcast(intent);
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
final float percent = (float) progress / (float) 100;
final int alpha = (int) (255 * percent);
newBgColor = Color.argb(alpha, 0, 0, 0);
background.setBackgroundColor(newBgColor);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
}

View file

@ -6,6 +6,7 @@ import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.RemoteViews;
@ -18,7 +19,7 @@ public class MyWidgetProvider extends AppWidgetProvider {
private static final String NEXT = "next";
private static final String STOP = "stop";
private static int widgetId;
private static int[] widgetIds;
private static RemoteViews remoteViews;
private static AppWidgetManager widgetManager;
private static Context cxt;
@ -44,14 +45,18 @@ public class MyWidgetProvider extends AppWidgetProvider {
}
private void initVariables(Context context) {
final SharedPreferences prefs = initPrefs(context);
final int defaultColor = context.getResources().getColor(R.color.dark_grey);
final int newBgColor = prefs.getInt(Constants.WIDGET_BG_COLOR, defaultColor);
final ComponentName component = new ComponentName(context, MyWidgetProvider.class);
widgetManager = AppWidgetManager.getInstance(context);
final int[] widgetIds = widgetManager.getAppWidgetIds(component);
widgetIds = widgetManager.getAppWidgetIds(component);
if (widgetIds.length == 0)
return;
widgetId = widgetIds[0];
remoteViews = getRemoteViews(widgetManager, context, widgetId);
remoteViews = getRemoteViews(widgetManager, context, widgetIds[0]);
remoteViews.setInt(R.id.widget_holder, "setBackgroundColor", newBgColor);
if (bus == null) {
bus = BusProvider.getInstance();
@ -59,6 +64,10 @@ public class MyWidgetProvider extends AppWidgetProvider {
}
}
private SharedPreferences initPrefs(Context context) {
return context.getSharedPreferences(Constants.PREFS, Context.MODE_PRIVATE);
}
@Subscribe
public void songChangedEvent(Events.SongChanged event) {
currSong = event.getSong();
@ -75,15 +84,16 @@ public class MyWidgetProvider extends AppWidgetProvider {
remoteViews.setTextViewText(R.id.songTitle, title);
remoteViews.setTextViewText(R.id.songArtist, artist);
updateWidget();
updatePlayPauseButton();
}
@Subscribe
public void songStateChanged(Events.SongStateChanged event) {
isPlaying = event.getIsPlaying();
setupPlayPauseButton();
updatePlayPauseButton();
}
private void setupPlayPauseButton() {
private void updatePlayPauseButton() {
int icon = R.mipmap.play_white;
if (isPlaying)
@ -94,12 +104,12 @@ public class MyWidgetProvider extends AppWidgetProvider {
}
private void updateWidget() {
widgetManager.updateAppWidget(widgetId, remoteViews);
widgetManager.updateAppWidget(widgetIds, remoteViews);
}
@Override
public void onReceive(Context context, Intent intent) {
if (remoteViews == null || widgetManager == null || widgetId == 0 || bus == null)
if (remoteViews == null || widgetManager == null || widgetIds == null || bus == null)
initVariables(context);
final String action = intent.getAction();
@ -137,7 +147,7 @@ public class MyWidgetProvider extends AppWidgetProvider {
setupIntent(PLAYPAUSE, R.id.playPauseBtn);
setupIntent(NEXT, R.id.nextBtn);
setupIntent(STOP, R.id.stopBtn);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
appWidgetManager.updateAppWidget(widgetIds, remoteViews);
}
@Override
@ -145,7 +155,7 @@ public class MyWidgetProvider extends AppWidgetProvider {
remoteViews = getRemoteViews(appWidgetManager, context, widgetId);
setupButtons(appWidgetManager);
updateSongInfo();
setupPlayPauseButton();
updatePlayPauseButton();
super.onAppWidgetOptionsChanged(context, appWidgetManager, widgetId, newOptions);
}

View file

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

View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="@color/dark_grey"/>
</shape>

View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="@color/dark_grey_pressed"/>
</shape>

View file

@ -1,9 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/black"
android:gravity="center_horizontal">
<RelativeLayout
android:id="@+id/widget_holder"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/black"
android:gravity="center_horizontal">
<TextView
android:id="@+id/songTitle"

View file

@ -0,0 +1,48 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="80dp"
android:layout_marginLeft="@dimen/activity_margin"
android:layout_marginRight="@dimen/activity_margin"
android:layout_marginTop="80dp">
<include
android:id="@+id/config_player"
layout="@layout/widget"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_margin"/>
<RelativeLayout
android:id="@+id/config_bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@android:color/white">
<Button
android:id="@+id/config_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@drawable/config_button"
android:paddingLeft="@dimen/activity_margin"
android:paddingRight="@dimen/activity_margin"
android:text="Save"
android:textColor="@android:color/white"
android:textSize="@dimen/config_text_size"/>
<SeekBar
android:id="@+id/config_seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginRight="@dimen/activity_margin"
android:layout_toLeftOf="@id/config_save"/>
</RelativeLayout>
</RelativeLayout>

View file

@ -3,4 +3,7 @@
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="dark_grey">#88000000</color>
<color name="dark_grey_pressed">#aa000000</color>
</resources>

View file

@ -3,4 +3,5 @@
<dimen name="song_item_padding">8dp</dimen>
<dimen name="medium_padding">10dp</dimen>
<dimen name="widget_row_height">60dp</dimen>
<dimen name="config_text_size">18sp</dimen>
</resources>

View file

@ -1,11 +1,17 @@
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="MyWidgetConfigTheme" parent="@style/AppTheme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:textColor">@android:color/white</item>
</style>
</resources>

View file

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:configure="musicplayer.simplemobiletools.com.MyWidgetConfigure"
android:initialLayout="@layout/widget"
android:minHeight="110dp"
android:minWidth="250dp">