1 /* 2 * Copyright (C) 2021 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 com.android.car; 18 19 import android.app.Service; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.os.IBinder; 23 24 import com.android.car.internal.ProxiedService; 25 26 import java.io.FileDescriptor; 27 import java.io.PrintWriter; 28 import java.lang.reflect.Constructor; 29 30 /** 31 * Base class to wrap Service lifecycle with real Service code loaded from updatable car service 32 * package. Public Service defined inside builtin should inherit this to provide automatic 33 * wrapping. 34 */ 35 public class ServiceProxy extends Service { 36 private static final String TAG = "CAR.ServiceProxy"; 37 38 private final String mRealServiceClassName; 39 40 private UpdatablePackageContext mUpdatablePackageContext; 41 private Class mRealServiceClass; 42 private ProxiedService mRealService; 43 ServiceProxy(String realServiceClassName)44 public ServiceProxy(String realServiceClassName) { 45 mRealServiceClassName = realServiceClassName; 46 } 47 48 @Override onCreate()49 public void onCreate() { 50 init(); 51 mRealService.onCreate(); 52 } 53 54 @Override onDestroy()55 public void onDestroy() { 56 mRealService.onDestroy(); 57 } 58 59 @Override onBind(Intent intent)60 public IBinder onBind(Intent intent) { 61 return mRealService.onBind(intent); 62 } 63 64 @Override onUnbind(Intent intent)65 public boolean onUnbind(Intent intent) { 66 return mRealService.onUnbind(intent); 67 } 68 @Override onRebind(Intent intent)69 public void onRebind(Intent intent) { 70 mRealService.onRebind(intent); 71 } 72 73 @Override onStartCommand(Intent intent, int flags, int startId)74 public int onStartCommand(Intent intent, int flags, int startId) { 75 return mRealService.onStartCommand(intent, flags, startId); 76 } 77 78 @Override dump(FileDescriptor fd, PrintWriter writer, String[] args)79 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { 80 mRealService.doDump(fd, writer, args); 81 } 82 init()83 private void init() { 84 mUpdatablePackageContext = UpdatablePackageContext.create(this); 85 try { 86 mRealServiceClass = mUpdatablePackageContext.getClassLoader().loadClass( 87 mRealServiceClassName); 88 // Use default constructor always 89 Constructor constructor = mRealServiceClass.getConstructor(); 90 mRealService = (ProxiedService) constructor.newInstance(); 91 mRealService.doAttachBaseContext(mUpdatablePackageContext); 92 mRealService.setBuiltinPackageContext(this); 93 } catch (Exception e) { 94 throw new RuntimeException("Cannot load class:" + mRealServiceClassName, e); 95 } 96 } 97 98 /** Check {@link Service#attachBaseContext(Context)}. */ doAttachBaseContext(Context newBase)99 public void doAttachBaseContext(Context newBase) { 100 attachBaseContext(newBase); 101 } 102 103 /** Check {@link Service#dump(FileDescriptor, PrintWriter, String[])}. */ doDump(FileDescriptor fd, PrintWriter writer, String[] args)104 public void doDump(FileDescriptor fd, PrintWriter writer, String[] args) { 105 dump(fd, writer, args); 106 } 107 } 108