Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Wear OS Application Development

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.
Avatar for ymnder ymnder
October 14, 2022

Introduction to Wear OS Application Development

DroidKaigi 2022 - 2022/10/05 12:10-12:50

Avatar for ymnder

ymnder

October 14, 2022
Tweet

More Decks by ymnder

Other Decks in Technology

Transcript

  1. • Ryo Yamazaki ◦ twitter: @ymnd ◦ GitHub: @ymnder •

    Application Engineer ◦ Android 日経電子版アプリ ◦ Android 紙面ビューアーアプリ • DroidKaigi co-organizer whoami
  2. • Health Servicesの実装 ◦ フィットネスや健康に関するセンサー • Watch Faceの実装 / Watch

    Face Studio ◦ 時計の盤面 • User input ◦ IMEや音声入力 • Data Layer API ◦ ペア設定したモバイルアプリとの通信 本セッションで扱わないもの
  3. • Wearアプリ ◦ Wear OS by Googleがインストールされたスマートウォッチで動作 するアプリ。正確にはWear OSアプリ •

    モバイルアプリ ◦ Androidがインストールされたスマートフォン/タブレット端末で動作す るアプリ 本セッションでの便宜的なアプリの呼び方
  4. • Wearableデバイス向けに設計されたAndroidベースのOS ◦ https://cs.android.com/android/platform/superproject/+/and roid-wear-9.0.0_r34 ◦ 現在の最新はWear OS 3 (API

    level 30) : Google I/O 2021で発表 • 接続できるモバイルのOS ◦ Wear OSのデバイスはiOSとAndroidに対応している • Wear単体での使用も可能 ◦ モバイルが近くになくても使えるように実装できる Wear OSって何?
  5. • こちらを参照 ◦ https://wearos.google.com/intl/ja_jp/#find-your-watch ◦ Galaxy watch4が代表的 • Wear OSではないデバイス

    ◦ Fitbit Sense ◦ Mi Watch ◦ wena ◦ … Wear OSがインストールされたデバイス
  6. • Wear OS 3 (api 30) ◦ 15台(Galaxy Watch4他) •

    Wear OS 2 (api 25, 26, 28) ◦ 66台(Fossil Gen 5他) Wear OSのバージョン別台数
  7. • 基本できることは同じ ◦ Tiles, Complications, Overlays, Watch Face • 今の段階では3が普及していないため2もサポートした方が良い

    • すでにWear OS 2に向けてアプリを公開している場合 ◦ Support Library から Jetpack Libraryへ移行する ▪ https://android-developers.googleblog.com/2021/09/w ear-os-jetpack-libraries-now-in-stable.html ◦ デザイン ガイドライン対応をする Wear OSのバージョン差
  8. • Android Studio Dolphinでエミュレーターの更新があった ◦ ボタンが実機に近くなった ◦ Complication, Tile, and

    Watch Faceを直接反映できる🎉 ▪ いままでは毎回自分で手動で再配置する必要があった デバッグをどう進めるか
  9. • 開発時はエミュレーターを使うのがおすすめ ◦ モバイルとペアリングできる ◦ デバッグが行いやすい ◦ シンプルなWatchFaceが提供されている ◦ Rotary

    inputなど実機にない機能検証ができる • 企画時や使用感のチェックで実機を用いていく 実機とエミュレーターの使い分け
  10. • できないこと ◦ WearアプリではURLを開けない ▪ 開いたURLはモバイルに転送される ◦ Firebase App Testerアプリはサポートされていない

    • できること ◦ Google Playアプリ経由でインストールする ▪ クローズドテストなど ◦ Android Studioでbuild & installする ◦ adbコマンドでインストールする インストールしての動作確認をどう進めるか
  11. • 時計の盤面 • ComplicationはWatch Face次第で置ける内容や個数がかわる • 表示方法 ◦ WatchFaceを長押しすると他のWatch Faceに変更できる

    • 補足 ◦ Watch Face StudioというSamsungのIDEがある ◦ 自力で実装することもできる Wear OSのUI: Watch Face
  12. • Code Samplesがすごく充実しています ◦ https://github.com/android/wear-os-samples • Overlayについて学ぶ -> ComposeStarter •

    Complicationについて学ぶ -> WearComplicationProvidersTestSuite • Tileについて学ぶ -> WearTilesKotlin • Watch Faceについて学ぶ -> WatchFaceKotlin Wear OSのUIに対応したCode Samples
  13. • 物理 ◦ 物理ボタン ◦ クラウンのロータリー入力(縦方向のスクロールと押し込みでの選択) ◦ 手首の操作 • 入力

    ◦ IME ◦ 音声入力 • 画面操作 ◦ 画面の縦スクロール ◦ 画面の横スワイプ ◦ ボタンのタップ Wear OSではどんなことができるのか(UX)
  14. • https://github.com/google/horologist ◦ AccompanistのようにComposeの機能を補うライブラリ ◦ Horologist is a group of

    libraries that aim to supplement Wear OS developers with features that are commonly required by developers but not yet available. • WearOSのUIやAnimationなどを強力にサポートしてくれる ぜひ使いたい便利なライブラリ: Horologist
  15. • 新規アプリの作成や Moduleの追加はドキュメントが詳しいです ◦ https://developer.android.com/training/wearables/get-started/creating#creating • 重要な事項 ◦ AndroidManifestに次のタグがある ▪

    <uses-feature android:name="android.hardware.type.watch" /> ◦ もし既存のモバイルアプリと一緒にリリースする場合 ▪ 同じパッケージ名を付け、同じ証明書鍵で署名する Wearアプリの開発をはじめる
  16. • androidx.wear:wear-remote-interactionsを使います • Wearアプリからモバイルを開く val remoteActivityHelper = RemoteActivityHelper( this) try

    { val intent = Intent(Intent. ACTION_VIEW) .setData(Uri.parse( "https://play.google.com/store/apps/details?id=com.nikkei.newspaper" )) .addCategory(Intent. CATEGORY_BROWSABLE) // awaitは"org.jetbrains.kotlinx:kotlinx-coroutines-android" remoteActivityHelper.startRemoteActivity(intent).await() } catch (e: RemoteActivityHelper.RemoteIntentException) { //... }
  17. • スクロール位置を表示する PositionIndicator val listState = rememberScalingLazyListState() Scaffold( positionIndicator =

    { PositionIndicator(scalingLazyListState = listState) } ) { ScalingLazyColumn( state = listState ) { //… } }
  18. • LazyColumnよりもコンテンツにフォーカスがあたる ◦ 画面の上部と下部でコンテンツが小さくなる ScalingLazyColumn val rooms = listOf("App bars",

    "Backdrop", "Cards", "Dialogs", "Online") ScalingLazyColumn { items(rooms) { room -> TitleCard( onClick = { /*TODO*/ }, time = null, title = { Text(text = room) }, content = { Text(text = "この部屋は${room}です。") } ) } }
  19. • リストではデフォルトで2つめのアイテムが中央に表示される • 1本目を表示したい場合は次のindexで0を指定する ScalingLazyColumnの表示位置 val listState = rememberScalingLazyListState(initialCenterItemIndex =

    0) Scaffold( timeText = { TimeText( modifier = Modifier. fadeAwayScalingLazyList( initialIndex = 0, ) { listState }, ) }, ) { ScalingLazyColumn( autoCentering = AutoCenteringParams( itemIndex = 0), ) { //… } }
  20. • HorologistのscrollableColumnを使う • あわせてFocusRequesterを呼び出す ScalingLazyColumnのRotary input対応 val focusRequester = remember

    { FocusRequester() } val listState = rememberScalingLazyListState(initialCenterItemIndex = 0) Scaffold( //… ) { ScalingLazyColumn( modifier = Modifier . fillMaxSize() . scrollableColumn(focusRequester , listState) , autoCentering = AutoCenteringParams( itemIndex = 0), state = listState ) { //… } } LaunchedEffect(Unit) { focusRequester.requestFocus() }
  21. • Actions ◦ Floating action buttons -> ない ◦ Segmented

    buttons -> ToggleButtonを使う • Containment ◦ Bottom sheets -> ない • Navigation ◦ Bottom app bar -> ない ◦ Navigation bar -> ない ◦ Navigation drawer -> WearableDrawerLayoutがあるが、Composeではない ◦ Navigation rail -> ない ◦ Top app bars -> ない • Selection ◦ Menus -> ない ◦ Radio Button -> ToggleChipを使う ◦ Switch -> ToggleChipを使う WearアプリとMaterial Components
  22. • まずはライブラリを入れる ◦ https://developer.android.com/jetpack/androidx/releases/ wear-watchface Complicationを作る // for Java implementation

    "androidx.wear.watchface:watchface-complications-data-source:1.1.1" // for Kotlin implementation "androidx.wear.watchface:watchface-complications-data-source-ktx:1.1.1"
  23. Complicationを作る class HelloComplicationService : SuspendingComplicationDataSourceService() { override fun getPreviewData (type:

    ComplicationType): ComplicationData? { return if (type == ComplicationType. SHORT_TEXT) { val text = PlainComplicationText.Builder( "title").build() val contentDescription = PlainComplicationText.Builder( "description").build() val builder = ShortTextComplicationData.Builder(text , contentDescription) builder.setTapAction( null).build() } else null } override suspend fun onComplicationRequest (request: ComplicationRequest): ComplicationData? { return if (request.complicationType == ComplicationType. SHORT_TEXT) { val text = PlainComplicationText.Builder( "hello").build() val contentDescription = PlainComplicationText.Builder( "world").build() val builder = ShortTextComplicationData.Builder(text , contentDescription) builder.setTapAction( null).build() } else null } }
  24. • Complicationは次のタイプが存在する ◦ SHORT_TEXT ◦ LONG_TEXT ◦ RANGED_VALUE ◦ SMALL_IMAGE

    -> 焼付き防止がある ◦ PHOTO_IMAGE ◦ MONOCHROMATIC_IMAGE Complicationを追加する
  25. • タイプに対応したクラスも存在する ◦ SHORT_TEXT: ShortTextComplicationData ◦ LONG_TEXT: LongTextComplicationData ◦ RANGED_VALUE:

    RangedValueComplicationData ◦ SMALL_IMAGE: SmallImageComplicationData ◦ PHOTO_IMAGE: PhotoImageComplicationData ◦ MONOCHROMATIC_IMAGE: MonochromaticImageComplicationData • データをセットしてWatch Faceに描画を依頼する ◦ どのように描画されるかはWatch Face次第 Complicationを追加する
  26. Complicationを作る <service android:name="com.example.wearsampleapp.presentation.HelloComplicationService" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:exported="true" android:permission="com.google.android.wearable.permission.BIND_COMPLICATION_PROVIDER" > <intent-filter> <action

    android:name="android.support.wearable.complications.ACTION_COMPLICATION_UPDATE_REQUEST" /> </intent-filter> <meta-data android:name="android.support.wearable.complications.SUPPORTED_TYPES" android:value="SHORT_TEXT" /> <meta-data android:name="android.support.wearable.complications.UPDATE_PERIOD_SECONDS" android:value="300" /> </service>
  27. Complicationを作る <service android:name="com.example.wearsampleapp.presentation.HelloComplicationService" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:exported="true" // Wear OS システムだけがプロバイダ

    サービスにバインドできるよう にする android:permission="com.google.android.wearable.permission.BIND_COMPLICATION_PROVIDER" > <intent-filter> // Wear OS システムだけがプロバイダ サービスにバインドできるようにする <action android:name="android.support.wearable.complications.ACTION_COMPLICATION_UPDATE_REQUEST" /> </intent-filter> // サポートされている表示形式を指定する <meta-data android:name="android.support.wearable.complications.SUPPORTED_TYPES" android:value="SHORT_TEXT" /> // データの更新頻度を指定する。単位は秒で、最短は 300秒、0を指定すると更新されない <meta-data android:name="android.support.wearable.complications.UPDATE_PERIOD_SECONDS" android:value="300" /> </service>
  28. • Watch Faceに配置できるためモバイルのWidgetのようにユーザーが繰り返 し使いやすい位置でコアな機能を提供できる • 一方で、配置が難しい • また、どのWatch Faceを使っているかに依存する ◦

    Complicationが置けない可能性 ▪ SHORT_TEXTやSMALL_IMAGEは比較的サポートされている ◦ 表示方法が異なる可能性 ▪ SHORT_TEXTのテキストとアイコンをどの位置に表示するかは Watch Face次第 Complicationについて
  29. • 1つのAPK/AABファイルをモバイル・Wear両方のデバイスでインストール し使うことはできない ◦ > 現時点では、Wear デバイスと Wear 以外のデバイス用に単一の APK

    を作成することはできません。 ◦ https://developer.android.com/training/wearables/packagi ng • デバイスごとのAPK(AAB)のアップロードが必要となる 1つのAPK/AABでアプリを配信できる?
  30. • 現行のドキュメント ◦ https://developer.android.com/training/wearables/packaging • 古いドキュメント ◦ https://developer.android.com/training/wearables/apps/packaging?hl =ja ◦

    Wear 1.x / Wear 2.0向けのドキュメント ◦ Codelabのコードの中にもWear 2.0向けのコードがまだある ▪ 例えばモバイルアプリのapp/build.gradleにwearAppの依存関係ルールを 定義しなくてよくなった パッケージ化の方法にアップデートがあります
  31. • たとえば審査でこういう点が指摘される ◦ ストアの掲載情報で「 Wear OS」に言及していない ◦ Wear のスクリーンショットがない ◦

    スクリーンショットに透明な背景やマスキングが含まれる ◦ 円形ディスプレイ向けにフォーマットされていない • 審査は長期化する可能性があるので余裕をもってリリーススケジュールを組みましょう • 関連情報 ◦ https://developer.android.com/training/wearables/packaging#reasons-for-rejec tion ◦ https://support.google.com/googleplay/android-developer/answer/9866151 ◦ https://developer.android.com/docs/quality-guidelines/wear-app-quality Play Consoleの審査に注意
  32. 1. WearアプリのAABもしくはAPKを用意する 2. Google Play Consoleの設定 > 詳細設定 > リリースの種類でWear

    OSを選択する 3. ストアの掲載情報を更新する a. Wear OSについて言及する b. Wear OSのスクリーンショットを含める 4. オープンテストなどでWearアプリをリリースする 5. Google Play Console の [詳細設定] から Wear OS にオプトイン Wearアプリの配信ステップ
  33. 1. WearアプリのAABもしくはAPKを用意する • 例:モバイルアプリ(1.0.0)がすでにリリースされているときにWearアプリ (1.0.0)をリリースする mobile: wear: 1_00_00 今のストア Wearをリリース

    モバイルをhotfix Wearアプリであることをprefixで表現 している 必要な方だけバージョンを上げてリ リースできる 20_01_00_00 10_01_00_00 10_01_01_00 20_01_00_00
  34. • Android Studioのスクリーンショット機能 • adb shell screencap • 端末のスクリーンショット機能 •

    paparazzi ◦ https://github.com/google/horologist/issues?q=is%3Aissue +paparazzi+is%3Aclosed 付録: スクリーンショットの方法
  35. • Android Studio ◦ LogcatのScreen Captureを押す • エミュレーターの場合 ◦ Take

    Screenshotボタンを押す • 実機の場合 ◦ Wearコンパニオンアプリの右上詳細ボタンから選択できる 付録: スクリーンショットの方法
  36. • ニュース ◦ https://android-developers-jp.googleblog.com/2021/11/compose-for-wear-os-now-in-developer.html ◦ https://android-developers-jp.googleblog.com/2021/03/creating-custom-tiles-on-wear-os-by-google-with-jetp ack-tiles-library.html ◦ https://android-developers.googleblog.com/2022/05/announcing-compose-for-wear-os-beta.html ◦

    https://blog.google/products/wear-os/wear-io21/ • Google I/O ◦ https://io.google/2022/program/7f0f4d16-b260-414d-9f2d-bd2e18ea469b/intl/ja/ • 便利なツール ◦ https://developer.android.com/distribute/marketing-tools/device-art-generator?hl=ja • 発表 ◦ https://www.droidcon.com/tag/wear-os/ ◦ https://speakerdeck.com/syarihu/droidkaigi-2018-android-wearfalsewatch-facewozuo-rou-shi-ji-falsepan-mia n-nixiao-sanaqing-bao-wotian-ete 参考資料
  37. • Wear OSの画面機能について ◦ https://developer.android.com/training/wearables/user-interfaces • Pathway ◦ https://developer.android.com/courses/pathways/wear •

    ComposeのAPI ◦ https://developer.android.com/training/wearables/compose • Overlayについて ◦ https://developer.android.com/training/wearables/overlays • Complicationについて ◦ https://developer.android.com/training/wearables/tiles/complications ◦ https://medium.com/@danybony/wear-complications-api-16ab65290aa1 • デザイン原則について ◦ https://developer.android.com/training/wearables/design/design-principles 参考資料