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.settingslib.suggestions;
     18 
     19 import android.app.LoaderManager;
     20 import android.arch.lifecycle.OnLifecycleEvent;
     21 import android.content.ComponentName;
     22 import android.content.Context;
     23 import android.content.Loader;
     24 import android.os.Bundle;
     25 import android.service.settings.suggestions.Suggestion;
     26 import android.support.annotation.Nullable;
     27 import android.util.Log;
     28 
     29 import com.android.settingslib.core.lifecycle.Lifecycle;
     30 
     31 import java.util.List;
     32 
     33 /**
     34  * Manages IPC communication to SettingsIntelligence for suggestion related services.
     35  */
     36 public class SuggestionControllerMixin implements SuggestionController.ServiceConnectionListener,
     37         android.arch.lifecycle.LifecycleObserver, LoaderManager.LoaderCallbacks<List<Suggestion>> {
     38 
     39     public interface SuggestionControllerHost {
     40         /**
     41          * Called when suggestion data fetching is ready.
     42          */
     43         void onSuggestionReady(List<Suggestion> data);
     44 
     45         /**
     46          * Returns {@link LoaderManager} associated with the host. If host is not attached to
     47          * activity then return null.
     48          */
     49         @Nullable
     50         LoaderManager getLoaderManager();
     51     }
     52 
     53     private static final String TAG = "SuggestionCtrlMixin";
     54     private static final boolean DEBUG = false;
     55 
     56     private final Context mContext;
     57     private final SuggestionController mSuggestionController;
     58     private final SuggestionControllerHost mHost;
     59 
     60     private boolean mSuggestionLoaded;
     61 
     62     public SuggestionControllerMixin(Context context, SuggestionControllerHost host,
     63             Lifecycle lifecycle, ComponentName componentName) {
     64         mContext = context.getApplicationContext();
     65         mHost = host;
     66         mSuggestionController = new SuggestionController(mContext, componentName,
     67                     this /* serviceConnectionListener */);
     68         if (lifecycle != null) {
     69             lifecycle.addObserver(this);
     70         }
     71     }
     72 
     73     @OnLifecycleEvent(Lifecycle.Event.ON_START)
     74     public void onStart() {
     75         if (DEBUG) {
     76             Log.d(TAG, "SuggestionController started");
     77         }
     78         mSuggestionController.start();
     79     }
     80 
     81     @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
     82     public void onStop() {
     83         if (DEBUG) {
     84             Log.d(TAG, "SuggestionController stopped.");
     85         }
     86         mSuggestionController.stop();
     87     }
     88 
     89     @Override
     90     public void onServiceConnected() {
     91         final LoaderManager loaderManager = mHost.getLoaderManager();
     92         if (loaderManager != null) {
     93             loaderManager.restartLoader(SuggestionLoader.LOADER_ID_SUGGESTIONS,
     94                     null /* args */, this /* callback */);
     95         }
     96     }
     97 
     98     @Override
     99     public void onServiceDisconnected() {
    100         if (DEBUG) {
    101             Log.d(TAG, "SuggestionService disconnected");
    102         }
    103         final LoaderManager loaderManager = mHost.getLoaderManager();
    104         if (loaderManager != null) {
    105             loaderManager.destroyLoader(SuggestionLoader.LOADER_ID_SUGGESTIONS);
    106         }
    107     }
    108 
    109     @Override
    110     public Loader<List<Suggestion>> onCreateLoader(int id, Bundle args) {
    111         if (id == SuggestionLoader.LOADER_ID_SUGGESTIONS) {
    112             mSuggestionLoaded = false;
    113             return new SuggestionLoader(mContext, mSuggestionController);
    114         }
    115         throw new IllegalArgumentException("This loader id is not supported " + id);
    116     }
    117 
    118     @Override
    119     public void onLoadFinished(Loader<List<Suggestion>> loader, List<Suggestion> data) {
    120         mSuggestionLoaded = true;
    121         mHost.onSuggestionReady(data);
    122     }
    123 
    124     @Override
    125     public void onLoaderReset(Loader<List<Suggestion>> loader) {
    126         mSuggestionLoaded = false;
    127     }
    128 
    129     public boolean isSuggestionLoaded() {
    130         return mSuggestionLoaded;
    131     }
    132 
    133     public void dismissSuggestion(Suggestion suggestion) {
    134         mSuggestionController.dismissSuggestions(suggestion);
    135     }
    136 
    137     public void launchSuggestion(Suggestion suggestion) {
    138         mSuggestionController.launchSuggestion(suggestion);
    139     }
    140 }
    141