Skip to content

Commit

Permalink
slight adjustments and performance improvements
Browse files Browse the repository at this point in the history
inverted date size bar
added some hint texts
  • Loading branch information
programminghoch10 committed Oct 5, 2019
1 parent 6d2b081 commit 769c1aa
Show file tree
Hide file tree
Showing 15 changed files with 80 additions and 49 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ I'm always on the quest to make this the best HangoverClock ever.
* The only public method is generateWidget, which returns a Bitmap Image.
* The fonts get dynamically collected from all available resource files
and saved in a static String array within the ClockWidgetProvider class.
* The resulting bitmap resolution will be calculated to reach the best quality possible on every device.
* The sharedPreference keys are defined in preferencekeys.xml and their defaults in preferencekeydefaults.xml.
* sharedPreferences are saved in the format key and directly appended widget ID.
* If a key is not defined its default value is assumed and only gets saved when not default or when alwayssavepreferences bool is active.
Expand Down
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ android {
applicationId "com.JJ.hangoverclock"
minSdkVersion 14
targetSdkVersion 28
versionCode 7
versionCode 8
versionName "dynamite"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
Binary file modified app/release/app-release.apk
Binary file not shown.
2 changes: 1 addition & 1 deletion app/release/output.json
Original file line number Diff line number Diff line change
@@ -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":{}}]
[{"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":{}}]
2 changes: 0 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>

<receiver android:name=".ClockWidgetProvider">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="com.JJ.hangoverclock.widgetupdate" />
</intent-filter>

<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/appwidgetproviderinfo" />
Expand Down
6 changes: 2 additions & 4 deletions app/src/main/java/com/JJ/hangoverclock/ConfigureWidget.java
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ private void updatepreview() {
SeekBar seekbaralpha = findViewById(R.id.seekbaralpha);
int color = Color.argb(seekbaralpha.getProgress(), seekbarred.getProgress(), seekbargreen.getProgress(), seekbarblue.getProgress());
SeekBar fontsizedividerseekbar = findViewById(R.id.datefontsizeseekbar);
float fontsizedivider = context.getResources().getInteger(R.integer.maxfontscale) - (float) (fontsizedividerseekbar.getMax() - fontsizedividerseekbar.getProgress()) / 100;
float fontsizedivider = context.getResources().getInteger(R.integer.maxfontscale) - (float) fontsizedividerseekbar.getProgress() / 100;
if (((Switch) findViewById(R.id.dateselector)).isChecked()) {
findViewById(R.id.overhanginputdate).setVisibility(View.VISIBLE);
fontsizedividerseekbar.setVisibility(View.VISIBLE);
Expand Down Expand Up @@ -392,15 +392,13 @@ private void updatepreview() {
//Expected if called to early
font = context.getResources().getString(R.string.defaultfonttext);
}
int fontresolution = context.getResources().getInteger(R.integer.widgetfontresolution);
Bitmap bitmap = WidgetGenerator.generateWidget(
context, Calendar.getInstance().getTimeInMillis(),
secondoverhang, minuteoverhang, houroverhang, dayoverhang, monthoverhang,
twelvehour, withseconds, withdate,
font, color, fontsizedivider, fontresolution
font, color, fontsizedivider
);
bitmap.prepareToDraw();
imageView.setImageBitmap(bitmap);

}
}
54 changes: 39 additions & 15 deletions app/src/main/java/com/JJ/hangoverclock/WidgetGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,44 +22,43 @@ static Bitmap generateWidget(Context context, long timestamp,
int secondoverhang, int minuteoverhang, int houroverhang,
int dayoverhang, int monthoverhang,
boolean twelvehours, boolean withseconds, boolean withdate,
String font, int color, float fontscale, int fontresolution) {
String font, int color, float fontscale) {
if (!withdate) {
return generateBitmap(context,
calculatetime(timestamp, houroverhang, minuteoverhang, secondoverhang, twelvehours, withseconds),
font, color, fontresolution);
font, color);
} else {
String[] hangovertext = combinedcalculate(timestamp,
monthoverhang, dayoverhang,
houroverhang, minuteoverhang, secondoverhang,
withseconds, twelvehours);
return generateBitmap(context,
hangovertext[0], hangovertext[1],
font, color, fontscale, fontresolution);
font, color, fontscale);
}
}

