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