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.Context; 21 import android.content.Intent; 22 import android.os.IBinder; 23 import android.os.Handler; 24 import android.os.Message; 25 import android.os.Messenger; 26 import android.os.Process; 27 import android.os.RemoteException; 28 import android.util.Log; 29 30 import android.externalservice.common.RunningServiceInfo; 31 import android.externalservice.common.ServiceMessages; 32 33 public class BaseService extends Service { 34 private static final String TAG = "ExternalServiceTest.Service"; 35 36 private final Handler mHandler = new BaseHandler(this); 37 private final Messenger mMessenger = new Messenger(mHandler); 38 39 @Override onBind(Intent intent)40 public IBinder onBind(Intent intent) { 41 Log.d(TAG, "onBind " + intent); 42 return mMessenger.getBinder(); 43 } 44 45 static class BaseHandler extends Handler { 46 private Context mContext; 47 BaseHandler(Context context)48 public BaseHandler(Context context) { 49 mContext = context; 50 } 51 52 @Override handleMessage(Message msg)53 public void handleMessage(Message msg) { 54 Log.d(TAG, "Received message: " + msg); 55 switch (msg.what) { 56 case ServiceMessages.MSG_IDENTIFY: 57 Message reply = Message.obtain(null, ServiceMessages.MSG_IDENTIFY_RESPONSE); 58 RunningServiceInfo serviceInfo = new RunningServiceInfo(); 59 serviceInfo.uid = Process.myUid(); 60 serviceInfo.pid = Process.myPid(); 61 serviceInfo.packageName = mContext.getPackageName(); 62 serviceInfo.zygotePid = ZygotePreload.getZygotePid(); 63 serviceInfo.zygotePackage = ZygotePreload.getZygotePackage(); 64 reply.getData().putParcelable(ServiceMessages.IDENTIFY_INFO, 65 serviceInfo); 66 try { 67 msg.replyTo.send(reply); 68 } catch (RemoteException e) { 69 Log.e(TAG, "Error sending MSG_IDENTIFY_RESPONSE", e); 70 } 71 break; 72 } 73 super.handleMessage(msg); 74 } 75 } 76 } 77