Skip to content

Commit

Permalink
update widget
Browse files Browse the repository at this point in the history
  • Loading branch information
VishnuSanal committed Nov 18, 2023
1 parent 234d25b commit 1b4d3ea
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 30 deletions.
66 changes: 59 additions & 7 deletions app/src/main/java/phone/vishnu/quotes/receiver/QuoteWidget.java
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand All @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_favorite_outline.xml
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="@color/textColor"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M19.66,3.99c-2.64,-1.8 -5.9,-0.96 -7.66,1.1 -1.76,-2.06 -5.02,-2.91 -7.66,-1.1 -1.4,0.96 -2.28,2.58 -2.34,4.29 -0.14,3.88 3.3,6.99 8.55,11.76l0.1,0.09c0.76,0.69 1.93,0.69 2.69,-0.01l0.11,-0.1c5.25,-4.76 8.68,-7.87 8.55,-11.75 -0.06,-1.7 -0.94,-3.32 -2.34,-4.28zM12.1,18.55l-0.1,0.1 -0.1,-0.1C7.14,14.24 4,11.39 4,8.5 4,6.5 5.5,5 7.5,5c1.54,0 3.04,0.99 3.57,2.36h1.87C13.46,5.99 14.96,5 16.5,5c2,0 3.5,1.5 3.5,3.5 0,2.89 -3.14,5.74 -7.9,10.05z" />
</vector>
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_share_outline.xml
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="@color/textColor"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M18,16.08c-0.76,0 -1.44,0.3 -1.96,0.77L8.91,12.7c0.05,-0.23 0.09,-0.46 0.09,-0.7s-0.04,-0.47 -0.09,-0.7l7.05,-4.11c0.54,0.5 1.25,0.81 2.04,0.81 1.66,0 3,-1.34 3,-3s-1.34,-3 -3,-3 -3,1.34 -3,3c0,0.24 0.04,0.47 0.09,0.7L8.04,9.81C7.5,9.31 6.79,9 6,9c-1.66,0 -3,1.34 -3,3s1.34,3 3,3c0.79,0 1.5,-0.31 2.04,-0.81l7.12,4.16c-0.05,0.21 -0.08,0.43 -0.08,0.65 0,1.61 1.31,2.92 2.92,2.92s2.92,-1.31 2.92,-2.92c0,-1.61 -1.31,-2.92 -2.92,-2.92zM18,4c0.55,0 1,0.45 1,1s-0.45,1 -1,1 -1,-0.45 -1,-1 0.45,-1 1,-1zM6,13c-0.55,0 -1,-0.45 -1,-1s0.45,-1 1,-1 1,0.45 1,1 -0.45,1 -1,1zM18,20.02c-0.55,0 -1,-0.45 -1,-1s0.45,-1 1,-1 1,0.45 1,1 -0.45,1 -1,1z" />
</vector>
12 changes: 12 additions & 0 deletions app/src/main/res/drawable/widget_background.xml
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

<corners
android:bottomLeftRadius="24dp"
android:bottomRightRadius="24dp"
android:topLeftRadius="24dp"
android:topRightRadius="24dp" />

<solid android:color="@color/BGColor" />

</shape>
51 changes: 28 additions & 23 deletions app/src/main/res/layout/quote_widget.xml
@@ -1,10 +1,10 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/widget_background"
android:gravity="center"
android:orientation="vertical"
>
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
Expand All @@ -19,35 +19,40 @@
android:layout_height="wrap_content"
android:contentDescription="@string/quote_widget_favourite_button"
android:padding="8dp"
android:src="@drawable/ic_favorite" />
android:src="@drawable/ic_favorite_outline"
app:tint="@color/textColor" />

<ImageView
android:id="@+id/widgetShareImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/quote_widget_share_button"
android:padding="8dp"
android:src="@drawable/ic_share" />
android:src="@drawable/ic_share_outline"
app:tint="@color/textColor" />

</LinearLayout>

<TextView
android:id="@+id/widgetQuoteTextView"
android:layout_width="match_parent"
<ImageView
android:id="@+id/widgetQuoteContainerImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="serif"
android:gravity="center"
android:text="@string/quote_loading"
android:textColor="@android:color/darker_gray"
android:textSize="24sp" />
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" />

<TextView
android:id="@+id/widgetAuthorTextView"
android:layout_width="match_parent"
<ImageView
android:id="@+id/widgetAuthorContainerImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="cursive"
android:gravity="end"
android:text=""
android:textColor="@android:color/darker_gray"
android:textSize="20sp" />
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" />

</LinearLayout>
</LinearLayout>
2 changes: 2 additions & 0 deletions app/src/main/res/values-night/colors.xml
Expand Up @@ -20,4 +20,6 @@

<color name="sliderIconColor">@color/BGColor</color>

<color name="widgetTextColor">@color/textColorLight</color>

</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values/colors.xml
Expand Up @@ -25,6 +25,8 @@

<color name="sliderIconColor">#DCDCDC</color>

<color name="widgetTextColor">@color/textColor</color>


<color name="colorWhite">#FFFFFF</color>

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Expand Up @@ -186,6 +186,8 @@
<string name="settings_screen_share_tv_hint" translatable="false">Settings Screen Share TV Hint</string>
<string name="quote_widget_favourite_button" translatable="false">Quote Widget Favourite Button</string>
<string name="quote_widget_share_button" translatable="false">Quote Widget Share Button</string>
<string name="quote_widget_quote_container_imageview" translatable="false">Quote Widget Quote Container ImageView</string>
<string name="quote_widget_author_container_imageview" translatable="false">Quote Widget Author Container ImageView</string>
<string name="bottom_sheet_done_indicator" translatable="false">Bottom Sheet Done Indicator</string>
<string name="tour_fragment_imageview" translatable="false">Tour Fragment ImageView</string>
<string name="home_search_image_view" translatable="false">Home Search ImageView</string>
Expand Down

0 comments on commit 1b4d3ea

Please sign in to comment.