SetupWizard: Fix race conditions with async callbacks

Change-Id: Ifeeec3ac74b4644bb26b9f90d0159a64b173785b
This commit is contained in:
cretin45 2015-02-23 15:58:21 -08:00
parent d7e70040b7
commit d385c7e906
4 changed files with 87 additions and 14 deletions

View File

@ -33,11 +33,17 @@ public abstract class AbstractSetupData extends BroadcastReceiver implements Set
private int mCurrentPageIndex = 0; private int mCurrentPageIndex = 0;
private OnResumeRunnable mOnResumeRunnable;
public AbstractSetupData(SetupWizardActivity context) { public AbstractSetupData(SetupWizardActivity context) {
mContext = context; mContext = context;
mPageList = onNewPageList(); mPageList = onNewPageList();
} }
public void setContext(SetupWizardActivity context) {
mContext = context;
}
protected abstract PageList onNewPageList(); protected abstract PageList onNewPageList();
@Override @Override
@ -75,6 +81,12 @@ public abstract class AbstractSetupData extends BroadcastReceiver implements Set
return mPageList.getPage(mCurrentPageIndex); return mPageList.getPage(mCurrentPageIndex);
} }
@Override
public boolean isCurrentPage(Page page) {
if (page == null) return false;
return page.getKey().equals(getCurrentPage().getKey());
}
public boolean isFirstPage() { public boolean isFirstPage() {
return mCurrentPageIndex == 0; return mCurrentPageIndex == 0;
} }
@ -85,6 +97,9 @@ public abstract class AbstractSetupData extends BroadcastReceiver implements Set
@Override @Override
public void onNextPage() { public void onNextPage() {
Runnable runnable = new Runnable() {
@Override
public void run() {
if (getCurrentPage().doNextAction() == false) { if (getCurrentPage().doNextAction() == false) {
if (advanceToNextUnhidden()) { if (advanceToNextUnhidden()) {
for (int i = 0; i < mListeners.size(); i++) { for (int i = 0; i < mListeners.size(); i++) {
@ -93,9 +108,15 @@ public abstract class AbstractSetupData extends BroadcastReceiver implements Set
} }
} }
} }
};
doPreviousNext(runnable);
}
@Override @Override
public void onPreviousPage() { public void onPreviousPage() {
Runnable runnable = new Runnable() {
@Override
public void run() {
if (getCurrentPage().doPreviousAction() == false) { if (getCurrentPage().doPreviousAction() == false) {
if (advanceToPreviousUnhidden()) { if (advanceToPreviousUnhidden()) {
for (int i = 0; i < mListeners.size(); i++) { for (int i = 0; i < mListeners.size(); i++) {
@ -104,6 +125,9 @@ public abstract class AbstractSetupData extends BroadcastReceiver implements Set
} }
} }
} }
};
doPreviousNext(runnable);
}
private boolean advanceToNextUnhidden() { private boolean advanceToNextUnhidden() {
while (mCurrentPageIndex < mPageList.size()) { while (mCurrentPageIndex < mPageList.size()) {
@ -134,6 +158,24 @@ public abstract class AbstractSetupData extends BroadcastReceiver implements Set
} }
} }
private void doPreviousNext(Runnable runnable) {
if (mContext.isResumed()) {
runnable.run();
} else {
mOnResumeRunnable = new OnResumeRunnable(runnable, this);
}
}
public void onDestroy() {
mOnResumeRunnable = null;
}
public void onResume() {
if (mOnResumeRunnable != null) {
mOnResumeRunnable.run();
}
}
public void finishPages() { public void finishPages() {
for (Page page : mPageList.values()) { for (Page page : mPageList.values()) {
page.onFinishSetup(); page.onFinishSetup();
@ -155,4 +197,21 @@ public abstract class AbstractSetupData extends BroadcastReceiver implements Set
public void unregisterListener(SetupDataCallbacks listener) { public void unregisterListener(SetupDataCallbacks listener) {
mListeners.remove(listener); mListeners.remove(listener);
} }
private static class OnResumeRunnable implements Runnable {
private final AbstractSetupData mAbstractSetupData;
private final Runnable mRunnable;
private OnResumeRunnable(Runnable runnable, AbstractSetupData abstractSetupData) {
mAbstractSetupData = abstractSetupData;
mRunnable = runnable;
}
@Override
public void run() {
mRunnable.run();
mAbstractSetupData.mOnResumeRunnable = null;
}
}
} }

View File

@ -22,6 +22,7 @@ public interface SetupDataCallbacks {
void onPageLoaded(Page page); void onPageLoaded(Page page);
void onPageTreeChanged(); void onPageTreeChanged();
void onFinish(); void onFinish();
boolean isCurrentPage(Page page);
Page getPage(String key); Page getPage(String key);
Page getPage(int key); Page getPage(int key);
} }

View File

@ -59,7 +59,7 @@ public class WifiSetupPage extends SetupPage {
private Runnable mFinishCaptivePortalCheckRunnable = new Runnable() { private Runnable mFinishCaptivePortalCheckRunnable = new Runnable() {
@Override @Override
public void run() { public void run() {
final Activity activity = mContext; final SetupWizardActivity activity = mContext;
if (mIsCaptivePortal) { if (mIsCaptivePortal) {
try { try {
int netId = ConnectivityManager.from(activity) int netId = ConnectivityManager.from(activity)
@ -82,12 +82,16 @@ public class WifiSetupPage extends SetupPage {
} catch (Exception e) { } catch (Exception e) {
//Oh well //Oh well
Log.e(TAG, "No captive portal activity found" + e); Log.e(TAG, "No captive portal activity found" + e);
if (activity.isCurrentPage(WifiSetupPage.this)) {
getCallbacks().onNextPage(); getCallbacks().onNextPage();
} }
}
} else { } else {
if (activity.isCurrentPage(WifiSetupPage.this)) {
getCallbacks().onNextPage(); getCallbacks().onNextPage();
} }
} }
}
}; };
public WifiSetupPage(SetupWizardActivity context, SetupDataCallbacks callbacks) { public WifiSetupPage(SetupWizardActivity context, SetupDataCallbacks callbacks) {
@ -147,7 +151,7 @@ public class WifiSetupPage extends SetupPage {
} }
} else if (requestCode == SetupWizardApp.REQUEST_CODE_SETUP_CAPTIVE_PORTAL) { } else if (requestCode == SetupWizardApp.REQUEST_CODE_SETUP_CAPTIVE_PORTAL) {
if (resultCode == Activity.RESULT_CANCELED) { if (resultCode == Activity.RESULT_CANCELED) {
SetupWizardUtils.launchWifiSetup((Activity)mContext); SetupWizardUtils.launchWifiSetup(mContext);
} else { } else {
getCallbacks().onNextPage(); getCallbacks().onNextPage();
} }

View File

@ -69,6 +69,8 @@ public class SetupWizardActivity extends Activity implements SetupDataCallbacks
mSetupData = (CMSetupWizardData)getLastNonConfigurationInstance(); mSetupData = (CMSetupWizardData)getLastNonConfigurationInstance();
if (mSetupData == null) { if (mSetupData == null) {
mSetupData = new CMSetupWizardData(this); mSetupData = new CMSetupWizardData(this);
} else {
mSetupData.setContext(this);
} }
mNextButton = (Button) findViewById(R.id.next_button); mNextButton = (Button) findViewById(R.id.next_button);
mPrevButton = (Button) findViewById(R.id.prev_button); mPrevButton = (Button) findViewById(R.id.prev_button);
@ -128,6 +130,7 @@ public class SetupWizardActivity extends Activity implements SetupDataCallbacks
| View.SYSTEM_UI_FLAG_IMMERSIVE | View.SYSTEM_UI_FLAG_IMMERSIVE
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION); | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
super.onResume(); super.onResume();
mSetupData.onResume();
onPageTreeChanged(); onPageTreeChanged();
enableButtonBar(true); enableButtonBar(true);
} }
@ -135,6 +138,7 @@ public class SetupWizardActivity extends Activity implements SetupDataCallbacks
@Override @Override
protected void onDestroy() { protected void onDestroy() {
super.onDestroy(); super.onDestroy();
mSetupData.onDestroy();
mSetupData.unregisterListener(this); mSetupData.unregisterListener(this);
unregisterReceiver(mSetupData); unregisterReceiver(mSetupData);
} }
@ -236,6 +240,11 @@ public class SetupWizardActivity extends Activity implements SetupDataCallbacks
return mSetupData.getPage(key); return mSetupData.getPage(key);
} }
@Override
public boolean isCurrentPage(Page page) {
return mSetupData.isCurrentPage(page);
}
@Override @Override
public void onFinish() { public void onFinish() {
animateOut(); animateOut();