blob: 9f6dad8f2e2a2d10420a54fa4dbf639aa36d6c4d [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;
24import com.android.printservice.recommendation.plugin.mdnsFilter.MDNSFilterPlugin;
25import com.android.printservice.recommendation.plugin.mdnsFilter.VendorConfig;
26import org.xmlpull.v1.XmlPullParserException;
27
28import java.io.IOException;
29import java.util.ArrayList;
30
31/**
32 * Service that recommends {@link PrintService print services} that might be a good idea to install.
33 */
34public class RecommendationServiceImpl extends RecommendationService
35 implements RemotePrintServicePlugin.OnChangedListener {
36 private static final String LOG_TAG = "PrintServiceRecService";
37
38 /** All registered plugins */
39 private ArrayList<RemotePrintServicePlugin> mPlugins;
40
41 @Override
42 public void onConnected() {
43 mPlugins = new ArrayList<>();
44
45 try {
46 for (VendorConfig config : VendorConfig.getAllConfigs(this)) {
47 try {
48 mPlugins.add(new RemotePrintServicePlugin(new MDNSFilterPlugin(this,
49 config.name, config.packageName, config.mDNSNames), this, false));
50 } catch (Exception e) {
51 Log.e(LOG_TAG, "Could not initiate simple MDNS plugin for " +
52 config.packageName, e);
53 }
54 }
55 } catch (IOException | XmlPullParserException e) {
56 new RuntimeException("Could not parse vendorconfig", e);
57 }
58
59 final int numPlugins = mPlugins.size();
60 for (int i = 0; i < numPlugins; i++) {
61 try {
62 mPlugins.get(i).start();
63 } catch (RemotePrintServicePlugin.PluginException e) {
64 Log.e(LOG_TAG, "Could not start plugin", e);
65 }
66 }
67 }
68
69 @Override
70 public void onDisconnected() {
71 final int numPlugins = mPlugins.size();
72 for (int i = 0; i < numPlugins; i++) {
73 try {
74 mPlugins.get(i).stop();
75 } catch (RemotePrintServicePlugin.PluginException e) {
76 Log.e(LOG_TAG, "Could not stop plugin", e);
77 }
78 }
79 }
80
81 @Override
82 public void onConfigurationChanged(Configuration newConfig) {
83 // Need to update plugin names as they might be localized
84 onChanged();
85 }
86
87 @Override
88 public void onChanged() {
89 ArrayList<RecommendationInfo> recommendations = new ArrayList<>();
90
91 final int numPlugins = mPlugins.size();
92 for (int i = 0; i < numPlugins; i++) {
93 RemotePrintServicePlugin plugin = mPlugins.get(i);
94
95 try {
96 int numPrinters = plugin.getNumPrinters();
97
98 if (numPrinters > 0) {
99 recommendations.add(new RecommendationInfo(plugin.packageName,
100 getString(plugin.name), numPrinters,
101 plugin.recommendsMultiVendorService));
102 }
103 } catch (Exception e) {
104 Log.e(LOG_TAG, "Could not read state of plugin for " + plugin.packageName, e);
105 }
106 }
107
108 updateRecommendations(recommendations);
109 }
110}