Home | History | Annotate | Download | only in suggestions
      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.intelligence.suggestions;
     18 
     19 import android.content.Context;
     20 import android.content.SharedPreferences;
     21 import android.service.settings.suggestions.Suggestion;
     22 
     23 import com.android.settings.intelligence.suggestions.ranking.SuggestionEventStore;
     24 import com.android.settings.intelligence.suggestions.ranking.SuggestionFeaturizer;
     25 import com.android.settings.intelligence.suggestions.ranking.SuggestionRanker;
     26 
     27 import java.util.List;
     28 
     29 public class SuggestionFeatureProvider {
     30 
     31     private static final String SHARED_PREF_FILENAME = "suggestions";
     32     private static final String IS_DISMISSED = "_is_dismissed";
     33 
     34     private SuggestionRanker mRanker;
     35 
     36     /**
     37      * Returns the {@link SharedPreferences} to store suggestion related user data.
     38      */
     39     public SharedPreferences getSharedPrefs(Context context) {
     40         return context.getApplicationContext()
     41                 .getSharedPreferences(SHARED_PREF_FILENAME, Context.MODE_PRIVATE);
     42     }
     43 
     44     /**
     45      * Returns a list of suggestions that UI should display, sorted based on importance.
     46      */
     47     public List<Suggestion> getSuggestions(Context context) {
     48         final SuggestionParser parser = new SuggestionParser(context);
     49         final List<Suggestion> list = parser.getSuggestions();
     50 
     51         final List<Suggestion> rankedSuggestions = getRanker(context).rankRelevantSuggestions(list);
     52 
     53         final SuggestionEventStore eventStore = SuggestionEventStore.get(context);
     54         for (Suggestion suggestion : rankedSuggestions) {
     55             eventStore.writeEvent(suggestion.getId(), SuggestionEventStore.EVENT_SHOWN);
     56         }
     57         return rankedSuggestions;
     58     }
     59 
     60     /**
     61      * Mark a suggestion as dismissed
     62      */
     63     public void markSuggestionDismissed(Context context, String id) {
     64         getSharedPrefs(context)
     65                 .edit()
     66                 .putBoolean(getDismissKey(id), true)
     67                 .apply();
     68 
     69         SuggestionEventStore.get(context).writeEvent(id, SuggestionEventStore.EVENT_DISMISSED);
     70     }
     71 
     72     /**
     73      * Mark a suggestion as not dismissed
     74      */
     75     public void markSuggestionNotDismissed(Context context, String id) {
     76         getSharedPrefs(context)
     77                 .edit()
     78                 .putBoolean(getDismissKey(id), false)
     79                 .apply();
     80     }
     81 
     82     public void markSuggestionLaunched(Context context, String id) {
     83         SuggestionEventStore.get(context).writeEvent(id, SuggestionEventStore.EVENT_CLICKED);
     84     }
     85 
     86     /**
     87      * Whether or not a suggestion is dismissed
     88      */
     89     public boolean isSuggestionDismissed(Context context, String id) {
     90         return getSharedPrefs(context)
     91                 .getBoolean(getDismissKey(id), false);
     92     }
     93 
     94     /**
     95      * Returns a manager that knows how to rank suggestions.
     96      */
     97     protected SuggestionRanker getRanker(Context context) {
     98         if (mRanker == null) {
     99             mRanker = new SuggestionRanker(context,
    100                     new SuggestionFeaturizer(context.getApplicationContext()));
    101         }
    102         return mRanker;
    103     }
    104 
    105     private static String getDismissKey(String id) {
    106         return id + IS_DISMISSED;
    107     }
    108 }
    109