mirror of
https://github.com/SimpleMobileTools/Simple-Notes.git
synced 2025-02-28 17:18:00 +01:00
add a widget configuration screen
This commit is contained in:
parent
60ab699dab
commit
00a93e19ea
@ -23,4 +23,6 @@ dependencies {
|
|||||||
compile fileTree(dir: 'libs', include: ['*.jar'])
|
compile fileTree(dir: 'libs', include: ['*.jar'])
|
||||||
testCompile 'junit:junit:4.12'
|
testCompile 'junit:junit:4.12'
|
||||||
compile 'com.android.support:appcompat-v7:23.1.1'
|
compile 'com.android.support:appcompat-v7:23.1.1'
|
||||||
|
compile 'com.jakewharton:butterknife:7.0.1'
|
||||||
|
compile 'com.github.yukuku:ambilwarna:2.0.1'
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,15 @@
|
|||||||
</intent-filter>
|
</intent-filter>
|
||||||
</activity>
|
</activity>
|
||||||
|
|
||||||
|
<activity
|
||||||
|
android:name=".MyWidgetConfigure"
|
||||||
|
android:screenOrientation="portrait"
|
||||||
|
android:theme="@style/MyWidgetConfigTheme">
|
||||||
|
<intent-filter>
|
||||||
|
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
|
||||||
|
</intent-filter>
|
||||||
|
</activity>
|
||||||
|
|
||||||
<receiver
|
<receiver
|
||||||
android:name=".MyWidgetProvider"
|
android:name=".MyWidgetProvider"
|
||||||
android:icon="@mipmap/ic_launcher"
|
android:icon="@mipmap/ic_launcher"
|
||||||
|
@ -3,4 +3,6 @@ package notes.simplemobiletools.com;
|
|||||||
public class Constants {
|
public class Constants {
|
||||||
public static final String PREFS = "prefs";
|
public static final String PREFS = "prefs";
|
||||||
public static final String TEXT = "text";
|
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.EditText;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import butterknife.Bind;
|
||||||
|
import butterknife.ButterKnife;
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity {
|
public class MainActivity extends AppCompatActivity {
|
||||||
private SharedPreferences prefs;
|
private SharedPreferences prefs;
|
||||||
private EditText notesView;
|
@Bind(R.id.notes_view) EditText notesView;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
|
ButterKnife.bind(this);
|
||||||
|
|
||||||
prefs = getSharedPreferences(Constants.PREFS, Context.MODE_PRIVATE);
|
prefs = getSharedPreferences(Constants.PREFS, Context.MODE_PRIVATE);
|
||||||
final String text = prefs.getString(Constants.TEXT, "");
|
final String text = prefs.getString(Constants.TEXT, "");
|
||||||
notesView = (EditText) findViewById(R.id.notesView);
|
|
||||||
notesView.setText(text);
|
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.appwidget.AppWidgetProvider;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
|
import android.graphics.Color;
|
||||||
import android.widget.RemoteViews;
|
import android.widget.RemoteViews;
|
||||||
|
|
||||||
public class MyWidgetProvider extends AppWidgetProvider {
|
public class MyWidgetProvider extends AppWidgetProvider {
|
||||||
@ -12,16 +13,22 @@ public class MyWidgetProvider extends AppWidgetProvider {
|
|||||||
@Override
|
@Override
|
||||||
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
|
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
|
||||||
prefs = context.getSharedPreferences(Constants.PREFS, Context.MODE_PRIVATE);
|
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) {
|
for (int widgetId : appWidgetIds) {
|
||||||
updateWidget(context, appWidgetManager, widgetId);
|
updateWidget(appWidgetManager, widgetId, remoteViews);
|
||||||
}
|
}
|
||||||
super.onUpdate(context, appWidgetManager, appWidgetIds);
|
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 String text = prefs.getString(Constants.TEXT, "");
|
||||||
final RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
|
remoteViews.setTextViewText(R.id.notes_view, text);
|
||||||
remoteViews.setTextViewText(R.id.notesView, text);
|
|
||||||
widgetManager.updateAppWidget(widgetId, remoteViews);
|
widgetManager.updateAppWidget(widgetId, remoteViews);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
android:padding="@dimen/activity_margin">
|
android:padding="@dimen/activity_margin">
|
||||||
|
|
||||||
<EditText
|
<EditText
|
||||||
android:id="@+id/notesView"
|
android:id="@+id/notes_view"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
android:background="@null"
|
android:background="@null"
|
||||||
|
@ -5,10 +5,12 @@
|
|||||||
android:layout_height="match_parent">
|
android:layout_height="match_parent">
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/notesView"
|
android:id="@+id/notes_view"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
android:background="@null"
|
android:background="@null"
|
||||||
android:gravity="top"
|
android:gravity="top"
|
||||||
|
android:padding="@dimen/activity_margin"
|
||||||
android:textSize="18sp"/>
|
android:textSize="18sp"/>
|
||||||
|
|
||||||
</RelativeLayout>
|
</RelativeLayout>
|
||||||
|
66
app/src/main/res/layout/widget_config.xml
Normal file
66
app/src/main/res/layout/widget_config.xml
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<RelativeLayout
|
||||||
|
android:id="@+id/notes_holder"
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_centerHorizontal="true"
|
||||||
|
android:layout_margin="@dimen/activity_margin"
|
||||||
|
android:paddingBottom="@dimen/activity_margin">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/notes_view"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:layout_above="@+id/config_bg_color"
|
||||||
|
android:layout_marginBottom="@dimen/activity_margin"
|
||||||
|
android:background="@null"
|
||||||
|
android:gravity="top"
|
||||||
|
android:padding="@dimen/activity_margin"
|
||||||
|
android:text="hello world"
|
||||||
|
android:textSize="18sp"/>
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/config_bg_color"
|
||||||
|
android:layout_width="50dp"
|
||||||
|
android:layout_height="50dp"
|
||||||
|
android:layout_above="@+id/config_text_color"/>
|
||||||
|
|
||||||
|
<RelativeLayout
|
||||||
|
android:id="@+id/config_bg_seekbar_holder"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:layout_alignBottom="@+id/config_bg_color"
|
||||||
|
android:layout_alignTop="@+id/config_bg_color"
|
||||||
|
android:layout_toRightOf="@+id/config_bg_color"
|
||||||
|
android:background="@android:color/white">
|
||||||
|
|
||||||
|
<SeekBar
|
||||||
|
android:id="@+id/config_bg_seekbar"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_centerVertical="true"
|
||||||
|
android:paddingLeft="@dimen/activity_margin"
|
||||||
|
android:paddingRight="@dimen/activity_margin"/>
|
||||||
|
</RelativeLayout>
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/config_text_color"
|
||||||
|
android:layout_width="50dp"
|
||||||
|
android:layout_height="50dp"
|
||||||
|
android:layout_alignParentBottom="true"/>
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/config_save"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:layout_alignBottom="@+id/config_text_color"
|
||||||
|
android:layout_alignParentRight="true"
|
||||||
|
android:layout_alignTop="@+id/config_text_color"
|
||||||
|
android:paddingLeft="@dimen/activity_margin"
|
||||||
|
android:paddingRight="@dimen/activity_margin"
|
||||||
|
android:text="OK"
|
||||||
|
android:textColor="@android:color/white"
|
||||||
|
android:textSize="18sp"/>
|
||||||
|
|
||||||
|
</RelativeLayout>
|
@ -3,4 +3,5 @@
|
|||||||
<color name="colorPrimary">#3F51B5</color>
|
<color name="colorPrimary">#3F51B5</color>
|
||||||
<color name="colorPrimaryDark">#303F9F</color>
|
<color name="colorPrimaryDark">#303F9F</color>
|
||||||
<color name="colorAccent">#FF4081</color>
|
<color name="colorAccent">#FF4081</color>
|
||||||
|
<color name="dark_grey">#88000000</color>
|
||||||
</resources>
|
</resources>
|
||||||
|
@ -6,4 +6,9 @@
|
|||||||
<item name="colorAccent">@color/colorAccent</item>
|
<item name="colorAccent">@color/colorAccent</item>
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
|
<style name="MyWidgetConfigTheme" parent="@style/AppTheme">
|
||||||
|
<item name="android:windowBackground">@android:color/transparent</item>
|
||||||
|
<item name="android:windowShowWallpaper">true</item>
|
||||||
|
</style>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
|
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:configure="notes.simplemobiletools.com.MyWidgetConfigure"
|
||||||
android:initialLayout="@layout/widget"
|
android:initialLayout="@layout/widget"
|
||||||
android:minHeight="110dp"
|
android:minHeight="110dp"
|
||||||
android:minWidth="110dp">
|
android:minWidth="110dp">
|
||||||
|
Loading…
x
Reference in New Issue
Block a user