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.content.Context;
21 import android.database.ContentObserver;
22 import android.net.Uri;
23 import android.provider.Settings;
24 import android.util.FeatureFlagUtils;
25 
26 import androidx.annotation.NonNull;
27 import androidx.annotation.Nullable;
28 
29 import com.android.settings.activityembedding.ActivityEmbeddingRulesController;
30 import com.android.settings.activityembedding.ActivityEmbeddingUtils;
31 import com.android.settings.biometrics.fingerprint2.BiometricsEnvironment;
32 import com.android.settings.core.instrumentation.ElapsedTimeUtils;
33 import com.android.settings.development.DeveloperOptionsActivityLifecycle;
34 import com.android.settings.fuelgauge.BatterySettingsStorage;
35 import com.android.settings.homepage.SettingsHomepageActivity;
36 import com.android.settings.localepicker.LocaleNotificationDataManager;
37 import com.android.settings.overlay.FeatureFactory;
38 import com.android.settings.overlay.FeatureFactoryImpl;
39 import com.android.settings.spa.SettingsSpaEnvironment;
40 import com.android.settingslib.applications.AppIconCacheManager;
41 import com.android.settingslib.datastore.BackupRestoreStorageManager;
42 import com.android.settingslib.spa.framework.common.SpaEnvironmentFactory;
43 
44 import com.google.android.setupcompat.util.WizardManagerHelper;
45 
46 import java.lang.ref.WeakReference;
47 
48 /** Settings application which sets up activity embedding rules for the large screen device. */
49 public class SettingsApplication extends Application {
50 
51     private WeakReference<SettingsHomepageActivity> mHomeActivity = new WeakReference<>(null);
52     private BiometricsEnvironment mBiometricsEnvironment;
53 
54     @Override
attachBaseContext(Context base)55     protected void attachBaseContext(Context base) {
56         super.attachBaseContext(base);
57         FeatureFactory.setFactory(this, getFeatureFactory());
58     }
59 
60     @Override
onCreate()61     public void onCreate() {
62         super.onCreate();
63 
64         BackupRestoreStorageManager.getInstance(this)
65                 .add(
66                         new BatterySettingsStorage(this),
67                         LocaleNotificationDataManager.getSharedPreferencesStorage(this));
68 
69         // Add null checking to avoid test case failed.
70         if (getApplicationContext() != null) {
71             ElapsedTimeUtils.assignSuwFinishedTimeStamp(getApplicationContext());
72         }
73 
74         // Set Spa environment.
75         setSpaEnvironment();
76         mBiometricsEnvironment = new BiometricsEnvironment(this);
77 
78         if (ActivityEmbeddingUtils.isSettingsSplitEnabled(this)
79                 && FeatureFlagUtils.isEnabled(this,
80                         FeatureFlagUtils.SETTINGS_SUPPORT_LARGE_SCREEN)) {
81             if (WizardManagerHelper.isUserSetupComplete(this)) {
82                 new ActivityEmbeddingRulesController(this).initRules();
83             } else {
84                 new DeviceProvisionedObserver().registerContentObserver();
85             }
86         }
87 
88         registerActivityLifecycleCallbacks(new DeveloperOptionsActivityLifecycle());
89     }
90 
91     @Override
onTerminate()92     public void onTerminate() {
93         BackupRestoreStorageManager.getInstance(this).removeAll();
94         super.onTerminate();
95     }
96 
97     @NonNull
getFeatureFactory()98     protected FeatureFactory getFeatureFactory() {
99         return new FeatureFactoryImpl();
100     }
101 
102     /**
103      * Set the spa environment instance.
104      * Override this function to set different spa environment for different Settings app.
105      */
setSpaEnvironment()106     protected void setSpaEnvironment() {
107         SpaEnvironmentFactory.INSTANCE.reset(new SettingsSpaEnvironment(this));
108     }
109 
setHomeActivity(SettingsHomepageActivity homeActivity)110     public void setHomeActivity(SettingsHomepageActivity homeActivity) {
111         mHomeActivity = new WeakReference<>(homeActivity);
112     }
113 
getHomeActivity()114     public SettingsHomepageActivity getHomeActivity() {
115         return mHomeActivity.get();
116     }
117 
118     @Nullable
getBiometricEnvironment()119     public BiometricsEnvironment getBiometricEnvironment() {
120         return mBiometricsEnvironment;
121     }
122 
123     @Override
onTrimMemory(int level)124     public void onTrimMemory(int level) {
125         super.onTrimMemory(level);
126         AppIconCacheManager.getInstance().trimMemory(level);
127     }
128 
129     private class DeviceProvisionedObserver extends ContentObserver {
130         private final Uri mDeviceProvisionedUri = Settings.Secure.getUriFor(
131                 Settings.Secure.USER_SETUP_COMPLETE);
132 
DeviceProvisionedObserver()133         DeviceProvisionedObserver() {
134             super(null /* handler */);
135         }
136 
137         @Override
onChange(boolean selfChange, Uri uri, int flags)138         public void onChange(boolean selfChange, Uri uri, int flags) {
139             if (!mDeviceProvisionedUri.equals(uri)) {
140                 return;
141             }
142 
143             SettingsApplication.this.getContentResolver().unregisterContentObserver(this);
144             new ActivityEmbeddingRulesController(SettingsApplication.this).initRules();
145         }
146 
registerContentObserver()147         public void registerContentObserver() {
148             SettingsApplication.this.getContentResolver().registerContentObserver(
149                     mDeviceProvisionedUri,
150                     false /* notifyForDescendants */,
151                     this);
152         }
153     }
154 }
155