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 
51                     String serviceName;
52                     boolean useZygote = msg.arg1 == 1;
53                     if (useZygote) {
54                         serviceName = ".ExternalServiceWithZygote";
55                     } else {
56                         serviceName = ".ExternalService";
57                     }
58                     intent.setComponent(new ComponentName(pkgName, pkgName+serviceName));
59                     CreatorConnection conn = new CreatorConnection(msg.replyTo);
60                     boolean result = context.bindService(intent, conn,
61                             Context.BIND_AUTO_CREATE | Context.BIND_EXTERNAL_SERVICE);
62                     if (result) {
63                         mConnections.add(conn);
64                     } else {
65                         Message reply = Message.obtain();
66                         reply.what = ServiceMessages.MSG_CREATE_EXTERNAL_SERVICE_RESPONSE;
67                         try {
68                             msg.replyTo.send(reply);
69                         } catch (RemoteException e) {
70                             Log.e(TAG, "Failed to send MSG_CREATE_EXTERNAL_SERVICE_RESPONSE", e);
71                         }
72                     }
73             }
74             super.handleMessage(msg);
75         }
76     };
77 
78     private final Messenger mMessenger = new Messenger(mHandler);
79 
80     @Override
onBind(Intent intent)81     public IBinder onBind(Intent intent) {
82         Log.d(TAG, "onBind " + intent);
83         return mMessenger.getBinder();
84     }
85 
86     @Override
onDestroy()87     public void onDestroy() {
88         for (final CreatorConnection conn : mConnections) {
89             unbindService(conn);
90         }
91         super.onDestroy();
92     }
93 
94     private class CreatorConnection implements ServiceConnection {
95         private IBinder mService = null;
96         private Messenger mReplyTo = null;
97 
CreatorConnection(Messenger replyTo)98         public CreatorConnection(Messenger replyTo) {
99             mReplyTo = replyTo;
100         }
101 
102         @Override
onServiceConnected(ComponentName name, IBinder service)103         public void onServiceConnected(ComponentName name, IBinder service) {
104             Log.d(TAG, "onServiceConnected " + name);
105             Message msg =
106                     Message.obtain(null, ServiceMessages.MSG_CREATE_EXTERNAL_SERVICE_RESPONSE);
107             msg.obj = new Messenger(service);
108             try {
109                 mReplyTo.send(msg);
110             } catch (RemoteException e) {
111                 Log.e(TAG, "Failed to send MSG_CREATE_EXTERNAL_SERVICE_RESPONSE", e);
112             }
113             mReplyTo = null;
114         }
115 
116         @Override
onServiceDisconnected(ComponentName name)117         public void onServiceDisconnected(ComponentName name) {
118             Log.d(TAG, "onServiceDisconnected " + name);
119             mService = null;
120         }
121     }
122 }
123