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