Skip to content

Commit cecf2fa

Browse files
committed
add a widget config activity for changing widget alpha
1 parent ac8ef37 commit cecf2fa

File tree

12 files changed

+207
-0
lines changed

12 files changed

+207
-0
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@
2727
</intent-filter>
2828
</activity>
2929

30+
<activity
31+
android:name=".activities.WidgetConfigureActivity"
32+
android:screenOrientation="portrait"
33+
android:theme="@style/MyWidgetConfigTheme">
34+
<intent-filter>
35+
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
36+
</intent-filter>
37+
</activity>
38+
3039
<activity
3140
android:name=".activities.AboutActivity"
3241
android:label="@string/about"

app/src/main/java/com/simplemobiletools/flashlight/Constants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ public class Constants {
66
public static final String IS_FIRST_RUN = "is_first_run";
77
public static final String IS_DARK_THEME = "is_dark_theme";
88
public static final String BRIGHT_DISPLAY = "bright_display";
9+
public static final String WIDGET_COLOR = "widget_color";
910
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RelativeLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
android:layout_width="match_parent"
5+
android:layout_height="wrap_content"
6+
android:layout_centerHorizontal="true"
7+
android:layout_margin="@dimen/activity_margin"
8+
android:paddingBottom="@dimen/activity_margin">
9+
10+
<ImageView
11+
android:id="@+id/config_image"
12+
android:layout_width="match_parent"
13+
android:layout_height="match_parent"
14+
android:layout_above="@+id/config_widget_color"
15+
android:scaleType="centerInside"
16+
android:src="@mipmap/flashlight_small"/>
17+
18+
<Button
19+
android:id="@+id/config_widget_color"
20+
android:layout_width="50dp"
21+
android:layout_height="50dp"
22+
android:layout_above="@+id/config_save"/>
23+
24+
<RelativeLayout
25+
android:id="@+id/config_widget_seekbar_holder"
26+
android:layout_width="match_parent"
27+
android:layout_height="match_parent"
28+
android:layout_alignBottom="@+id/config_widget_color"
29+
android:layout_alignTop="@+id/config_widget_color"
30+
android:layout_toRightOf="@+id/config_widget_color"
31+
android:background="@android:color/white">
32+
33+
<SeekBar
34+
android:id="@+id/config_widget_seekbar"
35+
android:layout_width="match_parent"
36+
android:layout_height="wrap_content"
37+
android:layout_centerVertical="true"
38+
android:paddingLeft="@dimen/activity_margin"
39+
android:paddingRight="@dimen/activity_margin"/>
40+
</RelativeLayout>
41+
42+
<Button
43+
android:id="@+id/config_save"
44+
android:layout_width="wrap_content"
45+
android:layout_height="50dp"
46+
android:layout_alignParentBottom="true"
47+
android:layout_alignParentRight="true"
48+
android:background="@color/translucent_black"
49+
android:fontFamily="sans-serif-light"
50+
android:paddingLeft="@dimen/activity_margin"
51+
android:paddingRight="@dimen/activity_margin"
52+
android:text="@string/ok"
53+
android:textColor="@color/colorPrimary"
54+
android:textSize="@dimen/config_text_size"/>
55+
56+
</RelativeLayout>

app/src/main/res/values-it/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<string name="app_name">Simple Flashlight</string>
33
<string name="app_launcher_name">Flashlight</string>
44
<string name="camera_error">Rilevamento fotocamera fallito</string>
5+
<string name="ok">OK</string>
56

67
<!-- Settings -->
78
<string name="settings">Impostazioni</string>

app/src/main/res/values-ja/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<string name="app_name">シンプル フラッシュライト</string>
33
<string name="app_launcher_name">Flashlight</string>
44
<string name="camera_error">カメラの取得に失敗しました</string>
5+
<string name="ok">OK</string>
56

67
<!-- Settings -->
78
<string name="settings">設定</string>

app/src/main/res/values-sv/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<string name="app_name">Simple Flashlight</string>
33
<string name="app_launcher_name">Flashlight</string>
44
<string name="camera_error">Det gick inte att komma åt kameran</string>
5+
<string name="ok">OK</string>
56

67
<!-- Settings -->
78
<string name="settings">Inställningar</string>

app/src/main/res/values/colors.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
<color name="colorPrimaryDark">#ffe27725</color>
55
<color name="colorAccent">@color/colorPrimary</color>
66
<color name="translucent_white">#aaffffff</color>
7+
<color name="translucent_black">#88000000</color>
78
</resources>

app/src/main/res/values/dimens.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66
<dimen name="settings_padding">8dp</dimen>
77

88
<dimen name="normal_text_size">14sp</dimen>
9+
<dimen name="config_text_size">18sp</dimen>
910
</resources>

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<string name="app_name">Simple Flashlight</string>
33
<string name="app_launcher_name">Flashlight</string>
44
<string name="camera_error">Obtaining the camera failed</string>
5+
<string name="ok">OK</string>
56

67
<!-- Settings -->
78
<string name="settings">Settings</string>

0 commit comments

Comments
 (0)