Home | History | Annotate | Download | only in location
      1 /*
      2  * Copyright 2018 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.settings.location;
     18 
     19 import android.content.Context;
     20 import android.support.annotation.VisibleForTesting;
     21 import android.support.v7.preference.Preference;
     22 import android.support.v7.preference.PreferenceCategory;
     23 import android.support.v7.preference.PreferenceScreen;
     24 import com.android.settings.widget.AppPreference;
     25 import com.android.settingslib.core.lifecycle.Lifecycle;
     26 import com.android.settingslib.location.RecentLocationApps;
     27 import java.util.List;
     28 
     29 /** Preference controller for preference category displaying all recent location requests. */
     30 public class RecentLocationRequestSeeAllPreferenceController
     31         extends LocationBasePreferenceController {
     32 
     33     /** Key for preference category "All recent location requests" */
     34     private static final String KEY_ALL_RECENT_LOCATION_REQUESTS = "all_recent_location_requests";
     35     private final RecentLocationRequestSeeAllFragment mFragment;
     36     private PreferenceCategory mCategoryAllRecentLocationRequests;
     37     private RecentLocationApps mRecentLocationApps;
     38 
     39     public RecentLocationRequestSeeAllPreferenceController(
     40             Context context, Lifecycle lifecycle, RecentLocationRequestSeeAllFragment fragment) {
     41         this(context, lifecycle, fragment, new RecentLocationApps(context));
     42     }
     43 
     44     @VisibleForTesting
     45     RecentLocationRequestSeeAllPreferenceController(
     46             Context context,
     47             Lifecycle lifecycle,
     48             RecentLocationRequestSeeAllFragment fragment,
     49             RecentLocationApps recentLocationApps) {
     50         super(context, lifecycle);
     51         mFragment = fragment;
     52         mRecentLocationApps = recentLocationApps;
     53     }
     54 
     55     @Override
     56     public String getPreferenceKey() {
     57         return KEY_ALL_RECENT_LOCATION_REQUESTS;
     58     }
     59 
     60     @Override
     61     public void onLocationModeChanged(int mode, boolean restricted) {
     62         mCategoryAllRecentLocationRequests.setEnabled(mLocationEnabler.isEnabled(mode));
     63     }
     64 
     65     @Override
     66     public void displayPreference(PreferenceScreen screen) {
     67         super.displayPreference(screen);
     68         mCategoryAllRecentLocationRequests =
     69                 (PreferenceCategory) screen.findPreference(KEY_ALL_RECENT_LOCATION_REQUESTS);
     70 
     71     }
     72 
     73     @Override
     74     public void updateState(Preference preference) {
     75         mCategoryAllRecentLocationRequests.removeAll();
     76         List<RecentLocationApps.Request> requests = mRecentLocationApps.getAppListSorted();
     77         for (RecentLocationApps.Request request : requests) {
     78             Preference appPreference = createAppPreference(preference.getContext(), request);
     79             mCategoryAllRecentLocationRequests.addPreference(appPreference);
     80         }
     81     }
     82 
     83     @VisibleForTesting
     84     AppPreference createAppPreference(
     85             Context prefContext, RecentLocationApps.Request request) {
     86         final AppPreference pref = new AppPreference(prefContext);
     87         pref.setSummary(request.contentDescription);
     88         pref.setIcon(request.icon);
     89         pref.setTitle(request.label);
     90         pref.setOnPreferenceClickListener(
     91                 new RecentLocationRequestPreferenceController.PackageEntryClickedListener(
     92                         mFragment, request.packageName, request.userHandle));
     93         return pref;
     94     }
     95 }
     96