Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 496d3f6f authored by tmfang's avatar tmfang Committed by Tsung-Mao Fang
Browse files

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 0c37f019)
parent 13d6e131
Loading
Loading
Loading
Loading
+20 −9
Original line number Diff line number Diff line
@@ -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.
    protected void refreshSummary(Preference preference) {
        if (preference == null) {
            return;
        }

        ThreadUtils.postOnBackgroundThread(() -> {
            final NumberFormat percentageFormat = NumberFormat.getPercentInstance();
            final PrivateStorageInfo info = PrivateStorageInfo.getPrivateStorageInfo(
                    mStorageManagerVolumeProvider);
        double privateUsedBytes = info.totalBytes - info.freeBytes;
        return mContext.getString(R.string.storage_summary,
            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));
                        Formatter.formatFileSize(mContext, info.freeBytes)));
            });
        });
    }
}