From 1b4d3ea9a541cc6b0256cd8c8e402fd38a5dc2d1 Mon Sep 17 00:00:00 2001 From: VishnuSanal Date: Sat, 18 Nov 2023 13:32:24 +0530 Subject: [PATCH] update widget --- .../vishnu/quotes/receiver/QuoteWidget.java | 66 +++++++++++++++++-- .../main/res/drawable/ic_favorite_outline.xml | 10 +++ .../main/res/drawable/ic_share_outline.xml | 10 +++ .../main/res/drawable/widget_background.xml | 12 ++++ app/src/main/res/layout/quote_widget.xml | 51 +++++++------- app/src/main/res/values-night/colors.xml | 2 + app/src/main/res/values/colors.xml | 2 + app/src/main/res/values/strings.xml | 2 + 8 files changed, 125 insertions(+), 30 deletions(-) create mode 100644 app/src/main/res/drawable/ic_favorite_outline.xml create mode 100644 app/src/main/res/drawable/ic_share_outline.xml create mode 100644 app/src/main/res/drawable/widget_background.xml diff --git a/app/src/main/java/phone/vishnu/quotes/receiver/QuoteWidget.java b/app/src/main/java/phone/vishnu/quotes/receiver/QuoteWidget.java index 6f259598..ad0ce3f9 100644 --- a/app/src/main/java/phone/vishnu/quotes/receiver/QuoteWidget.java +++ b/app/src/main/java/phone/vishnu/quotes/receiver/QuoteWidget.java @@ -25,13 +25,22 @@ import android.content.ComponentName; import android.content.Context; import android.content.Intent; +import android.graphics.Bitmap; +import android.graphics.Canvas; +import android.graphics.Typeface; import android.os.Build; +import android.text.Layout; +import android.text.StaticLayout; +import android.text.TextPaint; +import android.util.Log; import android.widget.RemoteViews; +import java.io.File; import phone.vishnu.quotes.R; import phone.vishnu.quotes.activity.MainActivity; import phone.vishnu.quotes.helper.AlarmHelper; import phone.vishnu.quotes.helper.Constants; import phone.vishnu.quotes.helper.SharedPreferenceHelper; +import phone.vishnu.quotes.helper.Utils; import phone.vishnu.quotes.model.Quote; import phone.vishnu.quotes.repository.QuotesRepository; @@ -96,9 +105,13 @@ private PendingIntent getPendingIntent(Context context, int REQ_CODE) { private void updateQuoteWidget(Context context, Quote quote) { RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.quote_widget); - remoteViews.setTextViewText(R.id.widgetQuoteTextView, quote.getQuote()); - remoteViews.setTextViewText( - R.id.widgetAuthorTextView, String.format("-%s", quote.getAuthor())); + remoteViews.setImageViewBitmap( + R.id.widgetQuoteContainerImageView, + buildBitmap(context, quote.getQuote(), Layout.Alignment.ALIGN_CENTER)); + + remoteViews.setImageViewBitmap( + R.id.widgetAuthorContainerImageView, + buildBitmap(context, "- " + quote.getAuthor(), Layout.Alignment.ALIGN_OPPOSITE)); remoteViews.setOnClickPendingIntent( R.id.widgetShareImageView, @@ -114,13 +127,52 @@ private void updateQuoteWidget(Context context, Quote quote) { saveWidgetQuote(context, quote); } - private void initAppWidget(final Context context) { + public Bitmap buildBitmap(Context context, String text, Layout.Alignment alignment) { + TextPaint textPaint = new TextPaint(); + textPaint.setAntiAlias(true); + textPaint.setTextSize(Utils.Companion.DPtoPX(context, 24)); + textPaint.setColor(context.getResources().getColor(R.color.widgetTextColor)); + + SharedPreferenceHelper sharedPreferenceHelper = new SharedPreferenceHelper(context); + String fontPath = sharedPreferenceHelper.getFontPath(); + + if (!(fontPath.equals("-1")) && (new File(fontPath).exists())) { + try { + Typeface face = Typeface.createFromFile(fontPath); + textPaint.setTypeface(face); + } catch (Exception e) { + e.printStackTrace(); + } + } + + StaticLayout staticLayout = + new StaticLayout( + text, + textPaint, + Utils.Companion.getScreenWidth(), + alignment, + 1.0f, + 0, + false); + + Bitmap bitmap = + Bitmap.createBitmap( + Utils.Companion.getScreenWidth(), + staticLayout.getHeight(), + Bitmap.Config.ARGB_8888); + + staticLayout.draw(new Canvas(bitmap)); + + return bitmap; + } + + private void initAppWidget(final Context context) { new QuotesRepository() .getRandomQuote( - quote -> { - updateQuoteWidget(context, quote); - }); + quote -> + updateQuoteWidget(context, quote) + ); } private void saveWidgetQuote(Context context, Quote quote) { diff --git a/app/src/main/res/drawable/ic_favorite_outline.xml b/app/src/main/res/drawable/ic_favorite_outline.xml new file mode 100644 index 00000000..c908dcfb --- /dev/null +++ b/app/src/main/res/drawable/ic_favorite_outline.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/drawable/ic_share_outline.xml b/app/src/main/res/drawable/ic_share_outline.xml new file mode 100644 index 00000000..e906bdc2 --- /dev/null +++ b/app/src/main/res/drawable/ic_share_outline.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/drawable/widget_background.xml b/app/src/main/res/drawable/widget_background.xml new file mode 100644 index 00000000..9156f90c --- /dev/null +++ b/app/src/main/res/drawable/widget_background.xml @@ -0,0 +1,12 @@ + + + + + + + + diff --git a/app/src/main/res/layout/quote_widget.xml b/app/src/main/res/layout/quote_widget.xml index ca5dc0a5..2b7ed7bc 100644 --- a/app/src/main/res/layout/quote_widget.xml +++ b/app/src/main/res/layout/quote_widget.xml @@ -1,10 +1,10 @@ + android:orientation="vertical"> + android:src="@drawable/ic_favorite_outline" + app:tint="@color/textColor" /> + android:src="@drawable/ic_share_outline" + app:tint="@color/textColor" /> + - + android:layout_gravity="center_vertical" + android:layout_marginStart="8dp" + android:layout_marginTop="8dp" + android:layout_marginEnd="8dp" + android:layout_marginBottom="2dp" + android:contentDescription="@string/quote_widget_quote_container_imageview" /> - + android:layout_gravity="end" + android:layout_marginStart="8dp" + android:layout_marginTop="2dp" + android:layout_marginEnd="8dp" + android:layout_marginBottom="8dp" + android:contentDescription="@string/quote_widget_author_container_imageview" /> - \ No newline at end of file + diff --git a/app/src/main/res/values-night/colors.xml b/app/src/main/res/values-night/colors.xml index 4c094c79..6b216bf3 100644 --- a/app/src/main/res/values-night/colors.xml +++ b/app/src/main/res/values-night/colors.xml @@ -20,4 +20,6 @@ @color/BGColor + @color/textColorLight + diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index b5b583c5..87702cb1 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -25,6 +25,8 @@ #DCDCDC + @color/textColor + #FFFFFF diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 400805c2..8157bc73 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -186,6 +186,8 @@ Settings Screen Share TV Hint Quote Widget Favourite Button Quote Widget Share Button + Quote Widget Quote Container ImageView + Quote Widget Author Container ImageView Bottom Sheet Done Indicator Tour Fragment ImageView Home Search ImageView