Skip to content

Commit

Permalink
Merge branch 'home-widget'
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLastGimbus committed Sep 19, 2023
2 parents be442aa + f63f3ac commit b269c51
Show file tree
Hide file tree
Showing 22 changed files with 410 additions and 35 deletions.
18 changes: 17 additions & 1 deletion android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,22 @@ apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
namespace 'com.lastgimbus.the.freebuddy'
compileSdk 33
compileSdk 34
ndkVersion flutter.ndkVersion

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

buildFeatures {
compose true
}

composeOptions {
kotlinCompilerExtensionVersion = "1.5.3"
}

kotlinOptions {
jvmTarget = '1.8'
}
Expand Down Expand Up @@ -92,4 +100,12 @@ flutter {

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

// All of this for AppWidgets support
implementation "androidx.compose.runtime:runtime:1.5.1"
implementation "androidx.compose.runtime:runtime-livedata:1.5.1"
implementation "androidx.compose.runtime:runtime-rxjava2:1.5.1"
implementation "androidx.glance:glance-appwidget:1.0.0"
implementation "androidx.glance:glance-material3:1.0.0"
implementation 'androidx.compose.ui:ui-unit-android:1.5.1'
}
45 changes: 28 additions & 17 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,32 @@
<!-- <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />-->

<!-- Request legacy Bluetooth permissions on older devices. -->
<uses-permission android:name="android.permission.BLUETOOTH"
android:maxSdkVersion="30"/>
<uses-permission
android:name="android.permission.BLUETOOTH"
android:maxSdkVersion="30"/>
<!-- Needed only if your app communicates with already-paired Bluetooth devices. -->
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>


<application
android:label="freebuddy"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
android:label="freebuddy"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
<activity
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<!-- Specifies an Android theme to apply to this Activity as soon as
the Android process has started. This theme is visible to the user
while the Flutter UI initializes. After that, this theme continues
to determine the Window background behind the Flutter UI. -->
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme"
/>
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme"/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
Expand All @@ -43,9 +43,20 @@
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />
android:value="2"/>

<service android:name="com.pravera.flutter_foreground_task.service.ForegroundService" />
<service android:name="com.pravera.flutter_foreground_task.service.ForegroundService"/>

<receiver
android:name=".BatteryWidgetReceiver"
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/battery_widget_config"/>
</receiver>

</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package com.lastgimbus.the.freebuddy

import android.content.Context
import androidx.compose.runtime.Composable
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.glance.*
import androidx.glance.appwidget.*
import androidx.glance.layout.*
import androidx.glance.text.FontWeight
import androidx.glance.text.Text
import androidx.glance.text.TextDefaults
import androidx.glance.unit.ColorProvider
import es.antonborri.home_widget.HomeWidgetPlugin
import kotlin.math.max


