1a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav/*
2a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav * Copyright (C) 2014 The Android Open Source Project
3a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav *
4a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav * Licensed under the Apache License, Version 2.0 (the "License");
5a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav * you may not use this file except in compliance with the License.
6a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav * You may obtain a copy of the License at
7a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav *
8a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav *      http://www.apache.org/licenses/LICENSE-2.0
9a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav *
10a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav * Unless required by applicable law or agreed to in writing, software
11a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav * distributed under the License is distributed on an "AS IS" BASIS,
12a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav * See the License for the specific language governing permissions and
14a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav * limitations under the License.
15a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav */
16a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav
17a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslavpackage com.android.printspooler.model;
18a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav
19a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslavimport android.content.ComponentName;
20a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslavimport android.content.Context;
21a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslavimport android.content.Intent;
22a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslavimport android.content.ServiceConnection;
23a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslavimport android.os.IBinder;
24a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav
25a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslavpublic class PrintSpoolerProvider implements ServiceConnection {
26a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav    private final Context mContext;
27a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav    private final Runnable mCallback;
28a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav
29a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav    private PrintSpoolerService mSpooler;
30a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav
31a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav    public PrintSpoolerProvider(Context context, Runnable callback) {
32a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav        mContext = context;
33a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav        mCallback = callback;
34a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav        Intent intent = new Intent(mContext, PrintSpoolerService.class);
35a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav        mContext.bindService(intent, this, 0);
36a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav    }
37a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav
38a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav    public PrintSpoolerService getSpooler() {
39a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav        return mSpooler;
40a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav    }
41a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav
42a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav    public void destroy() {
43a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav        if (mSpooler != null) {
44a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav            mContext.unbindService(this);
45a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav        }
46a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav    }
47a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav
48a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav    @Override
49a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav    public void onServiceConnected(ComponentName name, IBinder service) {
50a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav        mSpooler = ((PrintSpoolerService.PrintSpooler) service).getService();
51a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav        if (mSpooler != null) {
52a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav            mCallback.run();
53a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav        }
54a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav    }
55a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav
56a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav    @Override
57a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav    public void onServiceDisconnected(ComponentName name) {
58a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav        /* do nothing - we are in the same process */
59a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav    }
60a798c0a984f29f7180883a61839f68d2cbf0c6ceSvetoslav}
61