summaryrefslogtreecommitdiff
path: root/packages/SystemUI/tests/utils/src/com/android/systemui/statusbar/SbnBuilder.java
blob: 1b0ed112cea1faa9c120f771b97434e42a47adec (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
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.statusbar;

import android.annotation.Nullable;
import android.app.Notification;
import android.content.Context;
import android.os.UserHandle;
import android.service.notification.StatusBarNotification;

import com.android.internal.logging.InstanceId;

/**
 * Convenience builder for {@link StatusBarNotification} since its constructor is terrifying.
 *
 * Only for use in tests.
 */
public class SbnBuilder {
    private String mPkg = "test_pkg";
    private String mOpPkg;
    private int mId;
    private String mTag;
    private int mUid;
    private int mInitialPid;
    @Nullable private Notification mNotification;
    @Nullable private Notification.Builder mNotificationBuilder;
    private Notification.BubbleMetadata mBubbleMetadata;
    private UserHandle mUser = UserHandle.of(0);
    private String mOverrideGroupKey;
    private long mPostTime;
    private InstanceId mInstanceId;

    public SbnBuilder() {
    }

    public SbnBuilder(StatusBarNotification source) {
        mPkg = source.getPackageName();
        mOpPkg = source.getOpPkg();
        mId = source.getId();
        mTag = source.getTag();
        mUid = source.getUid();
        mInitialPid = source.getInitialPid();
        mNotification = source.getNotification();
        mUser = source.getUser();
        mOverrideGroupKey = source.getOverrideGroupKey();
        mPostTime = source.getPostTime();
        mInstanceId = source.getInstanceId();
    }

    public StatusBarNotification build() {
        Notification notification;
        if (mNotificationBuilder != null) {
            notification = mNotificationBuilder.build();
        } else if (mNotification != null) {
            notification = mNotification;
        } else {
            notification = new Notification();
        }

        if (mBubbleMetadata != null) {
            notification.setBubbleMetadata(mBubbleMetadata);
        }

        StatusBarNotification result = new StatusBarNotification(
                mPkg,
                mOpPkg,
                mId,
                mTag,
                mUid,
                mInitialPid,
                notification,
                mUser,
                mOverrideGroupKey,
                mPostTime);
        if (mInstanceId != null) {
            result.setInstanceId(mInstanceId);
        }
        return result;
    }

    public SbnBuilder setPkg(String pkg) {
        mPkg = pkg;
        return this;
    }

    public SbnBuilder setOpPkg(String opPkg) {
        mOpPkg = opPkg;
        return this;
    }

    public SbnBuilder setId(int id) {
        mId = id;
        return this;
    }

    public SbnBuilder setTag(String tag) {
        mTag = tag;
        return this;
    }

    public SbnBuilder setUid(int uid) {
        mUid = uid;
        return this;
    }

    public SbnBuilder setInitialPid(int initialPid) {
        mInitialPid = initialPid;
        return this;
    }

    public SbnBuilder setNotification(Notification notification) {
        mNotification = notification;
        mNotificationBuilder = null;
        return this;
    }

    public SbnBuilder setContentTitle(Context context, String contentTitle) {
        modifyNotification(context).setContentTitle(contentTitle);
        return this;
    }

    public SbnBuilder setContentText(Context context, String contentText) {
        modifyNotification(context).setContentText(contentText);
        return this;
    }

    public SbnBuilder setGroup(Context context, String groupKey) {
        modifyNotification(context).setGroup(groupKey);
        return this;
    }

    public SbnBuilder setGroupSummary(Context context, boolean isGroupSummary) {
        modifyNotification(context).setGroupSummary(isGroupSummary);
        return this;
    }

    public SbnBuilder setFlag(Context context, int mask, boolean value) {
        modifyNotification(context).setFlag(mask, value);
        return this;
    }

    public Notification.Builder modifyNotification(Context context) {
        if (mNotification != null) {
            mNotificationBuilder = new Notification.Builder(context, mNotification);
            mNotification = null;
        } else if (mNotificationBuilder == null) {
            mNotificationBuilder = new Notification.Builder(context);
        }

        return mNotificationBuilder;
    }

    public SbnBuilder setUser(UserHandle user) {
        mUser = user;
        return this;
    }

    public SbnBuilder setOverrideGroupKey(String overrideGroupKey) {
        mOverrideGroupKey = overrideGroupKey;
        return this;
    }

    public SbnBuilder setPostTime(long postTime) {
        mPostTime = postTime;
        return this;
    }

    public SbnBuilder setBubbleMetadata(Notification.BubbleMetadata data) {
        mBubbleMetadata = data;
        return this;
    }

    public SbnBuilder setInstanceId(InstanceId instanceId) {
        mInstanceId = instanceId;
        return this;
    }
}