Skip to content

Commit

Permalink
Merge pull request #14 from enpitut2021/funami/home_widget
Browse files Browse the repository at this point in the history
Funami/home_widget: Androidでウィジェットを押すことでアプリが開く
  • Loading branch information
pfunami authored Dec 3, 2021
2 parents f393d0a + a427c8e commit d6df3f9
Show file tree
Hide file tree
Showing 12 changed files with 499 additions and 26 deletions.
69 changes: 44 additions & 25 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,46 +1,65 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.chimimoryo_autumn">
<application
android:label="chimimoryo_autumn"
android:icon="@mipmap/ic_launcher">
package="com.example.chimimoryo_autumn">
<application
android:label="chimimoryo_autumn"
android:icon="@mipmap/ic_launcher">
<activity
android:name=".MainActivity"
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: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"
/>
<!-- Displays an Android View that continues showing the launch screen
Drawable until Flutter paints its first frame, then this splash
screen fades out. A splash screen is useful to avoid any visual
gap between the end of Android's launch screen and the painting of
Flutter's first frame. -->
<meta-data
android:name="io.flutter.embedding.android.SplashScreenDrawable"
android:resource="@drawable/launch_background" />
android:name="io.flutter.embedding.android.SplashScreenDrawable"
android:resource="@drawable/launch_background"/>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />
</application>

<receiver android:name=".HomeWidgetExampleProvider">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/home_widget"/>
</receiver>

<!-- Used for Background Work -->
<receiver android:name="es.antonborri.home_widget.HomeWidgetBackgroundReceiver"
android:exported="true">
<intent-filter>
<action android:name="es.antonborri.home_widget.action.BACKGROUND"/>
</intent-filter>
</receiver>
<service android:name="es.antonborri.home_widget.HomeWidgetBackgroundService"
android:permission="android.permission.BIND_JOB_SERVICE" android:exported="true"/>

<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
android:value="2"/>
</application>
<queries>
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="https" />
<action android:name="android.intent.action.VIEW"/>
<data android:scheme="https"/>
</intent>
</queries>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.example.chimimoryo_autumn

import android.appwidget.AppWidgetManager
import android.content.Context
import android.content.SharedPreferences
import android.net.Uri
import android.widget.RemoteViews
import es.antonborri.home_widget.HomeWidgetBackgroundIntent
import es.antonborri.home_widget.HomeWidgetLaunchIntent
import es.antonborri.home_widget.HomeWidgetProvider
import com.example.chimimoryo_autumn.*

class HomeWidgetExampleProvider : HomeWidgetProvider() {

override fun onUpdate(
context: Context,
appWidgetManager: AppWidgetManager,
appWidgetIds: IntArray,
widgetData: SharedPreferences
) {
appWidgetIds.forEach { widgetId ->
val views = RemoteViews(context.packageName, R.layout.layout).apply {
// Open App on Widget Click
val pendingIntent = HomeWidgetLaunchIntent.getActivity(context, MainActivity::class.java)
setOnClickPendingIntent(R.id.widget_container, pendingIntent)

// Swap Title Text by calling Dart Code in the Background
setTextViewText(
R.id.widget_title, widgetData.getString("title", null)
?: "魑魅魍魎👊"
)
val backgroundIntent = HomeWidgetBackgroundIntent.getBroadcast(
context,
Uri.parse("homeWidgetExample://titleClicked")
)
// Change widget title
// setOnClickPendingIntent(R.id.widget_title, backgroundIntent)

val message = widgetData.getString("message", null)
setTextViewText(
R.id.widget_message, message
?: "No Message Set"
)
// Detect App opened via Click inside Flutter
val pendingIntentWithData = HomeWidgetLaunchIntent.getActivity(
context,
MainActivity::class.java,
Uri.parse("homeWidgetExample://message?message=$message")
)
setOnClickPendingIntent(R.id.widget_message, pendingIntentWithData)
}

appWidgetManager.updateAppWidget(widgetId, views)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.chimimoryo_autumn

import io.flutter.embedding.android.FlutterActivity
import com.example.chimimoryo_autumn.*

class MainActivity: FlutterActivity() {
}
5 changes: 5 additions & 0 deletions android/app/src/main/res/drawable/widget_background.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF"/>
<corners android:radius="16dp"/>
</shape>
27 changes: 27 additions & 0 deletions android/app/src/main/res/layout/layout.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"
android:orientation="vertical"
android:background="@drawable/widget_background"
android:padding="8dp"
android:id="@+id/widget_container">

<TextView
android:id="@+id/widget_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="36sp"
android:textStyle="bold"
tools:text="Title" />

<TextView
android:id="@+id/widget_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
tools:text="Message" />
</LinearLayout>
9 changes: 9 additions & 0 deletions android/app/src/main/res/xml/home_widget.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?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/layout"
android:resizeMode="horizontal|vertical"
android:widgetCategory="home_screen">
</appwidget-provider>
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.3.50'
ext.kotlin_version = '1.4.0'
repositories {
google()
mavenCentral()
Expand Down
17 changes: 17 additions & 0 deletions docs/home_widget/home_widget.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# home_widget
home widgetのための覚書。

## android

- `home_widget_helloworld.dart`は、ホームウィジェットを動かすためのexample code
- ベースとなっているコードは、公式の https://pub.dev/packages/home_widget のものです。
- ソースは https://github.com/ABausG/home_widget
- 公式のexampleに対してこの`home_widget_helloworld.dart`は、
chimimoryo内で動くように次の点が異なります
- 関連ファイルへのパス(ウィジェットの動きやスタイルを決めてるkotlinファイルなど)
- nullに対応するための`?`オペレータ等の追加
- import文やpackage宣言の細かい変更
- など

## iOS
TBA
Loading

0 comments on commit d6df3f9

Please sign in to comment.