From e8bc37f87ff94fd82064dc6b9007e472871b9050 Mon Sep 17 00:00:00 2001 From: Charles Chen Date: Thu, 7 May 2020 15:13:24 +0800 Subject: [PATCH] Introduce @(Non)UiContext and @DisplayContext This annotations is used for marking generic Context. It will be furthur used for lint checks. Test: flash and build Bug: 151414704 Change-Id: Ic17f4f7f393590b5f8403bf978e168ea3fc3d39c --- .../android/annotation/DisplayContext.java | 60 +++++++++++++++++++ .../java/android/annotation/NonUiContext.java | 49 +++++++++++++++ core/java/android/annotation/UiContext.java | 58 ++++++++++++++++++ 3 files changed, 167 insertions(+) create mode 100644 core/java/android/annotation/DisplayContext.java create mode 100644 core/java/android/annotation/NonUiContext.java create mode 100644 core/java/android/annotation/UiContext.java diff --git a/core/java/android/annotation/DisplayContext.java b/core/java/android/annotation/DisplayContext.java new file mode 100644 index 0000000000000..e6b464ac237a2 --- /dev/null +++ b/core/java/android/annotation/DisplayContext.java @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2020 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 android.annotation; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.SOURCE; + +import android.app.Activity; +import android.app.WallpaperManager; +import android.content.Context; +import android.os.Bundle; +import android.view.Display; +import android.view.LayoutInflater; +import android.view.WindowManager; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +/** + * Denotes a {@link Context} that is tied to a {@link Display} and can be used to obtain one + * via {@link Context#getDisplay}, but may not be able to obtain {@link WindowManager}, + * {@link LayoutInflater} or {@link WallpaperManager} via {@link Context#getSystemService(String)}. + * If the UI services mentioned above are required, please use contexts which are marked as + * {@link UiContext}. + *

+ * {@link Activity}, and the result of {@link Context#createWindowContext(int, Bundle)} or + * {@link Context#createDisplayContext(Display)} can be + * used where a {@link DisplayContext} is required. + *

+ * This is a marker annotation and has no specific attributes. + * + * @see Context#getDisplay() + * @see Context#getSystemService(String) + * @see Context#getSystemService(Class) + * @see Context#createDisplayContext(Display) + * @see Context#createWindowContext(int, Bundle) + * @see UiContext + * @hide + */ +@Retention(SOURCE) +@Target({TYPE, METHOD, PARAMETER, FIELD}) +public @interface DisplayContext { +} diff --git a/core/java/android/annotation/NonUiContext.java b/core/java/android/annotation/NonUiContext.java new file mode 100644 index 0000000000000..c119ca20ba111 --- /dev/null +++ b/core/java/android/annotation/NonUiContext.java @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2020 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 android.annotation; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.SOURCE; + +import android.app.WallpaperManager; +import android.content.Context; +import android.view.Display; +import android.view.LayoutInflater; +import android.view.WindowManager; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +/** + * Denotes a {@link Context} that can not be used to obtain a {@link Display} via + * {@link Context#getDisplay} nor to obtain a {@link WindowManager}, {@link LayoutInflater} or + * {@link WallpaperManager} via {@link Context#getSystemService(String)}. + *

+ * This is a marker annotation and has no specific attributes. + * + * @see Context#getDisplay() + * @see Context#getSystemService(String) + * @see Context#getSystemService(Class) + * @hide + */ +@Retention(SOURCE) +@Target({TYPE, METHOD, PARAMETER, FIELD}) +public @interface NonUiContext { +} diff --git a/core/java/android/annotation/UiContext.java b/core/java/android/annotation/UiContext.java new file mode 100644 index 0000000000000..47becc08c2f3c --- /dev/null +++ b/core/java/android/annotation/UiContext.java @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2020 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 android.annotation; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.ElementType.PARAMETER; +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.SOURCE; + +import android.app.Activity; +import android.app.WallpaperManager; +import android.content.Context; +import android.os.Bundle; +import android.view.Display; +import android.view.LayoutInflater; +import android.view.WindowManager; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +/** + * Denotes a {@link Context} that can be used to create UI, meaning that it can provide a + * {@link Display} via {@link Context#getDisplay} and can be used to obtain a {@link WindowManager}, + * {@link LayoutInflater} or {@link WallpaperManager} via {@link Context#getSystemService(String)}. + * A {@link Context} which is marked as {@link UiContext} implies that the {@link Context} is also + * a {@link DisplayContext}. + *

+ * This kind of {@link Context} is usually an {@link Activity} or + * created via {@link Context#createWindowContext(int, Bundle)}. + *

+ * This is a marker annotation and has no specific attributes. + * + * @see Context#getDisplay() + * @see Context#getSystemService(String) + * @see Context#getSystemService(Class) + * @see Context#createWindowContext(int, Bundle) + * @see DisplayContext + * @hide + */ +@Retention(SOURCE) +@Target({TYPE, METHOD, PARAMETER, FIELD}) +public @interface UiContext { +}