1 /* 2 * Copyright (C) 2016 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.printservice.recommendation; 18 19 import android.content.res.Configuration; 20 import android.net.wifi.WifiManager; 21 import android.printservice.PrintService; 22 import android.printservice.recommendation.RecommendationInfo; 23 import android.printservice.recommendation.RecommendationService; 24 import android.util.Log; 25 26 import com.android.printservice.recommendation.plugin.google.CloudPrintPlugin; 27 import com.android.printservice.recommendation.plugin.hp.HPRecommendationPlugin; 28 import com.android.printservice.recommendation.plugin.mdnsFilter.MDNSFilterPlugin; 29 import com.android.printservice.recommendation.plugin.mdnsFilter.VendorConfig; 30 import com.android.printservice.recommendation.plugin.mopria.MopriaRecommendationPlugin; 31 import com.android.printservice.recommendation.plugin.samsung.SamsungRecommendationPlugin; 32 import com.android.printservice.recommendation.plugin.xerox.XeroxPrintServiceRecommendationPlugin; 33 34 import org.xmlpull.v1.XmlPullParserException; 35 36 import java.io.IOException; 37 import java.net.InetAddress; 38 import java.util.ArrayList; 39 import java.util.List; 40 41 /** 42 * Service that recommends {@link PrintService print services} that might be a good idea to install. 43 */ 44 public class RecommendationServiceImpl extends RecommendationService 45 implements RemotePrintServicePlugin.OnChangedListener { 46 private static final String LOG_TAG = "PrintServiceRecService"; 47 48 /** All registered plugins */ 49 private final ArrayList<RemotePrintServicePlugin> mPlugins = new ArrayList<>(); 50 51 /** Lock to keep multi-cast enabled */ 52 private WifiManager.MulticastLock mMultiCastLock; 53 54 @Override onConnected()55 public void onConnected() { 56 WifiManager wifiManager = getSystemService(WifiManager.class); 57 if (wifiManager != null) { 58 if (mMultiCastLock == null) { 59 mMultiCastLock = wifiManager.createMulticastLock(LOG_TAG); 60 } 61 62 mMultiCastLock.acquire(); 63 } 64 65 try { 66 for (VendorConfig config : VendorConfig.getAllConfigs(this)) { 67 try { 68 mPlugins.add(new RemotePrintServicePlugin(new MDNSFilterPlugin(this, 69 config.name, config.packageName, config.mDNSNames), this, false)); 70 } catch (Exception e) { 71 Log.e(LOG_TAG, "Could not initiate simple MDNS plugin for " + 72 config.packageName, e); 73 } 74 } 75 } catch (IOException | XmlPullParserException e) { 76 throw new RuntimeException("Could not parse vendorconfig", e); 77 } 78 79 try { 80 mPlugins.add(new RemotePrintServicePlugin(new CloudPrintPlugin(this), this, 81 true)); 82 } catch (Exception e) { 83 Log.e(LOG_TAG, "Could not initiate " 84 + getString(R.string.plugin_vendor_google_cloud_print) + " plugin", e); 85 } 86 87 try { 88 mPlugins.add(new RemotePrintServicePlugin(new HPRecommendationPlugin(this), this, 89 false)); 90 } catch (Exception e) { 91 Log.e(LOG_TAG, "Could not initiate " + getString(R.string.plugin_vendor_hp) + " plugin", 92 e); 93 } 94 95 try { 96 mPlugins.add(new RemotePrintServicePlugin(new MopriaRecommendationPlugin(this), this, 97 true)); 98 } catch (Exception e) { 99 Log.e(LOG_TAG, "Could not initiate " + getString(R.string.plugin_vendor_morpia) + 100 " plugin", e); 101 } 102 103 try { 104 mPlugins.add(new RemotePrintServicePlugin(new SamsungRecommendationPlugin(this), this, 105 true)); 106 } catch (Exception e) { 107 Log.e(LOG_TAG, "Could not initiate " + getString(R.string.plugin_vendor_samsung) + 108 " plugin", e); 109 } 110 111 try { 112 mPlugins.add(new RemotePrintServicePlugin( 113 new XeroxPrintServiceRecommendationPlugin(this), this, false)); 114 } catch (Exception e) { 115 Log.e(LOG_TAG, "Could not initiate " + getString(R.string.plugin_vendor_xerox) + 116 " plugin", e); 117 } 118 119 final int numPlugins = mPlugins.size(); 120 for (int i = 0; i < numPlugins; i++) { 121 try { 122 mPlugins.get(i).start(); 123 } catch (RemotePrintServicePlugin.PluginException e) { 124 Log.e(LOG_TAG, "Could not start plugin", e); 125 } 126 } 127 } 128 129 @Override onDisconnected()130 public void onDisconnected() { 131 final int numPlugins = mPlugins.size(); 132 for (int i = 0; i < numPlugins; i++) { 133 try { 134 mPlugins.get(i).stop(); 135 } catch (RemotePrintServicePlugin.PluginException e) { 136 Log.e(LOG_TAG, "Could not stop plugin", e); 137 } 138 } 139 mPlugins.clear(); 140 141 if (mMultiCastLock != null) { 142 mMultiCastLock.release(); 143 } 144 } 145 146 @Override onConfigurationChanged(Configuration newConfig)147 public void onConfigurationChanged(Configuration newConfig) { 148 // Need to update plugin names as they might be localized 149 onChanged(); 150 } 151 152 @Override onChanged()153 public void onChanged() { 154 ArrayList<RecommendationInfo> recommendations = new ArrayList<>(); 155 156 final int numPlugins = mPlugins.size(); 157 for (int i = 0; i < numPlugins; i++) { 158 RemotePrintServicePlugin plugin = mPlugins.get(i); 159 160 try { 161 List<InetAddress> printers = plugin.getPrinters(); 162 163 if (!printers.isEmpty()) { 164 recommendations.add(new RecommendationInfo(plugin.packageName, 165 getString(plugin.name), printers, plugin.recommendsMultiVendorService)); 166 } 167 } catch (Exception e) { 168 Log.e(LOG_TAG, "Could not read state of plugin for " + plugin.packageName, e); 169 } 170 } 171 172 updateRecommendations(recommendations); 173 } 174 } 175