Skip to content

Commit c6390c0

Browse files
Merge branch 'dev'
2 parents 6a37380 + 769c1aa commit c6390c0

File tree

15 files changed

+96
-50
lines changed

15 files changed

+96
-50
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ I'm always on the quest to make this the best HangoverClock ever.
108108
* The only public method is generateWidget, which returns a Bitmap Image.
109109
* The fonts get dynamically collected from all available resource files
110110
and saved in a static String array within the ClockWidgetProvider class.
111+
* The resulting bitmap resolution will be calculated to reach the best quality possible on every device.
111112
* The sharedPreference keys are defined in preferencekeys.xml and their defaults in preferencekeydefaults.xml.
112113
* sharedPreferences are saved in the format key and directly appended widget ID.
113114
* If a key is not defined its default value is assumed and only gets saved when not default or when alwayssavepreferences bool is active.

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ android {
66
applicationId "com.JJ.hangoverclock"
77
minSdkVersion 14
88
targetSdkVersion 28
9-
versionCode 7
9+
versionCode 8
1010
versionName "dynamite"
1111
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1212
}

app/release/app-release.apk

-1.67 KB
Binary file not shown.

app/release/output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[{"outputType":{"type":"APK"},"apkData":{"type":"MAIN","splits":[],"versionCode":7,"versionName":"dynamite","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}]
1+
[{"outputType":{"type":"APK"},"apkData":{"type":"MAIN","splits":[],"versionCode":8,"versionName":"dynamite","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}]

app/src/main/AndroidManifest.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@
1717
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
1818
</intent-filter>
1919
</activity>
20-
2120
<receiver android:name=".ClockWidgetProvider">
2221
<intent-filter>
2322
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
2423
<action android:name="com.JJ.hangoverclock.widgetupdate" />
2524
</intent-filter>
26-
2725
<meta-data
2826
android:name="android.appwidget.provider"
2927
android:resource="@xml/appwidgetproviderinfo" />

app/src/main/java/com/JJ/hangoverclock/ConfigureWidget.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,22 @@ public void onClick(View v) {
8686
boolean twelvehours = ((Switch) findViewById(R.id.hourselector)).isChecked();
8787
boolean enabledate = ((Switch) findViewById(R.id.dateselector)).isChecked();
8888
boolean as = context.getResources().getBoolean(R.bool.alwayssavepreference); //wether preferences should always be saved
89+
int[] flushkeys = {
90+
R.string.keyenableseconds,
91+
R.string.keyfont,
92+
R.string.keyfontscale,
93+
R.string.keyhouroverhang,
94+
R.string.keyminuteoverhang,
95+
R.string.keysecondoverhang,
96+
R.string.keydayoverhang,
97+
R.string.keymonthoverhang,
98+
R.string.keycolor,
99+
R.string.keytwelvehour,
100+
R.string.keyenabledate,
101+
};
102+
for (int key:flushkeys) {
103+
editor.remove(context.getResources().getString(key) + appWidgetID);
104+
}
89105
if (as | context.getResources().getBoolean(R.bool.defaultenableseconds) != enableseconds)
90106
editor.putBoolean(context.getResources().getString(R.string.keyenableseconds) + appWidgetID, enableseconds);
91107
if (as | !context.getResources().getString(R.string.defaultfonttext).equals(font))
@@ -254,7 +270,6 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
254270
((Switch) findViewById(R.id.dateselector)).setOnCheckedChangeListener(updatepreviewlistener);
255271
((Switch) findViewById(R.id.secondsselector)).setOnCheckedChangeListener(updatepreviewlistener);
256272
((Switch) findViewById(R.id.autohourselector)).setOnCheckedChangeListener(updatepreviewlistener);
257-
//TODO: replace spinner with recyclerview (big project ._.)
258273
Spinner fontspinner = findViewById(R.id.fontspinner);
259274
ArrayList<RowItem> rowItems = new ArrayList<RowItem>();
260275
ArrayList<String> fonts = ClockWidgetProvider.fonts;
@@ -349,7 +364,7 @@ private void updatepreview() {
349364
SeekBar seekbaralpha = findViewById(R.id.seekbaralpha);
350365
int color = Color.argb(seekbaralpha.getProgress(), seekbarred.getProgress(), seekbargreen.getProgress(), seekbarblue.getProgress());
351366
SeekBar fontsizedividerseekbar = findViewById(R.id.datefontsizeseekbar);
352-
float fontsizedivider = context.getResources().getInteger(R.integer.maxfontscale) - (float) (fontsizedividerseekbar.getMax() - fontsizedividerseekbar.getProgress()) / 100;
367+
float fontsizedivider = context.getResources().getInteger(R.integer.maxfontscale) - (float) fontsizedividerseekbar.getProgress() / 100;
353368
if (((Switch) findViewById(R.id.dateselector)).isChecked()) {
354369
findViewById(R.id.overhanginputdate).setVisibility(View.VISIBLE);
355370
fontsizedividerseekbar.setVisibility(View.VISIBLE);
@@ -377,15 +392,13 @@ private void updatepreview() {
377392
//Expected if called to early
378393
font = context.getResources().getString(R.string.defaultfonttext);
379394
}
380-
int fontresolution = context.getResources().getInteger(R.integer.widgetfontresolution);
381395
Bitmap bitmap = WidgetGenerator.generateWidget(
382396
context, Calendar.getInstance().getTimeInMillis(),
383397
secondoverhang, minuteoverhang, houroverhang, dayoverhang, monthoverhang,
384398
twelvehour, withseconds, withdate,
385-
font, color, fontsizedivider, fontresolution
399+
font, color, fontsizedivider
386400
);
387401
bitmap.prepareToDraw();
388402
imageView.setImageBitmap(bitmap);
389-
390403
}
391404
}

app/src/main/java/com/JJ/hangoverclock/WidgetGenerator.java

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,44 +22,43 @@ static Bitmap generateWidget(Context context, long timestamp,
2222
int secondoverhang, int minuteoverhang, int houroverhang,
2323
int dayoverhang, int monthoverhang,
2424
boolean twelvehours, boolean withseconds, boolean withdate,
25-
String font, int color, float fontscale, int fontresolution) {
25+
String font, int color, float fontscale) {
2626
if (!withdate) {
2727
return generateBitmap(context,
2828
calculatetime(timestamp, houroverhang, minuteoverhang, secondoverhang, twelvehours, withseconds),
29-
font, color, fontresolution);
29+
font, color);
3030
} else {
3131
String[] hangovertext = combinedcalculate(timestamp,
3232
monthoverhang, dayoverhang,
3333
houroverhang, minuteoverhang, secondoverhang,
3434
withseconds, twelvehours);
3535
return generateBitmap(context,
3636
hangovertext[0], hangovertext[1],
37-
font, color, fontscale, fontresolution);
37+
font, color, fontscale);
3838
}
3939
}
4040

41-
private static Bitmap generateBitmap(Context context, String time, String date, String font, int color, float datefontscale, int fontresolution) {
41+
private static Bitmap generateBitmap(Context context, String time, String date, String font, int color, float datefontscale) {
4242
if (date == null) {
43-
return generateBitmap(context, time, font, color, fontresolution);
43+
return generateBitmap(context, time, font, color);
4444
} else {
45-
return generateBitmap(context, time, font, color, date, font, color, datefontscale, fontresolution);
45+
return generateBitmap(context, time, font, color, date, font, color, datefontscale);
4646
}
4747
}
4848

49-
private static Bitmap generateBitmap(Context context, String time, String timefont, int timecolor, int fontresolution) {
50-
return generateBitmap(context, false, time, timefont, timecolor, null, null, 0,
51-
0, fontresolution);
49+
private static Bitmap generateBitmap(Context context, String time, String timefont, int timecolor) {
50+
return generateBitmap(context, false, time, timefont, timecolor, null, null, 0, 0);
5251
}
5352

5453
private static Bitmap generateBitmap(Context context, String time, String timefont, int timecolor,
55-
String date, String datefont, int datecolor, float datefontscale, int fontresolution) {
56-
return generateBitmap(context, true, time, timefont, timecolor, date, datefont, datecolor, datefontscale, fontresolution);
54+
String date, String datefont, int datecolor, float datefontscale) {
55+
return generateBitmap(context, true, time, timefont, timecolor, date, datefont, datecolor, datefontscale);
5756
}
5857

5958
private static Bitmap generateBitmap(Context context, boolean withdate,
6059
String time, String timefont, int timecolor,
6160
String date, String datefont, int datecolor,
62-
float fontscale, int fontresolution) {
61+
float fontscale) {
6362
//ah shit .settypeface doesnt exist in remoteviews wth do I do now? guess ill be rendering a bitmap
6463
//solution: https://stackoverflow.com/questions/4318572/how-to-use-a-custom-typeface-in-a-widget
6564
//but i added the date myself
@@ -220,6 +219,7 @@ private static String[] combinedcalculate(long timestamp,
220219

221220
private static int calculatefontsize(Context context, String text, String font) {
222221
String TAG = "calculatefontsize";
222+
int cap = 5000; //max iterations cap to preventto crash rather then ANR
223223
Typeface typeface = Typeface.defaultFromStyle(Typeface.NORMAL);
224224
font = font.replace(" ", "_");
225225
if (!context.getString(R.string.defaultfonttext).equals(font)) {
@@ -251,16 +251,40 @@ private static int calculatefontsize(Context context, String text, String font)
251251
while (currentbytes < maxbytes) {
252252
fontsize++;
253253
count++;
254-
if ((float)currentbytes/maxbytes<1) fontsize*=(1-(float)currentbytes/maxbytes)/2+1;
255-
paint.setTextSize(fontsize);
254+
//i wasted so many days just to find out that my first formula works best :(
255+
if ((float)currentbytes/maxbytes<1) fontsize*=(1-(float)currentbytes/maxbytes)/2+1; //works in about 18 iterations
256+
//if ((float)currentbytes/maxbytes<1) fontsize = fontsize * (int)(0+((1*(Math.exp(-currentbytes+(maxbytes/2)))+maxbytes)/maxbytes)-0); //not working at all, hits the cap
257+
//if ((float)currentbytes/maxbytes<1) fontsize = ((Math.log((y-e)/a))/1)-0; //unfinished
258+
//if ((float)currentbytes/maxbytes<1) fontsize = (int)(fontsize * (1/((float)currentbytes/maxbytes))); //big overshoot
259+
//if ((float)currentbytes/maxbytes<1) fontsize = (int)(fontsize * (5*Math.exp(-9*((float)currentbytes/maxbytes))+1)); //its alright
260+
paint.setTextSize(fontsize);
256261
int pad = (fontsize / 9);
257262
int textWidth = (int) (paint.measureText(text) + pad * 2);
258263
int height = (int) (fontsize / 0.7);
259264
currentbytes = (textWidth * height * 4);
260265
//Log.d(TAG, "calculatefontsize: itaration "+count+", fontsize "+fontsize+", size is "+currentbytes+", that is "+((float)currentbytes/maxbytes));
266+
if (count>cap) break;
261267
}
262-
//Log.d(TAG, "calculatefontsize: using "+currentbytes+" of "+maxbytes);
263268
fontsize--;
269+
paint.setTextSize(fontsize);
270+
{
271+
int pad = (fontsize / 9);
272+
int textWidth = (int) (paint.measureText(text) + pad * 2);
273+
int height = (int) (fontsize / 0.7);
274+
currentbytes = (textWidth * height * 4);
275+
}
276+
while (currentbytes > maxbytes) {
277+
fontsize--;
278+
count++;
279+
paint.setTextSize(fontsize);
280+
int pad = (fontsize / 9);
281+
int textWidth = (int) (paint.measureText(text) + pad * 2);
282+
int height = (int) (fontsize / 0.7);
283+
currentbytes = (textWidth * height * 4);
284+
//Log.d(TAG, "calculatefontsize: subtratcting itaration "+count+", fontsize "+fontsize+", size is "+currentbytes+", that is "+((float)currentbytes/maxbytes));
285+
if (count>cap*2) break;
286+
}
287+
//Log.d(TAG, "calculatefontsize: using "+currentbytes+" of "+maxbytes+", that is "+((float)currentbytes/maxbytes));
264288
//Log.d(TAG, "calculatefontsize: calculated font size is "+fontsize);
265289
return fontsize;
266290
}

app/src/main/java/com/jj/hangoverclock/ClockWidgetProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
public class ClockWidgetProvider extends AppWidgetProvider {
2424

25+
//TODO: automatischer Farbwechsel
2526
static final String TAG = "ClockWidgetProvider";
2627
public static ArrayList<String> fonts = new ArrayList<String>() {{
2728
add("default");
@@ -146,13 +147,12 @@ public void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
146147
String font = sharedPreferences.getString(context.getResources().getString(R.string.keyfont) + appWidgetId, context.getResources().getString(R.string.defaultfonttext));
147148
float fontscale = sharedPreferences.getFloat(context.getResources().getString(R.string.keyfontscale) + appWidgetId, context.getResources().getInteger(R.integer.defaultdatefontscale));
148149
int color = sharedPreferences.getInt(context.getResources().getString(R.string.keycolor) + appWidgetId, context.getResources().getColor(R.color.defaultWidgetColor));
149-
int fontresolution = context.getResources().getInteger(R.integer.widgetfontresolution);
150150
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
151151
remoteViews.setImageViewBitmap(R.id.clock,
152152
WidgetGenerator.generateWidget(
153153
context, Calendar.getInstance().getTimeInMillis(),
154154
secondoverhang, minuteoverhang, houroverhang, dayoverhang, monthoverhang,
155-
twelvehour, enableseconds, enabledate, font, color, fontscale, fontresolution)
155+
twelvehour, enableseconds, enabledate, font, color, fontscale)
156156
);
157157
try {
158158
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);

app/src/main/res/layout/widget.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111
android:id="@+id/clock"
1212
android:layout_width="match_parent"
1313
android:layout_height="match_parent"
14-
android:src="@mipmap/clock" />
14+
android:src="@drawable/clock" />
1515

1616
</FrameLayout>

app/src/main/res/layout/widget_configure.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,14 @@
111111
android:layout_height="2dp"
112112
android:background="?android:attr/listDivider" />
113113

114+
<TextView
115+
android:id="@+id/colorheader"
116+
android:layout_width="match_parent"
117+
android:layout_height="wrap_content"
118+
android:gravity="center_horizontal"
119+
android:text="@string/colorheader"
120+
android:textAllCaps="false" />
121+
114122
<TableLayout
115123
android:id="@+id/colorselector"
116124
android:layout_width="match_parent"
@@ -224,6 +232,13 @@
224232
android:layout_height="2dp"
225233
android:background="?android:attr/listDivider" />
226234

235+
<TextView
236+
android:id="@+id/settingsheader"
237+
android:layout_width="match_parent"
238+
android:layout_height="wrap_content"
239+
android:gravity="center_horizontal"
240+
android:text="@string/settingsheader" />
241+
227242
<LinearLayout
228243
android:id="@+id/switchlayout"
229244
android:layout_width="wrap_content"
@@ -285,6 +300,13 @@
285300
android:layout_height="2dp"
286301
android:background="?android:attr/listDivider" />
287302

303+
<TextView
304+
android:id="@+id/fontheader"
305+
android:layout_width="match_parent"
306+
android:layout_height="wrap_content"
307+
android:gravity="center_horizontal"
308+
android:text="@string/fontheader" />
309+
288310
<Spinner
289311
android:id="@+id/fontspinner"
290312
android:layout_width="match_parent"

0 commit comments

Comments
 (0)