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.settings; 18 19 import android.app.Application; 20 import android.database.ContentObserver; 21 import android.net.Uri; 22 import android.provider.Settings; 23 import android.util.FeatureFlagUtils; 24 25 import com.android.settings.activityembedding.ActivityEmbeddingRulesController; 26 import com.android.settings.activityembedding.ActivityEmbeddingUtils; 27 import com.android.settings.core.instrumentation.ElapsedTimeUtils; 28 import com.android.settings.homepage.SettingsHomepageActivity; 29 import com.android.settings.spa.SettingsSpaEnvironment; 30 import com.android.settingslib.applications.AppIconCacheManager; 31 import com.android.settingslib.spa.framework.common.SpaEnvironmentFactory; 32 33 import com.google.android.setupcompat.util.WizardManagerHelper; 34 35 import java.lang.ref.WeakReference; 36 37 /** Settings application which sets up activity embedding rules for the large screen device. */ 38 public class SettingsApplication extends Application { 39 40 private WeakReference<SettingsHomepageActivity> mHomeActivity = new WeakReference<>(null); 41 42 @Override onCreate()43 public void onCreate() { 44 super.onCreate(); 45 46 // Add null checking to avoid test case failed. 47 if (getApplicationContext() != null) { 48 ElapsedTimeUtils.assignSuwFinishedTimeStamp(getApplicationContext()); 49 } 50 51 // Set Spa environment. 52 setSpaEnvironment(); 53 54 if (ActivityEmbeddingUtils.isSettingsSplitEnabled(this) 55 && FeatureFlagUtils.isEnabled(this, 56 FeatureFlagUtils.SETTINGS_SUPPORT_LARGE_SCREEN)) { 57 if (WizardManagerHelper.isUserSetupComplete(this)) { 58 new ActivityEmbeddingRulesController(this).initRules(); 59 } else { 60 new DeviceProvisionedObserver().registerContentObserver(); 61 } 62 } 63 } 64 65 /** 66 * Set the spa environment instance. 67 * Override this function to set different spa environment for different Settings app. 68 */ setSpaEnvironment()69 protected void setSpaEnvironment() { 70 SpaEnvironmentFactory.INSTANCE.reset(new SettingsSpaEnvironment(this)); 71 } 72 setHomeActivity(SettingsHomepageActivity homeActivity)73 public void setHomeActivity(SettingsHomepageActivity homeActivity) { 74 mHomeActivity = new WeakReference<>(homeActivity); 75 } 76 getHomeActivity()77 public SettingsHomepageActivity getHomeActivity() { 78 return mHomeActivity.get(); 79 } 80 81 @Override onTrimMemory(int level)82 public void onTrimMemory(int level) { 83 super.onTrimMemory(level); 84 AppIconCacheManager.getInstance().trimMemory(level); 85 } 86 87 private class DeviceProvisionedObserver extends ContentObserver { 88 private final Uri mDeviceProvisionedUri = Settings.Secure.getUriFor( 89 Settings.Secure.USER_SETUP_COMPLETE); 90 DeviceProvisionedObserver()91 DeviceProvisionedObserver() { 92 super(null /* handler */); 93 } 94 95 @Override onChange(boolean selfChange, Uri uri, int flags)96 public void onChange(boolean selfChange, Uri uri, int flags) { 97 if (!mDeviceProvisionedUri.equals(uri)) { 98 return; 99 } 100 101 SettingsApplication.this.getContentResolver().unregisterContentObserver(this); 102 new ActivityEmbeddingRulesController(SettingsApplication.this).initRules(); 103 } 104 registerContentObserver()105 public void registerContentObserver() { 106 SettingsApplication.this.getContentResolver().registerContentObserver( 107 mDeviceProvisionedUri, 108 false /* notifyForDescendants */, 109 this); 110 } 111 } 112 } 113