The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.widget; |
| 18 | |
Bernardo Rufino | 3231f61 | 2019-12-20 18:04:48 +0000 | [diff] [blame] | 19 | import static com.android.internal.util.Preconditions.checkNotNull; |
Bernardo Rufino | ab338ac | 2019-12-20 18:35:30 +0000 | [diff] [blame] | 20 | import static com.android.internal.util.Preconditions.checkState; |
Bernardo Rufino | 3231f61 | 2019-12-20 18:04:48 +0000 | [diff] [blame] | 21 | |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 22 | import android.annotation.IntDef; |
Geoffrey Pitsch | d34c187 | 2017-05-04 16:02:15 -0400 | [diff] [blame] | 23 | import android.annotation.NonNull; |
| 24 | import android.annotation.Nullable; |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 25 | import android.annotation.StringRes; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import android.app.INotificationManager; |
| 27 | import android.app.ITransientNotification; |
Bernardo Rufino | ab338ac | 2019-12-20 18:35:30 +0000 | [diff] [blame] | 28 | import android.app.ITransientNotificationCallback; |
| 29 | import android.compat.Compatibility; |
| 30 | import android.compat.annotation.ChangeId; |
Bernardo Rufino | 944121a | 2020-01-29 11:55:20 +0000 | [diff] [blame] | 31 | import android.compat.annotation.EnabledAfter; |
Artur Satayev | ed5a6ae | 2019-12-10 17:47:54 +0000 | [diff] [blame] | 32 | import android.compat.annotation.UnsupportedAppUsage; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | import android.content.Context; |
Fabrice Di Meglio | 025f960 | 2012-07-30 18:53:09 -0700 | [diff] [blame] | 34 | import android.content.res.Configuration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.content.res.Resources; |
| 36 | import android.graphics.PixelFormat; |
Bernardo Rufino | 52af6db | 2019-12-20 18:26:07 +0000 | [diff] [blame] | 37 | import android.os.Binder; |
Mathew Inwood | 8c854f8 | 2018-09-14 12:35:36 +0100 | [diff] [blame] | 38 | import android.os.Build; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | import android.os.Handler; |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 40 | import android.os.IBinder; |
Geoffrey Pitsch | d34c187 | 2017-05-04 16:02:15 -0400 | [diff] [blame] | 41 | import android.os.Looper; |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 42 | import android.os.Message; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 43 | import android.os.RemoteException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | import android.os.ServiceManager; |
| 45 | import android.util.Log; |
| 46 | import android.view.Gravity; |
| 47 | import android.view.LayoutInflater; |
| 48 | import android.view.View; |
| 49 | import android.view.WindowManager; |
Svetoslav Ganov | cd2613a | 2010-10-05 15:55:39 -0700 | [diff] [blame] | 50 | import android.view.accessibility.AccessibilityEvent; |
| 51 | import android.view.accessibility.AccessibilityManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | |
Bernardo Rufino | 3231f61 | 2019-12-20 18:04:48 +0000 | [diff] [blame] | 53 | import com.android.internal.annotations.GuardedBy; |
| 54 | |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 55 | import java.lang.annotation.Retention; |
| 56 | import java.lang.annotation.RetentionPolicy; |
Bernardo Rufino | 3231f61 | 2019-12-20 18:04:48 +0000 | [diff] [blame] | 57 | import java.util.ArrayList; |
| 58 | import java.util.List; |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 59 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | /** |
| 61 | * A toast is a view containing a quick little message for the user. The toast class |
| 62 | * helps you create and show those. |
| 63 | * {@more} |
| 64 | * |
| 65 | * <p> |
| 66 | * When the view is shown to the user, appears as a floating view over the |
| 67 | * application. It will never receive focus. The user will probably be in the |
| 68 | * middle of typing something else. The idea is to be as unobtrusive as |
| 69 | * possible, while still showing the user the information you want them to see. |
| 70 | * Two examples are the volume control, and the brief message saying that your |
| 71 | * settings have been saved. |
| 72 | * <p> |
| 73 | * The easiest way to use this class is to call one of the static methods that constructs |
| 74 | * everything you need and returns a new Toast object. |
Bernardo Rufino | 7b843ab | 2020-01-17 19:15:36 +0000 | [diff] [blame] | 75 | * <p> |
| 76 | * Note that |
| 77 | * <a href="{@docRoot}reference/com/google/android/material/snackbar/Snackbar">Snackbars</a> are |
| 78 | * preferred for brief messages while the app is in the foreground. |
Joe Fernandez | 558459f | 2011-10-13 16:47:36 -0700 | [diff] [blame] | 79 | * |
| 80 | * <div class="special reference"> |
| 81 | * <h3>Developer Guides</h3> |
| 82 | * <p>For information about creating Toast notifications, read the |
| 83 | * <a href="{@docRoot}guide/topics/ui/notifiers/toasts.html">Toast Notifications</a> developer |
| 84 | * guide.</p> |
| 85 | * </div> |
Geoffrey Pitsch | d34c187 | 2017-05-04 16:02:15 -0400 | [diff] [blame] | 86 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | public class Toast { |
| 88 | static final String TAG = "Toast"; |
| 89 | static final boolean localLOGV = false; |
| 90 | |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 91 | /** @hide */ |
Jeff Sharkey | ce8db99 | 2017-12-13 20:05:05 -0700 | [diff] [blame] | 92 | @IntDef(prefix = { "LENGTH_" }, value = { |
| 93 | LENGTH_SHORT, |
| 94 | LENGTH_LONG |
| 95 | }) |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 96 | @Retention(RetentionPolicy.SOURCE) |
| 97 | public @interface Duration {} |
| 98 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | /** |
| 100 | * Show the view or text notification for a short period of time. This time |
| 101 | * could be user-definable. This is the default. |
| 102 | * @see #setDuration |
| 103 | */ |
| 104 | public static final int LENGTH_SHORT = 0; |
| 105 | |
| 106 | /** |
| 107 | * Show the view or text notification for a long period of time. This time |
| 108 | * could be user-definable. |
| 109 | * @see #setDuration |
| 110 | */ |
| 111 | public static final int LENGTH_LONG = 1; |
| 112 | |
Bernardo Rufino | ab338ac | 2019-12-20 18:35:30 +0000 | [diff] [blame] | 113 | /** |
| 114 | * Text toasts will be rendered by SystemUI instead of in-app, so apps can't circumvent |
| 115 | * background custom toast restrictions. |
Bernardo Rufino | ab338ac | 2019-12-20 18:35:30 +0000 | [diff] [blame] | 116 | */ |
| 117 | @ChangeId |
Bernardo Rufino | 944121a | 2020-01-29 11:55:20 +0000 | [diff] [blame] | 118 | @EnabledAfter(targetSdkVersion = Build.VERSION_CODES.Q) |
Bernardo Rufino | ab338ac | 2019-12-20 18:35:30 +0000 | [diff] [blame] | 119 | private static final long CHANGE_TEXT_TOASTS_IN_THE_SYSTEM = 147798919L; |
| 120 | |
| 121 | |
Bernardo Rufino | 52af6db | 2019-12-20 18:26:07 +0000 | [diff] [blame] | 122 | private final Binder mToken; |
| 123 | private final Context mContext; |
Bernardo Rufino | ab338ac | 2019-12-20 18:35:30 +0000 | [diff] [blame] | 124 | private final Handler mHandler; |
Daniel Santiago Rivera | e242907 | 2019-02-04 15:19:14 -0800 | [diff] [blame] | 125 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | final TN mTN; |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 127 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | int mDuration; |
Bernardo Rufino | ab338ac | 2019-12-20 18:35:30 +0000 | [diff] [blame] | 129 | |
| 130 | /** |
| 131 | * This is also passed to {@link TN} object, where it's also accessed with itself as its own |
| 132 | * lock. |
| 133 | */ |
| 134 | @GuardedBy("mCallbacks") |
| 135 | private final List<Callback> mCallbacks; |
| 136 | |
| 137 | /** |
| 138 | * View to be displayed, in case this is a custom toast (e.g. not created with {@link |
| 139 | * #makeText(Context, int, int)} or its variants). |
| 140 | */ |
| 141 | @Nullable |
| 142 | private View mNextView; |
| 143 | |
| 144 | /** |
| 145 | * Text to be shown, in case this is NOT a custom toast (e.g. created with {@link |
| 146 | * #makeText(Context, int, int)} or its variants). |
| 147 | */ |
| 148 | @Nullable |
| 149 | private CharSequence mText; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | |
| 151 | /** |
| 152 | * Construct an empty Toast object. You must call {@link #setView} before you |
| 153 | * can call {@link #show}. |
| 154 | * |
| 155 | * @param context The context to use. Usually your {@link android.app.Application} |
| 156 | * or {@link android.app.Activity} object. |
| 157 | */ |
| 158 | public Toast(Context context) { |
Geoffrey Pitsch | d34c187 | 2017-05-04 16:02:15 -0400 | [diff] [blame] | 159 | this(context, null); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Constructs an empty Toast object. If looper is null, Looper.myLooper() is used. |
| 164 | * @hide |
| 165 | */ |
| 166 | public Toast(@NonNull Context context, @Nullable Looper looper) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | mContext = context; |
Bernardo Rufino | 52af6db | 2019-12-20 18:26:07 +0000 | [diff] [blame] | 168 | mToken = new Binder(); |
Bernardo Rufino | ab338ac | 2019-12-20 18:35:30 +0000 | [diff] [blame] | 169 | looper = getLooper(looper); |
| 170 | mHandler = new Handler(looper); |
| 171 | mCallbacks = new ArrayList<>(); |
| 172 | mTN = new TN(context.getPackageName(), mToken, mCallbacks, looper); |
Romain Guy | cf3dd6b | 2011-01-19 16:54:13 -0800 | [diff] [blame] | 173 | mTN.mY = context.getResources().getDimensionPixelSize( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | com.android.internal.R.dimen.toast_y_offset); |
Jose Lima | 08bef37 | 2013-08-08 11:51:00 -0700 | [diff] [blame] | 175 | mTN.mGravity = context.getResources().getInteger( |
| 176 | com.android.internal.R.integer.config_toastDefaultGravity); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | } |
Robert Carr | 6cfc4e3 | 2016-10-05 15:33:16 -0700 | [diff] [blame] | 178 | |
Bernardo Rufino | ab338ac | 2019-12-20 18:35:30 +0000 | [diff] [blame] | 179 | private Looper getLooper(@Nullable Looper looper) { |
| 180 | if (looper != null) { |
| 181 | return looper; |
| 182 | } |
| 183 | return checkNotNull(Looper.myLooper(), |
| 184 | "Can't toast on a thread that has not called Looper.prepare()"); |
| 185 | } |
| 186 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | /** |
| 188 | * Show the view for the specified duration. |
| 189 | */ |
| 190 | public void show() { |
Bernardo Rufino | ab338ac | 2019-12-20 18:35:30 +0000 | [diff] [blame] | 191 | if (Compatibility.isChangeEnabled(CHANGE_TEXT_TOASTS_IN_THE_SYSTEM)) { |
| 192 | checkState(mNextView != null || mText != null, "You must either set a text or a view"); |
| 193 | } else { |
| 194 | if (mNextView == null) { |
| 195 | throw new RuntimeException("setView must have been called"); |
| 196 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | INotificationManager service = getService(); |
Tony Hill | 929442d | 2014-09-16 11:27:35 +0100 | [diff] [blame] | 200 | String pkg = mContext.getOpPackageName(); |
Romain Guy | cf3dd6b | 2011-01-19 16:54:13 -0800 | [diff] [blame] | 201 | TN tn = mTN; |
| 202 | tn.mNextView = mNextView; |
Yohei Yukawa | 5281b6b | 2018-10-15 07:38:25 +0800 | [diff] [blame] | 203 | final int displayId = mContext.getDisplayId(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 205 | try { |
Bernardo Rufino | ab338ac | 2019-12-20 18:35:30 +0000 | [diff] [blame] | 206 | if (Compatibility.isChangeEnabled(CHANGE_TEXT_TOASTS_IN_THE_SYSTEM)) { |
| 207 | if (mNextView != null) { |
| 208 | // It's a custom toast |
| 209 | service.enqueueToast(pkg, mToken, tn, mDuration, displayId); |
| 210 | } else { |
| 211 | // It's a text toast |
| 212 | ITransientNotificationCallback callback = |
| 213 | new CallbackBinder(mCallbacks, mHandler); |
| 214 | service.enqueueTextToast(pkg, mToken, mText, mDuration, displayId, callback); |
| 215 | } |
Bernardo Rufino | 60c39b3 | 2019-11-08 16:11:15 +0000 | [diff] [blame] | 216 | } else { |
Bernardo Rufino | ab338ac | 2019-12-20 18:35:30 +0000 | [diff] [blame] | 217 | service.enqueueToast(pkg, mToken, tn, mDuration, displayId); |
Bernardo Rufino | 60c39b3 | 2019-11-08 16:11:15 +0000 | [diff] [blame] | 218 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | } catch (RemoteException e) { |
| 220 | // Empty |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Close the view if it's showing, or don't show it if it isn't showing yet. |
| 226 | * You do not normally have to call this. Normally view will disappear on its own |
| 227 | * after the appropriate duration. |
| 228 | */ |
| 229 | public void cancel() { |
Bernardo Rufino | ab338ac | 2019-12-20 18:35:30 +0000 | [diff] [blame] | 230 | if (Compatibility.isChangeEnabled(CHANGE_TEXT_TOASTS_IN_THE_SYSTEM) |
| 231 | && mNextView == null) { |
| 232 | try { |
| 233 | getService().cancelToast(mContext.getOpPackageName(), mToken); |
| 234 | } catch (RemoteException e) { |
| 235 | // Empty |
| 236 | } |
| 237 | } else { |
| 238 | mTN.cancel(); |
| 239 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | } |
Robert Carr | 6cfc4e3 | 2016-10-05 15:33:16 -0700 | [diff] [blame] | 241 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 242 | /** |
| 243 | * Set the view to show. |
Bernardo Rufino | 7b843ab | 2020-01-17 19:15:36 +0000 | [diff] [blame] | 244 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 245 | * @see #getView |
Bernardo Rufino | 7b843ab | 2020-01-17 19:15:36 +0000 | [diff] [blame] | 246 | * @deprecated Custom toast views are deprecated. Apps can create a standard text toast with the |
| 247 | * {@link #makeText(Context, CharSequence, int)} method, or use a |
| 248 | * <a href="{@docRoot}reference/com/google/android/material/snackbar/Snackbar">Snackbar</a> |
| 249 | * when in the foreground. Starting from Android {@link Build.VERSION_CODES#R}, apps |
| 250 | * targeting API level {@link Build.VERSION_CODES#R} or higher that are in the background |
| 251 | * will not have custom toast views displayed. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | */ |
Bernardo Rufino | 7b843ab | 2020-01-17 19:15:36 +0000 | [diff] [blame] | 253 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 254 | public void setView(View view) { |
| 255 | mNextView = view; |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Return the view. |
Bernardo Rufino | 7b843ab | 2020-01-17 19:15:36 +0000 | [diff] [blame] | 260 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 261 | * @see #setView |
Bernardo Rufino | 7b843ab | 2020-01-17 19:15:36 +0000 | [diff] [blame] | 262 | * @deprecated Custom toast views are deprecated. Apps can create a standard text toast with the |
| 263 | * {@link #makeText(Context, CharSequence, int)} method, or use a |
| 264 | * <a href="{@docRoot}reference/com/google/android/material/snackbar/Snackbar">Snackbar</a> |
| 265 | * when in the foreground. Starting from Android {@link Build.VERSION_CODES#R}, apps |
| 266 | * targeting API level {@link Build.VERSION_CODES#R} or higher that are in the background |
| 267 | * will not have custom toast views displayed. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 268 | */ |
| 269 | public View getView() { |
| 270 | return mNextView; |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Set how long to show the view for. |
| 275 | * @see #LENGTH_SHORT |
| 276 | * @see #LENGTH_LONG |
| 277 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 278 | public void setDuration(@Duration int duration) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 279 | mDuration = duration; |
Robert Carr | 70f0d22 | 2016-04-10 16:33:08 -0700 | [diff] [blame] | 280 | mTN.mDuration = duration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Return the duration. |
| 285 | * @see #setDuration |
| 286 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 287 | @Duration |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 288 | public int getDuration() { |
| 289 | return mDuration; |
| 290 | } |
Geoffrey Pitsch | d34c187 | 2017-05-04 16:02:15 -0400 | [diff] [blame] | 291 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 292 | /** |
| 293 | * Set the margins of the view. |
| 294 | * |
| 295 | * @param horizontalMargin The horizontal margin, in percentage of the |
| 296 | * container width, between the container's edges and the |
| 297 | * notification |
| 298 | * @param verticalMargin The vertical margin, in percentage of the |
| 299 | * container height, between the container's edges and the |
| 300 | * notification |
| 301 | */ |
| 302 | public void setMargin(float horizontalMargin, float verticalMargin) { |
Romain Guy | cf3dd6b | 2011-01-19 16:54:13 -0800 | [diff] [blame] | 303 | mTN.mHorizontalMargin = horizontalMargin; |
| 304 | mTN.mVerticalMargin = verticalMargin; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Return the horizontal margin. |
| 309 | */ |
| 310 | public float getHorizontalMargin() { |
Romain Guy | cf3dd6b | 2011-01-19 16:54:13 -0800 | [diff] [blame] | 311 | return mTN.mHorizontalMargin; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | /** |
| 315 | * Return the vertical margin. |
| 316 | */ |
| 317 | public float getVerticalMargin() { |
Romain Guy | cf3dd6b | 2011-01-19 16:54:13 -0800 | [diff] [blame] | 318 | return mTN.mVerticalMargin; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Set the location at which the notification should appear on the screen. |
| 323 | * @see android.view.Gravity |
| 324 | * @see #getGravity |
| 325 | */ |
| 326 | public void setGravity(int gravity, int xOffset, int yOffset) { |
Romain Guy | cf3dd6b | 2011-01-19 16:54:13 -0800 | [diff] [blame] | 327 | mTN.mGravity = gravity; |
| 328 | mTN.mX = xOffset; |
| 329 | mTN.mY = yOffset; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | /** |
| 333 | * Get the location at which the notification should appear on the screen. |
| 334 | * @see android.view.Gravity |
| 335 | * @see #getGravity |
| 336 | */ |
| 337 | public int getGravity() { |
Romain Guy | cf3dd6b | 2011-01-19 16:54:13 -0800 | [diff] [blame] | 338 | return mTN.mGravity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Return the X offset in pixels to apply to the gravity's location. |
| 343 | */ |
| 344 | public int getXOffset() { |
Romain Guy | cf3dd6b | 2011-01-19 16:54:13 -0800 | [diff] [blame] | 345 | return mTN.mX; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 346 | } |
Geoffrey Pitsch | d34c187 | 2017-05-04 16:02:15 -0400 | [diff] [blame] | 347 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 348 | /** |
| 349 | * Return the Y offset in pixels to apply to the gravity's location. |
| 350 | */ |
| 351 | public int getYOffset() { |
Romain Guy | cf3dd6b | 2011-01-19 16:54:13 -0800 | [diff] [blame] | 352 | return mTN.mY; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 353 | } |
Jason Monk | a585268 | 2014-10-23 11:48:36 -0400 | [diff] [blame] | 354 | |
| 355 | /** |
Bernardo Rufino | 3231f61 | 2019-12-20 18:04:48 +0000 | [diff] [blame] | 356 | * Adds a callback to be notified when the toast is shown or hidden. |
| 357 | * |
| 358 | * Note that if the toast is blocked for some reason you won't get a call back. |
| 359 | * |
| 360 | * @see #removeCallback(Callback) |
| 361 | */ |
| 362 | public void addCallback(@NonNull Callback callback) { |
| 363 | checkNotNull(callback); |
Bernardo Rufino | ab338ac | 2019-12-20 18:35:30 +0000 | [diff] [blame] | 364 | synchronized (mCallbacks) { |
| 365 | mCallbacks.add(callback); |
Bernardo Rufino | 3231f61 | 2019-12-20 18:04:48 +0000 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * Removes a callback previously added with {@link #addCallback(Callback)}. |
| 371 | */ |
| 372 | public void removeCallback(@NonNull Callback callback) { |
Bernardo Rufino | ab338ac | 2019-12-20 18:35:30 +0000 | [diff] [blame] | 373 | synchronized (mCallbacks) { |
| 374 | mCallbacks.remove(callback); |
Bernardo Rufino | 3231f61 | 2019-12-20 18:04:48 +0000 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | |
| 378 | /** |
Jason Monk | a585268 | 2014-10-23 11:48:36 -0400 | [diff] [blame] | 379 | * Gets the LayoutParams for the Toast window. |
| 380 | * @hide |
| 381 | */ |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 382 | @UnsupportedAppUsage |
Jason Monk | a585268 | 2014-10-23 11:48:36 -0400 | [diff] [blame] | 383 | public WindowManager.LayoutParams getWindowParams() { |
| 384 | return mTN.mParams; |
| 385 | } |
Geoffrey Pitsch | d34c187 | 2017-05-04 16:02:15 -0400 | [diff] [blame] | 386 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 387 | /** |
| 388 | * Make a standard toast that just contains a text view. |
| 389 | * |
| 390 | * @param context The context to use. Usually your {@link android.app.Application} |
| 391 | * or {@link android.app.Activity} object. |
| 392 | * @param text The text to show. Can be formatted text. |
| 393 | * @param duration How long to display the message. Either {@link #LENGTH_SHORT} or |
| 394 | * {@link #LENGTH_LONG} |
| 395 | * |
| 396 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 397 | public static Toast makeText(Context context, CharSequence text, @Duration int duration) { |
Geoffrey Pitsch | d34c187 | 2017-05-04 16:02:15 -0400 | [diff] [blame] | 398 | return makeText(context, null, text, duration); |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * Make a standard toast to display using the specified looper. |
| 403 | * If looper is null, Looper.myLooper() is used. |
Bernardo Rufino | ab338ac | 2019-12-20 18:35:30 +0000 | [diff] [blame] | 404 | * |
Geoffrey Pitsch | d34c187 | 2017-05-04 16:02:15 -0400 | [diff] [blame] | 405 | * @hide |
| 406 | */ |
| 407 | public static Toast makeText(@NonNull Context context, @Nullable Looper looper, |
| 408 | @NonNull CharSequence text, @Duration int duration) { |
Bernardo Rufino | ab338ac | 2019-12-20 18:35:30 +0000 | [diff] [blame] | 409 | if (Compatibility.isChangeEnabled(CHANGE_TEXT_TOASTS_IN_THE_SYSTEM)) { |
| 410 | Toast result = new Toast(context, looper); |
| 411 | result.mText = text; |
| 412 | result.mDuration = duration; |
| 413 | return result; |
| 414 | } else { |
| 415 | Toast result = new Toast(context, looper); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 416 | |
Bernardo Rufino | ab338ac | 2019-12-20 18:35:30 +0000 | [diff] [blame] | 417 | LayoutInflater inflate = (LayoutInflater) |
| 418 | context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 419 | View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null); |
| 420 | TextView tv = (TextView) v.findViewById(com.android.internal.R.id.message); |
| 421 | tv.setText(text); |
Geoffrey Pitsch | d34c187 | 2017-05-04 16:02:15 -0400 | [diff] [blame] | 422 | |
Bernardo Rufino | ab338ac | 2019-12-20 18:35:30 +0000 | [diff] [blame] | 423 | result.mNextView = v; |
| 424 | result.mDuration = duration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 425 | |
Bernardo Rufino | ab338ac | 2019-12-20 18:35:30 +0000 | [diff] [blame] | 426 | return result; |
| 427 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | /** |
| 431 | * Make a standard toast that just contains a text view with the text from a resource. |
| 432 | * |
| 433 | * @param context The context to use. Usually your {@link android.app.Application} |
| 434 | * or {@link android.app.Activity} object. |
| 435 | * @param resId The resource id of the string resource to use. Can be formatted text. |
| 436 | * @param duration How long to display the message. Either {@link #LENGTH_SHORT} or |
| 437 | * {@link #LENGTH_LONG} |
| 438 | * |
| 439 | * @throws Resources.NotFoundException if the resource can't be found. |
| 440 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 441 | public static Toast makeText(Context context, @StringRes int resId, @Duration int duration) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 442 | throws Resources.NotFoundException { |
| 443 | return makeText(context, context.getResources().getText(resId), duration); |
| 444 | } |
| 445 | |
| 446 | /** |
| 447 | * Update the text in a Toast that was previously created using one of the makeText() methods. |
| 448 | * @param resId The new text for the Toast. |
| 449 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 450 | public void setText(@StringRes int resId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 451 | setText(mContext.getText(resId)); |
| 452 | } |
Geoffrey Pitsch | d34c187 | 2017-05-04 16:02:15 -0400 | [diff] [blame] | 453 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 454 | /** |
| 455 | * Update the text in a Toast that was previously created using one of the makeText() methods. |
| 456 | * @param s The new text for the Toast. |
| 457 | */ |
| 458 | public void setText(CharSequence s) { |
Bernardo Rufino | ab338ac | 2019-12-20 18:35:30 +0000 | [diff] [blame] | 459 | if (Compatibility.isChangeEnabled(CHANGE_TEXT_TOASTS_IN_THE_SYSTEM)) { |
| 460 | if (mNextView != null) { |
| 461 | throw new IllegalStateException( |
| 462 | "Text provided for custom toast, remove previous setView() calls if you " |
| 463 | + "want a text toast instead."); |
| 464 | } |
| 465 | mText = s; |
| 466 | } else { |
| 467 | if (mNextView == null) { |
| 468 | throw new RuntimeException("This Toast was not created with Toast.makeText()"); |
| 469 | } |
| 470 | TextView tv = mNextView.findViewById(com.android.internal.R.id.message); |
| 471 | if (tv == null) { |
| 472 | throw new RuntimeException("This Toast was not created with Toast.makeText()"); |
| 473 | } |
| 474 | tv.setText(s); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 475 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 476 | } |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 477 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 478 | // ======================================================================================= |
| 479 | // All the gunk below is the interaction with the Notification Service, which handles |
| 480 | // the proper ordering of these system-wide. |
| 481 | // ======================================================================================= |
| 482 | |
Adam Powell | c7ac9f0 | 2019-02-04 13:29:11 -0800 | [diff] [blame] | 483 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 484 | private static INotificationManager sService; |
| 485 | |
Adam Powell | c7ac9f0 | 2019-02-04 13:29:11 -0800 | [diff] [blame] | 486 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
Romain Guy | 0420cc7 | 2009-07-08 11:59:00 -0700 | [diff] [blame] | 487 | static private INotificationManager getService() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 488 | if (sService != null) { |
| 489 | return sService; |
| 490 | } |
| 491 | sService = INotificationManager.Stub.asInterface(ServiceManager.getService("notification")); |
| 492 | return sService; |
| 493 | } |
| 494 | |
Romain Guy | c4d09f2 | 2011-01-19 10:55:19 -0800 | [diff] [blame] | 495 | private static class TN extends ITransientNotification.Stub { |
Daniel Santiago Rivera | e242907 | 2019-02-04 15:19:14 -0800 | [diff] [blame] | 496 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
Romain Guy | 0420cc7 | 2009-07-08 11:59:00 -0700 | [diff] [blame] | 497 | private final WindowManager.LayoutParams mParams = new WindowManager.LayoutParams(); |
Robert Carr | 6cfc4e3 | 2016-10-05 15:33:16 -0700 | [diff] [blame] | 498 | |
| 499 | private static final int SHOW = 0; |
| 500 | private static final int HIDE = 1; |
| 501 | private static final int CANCEL = 2; |
Geoffrey Pitsch | d34c187 | 2017-05-04 16:02:15 -0400 | [diff] [blame] | 502 | final Handler mHandler; |
Romain Guy | cf3dd6b | 2011-01-19 16:54:13 -0800 | [diff] [blame] | 503 | |
Daniel Santiago Rivera | e242907 | 2019-02-04 15:19:14 -0800 | [diff] [blame] | 504 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
Jose Lima | 08bef37 | 2013-08-08 11:51:00 -0700 | [diff] [blame] | 505 | int mGravity; |
Mathew Inwood | 74e7aed | 2018-08-16 17:29:31 +0100 | [diff] [blame] | 506 | int mX; |
Daniel Santiago Rivera | e242907 | 2019-02-04 15:19:14 -0800 | [diff] [blame] | 507 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
Mathew Inwood | 74e7aed | 2018-08-16 17:29:31 +0100 | [diff] [blame] | 508 | int mY; |
Romain Guy | cf3dd6b | 2011-01-19 16:54:13 -0800 | [diff] [blame] | 509 | float mHorizontalMargin; |
| 510 | float mVerticalMargin; |
| 511 | |
Jose Lima | 08bef37 | 2013-08-08 11:51:00 -0700 | [diff] [blame] | 512 | |
Daniel Santiago Rivera | e242907 | 2019-02-04 15:19:14 -0800 | [diff] [blame] | 513 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
Romain Guy | cf3dd6b | 2011-01-19 16:54:13 -0800 | [diff] [blame] | 514 | View mView; |
Daniel Santiago Rivera | e242907 | 2019-02-04 15:19:14 -0800 | [diff] [blame] | 515 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
Romain Guy | cf3dd6b | 2011-01-19 16:54:13 -0800 | [diff] [blame] | 516 | View mNextView; |
Robert Carr | 70f0d22 | 2016-04-10 16:33:08 -0700 | [diff] [blame] | 517 | int mDuration; |
Craig Mautner | b6ee2a2 | 2012-08-27 16:55:04 -0700 | [diff] [blame] | 518 | |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 519 | WindowManager mWM; |
Romain Guy | 0420cc7 | 2009-07-08 11:59:00 -0700 | [diff] [blame] | 520 | |
Bernardo Rufino | 52af6db | 2019-12-20 18:26:07 +0000 | [diff] [blame] | 521 | final String mPackageName; |
| 522 | final Binder mToken; |
Robert Carr | 6cfc4e3 | 2016-10-05 15:33:16 -0700 | [diff] [blame] | 523 | |
Bernardo Rufino | 3231f61 | 2019-12-20 18:04:48 +0000 | [diff] [blame] | 524 | @GuardedBy("mCallbacks") |
Bernardo Rufino | ab338ac | 2019-12-20 18:35:30 +0000 | [diff] [blame] | 525 | private final List<Callback> mCallbacks; |
Bernardo Rufino | 3231f61 | 2019-12-20 18:04:48 +0000 | [diff] [blame] | 526 | |
Robert Carr | 017dbf5 | 2016-07-21 18:16:00 -0700 | [diff] [blame] | 527 | static final long SHORT_DURATION_TIMEOUT = 4000; |
| 528 | static final long LONG_DURATION_TIMEOUT = 7000; |
Robert Carr | 70f0d22 | 2016-04-10 16:33:08 -0700 | [diff] [blame] | 529 | |
Bernardo Rufino | ab338ac | 2019-12-20 18:35:30 +0000 | [diff] [blame] | 530 | /** |
| 531 | * Creates a {@link ITransientNotification} object. |
| 532 | * |
| 533 | * The parameter {@code callbacks} is not copied and is accessed with itself as its own |
| 534 | * lock. |
| 535 | */ |
| 536 | TN(String packageName, Binder token, List<Callback> callbacks, @Nullable Looper looper) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 537 | // XXX This should be changed to use a Dialog, with a Theme.Toast |
| 538 | // defined that sets up the layout params appropriately. |
Romain Guy | 0420cc7 | 2009-07-08 11:59:00 -0700 | [diff] [blame] | 539 | final WindowManager.LayoutParams params = mParams; |
| 540 | params.height = WindowManager.LayoutParams.WRAP_CONTENT; |
| 541 | params.width = WindowManager.LayoutParams.WRAP_CONTENT; |
Romain Guy | 0420cc7 | 2009-07-08 11:59:00 -0700 | [diff] [blame] | 542 | params.format = PixelFormat.TRANSLUCENT; |
| 543 | params.windowAnimations = com.android.internal.R.style.Animation_Toast; |
| 544 | params.type = WindowManager.LayoutParams.TYPE_TOAST; |
Tiger Huang | 5272444 | 2020-01-20 21:38:42 +0800 | [diff] [blame] | 545 | params.setFitInsetsIgnoringVisibility(true); |
Romain Guy | 0420cc7 | 2009-07-08 11:59:00 -0700 | [diff] [blame] | 546 | params.setTitle("Toast"); |
John Spurlock | f1a3664 | 2013-10-12 17:50:42 -0400 | [diff] [blame] | 547 | params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
John Spurlock | dcf4f21 | 2013-05-21 17:19:53 -0400 | [diff] [blame] | 548 | | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
John Spurlock | f1a3664 | 2013-10-12 17:50:42 -0400 | [diff] [blame] | 549 | | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE; |
Robert Carr | 6cfc4e3 | 2016-10-05 15:33:16 -0700 | [diff] [blame] | 550 | |
| 551 | mPackageName = packageName; |
Bernardo Rufino | 52af6db | 2019-12-20 18:26:07 +0000 | [diff] [blame] | 552 | mToken = token; |
Bernardo Rufino | ab338ac | 2019-12-20 18:35:30 +0000 | [diff] [blame] | 553 | mCallbacks = callbacks; |
Geoffrey Pitsch | d34c187 | 2017-05-04 16:02:15 -0400 | [diff] [blame] | 554 | |
Geoffrey Pitsch | d34c187 | 2017-05-04 16:02:15 -0400 | [diff] [blame] | 555 | mHandler = new Handler(looper, null) { |
| 556 | @Override |
| 557 | public void handleMessage(Message msg) { |
| 558 | switch (msg.what) { |
| 559 | case SHOW: { |
| 560 | IBinder token = (IBinder) msg.obj; |
| 561 | handleShow(token); |
| 562 | break; |
| 563 | } |
| 564 | case HIDE: { |
| 565 | handleHide(); |
| 566 | // Don't do this in handleHide() because it is also invoked by |
| 567 | // handleShow() |
| 568 | mNextView = null; |
| 569 | break; |
| 570 | } |
| 571 | case CANCEL: { |
| 572 | handleHide(); |
| 573 | // Don't do this in handleHide() because it is also invoked by |
| 574 | // handleShow() |
| 575 | mNextView = null; |
| 576 | try { |
Bernardo Rufino | 52af6db | 2019-12-20 18:26:07 +0000 | [diff] [blame] | 577 | getService().cancelToast(mPackageName, mToken); |
Geoffrey Pitsch | d34c187 | 2017-05-04 16:02:15 -0400 | [diff] [blame] | 578 | } catch (RemoteException e) { |
| 579 | } |
| 580 | break; |
| 581 | } |
| 582 | } |
| 583 | } |
| 584 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 585 | } |
| 586 | |
Bernardo Rufino | 3231f61 | 2019-12-20 18:04:48 +0000 | [diff] [blame] | 587 | private List<Callback> getCallbacks() { |
| 588 | synchronized (mCallbacks) { |
| 589 | return new ArrayList<>(mCallbacks); |
| 590 | } |
| 591 | } |
| 592 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 593 | /** |
| 594 | * schedule handleShow into the right thread |
| 595 | */ |
Craig Mautner | b6ee2a2 | 2012-08-27 16:55:04 -0700 | [diff] [blame] | 596 | @Override |
Mathew Inwood | 8c854f8 | 2018-09-14 12:35:36 +0100 | [diff] [blame] | 597 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 598 | public void show(IBinder windowToken) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 599 | if (localLOGV) Log.v(TAG, "SHOW: " + this); |
Robert Carr | 6cfc4e3 | 2016-10-05 15:33:16 -0700 | [diff] [blame] | 600 | mHandler.obtainMessage(SHOW, windowToken).sendToTarget(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | /** |
| 604 | * schedule handleHide into the right thread |
| 605 | */ |
Craig Mautner | b6ee2a2 | 2012-08-27 16:55:04 -0700 | [diff] [blame] | 606 | @Override |
Romain Guy | 0420cc7 | 2009-07-08 11:59:00 -0700 | [diff] [blame] | 607 | public void hide() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 608 | if (localLOGV) Log.v(TAG, "HIDE: " + this); |
Robert Carr | 6cfc4e3 | 2016-10-05 15:33:16 -0700 | [diff] [blame] | 609 | mHandler.obtainMessage(HIDE).sendToTarget(); |
| 610 | } |
| 611 | |
| 612 | public void cancel() { |
| 613 | if (localLOGV) Log.v(TAG, "CANCEL: " + this); |
| 614 | mHandler.obtainMessage(CANCEL).sendToTarget(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 615 | } |
| 616 | |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 617 | public void handleShow(IBinder windowToken) { |
Romain Guy | cf3dd6b | 2011-01-19 16:54:13 -0800 | [diff] [blame] | 618 | if (localLOGV) Log.v(TAG, "HANDLE SHOW: " + this + " mView=" + mView |
| 619 | + " mNextView=" + mNextView); |
Svet Ganov | 0df3702 | 2017-05-15 23:45:03 -0700 | [diff] [blame] | 620 | // If a cancel/hide is pending - no need to show - at this point |
| 621 | // the window token is already invalid and no need to do any work. |
| 622 | if (mHandler.hasMessages(CANCEL) || mHandler.hasMessages(HIDE)) { |
| 623 | return; |
| 624 | } |
Romain Guy | cf3dd6b | 2011-01-19 16:54:13 -0800 | [diff] [blame] | 625 | if (mView != mNextView) { |
| 626 | // remove the old view if necessary |
| 627 | handleHide(); |
| 628 | mView = mNextView; |
Craig Mautner | 54ae2f3 | 2012-12-05 09:49:57 -0800 | [diff] [blame] | 629 | Context context = mView.getContext().getApplicationContext(); |
Tony Hill | 929442d | 2014-09-16 11:27:35 +0100 | [diff] [blame] | 630 | String packageName = mView.getContext().getOpPackageName(); |
Craig Mautner | 54ae2f3 | 2012-12-05 09:49:57 -0800 | [diff] [blame] | 631 | if (context == null) { |
| 632 | context = mView.getContext(); |
| 633 | } |
| 634 | mWM = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE); |
Fabrice Di Meglio | b494634 | 2012-06-07 16:29:18 -0700 | [diff] [blame] | 635 | // We can resolve the Gravity here by using the Locale for getting |
| 636 | // the layout direction |
Fabrice Di Meglio | 025f960 | 2012-07-30 18:53:09 -0700 | [diff] [blame] | 637 | final Configuration config = mView.getContext().getResources().getConfiguration(); |
Fabrice Di Meglio | 5f79799 | 2012-06-15 20:16:41 -0700 | [diff] [blame] | 638 | final int gravity = Gravity.getAbsoluteGravity(mGravity, config.getLayoutDirection()); |
Romain Guy | cf3dd6b | 2011-01-19 16:54:13 -0800 | [diff] [blame] | 639 | mParams.gravity = gravity; |
| 640 | if ((gravity & Gravity.HORIZONTAL_GRAVITY_MASK) == Gravity.FILL_HORIZONTAL) { |
| 641 | mParams.horizontalWeight = 1.0f; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 642 | } |
Romain Guy | cf3dd6b | 2011-01-19 16:54:13 -0800 | [diff] [blame] | 643 | if ((gravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.FILL_VERTICAL) { |
| 644 | mParams.verticalWeight = 1.0f; |
| 645 | } |
| 646 | mParams.x = mX; |
| 647 | mParams.y = mY; |
| 648 | mParams.verticalMargin = mVerticalMargin; |
| 649 | mParams.horizontalMargin = mHorizontalMargin; |
Tony Hill | 929442d | 2014-09-16 11:27:35 +0100 | [diff] [blame] | 650 | mParams.packageName = packageName; |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 651 | mParams.hideTimeoutMilliseconds = mDuration == |
Robert Carr | 70f0d22 | 2016-04-10 16:33:08 -0700 | [diff] [blame] | 652 | Toast.LENGTH_LONG ? LONG_DURATION_TIMEOUT : SHORT_DURATION_TIMEOUT; |
Svetoslav Ganov | aa07653 | 2016-08-01 19:16:43 -0700 | [diff] [blame] | 653 | mParams.token = windowToken; |
Romain Guy | cf3dd6b | 2011-01-19 16:54:13 -0800 | [diff] [blame] | 654 | if (mView.getParent() != null) { |
| 655 | if (localLOGV) Log.v(TAG, "REMOVE! " + mView + " in " + this); |
| 656 | mWM.removeView(mView); |
| 657 | } |
| 658 | if (localLOGV) Log.v(TAG, "ADD! " + mView + " in " + this); |
Svet Ganov | 0df3702 | 2017-05-15 23:45:03 -0700 | [diff] [blame] | 659 | // Since the notification manager service cancels the token right |
| 660 | // after it notifies us to cancel the toast there is an inherent |
| 661 | // race and we may attempt to add a window after the token has been |
| 662 | // invalidated. Let us hedge against that. |
| 663 | try { |
| 664 | mWM.addView(mView, mParams); |
| 665 | trySendAccessibilityEvent(); |
Bernardo Rufino | 3231f61 | 2019-12-20 18:04:48 +0000 | [diff] [blame] | 666 | for (Callback callback : getCallbacks()) { |
| 667 | callback.onToastShown(); |
| 668 | } |
Svet Ganov | 0df3702 | 2017-05-15 23:45:03 -0700 | [diff] [blame] | 669 | } catch (WindowManager.BadTokenException e) { |
| 670 | /* ignore */ |
| 671 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 672 | } |
| 673 | } |
| 674 | |
Romain Guy | cf3dd6b | 2011-01-19 16:54:13 -0800 | [diff] [blame] | 675 | private void trySendAccessibilityEvent() { |
| 676 | AccessibilityManager accessibilityManager = |
| 677 | AccessibilityManager.getInstance(mView.getContext()); |
Eugene Susla | d4128ec | 2017-12-04 19:48:41 +0000 | [diff] [blame] | 678 | if (!accessibilityManager.isEnabled()) { |
Romain Guy | cf3dd6b | 2011-01-19 16:54:13 -0800 | [diff] [blame] | 679 | return; |
| 680 | } |
| 681 | // treat toasts as notifications since they are used to |
| 682 | // announce a transient piece of information to the user |
| 683 | AccessibilityEvent event = AccessibilityEvent.obtain( |
| 684 | AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED); |
| 685 | event.setClassName(getClass().getName()); |
| 686 | event.setPackageName(mView.getContext().getPackageName()); |
| 687 | mView.dispatchPopulateAccessibilityEvent(event); |
| 688 | accessibilityManager.sendAccessibilityEvent(event); |
Geoffrey Pitsch | d34c187 | 2017-05-04 16:02:15 -0400 | [diff] [blame] | 689 | } |
Romain Guy | cf3dd6b | 2011-01-19 16:54:13 -0800 | [diff] [blame] | 690 | |
Mathew Inwood | 978c6e2 | 2018-08-21 15:58:55 +0100 | [diff] [blame] | 691 | @UnsupportedAppUsage |
Romain Guy | 0420cc7 | 2009-07-08 11:59:00 -0700 | [diff] [blame] | 692 | public void handleHide() { |
Romain Guy | cf3dd6b | 2011-01-19 16:54:13 -0800 | [diff] [blame] | 693 | if (localLOGV) Log.v(TAG, "HANDLE HIDE: " + this + " mView=" + mView); |
| 694 | if (mView != null) { |
| 695 | // note: checking parent() just to make sure the view has |
| 696 | // been added... i have seen cases where we get here when |
| 697 | // the view isn't yet added, so let's try not to crash. |
| 698 | if (mView.getParent() != null) { |
| 699 | if (localLOGV) Log.v(TAG, "REMOVE! " + mView + " in " + this); |
Svet Ganov | 1777c91 | 2016-09-08 07:11:52 -0700 | [diff] [blame] | 700 | mWM.removeViewImmediate(mView); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 701 | } |
Romain Guy | cf3dd6b | 2011-01-19 16:54:13 -0800 | [diff] [blame] | 702 | |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 703 | |
| 704 | // Now that we've removed the view it's safe for the server to release |
| 705 | // the resources. |
| 706 | try { |
Bernardo Rufino | 52af6db | 2019-12-20 18:26:07 +0000 | [diff] [blame] | 707 | getService().finishToken(mPackageName, mToken); |
Robert Carr | 99742734 | 2018-02-28 18:06:10 -0800 | [diff] [blame] | 708 | } catch (RemoteException e) { |
| 709 | } |
| 710 | |
Bernardo Rufino | 3231f61 | 2019-12-20 18:04:48 +0000 | [diff] [blame] | 711 | for (Callback callback : getCallbacks()) { |
| 712 | callback.onToastHidden(); |
| 713 | } |
Romain Guy | cf3dd6b | 2011-01-19 16:54:13 -0800 | [diff] [blame] | 714 | mView = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 715 | } |
| 716 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 717 | } |
Bernardo Rufino | 3231f61 | 2019-12-20 18:04:48 +0000 | [diff] [blame] | 718 | |
| 719 | /** |
| 720 | * Callback object to be called when the toast is shown or hidden. |
| 721 | * |
| 722 | * Callback methods will be called on the looper thread provided on construction. |
| 723 | * |
| 724 | * @see #addCallback(Callback) |
| 725 | */ |
| 726 | public abstract static class Callback { |
| 727 | /** |
| 728 | * Called when the toast is displayed on the screen. |
| 729 | */ |
| 730 | public void onToastShown() {} |
| 731 | |
| 732 | /** |
| 733 | * Called when the toast is hidden. |
| 734 | */ |
| 735 | public void onToastHidden() {} |
| 736 | } |
Bernardo Rufino | ab338ac | 2019-12-20 18:35:30 +0000 | [diff] [blame] | 737 | |
| 738 | private static class CallbackBinder extends ITransientNotificationCallback.Stub { |
| 739 | private final Handler mHandler; |
| 740 | |
| 741 | @GuardedBy("mCallbacks") |
| 742 | private final List<Callback> mCallbacks; |
| 743 | |
| 744 | /** |
| 745 | * Creates a {@link ITransientNotificationCallback} object. |
| 746 | * |
| 747 | * The parameter {@code callbacks} is not copied and is accessed with itself as its own |
| 748 | * lock. |
| 749 | */ |
| 750 | private CallbackBinder(List<Callback> callbacks, Handler handler) { |
| 751 | mCallbacks = callbacks; |
| 752 | mHandler = handler; |
| 753 | } |
| 754 | |
| 755 | @Override |
| 756 | public void onToastShown() { |
| 757 | mHandler.post(() -> { |
| 758 | for (Callback callback : getCallbacks()) { |
| 759 | callback.onToastShown(); |
| 760 | } |
| 761 | }); |
| 762 | } |
| 763 | |
| 764 | @Override |
| 765 | public void onToastHidden() { |
| 766 | mHandler.post(() -> { |
| 767 | for (Callback callback : getCallbacks()) { |
| 768 | callback.onToastHidden(); |
| 769 | } |
| 770 | }); |
| 771 | } |
| 772 | |
| 773 | private List<Callback> getCallbacks() { |
| 774 | synchronized (mCallbacks) { |
| 775 | return new ArrayList<>(mCallbacks); |
| 776 | } |
| 777 | } |
| 778 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 779 | } |