Always attempt to push swap repo via NFC.

Previously, swap would only enable this if the user hadn't previously
said "Don't show NFC message again". However, we really want people
to be able to swap regardless of whether the actual UI message is shown
or not.

Refactored NfcBeamManager to NfcHelper, to allow extra utility methods
to live there. Specifically, the process of sending a URI over NFC.

Removed some superfluous debugging calls to Log.i().
This commit is contained in:
Peter Serwylo 2014-08-12 06:50:41 +09:30
parent 6b27568ac4
commit 6d006e70b3
6 changed files with 49 additions and 63 deletions

View File

@ -1327,10 +1327,10 @@ public class AppDetails extends ActionBarActivity implements ProgressListener, A
TextView statusView = (TextView) view.findViewById(R.id.status);
if (getApp().isInstalled()) {
statusView.setText(getString(R.string.details_installed, getApp().installedVersionName));
NfcBeamManager.setAndroidBeam(getActivity(), getApp().id);
NfcHelper.setAndroidBeam(getActivity(), getApp().id);
} else {
statusView.setText(getString(R.string.details_notinstalled));
NfcBeamManager.disableAndroidBeam(getActivity());
NfcHelper.disableAndroidBeam(getActivity());
}
}

View File

@ -19,7 +19,6 @@
package org.fdroid.fdroid;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.NotificationManager;
@ -112,7 +111,7 @@ public class FDroid extends ActionBarActivity {
protected void onResume() {
super.onResume();
// AppDetails and RepoDetailsActivity set different NFC actions, so reset here
NfcBeamManager.setAndroidBeam(this, getApplication().getPackageName());
NfcHelper.setAndroidBeam(this, getApplication().getPackageName());
checkForAddRepoIntent();
}

View File

@ -3,21 +3,44 @@ package org.fdroid.fdroid;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.os.Build;
@TargetApi(16)
public class NfcBeamManager {
public class NfcHelper {
@TargetApi(14)
private static NfcAdapter getAdapter(Context context) {
if (Build.VERSION.SDK_INT < 14)
return null;
return NfcAdapter.getDefaultAdapter(context.getApplicationContext());
}
@TargetApi(14)
public static boolean setPushMessage(Activity activity, Uri toShare) {
NfcAdapter adapter = getAdapter(activity);
if (adapter != null) {
adapter.setNdefPushMessage(new NdefMessage(new NdefRecord[]{
NdefRecord.createUri(toShare),
}), activity);
return true;
}
return false;
}
@TargetApi(16)
static void setAndroidBeam(Activity activity, String packageName) {
if (Build.VERSION.SDK_INT < 16)
return;
PackageManager pm = activity.getPackageManager();
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(activity);
NfcAdapter nfcAdapter = getAdapter(activity);
if (nfcAdapter != null) {
ApplicationInfo appInfo;
try {
@ -32,10 +55,11 @@ public class NfcBeamManager {
}
}
@TargetApi(16)
static void disableAndroidBeam(Activity activity) {
if (Build.VERSION.SDK_INT < 16)
return;
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(activity);
NfcAdapter nfcAdapter = getAdapter(activity);
if (nfcAdapter != null)
nfcAdapter.setBeamPushUris(null, activity);
}

View File

@ -5,7 +5,6 @@ import android.annotation.TargetApi;
import android.content.Intent;
import android.net.Uri;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.os.Build;
import android.os.Bundle;
@ -17,6 +16,7 @@ import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.Toast;
import org.fdroid.fdroid.FDroidApp;
import org.fdroid.fdroid.NfcHelper;
import org.fdroid.fdroid.Utils;
import org.fdroid.fdroid.data.Repo;
import org.fdroid.fdroid.data.RepoProvider;
@ -67,27 +67,18 @@ public class RepoDetailsActivity extends ActionBarActivity {
@TargetApi(14)
private void setNfc() {
if (Build.VERSION.SDK_INT < 14)
return;
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (nfcAdapter == null) {
return;
if (NfcHelper.setPushMessage(this, Utils.getSharingUri(this, repo))) {
findViewById(android.R.id.content).post(new Runnable() {
@Override
public void run() {
onNewIntent(getIntent());
}
});
}
nfcAdapter.setNdefPushMessage(new NdefMessage(new NdefRecord[] {
NdefRecord.createUri(Utils.getSharingUri(this, repo)),
}), this);
findViewById(android.R.id.content).post(new Runnable() {
@Override
public void run() {
Log.i(TAG, "Runnable.run()");
onNewIntent(getIntent());
}
});
}
@Override
public void onResume() {
Log.i(TAG, "onResume");
super.onResume();
// FDroid.java and AppDetails set different NFC actions, so reset here
setNfc();
@ -96,9 +87,6 @@ public class RepoDetailsActivity extends ActionBarActivity {
@Override
public void onNewIntent(Intent i) {
Log.i(TAG, "onNewIntent");
Log.i(TAG, "action: " + i.getAction());
Log.i(TAG, "data: " + i.getData());
// onResume gets called after this to handle the intent
setIntent(i);
}
@ -108,7 +96,6 @@ public class RepoDetailsActivity extends ActionBarActivity {
if (Build.VERSION.SDK_INT < 9)
return;
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(i.getAction())) {
Log.i(TAG, "ACTION_NDEF_DISCOVERED");
Parcelable[] rawMsgs =
i.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefMessage msg = (NdefMessage) rawMsgs[0];

View File

@ -1,11 +1,5 @@
package org.fdroid.fdroid.views.swap;
import android.annotation.TargetApi;
import android.content.Context;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
@ -13,10 +7,8 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import org.fdroid.fdroid.FDroidApp;
import org.fdroid.fdroid.Preferences;
import org.fdroid.fdroid.R;
import org.fdroid.fdroid.Utils;
public class NfcSwapFragment extends Fragment {
@ -30,32 +22,7 @@ public class NfcSwapFragment extends Fragment {
Preferences.get().setShowNfcDuringSwap(!isChecked);
}
});
setupNfc();
return view;
}
public static boolean isNfcSupported(Context context) {
return Build.VERSION.SDK_INT >= 14 && getNfcAdapter(context) != null;
}
@TargetApi(10)
private static NfcAdapter getNfcAdapter(Context context) {
return NfcAdapter.getDefaultAdapter(context.getApplicationContext());
}
@TargetApi(10)
private void setupNfc() {
// the required NFC API was added in 4.0 aka Ice Cream Sandwich
if (Build.VERSION.SDK_INT >= 14) {
NfcAdapter nfcAdapter = getNfcAdapter(getActivity());
if (nfcAdapter == null)
return;
nfcAdapter.setNdefPushMessage(new NdefMessage(new NdefRecord[] {
NdefRecord.createUri(Utils.getSharingUri(getActivity(), FDroidApp.repo)),
}), getActivity());
}
}
}

View File

@ -12,6 +12,7 @@ import android.view.Menu;
import android.view.MenuItem;
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;
@ -146,7 +147,15 @@ public class SwapActivity extends ActionBarActivity implements SwapProcessManage
}
public void onAttemptNfc() {
if (Preferences.get().showNfcDuringSwap() && NfcSwapFragment.isNfcSupported(this)) {
// 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(this, FDroidApp.repo));
if (Preferences.get().showNfcDuringSwap() && nfcMessageReady) {
getSupportFragmentManager()
.beginTransaction()
.replace(android.R.id.content, new NfcSwapFragment(), STATE_NFC)