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