diff --git a/app/src/main/java/com/simplemobiletools/notes/activities/WidgetConfigureActivity.java b/app/src/main/java/com/simplemobiletools/notes/activities/WidgetConfigureActivity.java deleted file mode 100644 index e4fecf1b..00000000 --- a/app/src/main/java/com/simplemobiletools/notes/activities/WidgetConfigureActivity.java +++ /dev/null @@ -1,182 +0,0 @@ -package com.simplemobiletools.notes.activities; - -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.support.v7.app.AppCompatActivity; -import android.util.TypedValue; -import android.view.View; -import android.widget.Button; -import android.widget.RemoteViews; -import android.widget.SeekBar; -import android.widget.TextView; - -import com.simplemobiletools.notes.Constants; -import com.simplemobiletools.notes.MyWidgetProvider; -import com.simplemobiletools.notes.R; -import com.simplemobiletools.notes.Utils; - -import butterknife.BindView; -import butterknife.ButterKnife; -import butterknife.OnClick; -import yuku.ambilwarna.AmbilWarnaDialog; - -public class WidgetConfigureActivity extends AppCompatActivity { - @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.notes_view) TextView mNotesView; - @BindView(R.id.config_save) Button mSaveBtn; - - private float mBgAlpha; - private int mWidgetId; - private int mBgColor; - private int mBgColorWithoutTransparency; - private int mTextColor; - - @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(); - } - - @Override - protected void onResume() { - super.onResume(); - mNotesView.setTextSize(TypedValue.COMPLEX_UNIT_PX, Utils.INSTANCE.getTextSize(getApplicationContext())); - } - - private void initVariables() { - final SharedPreferences prefs = getSharedPreferences(Constants.PREFS_KEY, Context.MODE_PRIVATE); - 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)); - updateBackgroundColor(); - - mTextColor = prefs.getInt(Constants.WIDGET_TEXT_COLOR, getResources().getColor(R.color.colorPrimary)); - updateTextColor(); - } - - @OnClick(R.id.config_save) - public void saveConfig() { - final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this); - final RemoteViews views = new RemoteViews(getPackageName(), R.layout.activity_main); - views.setInt(R.id.notes_view, "setBackgroundColor", mBgColor); - appWidgetManager.updateAppWidget(mWidgetId, views); - - storeWidgetBackground(); - requestWidgetUpdate(); - - final Intent resultValue = new Intent(); - resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mWidgetId); - setResult(RESULT_OK, resultValue); - finish(); - } - - private void storeWidgetBackground() { - final SharedPreferences prefs = getSharedPreferences(Constants.PREFS_KEY, Context.MODE_PRIVATE); - prefs.edit().putInt(Constants.WIDGET_BG_COLOR, mBgColor).apply(); - prefs.edit().putInt(Constants.WIDGET_TEXT_COLOR, mTextColor).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[]{mWidgetId}); - sendBroadcast(intent); - } - - private void updateBackgroundColor() { - mBgColor = adjustAlpha(mBgColorWithoutTransparency, mBgAlpha); - mNotesView.setBackgroundColor(mBgColor); - mBgColorPicker.setBackgroundColor(mBgColor); - mSaveBtn.setBackgroundColor(mBgColor); - } - - private void updateTextColor() { - mTextColorPicker.setBackgroundColor(mTextColor); - mSaveBtn.setTextColor(mTextColor); - mNotesView.setTextColor(mTextColor); - } - - @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; - updateBackgroundColor(); - } - }); - - 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) { - mTextColor = color; - updateTextColor(); - } - }); - - dialog.show(); - } - - private SeekBar.OnSeekBarChangeListener bgSeekbarChangeListener = new SeekBar.OnSeekBarChangeListener() { - @Override - public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { - mBgAlpha = (float) progress / (float) 100; - updateBackgroundColor(); - } - - @Override - public void onStartTrackingTouch(SeekBar seekBar) { - - } - - @Override - public void onStopTrackingTouch(SeekBar seekBar) { - - } - }; - - private int adjustAlpha(int color, float factor) { - final int alpha = Math.round(Color.alpha(color) * factor); - final int red = Color.red(color); - final int green = Color.green(color); - final int blue = Color.blue(color); - return Color.argb(alpha, red, green, blue); - } -} diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/activities/WidgetConfigureActivity.kt b/app/src/main/kotlin/com/simplemobiletools/notes/activities/WidgetConfigureActivity.kt new file mode 100644 index 00000000..07d8c2f7 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/notes/activities/WidgetConfigureActivity.kt @@ -0,0 +1,162 @@ +package com.simplemobiletools.notes.activities + +import android.app.Activity +import android.appwidget.AppWidgetManager +import android.content.Context +import android.content.Intent +import android.graphics.Color +import android.os.Bundle +import android.support.v7.app.AppCompatActivity +import android.util.TypedValue +import android.widget.RemoteViews +import android.widget.SeekBar +import com.simplemobiletools.notes.Constants +import com.simplemobiletools.notes.MyWidgetProvider +import com.simplemobiletools.notes.R +import com.simplemobiletools.notes.Utils +import kotlinx.android.synthetic.main.widget_config.* +import yuku.ambilwarna.AmbilWarnaDialog + +class WidgetConfigureActivity : AppCompatActivity() { + private var mBgAlpha = 0f + private var mWidgetId = 0 + private var mBgColor = 0 + private var mBgColorWithoutTransparency = 0 + private var mTextColor = 0 + + public override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setResult(RESULT_CANCELED) + setContentView(R.layout.widget_config) + initVariables() + + val extras = intent.extras + if (extras != null) + mWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID) + + if (mWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) + finish() + + config_save.setOnClickListener { saveConfig() } + config_bg_color.setOnClickListener { pickBackgroundColor() } + config_text_color.setOnClickListener { pickTextColor() } + } + + override fun onResume() { + super.onResume() + notes_view.setTextSize(TypedValue.COMPLEX_UNIT_PX, Utils.getTextSize(applicationContext)) + } + + private fun initVariables() { + val prefs = getSharedPreferences(Constants.PREFS_KEY, Context.MODE_PRIVATE) + mBgColor = prefs.getInt(Constants.WIDGET_BG_COLOR, 1) + if (mBgColor == 1) { + mBgColor = Color.BLACK + mBgAlpha = .2f + } else { + mBgAlpha = Color.alpha(mBgColor) / 255.toFloat() + } + + mBgColorWithoutTransparency = Color.rgb(Color.red(mBgColor), Color.green(mBgColor), Color.blue(mBgColor)) + config_bg_seekbar.apply { + setOnSeekBarChangeListener(bgSeekbarChangeListener) + progress = (mBgAlpha * 100).toInt() + } + updateBackgroundColor() + + mTextColor = prefs.getInt(Constants.WIDGET_TEXT_COLOR, resources.getColor(R.color.colorPrimary)) + updateTextColor() + } + + fun saveConfig() { + val views = RemoteViews(packageName, R.layout.activity_main) + views.setInt(R.id.notes_view, "setBackgroundColor", mBgColor) + AppWidgetManager.getInstance(this).updateAppWidget(mWidgetId, views) + + storeWidgetBackground() + requestWidgetUpdate() + + Intent().apply { + putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mWidgetId) + setResult(Activity.RESULT_OK, this) + } + finish() + } + + private fun storeWidgetBackground() { + getSharedPreferences(Constants.PREFS_KEY, Context.MODE_PRIVATE).apply { + edit().putInt(Constants.WIDGET_BG_COLOR, mBgColor).putInt(Constants.WIDGET_TEXT_COLOR, mTextColor).apply() + } + } + + private fun requestWidgetUpdate() { + Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, this, MyWidgetProvider::class.java).apply { + putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, intArrayOf(mWidgetId)) + sendBroadcast(this) + } + } + + private fun updateBackgroundColor() { + mBgColor = adjustAlpha(mBgColorWithoutTransparency, mBgAlpha) + notes_view.setBackgroundColor(mBgColor) + config_bg_color.setBackgroundColor(mBgColor) + config_save.setBackgroundColor(mBgColor) + } + + private fun updateTextColor() { + config_text_color.setBackgroundColor(mTextColor) + config_save.setTextColor(mTextColor) + notes_view.setTextColor(mTextColor) + } + + fun pickBackgroundColor() { + val dialog = AmbilWarnaDialog(this, mBgColorWithoutTransparency, object : AmbilWarnaDialog.OnAmbilWarnaListener { + override fun onCancel(dialog: AmbilWarnaDialog) { + } + + override fun onOk(dialog: AmbilWarnaDialog, color: Int) { + mBgColorWithoutTransparency = color + updateBackgroundColor() + } + }) + + dialog.show() + } + + fun pickTextColor() { + val dialog = AmbilWarnaDialog(this, mTextColor, object : AmbilWarnaDialog.OnAmbilWarnaListener { + override fun onCancel(dialog: AmbilWarnaDialog) { + } + + override fun onOk(dialog: AmbilWarnaDialog, color: Int) { + mTextColor = color + updateTextColor() + } + }) + + dialog.show() + } + + private val bgSeekbarChangeListener = object : SeekBar.OnSeekBarChangeListener { + override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) { + mBgAlpha = progress.toFloat() / 100.toFloat() + updateBackgroundColor() + } + + override fun onStartTrackingTouch(seekBar: SeekBar) { + + } + + override fun onStopTrackingTouch(seekBar: SeekBar) { + + } + } + + private fun adjustAlpha(color: Int, factor: Float): Int { + val alpha = Math.round(Color.alpha(color) * factor) + val red = Color.red(color) + val green = Color.green(color) + val blue = Color.blue(color) + return Color.argb(alpha, red, green, blue) + } +}