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