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

Commit 4aa6da6d authored by Fan Zhang's avatar Fan Zhang
Browse files

Remove cached summary text. load from metadata every time.

Bug: 77600770
Test: robotests
Change-Id: I8d4aec5df1ea13fd9f51138448d43c1375f3fb1b
parent a6afef81
Loading
Loading
Loading
Loading
+56 −11
Original line number Diff line number Diff line
@@ -21,6 +21,8 @@ import static com.android.settingslib.drawer.TileUtils.META_DATA_KEY_PROFILE;
import static com.android.settingslib.drawer.TileUtils.META_DATA_PREFERENCE_ICON;
import static com.android.settingslib.drawer.TileUtils.META_DATA_PREFERENCE_ICON_URI;
import static com.android.settingslib.drawer.TileUtils.META_DATA_PREFERENCE_KEYHINT;
import static com.android.settingslib.drawer.TileUtils.META_DATA_PREFERENCE_SUMMARY;
import static com.android.settingslib.drawer.TileUtils.META_DATA_PREFERENCE_SUMMARY_URI;
import static com.android.settingslib.drawer.TileUtils.META_DATA_PREFERENCE_TITLE;
import static com.android.settingslib.drawer.TileUtils.PROFILE_ALL;
import static com.android.settingslib.drawer.TileUtils.PROFILE_PRIMARY;
@@ -51,14 +53,6 @@ public class Tile implements Parcelable {

    private static final String TAG = "Tile";


    /**
     * Optional summary describing what this tile controls.
     *
     * @attr ref android.R.styleable#PreferenceHeader_summary
     */
    public CharSequence summary;

    /**
     * Optional list of user handles which the intent should be launched on.
     */
@@ -68,11 +62,13 @@ public class Tile implements Parcelable {
     * The metaData from the activity that defines this tile.
     */
    private final Bundle mMetaData;

    private final String mActivityPackage;
    private final String mActivityName;
    private final Intent mIntent;

    private ActivityInfo mActivityInfo;
    private CharSequence mSummaryOverride;

    private String mCategory;

    public Tile(ActivityInfo activityInfo, String category) {
@@ -88,7 +84,6 @@ public class Tile implements Parcelable {
        mActivityPackage = in.readString();
        mActivityName = in.readString();
        mIntent = new Intent().setClassName(mActivityPackage, mActivityName);
        summary = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
        final int N = in.readInt();
        for (int i = 0; i < N; i++) {
            userHandle.add(UserHandle.CREATOR.createFromParcel(in));
@@ -106,7 +101,6 @@ public class Tile implements Parcelable {
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(mActivityPackage);
        dest.writeString(mActivityName);
        TextUtils.writeToParcel(summary, dest, flags);
        final int N = userHandle.size();
        dest.writeInt(N);
        for (int i = 0; i < N; i++) {
@@ -189,6 +183,57 @@ public class Tile implements Parcelable {
        return title;
    }

    /**
     * Returns the raw metadata for summary, this is used for comparing 2 summary text without
     * loading the real string.
     */
    public String getSummaryReference() {
        if (mSummaryOverride != null) {
            return mSummaryOverride.toString();
        }
        if (mMetaData != null && mMetaData.containsKey(META_DATA_PREFERENCE_SUMMARY)) {
            return mMetaData.get(META_DATA_PREFERENCE_SUMMARY).toString();
        }
        return null;
    }

    /**
     * Overrides the summary. This can happen when injected tile wants to provide dynamic summary.
     */
    public void overrideSummary(CharSequence summaryOverride) {
        mSummaryOverride = summaryOverride;
    }

    /**
     * Optional summary describing what this tile controls.
     */
    public CharSequence getSummary(Context context) {
        if (mSummaryOverride != null) {
            return mSummaryOverride;
        }
        CharSequence summary = null;
        final PackageManager packageManager = context.getPackageManager();
        if (mMetaData != null) {
            if (mMetaData.containsKey(META_DATA_PREFERENCE_SUMMARY_URI)) {
                return null;
            }
            if (mMetaData.containsKey(META_DATA_PREFERENCE_SUMMARY)) {
                if (mMetaData.get(META_DATA_PREFERENCE_SUMMARY) instanceof Integer) {
                    try {
                        final Resources res =
                                packageManager.getResourcesForApplication(mActivityPackage);
                        summary = res.getString(mMetaData.getInt(META_DATA_PREFERENCE_SUMMARY));
                    } catch (PackageManager.NameNotFoundException | Resources.NotFoundException e) {
                        Log.d(TAG, "Couldn't find info", e);
                    }
                } else {
                    summary = mMetaData.getString(META_DATA_PREFERENCE_SUMMARY);
                }
            }
        }
        return summary;
    }

    public Bundle getMetaData() {
        return mMetaData;
    }
+0 −3
Original line number Diff line number Diff line
@@ -347,9 +347,6 @@ public class TileUtils {
            } catch (PackageManager.NameNotFoundException | Resources.NotFoundException e) {
                if (DEBUG) Log.d(LOG_TAG, "Couldn't find info", e);
            }

            // Set title and summary for the preference
            tile.summary = summary;
            return true;
        }

+0 −45
Original line number Diff line number Diff line
@@ -24,7 +24,6 @@ import static com.android.settingslib.drawer.TileUtils.META_DATA_PREFERENCE_SUMM

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.argThat;
@@ -39,7 +38,6 @@ import static org.robolectric.RuntimeEnvironment.application;
import android.app.ActivityManager;
import android.content.ContentResolver;
import android.content.Context;
import android.content.IContentProvider;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
@@ -47,9 +45,7 @@ import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.os.RemoteException;
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.Settings.Global;
@@ -80,8 +76,6 @@ public class TileUtilsTest {
    @Mock
    private UserManager mUserManager;
    @Mock
    private IContentProvider mIContentProvider;
    @Mock
    private ContentResolver mContentResolver;

    private static final String URI_GET_SUMMARY = "content://authority/text/summary";
@@ -285,45 +279,6 @@ public class TileUtilsTest {
        assertThat(outTiles.get(0).isIconTintable(mContext)).isTrue();
    }

    @Test
    public void getTilesForIntent_shouldNotProcessInvalidUriContentSystemApp()
            throws RemoteException {
        Intent intent = new Intent();
        Map<Pair<String, String>, Tile> addedCache = new ArrayMap<>();
        List<Tile> outTiles = new ArrayList<>();
        List<ResolveInfo> info = new ArrayList<>();
        ResolveInfo resolveInfo = newInfo(true, null /* category */, null, null, URI_GET_SUMMARY);
        info.add(resolveInfo);

        when(mPackageManager.queryIntentActivitiesAsUser(eq(intent), anyInt(), anyInt()))
                .thenReturn(info);

        // Case 1: No provider associated with the uri specified.
        TileUtils.getTilesForIntent(mContext, UserHandle.CURRENT, intent, addedCache,
                null /* defaultCategory */, outTiles, false /* usePriority */);

        assertThat(outTiles.size()).isEqualTo(1);
        assertThat(outTiles.get(0).getIcon(mContext).getResId()).isEqualTo(314159);
        assertThat(outTiles.get(0).summary).isEqualTo("static-summary");

        // Case 2: Empty bundle.
        Bundle bundle = new Bundle();
        when(mIContentProvider.call(anyString(),
                eq(TileUtils.getMethodFromUri(Uri.parse(URI_GET_SUMMARY))), eq(URI_GET_SUMMARY),
                any())).thenReturn(bundle);
        when(mContentResolver.acquireUnstableProvider(anyString()))
                .thenReturn(mIContentProvider);
        when(mContentResolver.acquireUnstableProvider(any(Uri.class)))
                .thenReturn(mIContentProvider);

        TileUtils.getTilesForIntent(mContext, UserHandle.CURRENT, intent, addedCache,
                null /* defaultCategory */, outTiles, false /* usePriority */);

        assertThat(outTiles.size()).isEqualTo(1);
        assertThat(outTiles.get(0).getIcon(mContext).getResId()).isEqualTo(314159);
        assertThat(outTiles.get(0).summary).isEqualTo("static-summary");
    }

    @Test
    public void getTilesForIntent_shouldProcessUriContentForSystemApp() {
        Intent intent = new Intent();