Skip to content
This repository has been archived by the owner on Apr 12, 2023. It is now read-only.

Move WorkManager initialization to Application#onCreate #283

Merged
merged 4 commits into from
Jul 29, 2021

Conversation

keiji
Copy link
Collaborator

@keiji keiji commented Jul 15, 2021

Issue 番号 / Issue ID

目的 / Purpose

#136 (comment) より。

Android版の接触確認API(Google Play Services)は、システムが接触確認アプリを停止してバックグランド処理が実行されない場合に備えて「Force-stop Handling」を備えている。
Google Play Servicesは、Force-stop Handlingで6時間に1回の頻度でアプリのプロセスを再度起動して、WorkManagerのJobを復帰する。

ところが、一部の端末(some popular devices)では、Force-stopされたかを判定する処理でSecurityExceptionが発生するため、Force-stop Handlingが完了せず、Jobの復帰が行われない可能性がある。

androidx/androidx@9b77063#diff-7794a8af74a01c5c6d2ee10558482274048bd3aa011aaca99459c2c5049fd7dd

問題に対応するためWorkManagerにWorkerを登録する一連の処理を、現在のApp.xaml.csからAndroidのApplication#onCreateに移動することで、Force-stop HandlingでApplicationプロセスが起動されたタイミングでWorkerを再セットするように変更する。

破壊的変更をもたらしますか / Does this introduce a breaking change?

[x] Yes
[ ] No

Pull Request の種類 / Pull Request type

[ ] Bugfix
[x] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Documentation content changes
[ ] Other... Please describe:

検証方法 / How to test

コードの入手 / Get the code

git clone [repo-address]
cd [repo-name]
git checkout [branch-name]
dotnet restore

コードの検証 / Test the code

確認事項 / What to check

  1. BuildType Debug でビルドする(Exposure Notificationアプリとしてシステムに登録する必要があるため)
  2. Android端末でCOCOAを起動する
  3. Device File Explorerなどで、COCOAのData Directoryに timestamp.txt が作成されていることを確認する(MainApplicationが起動する度に、起動時刻のファイルが作成される)
  4. adb shell dumpsys jobschedulerで、Job SchedulerにCOCOAのJob(Worker)が登録されていることを確認する( jobscheduler-202107152120.txt
  5. adb shell am force-stop [COCOAのパッケージ名] で、COCOAを停止する
  6. adb shell dumpsys jobschedulerで、Job SchedulerにCOCOAのJob(Worker)が消えていることを確認する( jobscheduler-202107152121-forcestopped.txt
  7. 6時間程度待つ
  8. Device File Explorerなどで、COCOAのData Directoryに 新しいtimestamp.txt が作成されていることを確認する(Force-stop handlingでCOCOAのアプリケーションプロセスが再起動されたことを確認する)( Screen Shot 2021-07-16 at 10.06.20.png
  9. adb shell dumpsys jobschedulerで、Job SchedulerにCOCOAのJob(Worker)が(ふたたび)登録されていることを確認する
    jobscheduler-202107161006-forcestophandling.txt

iOS側のコードレビュー注意

ExposureNotificationの初期化処理をAppからAndroidおよびiOSのコードに移動させたため、iOS側にも影響がある。

アプリ起動時にExposureNotification.ios.csPlatformScheduleFetchが呼ばれることは確認しているが、念のためチェックが必要と考えます。

チェックしました。
起動時にPlatformScheduleFetchが呼ばれていることと、約4時間ごとにバックグラウンド処理が実行されることを確認しています(
ios_log-init_work_application_oncreate.txt )。

その他 / Other information

特になし。

Comment on lines 34 to 38
// for Debug only
string timestamp = DateTime.Now.ToString().Replace("/", "-").Replace(" ", "_");
var file = new File(DataDir, $"{timestamp}.txt");
file.CreateNewFile();

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: テスト用のコード。追加のコミットで消す

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@keiji
修正内容問題無さそうですが、こちらのデバッグコードの削除をしていただけますか。

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cocoa-dev 削除しました。

@keiji keiji marked this pull request as ready for review July 16, 2021 08:37
@keiji keiji self-assigned this Jul 16, 2021
@keiji keiji added waiting-for-confirmation 関係者に確認中のもの and removed waiting-for-confirmation 関係者に確認中のもの labels Jul 16, 2021
@kazuhiro4949
Copy link
Collaborator

using追加したので↓もnamespace削れそうに思いました

https://github.com/cocoa-mhlw/cocoa/pull/283/files#diff-66c817f2a3028fd3b857e1b8de6018d6001b53b1a3ce4596e0a4b0b1fd7e943cR86-R90

{
UseMockExposureNotificationImplementationIfNeeded();

ExposureNotification.Init();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

順番大丈夫そう

@kazuhiro4949
Copy link
Collaborator

修正と直接関係ないですが、iOSでデバッグしたときに初回起動時にExposureNotification内でBGTaskが必ずsubmitされないロジックになっているのはサンプル通りの実装なのかな??(Appleの実装の方を動かしていない)

@keiji keiji requested a review from kazuhiro4949 July 29, 2021 00:36
@keiji keiji merged commit 2e4d7b3 into cocoa-mhlw:develop Jul 29, 2021
@keiji keiji deleted the init_work_application_oncreate branch July 29, 2021 00:53
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Android] WorkManagerをApplication#onCreateで再スケジュールする
3 participants