summaryrefslogtreecommitdiffstats
path: root/src/com/android/settings/location/RecentLocationRequestPreferenceController.java
blob: f4ae15bdc54691cbbdaef10db6519eb98a8eda70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * Copyright (C) 2017 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.location;

import android.content.Context;
import android.os.Bundle;
import android.os.UserHandle;
import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.applications.appinfo.AppInfoDashboardFragment;
import com.android.settings.core.SubSettingLauncher;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.widget.AppPreference;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.location.RecentLocationApps;
import java.util.List;

public class RecentLocationRequestPreferenceController extends LocationBasePreferenceController {

    /** Key for preference category "Recent location requests" */
    private static final String KEY_RECENT_LOCATION_REQUESTS = "recent_location_requests";
    @VisibleForTesting
    static final String KEY_SEE_ALL_BUTTON = "recent_location_requests_see_all_button";
    private final LocationSettings mFragment;
    private final RecentLocationApps mRecentLocationApps;
    private PreferenceCategory mCategoryRecentLocationRequests;
    private Preference mSeeAllButton;

    /** Used in this class and {@link RecentLocationRequestSeeAllPreferenceController}*/
    static class PackageEntryClickedListener implements Preference.OnPreferenceClickListener {
        private final DashboardFragment mFragment;
        private final String mPackage;
        private final UserHandle mUserHandle;

        public PackageEntryClickedListener(DashboardFragment fragment, String packageName,
                UserHandle userHandle) {
            mFragment = fragment;
            mPackage = packageName;
            mUserHandle = userHandle;
        }

        @Override
        public boolean onPreferenceClick(Preference preference) {
            // start new fragment to display extended information
            final Bundle args = new Bundle();
            args.putString(AppInfoDashboardFragment.ARG_PACKAGE_NAME, mPackage);

            new SubSettingLauncher(mFragment.getContext())
                    .setDestination(AppInfoDashboardFragment.class.getName())
                    .setArguments(args)
                    .setTitle(R.string.application_info_label)
                    .setUserHandle(mUserHandle)
                    .setSourceMetricsCategory(mFragment.getMetricsCategory())
                    .launch();
            return true;
        }
    }

    public RecentLocationRequestPreferenceController(Context context, LocationSettings fragment,
            Lifecycle lifecycle) {
        this(context, fragment, lifecycle, new RecentLocationApps(context));
    }

    @VisibleForTesting
    RecentLocationRequestPreferenceController(Context context, LocationSettings fragment,
            Lifecycle lifecycle, RecentLocationApps recentApps) {
        super(context, lifecycle);
        mFragment = fragment;
        mRecentLocationApps = recentApps;
    }

    @Override
    public String getPreferenceKey() {
        return KEY_RECENT_LOCATION_REQUESTS;
    }

    @Override
    public void displayPreference(PreferenceScreen screen) {
        super.displayPreference(screen);
        mCategoryRecentLocationRequests =
                (PreferenceCategory) screen.findPreference(KEY_RECENT_LOCATION_REQUESTS);
        mSeeAllButton = screen.findPreference(KEY_SEE_ALL_BUTTON);

    }

    @Override
    public void updateState(Preference preference) {
        mCategoryRecentLocationRequests.removeAll();
        mSeeAllButton.setVisible(false);

        final Context prefContext = preference.getContext();
        final List<RecentLocationApps.Request> recentLocationRequests =
                mRecentLocationApps.getAppListSorted();

        if (recentLocationRequests.size() > 3) {
            // Display the top 3 preferences to container in original order.
            for (int i = 0; i < 3; i ++) {
                mCategoryRecentLocationRequests.addPreference(
                        createAppPreference(prefContext, recentLocationRequests.get(i)));
            }
            // Display a button to list all requests
            mSeeAllButton.setVisible(true);
        } else if (recentLocationRequests.size() > 0) {
            // Add preferences to container in original order (already sorted by recency).
            for (RecentLocationApps.Request request : recentLocationRequests) {
                mCategoryRecentLocationRequests.addPreference(
                        createAppPreference(prefContext, request));
            }
        } else {
            // If there's no item to display, add a "No recent apps" item.
            final Preference banner = createAppPreference(prefContext);
            banner.setTitle(R.string.location_no_recent_apps);
            banner.setSelectable(false);
            mCategoryRecentLocationRequests.addPreference(banner);
        }
    }

    @Override
    public void onLocationModeChanged(int mode, boolean restricted) {
        mCategoryRecentLocationRequests.setEnabled(mLocationEnabler.isEnabled(mode));
    }

    @VisibleForTesting
    AppPreference createAppPreference(Context prefContext) {
        return new AppPreference(prefContext);
    }

    @VisibleForTesting
    AppPreference createAppPreference(Context prefContext, RecentLocationApps.Request request) {
        final AppPreference pref = createAppPreference(prefContext);
        pref.setSummary(request.contentDescription);
        pref.setIcon(request.icon);
        pref.setTitle(request.label);
        pref.setOnPreferenceClickListener(new PackageEntryClickedListener(
                mFragment, request.packageName, request.userHandle));
        return pref;
    }
}