|
| 1 | +package com.simplemobiletools.flashlight.activities; |
| 2 | + |
| 3 | +import android.appwidget.AppWidgetManager; |
| 4 | +import android.content.Context; |
| 5 | +import android.content.Intent; |
| 6 | +import android.content.SharedPreferences; |
| 7 | +import android.graphics.Color; |
| 8 | +import android.graphics.PorterDuff; |
| 9 | +import android.os.Bundle; |
| 10 | +import android.support.v7.app.AppCompatActivity; |
| 11 | +import android.view.View; |
| 12 | +import android.widget.ImageView; |
| 13 | +import android.widget.RemoteViews; |
| 14 | +import android.widget.SeekBar; |
| 15 | + |
| 16 | +import com.simplemobiletools.flashlight.Constants; |
| 17 | +import com.simplemobiletools.flashlight.MyWidgetProvider; |
| 18 | +import com.simplemobiletools.flashlight.R; |
| 19 | + |
| 20 | +import butterknife.BindView; |
| 21 | +import butterknife.ButterKnife; |
| 22 | +import butterknife.OnClick; |
| 23 | + |
| 24 | +public class WidgetConfigureActivity extends AppCompatActivity { |
| 25 | + @BindView(R.id.config_widget_seekbar) SeekBar mWidgetSeekBar; |
| 26 | + @BindView(R.id.config_widget_color) View mWidgetColorPicker; |
| 27 | + @BindView(R.id.config_image) ImageView mImage; |
| 28 | + |
| 29 | + private static float mWidgetAlpha; |
| 30 | + private static int mWidgetId; |
| 31 | + private static int mWidgetColor; |
| 32 | + private static int mWidgetColorWithoutTransparency; |
| 33 | + |
| 34 | + @Override |
| 35 | + public void onCreate(Bundle savedInstanceState) { |
| 36 | + super.onCreate(savedInstanceState); |
| 37 | + setResult(RESULT_CANCELED); |
| 38 | + setContentView(R.layout.widget_config); |
| 39 | + ButterKnife.bind(this); |
| 40 | + initVariables(); |
| 41 | + |
| 42 | + final Intent intent = getIntent(); |
| 43 | + final Bundle extras = intent.getExtras(); |
| 44 | + if (extras != null) |
| 45 | + mWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); |
| 46 | + |
| 47 | + if (mWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) |
| 48 | + finish(); |
| 49 | + } |
| 50 | + |
| 51 | + private void initVariables() { |
| 52 | + final SharedPreferences prefs = getSharedPreferences(Constants.PREFS_KEY, Context.MODE_PRIVATE); |
| 53 | + mWidgetColor = prefs.getInt(Constants.WIDGET_COLOR, 1); |
| 54 | + if (mWidgetColor == 1) { |
| 55 | + mWidgetColor = getResources().getColor(R.color.colorPrimary); |
| 56 | + mWidgetAlpha = 1.f; |
| 57 | + } else { |
| 58 | + mWidgetAlpha = Color.alpha(mWidgetColor) / (float) 255; |
| 59 | + } |
| 60 | + |
| 61 | + mWidgetColorWithoutTransparency = Color.rgb(Color.red(mWidgetColor), Color.green(mWidgetColor), Color.blue(mWidgetColor)); |
| 62 | + mWidgetSeekBar.setOnSeekBarChangeListener(seekbarChangeListener); |
| 63 | + mWidgetSeekBar.setProgress((int) (mWidgetAlpha * 100)); |
| 64 | + updateBackgroundColor(); |
| 65 | + } |
| 66 | + |
| 67 | + @OnClick(R.id.config_save) |
| 68 | + public void saveConfig() { |
| 69 | + final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this); |
| 70 | + final RemoteViews views = new RemoteViews(getPackageName(), R.layout.widget); |
| 71 | + appWidgetManager.updateAppWidget(mWidgetId, views); |
| 72 | + |
| 73 | + storeWidgetColors(); |
| 74 | + requestWidgetUpdate(); |
| 75 | + |
| 76 | + final Intent resultValue = new Intent(); |
| 77 | + resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mWidgetId); |
| 78 | + setResult(RESULT_OK, resultValue); |
| 79 | + finish(); |
| 80 | + } |
| 81 | + |
| 82 | + @OnClick(R.id.config_widget_color) |
| 83 | + public void pickBackgroundColor() { |
| 84 | + |
| 85 | + } |
| 86 | + |
| 87 | + private void storeWidgetColors() { |
| 88 | + final SharedPreferences prefs = getSharedPreferences(Constants.PREFS_KEY, Context.MODE_PRIVATE); |
| 89 | + prefs.edit().putInt(Constants.WIDGET_COLOR, mWidgetColor).apply(); |
| 90 | + } |
| 91 | + |
| 92 | + private void requestWidgetUpdate() { |
| 93 | + final Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE, null, this, MyWidgetProvider.class); |
| 94 | + intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[]{mWidgetId}); |
| 95 | + sendBroadcast(intent); |
| 96 | + } |
| 97 | + |
| 98 | + private void updateBackgroundColor() { |
| 99 | + mWidgetColor = adjustAlpha(mWidgetColorWithoutTransparency, mWidgetAlpha); |
| 100 | + mWidgetColorPicker.setBackgroundColor(mWidgetColor); |
| 101 | + mImage.getDrawable().mutate().setColorFilter(mWidgetColor, PorterDuff.Mode.SRC_IN); |
| 102 | + } |
| 103 | + |
| 104 | + private SeekBar.OnSeekBarChangeListener seekbarChangeListener = new SeekBar.OnSeekBarChangeListener() { |
| 105 | + @Override |
| 106 | + public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { |
| 107 | + mWidgetAlpha = (float) progress / (float) 100; |
| 108 | + updateBackgroundColor(); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public void onStartTrackingTouch(SeekBar seekBar) { |
| 113 | + |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public void onStopTrackingTouch(SeekBar seekBar) { |
| 118 | + |
| 119 | + } |
| 120 | + }; |
| 121 | + |
| 122 | + private int adjustAlpha(int color, float factor) { |
| 123 | + final int alpha = Math.round(Color.alpha(color) * factor); |
| 124 | + final int red = Color.red(color); |
| 125 | + final int green = Color.green(color); |
| 126 | + final int blue = Color.blue(color); |
| 127 | + return Color.argb(alpha, red, green, blue); |
| 128 | + } |
| 129 | +} |
0 commit comments