implement a slider for setting the widget background opacity
This commit is contained in:
parent
8960387b48
commit
9b4f82bc2e
|
@ -24,4 +24,8 @@ public class Constants {
|
|||
public static final String SEVEN = "seven";
|
||||
public static final String EIGHT = "eight";
|
||||
public static final String NINE = "nine";
|
||||
|
||||
public static final String PREFS = "prefs";
|
||||
public static final String VALUE = "value";
|
||||
public static final String WIDGET_BG_COLOR = "widget_bg";
|
||||
}
|
||||
|
|
|
@ -2,18 +2,25 @@ package calculator.simplemobiletools.com.simple_calculator;
|
|||
|
||||
import android.app.Activity;
|
||||
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.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.RemoteViews;
|
||||
import android.widget.SeekBar;
|
||||
|
||||
import butterknife.Bind;
|
||||
import butterknife.ButterKnife;
|
||||
import butterknife.OnClick;
|
||||
|
||||
public class MyWidgetConfigure extends Activity {
|
||||
@Bind(R.id.btn_reset) Button resetBtn;
|
||||
public class MyWidgetConfigure extends Activity implements SeekBar.OnSeekBarChangeListener {
|
||||
@Bind(R.id.btn_reset) View resetBtn;
|
||||
@Bind(R.id.config_seekbar) SeekBar seekBar;
|
||||
@Bind(R.id.config_calc) View background;
|
||||
private int widgetId;
|
||||
private int newBgColor;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
|
@ -22,11 +29,13 @@ public class MyWidgetConfigure extends Activity {
|
|||
ButterKnife.bind(this);
|
||||
|
||||
resetBtn.setVisibility(View.VISIBLE);
|
||||
seekBar.setOnSeekBarChangeListener(this);
|
||||
seekBar.setProgress(50);
|
||||
|
||||
final Intent intent = getIntent();
|
||||
final Bundle extras = intent.getExtras();
|
||||
if (extras != null) {
|
||||
if (extras != null)
|
||||
widgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
|
||||
}
|
||||
|
||||
if (widgetId == AppWidgetManager.INVALID_APPWIDGET_ID)
|
||||
finish();
|
||||
|
@ -36,9 +45,44 @@ public class MyWidgetConfigure extends Activity {
|
|||
|
||||
@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.calculator_holder, "setBackgroundColor", newBgColor);
|
||||
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, newBgColor).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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
|
||||
final float percent = (float) progress / (float) 100;
|
||||
final int alpha = (int) (255 * percent);
|
||||
newBgColor = Color.argb(alpha, 0, 0, 0);
|
||||
background.setBackgroundColor(newBgColor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStartTrackingTouch(SeekBar seekBar) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStopTrackingTouch(SeekBar seekBar) {
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,9 +11,6 @@ import android.view.View;
|
|||
import android.widget.RemoteViews;
|
||||
|
||||
public class MyWidgetProvider extends AppWidgetProvider implements Calculator {
|
||||
private static final String PREFS = "prefs";
|
||||
private static final String VALUE = "value";
|
||||
|
||||
private static int[] widgetIds;
|
||||
private static RemoteViews remoteViews;
|
||||
private static CalculatorImpl calc;
|
||||
|
@ -61,19 +58,22 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calculator {
|
|||
}
|
||||
|
||||
private void initVariables(Context context) {
|
||||
prefs = initPrefs(context);
|
||||
final ComponentName component = new ComponentName(context, MyWidgetProvider.class);
|
||||
remoteViews = new RemoteViews(context.getPackageName(), R.layout.activity_main);
|
||||
remoteViews.setViewVisibility(R.id.btn_reset, View.VISIBLE);
|
||||
final int newBgColor = prefs.getInt(Constants.WIDGET_BG_COLOR, context.getResources().getColor(R.color.dark_grey));
|
||||
remoteViews.setInt(R.id.calculator_holder, "setBackgroundColor", newBgColor);
|
||||
|
||||
widgetManager = AppWidgetManager.getInstance(context);
|
||||
widgetIds = widgetManager.getAppWidgetIds(component);
|
||||
|
||||
prefs = initPrefs(context);
|
||||
final String value = prefs.getString(VALUE, "0");
|
||||
final String value = prefs.getString(Constants.VALUE, "0");
|
||||
calc = new CalculatorImpl(this, value);
|
||||
}
|
||||
|
||||
private SharedPreferences initPrefs(Context context) {
|
||||
return context.getSharedPreferences(PREFS, Context.MODE_PRIVATE);
|
||||
return context.getSharedPreferences(Constants.PREFS, Context.MODE_PRIVATE);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -109,7 +109,7 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calculator {
|
|||
}
|
||||
|
||||
private void myAction(String action, Context context) {
|
||||
if (calc == null)
|
||||
if (calc == null || remoteViews == null || widgetManager == null || widgetIds == null || prefs == null)
|
||||
initVariables(context);
|
||||
|
||||
switch (action) {
|
||||
|
@ -173,7 +173,7 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calculator {
|
|||
public void setValue(String value) {
|
||||
remoteViews.setTextViewText(R.id.result, value);
|
||||
widgetManager.updateAppWidget(widgetIds, remoteViews);
|
||||
prefs.edit().putString(VALUE, value).apply();
|
||||
prefs.edit().putString(Constants.VALUE, value).apply();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -187,6 +187,6 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calculator {
|
|||
if (prefs == null)
|
||||
prefs = initPrefs(context);
|
||||
|
||||
prefs.edit().remove(VALUE).apply();
|
||||
prefs.edit().remove(Constants.VALUE).apply();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,5 +34,15 @@
|
|||
android:paddingRight="@dimen/activity_margin"
|
||||
android:text="Save"
|
||||
android:textSize="@dimen/config_text_size"/>
|
||||
|
||||
<SeekBar
|
||||
android:id="@+id/config_seekbar"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentLeft="true"
|
||||
android:layout_alignParentStart="true"
|
||||
android:layout_centerVertical="true"
|
||||
android:layout_marginRight="@dimen/activity_margin"
|
||||
android:layout_toLeftOf="@id/config_save"/>
|
||||
</RelativeLayout>
|
||||
</RelativeLayout>
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
<resources>
|
||||
<color name="colorPrimary">#3F51B5</color>
|
||||
<color name="colorPrimaryDark">#303F9F</color>
|
||||
<color name="colorAccent">#FF4081</color>
|
||||
<color name="colorAccent">#FF0000</color>
|
||||
<color name="dark_grey">#88000000</color>
|
||||
<color name="dark_grey_pressed">#22000000</color>
|
||||
</resources>
|
||||
|
|
Loading…
Reference in New Issue