mirror of
https://github.com/SimpleMobileTools/Simple-Notes.git
synced 2025-06-05 17:00:23 +02:00
add a widget configuration screen
This commit is contained in:
@ -3,4 +3,6 @@ package notes.simplemobiletools.com;
|
||||
public class Constants {
|
||||
public static final String PREFS = "prefs";
|
||||
public static final String TEXT = "text";
|
||||
public static final String WIDGET_BG_COLOR = "widget_bg_color";
|
||||
public static final String WIDGET_TEXT_COLOR = "widget_text_color";
|
||||
}
|
||||
|
@ -14,18 +14,21 @@ import android.view.inputmethod.InputMethodManager;
|
||||
import android.widget.EditText;
|
||||
import android.widget.Toast;
|
||||
|
||||
import butterknife.Bind;
|
||||
import butterknife.ButterKnife;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
private SharedPreferences prefs;
|
||||
private EditText notesView;
|
||||
@Bind(R.id.notes_view) EditText notesView;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
ButterKnife.bind(this);
|
||||
|
||||
prefs = getSharedPreferences(Constants.PREFS, Context.MODE_PRIVATE);
|
||||
final String text = prefs.getString(Constants.TEXT, "");
|
||||
notesView = (EditText) findViewById(R.id.notesView);
|
||||
notesView.setText(text);
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,170 @@
|
||||
package notes.simplemobiletools.com;
|
||||
|
||||
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.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.RemoteViews;
|
||||
import android.widget.SeekBar;
|
||||
import android.widget.TextView;
|
||||
|
||||
import butterknife.Bind;
|
||||
import butterknife.ButterKnife;
|
||||
import butterknife.OnClick;
|
||||
import yuku.ambilwarna.AmbilWarnaDialog;
|
||||
|
||||
public class MyWidgetConfigure extends AppCompatActivity {
|
||||
@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.notes_view) TextView notesView;
|
||||
@Bind(R.id.config_save) Button saveBtn;
|
||||
|
||||
private int widgetId;
|
||||
private int bgColor;
|
||||
private int bgColorWithoutTransparency;
|
||||
private float bgAlpha;
|
||||
private int textColor;
|
||||
|
||||
@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() {
|
||||
final SharedPreferences prefs = getSharedPreferences(Constants.PREFS, Context.MODE_PRIVATE);
|
||||
bgColor = prefs.getInt(Constants.WIDGET_BG_COLOR, 0);
|
||||
if (bgColor == 0) {
|
||||
bgColor = Color.BLACK;
|
||||
bgAlpha = .5f;
|
||||
} 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));
|
||||
updateBackgroundColor();
|
||||
|
||||
textColor = prefs.getInt(Constants.WIDGET_TEXT_COLOR, Color.WHITE);
|
||||
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", bgColor);
|
||||
appWidgetManager.updateAppWidget(widgetId, views);
|
||||
|
||||
storeWidgetBackground();
|
||||
requestWidgetUpdate();
|
||||
|
||||
final Intent resultValue = new Intent();
|
||||
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
|
||||
setResult(RESULT_OK, resultValue);
|
||||
finish();
|
||||
}
|
||||
|
||||
private void storeWidgetBackground() {
|
||||
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, textColor).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[]{widgetId});
|
||||
sendBroadcast(intent);
|
||||
}
|
||||
|
||||
private void updateBackgroundColor() {
|
||||
bgColor = adjustAlpha(bgColorWithoutTransparency, bgAlpha);
|
||||
notesView.setBackgroundColor(bgColor);
|
||||
bgColorPicker.setBackgroundColor(bgColor);
|
||||
saveBtn.setBackgroundColor(bgColor);
|
||||
}
|
||||
|
||||
private void updateTextColor() {
|
||||
textColorPicker.setBackgroundColor(textColor);
|
||||
saveBtn.setTextColor(textColor);
|
||||
notesView.setTextColor(textColor);
|
||||
}
|
||||
|
||||
@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;
|
||||
updateBackgroundColor();
|
||||
}
|
||||
});
|
||||
|
||||
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) {
|
||||
textColor = color;
|
||||
updateTextColor();
|
||||
}
|
||||
});
|
||||
|
||||
dialog.show();
|
||||
}
|
||||
|
||||
private SeekBar.OnSeekBarChangeListener bgSeekbarChangeListener = new SeekBar.OnSeekBarChangeListener() {
|
||||
@Override
|
||||
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
|
||||
bgAlpha = (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);
|
||||
}
|
||||
}
|
@ -4,6 +4,7 @@ import android.appwidget.AppWidgetManager;
|
||||
import android.appwidget.AppWidgetProvider;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.graphics.Color;
|
||||
import android.widget.RemoteViews;
|
||||
|
||||
public class MyWidgetProvider extends AppWidgetProvider {
|
||||
@ -12,16 +13,22 @@ public class MyWidgetProvider extends AppWidgetProvider {
|
||||
@Override
|
||||
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
|
||||
prefs = context.getSharedPreferences(Constants.PREFS, Context.MODE_PRIVATE);
|
||||
final RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
|
||||
final int defaultColor = context.getResources().getColor(R.color.dark_grey);
|
||||
final int newBgColor = prefs.getInt(Constants.WIDGET_BG_COLOR, defaultColor);
|
||||
final int newTextColor = prefs.getInt(Constants.WIDGET_TEXT_COLOR, Color.WHITE);
|
||||
remoteViews.setInt(R.id.notes_view, "setBackgroundColor", newBgColor);
|
||||
remoteViews.setInt(R.id.notes_view, "setTextColor", newTextColor);
|
||||
|
||||
for (int widgetId : appWidgetIds) {
|
||||
updateWidget(context, appWidgetManager, widgetId);
|
||||
updateWidget(appWidgetManager, widgetId, remoteViews);
|
||||
}
|
||||
super.onUpdate(context, appWidgetManager, appWidgetIds);
|
||||
}
|
||||
|
||||
private void updateWidget(Context context, AppWidgetManager widgetManager, int widgetId) {
|
||||
private void updateWidget(AppWidgetManager widgetManager, int widgetId, RemoteViews remoteViews) {
|
||||
final String text = prefs.getString(Constants.TEXT, "");
|
||||
final RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
|
||||
remoteViews.setTextViewText(R.id.notesView, text);
|
||||
remoteViews.setTextViewText(R.id.notes_view, text);
|
||||
widgetManager.updateAppWidget(widgetId, remoteViews);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user