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

Commit 826879ed authored by tmfang's avatar tmfang
Browse files

Create a preference controller for "App info"

We try to avoid managing too many preferences in a controller.
So, we create another controller to manage all apps info preference.

RecentAppsPreferenceController and AllAppsInfoPreferenceController
share same state of recent apps in order to improve the performance.

Test: visual, robo test
Fixes: 126134996
Change-Id: I1d8a175b213831415797437c64fd9d432864f9d3
parent cabe72cf
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -27,7 +27,8 @@
        android:key="all_app_info"
        android:title="@string/applications_settings"
        android:order="-999"
        android:fragment="com.android.settings.applications.manageapplications.ManageApplications"/>
        android:fragment="com.android.settings.applications.manageapplications.ManageApplications"
        settings:controller="com.android.settings.applications.AllAppsInfoPreferenceController"/>

    <com.android.settingslib.widget.LayoutPreference
        android:key="recent_open_apps"
+58 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 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.applications;

import android.app.usage.UsageStats;
import android.content.Context;

import androidx.preference.Preference;

import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;

import java.util.List;

public class AllAppsInfoPreferenceController extends BasePreferenceController {

    private List<UsageStats> mRecentApps;

    public AllAppsInfoPreferenceController(Context context, String key) {
        super(context, key);
    }

    public void setRecentApps(List<UsageStats> recentApps) {
        mRecentApps = recentApps;
    }

    @Override
    public int getAvailabilityStatus() {
        return mRecentApps == null || mRecentApps.isEmpty() ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
    }

