1 /*
2  * Copyright (C) 2016 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 package com.android.settings.survey;
17 
18 import android.app.Activity;
19 
20 import androidx.fragment.app.Fragment;
21 
22 import com.android.settings.overlay.FeatureFactory;
23 import com.android.settings.overlay.SurveyFeatureProvider;
24 import com.android.settingslib.core.lifecycle.LifecycleObserver;
25 import com.android.settingslib.core.lifecycle.events.OnResume;
26 
27 /**
28  * attaches extra, survey related work to the onResume method of registered observable classes
29  * in settings. This allows new classes to automatically support settings provided the extend
30  * one of the relevant classes in com.android.settings.lifecycle.
31  */
32 public class SurveyMixin implements LifecycleObserver, OnResume {
33 
34     private String mName;
35     private Fragment mFragment;
36 
37     /**
38      * A mixin that attempts to perform survey related tasks right before onResume is called
39      * in a Settings PreferenceFragment. This will allow for remote updating and creation of
40      * surveys.
41      *
42      * @param fragment     The fragment that this mixin will be attached to.
43      * @param fragmentName The simple name of the fragment.
44      */
SurveyMixin(Fragment fragment, String fragmentName)45     public SurveyMixin(Fragment fragment, String fragmentName) {
46         mName = fragmentName;
47         mFragment = fragment;
48     }
49 
50     @Override
onResume()51     public void onResume() {
52         Activity activity = mFragment.getActivity();
53 
54         // guard against the activity not existing yet
55         if (activity != null) {
56             SurveyFeatureProvider provider =
57                     FeatureFactory.getFeatureFactory().getSurveyFeatureProvider(activity);
58             if (provider != null) {
59                 provider.sendActivityIfAvailable(mName);
60             }
61         }
62     }
63 }
64