RankingReconsideration.java revision 470c1accf5a54f9844a779eafab74e63c09342b5
1/*
2 * Copyright (C) 2014 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 */
16package com.android.server.notification;
17
18import com.android.server.notification.NotificationManagerService.NotificationRecord;
19
20import java.util.concurrent.TimeUnit;
21
22/**
23 * Represents future work required to extract signals from notifications for ranking.
24 *
25 * {@hide}
26 */
27public abstract class RankingReconsideration implements Runnable {
28    private static final long IMMEDIATE = 0l;
29
30    private static final int START = 0;
31    private static final int RUNNING = 1;
32    private static final int DONE = 2;
33    private static final int CANCELLED = 3;
34
35    private int mState;
36    private long mDelay;
37    protected String mKey;
38
39    public RankingReconsideration(String key) {
40        this(key, IMMEDIATE);
41    }
42
43    public RankingReconsideration(String key, long delay) {
44        mDelay = delay;
45        mKey = key;
46        mState = START;
47    }
48
49    public String getKey() {
50        return mKey;
51    }
52
53    public void run() {
54        if (mState == START) {
55            mState = RUNNING;
56
57            work();
58
59            mState = DONE;
60            synchronized (this) {
61                notifyAll();
62            }
63        }
64    }
65
66    public long getDelay(TimeUnit unit) {
67        return unit.convert(mDelay, TimeUnit.MILLISECONDS);
68    }
69
70    public boolean cancel(boolean mayInterruptIfRunning) {
71        if (mState == START) {  // can't cancel if running or done
72            mState = CANCELLED;
73            return true;
74        }
75        return false;
76    }
77
78    public boolean isCancelled() {
79        return mState == CANCELLED;
80    }
81
82    public boolean isDone() {
83        return mState == DONE;
84    }
85
86    /**
87     * Analyse the notification.  This will be called on a worker thread. To
88     * avoid concurrency issues, do not use held references to modify the
89     * {@link NotificationRecord}.
90     */
91    public abstract void work();
92
93    /**
94     * Apply any computed changes to the notification record.  This method will be
95     * called on the main service thread, synchronized on he mNotificationList.
96     * @param record The locked record to be updated.
97     */
98    public abstract void applyChangesLocked(NotificationRecord record);
99}
100