blob: b7ea51271043f572b6a8f51496147a48b7b5e86c [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 android.printservice.recommendation;
18
19import android.annotation.Nullable;
20import android.annotation.SystemApi;
21import android.app.Service;
22import android.content.Context;
23import android.content.Intent;
24import android.os.Handler;
25import android.os.IBinder;
26import android.os.Looper;
27import android.os.Message;
28import android.os.RemoteException;
29import android.util.Log;
30import com.android.internal.annotations.GuardedBy;
31
32import java.util.List;
33
34/**
35 * Base class for the print service recommendation services.
36 *
37 * @hide
38 */
39@SystemApi
40public abstract class RecommendationService extends Service {
41 private static final String LOG_TAG = "PrintServiceRecS";
42
43 /** Used to push onConnect and onDisconnect on the main thread */
44 private Handler mHandler;
45
46 /**
47 * The {@link Intent} action that must be declared as handled by a service in its manifest for
48 * the system to recognize it as a print service recommendation service.
49 *
50 * @hide
51 */
52 public static final String SERVICE_INTERFACE =
53 "android.printservice.recommendation.RecommendationService";
54
55 /** Registered callbacks, only modified on main thread */
56 private IRecommendationServiceCallbacks mCallbacks;
57
58 @Override
59 protected void attachBaseContext(Context base) {
60 super.attachBaseContext(base);
61
62 mHandler = new MyHandler();
63 }
64
65 /**
66 * Update the print service recommendations.
67 *
68 * @param recommendations The new set of recommendations
69 */
70 public final void updateRecommendations(@Nullable List<RecommendationInfo> recommendations) {
71 mHandler.obtainMessage(MyHandler.MSG_UPDATE, recommendations).sendToTarget();
72 }
73
74 @Override
75 public final IBinder onBind(Intent intent) {
76 return new IRecommendationService.Stub() {
77 @Override
78 public void registerCallbacks(IRecommendationServiceCallbacks callbacks) {
79 // The callbacks come in order of the caller on oneway calls. Hence while the caller
80 // cannot know at what time the connection is made, he can know the ordering of
81 // connection and disconnection.
82 //
83 // Similar he cannot know when the disconnection is processed, hence he has to
84 // handle callbacks after calling disconnect.
85 if (callbacks != null) {
86 mHandler.obtainMessage(MyHandler.MSG_CONNECT, callbacks).sendToTarget();
87 } else {
88 mHandler.obtainMessage(MyHandler.MSG_DISCONNECT).sendToTarget();
89 }
90 }
91 };
92 }
93
94 /**
95 * Called when the client connects to the recommendation service.
96 */
97 public abstract void onConnected();
98
99 /**
100 * Called when the client disconnects from the recommendation service.
101 */
102 public abstract void onDisconnected();
103
104 private class MyHandler extends Handler {
105 static final int MSG_CONNECT = 1;
106 static final int MSG_DISCONNECT = 2;
107 static final int MSG_UPDATE = 3;
108
109 MyHandler() {
110 super(Looper.getMainLooper());
111 }
112
113 @Override
114 public void handleMessage(Message msg) {
115 switch (msg.what) {
116 case MSG_CONNECT:
117 mCallbacks = (IRecommendationServiceCallbacks) msg.obj;
118 onConnected();
119 break;
120 case MSG_DISCONNECT:
121 onDisconnected();
122 mCallbacks = null;
123 break;
124 case MSG_UPDATE:
125 // Note that there might be a connection change in progress. In this case the
126 // message is handled as before the change. This is acceptable as the caller of
127 // the connection change has not guarantee when the connection change binder
128 // transaction is actually processed.
129 try {
130 mCallbacks.onRecommendationsUpdated((List<RecommendationInfo>) msg.obj);
131 } catch (RemoteException | NullPointerException e) {
132 Log.e(LOG_TAG, "Could not update recommended services", e);
133 }
134 break;
135 }
136 }
137 }
138}