From 97994c5e43225fc4f696aa699eb84237a4669056 Mon Sep 17 00:00:00 2001 From: Peter Serwylo Date: Sat, 23 May 2015 00:55:39 +1000 Subject: [PATCH] WIP: Refactored NFC fragment to view. --- F-Droid/res/layout/swap_nfc.xml | 4 +- .../views/swap/SwapWorkflowActivity.java | 61 ++++++++++++++++- .../fdroid/views/swap/views/NfcView.java | 65 +++++++++++++++++++ 3 files changed, 125 insertions(+), 5 deletions(-) create mode 100644 F-Droid/src/org/fdroid/fdroid/views/swap/views/NfcView.java diff --git a/F-Droid/res/layout/swap_nfc.xml b/F-Droid/res/layout/swap_nfc.xml index eb4ad525c..f58ba7fc7 100644 --- a/F-Droid/res/layout/swap_nfc.xml +++ b/F-Droid/res/layout/swap_nfc.xml @@ -1,6 +1,6 @@ - - \ No newline at end of file + \ No newline at end of file diff --git a/F-Droid/src/org/fdroid/fdroid/views/swap/SwapWorkflowActivity.java b/F-Droid/src/org/fdroid/fdroid/views/swap/SwapWorkflowActivity.java index 90acca56f..8580e1b2d 100644 --- a/F-Droid/src/org/fdroid/fdroid/views/swap/SwapWorkflowActivity.java +++ b/F-Droid/src/org/fdroid/fdroid/views/swap/SwapWorkflowActivity.java @@ -16,11 +16,15 @@ import android.view.ViewGroup; import android.widget.Toast; import org.fdroid.fdroid.FDroidApp; +import org.fdroid.fdroid.NfcHelper; +import org.fdroid.fdroid.Preferences; import org.fdroid.fdroid.R; import org.fdroid.fdroid.Utils; import org.fdroid.fdroid.localrepo.LocalRepoManager; import java.util.Set; +import java.util.Timer; +import java.util.TimerTask; public class SwapWorkflowActivity extends FragmentActivity { @@ -39,6 +43,7 @@ public class SwapWorkflowActivity extends FragmentActivity { private InnerView currentView; private boolean hasPreparedLocalRepo = false; private UpdateAsyncTask updateSwappableAppsTask = null; + private Timer shutdownLocalRepoTimer; @Override protected void onCreate(Bundle savedInstanceState) { @@ -108,10 +113,60 @@ public class SwapWorkflowActivity extends FragmentActivity { } public void onJoinWifiComplete() { - /*ensureLocalRepoRunning(); + ensureLocalRepoRunning(); if (!attemptToShowNfc()) { - showWifiQr(); - }*/ + // showWifiQr(); + } + } + + private boolean attemptToShowNfc() { + // TODO: What if NFC is disabled? Hook up with NfcNotEnabledActivity? Or maybe only if they + // click a relevant button? + + // Even if they opted to skip the message which says "Touch devices to swap", + // we still want to actually enable the feature, so that they could touch + // during the wifi qr code being shown too. + boolean nfcMessageReady = NfcHelper.setPushMessage(this, Utils.getSharingUri(FDroidApp.repo)); + + if (Preferences.get().showNfcDuringSwap() && nfcMessageReady) { + inflateInnerView(R.layout.swap_nfc); + return true; + } + return false; + } + + private void ensureLocalRepoRunning() { + if (!FDroidApp.isLocalRepoServiceRunning()) { + FDroidApp.startLocalRepoService(this); + initLocalRepoTimer(900000); // 15 mins + } + } + + private void initLocalRepoTimer(long timeoutMilliseconds) { + + // reset the timer if viewing this Activity again + if (shutdownLocalRepoTimer != null) + shutdownLocalRepoTimer.cancel(); + + // automatically turn off after 15 minutes + shutdownLocalRepoTimer = new Timer(); + shutdownLocalRepoTimer.schedule(new TimerTask() { + @Override + public void run() { + FDroidApp.stopLocalRepoService(SwapWorkflowActivity.this); + } + }, timeoutMilliseconds); + + } + + public void stopSwapping() { + if (FDroidApp.isLocalRepoServiceRunning()) { + if (shutdownLocalRepoTimer != null) { + shutdownLocalRepoTimer.cancel(); + } + FDroidApp.stopLocalRepoService(SwapWorkflowActivity.this); + } + finish(); } class UpdateAsyncTask extends AsyncTask { diff --git a/F-Droid/src/org/fdroid/fdroid/views/swap/views/NfcView.java b/F-Droid/src/org/fdroid/fdroid/views/swap/views/NfcView.java new file mode 100644 index 000000000..8faf61da3 --- /dev/null +++ b/F-Droid/src/org/fdroid/fdroid/views/swap/views/NfcView.java @@ -0,0 +1,65 @@ +package org.fdroid.fdroid.views.swap.views; + +import android.annotation.TargetApi; +import android.content.Context; +import android.os.Build; +import android.support.annotation.NonNull; +import android.support.v4.view.MenuItemCompat; +import android.util.AttributeSet; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; +import android.widget.CheckBox; +import android.widget.CompoundButton; +import android.widget.RelativeLayout; + +import org.fdroid.fdroid.Preferences; +import org.fdroid.fdroid.R; +import org.fdroid.fdroid.views.swap.SwapWorkflowActivity; + +public class NfcView extends RelativeLayout implements SwapWorkflowActivity.InnerView { + + public NfcView(Context context) { + super(context); + } + + public NfcView(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public NfcView(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + } + + @TargetApi(Build.VERSION_CODES.LOLLIPOP) + public NfcView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { + super(context, attrs, defStyleAttr, defStyleRes); + } + + @Override + protected void onFinishInflate() { + super.onFinishInflate(); + CheckBox dontShowAgain = (CheckBox)findViewById(R.id.checkbox_dont_show); + dontShowAgain.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + Preferences.get().setShowNfcDuringSwap(!isChecked); + } + }); + } + + @Override + public boolean buildMenu(Menu menu, @NonNull MenuInflater inflater) { + inflater.inflate(R.menu.swap_skip, menu); + MenuItem next = menu.findItem(R.id.action_next); + MenuItemCompat.setShowAsAction(next, MenuItemCompat.SHOW_AS_ACTION_ALWAYS | MenuItemCompat.SHOW_AS_ACTION_WITH_TEXT); + next.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { + @Override + public boolean onMenuItemClick(MenuItem item) { + // TODO: Show QR Code. + return true; + } + }); + return true; + } +}