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