OSDN Git Service

Revert "Revert "Filter and display top 3 suggestions for exclusive type""
[android-x86/packages-apps-Settings.git] / src / com / android / settings / dashboard / suggestions / SuggestionFeatureProviderImpl.java
1 /*
2  * Copyright (C) 2017 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.dashboard.suggestions;
18
19 import android.content.Context;
20 import android.content.pm.PackageManager;
21 import android.util.Log;
22
23 import com.android.internal.logging.nano.MetricsProto;
24 import com.android.settings.core.instrumentation.MetricsFeatureProvider;
25 import com.android.settings.overlay.FeatureFactory;
26 import com.android.settingslib.drawer.Tile;
27 import com.android.settingslib.suggestions.SuggestionParser;
28
29 import java.util.List;
30
31 public class SuggestionFeatureProviderImpl implements SuggestionFeatureProvider {
32
33     private static final String TAG = "SuggestionFeature";
34     private static final int EXCLUSIVE_SUGGESTION_MAX_COUNT = 3;
35
36     private final SuggestionRanker mSuggestionRanker;
37     private final MetricsFeatureProvider mMetricsFeatureProvider;
38
39     @Override
40     public boolean isSmartSuggestionEnabled(Context context) {
41         return false;
42     }
43
44     @Override
45     public boolean isPresent(String className) {
46         return false;
47     }
48
49     @Override
50     public boolean isSuggestionCompleted(Context context) {
51         return false;
52     }
53
54
55     public SuggestionFeatureProviderImpl(Context context) {
56         final Context appContext = context.getApplicationContext();
57         mSuggestionRanker = new SuggestionRanker(
58                 new SuggestionFeaturizer(new EventStore(appContext)));
59         mMetricsFeatureProvider = FeatureFactory.getFactory(appContext)
60                 .getMetricsFeatureProvider();
61     }
62
63     @Override
64     public void rankSuggestions(final List<Tile> suggestions, List<String> suggestionIds) {
65         mSuggestionRanker.rankSuggestions(suggestions, suggestionIds);
66     }
67
68     @Override
69     public void filterExclusiveSuggestions(List<Tile> suggestions) {
70         if (suggestions == null) {
71             return;
72         }
73         for (int i = suggestions.size() - 1; i >= EXCLUSIVE_SUGGESTION_MAX_COUNT; i--) {
74             Log.d(TAG, "Removing exclusive suggestion");
75             suggestions.remove(i);
76         }
77     }
78
79     @Override
80     public void dismissSuggestion(Context context, SuggestionParser parser, Tile suggestion) {
81         if (parser == null || suggestion == null || context == null) {
82             return;
83         }
84         mMetricsFeatureProvider.action(
85                 context, MetricsProto.MetricsEvent.ACTION_SETTINGS_DISMISS_SUGGESTION,
86                 getSuggestionIdentifier(context, suggestion));
87
88         final boolean isSmartSuggestionEnabled = isSmartSuggestionEnabled(context);
89         if (!parser.dismissSuggestion(suggestion, isSmartSuggestionEnabled)) {
90             return;
91         }
92         context.getPackageManager().setComponentEnabledSetting(
93                 suggestion.intent.getComponent(),
94                 PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
95                 PackageManager.DONT_KILL_APP);
96     }
97
98     @Override
99     public String getSuggestionIdentifier(Context context, Tile suggestion) {
100         if (suggestion.intent == null || suggestion.intent.getComponent() == null
101                 || context == null) {
102             return "unknown_suggestion";
103         }
104         String packageName = suggestion.intent.getComponent().getPackageName();
105         if (packageName.equals(context.getPackageName())) {
106             // Since Settings provides several suggestions, fill in the class instead of the
107             // package for these.
108             packageName = suggestion.intent.getComponent().getClassName();
109         }
110         return packageName;
111     }
112
113 }