diff options
author | Roshan Pius <rpius@google.com> | 2020-03-19 09:08:16 -0700 |
---|---|---|
committer | Roshan Pius <rpius@google.com> | 2020-03-19 09:27:00 -0700 |
commit | cfae7fe0e506ee5f353c91caeb78325a4e57f0dd (patch) | |
tree | 8a8b0ffbf8f50155c098509fcaa769f1cf71da86 /service | |
parent | 4ea546fce83b7a8b0f3b966d0bc821c613356e73 (diff) |
Add a class for caching settings migration data
This avoids calling the method multiple times on first bootup.
Bug: 151888061
Test: atest com.android.server.wifi
Change-Id: I9c92861ebb026d8305aeb0e29b388e4b1037cfaa
Diffstat (limited to 'service')
5 files changed, 74 insertions, 9 deletions
diff --git a/service/java/com/android/server/wifi/SoftApBackupRestore.java b/service/java/com/android/server/wifi/SoftApBackupRestore.java index 6034e8f1a..441321cd3 100644 --- a/service/java/com/android/server/wifi/SoftApBackupRestore.java +++ b/service/java/com/android/server/wifi/SoftApBackupRestore.java @@ -25,6 +25,7 @@ import android.util.BackupUtils; import android.util.Log; import com.android.server.wifi.util.ApConfigUtil; +import com.android.server.wifi.util.SettingsMigrationDataHolder; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -54,9 +55,12 @@ public class SoftApBackupRestore { private static final int ETHER_ADDR_LEN = 6; // Byte array size of MacAddress private final Context mContext; + private final SettingsMigrationDataHolder mSettingsMigrationDataHolder; - public SoftApBackupRestore(Context context) { + public SoftApBackupRestore(Context context, + SettingsMigrationDataHolder settingsMigrationDataHolder) { mContext = context; + mSettingsMigrationDataHolder = settingsMigrationDataHolder; } /** @@ -163,7 +167,7 @@ public class SoftApBackupRestore { } else { // Migrate data out of settings. WifiMigration.SettingsMigrationData migrationData = - WifiMigration.loadFromSettings(mContext); + mSettingsMigrationDataHolder.retrieveData(); if (migrationData == null) { Log.e(TAG, "No migration data present"); } else { diff --git a/service/java/com/android/server/wifi/SoftApStoreData.java b/service/java/com/android/server/wifi/SoftApStoreData.java index ac32ae66a..ae9a40af9 100644 --- a/service/java/com/android/server/wifi/SoftApStoreData.java +++ b/service/java/com/android/server/wifi/SoftApStoreData.java @@ -25,6 +25,7 @@ import android.text.TextUtils; import android.util.Log; import com.android.server.wifi.util.ApConfigUtil; +import com.android.server.wifi.util.SettingsMigrationDataHolder; import com.android.server.wifi.util.WifiConfigStoreEncryptionUtil; import com.android.server.wifi.util.XmlUtil; @@ -59,6 +60,7 @@ public class SoftApStoreData implements WifiConfigStore.StoreData { private static final String XML_TAG_ALLOWED_CLIENT_LIST = "AllowedClientList"; private final Context mContext; + private final SettingsMigrationDataHolder mSettingsMigrationDataHolder; private final DataSource mDataSource; /** @@ -95,8 +97,10 @@ public class SoftApStoreData implements WifiConfigStore.StoreData { * * @param dataSource The DataSource that implements the update and retrieval of the SSID set. */ - SoftApStoreData(Context context, DataSource dataSource) { + SoftApStoreData(Context context, SettingsMigrationDataHolder settingsMigrationDataHolder, + DataSource dataSource) { mContext = context; + mSettingsMigrationDataHolder = settingsMigrationDataHolder; mDataSource = dataSource; } @@ -265,7 +269,7 @@ public class SoftApStoreData implements WifiConfigStore.StoreData { if (!autoShutdownEnabledTagPresent) { // Migrate data out of settings. WifiMigration.SettingsMigrationData migrationData = - WifiMigration.loadFromSettings(mContext); + mSettingsMigrationDataHolder.retrieveData(); if (migrationData == null) { Log.e(TAG, "No migration data present"); } else { diff --git a/service/java/com/android/server/wifi/WifiInjector.java b/service/java/com/android/server/wifi/WifiInjector.java index 87e161612..f388d2370 100644 --- a/service/java/com/android/server/wifi/WifiInjector.java +++ b/service/java/com/android/server/wifi/WifiInjector.java @@ -54,6 +54,7 @@ import com.android.server.wifi.p2p.WifiP2pMonitor; import com.android.server.wifi.p2p.WifiP2pNative; import com.android.server.wifi.rtt.RttMetrics; import com.android.server.wifi.util.NetdWrapper; +import com.android.server.wifi.util.SettingsMigrationDataHolder; import com.android.server.wifi.util.TelephonyUtil; import com.android.server.wifi.util.WifiPermissionsUtil; import com.android.server.wifi.util.WifiPermissionsWrapper; @@ -160,6 +161,7 @@ public class WifiInjector { private final WifiSettingsConfigStore mSettingsConfigStore; private final WifiScanAlwaysAvailableSettingsCompatibility mWifiScanAlwaysAvailableSettingsCompatibility; + private final SettingsMigrationDataHolder mSettingsMigrationDataHolder; public WifiInjector(Context context) { if (context == null) { @@ -185,6 +187,7 @@ public class WifiInjector { mFrameworkFacade = new FrameworkFacade(); mMacAddressUtil = new MacAddressUtil(); mContext = context; + mSettingsMigrationDataHolder = new SettingsMigrationDataHolder(mContext); mConnectionFailureNotificationBuilder = new ConnectionFailureNotificationBuilder( mContext, getWifiStackPackageName(), mFrameworkFacade); mBatteryStats = context.getSystemService(BatteryStatsManager.class); @@ -198,7 +201,7 @@ public class WifiInjector { mWifiPermissionsUtil = new WifiPermissionsUtil(mWifiPermissionsWrapper, mContext, mUserManager, this); mWifiBackupRestore = new WifiBackupRestore(mWifiPermissionsUtil); - mSoftApBackupRestore = new SoftApBackupRestore(mContext); + mSoftApBackupRestore = new SoftApBackupRestore(mContext, mSettingsMigrationDataHolder); mWifiStateTracker = new WifiStateTracker(mBatteryStats); mWifiThreadRunner = new WifiThreadRunner(wifiHandler); mWifiP2pServiceHandlerThread = new HandlerThread("WifiP2pService"); @@ -264,8 +267,8 @@ public class WifiInjector { new NetworkListUserStoreData(mContext), new RandomizedMacStoreData(), mFrameworkFacade, wifiHandler, mDeviceConfigFacade, mWifiScoreCard); - mSettingsConfigStore = new WifiSettingsConfigStore(context, wifiHandler, mWifiConfigManager, - mWifiConfigStore); + mSettingsConfigStore = new WifiSettingsConfigStore(context, wifiHandler, + mSettingsMigrationDataHolder, mWifiConfigManager, mWifiConfigStore); mSettingsStore = new WifiSettingsStore(mContext, mSettingsConfigStore); mWifiMetrics.setWifiConfigManager(mWifiConfigManager); @@ -679,7 +682,7 @@ public class WifiInjector { */ public SoftApStoreData makeSoftApStoreData( SoftApStoreData.DataSource dataSource) { - return new SoftApStoreData(mContext, dataSource); + return new SoftApStoreData(mContext, mSettingsMigrationDataHolder, dataSource); } public WifiPermissionsUtil getWifiPermissionsUtil() { diff --git a/service/java/com/android/server/wifi/WifiSettingsConfigStore.java b/service/java/com/android/server/wifi/WifiSettingsConfigStore.java index 872d0d437..d77aa4803 100644 --- a/service/java/com/android/server/wifi/WifiSettingsConfigStore.java +++ b/service/java/com/android/server/wifi/WifiSettingsConfigStore.java @@ -25,6 +25,7 @@ import android.text.TextUtils; import android.util.Log; import com.android.internal.annotations.GuardedBy; +import com.android.server.wifi.util.SettingsMigrationDataHolder; import com.android.server.wifi.util.WifiConfigStoreEncryptionUtil; import com.android.server.wifi.util.XmlUtil; @@ -85,6 +86,7 @@ public class WifiSettingsConfigStore { private final Context mContext; private final Handler mHandler; + private final SettingsMigrationDataHolder mSettingsMigrationDataHolder; private final WifiConfigManager mWifiConfigManager; private final Object mLock = new Object(); @@ -112,10 +114,12 @@ public class WifiSettingsConfigStore { } public WifiSettingsConfigStore(@NonNull Context context, @NonNull Handler handler, + @NonNull SettingsMigrationDataHolder settingsMigrationDataHolder, @NonNull WifiConfigManager wifiConfigManager, @NonNull WifiConfigStore wifiConfigStore) { mContext = context; mHandler = handler; + mSettingsMigrationDataHolder = settingsMigrationDataHolder; mWifiConfigManager = wifiConfigManager; // Register our data store. @@ -176,7 +180,7 @@ public class WifiSettingsConfigStore { private void migrateFromSettingsIfNeeded() { if (!mSettings.isEmpty()) return; // already migrated. - mCachedMigrationData = WifiMigration.loadFromSettings(mContext); + mCachedMigrationData = mSettingsMigrationDataHolder.retrieveData(); if (mCachedMigrationData == null) { Log.e(TAG, "No settings data to migrate"); return; diff --git a/service/java/com/android/server/wifi/util/SettingsMigrationDataHolder.java b/service/java/com/android/server/wifi/util/SettingsMigrationDataHolder.java new file mode 100644 index 000000000..cdd0391be --- /dev/null +++ b/service/java/com/android/server/wifi/util/SettingsMigrationDataHolder.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.server.wifi.util; + +import android.annotation.Nullable; +import android.content.Context; +import android.net.wifi.WifiMigration; + +/** + * Holder for storing the migration settings data retrieved from + * {@link android.net.wifi.WifiMigration#loadFromSettings(Context)} to avoid invoking the method + * multiple times. + */ +public class SettingsMigrationDataHolder { + private final Context mContext; + private WifiMigration.SettingsMigrationData mData = null; + private boolean mRetrieved = false; + + public SettingsMigrationDataHolder(Context context) { + mContext = context; + } + + private void retrieveDataIfNecessary() { + if (mRetrieved) return; + mData = WifiMigration.loadFromSettings(mContext); + mRetrieved = true; + } + + /** + * Retrieve the cached data returned from {@link WifiMigration#loadFromSettings(Context)}. + */ + @Nullable + public WifiMigration.SettingsMigrationData retrieveData() { + retrieveDataIfNecessary(); + return mData; + } +} |