blob: ac60a88e5ca641f570dfa82eb5a7a97b3222f2b3 [file] [log] [blame]
Philip P. Moltmann9dcb86a2016-03-14 14:31:12 -07001/*
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;
Philip P. Moltmannbfaa4722016-05-03 13:35:28 -070024import com.android.printservice.recommendation.plugin.hp.HPRecommendationPlugin;
Philip P. Moltmann9dcb86a2016-03-14 14:31:12 -070025import com.android.printservice.recommendation.plugin.mdnsFilter.MDNSFilterPlugin;
26import com.android.printservice.recommendation.plugin.mdnsFilter.VendorConfig;
Philip P. Moltmannbfaa4722016-05-03 13:35:28 -070027import com.android.printservice.recommendation.plugin.mopria.MopriaRecommendationPlugin;
Philip P. Moltmanna26b7752016-05-23 10:33:57 -070028import com.android.printservice.recommendation.plugin.samsung.SamsungRecommendationPlugin;
Philip P. Moltmann63498af2016-05-27 14:10:54 -070029import com.android.printservice.recommendation.plugin.xerox.XeroxPrintServiceRecommendationPlugin;
Philip P. Moltmann9dcb86a2016-03-14 14:31:12 -070030import org.xmlpull.v1.XmlPullParserException;
31
32import java.io.IOException;
33import java.util.ArrayList;
34
35/**
36 * Service that recommends {@link PrintService print services} that might be a good idea to install.
37 */
38public 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
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
Philip P. Moltmannbfaa4722016-05-03 13:35:28 -070063 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
Philip P. Moltmanna26b7752016-05-23 10:33:57 -070079 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
Philip P. Moltmann63498af2016-05-27 14:10:54 -070087 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
Philip P. Moltmann9dcb86a2016-03-14 14:31:12 -070095 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
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
118 public void onConfigurationChanged(Configuration newConfig) {
119 // Need to update plugin names as they might be localized
120 onChanged();
121 }
122
123 @Override
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}