Merge "Update survey triggering mechanisms" into tm-qpr-dev

This commit is contained in:
Binyi Wu
2022-11-16 07:13:53 +00:00
committed by Android (Google) Code Review
3 changed files with 31 additions and 129 deletions

View File

@@ -26,14 +26,15 @@ import androidx.localbroadcastmanager.content.LocalBroadcastManager;
* An interface for classes wishing to provide the ability to serve surveys to implement.
*/
public interface SurveyFeatureProvider {
/**
* Downloads a survey asynchronously to shared preferences to be served at a later date.
*
* @param activity A valid context.
* @param surveyId A unique Id representing a survey to download.
* @param data a text blob to be attached to the survey results.
* @param data a text blob to be attached to the survey results.
* @deprecated This is not used after T.
*/
@Deprecated
void downloadSurvey(Activity activity, String surveyId, @Nullable String data);
/**
@@ -42,17 +43,21 @@ public interface SurveyFeatureProvider {
* @param activity The host activity to show the survey in.
* @param surveyId A unique Id representing a survey to download.
* @return A boolean indicating if a survey was shown or not.
* @deprecated This is not used after T.
*/
@Deprecated
boolean showSurveyIfAvailable(Activity activity, String surveyId);
/**
* A helper method to get the surveyId. Implementers should create a mapping of
* keys to surveyIds and provide them via this function.
*
* @param context A valid context.
* @param context A valid context.
* @param simpleKey The simple name of the key to get the surveyId for.
* @return The unique Id as a string or null on error.
* @deprecated This is not used after T.
*/
@Deprecated
String getSurveyId(Context context, String simpleKey);
/**
@@ -60,28 +65,36 @@ public interface SurveyFeatureProvider {
* unix timestamp) for the remaining survey should it exist and be ready to show. Returns -1 if
* no valid survey exists after removing the potentially expired one.
*
* @param context the calling context.
* @param context the calling context.
* @param surveyId the site ID.
* @return the unix timestamp for the available survey for the given {@coe siteId} or -1 if
* there is none available.
* @deprecated This is not used after T.
*/
@Deprecated
long getSurveyExpirationDate(Context context, String surveyId);
/**
* Registers an activity to show surveys/prompts as soon as they are downloaded. The receiver
* should be unregistered prior to destroying the activity to avoid undefined behavior by
* calling {@link #unregisterReceiver(Activity, BroadcastReceiver)}.
*
* @param activity The activity that should show surveys once they are downloaded.
* @return the broadcast receiver listening for survey downloads. Must be unregistered before
* leaving the activity.
* @deprecated This is not used after T.
*/
@Deprecated
BroadcastReceiver createAndRegisterReceiver(Activity activity);
/**
* Unregisters the broadcast receiver for this activity. Should only be called once per activity
* after a call to {@link #createAndRegisterReceiver(Activity)}.
*
* @param activity The activity that was used to register the BroadcastReceiver.
* @deprecated This is not used after T.
*/
@Deprecated
static void unregisterReceiver(Activity activity, BroadcastReceiver receiver) {
if (activity == null) {
throw new IllegalStateException("Cannot unregister receiver if activity is null");
@@ -89,4 +102,11 @@ public interface SurveyFeatureProvider {
LocalBroadcastManager.getInstance(activity).unregisterReceiver(receiver);
}
/**
* Send the visited activity to the place where it will trigger a survey if possible.
*
* @param simpleKey The simple name of the key to get the surveyId for.
*/
void sendActivityIfAvailable(String simpleKey);
}

View File

@@ -16,14 +16,12 @@
package com.android.settings.survey;
import android.app.Activity;
import android.content.BroadcastReceiver;
import androidx.fragment.app.Fragment;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.overlay.SurveyFeatureProvider;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnPause;
import com.android.settingslib.core.lifecycle.events.OnResume;
/**
@@ -31,17 +29,17 @@ import com.android.settingslib.core.lifecycle.events.OnResume;
* in settings. This allows new classes to automatically support settings provided the extend
* one of the relevant classes in com.android.settings.lifecycle.
*/
public class SurveyMixin implements LifecycleObserver, OnResume, OnPause {
public class SurveyMixin implements LifecycleObserver, OnResume {
private String mName;
private Fragment mFragment;
private BroadcastReceiver mReceiver;
/**
* A mixin that attempts to perform survey related tasks right before onResume is called
* in a Settings PreferenceFragment. This will allow for remote updating and creation of
* surveys.
* @param fragment The fragment that this mixin will be attached to.
*
* @param fragment The fragment that this mixin will be attached to.
* @param fragmentName The simple name of the fragment.
*/
public SurveyMixin(Fragment fragment, String fragmentName) {
@@ -53,31 +51,13 @@ public class SurveyMixin implements LifecycleObserver, OnResume, OnPause {
public void onResume() {
Activity activity = mFragment.getActivity();
// guard against the activity not existing yet or the feature being disabled
// guard against the activity not existing yet
if (activity != null) {
SurveyFeatureProvider provider =
FeatureFactory.getFactory(activity).getSurveyFeatureProvider(activity);
if (provider != null) {
// Try to download a survey if there is none available, show the survey otherwise
String id = provider.getSurveyId(activity, mName);
if (provider.getSurveyExpirationDate(activity, id) <= -1) {
// register the receiver to show the survey on completion.
mReceiver = provider.createAndRegisterReceiver(activity);
provider.downloadSurvey(activity, id, null /* data */);
} else {
provider.showSurveyIfAvailable(activity, id);
}
provider.sendActivityIfAvailable(mName);
}
}
}
@Override
public void onPause() {
Activity activity = mFragment.getActivity();
if (mReceiver != null && activity != null) {
SurveyFeatureProvider.unregisterReceiver(activity, mReceiver);
mReceiver = null;
}
}
}