Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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.server.print; |
| 18 | |
| 19 | import android.content.ComponentName; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.content.pm.PackageManager; |
| 23 | import android.content.pm.ResolveInfo; |
| 24 | import android.print.IPrinterDiscoveryObserver; |
| 25 | import android.print.PrintJobInfo; |
| 26 | import android.printservice.PrintServiceInfo; |
| 27 | import android.provider.Settings; |
| 28 | import android.text.TextUtils; |
| 29 | import android.text.TextUtils.SimpleStringSplitter; |
| 30 | import android.util.Slog; |
| 31 | |
| 32 | import com.android.server.print.RemotePrintSpooler.PrintSpoolerCallbacks; |
| 33 | |
| 34 | import java.util.ArrayList; |
| 35 | import java.util.HashMap; |
| 36 | import java.util.HashSet; |
| 37 | import java.util.List; |
| 38 | import java.util.Map; |
| 39 | import java.util.Set; |
| 40 | |
| 41 | /** |
| 42 | * Represents the print state for a user. |
| 43 | */ |
| 44 | final class UserState implements PrintSpoolerCallbacks { |
| 45 | |
| 46 | private static final String LOG_TAG = "UserState"; |
| 47 | |
| 48 | private static final char COMPONENT_NAME_SEPARATOR = ':'; |
| 49 | |
| 50 | private final SimpleStringSplitter mStringColonSplitter = |
| 51 | new SimpleStringSplitter(COMPONENT_NAME_SEPARATOR); |
| 52 | |
| 53 | private final Intent mQueryIntent = |
| 54 | new Intent(android.printservice.PrintService.SERVICE_INTERFACE); |
| 55 | |
| 56 | private final Map<ComponentName, RemotePrintService> mActiveServices = |
| 57 | new HashMap<ComponentName, RemotePrintService>(); |
| 58 | |
| 59 | private final List<PrintServiceInfo> mInstalledServices = |
| 60 | new ArrayList<PrintServiceInfo>(); |
| 61 | |
| 62 | private final Set<ComponentName> mEnabledServices = |
| 63 | new HashSet<ComponentName>(); |
| 64 | |
| 65 | private final Object mLock; |
| 66 | |
| 67 | private final Context mContext; |
| 68 | |
| 69 | private final int mUserId; |
| 70 | |
| 71 | private final RemotePrintSpooler mSpooler; |
| 72 | |
| 73 | private boolean mDestroyed; |
| 74 | |
| 75 | public UserState(Context context, int userId, Object lock) { |
| 76 | mContext = context; |
| 77 | mUserId = userId; |
| 78 | mLock = lock; |
| 79 | mSpooler = new RemotePrintSpooler(context, userId, this); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public void onPrintJobQueued(PrintJobInfo printJob) { |
| 84 | final RemotePrintService service; |
| 85 | synchronized (mLock) { |
| 86 | throwIfDestroyedLocked(); |
| 87 | ComponentName printServiceName = printJob.getPrinterId().getService(); |
| 88 | service = mActiveServices.get(printServiceName); |
| 89 | } |
| 90 | if (service != null) { |
| 91 | service.onPrintJobQueued(printJob); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public void onAllPrintJobsForServiceHandled(ComponentName printService) { |
| 97 | final RemotePrintService service; |
| 98 | synchronized (mLock) { |
| 99 | throwIfDestroyedLocked(); |
| 100 | service = mActiveServices.get(printService); |
| 101 | } |
| 102 | if (service != null) { |
| 103 | service.onAllPrintJobsHandled(); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public void onStartPrinterDiscovery(IPrinterDiscoveryObserver observer) { |
| 109 | final List<RemotePrintService> services; |
| 110 | synchronized (mLock) { |
| 111 | throwIfDestroyedLocked(); |
| 112 | if (mActiveServices.isEmpty()) { |
| 113 | return; |
| 114 | } |
| 115 | services = new ArrayList<RemotePrintService>(mActiveServices.values()); |
| 116 | } |
| 117 | final int serviceCount = services.size(); |
| 118 | for (int i = 0; i < serviceCount; i++) { |
| 119 | RemotePrintService service = services.get(i); |
| 120 | service.onStartPrinterDiscovery(observer); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public void onStopPrinterDiscovery() { |
| 126 | final List<RemotePrintService> services; |
| 127 | synchronized (mLock) { |
| 128 | throwIfDestroyedLocked(); |
| 129 | if (mActiveServices.isEmpty()) { |
| 130 | return; |
| 131 | } |
| 132 | services = new ArrayList<RemotePrintService>(mActiveServices.values()); |
| 133 | } |
| 134 | final int serviceCount = services.size(); |
| 135 | for (int i = 0; i < serviceCount; i++) { |
| 136 | RemotePrintService service = services.get(i); |
| 137 | service.onStopPrinterDiscovery(); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | public void updateIfNeededLocked() { |
| 142 | throwIfDestroyedLocked(); |
| 143 | if (readConfigurationLocked()) { |
| 144 | onConfigurationChangedLocked(); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | public RemotePrintSpooler getSpoolerLocked() { |
| 149 | throwIfDestroyedLocked(); |
| 150 | return mSpooler; |
| 151 | } |
| 152 | |
| 153 | public Map<ComponentName, RemotePrintService> getActiveServices() { |
| 154 | synchronized(mLock) { |
| 155 | throwIfDestroyedLocked(); |
| 156 | return mActiveServices; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | public Set<ComponentName> getEnabledServices() { |
| 161 | synchronized(mLock) { |
| 162 | throwIfDestroyedLocked(); |
| 163 | return mEnabledServices; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | public void destroyLocked() { |
| 168 | throwIfDestroyedLocked(); |
| 169 | mSpooler.destroy(); |
| 170 | for (RemotePrintService service : mActiveServices.values()) { |
| 171 | service.destroy(); |
| 172 | } |
| 173 | mActiveServices.clear(); |
| 174 | mInstalledServices.clear(); |
| 175 | mEnabledServices.clear(); |
| 176 | mDestroyed = true; |
| 177 | } |
| 178 | |
| 179 | private boolean readConfigurationLocked() { |
| 180 | boolean somethingChanged = false; |
| 181 | somethingChanged |= readInstalledPrintServicesLocked(); |
| 182 | somethingChanged |= readEnabledPrintServicesLocked(); |
| 183 | return somethingChanged; |
| 184 | } |
| 185 | |
| 186 | private boolean readInstalledPrintServicesLocked() { |
| 187 | Set<PrintServiceInfo> tempPrintServices = new HashSet<PrintServiceInfo>(); |
| 188 | |
| 189 | List<ResolveInfo> installedServices = mContext.getPackageManager() |
| 190 | .queryIntentServicesAsUser(mQueryIntent, PackageManager.GET_SERVICES |
| 191 | | PackageManager.GET_META_DATA, mUserId); |
| 192 | |
| 193 | final int installedCount = installedServices.size(); |
| 194 | for (int i = 0, count = installedCount; i < count; i++) { |
| 195 | ResolveInfo installedService = installedServices.get(i); |
| 196 | if (!android.Manifest.permission.BIND_PRINT_SERVICE.equals( |
| 197 | installedService.serviceInfo.permission)) { |
| 198 | ComponentName serviceName = new ComponentName( |
| 199 | installedService.serviceInfo.packageName, |
| 200 | installedService.serviceInfo.name); |
| 201 | Slog.w(LOG_TAG, "Skipping print service " |
| 202 | + serviceName.flattenToShortString() |
| 203 | + " since it does not require permission " |
| 204 | + android.Manifest.permission.BIND_PRINT_SERVICE); |
| 205 | continue; |
| 206 | } |
| 207 | tempPrintServices.add(PrintServiceInfo.create(installedService, mContext)); |
| 208 | } |
| 209 | |
| 210 | if (!tempPrintServices.equals(mInstalledServices)) { |
| 211 | mInstalledServices.clear(); |
| 212 | mInstalledServices.addAll(tempPrintServices); |
| 213 | return true; |
| 214 | } |
| 215 | |
| 216 | return false; |
| 217 | } |
| 218 | |
| 219 | private boolean readEnabledPrintServicesLocked() { |
| 220 | Set<ComponentName> tempEnabledServiceNameSet = new HashSet<ComponentName>(); |
| 221 | |
| 222 | String settingValue = Settings.Secure.getStringForUser(mContext.getContentResolver(), |
| 223 | Settings.Secure.ENABLED_PRINT_SERVICES, mUserId); |
| 224 | if (!TextUtils.isEmpty(settingValue)) { |
| 225 | TextUtils.SimpleStringSplitter splitter = mStringColonSplitter; |
| 226 | splitter.setString(settingValue); |
| 227 | while (splitter.hasNext()) { |
| 228 | String string = splitter.next(); |
| 229 | if (TextUtils.isEmpty(string)) { |
| 230 | continue; |
| 231 | } |
| 232 | ComponentName componentName = ComponentName.unflattenFromString(string); |
| 233 | if (componentName != null) { |
| 234 | tempEnabledServiceNameSet.add(componentName); |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | if (!tempEnabledServiceNameSet.equals(mEnabledServices)) { |
| 240 | mEnabledServices.clear(); |
| 241 | mEnabledServices.addAll(tempEnabledServiceNameSet); |
| 242 | return true; |
| 243 | } |
| 244 | |
| 245 | return false; |
| 246 | } |
| 247 | |
| 248 | private void onConfigurationChangedLocked() { |
| 249 | final int installedCount = mInstalledServices.size(); |
| 250 | for (int i = 0; i < installedCount; i++) { |
| 251 | ResolveInfo resolveInfo = mInstalledServices.get(i).getResolveInfo(); |
| 252 | ComponentName serviceName = new ComponentName(resolveInfo.serviceInfo.packageName, |
| 253 | resolveInfo.serviceInfo.name); |
| 254 | if (mEnabledServices.contains(serviceName)) { |
| 255 | if (!mActiveServices.containsKey(serviceName)) { |
| 256 | mActiveServices.put(serviceName, new RemotePrintService( |
| 257 | mContext, serviceName, mUserId, mSpooler)); |
| 258 | } |
| 259 | } else { |
| 260 | RemotePrintService service = mActiveServices.remove(serviceName); |
| 261 | if (service != null) { |
| 262 | service.destroy(); |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | private void throwIfDestroyedLocked() { |
| 269 | if (mDestroyed) { |
| 270 | throw new IllegalStateException("Cannot interact with a destroyed instance."); |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | |