allow changing the widgets background color

This commit is contained in:
tibbi 2016-01-31 18:27:22 +01:00
parent 53a3c76299
commit 24efdfda9b
6 changed files with 102 additions and 23 deletions

View File

@ -6,5 +6,6 @@ public class Constants {
public static final float HIGH_ALPHA = 0.9f;
public static final String PREFS = "prefs";
public static final String WIDGET_BG_COLOR = "widget_bg_color";
public static final String WIDGET_TEXT_COLOR = "widget_text_color";
}

View File

@ -11,6 +11,7 @@ import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import org.joda.time.DateTime;
@ -27,7 +28,10 @@ public class MyWidgetConfigure extends AppCompatActivity implements Calendar {
@Bind(R.id.left_arrow) ImageView leftArrow;
@Bind(R.id.right_arrow) ImageView rightArrow;
@Bind(R.id.table_month) TextView monthTV;
@Bind(R.id.config_bg_color) View bgColorPicker;
@Bind(R.id.config_bg_seekbar) SeekBar bgSeekBar;
@Bind(R.id.config_text_color) View textColorPicker;
@Bind(R.id.config_calendar) View widgetBackground;
@BindDimen(R.dimen.day_text_size) float dayTextSize;
@BindDimen(R.dimen.today_text_size) float todayTextSize;
@ -36,9 +40,13 @@ public class MyWidgetConfigure extends AppCompatActivity implements Calendar {
private Resources res;
private String packageName;
private List<Day> days;
private static int textColorWithoutTransparency;
private static int textColor;
private static int weakTextColor;
private int bgColorWithoutTransparency;
private int bgColor;
private float bgAlpha;
private int textColorWithoutTransparency;
private int textColor;
private int weakTextColor;
@Override
public void onCreate(Bundle savedInstanceState) {
@ -67,6 +75,13 @@ public class MyWidgetConfigure extends AppCompatActivity implements Calendar {
textColorWithoutTransparency = prefs.getInt(Constants.WIDGET_TEXT_COLOR, Color.WHITE);
updateTextColors();
bgColor = prefs.getInt(Constants.WIDGET_BG_COLOR, Color.BLACK);
bgColorWithoutTransparency = Color.rgb(Color.red(bgColor), Color.green(bgColor), Color.blue(bgColor));
bgAlpha = Color.alpha(bgColor) / (float) 255;
bgSeekBar.setOnSeekBarChangeListener(bgSeekbarChangeListener);
bgSeekBar.setProgress((int) (bgAlpha * 100));
updateBgColor();
calendar = new CalendarImpl(this);
calendar.updateCalendar(new DateTime());
}
@ -88,9 +103,27 @@ public class MyWidgetConfigure extends AppCompatActivity implements Calendar {
private void storeWidgetColors() {
final SharedPreferences prefs = getSharedPreferences(Constants.PREFS, Context.MODE_PRIVATE);
prefs.edit().putInt(Constants.WIDGET_BG_COLOR, bgColor).apply();
prefs.edit().putInt(Constants.WIDGET_TEXT_COLOR, textColorWithoutTransparency).apply();
}
@OnClick(R.id.config_bg_color)
public void pickBackgroundColor() {
AmbilWarnaDialog dialog = new AmbilWarnaDialog(this, bgColorWithoutTransparency, new AmbilWarnaDialog.OnAmbilWarnaListener() {
@Override
public void onCancel(AmbilWarnaDialog dialog) {
}
@Override
public void onOk(AmbilWarnaDialog dialog, int color) {
bgColorWithoutTransparency = color;
updateBgColor();
}
});
dialog.show();
}
@OnClick(R.id.config_text_color)
public void pickTextColor() {
AmbilWarnaDialog dialog = new AmbilWarnaDialog(this, textColor, new AmbilWarnaDialog.OnAmbilWarnaListener() {
@ -126,6 +159,12 @@ public class MyWidgetConfigure extends AppCompatActivity implements Calendar {
updateLabels();
}
private void updateBgColor() {
bgColor = Helpers.adjustAlpha(bgColorWithoutTransparency, bgAlpha);
widgetBackground.setBackgroundColor(bgColor);
bgColorPicker.setBackgroundColor(bgColor);
}
private void updateDays() {
final int len = days.size();
for (int i = 0; i < len; i++) {
@ -148,6 +187,24 @@ public class MyWidgetConfigure extends AppCompatActivity implements Calendar {
}
}
private SeekBar.OnSeekBarChangeListener bgSeekbarChangeListener = new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
bgAlpha = (float) progress / (float) 100;
updateBgColor();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
};
@OnClick(R.id.left_arrow)
public void leftArrowClicked() {
calendar.getPrevMonth();

View File

@ -34,7 +34,6 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calendar {
private static Resources res;
private static float dayTextSize;
private static float todayTextSize;
private static int bgColor;
private static int textColor;
private static int weakTextColor;
@ -46,13 +45,13 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calendar {
private void initVariables(Context context) {
cxt = context;
res = cxt.getResources();
bgColor = Color.BLACK;
final SharedPreferences prefs = initPrefs(cxt);
final int storedTextColor = prefs.getInt(Constants.WIDGET_TEXT_COLOR, Color.WHITE);
textColor = Helpers.adjustAlpha(storedTextColor, Constants.HIGH_ALPHA);
weakTextColor = Helpers.adjustAlpha(storedTextColor, Constants.LOW_ALPHA);
dayTextSize = res.getDimension(R.dimen.day_text_size) / res.getDisplayMetrics().density;
todayTextSize = res.getDimension(R.dimen.today_text_size) / res.getDisplayMetrics().density;
@ -66,6 +65,9 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calendar {
updateLabelColor();
updateTopViews();
final int bgColor = prefs.getInt(Constants.WIDGET_BG_COLOR, Color.BLACK);
remoteViews.setInt(R.id.calendar_holder, "setBackgroundColor", bgColor);
calendar = new CalendarImpl(this);
calendar.updateCalendar(new DateTime());

View File

@ -46,7 +46,6 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/table_month"
android:layout_marginTop="@dimen/activity_margin"
android:gravity="center"
android:orientation="vertical">

View File

@ -11,28 +11,47 @@
layout="@layout/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/config_bottom"
android:layout_above="@+id/config_bg_color"
android:layout_marginBottom="@dimen/activity_margin"/>
<Button
android:id="@+id/config_bg_color"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_above="@+id/config_text_color"/>
<RelativeLayout
android:id="@+id/config_bottom"
android:id="@+id/config_bg_seekbar_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignBottom="@+id/config_bg_color"
android:layout_alignTop="@+id/config_bg_color"
android:layout_toRightOf="@+id/config_bg_color"
android:background="@android:color/white">
<SeekBar
android:id="@+id/config_bg_seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
android:layout_centerVertical="true"
android:paddingLeft="@dimen/activity_margin"
android:paddingRight="@dimen/activity_margin"/>
</RelativeLayout>
<Button
android:id="@+id/config_text_color"
android:layout_width="50dp"
android:layout_height="50dp"/>
android:layout_height="50dp"
android:layout_alignParentBottom="true"/>
<Button
android:id="@+id/config_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:paddingLeft="@dimen/activity_margin"
android:paddingRight="@dimen/activity_margin"
android:text="Save"
android:text="OK"
android:textSize="18sp"/>
</RelativeLayout>
</RelativeLayout>

View File

@ -3,5 +3,6 @@
android:configure="calendar.simplemobiletools.com.MyWidgetConfigure"
android:initialLayout="@layout/activity_main"
android:minHeight="250dp"
android:minWidth="250dp">
android:minWidth="250dp"
android:updatePeriodMillis="18000000">
</appwidget-provider>