Improve Settings launch performance

From traces analysis, we found getFreeBytes
was taking a long time to return.

getFreeBytes was used when storage controller
tried to get storage information.

In order to prevent this case, we put the action
which takes too much time in background thread.

Test: I can't reproduce it locally. From code view,
this is a reasonable root cause.
Fixes: 136268875

Change-Id: I78e42cde88553c003f198cffb5747b352055f59a
(cherry picked from commit 0c37f019f6)
This commit is contained in:
tmfang
2019-07-03 20:44:42 +08:00
committed by Tsung-Mao Fang
parent 13d6e13166
commit 496d3f6f9c

View File

@@ -20,10 +20,13 @@ import android.content.Context;
import android.os.storage.StorageManager;
import android.text.format.Formatter;
import androidx.preference.Preference;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.deviceinfo.PrivateStorageInfo;
import com.android.settingslib.deviceinfo.StorageManagerVolumeProvider;
import com.android.settingslib.utils.ThreadUtils;
import java.text.NumberFormat;
@@ -44,14 +47,22 @@ public class TopLevelStoragePreferenceController extends BasePreferenceControlle
}
@Override
public CharSequence getSummary() {
// TODO: Register listener.
final NumberFormat percentageFormat = NumberFormat.getPercentInstance();
final PrivateStorageInfo info = PrivateStorageInfo.getPrivateStorageInfo(
mStorageManagerVolumeProvider);
double privateUsedBytes = info.totalBytes - info.freeBytes;
return mContext.getString(R.string.storage_summary,
percentageFormat.format(privateUsedBytes / info.totalBytes),
Formatter.formatFileSize(mContext, info.freeBytes));
protected void refreshSummary(Preference preference) {
if (preference == null) {
return;
}
ThreadUtils.postOnBackgroundThread(() -> {
final NumberFormat percentageFormat = NumberFormat.getPercentInstance();
final PrivateStorageInfo info = PrivateStorageInfo.getPrivateStorageInfo(
mStorageManagerVolumeProvider);
final double privateUsedBytes = info.totalBytes - info.freeBytes;
ThreadUtils.postOnMainThread(() -> {
preference.setSummary(mContext.getString(R.string.storage_summary,
percentageFormat.format(privateUsedBytes / info.totalBytes),
Formatter.formatFileSize(mContext, info.freeBytes)));
});
});
}
}