some refactoring, no functionality change

This commit is contained in:
tibbi 2016-06-18 23:55:42 +02:00
parent d021053075
commit 858fde96d8
10 changed files with 388 additions and 377 deletions

View File

@ -20,7 +20,7 @@
</activity>
<activity
android:name=".MyWidgetConfigure"
android:name=".activities.WidgetConfigureActivity"
android:screenOrientation="portrait"
android:theme="@style/MyWidgetConfigTheme">
<intent-filter>

View File

@ -11,27 +11,27 @@ public class CalendarImpl {
private static final String DATE_PATTERN = "ddMMYYYY";
private static final String YEAR_PATTERN = "YYYY";
private final Calendar callback;
private final String today;
private DateTime targetDate;
private final Calendar mCallback;
private final String mToday;
private DateTime mTargetDate;
public CalendarImpl(Calendar callback) {
this.callback = callback;
today = new DateTime().toString(DATE_PATTERN);
this.mCallback = callback;
mToday = new DateTime().toString(DATE_PATTERN);
}
public void updateCalendar(DateTime targetDate) {
this.targetDate = targetDate;
this.mTargetDate = targetDate;
getMonthName();
getDays(targetDate);
}
public void getPrevMonth() {
updateCalendar(targetDate.minusMonths(1));
updateCalendar(mTargetDate.minusMonths(1));
}
public void getNextMonth() {
updateCalendar(targetDate.plusMonths(1));
updateCalendar(mTargetDate.plusMonths(1));
}
private void getDays(DateTime targetDate) {
@ -63,17 +63,17 @@ public class CalendarImpl {
value++;
}
callback.updateCalendar(getMonthName(), days);
mCallback.updateCalendar(getMonthName(), days);
}
private boolean isToday(DateTime targetDate, int curDayInMonth) {
return targetDate.withDayOfMonth(curDayInMonth).toString(DATE_PATTERN).equals(today);
return targetDate.withDayOfMonth(curDayInMonth).toString(DATE_PATTERN).equals(mToday);
}
private String getMonthName() {
final String[] months = new DateFormatSymbols().getMonths();
String month = (months[targetDate.getMonthOfYear() - 1]);
final String targetYear = targetDate.toString(YEAR_PATTERN);
String month = (months[mTargetDate.getMonthOfYear() - 1]);
final String targetYear = mTargetDate.toString(YEAR_PATTERN);
if (!targetYear.equals(new DateTime().toString(YEAR_PATTERN))) {
month += " " + targetYear;
}
@ -81,6 +81,6 @@ public class CalendarImpl {
}
public DateTime getTargetDate() {
return targetDate;
return mTargetDate;
}
}

View File

@ -1,25 +1,25 @@
package com.simplemobiletools.calendar;
public class Day {
private final int value;
private final boolean isThisMonth;
private final boolean isToday;
private final int mValue;
private final boolean mIsThisMonth;
private final boolean mIsToday;
public Day(int value, boolean isThisMonth, boolean isToday) {
this.value = value;
this.isThisMonth = isThisMonth;
this.isToday = isToday;
this.mValue = value;
this.mIsThisMonth = isThisMonth;
this.mIsToday = isToday;
}
public int getValue() {
return value;
return mValue;
}
public boolean getIsThisMonth() {
return isThisMonth;
return mIsThisMonth;
}
public boolean getIsToday() {
return isToday;
return mIsToday;
}
}

View File

@ -1,246 +0,0 @@
package com.simplemobiletools.calendar;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import org.joda.time.DateTime;
import java.util.List;
import butterknife.BindDimen;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import yuku.ambilwarna.AmbilWarnaDialog;
public class MyWidgetConfigure extends AppCompatActivity implements Calendar {
@BindView(R.id.left_arrow) ImageView leftArrow;
@BindView(R.id.right_arrow) ImageView rightArrow;
@BindView(R.id.table_month) TextView monthTV;
@BindView(R.id.config_bg_color) View bgColorPicker;
@BindView(R.id.config_bg_seekbar) SeekBar bgSeekBar;
@BindView(R.id.config_text_color) View textColorPicker;
@BindView(R.id.config_calendar) View widgetBackground;
@BindView(R.id.config_save) Button saveBtn;
@BindDimen(R.dimen.day_text_size) float dayTextSize;
@BindDimen(R.dimen.today_text_size) float todayTextSize;
private int widgetId;
private CalendarImpl calendar;
private Resources res;
private String packageName;
private List<Day> days;
private int bgColorWithoutTransparency;
private int bgColor;
private float bgAlpha;
private int textColorWithoutTransparency;
private int textColor;
private int weakTextColor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setResult(RESULT_CANCELED);
setContentView(R.layout.widget_config);
ButterKnife.bind(this);
initVariables();
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();
}
private void initVariables() {
res = getResources();
packageName = getPackageName();
dayTextSize /= res.getDisplayMetrics().density;
todayTextSize /= res.getDisplayMetrics().density;
final SharedPreferences prefs = initPrefs(this);
textColorWithoutTransparency = prefs.getInt(Constants.WIDGET_TEXT_COLOR, getResources().getColor(R.color.colorPrimary));
updateTextColors();
bgColor = prefs.getInt(Constants.WIDGET_BG_COLOR, 1);
if (bgColor == 1) {
bgColor = Color.BLACK;
bgAlpha = .2f;
} else {
bgAlpha = Color.alpha(bgColor) / (float) 255;
}
bgColorWithoutTransparency = Color.rgb(Color.red(bgColor), Color.green(bgColor), Color.blue(bgColor));
bgSeekBar.setOnSeekBarChangeListener(bgSeekbarChangeListener);
bgSeekBar.setProgress((int) (bgAlpha * 100));
updateBgColor();
calendar = new CalendarImpl(this);
calendar.updateCalendar(new DateTime());
}
private SharedPreferences initPrefs(Context context) {
return context.getSharedPreferences(Constants.PREFS, Context.MODE_PRIVATE);
}
@OnClick(R.id.config_save)
public void saveConfig() {
storeWidgetColors();
requestWidgetUpdate();
final Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
setResult(RESULT_OK, resultValue);
finish();
}
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() {
@Override
public void onCancel(AmbilWarnaDialog dialog) {
}
@Override
public void onOk(AmbilWarnaDialog dialog, int color) {
textColorWithoutTransparency = color;
updateTextColors();
updateDays();
}
});
dialog.show();
}
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);
}
private void updateTextColors() {
textColor = Helpers.adjustAlpha(textColorWithoutTransparency, Constants.HIGH_ALPHA);
weakTextColor = Helpers.adjustAlpha(textColorWithoutTransparency, Constants.LOW_ALPHA);
leftArrow.getDrawable().mutate().setColorFilter(textColor, PorterDuff.Mode.SRC_ATOP);
rightArrow.getDrawable().mutate().setColorFilter(textColor, PorterDuff.Mode.SRC_ATOP);
monthTV.setTextColor(textColor);
textColorPicker.setBackgroundColor(textColor);
saveBtn.setTextColor(textColor);
updateLabels();
}
private void updateBgColor() {
bgColor = Helpers.adjustAlpha(bgColorWithoutTransparency, bgAlpha);
widgetBackground.setBackgroundColor(bgColor);
bgColorPicker.setBackgroundColor(bgColor);
saveBtn.setBackgroundColor(bgColor);
}
private void updateDays() {
final int len = days.size();
for (int i = 0; i < len; i++) {
final Day day = days.get(i);
final TextView dayTV = (TextView) findViewById(res.getIdentifier("day_" + i, "id", packageName));
int curTextColor = weakTextColor;
float curTextSize = dayTextSize;
if (day.getIsThisMonth()) {
curTextColor = textColor;
}
if (day.getIsToday()) {
curTextSize = todayTextSize;
}
dayTV.setText(String.valueOf(day.getValue()));
dayTV.setTextColor(curTextColor);
dayTV.setTextSize(curTextSize);
}
}
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();
}
@OnClick(R.id.right_arrow)
public void rightArrowClicked() {
calendar.getNextMonth();
}
@Override
public void updateCalendar(String month, List<Day> days) {
this.days = days;
updateMonth(month);
updateDays();
}
private void updateMonth(String month) {
monthTV.setText(month);
}
private void updateLabels() {
for (int i = 0; i < 7; i++) {
final TextView dayTV = (TextView) findViewById(res.getIdentifier("label_" + i, "id", packageName));
dayTV.setTextSize(dayTextSize);
dayTV.setTextColor(weakTextColor);
}
}
}

