Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f204e76a authored by Michael Chan's avatar Michael Chan Committed by Android (Google) Code Review
Browse files

Merge "Add tests for notification" into jb-dev

parents c948d1a7 25b09db8
Loading
Loading
Loading
Loading
+28 −15
Original line number Diff line number Diff line
@@ -16,14 +16,10 @@

package com.android.calendar.alerts;

import com.android.calendar.R;
import com.android.calendar.Utils;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
@@ -42,9 +38,12 @@ import android.text.style.RelativeSizeSpan;
import android.text.style.TextAppearanceSpan;
import android.util.Log;

import com.android.calendar.R;
import com.android.calendar.Utils;
import com.android.calendar.alerts.AlertService.NotificationWrapper;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
@@ -216,11 +215,14 @@ public class AlertReceiver extends BroadcastReceiver {
        return PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    }

    public static Notification makeBasicNotification(Context context, String title,
    public static NotificationWrapper makeBasicNotification(Context context, String title,
            String summaryText, long startMillis, long endMillis, long eventId,
            int notificationId, boolean doPopup) {
        return makeBasicNotificationBuilder(context, title, summaryText, startMillis, endMillis,
                eventId, notificationId, doPopup, false, false).build();

        Notification n = makeBasicNotificationBuilder(context, title, summaryText, startMillis,
                endMillis, eventId, notificationId, doPopup, false, false).build();

        return new NotificationWrapper(n, notificationId, eventId, startMillis, endMillis, doPopup);
    }

    private static Notification.Builder makeBasicNotificationBuilder(Context context, String title,
@@ -282,7 +284,7 @@ public class AlertReceiver extends BroadcastReceiver {
     * Creates an expanding notification.  The initial expanded state is decided by
     * the notification manager based on the priority.
     */
    public static Notification makeExpandingNotification(Context context, String title,
    public static NotificationWrapper makeExpandingNotification(Context context, String title,
            String summaryText, String description, long startMillis, long endMillis, long eventId,
            int notificationId, boolean doPopup, boolean highPriority) {
        Notification.Builder basicBuilder = makeBasicNotificationBuilder(context, title,
@@ -309,13 +311,15 @@ public class AlertReceiver extends BroadcastReceiver {
            text = stringBuilder;
        }
        expandedBuilder.bigText(text);
        return expandedBuilder.build();

        return new NotificationWrapper(expandedBuilder.build(), notificationId, eventId,
                startMillis, endMillis, doPopup);
    }

    /**
     * Creates an expanding digest notification for expired events.
     */
    public static Notification makeDigestNotification(Context context,
    public static NotificationWrapper makeDigestNotification(Context context,
            ArrayList<AlertService.NotificationInfo> notificationInfos, String digestTitle,
            boolean expandable) {
        if (notificationInfos == null || notificationInfos.size() < 1) {
@@ -360,6 +364,8 @@ public class AlertReceiver extends BroadcastReceiver {
        // Set to min priority to encourage the notification manager to collapse it.
        notificationBuilder.setPriority(Notification.PRIORITY_MIN);

        Notification n;

        if (expandable) {
            // Multiple reminders.  Combine into an expanded digest notification.
            Notification.InboxStyle expandedBuilder = new Notification.InboxStyle(
@@ -409,11 +415,20 @@ public class AlertReceiver extends BroadcastReceiver {
            // Remove the title in the expanded form (redundant with the listed items).
            expandedBuilder.setBigContentTitle("");

            return expandedBuilder.build();
            n = expandedBuilder.build();
        } else {
            return notificationBuilder.build();
            n = notificationBuilder.build();
        }

        NotificationWrapper nw = new NotificationWrapper(n);
        if (AlertService.DEBUG) {
            for (AlertService.NotificationInfo info : notificationInfos) {
                nw.add(new NotificationWrapper(null, 0, info.eventId, info.startMillis,
                        info.endMillis, false));
            }
        }
        return nw;
    }

    private static final String[] ATTENDEES_PROJECTION = new String[] {
        Attendees.ATTENDEE_EMAIL,           // 0
@@ -499,8 +514,6 @@ public class AlertReceiver extends BroadcastReceiver {
     * are no emailable attendees.
     */
    private static Intent createEmailIntent(Context context, long eventId) {
        ContentResolver resolver = context.getContentResolver();

        // TODO: Refactor to move query part into Utils.createEmailAttendeeIntent, to
        // be shared with EventInfoFragment.

+86 −18
Original line number Diff line number Diff line
@@ -16,10 +16,6 @@

package com.android.calendar.alerts;

import com.android.calendar.GeneralPreferences;
import com.android.calendar.R;
import com.android.calendar.Utils;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
@@ -48,6 +44,10 @@ import android.text.format.DateUtils;
import android.text.format.Time;
import android.util.Log;

import com.android.calendar.GeneralPreferences;
import com.android.calendar.R;
import com.android.calendar.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -62,7 +62,7 @@ public class AlertService extends Service {
    private volatile Looper mServiceLooper;
    private volatile ServiceHandler mServiceHandler;

    private static final String[] ALERT_PROJECTION = new String[] {
    static final String[] ALERT_PROJECTION = new String[] {
        CalendarAlerts._ID,                     // 0
        CalendarAlerts.EVENT_ID,                // 1
        CalendarAlerts.STATE,                   // 2
@@ -109,7 +109,72 @@ public class AlertService extends Service {
    private static final int MIN_DEPRIORITIZE_GRACE_PERIOD_MS = 15 * MINUTE_MS;

    // Hard limit to the number of notifications displayed.
    private static final int MAX_NOTIFICATIONS = 20;
    public static final int MAX_NOTIFICATIONS = 20;

    // Added wrapper for testing
    public static class NotificationWrapper {
        Notification mNotification;
        long mEventId;
        long mBegin;
        long mEnd;
        ArrayList<NotificationWrapper> mNw;

        public NotificationWrapper(Notification n, int notificationId, long eventId,
                long startMillis, long endMillis, boolean doPopup) {
            mNotification = n;
            mEventId = eventId;
            mBegin = startMillis;
            mEnd = endMillis;

            // popup?
            // notification id?
        }

        public NotificationWrapper(Notification n) {
            mNotification = n;
        }

        public void add(NotificationWrapper nw) {
            if (mNw == null) {
                mNw = new ArrayList<NotificationWrapper>();
            }
            mNw.add(nw);
        }
    }

    // Added wrapper for testing
    public static class NotificationMgrWrapper implements NotificationMgr {
        NotificationManager mNm;

        public NotificationMgrWrapper(NotificationManager nm) {
            mNm = nm;
        }

        @Override
        public void cancel(int id) {
            mNm.cancel(id);
        }

        @Override
        public void cancel(String tag, int id) {
            mNm.cancel(tag, id);
        }

        @Override
        public void cancelAll() {
            mNm.cancelAll();
        }

        @Override
        public void notify(int id, NotificationWrapper nw) {
            mNm.notify(id, nw.mNotification);
        }

        @Override
        public void notify(String tag, int id, NotificationWrapper nw) {
            mNm.notify(tag, id, nw.mNotification);
        }
    }

    void processMessage(Message msg) {
        Bundle bundle = (Bundle) msg.obj;
@@ -148,8 +213,8 @@ public class AlertService extends Service {

    static boolean updateAlertNotification(Context context) {
        ContentResolver cr = context.getContentResolver();
        NotificationManager nm =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationMgr nm = new NotificationMgrWrapper(
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE));
        final long currentTime = System.currentTimeMillis();
        SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context);

@@ -182,6 +247,11 @@ public class AlertService extends Service {
            return false;
        }

        return generateAlerts(context, nm, prefs, alertCursor, currentTime);
    }

    public static boolean generateAlerts(Context context, NotificationMgr nm,
            SharedPreferences prefs, Cursor alertCursor, final long currentTime) {
        if (DEBUG) {
            Log.d(TAG, "alertCursor count:" + alertCursor.getCount());
        }
@@ -246,7 +316,7 @@ public class AlertService extends Service {
        int numLowPriority = lowPriorityEvents.size();
        if (numLowPriority > 0) {
            String expiredDigestTitle = getDigestTitle(lowPriorityEvents);
            Notification notification;
            NotificationWrapper notification;
            if (numLowPriority == 1) {
                // If only 1 expired event, display an "old-style" basic alert.
                NotificationInfo info = lowPriorityEvents.get(0);
@@ -415,7 +485,6 @@ public class AlertService extends Service {
        ContentResolver cr = context.getContentResolver();
        HashMap<Long, NotificationInfo> eventIds = new HashMap<Long, NotificationInfo>();
        int numFired = 0;
        int notificationId = 1;
        try {
            while (alertCursor.moveToNext()) {
                final long alertId = alertCursor.getLong(ALERT_INDEX_ID);
@@ -487,8 +556,7 @@ public class AlertService extends Service {
                    continue;
                }

                // Pick an Event title for the notification panel by the latest
                // alertTime and give prefer accepted events in case of ties.
                // TODO: Prefer accepted events in case of ties.
                int newStatus;
                switch (status) {
                    case Attendees.ATTENDEE_STATUS_ACCEPTED:
@@ -596,9 +664,9 @@ public class AlertService extends Service {

    private static void postNotification(NotificationInfo info, String summaryText,
            Context context, boolean highPriority, NotificationPrefs prefs,
            NotificationManager notificationMgr, int notificationId) {
            NotificationMgr notificationMgr, int notificationId) {
        String tickerText = getTickerText(info.eventName, info.location);
        Notification notification = AlertReceiver.makeExpandingNotification(context,
        NotificationWrapper notification = AlertReceiver.makeExpandingNotification(context,
                info.eventName, summaryText, info.description, info.startMillis,
                info.endMillis, info.eventId, notificationId, prefs.getDoPopup(),
                highPriority);
@@ -657,8 +725,9 @@ public class AlertService extends Service {
        }
    }

    private static void addNotificationOptions(Notification notification, boolean quietUpdate,
    private static void addNotificationOptions(NotificationWrapper nw, boolean quietUpdate,
            String tickerText, boolean defaultVibrate, String reminderRingtone) {
        Notification notification = nw.mNotification;
        notification.defaults |= Notification.DEFAULT_LIGHTS;

        // Quietly update notification bar. Nothing new. Maybe something just got deleted.
@@ -683,7 +752,7 @@ public class AlertService extends Service {
        }
    }

    private static class NotificationPrefs {
    /* package */ static class NotificationPrefs {
        boolean quietUpdate;
        private Context context;
        private SharedPreferences prefs;
@@ -695,8 +764,7 @@ public class AlertService extends Service {

        private static final String EMPTY_RINGTONE = "";

        NotificationPrefs(Context context, SharedPreferences prefs,
                boolean quietUpdate) {
        NotificationPrefs(Context context, SharedPreferences prefs, boolean quietUpdate) {
            this.context = context;
            this.prefs = prefs;
            this.quietUpdate = quietUpdate;
+11 −0
Original line number Diff line number Diff line
package com.android.calendar.alerts;

import com.android.calendar.alerts.AlertService.NotificationWrapper;

public interface NotificationMgr {
    public void cancel(int id);
    public void cancel(String tag, int id);
    public void cancelAll();
    public void notify(int id, NotificationWrapper notification);
    public void notify(String tag, int id, NotificationWrapper notification);
}
+0 −6
Original line number Diff line number Diff line
@@ -32,10 +32,4 @@
    <instrumentation android:name="android.test.InstrumentationTestRunner"
                     android:targetPackage="com.android.calendar"
                     android:label="calendar tests"/>

    <instrumentation android:name="com.android.calendar.CalendarLaunchPerformance"
        android:targetPackage="com.android.calendar"
        android:label="Calendar Launch Performance">
    </instrumentation>

</manifest>
+0 −51
Original line number Diff line number Diff line
/*
 * Copyright (C) 2007 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.calendar;

import android.app.Activity;
import android.test.LaunchPerformanceBase;
import android.os.Bundle;

/**
 * Instrumentation class for Browser launch performance testing.
 */
public class CalendarLaunchPerformance extends LaunchPerformanceBase {

    public static final String LOG_TAG = "CalendarLaunchPerformance";

    public CalendarLaunchPerformance() {
        super();
    }

    @Override
    public void onCreate(Bundle arguments) {
        super.onCreate(arguments);

        mIntent.setClassName(getTargetContext(), "com.android.calendar.LaunchActivity");
        start();
    }

    /**
     * Calls LaunchApp and finish.
     */
    @Override
    public void onStart() {
        super.onStart();
        LaunchApp();
        finish(Activity.RESULT_OK, mResults);
    }
}
Loading