1 /*
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 
17 package android.externalservice.service;
18 
19 import android.app.Service;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.ServiceConnection;
24 import android.os.Handler;
25 import android.os.IBinder;
26 import android.os.Message;
27 import android.os.Messenger;
28 import android.os.Process;
29 import android.os.RemoteException;
30 import android.util.Log;
31 
32 import java.util.ArrayList;
33 
34 import android.externalservice.common.ServiceMessages;
35 
36 public class ServiceCreator extends Service {
37     private static final String TAG = "ExternalServiceTest.ServiceCreator";
38 
39     private final ArrayList<CreatorConnection> mConnections = new ArrayList<>();
40 
41     private final Handler mHandler = new BaseService.BaseHandler(this) {
42         @Override
43         public void handleMessage(Message msg) {
44             Log.d(TAG, "Received message: " + msg);
45             switch (msg.what) {
46                 case ServiceMessages.MSG_CREATE_EXTERNAL_SERVICE:
47                     final Context context = ServiceCreator.this;
48                     final String pkgName = context.getPackageName();
49                     Intent intent = new Intent();
50                     intent.setComponent(new ComponentName(pkgName, pkgName+".ExternalService"));
51                     CreatorConnection conn = new CreatorConnection(msg.replyTo);
52                     boolean result = context.bindService(intent, conn,
53                             Context.BIND_AUTO_CREATE | Context.BIND_EXTERNAL_SERVICE);
54                     if (result) {
55                         mConnections.add(conn);
56                     } else {
57                         Message reply = Message.obtain();
58                         reply.what = ServiceMessages.MSG_CREATE_EXTERNAL_SERVICE_RESPONSE;
59                         try {
60                             msg.replyTo.send(reply);
61                         } catch (RemoteException e) {
62                             Log.e(TAG, "Failed to send MSG_CREATE_EXTERNAL_SERVICE_RESPONSE", e);
63                         }
64                     }
65             }
66             super.handleMessage(msg);
67         }
68     };
69 
70     private final Messenger mMessenger = new Messenger(mHandler);
71 
72     @Override
onBind(Intent intent)73     public IBinder onBind(Intent intent) {
74         Log.d(TAG, "onBind " + intent);
75         return mMessenger.getBinder();
76     }
77 
78     @Override
onDestroy()79     public void onDestroy() {
80         for (final CreatorConnection conn : mConnections) {
81             unbindService(conn);
82         }
83         super.onDestroy();
84     }
85 
86     private class CreatorConnection implements ServiceConnection {
87         private IBinder mService = null;
88         private Messenger mReplyTo = null;
89 
CreatorConnection(Messenger replyTo)90         public CreatorConnection(Messenger replyTo) {
91             mReplyTo = replyTo;
92         }
93 
94         @Override
onServiceConnected(ComponentName name, IBinder service)95         public void onServiceConnected(ComponentName name, IBinder service) {
96             Log.d(TAG, "onServiceConnected " + name);
97             Message msg =
98                     Message.obtain(null, ServiceMessages.MSG_CREATE_EXTERNAL_SERVICE_RESPONSE);
99             msg.obj = new Messenger(service);
100             try {
101                 mReplyTo.send(msg);
102             } catch (RemoteException e) {
103                 Log.e(TAG, "Failed to send MSG_CREATE_EXTERNAL_SERVICE_RESPONSE", e);
104             }
105             mReplyTo = null;
106         }
107 
108         @Override
onServiceDisconnected(ComponentName name)109         public void onServiceDisconnected(ComponentName name) {
110             Log.d(TAG, "onServiceDisconnected " + name);
111             mService = null;
112         }
113     }
114 }
115