View File

@ -27,16 +27,17 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calendar {
private static final String PREV = "prev";
private static final String NEXT = "next";
private static RemoteViews remoteViews;
private static AppWidgetManager widgetManager;
private static Intent intent;
private static Context cxt;
private static CalendarImpl calendar;
private static Resources res;
private static float dayTextSize;
private static float todayTextSize;
private static int textColor;
private static int weakTextColor;
private static RemoteViews mRemoteViews;
private static AppWidgetManager mWidgetManager;
private static Intent mIntent;
private static Context mContext;
private static CalendarImpl mCalendar;
private static Resources mRes;
private static float mDayTextSize;
private static float mTodayTextSize;
private static int mTextColor;
private static int mWeakTextColor;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
@ -46,46 +47,46 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calendar {
}
private void initVariables(Context context) {
cxt = context;
res = cxt.getResources();
mContext = context;
mRes = mContext.getResources();
final SharedPreferences prefs = initPrefs(cxt);
final SharedPreferences prefs = initPrefs(mContext);
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);
mTextColor = Helpers.adjustAlpha(storedTextColor, Constants.HIGH_ALPHA);
mWeakTextColor = 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;
widgetManager = AppWidgetManager.getInstance(cxt);
mDayTextSize = mRes.getDimension(R.dimen.day_text_size) / mRes.getDisplayMetrics().density;
mTodayTextSize = mRes.getDimension(R.dimen.today_text_size) / mRes.getDisplayMetrics().density;
mWidgetManager = AppWidgetManager.getInstance(mContext);
remoteViews = new RemoteViews(cxt.getPackageName(), R.layout.activity_main);
intent = new Intent(cxt, MyWidgetProvider.class);
mRemoteViews = new RemoteViews(mContext.getPackageName(), R.layout.activity_main);
mIntent = new Intent(mContext, MyWidgetProvider.class);
setupButtons();
updateLabelColor();
updateTopViews();
final int bgColor = prefs.getInt(Constants.WIDGET_BG_COLOR, Color.BLACK);
remoteViews.setInt(R.id.calendar_holder, "setBackgroundColor", bgColor);
mRemoteViews.setInt(R.id.calendar_holder, "setBackgroundColor", bgColor);
calendar = new CalendarImpl(this);
calendar.updateCalendar(new DateTime());
mCalendar = new CalendarImpl(this);
mCalendar.updateCalendar(new DateTime());
}
private void updateWidget() {
final ComponentName thisWidget = new ComponentName(cxt, MyWidgetProvider.class);
AppWidgetManager.getInstance(cxt).updateAppWidget(thisWidget, remoteViews);
final ComponentName thisWidget = new ComponentName(mContext, MyWidgetProvider.class);
AppWidgetManager.getInstance(mContext).updateAppWidget(thisWidget, mRemoteViews);
}
private void setupIntent(String action, int id) {
intent.setAction(action);
final PendingIntent pendingIntent = PendingIntent.getBroadcast(cxt, 0, intent, 0);
remoteViews.setOnClickPendingIntent(id, pendingIntent);
mIntent.setAction(action);
final PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, mIntent, 0);
mRemoteViews.setOnClickPendingIntent(id, pendingIntent);
}
private void setupAppOpenIntent(int id) {
final Intent intent = new Intent(cxt, MainActivity.class);
final PendingIntent pendingIntent = PendingIntent.getActivity(cxt, 0, intent, 0);
remoteViews.setOnClickPendingIntent(id, pendingIntent);
final Intent intent = new Intent(mContext, MainActivity.class);
final PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
mRemoteViews.setOnClickPendingIntent(id, pendingIntent);
}
private void setupButtons() {
@ -100,16 +101,16 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calendar {
@Override
public void onReceive(Context context, Intent intent) {
if (remoteViews == null || widgetManager == null || calendar == null || cxt == null)
if (mRemoteViews == null || mWidgetManager == null || mCalendar == null || mContext == null)
initVariables(context);
final String action = intent.getAction();
switch (action) {
case PREV:
calendar.getPrevMonth();
mCalendar.getPrevMonth();
break;
case NEXT:
calendar.getNextMonth();
mCalendar.getNextMonth();
break;
default:
super.onReceive(context, intent);
@ -117,40 +118,40 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calendar {
}
public void updateDays(List<Day> days) {
final String packageName = cxt.getPackageName();
final String packageName = mContext.getPackageName();
final int len = days.size();
for (int i = 0; i < len; i++) {
final Day day = days.get(i);
final int id = res.getIdentifier("day_" + i, "id", packageName);
int curTextColor = weakTextColor;
float curTextSize = dayTextSize;
final int id = mRes.getIdentifier("day_" + i, "id", packageName);
int curTextColor = mWeakTextColor;
float curTextSize = mDayTextSize;
if (day.getIsThisMonth()) {
curTextColor = textColor;
curTextColor = mTextColor;
}
if (day.getIsToday()) {
curTextSize = todayTextSize;
curTextSize = mTodayTextSize;
}
remoteViews.setTextViewText(id, String.valueOf(day.getValue()));
remoteViews.setInt(id, "setTextColor", curTextColor);
remoteViews.setFloat(id, "setTextSize", curTextSize);
mRemoteViews.setTextViewText(id, String.valueOf(day.getValue()));
mRemoteViews.setInt(id, "setTextColor", curTextColor);
mRemoteViews.setFloat(id, "setTextSize", curTextSize);
}
}
private void updateTopViews() {
remoteViews.setInt(R.id.table_month, "setTextColor", textColor);
mRemoteViews.setInt(R.id.table_month, "setTextColor", mTextColor);
Bitmap bmp = getColoredIcon(cxt, textColor, R.mipmap.arrow_left);
remoteViews.setImageViewBitmap(R.id.left_arrow, bmp);
Bitmap bmp = getColoredIcon(mContext, mTextColor, R.mipmap.arrow_left);
mRemoteViews.setImageViewBitmap(R.id.left_arrow, bmp);
bmp = getColoredIcon(cxt, textColor, R.mipmap.arrow_right);
remoteViews.setImageViewBitmap(R.id.right_arrow, bmp);
bmp = getColoredIcon(mContext, mTextColor, R.mipmap.arrow_right);
mRemoteViews.setImageViewBitmap(R.id.right_arrow, bmp);
}
public void updateMonth(String month) {
remoteViews.setTextViewText(R.id.table_month, month);
mRemoteViews.setTextViewText(R.id.table_month, month);
}
@Override
@ -161,10 +162,10 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calendar {
}
private void updateLabelColor() {
final String packageName = cxt.getPackageName();
final String packageName = mContext.getPackageName();
for (int i = 0; i < 7; i++) {
final int id = res.getIdentifier("label_" + i, "id", packageName);
remoteViews.setInt(id, "setTextColor", weakTextColor);
final int id = mRes.getIdentifier("label_" + i, "id", packageName);
mRemoteViews.setInt(id, "setTextColor", mWeakTextColor);
}
}

View File

@ -10,7 +10,6 @@ import android.widget.TextView;
import com.simplemobiletools.calendar.BuildConfig;
import com.simplemobiletools.calendar.R;
import com.simplemobiletools.calendar.activities.LicenseActivity;
import java.util.Calendar;
@ -19,17 +18,18 @@ import butterknife.ButterKnife;
import butterknife.OnClick;
public class AboutActivity extends AppCompatActivity {
@BindView(R.id.about_copyright) TextView copyright;
@BindView(R.id.about_version) TextView version;
@BindView(R.id.about_email) TextView emailTV;
private Resources res;
@BindView(R.id.about_copyright) TextView mCopyright;
@BindView(R.id.about_version) TextView mVersion;
@BindView(R.id.about_email) TextView mEmailTV;
private static Resources mRes;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
ButterKnife.bind(this);
res = getResources();
mRes = getResources();
setupEmail();
setupVersion();
@ -37,23 +37,23 @@ public class AboutActivity extends AppCompatActivity {
}
private void setupEmail() {
final String email = res.getString(R.string.email);
final String appName = res.getString(R.string.app_name);
final String email = mRes.getString(R.string.email);
final String appName = mRes.getString(R.string.app_name);
final String href = "<a href=\"mailto:" + email + "?subject=" + appName + "\">" + email + "</a>";
emailTV.setText(Html.fromHtml(href));
emailTV.setMovementMethod(LinkMovementMethod.getInstance());
mEmailTV.setText(Html.fromHtml(href));
mEmailTV.setMovementMethod(LinkMovementMethod.getInstance());
}
private void setupVersion() {
final String versionName = BuildConfig.VERSION_NAME;
final String versionText = String.format(res.getString(R.string.version), versionName);
version.setText(versionText);
final String versionText = String.format(mRes.getString(R.string.version), versionName);
mVersion.setText(versionText);
}
private void setupCopyright() {
final int year = Calendar.getInstance().get(Calendar.YEAR);
final String copyrightText = String.format(res.getString(R.string.copyright), year);
copyright.setText(copyrightText);
final String copyrightText = String.format(mRes.getString(R.string.copyright), year);
mCopyright.setText(copyrightText);
}
@OnClick(R.id.about_license)

View File

@ -11,6 +11,7 @@ import butterknife.ButterKnife;
import butterknife.OnClick;
public class LicenseActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

View File

@ -9,7 +9,6 @@ import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.DatePicker;
@ -36,19 +35,21 @@ import butterknife.ButterKnife;
import butterknife.OnClick;
public class MainActivity extends AppCompatActivity implements Calendar {
@BindView(R.id.left_arrow) ImageView leftArrow;
@BindView(R.id.right_arrow) ImageView rightArrow;
@BindView(R.id.table_month) TextView monthTV;
@BindView(R.id.calendar_holder) View calendarHolder;
@BindDimen(R.dimen.day_text_size) float dayTextSize;
@BindDimen(R.dimen.today_text_size) float todayTextSize;
@BindDimen(R.dimen.activity_margin) int activityMargin;
@BindView(R.id.left_arrow) ImageView mLeftArrow;
@BindView(R.id.right_arrow) ImageView mRightArrow;
@BindView(R.id.table_month) TextView mMonthTV;
@BindView(R.id.calendar_holder) View mCalendarHolder;
private CalendarImpl calendar;
private Resources res;
private String packageName;
private int textColor;
private int weakTextColor;
@BindDimen(R.dimen.day_text_size) float mDayTextSize;
@BindDimen(R.dimen.today_text_size) float mTodayTextSize;
@BindDimen(R.dimen.activity_margin) int mActivityMargin;
private CalendarImpl mCalendar;
private Resources mRes;
private String mPackageName;
private int mTextColor;
private int mWeakTextColor;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -57,28 +58,27 @@ public class MainActivity extends AppCompatActivity implements Calendar {
ButterKnife.bind(this);
Locale.setDefault(Locale.ENGLISH);
textColor = Helpers.adjustAlpha(Color.BLACK, Constants.HIGH_ALPHA);
weakTextColor = Helpers.adjustAlpha(Color.BLACK, Constants.LOW_ALPHA);
leftArrow.getDrawable().mutate().setColorFilter(textColor, PorterDuff.Mode.SRC_ATOP);
rightArrow.getDrawable().mutate().setColorFilter(textColor, PorterDuff.Mode.SRC_ATOP);
mTextColor = Helpers.adjustAlpha(Color.BLACK, Constants.HIGH_ALPHA);
mWeakTextColor = Helpers.adjustAlpha(Color.BLACK, Constants.LOW_ALPHA);
mLeftArrow.getDrawable().mutate().setColorFilter(mTextColor, PorterDuff.Mode.SRC_ATOP);
mRightArrow.getDrawable().mutate().setColorFilter(mTextColor, PorterDuff.Mode.SRC_ATOP);
res = getResources();
packageName = getPackageName();
dayTextSize /= res.getDisplayMetrics().density;
todayTextSize /= res.getDisplayMetrics().density;
mRes = getResources();
mPackageName = getPackageName();
mDayTextSize /= mRes.getDisplayMetrics().density;
mTodayTextSize /= mRes.getDisplayMetrics().density;
setupLabels();
final FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) calendarHolder.getLayoutParams();
params.setMargins(activityMargin, activityMargin, activityMargin, activityMargin);
final FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mCalendarHolder.getLayoutParams();
params.setMargins(mActivityMargin, mActivityMargin, mActivityMargin, mActivityMargin);
calendar = new CalendarImpl(this);
calendar.updateCalendar(new DateTime());
mCalendar = new CalendarImpl(this);
mCalendar.updateCalendar(new DateTime());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
@ -99,16 +99,16 @@ public class MainActivity extends AppCompatActivity implements Calendar {
for (int i = 0; i < len; i++) {
final Day day = days.get(i);
final TextView dayTV = (TextView) findViewById(res.getIdentifier("day_" + i, "id", packageName));
int curTextColor = weakTextColor;
float curTextSize = dayTextSize;
final TextView dayTV = (TextView) findViewById(mRes.getIdentifier("day_" + i, "id", mPackageName));
int curTextColor = mWeakTextColor;
float curTextSize = mDayTextSize;
if (day.getIsThisMonth()) {
curTextColor = textColor;
curTextColor = mTextColor;
}
if (day.getIsToday()) {
curTextSize = todayTextSize;
curTextSize = mTodayTextSize;
}
dayTV.setText(String.valueOf(day.getValue()));
@ -119,12 +119,12 @@ public class MainActivity extends AppCompatActivity implements Calendar {
@OnClick(R.id.left_arrow)
public void leftArrowClicked() {
calendar.getPrevMonth();
mCalendar.getPrevMonth();
}
@OnClick(R.id.right_arrow)
public void rightArrowClicked() {
calendar.getNextMonth();
mCalendar.getNextMonth();
}
@OnClick(R.id.table_month)
@ -134,7 +134,7 @@ public class MainActivity extends AppCompatActivity implements Calendar {
final DatePicker datePicker = (DatePicker) view.findViewById(R.id.date_picker);
hideDayPicker(datePicker);
final DateTime dateTime = new DateTime(calendar.getTargetDate().toString());
final DateTime dateTime = new DateTime(mCalendar.getTargetDate().toString());
datePicker.init(dateTime.getYear(), dateTime.getMonthOfYear() - 1, 1, null);
alertDialog.setView(view);
@ -142,7 +142,7 @@ public class MainActivity extends AppCompatActivity implements Calendar {
public void onClick(DialogInterface dialog, int id) {
final int month = datePicker.getMonth() + 1;
final int year = datePicker.getYear();
calendar.updateCalendar(new DateTime().withMonthOfYear(month).withYear(year));
mCalendar.updateCalendar(new DateTime().withMonthOfYear(month).withYear(year));
}
});
@ -162,15 +162,15 @@ public class MainActivity extends AppCompatActivity implements Calendar {
}
private void updateMonth(String month) {
monthTV.setText(month);
monthTV.setTextColor(textColor);
mMonthTV.setText(month);
mMonthTV.setTextColor(mTextColor);
}
private void setupLabels() {
for (int i = 0; i < 7; i++) {
final TextView dayTV = (TextView) findViewById(res.getIdentifier("label_" + i, "id", packageName));
dayTV.setTextSize(dayTextSize);
dayTV.setTextColor(weakTextColor);
final TextView dayTV = (TextView) findViewById(mRes.getIdentifier("label_" + i, "id", mPackageName));
dayTV.setTextSize(mDayTextSize);
dayTV.setTextColor(mWeakTextColor);
}
}
}

View File

@ -0,0 +1,255 @@
package com.simplemobiletools.calendar.activities;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import com.simplemobiletools.calendar.Calendar;
import com.simplemobiletools.calendar.CalendarImpl;
import com.simplemobiletools.calendar.Constants;
import com.simplemobiletools.calendar.Day;
import com.simplemobiletools.calendar.Helpers;
import com.simplemobiletools.calendar.MyWidgetProvider;
import com.simplemobiletools.calendar.R;
import org.joda.time.DateTime;
import java.util.List;
import butterknife.BindDimen;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import yuku.ambilwarna.AmbilWarnaDialog;
public class WidgetConfigureActivity extends AppCompatActivity implements Calendar {
@BindView(R.id.left_arrow) ImageView mLeftArrow;
@BindView(R.id.right_arrow) ImageView mRightArrow;
@BindView(R.id.table_month) TextView mMonthTV;
@BindView(R.id.config_bg_color) View mBgColorPicker;
@BindView(R.id.config_bg_seekbar) SeekBar mBgSeekBar;
@BindView(R.id.config_text_color) View mTextColorPicker;
@BindView(R.id.config_calendar) View mWidgetBackground;
@BindView(R.id.config_save) Button mSaveBtn;
@BindDimen(R.dimen.day_text_size) float mDayTextSize;
@BindDimen(R.dimen.today_text_size) float mTodayTextSize;
private static CalendarImpl mCalendar;
private static Resources mRes;
private static String mPackageName;
private List<Day> mDays;
private float mBgAlpha;
private int mWidgetId;
private int mBgColorWithoutTransparency;
private int mBgColor;
private int mTextColorWithoutTransparency;
private int mTextColor;
private int mWeakTextColor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setResult(RESULT_CANCELED);
setContentView(R.layout.widget_config);
ButterKnife.bind(this);
initVariables();
final Intent intent = getIntent();
final Bundle extras = intent.getExtras();
if (extras != null)
mWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
if (mWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID)
finish();
}
private void initVariables() {
mRes = getResources();
mPackageName = getPackageName();
mDayTextSize /= mRes.getDisplayMetrics().density;
mTodayTextSize /= mRes.getDisplayMetrics().density;
final SharedPreferences prefs = initPrefs(this);
mTextColorWithoutTransparency = prefs.getInt(Constants.WIDGET_TEXT_COLOR, getResources().getColor(R.color.colorPrimary));
updateTextColors();
mBgColor = prefs.getInt(Constants.WIDGET_BG_COLOR, 1);
if (mBgColor == 1) {
mBgColor = Color.BLACK;
mBgAlpha = .2f;
} else {
mBgAlpha = Color.alpha(mBgColor) / (float) 255;
}
mBgColorWithoutTransparency = Color.rgb(Color.red(mBgColor), Color.green(mBgColor), Color.blue(mBgColor));
mBgSeekBar.setOnSeekBarChangeListener(bgSeekbarChangeListener);
mBgSeekBar.setProgress((int) (mBgAlpha * 100));
updateBgColor();
mCalendar = new CalendarImpl(this);
mCalendar.updateCalendar(new DateTime());
}
private SharedPreferences initPrefs(Context context) {
return context.getSharedPreferences(Constants.PREFS, Context.MODE_PRIVATE);
}
@OnClick(R.id.config_save)
public void saveConfig() {
storeWidgetColors();
requestWidgetUpdate();
final Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mWidgetId);
setResult(RESULT_OK, resultValue);
finish();
}
private void storeWidgetColors() {
final SharedPreferences prefs = getSharedPreferences(Constants.PREFS, Context.MODE_PRIVATE);
prefs.edit().putInt(Constants.WIDGET_BG_COLOR, mBgColor).apply();
prefs.edit().putInt(Constants.WIDGET_TEXT_COLOR, mTextColorWithoutTransparency).apply();
}
@OnClick(R.id.config_bg_color)
public void pickBackgroundColor() {
AmbilWarnaDialog dialog = new AmbilWarnaDialog(this, mBgColorWithoutTransparency, new AmbilWarnaDialog.OnAmbilWarnaListener() {
@Override
public void onCancel(AmbilWarnaDialog dialog) {
}
@Override
public void onOk(AmbilWarnaDialog dialog, int color) {
mBgColorWithoutTransparency = color;
updateBgColor();
}
});
dialog.show();
}
@OnClick(R.id.config_text_color)
public void pickTextColor() {
AmbilWarnaDialog dialog = new AmbilWarnaDialog(this, mTextColor, new AmbilWarnaDialog.OnAmbilWarnaListener() {
@Override
public void onCancel(AmbilWarnaDialog dialog) {
}
@Override
public void onOk(AmbilWarnaDialog dialog, int color) {
mTextColorWithoutTransparency = color;
updateTextColors();
updateDays();
}
});
dialog.show();
}
private void requestWidgetUpdate() {
final Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, this, MyWidgetProvider.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[]{mWidgetId});
sendBroadcast(intent);
}
private void updateTextColors() {
mTextColor = Helpers.adjustAlpha(mTextColorWithoutTransparency, Constants.HIGH_ALPHA);
mWeakTextColor = Helpers.adjustAlpha(mTextColorWithoutTransparency, Constants.LOW_ALPHA);
mLeftArrow.getDrawable().mutate().setColorFilter(mTextColor, PorterDuff.Mode.SRC_ATOP);
mRightArrow.getDrawable().mutate().setColorFilter(mTextColor, PorterDuff.Mode.SRC_ATOP);
mMonthTV.setTextColor(mTextColor);
mTextColorPicker.setBackgroundColor(mTextColor);
mSaveBtn.setTextColor(mTextColor);
updateLabels();
}
private void updateBgColor() {
mBgColor = Helpers.adjustAlpha(mBgColorWithoutTransparency, mBgAlpha);
mWidgetBackground.setBackgroundColor(mBgColor);
mBgColorPicker.setBackgroundColor(mBgColor);
mSaveBtn.setBackgroundColor(mBgColor);
}
private void updateDays() {
final int len = mDays.size();
for (int i = 0; i < len; i++) {
final Day day = mDays.get(i);
final TextView dayTV = (TextView) findViewById(mRes.getIdentifier("day_" + i, "id", mPackageName));
int curTextColor = mWeakTextColor;
float curTextSize = mDayTextSize;
if (day.getIsThisMonth()) {
curTextColor = mTextColor;
}
if (day.getIsToday()) {
curTextSize = mTodayTextSize;
}
dayTV.setText(String.valueOf(day.getValue()));
dayTV.setTextColor(curTextColor);
dayTV.setTextSize(curTextSize);
}
}
private SeekBar.OnSeekBarChangeListener bgSeekbarChangeListener = new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
mBgAlpha = (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() {
mCalendar.getPrevMonth();
}
@OnClick(R.id.right_arrow)
public void rightArrowClicked() {
mCalendar.getNextMonth();
}
@Override
public void updateCalendar(String month, List<Day> days) {
this.mDays = days;
updateMonth(month);
updateDays();
}
private void updateMonth(String month) {
mMonthTV.setText(month);
}
private void updateLabels() {
for (int i = 0; i < 7; i++) {
final TextView dayTV = (TextView) findViewById(mRes.getIdentifier("label_" + i, "id", mPackageName));
dayTV.setTextSize(mDayTextSize);
dayTV.setTextColor(mWeakTextColor);
}
}
}

View File

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