• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  * Copyright (C) 2016 Mopria Alliance, Inc.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 package com.android.bips.discovery;
19 
20 import android.net.Uri;
21 import android.util.Log;
22 
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Collection;
26 import java.util.List;
27 
28 /** Combines the behavior of multiple child {@link Discovery} objects to a single one */
29 public class MultiDiscovery extends Discovery {
30     private static final String TAG = MultiDiscovery.class.getSimpleName();
31     private static final boolean DEBUG = false;
32 
33     private final List<Discovery> mDiscoveries = new ArrayList<>();
34     private final List<Discovery> mStartedDiscoveries = new ArrayList<>();
35     private final Listener mChildListener;
36 
37     /**
38      * Construct an aggregate discovery mechanism, with preferred discovery mechanisms first
39      */
MultiDiscovery(Discovery first, Discovery... rest)40     public MultiDiscovery(Discovery first, Discovery... rest) {
41         super(first.getPrintService());
42         mDiscoveries.add(first);
43         mDiscoveries.addAll(Arrays.asList(rest));
44         mChildListener = new Listener() {
45             @Override
46             public void onPrinterFound(DiscoveredPrinter printer) {
47                 printerFound(first(printer.getUri()));
48             }
49 
50             @Override
51             public void onPrinterLost(DiscoveredPrinter printer) {
52                 // Merge remaining printer records, if any
53                 DiscoveredPrinter first = first(printer.getUri());
54                 if (first == null) {
55                     printerLost(printer.getUri());
56                 } else {
57                     printerFound(first);
58                 }
59             }
60         };
61     }
62 
63     /** For a given URI return the first matching record, based on discovery mechanism order */
first(Uri printerUri)64     private DiscoveredPrinter first(Uri printerUri) {
65         for (Discovery discovery : getChildren()) {
66             DiscoveredPrinter found = discovery.getPrinter(printerUri);
67             if (found != null) {
68                 return found;
69             }
70         }
71         return null;
72     }
73 
74     @Override
onStart()75     void onStart() {
76         if (DEBUG) Log.d(TAG, "onStart()");
77         for (Discovery discovery : mDiscoveries) {
78             discovery.start(mChildListener);
79             mStartedDiscoveries.add(discovery);
80         }
81     }
82 
stopAndClearAll()83     private void stopAndClearAll() {
84         for (Discovery discovery : mStartedDiscoveries) {
85             discovery.stop(mChildListener);
86         }
87         mStartedDiscoveries.clear();
88         allPrintersLost();
89     }
90 
91     @Override
onStop()92     void onStop() {
93         if (DEBUG) Log.d(TAG, "onStop()");
94         stopAndClearAll();
95     }
96 
97     @Override
getChildren()98     Collection<Discovery> getChildren() {
99         List<Discovery> children = new ArrayList<>();
100         for (Discovery child : mDiscoveries) {
101             children.addAll(child.getChildren());
102         }
103         return children;
104     }
105 }
106