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