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.app.Fragment; 20 import android.content.BroadcastReceiver; 21 import com.android.settings.overlay.FeatureFactory; 22 import com.android.settings.overlay.SurveyFeatureProvider; 23 import com.android.settingslib.core.lifecycle.LifecycleObserver; 24 import com.android.settingslib.core.lifecycle.events.OnPause; 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, OnPause { 33 34 private String mName; 35 private Fragment mFragment; 36 private BroadcastReceiver mReceiver; 37 38 /** 39 * A mixin that attempts to perform survey related tasks right before onResume is called 40 * in a Settings PreferenceFragment. This will allow for remote updating and creation of 41 * surveys. 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 or the feature being disabled 55 if (activity != null) { 56 SurveyFeatureProvider provider = 57 FeatureFactory.getFactory(activity).getSurveyFeatureProvider(activity); 58 if (provider != null) { 59 60 // Try to download a survey if there is none available, show the survey otherwise 61 String id = provider.getSurveyId(activity, mName); 62 if (provider.getSurveyExpirationDate(activity, id) <= -1) { 63 // register the receiver to show the survey on completion. 64 mReceiver = provider.createAndRegisterReceiver(activity); 65 provider.downloadSurvey(activity, id, null /* data */); 66 } else { 67 provider.showSurveyIfAvailable(activity, id); 68 } 69 } 70 } 71 } 72 73 @Override onPause()74 public void onPause() { 75 Activity activity = mFragment.getActivity(); 76 if (mReceiver != null && activity != null) { 77 SurveyFeatureProvider.unregisterReceiver(activity, mReceiver); 78 mReceiver = null; 79 } 80 } 81 } 82