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.Application; 8 import android.content.Context; 9 import android.content.Intent; 10 import android.content.res.Configuration; 11 import android.os.Bundle; 12 13 import androidx.annotation.CallSuper; 14 15 import org.chromium.base.ContextUtils; 16 17 /** 18 * Application base class which will call through to the given {@link Impl}. Application classes 19 * which extend this class should also extend {@link Impl}, and call {@link #setImpl(Impl)} before 20 * calling {@link attachBaseContext(Context)}. 21 */ 22 public class SplitCompatApplication extends Application { 23 private Impl mImpl; 24 25 /** 26 * Holds the implementation of application logic. Will be called by {@link 27 * SplitCompatApplication}. 28 */ 29 public static class Impl { 30 private SplitCompatApplication mApplication; 31 setApplication(SplitCompatApplication application)32 private final void setApplication(SplitCompatApplication application) { 33 mApplication = application; 34 } 35 getApplication()36 protected final SplitCompatApplication getApplication() { 37 return mApplication; 38 } 39 40 @CallSuper attachBaseContext(Context context)41 public void attachBaseContext(Context context) { 42 mApplication.superAttachBaseContext(context); 43 } 44 45 @CallSuper startActivity(Intent intent, Bundle options)46 public void startActivity(Intent intent, Bundle options) { 47 mApplication.superStartActivity(intent, options); 48 } 49 onCreate()50 public void onCreate() {} 51 onTrimMemory(int level)52 public void onTrimMemory(int level) {} onConfigurationChanged(Configuration newConfig)53 public void onConfigurationChanged(Configuration newConfig) {} 54 } 55 setImpl(Impl impl)56 public final void setImpl(Impl impl) { 57 assert mImpl == null; 58 mImpl = impl; 59 mImpl.setApplication(this); 60 } 61 62 /** 63 * This exposes the super method so it can be called inside the Impl class code instead of just 64 * at the start. 65 */ superAttachBaseContext(Context context)66 private void superAttachBaseContext(Context context) { 67 super.attachBaseContext(context); 68 } 69 70 /** 71 * This exposes the super method so it can be called inside the Impl class code instead of just 72 * at the start. 73 */ superStartActivity(Intent intent, Bundle options)74 private void superStartActivity(Intent intent, Bundle options) { 75 super.startActivity(intent, options); 76 } 77 78 @Override attachBaseContext(Context context)79 protected void attachBaseContext(Context context) { 80 mImpl.attachBaseContext(context); 81 } 82 83 @Override onCreate()84 public void onCreate() { 85 super.onCreate(); 86 mImpl.onCreate(); 87 } 88 89 @Override onTrimMemory(int level)90 public void onTrimMemory(int level) { 91 super.onTrimMemory(level); 92 mImpl.onTrimMemory(level); 93 } 94 95 /** Forward all startActivity() calls to the two argument version. */ 96 @Override startActivity(Intent intent)97 public void startActivity(Intent intent) { 98 startActivity(intent, null); 99 } 100 101 @Override startActivity(Intent intent, Bundle options)102 public void startActivity(Intent intent, Bundle options) { 103 mImpl.startActivity(intent, options); 104 } 105 106 @Override onConfigurationChanged(Configuration newConfig)107 public void onConfigurationChanged(Configuration newConfig) { 108 super.onConfigurationChanged(newConfig); 109 mImpl.onConfigurationChanged(newConfig); 110 } 111 isBrowserProcess()112 public static boolean isBrowserProcess() { 113 return !ContextUtils.getProcessName().contains(":"); 114 } 115 } 116