Skip to content

Commit

Permalink
Add safety checks when showing dialog to prevent illegal state
Browse files Browse the repository at this point in the history
  • Loading branch information
lloydtorres committed Oct 30, 2016
1 parent 3dee3e6 commit adf2b72
Show file tree
Hide file tree
Showing 14 changed files with 112 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -377,10 +377,12 @@ public boolean onOptionsItemSelected(MenuItem item) {
finish();
return true;
case R.id.nav_dataset:
FragmentManager fm = getSupportFragmentManager();
DatasetDialog dialog = new DatasetDialog();
dialog.setDatasets(WORLD_CENSUS_ITEMS, id);
dialog.show(fm, DatasetDialog.DIALOG_TAG);
if (!isFinishing()) {
FragmentManager fm = getSupportFragmentManager();
DatasetDialog dialog = new DatasetDialog();
dialog.setDatasets(WORLD_CENSUS_ITEMS, id);
dialog.show(fm, DatasetDialog.DIALOG_TAG);
}
return true;
}
return super.onOptionsItemSelected(item);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,11 +322,13 @@ public void onClick(DialogInterface dialog, int which) {
}
};

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this, RaraHelper.getThemeMaterialDialog(this));
dialogBuilder.setTitle(R.string.exit_confirm)
.setPositiveButton(R.string.exit, dialogListener)
.setNegativeButton(R.string.explore_negative, null)
.show();
if (!isFinishing()) {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this, RaraHelper.getThemeMaterialDialog(this));
dialogBuilder.setTitle(R.string.exit_confirm)
.setPositiveButton(R.string.exit, dialogListener)
.setNegativeButton(R.string.explore_negative, null)
.show();
}
}

