convert widgetconfigureactivity to kotlin
This commit is contained in:
parent
32f4a00a7b
commit
3b02ad0daf
3 changed files with 169 additions and 193 deletions
|
@ -1,192 +0,0 @@
|
|||
package com.simplemobiletools.musicplayer.activities;
|
||||
|
||||
import android.appwidget.AppWidgetManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
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.RemoteViews;
|
||||
import android.widget.SeekBar;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.simplemobiletools.musicplayer.Constants;
|
||||
import com.simplemobiletools.musicplayer.helpers.MyWidgetProvider;
|
||||
import com.simplemobiletools.musicplayer.R;
|
||||
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
import butterknife.OnClick;
|
||||
import yuku.ambilwarna.AmbilWarnaDialog;
|
||||
|
||||
public class WidgetConfigureActivity extends AppCompatActivity {
|
||||
@BindView(R.id.config_bg_seekbar) SeekBar mBgSeekBar;
|
||||
@BindView(R.id.config_player) View mWidgetBackground;
|
||||
@BindView(R.id.config_bg_color) View mBgColorPicker;
|
||||
@BindView(R.id.config_text_color) View mTextColorPicker;
|
||||
@BindView(R.id.config_save) Button mSaveBtn;
|
||||
|
||||
@BindView(R.id.songTitle) TextView mSongTitle;
|
||||
@BindView(R.id.songArtist) TextView mSongArtist;
|
||||
|
||||
@BindView(R.id.previousBtn) ImageView mPrevBtn;
|
||||
@BindView(R.id.playPauseBtn) ImageView mPlayPauseBtn;
|
||||
@BindView(R.id.nextBtn) ImageView mNextBtn;
|
||||
|
||||
private static float mBgAlpha;
|
||||
private static int mWidgetId;
|
||||
private static int mBgColor;
|
||||
private static int mBgColorWithoutTransparency;
|
||||
private static 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();
|
||||
}
|
||||
|
||||
private void initVariables() {
|
||||
final SharedPreferences prefs = getSharedPreferences(Constants.PREFS, 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(seekbarChangeListener);
|
||||
mBgSeekBar.setProgress((int) (mBgAlpha * 100));
|
||||
updateBackgroundColor();
|
||||
|
||||
mTextColor = prefs.getInt(Constants.WIDGET_TEXT_COLOR, getResources().getColor(R.color.colorPrimary));
|
||||
updateTextColor();
|
||||
|
||||
mSongTitle.setText("Song Title");
|
||||
mSongArtist.setText("Song Artist");
|
||||
}
|
||||
|
||||
@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", mBgColor);
|
||||
appWidgetManager.updateAppWidget(mWidgetId, views);
|
||||
|
||||
storeWidgetColors();
|
||||
requestWidgetUpdate();
|
||||
|
||||
final Intent resultValue = new Intent();
|
||||
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mWidgetId);
|
||||
setResult(RESULT_OK, resultValue);
|
||||
finish();
|
||||
}
|
||||
|
||||
@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 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, 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);
|
||||
mWidgetBackground.setBackgroundColor(mBgColor);
|
||||
mBgColorPicker.setBackgroundColor(mBgColor);
|
||||
mSaveBtn.setBackgroundColor(mBgColor);
|
||||
}
|
||||
|
||||
private void updateTextColor() {
|
||||
mTextColorPicker.setBackgroundColor(mTextColor);
|
||||
|
||||
mSaveBtn.setTextColor(mTextColor);
|
||||
mSongTitle.setTextColor(mTextColor);
|
||||
mSongArtist.setTextColor(mTextColor);
|
||||
|
||||
mPrevBtn.getDrawable().mutate().setColorFilter(mTextColor, PorterDuff.Mode.SRC_IN);
|
||||
mPlayPauseBtn.getDrawable().mutate().setColorFilter(mTextColor, PorterDuff.Mode.SRC_IN);
|
||||
mNextBtn.getDrawable().mutate().setColorFilter(mTextColor, PorterDuff.Mode.SRC_IN);
|
||||
}
|
||||
|
||||
private SeekBar.OnSeekBarChangeListener seekbarChangeListener = 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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,166 @@
|
|||
package com.simplemobiletools.musicplayer.activities
|
||||
|
||||
import android.app.Activity
|
||||
import android.appwidget.AppWidgetManager
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.graphics.Color
|
||||
import android.graphics.PorterDuff
|
||||
import android.os.Bundle
|
||||
import android.support.v7.app.AppCompatActivity
|
||||
import android.widget.RemoteViews
|
||||
import android.widget.SeekBar
|
||||
import com.simplemobiletools.musicplayer.Constants
|
||||
import com.simplemobiletools.musicplayer.R
|
||||
import com.simplemobiletools.musicplayer.helpers.MyWidgetProvider
|
||||
import kotlinx.android.synthetic.main.widget.*
|
||||
import kotlinx.android.synthetic.main.widget_config.*
|
||||
import kotlinx.android.synthetic.main.widget_controls.*
|
||||
import yuku.ambilwarna.AmbilWarnaDialog
|
||||
|
||||
class WidgetConfigureActivity : AppCompatActivity() {
|
||||
companion object {
|
||||
private var mBgAlpha = 0.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(Activity.RESULT_CANCELED)
|
||||
setContentView(R.layout.widget_config)
|
||||
initVariables()
|
||||
|
||||
val intent = intent
|
||||
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() }
|
||||
}
|
||||
|
||||
private fun initVariables() {
|
||||
val prefs = getSharedPreferences(Constants.PREFS, 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.setOnSeekBarChangeListener(seekbarChangeListener)
|
||||
config_bg_seekbar.progress = (mBgAlpha * 100).toInt()
|
||||
updateBackgroundColor()
|
||||
|
||||
mTextColor = prefs.getInt(Constants.WIDGET_TEXT_COLOR, resources.getColor(R.color.colorPrimary))
|
||||
updateTextColor()
|
||||
}
|
||||
|
||||
fun saveConfig() {
|
||||
val appWidgetManager = AppWidgetManager.getInstance(this)
|
||||
val views = RemoteViews(packageName, R.layout.widget)
|
||||
views.setInt(R.id.widget_holder, "setBackgroundColor", mBgColor)
|
||||
appWidgetManager.updateAppWidget(mWidgetId, views)
|
||||
|
||||
storeWidgetColors()
|
||||
requestWidgetUpdate()
|
||||
|
||||
Intent().apply {
|
||||
putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mWidgetId)
|
||||
setResult(Activity.RESULT_OK, this)
|
||||
}
|
||||
finish()
|
||||
}
|
||||
|
||||
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 fun storeWidgetColors() {
|
||||
getSharedPreferences(Constants.PREFS, 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)
|
||||
config_player.setBackgroundColor(mBgColor)
|
||||
config_bg_color.setBackgroundColor(mBgColor)
|
||||
config_save.setBackgroundColor(mBgColor)
|
||||
}
|
||||
|
||||
private fun updateTextColor() {
|
||||
config_text_color.setBackgroundColor(mTextColor)
|
||||
|
||||
config_save.setTextColor(mTextColor)
|
||||
songTitle.setTextColor(mTextColor)
|
||||
songArtist.setTextColor(mTextColor)
|
||||
|
||||
previousBtn.drawable.mutate().setColorFilter(mTextColor, PorterDuff.Mode.SRC_IN)
|
||||
playPauseBtn.drawable.mutate().setColorFilter(mTextColor, PorterDuff.Mode.SRC_IN)
|
||||
nextBtn.drawable.mutate().setColorFilter(mTextColor, PorterDuff.Mode.SRC_IN)
|
||||
}
|
||||
|
||||
private val seekbarChangeListener = 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)
|
||||
}
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RelativeLayout
|
||||
android:id="@+id/widget_holder"
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/widget_holder"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="@android:color/black"
|
||||
|
@ -17,6 +17,7 @@
|
|||
android:ellipsize="end"
|
||||
android:gravity="center"
|
||||
android:lines="1"
|
||||
android:text="@string/title"
|
||||
android:textColor="@android:color/white"
|
||||
android:textSize="18sp"/>
|
||||
|
||||
|
@ -31,6 +32,7 @@
|
|||
android:ellipsize="end"
|
||||
android:gravity="center"
|
||||
android:lines="1"
|
||||
android:text="@string/artist"
|
||||
android:textColor="@android:color/white"
|
||||
android:textSize="16sp"/>
|
||||
|
||||
|
|
Loading…
Reference in a new issue