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:
parent
6b27568ac4
commit
6d006e70b3
@ -1327,10 +1327,10 @@ public class AppDetails extends ActionBarActivity implements ProgressListener, A
|
|||||||
TextView statusView = (TextView) view.findViewById(R.id.status);
|
TextView statusView = (TextView) view.findViewById(R.id.status);
|
||||||
if (getApp().isInstalled()) {
|
if (getApp().isInstalled()) {
|
||||||
statusView.setText(getString(R.string.details_installed, getApp().installedVersionName));
|
statusView.setText(getString(R.string.details_installed, getApp().installedVersionName));
|
||||||
NfcBeamManager.setAndroidBeam(getActivity(), getApp().id);
|
NfcHelper.setAndroidBeam(getActivity(), getApp().id);
|
||||||
} else {
|
} else {
|
||||||
statusView.setText(getString(R.string.details_notinstalled));
|
statusView.setText(getString(R.string.details_notinstalled));
|
||||||
NfcBeamManager.disableAndroidBeam(getActivity());
|
NfcHelper.disableAndroidBeam(getActivity());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,6 @@
|
|||||||
|
|
||||||
package org.fdroid.fdroid;
|
package org.fdroid.fdroid;
|
||||||
|
|
||||||
import android.app.Activity;
|
|
||||||
import android.app.AlertDialog;
|
import android.app.AlertDialog;
|
||||||
import android.app.AlertDialog.Builder;
|
import android.app.AlertDialog.Builder;
|
||||||
import android.app.NotificationManager;
|
import android.app.NotificationManager;
|
||||||
@ -112,7 +111,7 @@ public class FDroid extends ActionBarActivity {
|
|||||||
protected void onResume() {
|
protected void onResume() {
|
||||||
super.onResume();
|
super.onResume();
|
||||||
// AppDetails and RepoDetailsActivity set different NFC actions, so reset here
|
// AppDetails and RepoDetailsActivity set different NFC actions, so reset here
|
||||||
NfcBeamManager.setAndroidBeam(this, getApplication().getPackageName());
|
NfcHelper.setAndroidBeam(this, getApplication().getPackageName());
|
||||||
checkForAddRepoIntent();
|
checkForAddRepoIntent();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,21 +3,44 @@ package org.fdroid.fdroid;
|
|||||||
|
|
||||||
import android.annotation.TargetApi;
|
import android.annotation.TargetApi;
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
|
import android.content.Context;
|
||||||
import android.content.pm.ApplicationInfo;
|
import android.content.pm.ApplicationInfo;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.content.pm.PackageManager.NameNotFoundException;
|
import android.content.pm.PackageManager.NameNotFoundException;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
|
import android.nfc.NdefMessage;
|
||||||
|
import android.nfc.NdefRecord;
|
||||||
import android.nfc.NfcAdapter;
|
import android.nfc.NfcAdapter;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
|
|
||||||
@TargetApi(16)
|
public class NfcHelper {
|
||||||
public class NfcBeamManager {
|
|
||||||
|
|
||||||
|
@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) {
|
static void setAndroidBeam(Activity activity, String packageName) {
|
||||||
if (Build.VERSION.SDK_INT < 16)
|
if (Build.VERSION.SDK_INT < 16)
|
||||||
return;
|
return;
|
||||||
PackageManager pm = activity.getPackageManager();
|
PackageManager pm = activity.getPackageManager();
|
||||||
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(activity);
|
NfcAdapter nfcAdapter = getAdapter(activity);
|
||||||
if (nfcAdapter != null) {
|
if (nfcAdapter != null) {
|
||||||
ApplicationInfo appInfo;
|
ApplicationInfo appInfo;
|
||||||
try {
|
try {
|
||||||
@ -32,10 +55,11 @@ public class NfcBeamManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TargetApi(16)
|
||||||
static void disableAndroidBeam(Activity activity) {
|
static void disableAndroidBeam(Activity activity) {
|
||||||
if (Build.VERSION.SDK_INT < 16)
|
if (Build.VERSION.SDK_INT < 16)
|
||||||
return;
|
return;
|
||||||
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(activity);
|
NfcAdapter nfcAdapter = getAdapter(activity);
|
||||||
if (nfcAdapter != null)
|
if (nfcAdapter != null)
|
||||||
nfcAdapter.setBeamPushUris(null, activity);
|
nfcAdapter.setBeamPushUris(null, activity);
|
||||||
}
|
}
|
@ -5,7 +5,6 @@ import android.annotation.TargetApi;
|
|||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.nfc.NdefMessage;
|
import android.nfc.NdefMessage;
|
||||||
import android.nfc.NdefRecord;
|
|
||||||
import android.nfc.NfcAdapter;
|
import android.nfc.NfcAdapter;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
@ -17,6 +16,7 @@ import android.view.MenuItem;
|
|||||||
import android.widget.LinearLayout;
|
import android.widget.LinearLayout;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
import org.fdroid.fdroid.FDroidApp;
|
import org.fdroid.fdroid.FDroidApp;
|
||||||
|
import org.fdroid.fdroid.NfcHelper;
|
||||||
import org.fdroid.fdroid.Utils;
|
import org.fdroid.fdroid.Utils;
|
||||||
import org.fdroid.fdroid.data.Repo;
|
import org.fdroid.fdroid.data.Repo;
|
||||||
import org.fdroid.fdroid.data.RepoProvider;
|
import org.fdroid.fdroid.data.RepoProvider;
|
||||||
@ -67,27 +67,18 @@ public class RepoDetailsActivity extends ActionBarActivity {
|
|||||||
|
|
||||||
@TargetApi(14)
|
@TargetApi(14)
|
||||||
private void setNfc() {
|
private void setNfc() {
|
||||||
if (Build.VERSION.SDK_INT < 14)
|
if (NfcHelper.setPushMessage(this, Utils.getSharingUri(this, repo))) {
|
||||||
return;
|
findViewById(android.R.id.content).post(new Runnable() {
|
||||||
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
|
@Override
|
||||||
if (nfcAdapter == null) {
|
public void run() {
|
||||||
return;
|
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
|
@Override
|
||||||
public void onResume() {
|
public void onResume() {
|
||||||
Log.i(TAG, "onResume");
|
|
||||||
super.onResume();
|
super.onResume();
|
||||||
// FDroid.java and AppDetails set different NFC actions, so reset here
|
// FDroid.java and AppDetails set different NFC actions, so reset here
|
||||||
setNfc();
|
setNfc();
|
||||||
@ -96,9 +87,6 @@ public class RepoDetailsActivity extends ActionBarActivity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onNewIntent(Intent i) {
|
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
|
// onResume gets called after this to handle the intent
|
||||||
setIntent(i);
|
setIntent(i);
|
||||||
}
|
}
|
||||||
@ -108,7 +96,6 @@ public class RepoDetailsActivity extends ActionBarActivity {
|
|||||||
if (Build.VERSION.SDK_INT < 9)
|
if (Build.VERSION.SDK_INT < 9)
|
||||||
return;
|
return;
|
||||||
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(i.getAction())) {
|
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(i.getAction())) {
|
||||||
Log.i(TAG, "ACTION_NDEF_DISCOVERED");
|
|
||||||
Parcelable[] rawMsgs =
|
Parcelable[] rawMsgs =
|
||||||
i.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
|
i.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
|
||||||
NdefMessage msg = (NdefMessage) rawMsgs[0];
|
NdefMessage msg = (NdefMessage) rawMsgs[0];
|
||||||
|
@ -1,11 +1,5 @@
|
|||||||
package org.fdroid.fdroid.views.swap;
|
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.os.Bundle;
|
||||||
import android.support.v4.app.Fragment;
|
import android.support.v4.app.Fragment;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
@ -13,10 +7,8 @@ import android.view.View;
|
|||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.CheckBox;
|
import android.widget.CheckBox;
|
||||||
import android.widget.CompoundButton;
|
import android.widget.CompoundButton;
|
||||||
import org.fdroid.fdroid.FDroidApp;
|
|
||||||
import org.fdroid.fdroid.Preferences;
|
import org.fdroid.fdroid.Preferences;
|
||||||
import org.fdroid.fdroid.R;
|
import org.fdroid.fdroid.R;
|
||||||
import org.fdroid.fdroid.Utils;
|
|
||||||
|
|
||||||
public class NfcSwapFragment extends Fragment {
|
public class NfcSwapFragment extends Fragment {
|
||||||
|
|
||||||
@ -30,32 +22,7 @@ public class NfcSwapFragment extends Fragment {
|
|||||||
Preferences.get().setShowNfcDuringSwap(!isChecked);
|
Preferences.get().setShowNfcDuringSwap(!isChecked);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
setupNfc();
|
|
||||||
|
|
||||||
return view;
|
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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,7 @@ import android.view.Menu;
|
|||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
import org.fdroid.fdroid.FDroidApp;
|
import org.fdroid.fdroid.FDroidApp;
|
||||||
|
import org.fdroid.fdroid.NfcHelper;
|
||||||
import org.fdroid.fdroid.Preferences;
|
import org.fdroid.fdroid.Preferences;
|
||||||
import org.fdroid.fdroid.R;
|
import org.fdroid.fdroid.R;
|
||||||
import org.fdroid.fdroid.Utils;
|
import org.fdroid.fdroid.Utils;
|
||||||
@ -146,7 +147,15 @@ public class SwapActivity extends ActionBarActivity implements SwapProcessManage
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void onAttemptNfc() {
|
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()
|
getSupportFragmentManager()
|
||||||
.beginTransaction()
|
.beginTransaction()
|
||||||
.replace(android.R.id.content, new NfcSwapFragment(), STATE_NFC)
|
.replace(android.R.id.content, new NfcSwapFragment(), STATE_NFC)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user