    @Override
    public void updateState(Preference preference) {
        super.updateState(preference);
        // Show total number of installed apps as See all's summary.
        new InstalledAppCounter(mContext, InstalledAppCounter.IGNORE_INSTALL_REASON,
                mContext.getPackageManager()) {
            @Override
            protected void onCountComplete(int num) {
                preference.setSummary(mContext.getString(R.string.apps_summary, num));
            }
        }.execute();
    }
}
+24 −1
Original line number Diff line number Diff line
@@ -36,6 +36,10 @@ public class AppAndNotificationDashboardFragment extends DashboardFragment {

    private static final String TAG = "AppAndNotifDashboard";

    private boolean mIsFirstLaunch;
    private RecentAppsPreferenceController mRecentAppsPreferenceController;
    private AllAppsInfoPreferenceController mAllAppsInfoPreferenceController;

    @Override
    public int getMetricsCategory() {
        return SettingsEnums.SETTINGS_APP_NOTIF_CATEGORY;
@@ -61,7 +65,26 @@ public class AppAndNotificationDashboardFragment extends DashboardFragment {
        super.onAttach(context);

        use(SpecialAppAccessPreferenceController.class).setSession(getSettingsLifecycle());
        use(RecentAppsPreferenceController.class).setFragment(this /* fragment */);
        mRecentAppsPreferenceController = use(RecentAppsPreferenceController.class);
        mRecentAppsPreferenceController.setFragment(this /* fragment */);

        mAllAppsInfoPreferenceController = use(AllAppsInfoPreferenceController.class);
        mAllAppsInfoPreferenceController.setRecentApps(
                mRecentAppsPreferenceController.getRecentApps());

        mIsFirstLaunch = true;
    }

    @Override
    public void onResume() {
        if (!mIsFirstLaunch) {
            mRecentAppsPreferenceController.reloadData();
            mAllAppsInfoPreferenceController.setRecentApps(
                    mRecentAppsPreferenceController.getRecentApps());
        }

        super.onResume();
        mIsFirstLaunch = false;
    }

    @Override
+11 −34
Original line number Diff line number Diff line
@@ -66,8 +66,6 @@ import java.util.Set;
public class RecentAppsPreferenceController extends BasePreferenceController
        implements Comparator<UsageStats> {

    @VisibleForTesting
    static final String KEY_ALL_APP_INFO = "all_app_info";
    @VisibleForTesting
    static final String KEY_DIVIDER = "recent_apps_divider";

@@ -79,11 +77,7 @@ public class RecentAppsPreferenceController extends BasePreferenceController
    @VisibleForTesting
    LayoutPreference mRecentAppsPreference;
    @VisibleForTesting
    Preference mAllAppPref;
    @VisibleForTesting
    Preference mDivider;
    @VisibleForTesting
    boolean mIsFirstLaunch;

    private final PackageManager mPm;
    private final UsageStatsManager mUsageStatsManager;
@@ -119,7 +113,6 @@ public class RecentAppsPreferenceController extends BasePreferenceController
        mPowerManager = mContext.getSystemService(PowerManager.class);
        mUsageStatsManager = mContext.getSystemService(UsageStatsManager.class);
        mRecentApps = new ArrayList<>();
        mIsFirstLaunch = true;
        reloadData();
    }

@@ -129,14 +122,13 @@ public class RecentAppsPreferenceController extends BasePreferenceController

    @Override
    public int getAvailabilityStatus() {
        return mRecentApps.isEmpty() ? AVAILABLE_UNSEARCHABLE : AVAILABLE;
        return mRecentApps.isEmpty() ? CONDITIONALLY_UNAVAILABLE : AVAILABLE;
    }

    @Override
    public void displayPreference(PreferenceScreen screen) {
        super.displayPreference(screen);

        mAllAppPref = screen.findPreference(KEY_ALL_APP_INFO);
        mDivider = screen.findPreference(KEY_DIVIDER);
        mRecentAppsPreference = (LayoutPreference) screen.findPreference(getPreferenceKey());
        final View view = mRecentAppsPreference.findViewById(R.id.app_entities_header);
@@ -157,26 +149,18 @@ public class RecentAppsPreferenceController extends BasePreferenceController
    @Override
    public void updateState(Preference preference) {
        super.updateState(preference);
        // In order to improve launch time, we don't load data again at first launch.
        if (!mIsFirstLaunch) {
            reloadData();

        refreshUi();
        }
        // Show total number of installed apps as See all's summary.
        new InstalledAppCounter(mContext, InstalledAppCounter.IGNORE_INSTALL_REASON,
                mContext.getPackageManager()) {
            @Override
            protected void onCountComplete(int num) {
                if (mHasRecentApps) {
                mAppEntitiesController.setHeaderDetails(
                        mContext.getString(R.string.see_all_apps_title, num));
                mAppEntitiesController.apply();
                } else {
                    mAllAppPref.setSummary(mContext.getString(R.string.apps_summary, num));
                }
            }
        }.execute();
        mIsFirstLaunch = false;
    }

    @Override
@@ -185,14 +169,16 @@ public class RecentAppsPreferenceController extends BasePreferenceController
        return Long.compare(b.getLastTimeUsed(), a.getLastTimeUsed());
    }

    List<UsageStats> getRecentApps() {
        return mRecentApps;
    }

    @VisibleForTesting
    void refreshUi() {
        if (mRecentApps != null && !mRecentApps.isEmpty()) {
            mHasRecentApps = true;
            displayRecentApps();
        } else {
            mHasRecentApps = false;
            displayOnlyAppInfo();
            mDivider.setVisible(false);
        }
    }

@@ -209,13 +195,6 @@ public class RecentAppsPreferenceController extends BasePreferenceController
        updateDisplayableRecentAppList();
    }

    private void displayOnlyAppInfo() {
        mDivider.setVisible(false);
        mAllAppPref.setTitle(R.string.applications_settings);
        mAllAppPref.setVisible(true);
        mRecentAppsPreference.setVisible(false);
    }

    private void displayRecentApps() {
        int showAppsCount = 0;

@@ -230,8 +209,6 @@ public class RecentAppsPreferenceController extends BasePreferenceController
            }
        }
        mAppEntitiesController.apply();
        mRecentAppsPreference.setVisible(true);
        mAllAppPref.setVisible(false);
        mDivider.setVisible(true);
    }

+64 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 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.applications;

import static com.android.settings.core.BasePreferenceController.AVAILABLE;
import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE;

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

import android.app.usage.UsageStats;
import android.content.Context;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;

import java.util.ArrayList;
import java.util.List;

@RunWith(RobolectricTestRunner.class)
public class AllAppsInfoPreferenceControllerTest {

    private AllAppsInfoPreferenceController mController;

    @Before
    public void setUp() {
        final Context context = RuntimeEnvironment.application;
        mController = new AllAppsInfoPreferenceController(context, "test_key");
    }

    @Test
    public void getAvailabilityStatus_hasRecentApps_shouldReturnConditionallyUnavailable() {
        final List<UsageStats> stats = new ArrayList<>();
        final UsageStats stat1 = new UsageStats();
        stat1.mLastTimeUsed = System.currentTimeMillis();
        stat1.mPackageName = "pkg.class";
        stats.add(stat1);
        mController.setRecentApps(stats);

        assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
    }

    @Test
    public void getAvailabilityStatus_noRecentApps_shouldReturnAvailable() {
        // No data
        assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
    }
}
Loading