private static Bitmap generateBitmap(Context context, String time, String date, String font, int color, float datefontscale, int fontresolution) {
private static Bitmap generateBitmap(Context context, String time, String date, String font, int color, float datefontscale) {
if (date == null) {
return generateBitmap(context, time, font, color, fontresolution);
return generateBitmap(context, time, font, color);
} else {
return generateBitmap(context, time, font, color, date, font, color, datefontscale, fontresolution);
return generateBitmap(context, time, font, color, date, font, color, datefontscale);
}
}

private static Bitmap generateBitmap(Context context, String time, String timefont, int timecolor, int fontresolution) {
return generateBitmap(context, false, time, timefont, timecolor, null, null, 0,
0, fontresolution);
private static Bitmap generateBitmap(Context context, String time, String timefont, int timecolor) {
return generateBitmap(context, false, time, timefont, timecolor, null, null, 0, 0);
}

private static Bitmap generateBitmap(Context context, String time, String timefont, int timecolor,
String date, String datefont, int datecolor, float datefontscale, int fontresolution) {
return generateBitmap(context, true, time, timefont, timecolor, date, datefont, datecolor, datefontscale, fontresolution);
String date, String datefont, int datecolor, float datefontscale) {
return generateBitmap(context, true, time, timefont, timecolor, date, datefont, datecolor, datefontscale);
}

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

private static int calculatefontsize(Context context, String text, String font) {
String TAG = "calculatefontsize";
int cap = 5000; //max iterations cap to preventto crash rather then ANR
Typeface typeface = Typeface.defaultFromStyle(Typeface.NORMAL);
font = font.replace(" ", "_");
if (!context.getString(R.string.defaultfonttext).equals(font)) {
Expand Down Expand Up @@ -251,16 +251,40 @@ private static int calculatefontsize(Context context, String text, String font)
while (currentbytes < maxbytes) {
fontsize++;
count++;
if ((float)currentbytes/maxbytes<1) fontsize*=(1-(float)currentbytes/maxbytes)/2+1;
paint.setTextSize(fontsize);
//i wasted so many days just to find out that my first formula works best :(
if ((float)currentbytes/maxbytes<1) fontsize*=(1-(float)currentbytes/maxbytes)/2+1; //works in about 18 iterations
//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
//if ((float)currentbytes/maxbytes<1) fontsize = ((Math.log((y-e)/a))/1)-0; //unfinished
//if ((float)currentbytes/maxbytes<1) fontsize = (int)(fontsize * (1/((float)currentbytes/maxbytes))); //big overshoot
//if ((float)currentbytes/maxbytes<1) fontsize = (int)(fontsize * (5*Math.exp(-9*((float)currentbytes/maxbytes))+1)); //its alright
paint.setTextSize(fontsize);
int pad = (fontsize / 9);
int textWidth = (int) (paint.measureText(text) + pad * 2);
int height = (int) (fontsize / 0.7);
currentbytes = (textWidth * height * 4);
//Log.d(TAG, "calculatefontsize: itaration "+count+", fontsize "+fontsize+", size is "+currentbytes+", that is "+((float)currentbytes/maxbytes));
if (count>cap) break;
}
//Log.d(TAG, "calculatefontsize: using "+currentbytes+" of "+maxbytes);
fontsize--;
paint.setTextSize(fontsize);
{
int pad = (fontsize / 9);
int textWidth = (int) (paint.measureText(text) + pad * 2);
int height = (int) (fontsize / 0.7);
currentbytes = (textWidth * height * 4);
}
while (currentbytes > maxbytes) {
fontsize--;
count++;
paint.setTextSize(fontsize);
int pad = (fontsize / 9);
int textWidth = (int) (paint.measureText(text) + pad * 2);
int height = (int) (fontsize / 0.7);
currentbytes = (textWidth * height * 4);
//Log.d(TAG, "calculatefontsize: subtratcting itaration "+count+", fontsize "+fontsize+", size is "+currentbytes+", that is "+((float)currentbytes/maxbytes));
if (count>cap*2) break;
}
//Log.d(TAG, "calculatefontsize: using "+currentbytes+" of "+maxbytes+", that is "+((float)currentbytes/maxbytes));
//Log.d(TAG, "calculatefontsize: calculated font size is "+fontsize);
return fontsize;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

public class ClockWidgetProvider extends AppWidgetProvider {

//TODO: automatischer Farbwechsel
static final String TAG = "ClockWidgetProvider";
public static ArrayList<String> fonts = new ArrayList<String>() {{
add("default");
Expand Down Expand Up @@ -146,13 +147,12 @@ public void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
String font = sharedPreferences.getString(context.getResources().getString(R.string.keyfont) + appWidgetId, context.getResources().getString(R.string.defaultfonttext));
float fontscale = sharedPreferences.getFloat(context.getResources().getString(R.string.keyfontscale) + appWidgetId, context.getResources().getInteger(R.integer.defaultdatefontscale));
int color = sharedPreferences.getInt(context.getResources().getString(R.string.keycolor) + appWidgetId, context.getResources().getColor(R.color.defaultWidgetColor));
int fontresolution = context.getResources().getInteger(R.integer.widgetfontresolution);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
remoteViews.setImageViewBitmap(R.id.clock,
WidgetGenerator.generateWidget(
context, Calendar.getInstance().getTimeInMillis(),
secondoverhang, minuteoverhang, houroverhang, dayoverhang, monthoverhang,
twelvehour, enableseconds, enabledate, font, color, fontscale, fontresolution)
twelvehour, enableseconds, enabledate, font, color, fontscale)
);
try {
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/widget.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
android:id="@+id/clock"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/clock" />
android:src="@drawable/clock" />

</FrameLayout>
22 changes: 22 additions & 0 deletions app/src/main/res/layout/widget_configure.xml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@
android:layout_height="2dp"
android:background="?android:attr/listDivider" />

<TextView
android:id="@+id/colorheader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@string/colorheader"
android:textAllCaps="false" />

<TableLayout
android:id="@+id/colorselector"
android:layout_width="match_parent"
Expand Down Expand Up @@ -224,6 +232,13 @@
android:layout_height="2dp"
android:background="?android:attr/listDivider" />

<TextView
android:id="@+id/settingsheader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@string/settingsheader" />

<LinearLayout
android:id="@+id/switchlayout"
android:layout_width="wrap_content"
Expand Down Expand Up @@ -285,6 +300,13 @@
android:layout_height="2dp"
android:background="?android:attr/listDivider" />

<TextView
android:id="@+id/fontheader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@string/fontheader" />

<Spinner
android:id="@+id/fontspinner"
android:layout_width="match_parent"
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
<string name="enabledateswitch">Datum aktivieren</string>
<string name="inputoverhangday">Tage</string>
<string name="usesystemtimeformat">Systemzeitformat benutzen</string>
<string name="overhangheader">Overhang:</string>
<string name="overhangheader">Overhang</string>
<string name="inputoverhanghours">Stunden</string>
<string name="inputoverhangmonths">Monate</string>
<string name="colorheader">Farben</string>
<string name="fontheader">Schriftart</string>
<string name="settingsheader">Einstellungen</string>
</resources>
17 changes: 0 additions & 17 deletions app/src/main/res/values/font_certs.xml

This file was deleted.

3 changes: 1 addition & 2 deletions app/src/main/res/values/integers.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="widgetfontresolution">150</integer> <!-- obsolete now due to dynamic calculation --> <!-- i guess it should only affect resolution and not size due to scaled bitmap -->
<integer name="defaultdatefontscale">5</integer> <!-- scale date between 3 (largest) and maxfontscale (smallest) -->
<integer name="maxfontscale">5</integer> <!-- defines maximum smallness of font -->
<integer name="defaultdatefontscale">5</integer> <!-- scale date between 3 (largest) and maxfontscale (smallest) -->
</resources>
7 changes: 5 additions & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
<string name="savewidget">Create Widget</string>
<string name="inputoverhangminutes">minutes</string>
<string name="use12h">Use 12h format</string>
<string name="defaultfonttext">system default</string>
<string name="defaultfonttext">default</string>
<string name="enablesecondsswitch">Enable seconds</string>
<string name="secondsresourceheavywarning">Using seconds is a big system load and is not recommended!</string>
<string name="enabledateswitch">Enable Date</string>
<string name="inputoverhangday">days</string>
<string name="usesystemtimeformat">Use system timeformat</string>
<string name="overhangheader">Overhang:</string>
<string name="overhangheader">Overhang</string>
<string name="inputoverhanghours">hours</string>
<string name="inputoverhangmonths">months</string>
<string name="colorheader">Colors</string>
<string name="settingsheader">Settings</string>
<string name="fontheader">Fonts</string>
</resources>
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {

}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
classpath 'com.android.tools.build:gradle:3.5.1'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down

0 comments on commit 769c1aa

Please sign in to comment.