Home | History | Annotate | Download | only in discovery
      1 /*
      2  * Copyright (C) 2018 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 
     17 package com.android.bips.discovery;
     18 
     19 import android.net.Uri;
     20 import android.util.Log;
     21 
     22 import com.android.bips.DelayedAction;
     23 
     24 import java.util.Collection;
     25 import java.util.Collections;
     26 import java.util.HashMap;
     27 import java.util.Map;
     28 
     29 /**
     30  * A discovery that delays each discovery start and/or printer find by a fixed amount.
     31  */
     32 public class DelayedDiscovery extends Discovery {
     33     private static final String TAG = DelayedDiscovery.class.getSimpleName();
     34     private static final boolean DEBUG = false;
     35 
     36     private final Discovery mChild;
     37     private final int mStartDelay;
     38     private final Listener mChildListener;
     39     private final Map<Uri, DiscoveredPrinter> mPending = new HashMap<>();
     40     private DelayedAction mDelayedStart;
     41 
     42     /**
     43      * Construct a Discovery that behaves like the wrapped discovery but with delays
     44      *
     45      * @param startDelay Milliseconds to delay the first start() command
     46      * @param findDelay  Milliseconds to delay each printer found
     47      */
     48     public DelayedDiscovery(Discovery wrapped, int startDelay, int findDelay) {
     49         super(wrapped.getPrintService());
     50         mChild = wrapped;
     51         mStartDelay = startDelay;
     52         mChildListener = new Listener() {
     53             @Override
     54             public void onPrinterFound(DiscoveredPrinter printer) {
     55                 if (findDelay <= 0) {
     56                     printerFound(printer);
     57                     return;
     58                 }
     59 
     60                 DiscoveredPrinter old = mPending.put(printer.getUri(), printer);
     61                 if (old == null) {
     62                     getHandler().postDelayed(() -> {
     63                         DiscoveredPrinter pending = mPending.remove(printer.getUri());
     64                         if (pending != null) {
     65                             if (DEBUG) Log.d(TAG, "Delay complete for " + pending);
     66                             printerFound(pending);
     67                         }
     68                     }, findDelay);
     69                 }
     70             }
     71 
     72             @Override
     73             public void onPrinterLost(DiscoveredPrinter printer) {
     74                 mPending.remove(printer.getUri());
     75                 printerLost(printer.getUri());
     76             }
     77         };
     78     }
     79 
     80     @Override
     81     void onStart() {
     82         if (mStartDelay == 0) {
     83             mChild.start(mChildListener);
     84         } else {
     85             mDelayedStart = getPrintService().delay(mStartDelay, () -> {
     86                 if (!isStarted()) {
     87                     return;
     88                 }
     89                 mChild.start(mChildListener);
     90             });
     91         }
     92     }
     93 
     94     @Override
     95     void onStop() {
     96         if (mDelayedStart != null) {
     97             mDelayedStart.cancel();
     98         }
     99         mChild.stop(mChildListener);
    100         mPending.clear();
    101     }
    102 
    103     @Override
    104     Collection<Discovery> getChildren() {
    105         return Collections.singleton(mChild);
    106     }
    107 }
    108