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 ArrayList<RemotePrintServicePlugin> mPlugins; 50 51 /** Lock to keep multi-cast enabled */ 52 private WifiManager.MulticastLock mMultiCastLock; 53 54 @Override 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 mPlugins = new ArrayList<>(); 66 67 try { 68 for (VendorConfig config : VendorConfig.getAllConfigs(this)) { 69 try { 70 mPlugins.add(new RemotePrintServicePlugin(new MDNSFilterPlugin(this, 71 config.name, config.packageName, config.mDNSNames), this, false)); 72 } catch (Exception e) { 73 Log.e(LOG_TAG, "Could not initiate simple MDNS plugin for " + 74 config.packageName, e); 75 } 76 } 77 } catch (IOException | XmlPullParserException e) { 78 throw new RuntimeException("Could not parse vendorconfig", e); 79 } 80 81 try { 82 mPlugins.add(new RemotePrintServicePlugin(new CloudPrintPlugin(this), this, 83 true)); 84 } catch (Exception e) { 85 Log.e(LOG_TAG, "Could not initiate " 86 + getString(R.string.plugin_vendor_google_cloud_print) + " plugin", e); 87 } 88 89 try { 90 mPlugins.add(new RemotePrintServicePlugin(new HPRecommendationPlugin(this), this, 91 false)); 92 } catch (Exception e) { 93 Log.e(LOG_TAG, "Could not initiate " + getString(R.string.plugin_vendor_hp) + " plugin", 94 e); 95 } 96 97 try { 98 mPlugins.add(new RemotePrintServicePlugin(new MopriaRecommendationPlugin(this), this, 99 true)); 100 } catch (Exception e) { 101 Log.e(LOG_TAG, "Could not initiate " + getString(R.string.plugin_vendor_morpia) + 102 " plugin", e); 103 } 104 105 try { 106 mPlugins.add(new RemotePrintServicePlugin(new SamsungRecommendationPlugin(this), this, 107 true)); 108 } catch (Exception e) { 109 Log.e(LOG_TAG, "Could not initiate " + getString(R.string.plugin_vendor_samsung) + 110 " plugin", e); 111 } 112 113 try { 114 mPlugins.add(new RemotePrintServicePlugin( 115 new XeroxPrintServiceRecommendationPlugin(this), this, false)); 116 } catch (Exception e) { 117 Log.e(LOG_TAG, "Could not initiate " + getString(R.string.plugin_vendor_xerox) + 118 " plugin", e); 119 } 120 121 final int numPlugins = mPlugins.size(); 122 for (int i = 0; i < numPlugins; i++) { 123 try { 124 mPlugins.get(i).start(); 125 } catch (RemotePrintServicePlugin.PluginException e) { 126 Log.e(LOG_TAG, "Could not start plugin", e); 127 } 128 } 129 } 130 131 @Override 132 public void onDisconnected() { 133 final int numPlugins = mPlugins.size(); 134 for (int i = 0; i < numPlugins; i++) { 135 try { 136 mPlugins.get(i).stop(); 137 } catch (RemotePrintServicePlugin.PluginException e) { 138 Log.e(LOG_TAG, "Could not stop plugin", e); 139 } 140 } 141 142 if (mMultiCastLock != null) { 143 mMultiCastLock.release(); 144 } 145 } 146 147 @Override 148 public void onConfigurationChanged(Configuration newConfig) { 149 // Need to update plugin names as they might be localized 150 onChanged(); 151 } 152 153 @Override 154 public void onChanged() { 155 ArrayList<RecommendationInfo> recommendations = new ArrayList<>(); 156 157 final int numPlugins = mPlugins.size(); 158 for (int i = 0; i < numPlugins; i++) { 159 RemotePrintServicePlugin plugin = mPlugins.get(i); 160 161 try { 162 List<InetAddress> printers = plugin.getPrinters(); 163 164 if (!printers.isEmpty()) { 165 recommendations.add(new RecommendationInfo(plugin.packageName, 166 getString(plugin.name), printers, plugin.recommendsMultiVendorService)); 167 } 168 } catch (Exception e) { 169 Log.e(LOG_TAG, "Could not read state of plugin for " + plugin.packageName, e); 170 } 171 } 172 173 updateRecommendations(recommendations); 174 } 175 } 176