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

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

Tie ContextualWifiSlice to UI instead of garbage collector

- Introduce a static long in SliceFeatureProvider that updates once
  every "session". A session is when user is in an UI (screen rotation,
  going to subpage, etc does not break the continuation of session).

- Use session token in ContextualWifiSlice to determine when to refresh,
  instead of relying on WeakHashMap from CustomSliceManager.
  WeakHashMap can be cleaned up at any time by gc so it doesn't match
  what we want on the UI.

- Also as a side fix, merged CustomSliceManager into
  SliceFeatureProvider.

Fixes: 123937830
Test: robo
Change-Id: I199bceceb208b99a32f3f08e624787b5a03e73a9
parent ad29500d
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -67,7 +67,8 @@ public class ContextualCardLoader extends AsyncLoaderCompat<List<ContextualCard>

    @VisibleForTesting
    Uri mNotifyUri;
    private Context mContext;

    private final Context mContext;

    ContextualCardLoader(Context context) {
        super(context);
+2 −2
Original line number Diff line number Diff line
@@ -109,13 +109,13 @@ public class ContextualCardManager implements ContextualCardLoader.CardContentLo
        }
    }

    void loadContextualCards(ContextualCardsFragment fragment) {
    void loadContextualCards(LoaderManager loaderManager) {
        mStartTime = System.currentTimeMillis();
        final CardContentLoaderCallbacks cardContentLoaderCallbacks =
                new CardContentLoaderCallbacks(mContext);
        cardContentLoaderCallbacks.setListener(this);
        // Use the cached data when navigating back to the first page and upon screen rotation.
        LoaderManager.getInstance(fragment).initLoader(CARD_CONTENT_LOADER_ID, null /* bundle */,
        loaderManager.initLoader(CARD_CONTENT_LOADER_ID, null /* bundle */,
                cardContentLoaderCallbacks);
    }

+10 −2
Original line number Diff line number Diff line
@@ -19,16 +19,19 @@ package com.android.settings.homepage.contextualcards;
import static com.android.settings.homepage.contextualcards.ContextualCardsAdapter.SPAN_COUNT;

import android.app.settings.SettingsEnums;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.loader.app.LoaderManager;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.settings.R;
import com.android.settings.core.InstrumentedFragment;
import com.android.settings.overlay.FeatureFactory;

public class ContextualCardsFragment extends InstrumentedFragment {

@@ -42,14 +45,19 @@ public class ContextualCardsFragment extends InstrumentedFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContextualCardManager = new ContextualCardManager(getContext(), getSettingsLifecycle(),
        final Context context = getContext();
        if (savedInstanceState == null) {
            FeatureFactory.getFactory(context).getSlicesFeatureProvider().newUiSession();
        }
        mContextualCardManager = new ContextualCardManager(context, getSettingsLifecycle(),
                savedInstanceState);

    }

    @Override
    public void onStart() {
        super.onStart();
        mContextualCardManager.loadContextualCards(this);
        mContextualCardManager.loadContextualCards(LoaderManager.getInstance(this));
    }

    @Override
+0 −60
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 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.settings.slices;

import android.content.Context;
import android.net.Uri;

import java.util.Map;
import java.util.WeakHashMap;

/**
 * Manages custom {@link androidx.slice.Slice Slices}, which are all Slices not backed by
 * preferences.
 */
public class CustomSliceManager {

    private final Context mContext;
    private final Map<Uri, CustomSliceable> mSliceableCache;

    public CustomSliceManager(Context context) {
        mContext = context.getApplicationContext();
        mSliceableCache = new WeakHashMap<>();
    }

    /**
     * Return a {@link CustomSliceable} associated to the Uri.
     * <p>
     * Do not change this method signature to accommodate for a special-case slicable - a context is
     * the only thing that should be needed to create the object.
     */
    public CustomSliceable getSliceableFromUri(Uri uri) {
        final Uri newUri = CustomSliceRegistry.removeParameterFromUri(uri);
        if (mSliceableCache.containsKey(newUri)) {
            return mSliceableCache.get(newUri);
        }

        final Class clazz = CustomSliceRegistry.getSliceClassByUri(newUri);
        if (clazz == null) {
            throw new IllegalArgumentException("No Slice found for uri: " + uri);
        }

        final CustomSliceable sliceable = CustomSliceable.createInstance(mContext, clazz);
        mSliceableCache.put(newUri, sliceable);
        return sliceable;
    }
}
+2 −2
Original line number Diff line number Diff line
@@ -331,7 +331,7 @@ public class CustomSliceRegistry {

    /**
     * Returns {@code true} if {@param uri} is a valid Slice Uri handled by
     * {@link CustomSliceManager}.
     * {@link CustomSliceRegistry}.
     */
    public static boolean isValidUri(Uri uri) {
        return sUriToSlice.containsKey(removeParameterFromUri(uri));
@@ -339,7 +339,7 @@ public class CustomSliceRegistry {

    /**
     * Returns {@code true} if {@param action} is a valid intent action handled by
     * {@link CustomSliceManager}.
     * {@link CustomSliceRegistry}.
     */
    public static boolean isValidAction(String action) {
        return isValidUri(Uri.parse(action));
Loading