summaryrefslogtreecommitdiff
path: root/packages/SystemUI/src/com/android/systemui/toast/ToastUI.java
blob: 42f66875e7a193b97af8a854f2894270fc65480f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.toast;

import static android.content.res.Configuration.ORIENTATION_PORTRAIT;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.annotation.MainThread;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.INotificationManager;
import android.app.ITransientNotificationCallback;
import android.content.Context;
import android.content.res.Configuration;
import android.os.IBinder;
import android.os.ServiceManager;
import android.os.UserHandle;
import android.util.Log;
import android.view.accessibility.AccessibilityManager;
import android.view.accessibility.IAccessibilityManager;
import android.widget.ToastPresenter;

import androidx.annotation.VisibleForTesting;

import com.android.systemui.SystemUI;
import com.android.systemui.dagger.SysUISingleton;
import com.android.systemui.statusbar.CommandQueue;

import java.util.Objects;

import javax.inject.Inject;

/**
 * Controls display of text toasts.
 */
@SysUISingleton
public class ToastUI extends SystemUI implements CommandQueue.Callbacks {
    // values from NotificationManagerService#LONG_DELAY and NotificationManagerService#SHORT_DELAY
    private static final int TOAST_LONG_TIME = 3500; // 3.5 seconds
    private static final int TOAST_SHORT_TIME = 2000; // 2 seconds

    private static final String TAG = "ToastUI";

    private final CommandQueue mCommandQueue;
    private final INotificationManager mNotificationManager;
    private final IAccessibilityManager mIAccessibilityManager;
    private final AccessibilityManager mAccessibilityManager;
    private final ToastFactory mToastFactory;
    private final ToastLogger mToastLogger;
    @Nullable private ToastPresenter mPresenter;
    @Nullable private ITransientNotificationCallback mCallback;
    private ToastOutAnimatorListener mToastOutAnimatorListener;

    @VisibleForTesting SystemUIToast mToast;
    private int mOrientation = ORIENTATION_PORTRAIT;

    @Inject
    public ToastUI(
            Context context,
            CommandQueue commandQueue,
            ToastFactory toastFactory,
            ToastLogger toastLogger) {
        this(context, commandQueue,
                INotificationManager.Stub.asInterface(
                        ServiceManager.getService(Context.NOTIFICATION_SERVICE)),
                IAccessibilityManager.Stub.asInterface(
                        ServiceManager.getService(Context.ACCESSIBILITY_SERVICE)),
                toastFactory,
                toastLogger);
    }

    @VisibleForTesting
    ToastUI(Context context, CommandQueue commandQueue, INotificationManager notificationManager,
            @Nullable IAccessibilityManager accessibilityManager,
            ToastFactory toastFactory, ToastLogger toastLogger
    ) {
        super(context);
        mCommandQueue = commandQueue;
        mNotificationManager = notificationManager;
        mIAccessibilityManager = accessibilityManager;
        mToastFactory = toastFactory;
        mAccessibilityManager = mContext.getSystemService(AccessibilityManager.class);
        mToastLogger = toastLogger;
    }

    @Override
    public void start() {
        mCommandQueue.addCallback(this);
    }

    @Override
    @MainThread
    public void showToast(int uid, String packageName, IBinder token, CharSequence text,
            IBinder windowToken, int duration, @Nullable ITransientNotificationCallback callback) {
        Runnable showToastRunnable = () -> {
            UserHandle userHandle = UserHandle.getUserHandleForUid(uid);
            Context context = mContext.createContextAsUser(userHandle, 0);
            mToast = mToastFactory.createToast(mContext /* sysuiContext */, text, packageName,
                    userHandle.getIdentifier(), mOrientation);

            if (mToast.getInAnimation() != null) {
                mToast.getInAnimation().start();
            }

            mCallback = callback;
            mPresenter = new ToastPresenter(context, mIAccessibilityManager,
                    mNotificationManager, packageName);
            // Set as trusted overlay so touches can pass through toasts
            mPresenter.getLayoutParams().setTrustedOverlay();
            mToastLogger.logOnShowToast(uid, packageName, text.toString(), token.toString());
            mPresenter.show(mToast.getView(), token, windowToken, duration, mToast.getGravity(),
                    mToast.getXOffset(), mToast.getYOffset(), mToast.getHorizontalMargin(),
                    mToast.getVerticalMargin(), mCallback, mToast.hasCustomAnimation());
        };

        if (mToastOutAnimatorListener != null) {
            // if we're currently animating out a toast, show new toast after prev toast is hidden
            mToastOutAnimatorListener.setShowNextToastRunnable(showToastRunnable);
        } else if (mPresenter != null) {
            // if there's a toast already showing that we haven't tried hiding yet, hide it and
            // then show the next toast after its hidden animation is done
            hideCurrentToast(showToastRunnable);
        } else {
            // else, show this next toast immediately
            showToastRunnable.run();
        }
    }

    @Override
    @MainThread
    public void hideToast(String packageName, IBinder token) {
        if (mPresenter == null || !Objects.equals(mPresenter.getPackageName(), packageName)
                || !Objects.equals(mPresenter.getToken(), token)) {
            Log.w(TAG, "Attempt to hide non-current toast from package " + packageName);
            return;
        }
        mToastLogger.logOnHideToast(packageName, token.toString());
        hideCurrentToast(null);
    }

    @MainThread
    private void hideCurrentToast(Runnable runnable) {
        if (mToast.getOutAnimation() != null) {
            Animator animator = mToast.getOutAnimation();
            mToastOutAnimatorListener = new ToastOutAnimatorListener(mPresenter, mCallback,
                    runnable);
            animator.addListener(mToastOutAnimatorListener);
            animator.start();
        } else {
            mPresenter.hide(mCallback);
            if (runnable != null) {
                runnable.run();
            }
        }
        mToast = null;
        mPresenter = null;
        mCallback = null;
    }

    @Override
    protected void onConfigurationChanged(Configuration newConfig) {
        if (newConfig.orientation != mOrientation) {
            mOrientation = newConfig.orientation;
            if (mToast != null) {
                mToastLogger.logOrientationChange(mToast.mText.toString(),
                        mOrientation == ORIENTATION_PORTRAIT);
                mToast.onOrientationChange(mOrientation);
                mPresenter.updateLayoutParams(
                        mToast.getXOffset(),
                        mToast.getYOffset(),
                        mToast.getHorizontalMargin(),
                        mToast.getVerticalMargin(),
                        mToast.getGravity());
            }
        }
    }

    /**
     * Once the out animation for a toast is finished, start showing the next toast.
     */
    class ToastOutAnimatorListener extends AnimatorListenerAdapter {
        final ToastPresenter mPrevPresenter;
        final ITransientNotificationCallback mPrevCallback;
        @Nullable Runnable mShowNextToastRunnable;

        ToastOutAnimatorListener(
                @NonNull ToastPresenter presenter,
                @NonNull ITransientNotificationCallback callback,
                @Nullable Runnable runnable) {
            mPrevPresenter = presenter;
            mPrevCallback = callback;
            mShowNextToastRunnable = runnable;
        }

        void setShowNextToastRunnable(Runnable runnable) {
            mShowNextToastRunnable = runnable;
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            mPrevPresenter.hide(mPrevCallback);
            if (mShowNextToastRunnable != null) {
                mShowNextToastRunnable.run();
            }
            mToastOutAnimatorListener = null;
        }
    }
}