• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import android.content.BroadcastReceiver;
20 
21 import androidx.fragment.app.Fragment;
22 
23 import com.android.settings.overlay.FeatureFactory;
24 import com.android.settings.overlay.SurveyFeatureProvider;
25 import com.android.settingslib.core.lifecycle.LifecycleObserver;
26 import com.android.settingslib.core.lifecycle.events.OnPause;
27 import com.android.settingslib.core.lifecycle.events.OnResume;
28 
29 /**
30  * attaches extra, survey related work to the onResume method of registered observable classes
31  * in settings. This allows new classes to automatically support settings provided the extend
32  * one of the relevant classes in com.android.settings.lifecycle.
33  */
34 public class SurveyMixin implements LifecycleObserver, OnResume, OnPause {
35 
36     private String mName;
37     private Fragment mFragment;
38     private BroadcastReceiver mReceiver;
39 
40     /**
41      * A mixin that attempts to perform survey related tasks right before onResume is called
42      * in a Settings PreferenceFragment. This will allow for remote updating and creation of
43      * surveys.
44      * @param fragment The fragment that this mixin will be attached to.
45      * @param fragmentName The simple name of the fragment.
46      */
SurveyMixin(Fragment fragment, String fragmentName)47     public SurveyMixin(Fragment fragment, String fragmentName) {
48         mName = fragmentName;
49         mFragment = fragment;
50     }
51 
52     @Override
onResume()53     public void onResume() {
54         Activity activity = mFragment.getActivity();
55 
56         // guard against the activity not existing yet or the feature being disabled
57         if (activity != null) {
58             SurveyFeatureProvider provider =
59                     FeatureFactory.getFactory(activity).getSurveyFeatureProvider(activity);
60             if (provider != null) {
61 
62                 // Try to download a survey if there is none available, show the survey otherwise
63                 String id = provider.getSurveyId(activity, mName);
64                 if (provider.getSurveyExpirationDate(activity, id) <= -1) {
65                     // register the receiver to show the survey on completion.
66                     mReceiver = provider.createAndRegisterReceiver(activity);
67                     provider.downloadSurvey(activity, id, null /* data */);
68                 } else {
69                     provider.showSurveyIfAvailable(activity, id);
70                 }
71             }
72         }
73     }
74 
75     @Override
onPause()76     public void onPause() {
77         Activity activity = mFragment.getActivity();
78         if (mReceiver != null && activity != null) {
79             SurveyFeatureProvider.unregisterReceiver(activity, mReceiver);
80             mReceiver = null;
81         }
82     }
83 }
84