From 58db8a1f00acccd35cdc51f891e3c825393bcd80 Mon Sep 17 00:00:00 2001 From: Peter Serwylo Date: Mon, 22 Sep 2014 22:06:49 +0930 Subject: [PATCH] Correct handling of back button presses during swap workflow. When electing to "Swap Apps" from the main menu, and a LocalRepoService is already running, then it jumps straight to the wifi QR fragment. However, it didn't play nice when pressing the "back" button. This is now fixed, by manually recreating the backstack in this situation. It also fires up the NFC push message if NFC exists. --- res/menu/swap_skip.xml | 2 +- .../fdroid/views/swap/JoinWifiFragment.java | 18 +++ .../fdroid/views/swap/NfcSwapFragment.java | 18 +++ .../fdroid/views/swap/SelectAppsFragment.java | 18 +++ .../fdroid/views/swap/SwapActivity.java | 120 +++++------------- 5 files changed, 87 insertions(+), 89 deletions(-) diff --git a/res/menu/swap_skip.xml b/res/menu/swap_skip.xml index 8e5289def..8483ff835 100644 --- a/res/menu/swap_skip.xml +++ b/res/menu/swap_skip.xml @@ -1,7 +1,7 @@ diff --git a/src/org/fdroid/fdroid/views/swap/JoinWifiFragment.java b/src/org/fdroid/fdroid/views/swap/JoinWifiFragment.java index 9070b482c..f09750f9f 100644 --- a/src/org/fdroid/fdroid/views/swap/JoinWifiFragment.java +++ b/src/org/fdroid/fdroid/views/swap/JoinWifiFragment.java @@ -9,8 +9,12 @@ import android.net.wifi.WifiManager; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.content.LocalBroadcastManager; +import android.support.v4.view.MenuItemCompat; import android.text.TextUtils; import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; @@ -27,6 +31,20 @@ public class JoinWifiFragment extends Fragment { } }; + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setHasOptionsMenu(true); + } + + @Override + public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) { + menuInflater.inflate(R.menu.swap_next, menu); + MenuItem nextMenuItem = menu.findItem(R.id.action_next); + int flags = MenuItemCompat.SHOW_AS_ACTION_ALWAYS | MenuItemCompat.SHOW_AS_ACTION_WITH_TEXT; + MenuItemCompat.setShowAsAction(nextMenuItem, flags); + } + @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View joinWifiView = inflater.inflate(R.layout.swap_join_wifi, container, false); diff --git a/src/org/fdroid/fdroid/views/swap/NfcSwapFragment.java b/src/org/fdroid/fdroid/views/swap/NfcSwapFragment.java index 768cd234e..01167d534 100644 --- a/src/org/fdroid/fdroid/views/swap/NfcSwapFragment.java +++ b/src/org/fdroid/fdroid/views/swap/NfcSwapFragment.java @@ -2,7 +2,11 @@ package org.fdroid.fdroid.views.swap; import android.os.Bundle; import android.support.v4.app.Fragment; +import android.support.v4.view.MenuItemCompat; import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.CheckBox; @@ -12,6 +16,20 @@ import org.fdroid.fdroid.R; public class NfcSwapFragment extends Fragment { + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setHasOptionsMenu(true); + } + + @Override + public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) { + menuInflater.inflate(R.menu.swap_skip, menu); + MenuItem nextMenuItem = menu.findItem(R.id.action_next); + int flags = MenuItemCompat.SHOW_AS_ACTION_ALWAYS | MenuItemCompat.SHOW_AS_ACTION_WITH_TEXT; + MenuItemCompat.setShowAsAction(nextMenuItem, flags); + } + @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.swap_nfc, container, false); diff --git a/src/org/fdroid/fdroid/views/swap/SelectAppsFragment.java b/src/org/fdroid/fdroid/views/swap/SelectAppsFragment.java index ee9fc7da6..22d445727 100644 --- a/src/org/fdroid/fdroid/views/swap/SelectAppsFragment.java +++ b/src/org/fdroid/fdroid/views/swap/SelectAppsFragment.java @@ -8,10 +8,14 @@ import android.os.Bundle; import android.support.v4.app.LoaderManager; import android.support.v4.content.CursorLoader; import android.support.v4.content.Loader; +import android.support.v4.view.MenuItemCompat; import android.support.v4.widget.SimpleCursorAdapter; import android.text.TextUtils; import android.view.ActionMode; import android.view.ContextThemeWrapper; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; import android.view.View; import android.widget.ImageView; import android.widget.LinearLayout; @@ -40,6 +44,20 @@ public class SelectAppsFragment extends ThemeableListFragment return FDroidApp.selectedApps; } + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setHasOptionsMenu(true); + } + + @Override + public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) { + menuInflater.inflate(R.menu.swap_next, menu); + MenuItem nextMenuItem = menu.findItem(R.id.action_next); + int flags = MenuItemCompat.SHOW_AS_ACTION_ALWAYS | MenuItemCompat.SHOW_AS_ACTION_WITH_TEXT; + MenuItemCompat.setShowAsAction(nextMenuItem, flags); + } + @Override public void onResume() { super.onResume(); diff --git a/src/org/fdroid/fdroid/views/swap/SwapActivity.java b/src/org/fdroid/fdroid/views/swap/SwapActivity.java index f9ba06dea..651d1c466 100644 --- a/src/org/fdroid/fdroid/views/swap/SwapActivity.java +++ b/src/org/fdroid/fdroid/views/swap/SwapActivity.java @@ -5,10 +5,9 @@ import android.content.Context; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; +import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; -import android.support.v4.view.MenuItemCompat; import android.support.v7.app.ActionBarActivity; -import android.view.Menu; import android.view.MenuItem; import android.widget.Toast; import org.fdroid.fdroid.FDroidApp; @@ -30,7 +29,6 @@ public class SwapActivity extends ActionBarActivity implements SwapProcessManage private static final String STATE_NFC = "nfc"; private static final String STATE_WIFI_QR = "wifiQr"; - private String nextMenuItemLabel; private Timer shutdownLocalRepoTimer; private UpdateAsyncTask updateSwappableAppsTask = null; private boolean hasPreparedLocalRepo = false; @@ -52,60 +50,21 @@ public class SwapActivity extends ActionBarActivity implements SwapProcessManage public void nextStep() { String current = currentState(); if (current.equals(STATE_START_SWAP)) { - onSelectApps(); + showSelectApps(); } else if (current.equals(STATE_SELECT_APPS)) { prepareLocalRepo(); } else if (current.equals(STATE_JOIN_WIFI)) { - startLocalRepo(); - onAttemptNfc(); + ensureLocalRepoRunning(); + if (!attemptToShowNfc()) { + showWifiQr(); + } } else if (current.equals(STATE_NFC)) { - onWifiQr(); + showWifiQr(); } else if (current.equals(STATE_WIFI_QR)) { } supportInvalidateOptionsMenu(); } - @Override - public boolean onCreateOptionsMenu(Menu menu) { - super.onCreateOptionsMenu(menu); - getMenuInflater().inflate(R.menu.swap_next, menu); - MenuItem nextMenuItem = menu.findItem(R.id.action_next); - nextMenuItem.setVisible(false); - MenuItemCompat.setShowAsAction(nextMenuItem, MenuItemCompat.SHOW_AS_ACTION_ALWAYS | MenuItemCompat.SHOW_AS_ACTION_WITH_TEXT); - return true; - } - - private void hideNextButton() { - nextMenuItemLabel = null; - supportInvalidateOptionsMenu(); - } - - private void showNextButton() { - nextMenuItemLabel = getString(R.string.next); - supportInvalidateOptionsMenu(); - } - - private void showSkipButton() { - nextMenuItemLabel = getString(R.string.skip); - supportInvalidateOptionsMenu(); - } - - @Override - public boolean onPrepareOptionsMenu(Menu menu) { - MenuItem nextMenuItem = menu.findItem(R.id.action_next); - if (nextMenuItemLabel == null) { - nextMenuItem.setTitle(""); - nextMenuItem.setTitleCondensed(""); - nextMenuItem.setVisible(false); - return true; - } else { - nextMenuItem.setVisible(true); - nextMenuItem.setTitle(nextMenuItemLabel); - nextMenuItem.setTitleCondensed(nextMenuItemLabel); - return true; - } - } - @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == R.id.action_next) { @@ -121,46 +80,32 @@ public class SwapActivity extends ActionBarActivity implements SwapProcessManage if (savedInstanceState == null) { + showFragment(new StartSwapFragment(), STATE_START_SWAP); + if (FDroidApp.isLocalRepoServiceRunning()) { - onWifiQr(); - } else { - - getSupportFragmentManager() - .beginTransaction() - .replace(android.R.id.content, new StartSwapFragment(), STATE_START_SWAP) - .addToBackStack(STATE_START_SWAP) - .commit(); - hideNextButton(); - + showSelectApps(); + showJoinWifi(); + attemptToShowNfc(); + showWifiQr(); } } } - private void onSelectApps() { + private void showSelectApps() { - getSupportFragmentManager() - .beginTransaction() - .replace(android.R.id.content, new SelectAppsFragment(), STATE_SELECT_APPS) - .addToBackStack(STATE_SELECT_APPS) - .commit(); - showNextButton(); + showFragment(new SelectAppsFragment(), STATE_SELECT_APPS); } - private void onJoinWifi() { + private void showJoinWifi() { - getSupportFragmentManager() - .beginTransaction() - .replace(android.R.id.content, new JoinWifiFragment(), STATE_JOIN_WIFI) - .addToBackStack(STATE_JOIN_WIFI) - .commit(); - showNextButton(); + showFragment(new JoinWifiFragment(), STATE_JOIN_WIFI); } - public void onAttemptNfc() { + private boolean attemptToShowNfc() { // TODO: What if NFC is disabled? Hook up with NfcNotEnabledActivity? Or maybe only if they // click a relevant button? @@ -170,28 +115,27 @@ public class SwapActivity extends ActionBarActivity implements SwapProcessManage boolean nfcMessageReady = NfcHelper.setPushMessage(this, Utils.getSharingUri(this, FDroidApp.repo)); if (Preferences.get().showNfcDuringSwap() && nfcMessageReady) { - getSupportFragmentManager() - .beginTransaction() - .replace(android.R.id.content, new NfcSwapFragment(), STATE_NFC) - .addToBackStack(STATE_NFC) - .commit(); - showSkipButton(); + showFragment(new NfcSwapFragment(), STATE_NFC); + return true; } else { - onWifiQr(); + return false; } } - public void onBluetooth() { + private void showBluetooth() { } - public void onWifiQr() { + private void showWifiQr() { + showFragment(new WifiQrFragment(), STATE_WIFI_QR); + } + + private void showFragment(Fragment fragment, String name) { getSupportFragmentManager() .beginTransaction() - .replace(android.R.id.content, new WifiQrFragment(), STATE_WIFI_QR) - .addToBackStack(STATE_WIFI_QR) + .replace(android.R.id.content, fragment, name) + .addToBackStack(name) .commit(); - hideNextButton(); } private void prepareLocalRepo() { @@ -201,7 +145,7 @@ public class SwapActivity extends ActionBarActivity implements SwapProcessManage updateSwappableAppsTask = new UpdateAsyncTask(this, fragment.getSelectedApps()); updateSwappableAppsTask.execute(); } else { - onJoinWifi(); + showJoinWifi(); } } @@ -213,11 +157,11 @@ public class SwapActivity extends ActionBarActivity implements SwapProcessManage updateSwappableAppsTask = null; hasPreparedLocalRepo = true; - onJoinWifi(); + showJoinWifi(); } - private void startLocalRepo() { + private void ensureLocalRepoRunning() { if (!FDroidApp.isLocalRepoServiceRunning()) { FDroidApp.startLocalRepoService(this); initLocalRepoTimer(900000); // 15 mins