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

Commit 6d7aa4e9 authored by Fan Zhang's avatar Fan Zhang
Browse files

Do not start suggestion loader if host isn't attached.

Change-Id: I1ab1de8f0bea6c66d8415e06aedcc7cbc6baf89c
Fixes: 68759380
Test: robotests
parent f60c99af
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.settings.dashboard;

import android.app.Activity;
import android.app.LoaderManager;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
@@ -95,6 +96,14 @@ public class DashboardSummary extends InstrumentedFragment
        }
    }

    @Override
    public LoaderManager getLoaderManager() {
        if (!isAdded()) {
            return null;
        }
        return super.getLoaderManager();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        long startTime = System.currentTimeMillis();
+9 −3
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import android.content.Context;
import android.content.Loader;
import android.os.Bundle;
import android.service.settings.suggestions.Suggestion;
import android.support.annotation.Nullable;
import android.util.Log;

import com.android.settings.overlay.FeatureFactory;
@@ -44,8 +45,10 @@ public class SuggestionControllerMixin implements SuggestionController.ServiceCo
        void onSuggestionReady(List<Suggestion> data);

        /**
         * Returns {@link LoaderManager} associated with the host.
         * Returns {@link LoaderManager} associated with the host. If host is not attached to
         * activity then return null.
         */
        @Nullable
        LoaderManager getLoaderManager();
    }

@@ -82,9 +85,12 @@ public class SuggestionControllerMixin implements SuggestionController.ServiceCo

    @Override
    public void onServiceConnected() {
        mHost.getLoaderManager().restartLoader(SuggestionLoader.LOADER_ID_SUGGESTIONS,
        final LoaderManager loaderManager = mHost.getLoaderManager();
        if (loaderManager != null) {
            loaderManager.restartLoader(SuggestionLoader.LOADER_ID_SUGGESTIONS,
                    null /* args */, this /* callback */);
        }
    }

    @Override
    public void onServiceDisconnected() {
+10 −1
Original line number Diff line number Diff line
@@ -23,7 +23,6 @@ import static org.mockito.Mockito.when;

import android.app.LoaderManager;
import android.content.Context;
import android.database.MatrixCursor;

import com.android.settings.TestConfig;
import com.android.settings.testutils.FakeFeatureFactory;
@@ -88,4 +87,14 @@ public class SuggestionControllerMixinTest {
        verify(loaderManager).restartLoader(SuggestionLoader.LOADER_ID_SUGGESTIONS,
                null /* args */, mMixin /* callback */);
    }

    @Test
    public void onServiceConnected_hostNotAttached_shouldDoNothing() {
        when(mHost.getLoaderManager()).thenReturn(null);

        mMixin = new SuggestionControllerMixin(mContext, mHost, mLifecycle);
        mMixin.onServiceConnected();

        verify(mHost).getLoaderManager();
    }
}