@Override
Expand Down Expand Up @@ -504,6 +506,10 @@ private boolean isNoSelect(int key) {
* Start exploration dialog
*/
private void explore() {
if (isFinishing()) {
return;
}

FragmentManager fm = getSupportFragmentManager();
ExploreDialog exploreDialog = new ExploreDialog();
exploreDialog.show(fm, ExploreDialog.DIALOG_TAG);
Expand All @@ -513,6 +519,10 @@ private void explore() {
* Start switch nation dialog.
*/
private void switchNation() {
if (isFinishing()) {
return;
}

List<UserLogin> logins = UserLogin.listAll(UserLogin.class);
// If no other nations besides current one, show warning dialog
// with link to login activity
Expand Down Expand Up @@ -552,6 +562,10 @@ private void startSettings() {
* Start logout process
*/
private void logout() {
if (isFinishing()) {
return;
}

DialogInterface.OnClickListener dialogListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,10 @@ private void handleEndorsement() {
return;
}

if (isFinishing()) {
return;
}

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this, RaraHelper.getThemeMaterialDialog(this));
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
Expand Down Expand Up @@ -615,6 +619,10 @@ private void handleDossier() {
return;
}

if (isFinishing()) {
return;
}

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this, RaraHelper.getThemeMaterialDialog(this));
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
Expand Down Expand Up @@ -709,6 +717,10 @@ public void handleRegionMove() {
return;
}

if (isFinishing()) {
return;
}

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this, RaraHelper.getThemeMaterialDialog(this));
LayoutInflater inflater = getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.fragment_dialog_move_password, null);
Expand Down Expand Up @@ -807,6 +819,10 @@ public void onErrorResponse(VolleyError error) {
* Opens the explore dialog.
*/
private void openExploreDialog(boolean closeOnFinish) {
if (isFinishing()) {
return;
}

ExploreDialog exploreDialog = new ExploreDialog();

if (closeOnFinish) {
Expand Down Expand Up @@ -866,6 +882,10 @@ public void onErrorResponse(VolleyError error) {
* Call to show the superweapon dialog.
*/
public void showSuperweaponDialog() {
if (isFinishing()) {
return;
}

// Only show dialog if at least one superweapon is available
if (superweaponStatus != null &&
(superweaponStatus.isTZES || superweaponStatus.isCure || superweaponStatus.isHorde)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,9 +435,11 @@ public boolean onOptionsItemSelected(MenuItem item) {
showRegionDossier(fm);
return true;
case R.id.nav_subscriptions:
SubscriptionsDialog subscriptionsDialog = new SubscriptionsDialog();
subscriptionsDialog.setCallback(this);
subscriptionsDialog.show(fm, SubscriptionsDialog.DIALOG_TAG);
if (getActivity() != null && isAdded()) {
SubscriptionsDialog subscriptionsDialog = new SubscriptionsDialog();
subscriptionsDialog.setCallback(this);
subscriptionsDialog.show(fm, SubscriptionsDialog.DIALOG_TAG);
}
return true;
}
return super.onOptionsItemSelected(item);
Expand All @@ -448,6 +450,10 @@ public boolean onOptionsItemSelected(MenuItem item) {
* @param fm Fragment Manager
*/
private void showNationDossier(FragmentManager fm) {
if (getActivity() == null || !isAdded()) {
return;
}

if (dossierNations.size() > 0) {
Collections.sort(dossierNations);
NameListDialog nameListDialog = new NameListDialog();
Expand All @@ -469,6 +475,10 @@ private void showNationDossier(FragmentManager fm) {
* @param fm Fragment Manager
*/
private void showRegionDossier(FragmentManager fm) {
if (getActivity() == null || !isAdded()) {
return;
}

if (dossierRegions.size() > 0) {
Collections.sort(dossierRegions);
NameListDialog nameListDialog = new NameListDialog();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,9 @@ public void onClick(DialogInterface dialog, int which) {
break;
}

dialogBuilder.show();
if (!isFinishing()) {
dialogBuilder.show();
}
}
else {
startPostAdoptPosition(option);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,10 @@ private void executeRequest(NSStringRequest stringRequest, String lockedMessage)
* @param v
*/
public void startCreateNation(View v) {
if (isFinishing()) {
return;
}

DialogInterface.OnClickListener dialogListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Expand Down Expand Up @@ -399,6 +403,10 @@ private void continueToVerifyAccount(UserLogin u) {

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (isFinishing()) {
return;
}

if (requestCode == WebRegisterActivity.REGISTER_RESULT && resultCode == Activity.RESULT_OK) {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this, RaraHelper.getThemeMaterialDialog(this));
dialogBuilder.setTitle(R.string.create_nation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,10 @@ public void onErrorResponse(VolleyError error) {
* @param id
*/
public void confirmDelete(final int pos, final int id) {
if (isFinishing()) {
return;
}

DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ public void onErrorResponse(VolleyError error) {
* @param pollData Data for the target poll.
*/
public void showPollVoteDialog(Poll pollData) {
if (getActivity() == null || !isAdded()) {
return;
}

PollVoteDialog voteDialog = new PollVoteDialog();
voteDialog.setData(this, pollData);
voteDialog.show(getFragmentManager(), PollVoteDialog.DIALOG_TAG);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ private void setToolbar(Toolbar t) {
* Wrapper for sending the report to NS servers.
*/
private void startSendReport() {
if (isFinishing()) {
return;
}

String reportText = reportContent.getText().toString();
if (reportText.length() <= 0) {
SparkleHelper.makeSnackbar(view, getString(R.string.report_blank));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ public void onDestroy() {
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
switch (key) {
case SETTING_CRASHREPORT:
if (isFinishing()) {
return;
}
dialogBuilder.setMessage(getString(R.string.warn_crashreport)).setPositiveButton(getString(R.string.got_it), null).show();
break;
case SETTING_NOTIFS:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,10 @@ private void refreshRecycler(int direction) {
* @param fm
*/
private void showFoldersDialog(FragmentManager fm) {
if (getActivity() == null || !isAdded()) {
return;
}

FoldersDialog foldersDialog = new FoldersDialog();
foldersDialog.setFragment(this);
foldersDialog.setFolders(folders);
Expand Down Expand Up @@ -418,6 +422,10 @@ private void invalidateTelegram(int id) {
* Wrappers to call on NS to archive a telegram.
*/
public void showArchiveTelegramDialog(final int id) {
if (getActivity() == null || !isAdded()) {
return;
}

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getContext(), RaraHelper.getThemeMaterialDialog(getContext()));
dialogBuilder
.setTitle(getString(R.string.telegrams_archive_confirm))
Expand Down Expand Up @@ -449,6 +457,10 @@ private void archiveTelegram(final int id) {
}

public void showMoveTelegramDialog(final int id) {
if (getActivity() == null || !isAdded()) {
return;
}

ArrayList<TelegramFolder> moveableFolders = new ArrayList<TelegramFolder>();
for (int i=0; i<folders.size(); i++) {
String name = folders.get(i).name;
Expand Down Expand Up @@ -551,6 +563,10 @@ public void onErrorResponse(VolleyError error) {
}

public void showDeleteTelegramDialog(final int id) {
if (getActivity() == null || !isAdded()) {
return;
}

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getContext(), RaraHelper.getThemeMaterialDialog(getContext()));
dialogBuilder
.setTitle(getString(R.string.telegrams_delete_confirm))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,10 @@ public void onErrorResponse(VolleyError error) {
* @param vote Current choice in voting
*/
public void showVoteDialog(int vote) {
if (isFinishing()) {
return;
}

FragmentManager fm = getSupportFragmentManager();
VoteDialog voteDialog = new VoteDialog();
voteDialog.setChoice(vote);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,10 @@ public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_explore:
ExploreDialog exploreDialog = new ExploreDialog();
exploreDialog.show(getFragmentManager(), ExploreDialog.DIALOG_TAG);
if (getActivity() != null && isAdded()) {
ExploreDialog exploreDialog = new ExploreDialog();
exploreDialog.show(getFragmentManager(), ExploreDialog.DIALOG_TAG);
}
return true;
}
return super.onOptionsItemSelected(item);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,10 @@ private void initRecycler() {
* Either shows the zombie decision dialog or a message saying that no actions are available.
*/
public void showDecisionDialog() {
if (isFinishing()) {
return;
}

ZombieDecisionDialog zombieDialog = new ZombieDecisionDialog();
zombieDialog.setZombieData(userData.zombieData);
zombieDialog.show(getSupportFragmentManager(), VoteDialog.DIALOG_TAG);
Expand Down

0 comments on commit adf2b72

Please sign in to comment.