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