From da2acfb09c64bfbe74d3d4ee3ed8073bc2b6b7d6 Mon Sep 17 00:00:00 2001 From: NeoTheFox Date: Fri, 6 Jul 2018 21:25:16 +0300 Subject: [PATCH] Added different icon shapes --- .../space/neothefox/laytray/IconService.java | 59 ++++++++++++++++--- .../neothefox/laytray/SettingsActivity.java | 1 + app/src/main/res/values/strings.xml | 12 ++++ app/src/main/res/xml/pref_notification.xml | 7 +++ 4 files changed, 70 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/space/neothefox/laytray/IconService.java b/app/src/main/java/space/neothefox/laytray/IconService.java index 4b60784..06117b8 100644 --- a/app/src/main/java/space/neothefox/laytray/IconService.java +++ b/app/src/main/java/space/neothefox/laytray/IconService.java @@ -10,6 +10,9 @@ import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; +import android.graphics.PorterDuff; +import android.graphics.PorterDuffXfermode; +import android.graphics.RectF; import android.graphics.drawable.Icon; import android.preference.PreferenceManager; import android.util.Log; @@ -61,6 +64,7 @@ implements SharedPreferences.OnSharedPreferenceChangeListener{ Icon smallIcon = Icon.createWithBitmap(textAsBitmap(textIcon, Integer.parseInt(options.getString("textSize", "48")), options.getBoolean("textFakeBold", true), + Integer.parseInt(options.getString("textMode", "0")), Color.WHITE)); indicator = new Notification.Builder(this) @@ -74,19 +78,55 @@ implements SharedPreferences.OnSharedPreferenceChangeListener{ } //Borrowed from Ted Hopp from StackOverflow - public Bitmap textAsBitmap(String text, float textSize, boolean fakeBold, int textColor) { + public Bitmap textAsBitmap(String text, float textSize, boolean fakeBold, int mode, int textColor) { Paint paint = new Paint(ANTI_ALIAS_FLAG); + paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT)); paint.setTextSize(textSize); paint.setFakeBoldText(fakeBold); paint.setColor(textColor); - paint.setTextAlign(Paint.Align.LEFT); - float baseline = -paint.ascent(); // ascent() is negative - int width = (int) (paint.measureText(text) + 0.5f); // round - int height = (int) (baseline + paint.descent() + 0.5f); - Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); - Canvas canvas = new Canvas(image); - canvas.drawText(text, 0, baseline, paint); - return image; + switch (mode) + { + case 1: { + float baseline = -paint.ascent(); // ascent() is negative + int width = 48; + int height = 48; + Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); + Canvas canvas = new Canvas(image); + canvas.drawCircle(width / 2f, height / 2f, width / 2f, paint); + paint.setAlpha(255); + paint.setColor(Color.TRANSPARENT); + paint.setTextSize(textSize); + paint.setTextAlign(Paint.Align.CENTER); + canvas.drawText(text, width / 2f, height / 2f + textSize / 2f, paint); + return image; + } + + case 2: { + float baseline = -paint.ascent(); // ascent() is negative + int width = 48; + int height = 48; + Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); + Canvas canvas = new Canvas(image); + canvas.drawRoundRect(new RectF(0, 0, height, width), 5, 5, paint); + paint.setAlpha(255); + paint.setColor(Color.TRANSPARENT); + paint.setTextSize(textSize); + paint.setTextAlign(Paint.Align.CENTER); + canvas.drawText(text, width / 2f, height / 2f + textSize / 2f, paint); + return image; + } + + case 0: + default: + paint.setTextAlign(Paint.Align.LEFT); + float baseline = -paint.ascent(); // ascent() is negative + int width = (int) (paint.measureText(text) + 0.5f); // round + int height = (int) (baseline + paint.descent() + 0.5f); + Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); + Canvas canvas = new Canvas(image); + canvas.drawText(text, 0, baseline, paint); + return image; + } } @Override @@ -116,6 +156,7 @@ implements SharedPreferences.OnSharedPreferenceChangeListener{ @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { + options = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); updateNotification(lastToast); } } diff --git a/app/src/main/java/space/neothefox/laytray/SettingsActivity.java b/app/src/main/java/space/neothefox/laytray/SettingsActivity.java index 3029149..37fb449 100644 --- a/app/src/main/java/space/neothefox/laytray/SettingsActivity.java +++ b/app/src/main/java/space/neothefox/laytray/SettingsActivity.java @@ -198,6 +198,7 @@ public class SettingsActivity extends AppCompatPreferenceActivity { // updated to reflect the new value, per the Android Design // guidelines. bindPreferenceSummaryToValue(findPreference("notificationImportance")); + bindPreferenceSummaryToValue(findPreference("textMode")); } @Override diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 448c305..9d99f80 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -27,6 +27,18 @@ -2 + Notification shape + + Plain Text + Text in circle + Text in a square + + + 0 + 1 + 2 + + diff --git a/app/src/main/res/xml/pref_notification.xml b/app/src/main/res/xml/pref_notification.xml index 8b48370..7d676ae 100644 --- a/app/src/main/res/xml/pref_notification.xml +++ b/app/src/main/res/xml/pref_notification.xml @@ -11,6 +11,13 @@ android:positiveButtonText="@null" android:title="@string/pref_title_notification_importance" /> + +