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