NotificationTestHelper.java revision 10790672a98debafb8882971e94ff26aec9a6bfa
16fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek/*
26fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek * Copyright (C) 2017 The Android Open Source Project
36fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek *
46fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek * Licensed under the Apache License, Version 2.0 (the "License");
56fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek * you may not use this file except in compliance with the License.
66fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek * You may obtain a copy of the License at
76fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek *
86fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek *      http://www.apache.org/licenses/LICENSE-2.0
96fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek *
106fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek * Unless required by applicable law or agreed to in writing, software
116fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek * distributed under the License is distributed on an "AS IS" BASIS,
126fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
136fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek * See the License for the specific language governing permissions and
146fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek * limitations under the License
156fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek */
166fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek
176fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinekpackage com.android.systemui.statusbar;
186fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek
196fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinekimport android.app.ActivityManager;
206fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinekimport android.app.Notification;
216fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinekimport android.content.Context;
226fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinekimport android.os.UserHandle;
236fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinekimport android.service.notification.StatusBarNotification;
246fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinekimport android.view.LayoutInflater;
256fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinekimport android.widget.RemoteViews;
266fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek
276fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinekimport com.android.systemui.R;
286fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinekimport com.android.systemui.statusbar.notification.InflationException;
296fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinekimport com.android.systemui.statusbar.phone.NotificationGroupManager;
306fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek
316fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek/**
326fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek * A helper class to create {@link ExpandableNotificationRow}
336fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek */
346fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinekpublic class NotificationTestHelper {
356fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek
366fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek    private final Context mContext;
376fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek    private int mId;
386fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek    private final NotificationGroupManager mGroupManager = new NotificationGroupManager();
396fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek
406fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek    public NotificationTestHelper(Context context) {
416fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek        mContext = context;
426fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek    }
436fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek
446fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek    public ExpandableNotificationRow createRow() {
456fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek        Notification publicVersion = new Notification.Builder(mContext).setSmallIcon(
466fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek                R.drawable.ic_person)
476fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek                .setCustomContentView(new RemoteViews(mContext.getPackageName(),
486fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek                        R.layout.custom_view_dark))
496fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek                .build();
506fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek        Notification notification = new Notification.Builder(mContext).setSmallIcon(
516fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek                R.drawable.ic_person)
526fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek                .setContentTitle("Title")
536fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek                .setContentText("Text")
546fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek                .setPublicVersion(publicVersion)
556fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek                .build();
5610790672a98debafb8882971e94ff26aec9a6bfaSelim Cinek        return createRow(notification);
5710790672a98debafb8882971e94ff26aec9a6bfaSelim Cinek    }
5810790672a98debafb8882971e94ff26aec9a6bfaSelim Cinek
5910790672a98debafb8882971e94ff26aec9a6bfaSelim Cinek    public ExpandableNotificationRow createRow(Notification notification) {
6010790672a98debafb8882971e94ff26aec9a6bfaSelim Cinek        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
6110790672a98debafb8882971e94ff26aec9a6bfaSelim Cinek                mContext.LAYOUT_INFLATER_SERVICE);
6210790672a98debafb8882971e94ff26aec9a6bfaSelim Cinek        ExpandableNotificationRow row = (ExpandableNotificationRow) inflater.inflate(
6310790672a98debafb8882971e94ff26aec9a6bfaSelim Cinek                R.layout.status_bar_notification_row,
6410790672a98debafb8882971e94ff26aec9a6bfaSelim Cinek                null, false);
6510790672a98debafb8882971e94ff26aec9a6bfaSelim Cinek        row.setGroupManager(mGroupManager);
666fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek        UserHandle mUser = UserHandle.of(ActivityManager.getCurrentUser());
676fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek        StatusBarNotification sbn = new StatusBarNotification("com.android.systemui",
686fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek                "com.android.systemui", mId++, null, 1000,
696fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek                2000, notification, mUser, null, System.currentTimeMillis());
706fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek        NotificationData.Entry entry = new NotificationData.Entry(sbn);
716fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek        entry.row = row;
726fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek        try {
736fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek            entry.createIcons(mContext, sbn);
746fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek            row.updateNotification(entry);
756fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek        } catch (InflationException e) {
766fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek            throw new RuntimeException(e.getMessage());
776fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek        }
786fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek        return row;
796fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek    }
806fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek
816fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek    public ExpandableNotificationRow createGroup() {
826fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek        ExpandableNotificationRow row = createRow();
836fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek        row.addChildNotification(createRow());
846fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek        row.addChildNotification(createRow());
856fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek        return row;
866fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek    }
876fd06b5b8089952a7fa6e3286d246ab910090b79Selim Cinek}
88