diff options
author | nchalko <nchalko@google.com> | 2019-01-02 15:00:15 -0800 |
---|---|---|
committer | Nick Chalko <nchalko@google.com> | 2019-01-17 21:31:06 +0000 |
commit | 82d02eec8f5c46ae2e0617486c5d4ca2b6fdf715 (patch) | |
tree | 288c84ef650b91415e9c33f70dd6aeb6dd28908f /common | |
parent | fd8c11c0f69e35f132ab87e4f751226cfc8c15c7 (diff) | |
download | TV-82d02eec8f5c46ae2e0617486c5d4ca2b6fdf715.tar.gz |
Create ApplicationModule to provide Context and other android standard singletons.
PiperOrigin-RevId: 227587346
Change-Id: I9a7029dc0d291a6cab694b7ed15617dee468cda3
Diffstat (limited to 'common')
3 files changed, 98 insertions, 0 deletions
diff --git a/common/src/com/android/tv/common/dagger/ApplicationModule.java b/common/src/com/android/tv/common/dagger/ApplicationModule.java new file mode 100644 index 00000000..592717b8 --- /dev/null +++ b/common/src/com/android/tv/common/dagger/ApplicationModule.java @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.tv.common.dagger; + +import android.app.Application; +import android.content.Context; +import android.os.Looper; +import com.android.tv.common.dagger.annotations.ApplicationContext; +import com.android.tv.common.dagger.annotations.MainLooper; +import dagger.Module; +import dagger.Provides; + +/** + * Provides application-scope qualifiers for the {@link Application}, the application context, and + * the application's main looper. + */ +@Module +public final class ApplicationModule { + private final Application application; + + public ApplicationModule(Application application) { + this.application = application; + } + + @Provides + Application provideApplication() { + return application; + } + + @Provides + @ApplicationContext + Context provideContext() { + return application.getApplicationContext(); + } + + @Provides + @MainLooper + static Looper provideMainLooper() { + return Looper.getMainLooper(); + } +} diff --git a/common/src/com/android/tv/common/dagger/annotations/ApplicationContext.java b/common/src/com/android/tv/common/dagger/annotations/ApplicationContext.java new file mode 100644 index 00000000..86318156 --- /dev/null +++ b/common/src/com/android/tv/common/dagger/annotations/ApplicationContext.java @@ -0,0 +1,22 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.tv.common.dagger.annotations; + +import javax.inject.Qualifier; + +/** Annotation for requesting the application's context. */ +@Qualifier +public @interface ApplicationContext {} diff --git a/common/src/com/android/tv/common/dagger/annotations/MainLooper.java b/common/src/com/android/tv/common/dagger/annotations/MainLooper.java new file mode 100644 index 00000000..a8b4100f --- /dev/null +++ b/common/src/com/android/tv/common/dagger/annotations/MainLooper.java @@ -0,0 +1,22 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.tv.common.dagger.annotations; + +import javax.inject.Qualifier; + +/** Annotation for requesting a Looper that is on the UI thread. */ +@Qualifier +public @interface MainLooper {} |