Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android Widgets #19

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
plugins {
id "com.android.application"
// START: FlutterFire Configuration
id 'com.google.gms.google-services'
// id 'com.google.gms.google-services'
// END: FlutterFire Configuration
id "kotlin-android"
// The Flutter Gradle Plugin must be applied after the Android and Kotlin Gradle plugins.
Expand Down Expand Up @@ -67,7 +67,7 @@ android {

buildTypes {
release {
// signingConfig signingConfigs.debug
signingConfig signingConfigs.debug
signingConfig signingConfigs.release
shrinkResources false
minifyEnabled true
Expand Down
24 changes: 24 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,30 @@
</intent-filter>
</receiver>

<receiver
android:name="HomeScreenSmallWidgetProvider"
android:label="أدعية وأذكار"
android:exported="true">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_info_small" />
</receiver>

<receiver
android:name=".HomeScreenMediumWidgetProvider"
android:label="أدعية وأذكار"
android:exported="true">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_info_medium" />
</receiver>

<activity
android:name=".MainActivity"
android:exported="true"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.noor.sa

import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProvider
import android.content.Context
import android.widget.RemoteViews
import org.json.JSONArray
import org.json.JSONObject
import java.io.InputStream

class HomeScreenMediumWidgetProvider : AppWidgetProvider() {

override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
for (appWidgetId in appWidgetIds) {
val views = RemoteViews(context.packageName, R.layout.widget_layout_medium)
val bodyText = readJsonFromRaw(context)
views.setTextViewText(R.id.widget_description, bodyText)
appWidgetManager.updateAppWidget(appWidgetId, views)
}
}

private fun readJsonFromRaw(context: Context): String {
return try {
val inputStream: InputStream = context.resources.openRawResource(R.raw.athkar)
val jsonText = inputStream.bufferedReader().use { it.readText() }
parseJson(jsonText)
} catch (e: Exception) {
"Error reading JSON"
}
}

private fun parseJson(jsonText: String): String {
return try {
val jsonObject = JSONObject(jsonText)
val athkarArray: JSONArray = jsonObject.getJSONArray("الدعاء إذا تقلب ليلاً")
val bodyText = StringBuilder()
for (i in 0 until athkarArray.length()) {
val athkar = athkarArray.getJSONObject(i)
bodyText.append(athkar.optString("text", "No text available"))
bodyText.append("\n\n")
}
bodyText.toString().trim()
} catch (e: Exception) {
"Error parsing JSON"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.noor.sa

import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProvider
import android.content.Context
import android.widget.RemoteViews
import org.json.JSONArray
import org.json.JSONObject
import java.io.InputStream

class HomeScreenSmallWidgetProvider : AppWidgetProvider() {

override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
for (appWidgetId in appWidgetIds) {
val views = RemoteViews(context.packageName, R.layout.widget_layout_small)
val bodyText = readJsonFromRaw(context)
views.setTextViewText(R.id.widget_description, bodyText)
appWidgetManager.updateAppWidget(appWidgetId, views)
}
}

private fun readJsonFromRaw(context: Context): String {
return try {
val inputStream: InputStream = context.resources.openRawResource(R.raw.athkar)
val jsonText = inputStream.bufferedReader().use { it.readText() }
parseJson(jsonText)
} catch (e: Exception) {
"Error reading JSON"
}
}

private fun parseJson(jsonText: String): String {
return try {
val jsonObject = JSONObject(jsonText)
val athkarArray: JSONArray = jsonObject.getJSONArray("الدعاء إذا تقلب ليلاً")
val bodyText = StringBuilder()
for (i in 0 until athkarArray.length()) {
val athkar = athkarArray.getJSONObject(i)
bodyText.append(athkar.optString("text", "No text available"))
bodyText.append("\n\n")
}
bodyText.toString().trim()
} catch (e: Exception) {
"Error parsing JSON"
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added android/app/src/main/res/drawable/widget_bg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added android/app/src/main/res/drawable/widget_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
32 changes: 32 additions & 0 deletions android/app/src/main/res/layout/widget_layout_medium.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:background="@drawable/widget_bg"
android:gravity="center"
android:layout_gravity="center">

<ImageView
android:id="@+id/widget_logo"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/widget_logo"
android:scaleType="fitCenter" />

<TextView
android:id="@+id/widget_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Loading..."
android:textSize="15sp"
android:textColor="#FFFFFFFF"
android:layout_marginBottom="18dp"
android:textStyle="bold"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:textAlignment="center"
android:fontFamily="@font/sst_arabic_light" />

</LinearLayout>
32 changes: 32 additions & 0 deletions android/app/src/main/res/layout/widget_layout_small.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:background="@drawable/square_widget_bg"
android:gravity="center"
android:layout_gravity="center">

<ImageView
android:id="@+id/widget_logo"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/widget_logo"
android:scaleType="fitCenter" />

<TextView
android:id="@+id/widget_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Loading..."
android:textSize="15sp"
android:textColor="#FFFFFFFF"
android:layout_marginBottom="18dp"
android:textStyle="bold"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:textAlignment="center"
android:fontFamily="@font/sst_arabic_light" />

</LinearLayout>
Loading