Skip to content

Commit

Permalink
add more home screen widget options
Browse files Browse the repository at this point in the history
the widgets are complete, they might look different depending on the android version and launcher used, previews are from API level 33 aka android 13.
  • Loading branch information
MoazSalem committed Oct 25, 2023
1 parent 037858e commit 43d74ec
Show file tree
Hide file tree
Showing 24 changed files with 476 additions and 26 deletions.
3 changes: 3 additions & 0 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ android {
shrinkResources true
}
}
buildFeatures {
viewBinding true
}

}

Expand Down
33 changes: 33 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,39 @@
android:icon="@mipmap/ic_launcher"
android:label="Colorful Notes"
android:requestLegacyExternalStorage="true">
<receiver
android:name=".WideNoteWidget"
android:exported="false">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>

<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/wide_note_widget_info" />
</receiver>
<receiver
android:name=".SmallNoteWidget"
android:exported="false">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>

<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/small_note_widget_info" />
</receiver>
<receiver
android:name=".MidNoteWidget"
android:exported="false">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>

<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/mid_note_widget_info" />
</receiver>
<receiver
android:name=".BigNoteWidget"
android:exported="false">
Expand Down
15 changes: 6 additions & 9 deletions android/app/src/main/java/com/moazsalem/notes/BigNoteWidget.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,16 @@ class BigNoteWidget : AppWidgetProvider() {
// Create RemoteViews
val views = RemoteViews(context.packageName, R.layout.big_note_widget).apply {
val widgetData = HomeWidgetPlugin.getData(context)
val titlesArray = widgetData.getString("titles", "")?.split("||S||")
val contentsArray = widgetData.getString("contents", "")?.split("||S||")
val titlesArray = widgetData.getString("titles", "No Notes")?.split("||S||")
val contentsArray = widgetData.getString("contents", "Add Notes from App")?.split("||S||")
val colorsArray = widgetData.getString("colors", "#F4B907")?.split("||S||")
val layoutsArray = widgetData.getString("layouts", "")?.split("||S||")
val textCArray = widgetData.getString("textColors", "")?.split("||S||")
val textColor = if(textCArray?.get(globalIndex) == "1") "#000000" else "#ffffff"
setTextViewText(R.id.title, titlesArray?.get(globalIndex) ?: "No Title")
setTextColor(R.id.title, Color.parseColor(textColor))
setTextViewText(R.id.content, contentsArray?.get(globalIndex) ?: "No Content")
setTextColor(R.id.content, Color.parseColor(textColor))
setInt(R.id.background, "setBackgroundColor", Color.parseColor(colorsArray?.get(globalIndex) ?: "#F4B907"))
if (titlesArray?.get(globalIndex) == "" && contentsArray?.get(globalIndex) == "") {
setTextViewText(R.id.title, "No Notes")
setTextViewText(R.id.content, "Add a note from the app")
return
}

// Set the click action to increment globalIndex
setOnClickPendingIntent(R.id.background, pendingIntent)
}
Expand Down
68 changes: 68 additions & 0 deletions android/app/src/main/java/com/moazsalem/notes/MidNoteWidget.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.moazsalem.notes

import android.app.PendingIntent
import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProvider
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.widget.RemoteViews
import es.antonborri.home_widget.HomeWidgetPlugin

private var globalIndex: Int = 0
class MidNoteWidget : AppWidgetProvider() {
override fun onUpdate(
context: Context,
appWidgetManager: AppWidgetManager,
appWidgetIds: IntArray
) {
for (appWidgetId in appWidgetIds) {
// Create an intent to handle widget click
val intent = Intent(context, MidNoteWidget::class.java)
intent.action = "widgetClicked"

// Create a PendingIntent for the widget click
val pendingIntent = PendingIntent.getBroadcast(context, 0, intent,
PendingIntent.FLAG_IMMUTABLE)

// Create RemoteViews
val views = RemoteViews(context.packageName, R.layout.mid_note_widget).apply {
val widgetData = HomeWidgetPlugin.getData(context)
val titlesArray = widgetData.getString("titles", "No Notes")?.split("||S||")
val contentsArray = widgetData.getString("contents", "Add Notes from App")?.split("||S||")
val colorsArray = widgetData.getString("colors", "#ff8b34")?.split("||S||")
val textCArray = widgetData.getString("textColors", "")?.split("||S||")
val textColor = if(textCArray?.get(globalIndex) == "1") "#000000" else "#ffffff"
setTextViewText(R.id.title, titlesArray?.get(globalIndex) ?: "No Title")
setTextColor(R.id.title, Color.parseColor(textColor))
setTextViewText(R.id.content, contentsArray?.get(globalIndex) ?: "No Content")
setTextColor(R.id.content, Color.parseColor(textColor))
setInt(R.id.background, "setBackgroundColor", Color.parseColor(colorsArray?.get(globalIndex) ?: "#ff8b34"))
// Set the click action to increment globalIndex
setOnClickPendingIntent(R.id.background, pendingIntent)
}

appWidgetManager.updateAppWidget(appWidgetId, views)
}
}

override fun onReceive(context: Context, intent: Intent) {
super.onReceive(context, intent)

if (intent.action == "widgetClicked") {
// Handle widget click
val widgetData = HomeWidgetPlugin.getData(context)
val titlesArray = widgetData.getString("titles", "")?.split("||S||")
if (titlesArray != null && globalIndex < titlesArray.size - 1) {
globalIndex++
} else {
globalIndex = 0
}

val appWidgetManager = AppWidgetManager.getInstance(context)
val appWidgetIds = appWidgetManager.getAppWidgetIds(ComponentName(context, MidNoteWidget::class.java))
onUpdate(context, appWidgetManager, appWidgetIds)
}
}
}
68 changes: 68 additions & 0 deletions android/app/src/main/java/com/moazsalem/notes/SmallNoteWidget.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.moazsalem.notes

import android.app.PendingIntent
import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProvider
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.widget.RemoteViews
import es.antonborri.home_widget.HomeWidgetPlugin

private var globalIndex: Int = 0
class SmallNoteWidget : AppWidgetProvider() {
override fun onUpdate(
context: Context,
appWidgetManager: AppWidgetManager,
appWidgetIds: IntArray
) {
for (appWidgetId in appWidgetIds) {
// Create an intent to handle widget click
val intent = Intent(context, SmallNoteWidget::class.java)
intent.action = "widgetClicked"

// Create a PendingIntent for the widget click
val pendingIntent = PendingIntent.getBroadcast(context, 0, intent,
PendingIntent.FLAG_IMMUTABLE)

// Create RemoteViews
val views = RemoteViews(context.packageName, R.layout.small_note_widget).apply {
val widgetData = HomeWidgetPlugin.getData(context)
val titlesArray = widgetData.getString("titles", "No Notes")?.split("||S||")
val contentsArray = widgetData.getString("contents", "Add Notes from App")?.split("||S||")
val colorsArray = widgetData.getString("colors", "#f77b85")?.split("||S||")
val textCArray = widgetData.getString("textColors", "")?.split("||S||")
val textColor = if(textCArray?.get(globalIndex) == "1") "#000000" else "#ffffff"
setTextViewText(R.id.title, titlesArray?.get(globalIndex) ?: "No Title")
setTextColor(R.id.title, Color.parseColor(textColor))
setTextViewText(R.id.content, contentsArray?.get(globalIndex) ?: "No Content")
setTextColor(R.id.content, Color.parseColor(textColor))
setInt(R.id.background, "setBackgroundColor", Color.parseColor(colorsArray?.get(globalIndex) ?: "#f77b85"))
// Set the click action to increment globalIndex
setOnClickPendingIntent(R.id.background, pendingIntent)
}

appWidgetManager.updateAppWidget(appWidgetId, views)
}
}

override fun onReceive(context: Context, intent: Intent) {
super.onReceive(context, intent)

if (intent.action == "widgetClicked") {
// Handle widget click
val widgetData = HomeWidgetPlugin.getData(context)
val titlesArray = widgetData.getString("titles", "")?.split("||S||")
if (titlesArray != null && globalIndex < titlesArray.size - 1) {
globalIndex++
} else {
globalIndex = 0
}

val appWidgetManager = AppWidgetManager.getInstance(context)
val appWidgetIds = appWidgetManager.getAppWidgetIds(ComponentName(context, SmallNoteWidget::class.java))
onUpdate(context, appWidgetManager, appWidgetIds)
}
}
}
68 changes: 68 additions & 0 deletions android/app/src/main/java/com/moazsalem/notes/WideNoteWidget.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.moazsalem.notes

import android.app.PendingIntent
import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProvider
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.widget.RemoteViews
import es.antonborri.home_widget.HomeWidgetPlugin

private var globalIndex: Int = 0
class WideNoteWidget : AppWidgetProvider() {
override fun onUpdate(
context: Context,
appWidgetManager: AppWidgetManager,
appWidgetIds: IntArray
) {
for (appWidgetId in appWidgetIds) {
// Create an intent to handle widget click
val intent = Intent(context, WideNoteWidget::class.java)
intent.action = "widgetClicked"

// Create a PendingIntent for the widget click
val pendingIntent = PendingIntent.getBroadcast(context, 0, intent,
PendingIntent.FLAG_IMMUTABLE)

// Create RemoteViews
val views = RemoteViews(context.packageName, R.layout.wide_note_widget).apply {
val widgetData = HomeWidgetPlugin.getData(context)
val titlesArray = widgetData.getString("titles", "No Notes")?.split("||S||")
val contentsArray = widgetData.getString("contents", "Add Notes from App")?.split("||S||")
val colorsArray = widgetData.getString("colors", "#66c6c2")?.split("||S||")
val textCArray = widgetData.getString("textColors", "")?.split("||S||")
val textColor = if(textCArray?.get(globalIndex) == "1") "#000000" else "#ffffff"
setTextViewText(R.id.title, titlesArray?.get(globalIndex) ?: "No Title")
setTextColor(R.id.title, Color.parseColor(textColor))
setTextViewText(R.id.content, contentsArray?.get(globalIndex) ?: "No Content")
setTextColor(R.id.content, Color.parseColor(textColor))
setInt(R.id.background, "setBackgroundColor", Color.parseColor(colorsArray?.get(globalIndex) ?: "#66c6c2"))
// Set the click action to increment globalIndex
setOnClickPendingIntent(R.id.background, pendingIntent)
}

appWidgetManager.updateAppWidget(appWidgetId, views)
}
}

override fun onReceive(context: Context, intent: Intent) {
super.onReceive(context, intent)

if (intent.action == "widgetClicked") {
// Handle widget click
val widgetData = HomeWidgetPlugin.getData(context)
val titlesArray = widgetData.getString("titles", "")?.split("||S||")
if (titlesArray != null && globalIndex < titlesArray.size - 1) {
globalIndex++
} else {
globalIndex = 0
}

val appWidgetManager = AppWidgetManager.getInstance(context)
val appWidgetIds = appWidgetManager.getAppWidgetIds(ComponentName(context, WideNoteWidget::class.java))
onUpdate(context, appWidgetManager, appWidgetIds)
}
}
}
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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 8 additions & 6 deletions android/app/src/main/res/layout/big_note_widget.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F4B907"
android:gravity="start"
android:padding="0dp"
android:theme="@style/Theme.Android.AppWidgetContainer">

Expand All @@ -23,27 +24,28 @@
android:maxLines="2"
android:minWidth="80dp"
android:paddingLeft="20dp"
android:paddingTop="30dp"
android:paddingTop="20dp"
android:paddingRight="20dp"
android:text="@string/title"
android:textAlignment="center"
android:textColor="#ffffff"
android:textSize="26sp"
android:textSize="28sp"
android:textStyle="bold" />

<TextView
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="710dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="end"
android:maxLines="18"
android:gravity="start"
android:minWidth="80dp"
android:minLines="1"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:paddingRight="20dp"
android:paddingBottom="20dp"
android:text="@string/content"
android:textColor="#FFFFFF"
android:textSize="16sp" />
android:textSize="18sp" />
</LinearLayout>
</RelativeLayout>
50 changes: 50 additions & 0 deletions android/app/src/main/res/layout/mid_note_widget.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/background"
style="@style/Widget.Android.AppWidget.Container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff8b34"
android:padding="0dp"
android:theme="@style/Theme.Android.AppWidgetContainer">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="92dp"
android:ellipsize="end"
android:gravity="center|center_horizontal|center_vertical"
android:maxLines="2"
android:minWidth="80dp"
android:paddingLeft="20dp"
android:paddingTop="20dp"
android:paddingRight="20dp"
android:text="@string/title"
android:textAlignment="center"
android:textColor="#ffffff"
android:textSize="26sp"
android:textStyle="bold" />

<TextView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="end"
android:gravity="start"
android:minWidth="80dp"
android:minLines="1"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:paddingRight="20dp"
android:paddingBottom="20dp"
android:text="@string/content"
android:textColor="#FFFFFF"
android:textSize="16sp" />
</LinearLayout>
</RelativeLayout>
Loading

0 comments on commit 43d74ec

Please sign in to comment.