class BatteryWidget : GlanceAppWidget() {
// TODO: Fuck with this a bit more
companion object {
private val SMALL_SQUARE = DpSize(100.dp, 100.dp)
private val HORIZONTAL_RECTANGLE = DpSize(220.dp, 100.dp)
private val VERTICAL_RECTANGLE = DpSize(100.dp, 180.dp)
private val BIG_SQUARE = DpSize(200.dp, 200.dp)
}

override val sizeMode = SizeMode.Responsive(
setOf(
SMALL_SQUARE,
HORIZONTAL_RECTANGLE,
VERTICAL_RECTANGLE,
BIG_SQUARE,
)
)

override suspend fun provideGlance(context: Context, id: GlanceId) {

provideContent {
GlanceTheme {
val sp = HomeWidgetPlugin.getData(LocalContext.current)
val left = sp.getInt("left", 0)
val right = sp.getInt("right", 0)
val case = sp.getInt("case", 0)
val size = LocalSize.current
val barColor = ColorProvider(R.color.battery_widget_bar_color)
val barBackground = ColorProvider(R.color.battery_widget_bar_background)
val textStyle = TextDefaults.defaultTextStyle.copy(
color = ColorProvider(R.color.battery_widget_text_color),
fontWeight = FontWeight.Medium, fontSize = 16.sp
)

// TODO: Implement all cases here

if (size.height < VERTICAL_RECTANGLE.height) {
Row(
modifier = GlanceModifier.fillMaxSize().appWidgetBackground()
.background(GlanceTheme.colors.background)
.cornerRadius(R.dimen.batteryWidgetBackgroundRadius)
) {
@Composable
fun BatteryBox(progress: Float, text: String) {
Box(
modifier = GlanceModifier
.defaultWeight()
.fillMaxHeight()
.padding(R.dimen.batteryWidgetPadding),
contentAlignment = Alignment.Center
) {
LinearProgressIndicator(
modifier = GlanceModifier
.cornerRadius(R.dimen.batteryWidgetInnerRadius)
.fillMaxSize(),
progress = progress,
color = barColor,
backgroundColor = barBackground
)
Text(text, style = textStyle)
}

}
if (size.width <= SMALL_SQUARE.width) {
val b = max(left, right)
BatteryBox(b / 100f, "Buds • $b%")
} else {
BatteryBox(left / 100f, "Left • $left%")
BatteryBox(right / 100f, "Right • $right%")
BatteryBox(case / 100f, "Case • $case%")
}


}
} else {
Column(
modifier = GlanceModifier
.fillMaxSize()
.appWidgetBackground()
.background(GlanceTheme.colors.background)
.cornerRadius(R.dimen.batteryWidgetBackgroundRadius)
) {
@Composable
fun BatteryBox(progress: Float, text: String) {
Box(
modifier = GlanceModifier
.defaultWeight()
.padding(R.dimen.batteryWidgetPadding),
contentAlignment = Alignment.Center
) {
LinearProgressIndicator(
modifier = GlanceModifier
.cornerRadius(R.dimen.batteryWidgetInnerRadius)
.fillMaxSize(),
progress = progress,
color = barColor,
backgroundColor = barBackground
)
Text(text, style = textStyle)
}

}
BatteryBox(left / 100f, "Left • $left%")
BatteryBox(right / 100f, "Right • $right%")
BatteryBox(case / 100f, "Case • $case%")
}
}
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.lastgimbus.the.freebuddy

import androidx.glance.appwidget.GlanceAppWidget
import androidx.glance.appwidget.GlanceAppWidgetReceiver

class BatteryWidgetReceiver : GlanceAppWidgetReceiver() {
override val glanceAppWidget: GlanceAppWidget = BatteryWidget()
}
9 changes: 9 additions & 0 deletions android/app/src/main/res/drawable/charger_fill.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="960"
android:viewportHeight="960">
<path
android:fillColor="@android:color/white"
android:pathData="M452,800L652,410L512,410L512,160L312,550L452,550L452,800ZM480,880Q397,880 324,848.5Q251,817 197,763Q143,709 111.5,636Q80,563 80,480Q80,397 111.5,324Q143,251 197,197Q251,143 324,111.5Q397,80 480,80Q563,80 636,111.5Q709,143 763,197Q817,251 848.5,324Q880,397 880,480Q880,563 848.5,636Q817,709 763,763Q709,817 636,848.5Q563,880 480,880Z"/>
</vector>
13 changes: 13 additions & 0 deletions android/app/src/main/res/drawable/earbuds_case.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- I made this by adding a single line to https://pictogrammers.com/library/mdi/icon/circle-outline/ <3 -->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FF000000"
android:pathData="M12,20A8,8 0,0 1,4 12A8,8 0,0 1,12 4A8,8 0,0 1,20 12A8,8 0,0 1,12 20M12,2A10,10 0,0 0,2 12A10,10 0,0 0,12 22A10,10 0,0 0,22 12A10,10 0,0 0,12 2Z"/>
<path
android:pathData="M20.038,8.135H4.199L3.518,10.228H21.166Z"
android:fillColor="#000000"/>
</vector>
10 changes: 10 additions & 0 deletions android/app/src/main/res/drawable/left_earbud.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!-- I made this by modifying https://pictogrammers.com/library/mdi/icon/earbuds-outline/ a bit <3 -->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FF000000"
android:pathData="m13,3c2,0 3,2 3,3v5c0,1 -1,3 -3,3 -0.61,0 -1.32,-0.28 -2,-0.73V20c0,0.55 -0.45,1 -1,1H9C8.45,21 8,20.55 8,20V8C8,6 11,3 13,3m-3,7.23 l2.09,1.37c0.51,0.33 0.83,0.4 0.91,0.4 0.7,0 1,-0.92 1,-1V6.03C14,5.92 13.7,5 13,5 12.1,5 10,7.1 10,8v2.23"/>
</vector>
10 changes: 10 additions & 0 deletions android/app/src/main/res/drawable/right_earbud.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!-- I made this by modifying https://pictogrammers.com/library/mdi/icon/earbuds-outline/ a bit <3 -->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FF000000"
android:pathData="M11,3C9,3 8,5 8,6v5c0,1 1,3 3,3 0.61,0 1.32,-0.28 2,-0.73V20c0,0.55 0.45,1 1,1h1c0.55,0 1,-0.45 1,-1V8C16,6 13,3 11,3m3,7.23 l-2.09,1.37C11.4,11.93 11.08,12 11,12 10.3,12 10,11.08 10,11V6.03C10,5.92 10.3,5 11,5c0.9,0 3,2.1 3,3v2.23"/>
</vector>
6 changes: 6 additions & 0 deletions android/app/src/main/res/values-night-v31/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="battery_widget_text_color">@android:color/system_accent1_100</color>
<color name="battery_widget_bar_color">@android:color/system_accent1_600</color>
<color name="battery_widget_bar_background">@android:color/system_accent1_800</color>
</resources>
6 changes: 6 additions & 0 deletions android/app/src/main/res/values-night/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="battery_widget_text_color">#C1E8FF</color>
<color name="battery_widget_bar_color">#00668B</color>
<color name="battery_widget_bar_background">#003549</color>
</resources>
6 changes: 6 additions & 0 deletions android/app/src/main/res/values-v31/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="battery_widget_text_color">@android:color/system_accent1_900</color>
<color name="battery_widget_bar_color">@android:color/system_accent1_200</color>
<color name="battery_widget_bar_background">@android:color/system_accent1_100</color>
</resources>
5 changes: 5 additions & 0 deletions android/app/src/main/res/values-v31/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="batteryWidgetBackgroundRadius">@android:dimen/system_app_widget_background_radius</dimen>
<dimen name="batteryWidgetInnerRadius">@android:dimen/system_app_widget_inner_radius</dimen>
</resources>
6 changes: 6 additions & 0 deletions android/app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="ic_launcher_background">#ffefefef</color>
<!-- I was looking for those fucking values for like 1.5 hour -->
<!-- There they were, hidden in some documentation for vendors system implementations?? Or something?? -->
<!-- https://source.android.com/docs/core/display/dynamic-color -->
<color name="battery_widget_text_color">#001E2C</color>
<color name="battery_widget_bar_color">#76D1FF</color>
<color name="battery_widget_bar_background">#C1E8FF</color>
</resources>
6 changes: 6 additions & 0 deletions android/app/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="batteryWidgetPadding">8dp</dimen>
<dimen name="batteryWidgetBackgroundRadius">16dp</dimen>
<dimen name="batteryWidgetInnerRadius">8dp</dimen>
</resources>
8 changes: 8 additions & 0 deletions android/app/src/main/res/xml/battery_widget_config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="40dp"
android:minHeight="40dp"
android:updatePeriodMillis="86400000"
android:initialLayout="@layout/glance_default_loading_layout"
android:resizeMode="horizontal|vertical"
android:widgetCategory="home_screen" />
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
buildscript {
ext.kotlin_version = '1.8.10'
ext.kotlin_version = '1.9.10'
repositories {
google()
mavenCentral()
Expand Down
15 changes: 13 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import 'package:streaming_shared_preferences/streaming_shared_preferences.dart';
import 'package:the_last_bluetooth/the_last_bluetooth.dart';

import 'headphones/cubit/headphones_connection_cubit.dart';
import 'headphones/cubit/headphones_cubit_objects.dart';
import 'headphones/cubit/headphones_mock_cubit.dart';
import 'ui/app_settings.dart';
import 'ui/appwidgets/battery_appwidget.dart';
import 'ui/pages/about/about_page.dart';
import 'ui/pages/headphones_settings/headphones_settings_page.dart';
import 'ui/pages/home/home_page.dart';
Expand All @@ -29,14 +31,23 @@ void main() {
providers: [
BlocProvider<HeadphonesConnectionCubit>(
// need to use kIsWeb because Platform is from dart:io
create: (_) => (!kIsWeb && Platform.isAndroid)
create: (_) => (!kIsWeb &&
Platform.isAndroid &&
!const bool.fromEnvironment('USE_HEADPHONES_MOCK'))
? HeadphonesConnectionCubit(
bluetooth: TheLastBluetooth.instance,
)
: HeadphonesMockCubit(),
),
],
child: const MyApp(),
// don't know if this is good place to put this, but seems right
// maybe convert this to multi listener with advanced "listenWhen" logic
// this would make it a nice single place to know what launches when 🤔
child: const BlocListener<HeadphonesConnectionCubit,
HeadphonesConnectionState>(
listener: batteryHomeWidgetHearBloc,
child: MyApp(),
),
),
),
);
Expand Down
Loading

0 comments on commit b269c51

Please sign in to comment.