1 /* 2 * Copyright (C) 2014 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.printspooler.model; 18 19 import static android.content.Context.BIND_AUTO_CREATE; 20 21 import android.content.ComponentName; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.content.ServiceConnection; 25 import android.os.IBinder; 26 27 public class PrintSpoolerProvider implements ServiceConnection { 28 private final Context mContext; 29 private final Runnable mCallback; 30 31 private PrintSpoolerService mSpooler; 32 PrintSpoolerProvider(Context context, Runnable callback)33 public PrintSpoolerProvider(Context context, Runnable callback) { 34 mContext = context; 35 mCallback = callback; 36 Intent intent = new Intent(mContext, PrintSpoolerService.class); 37 mContext.bindService(intent, this, BIND_AUTO_CREATE); 38 } 39 getSpooler()40 public PrintSpoolerService getSpooler() { 41 return mSpooler; 42 } 43 destroy()44 public void destroy() { 45 if (mSpooler != null) { 46 mContext.unbindService(this); 47 } 48 } 49 50 @Override onServiceConnected(ComponentName name, IBinder service)51 public void onServiceConnected(ComponentName name, IBinder service) { 52 mSpooler = ((PrintSpoolerService.PrintSpooler) service).getService(); 53 if (mSpooler != null) { 54 mCallback.run(); 55 } 56 } 57 58 @Override onServiceDisconnected(ComponentName name)59 public void onServiceDisconnected(ComponentName name) { 60 /* do nothing - we are in the same process */ 61 } 62 } 63