1 // Copyright 2020 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package org.chromium.chrome.browser.base; 6 7 import android.app.Service; 8 import android.content.Context; 9 import android.content.Intent; 10 import android.os.IBinder; 11 12 import androidx.annotation.VisibleForTesting; 13 14 /** 15 * Service base class which will call through to the given {@link Impl}. This class must be present 16 * in the base module, while the Impl can be in the chrome module. 17 */ 18 public class SplitCompatService extends Service { 19 private String mServiceClassName; 20 private Impl mImpl; 21 SplitCompatService(String serviceClassName)22 public SplitCompatService(String serviceClassName) { 23 mServiceClassName = serviceClassName; 24 } 25 26 @Override attachBaseContext(Context context)27 protected void attachBaseContext(Context context) { 28 context = SplitCompatUtils.createChromeContext(context); 29 mImpl = (Impl) SplitCompatUtils.newInstance(context, mServiceClassName); 30 mImpl.setService(this); 31 super.attachBaseContext(context); 32 } 33 34 @Override onCreate()35 public void onCreate() { 36 super.onCreate(); 37 mImpl.onCreate(); 38 } 39 40 @Override onStartCommand(Intent intent, int flags, int startId)41 public int onStartCommand(Intent intent, int flags, int startId) { 42 return mImpl.onStartCommand(intent, flags, startId); 43 } 44 45 @Override onDestroy()46 public void onDestroy() { 47 super.onDestroy(); 48 mImpl.onDestroy(); 49 } 50 51 @Override onTaskRemoved(Intent rootIntent)52 public void onTaskRemoved(Intent rootIntent) { 53 super.onTaskRemoved(rootIntent); 54 mImpl.onTaskRemoved(rootIntent); 55 } 56 57 @Override onLowMemory()58 public void onLowMemory() { 59 super.onLowMemory(); 60 mImpl.onLowMemory(); 61 } 62 63 @Override onUnbind(Intent intent)64 public boolean onUnbind(Intent intent) { 65 return mImpl.onUnbind(intent); 66 } 67 68 @Override onBind(Intent intent)69 public IBinder onBind(Intent intent) { 70 return mImpl.onBind(intent); 71 } 72 superOnStartCommand(Intent intent, int flags, int startId)73 private int superOnStartCommand(Intent intent, int flags, int startId) { 74 return super.onStartCommand(intent, flags, startId); 75 } 76 superOnUnbind(Intent intent)77 private boolean superOnUnbind(Intent intent) { 78 return super.onUnbind(intent); 79 } 80 81 @VisibleForTesting attachBaseContextForTesting(Context context, Impl impl)82 public void attachBaseContextForTesting(Context context, Impl impl) { 83 mImpl = impl; 84 super.attachBaseContext(context); 85 } 86 87 /** 88 * Holds the implementation of service logic. Will be called by {@link SplitCompatService}. 89 */ 90 public abstract static class Impl { 91 private SplitCompatService mService; 92 setService(SplitCompatService service)93 protected final void setService(SplitCompatService service) { 94 mService = service; 95 } 96 getService()97 protected final Service getService() { 98 return mService; 99 } 100 onCreate()101 public void onCreate() {} 102 onStartCommand(Intent intent, int flags, int startId)103 public int onStartCommand(Intent intent, int flags, int startId) { 104 return mService.superOnStartCommand(intent, flags, startId); 105 } 106 onDestroy()107 public void onDestroy() {} 108 onTaskRemoved(Intent rootIntent)109 public void onTaskRemoved(Intent rootIntent) {} 110 onLowMemory()111 public void onLowMemory() {} 112 onUnbind(Intent intent)113 public boolean onUnbind(Intent intent) { 114 return mService.superOnUnbind(intent); 115 } 116 onBind(Intent intent)117 public abstract IBinder onBind(Intent intent); 118 } 119 } 120