diff --git a/res/values/strings.xml b/res/values/strings.xml index 3ded618fa..0895ad430 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -9,6 +9,7 @@ Version Edit Delete + Enable NFC Send… App cache Keep downloaded apk files on SD card Do not keep any apk files @@ -73,6 +74,8 @@ Please Wait Updating application list… Getting application from + NFC is not enabled! + Go to NFC Settings… Repository address Fingerprint (optional) diff --git a/src/org/fdroid/fdroid/NfcNotEnabledActivity.java b/src/org/fdroid/fdroid/NfcNotEnabledActivity.java new file mode 100644 index 000000000..711a17f97 --- /dev/null +++ b/src/org/fdroid/fdroid/NfcNotEnabledActivity.java @@ -0,0 +1,43 @@ + +package org.fdroid.fdroid; + +import android.annotation.TargetApi; +import android.app.Activity; +import android.content.Intent; +import android.nfc.NfcAdapter; +import android.os.Build; +import android.os.Bundle; +import android.provider.Settings; + +@TargetApi(14) +// aka Android 4.0 aka Ice Cream Sandwich +public class NfcNotEnabledActivity extends Activity { + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + final Intent intent = new Intent(); + if (Build.VERSION.SDK_INT >= 16) { + /* + * ACTION_NFC_SETTINGS was added in 4.1 aka Jelly Bean MR1 as a + * separate thing from ACTION_NFCSHARING_SETTINGS. It is now + * possible to have NFC enabled, but not "Android Beam", which is + * needed for NDEF. Therefore, we detect the current state of NFC, + * and steer the user accordingly. + */ + if (NfcAdapter.getDefaultAdapter(this).isEnabled()) + intent.setAction(Settings.ACTION_NFCSHARING_SETTINGS); + else + intent.setAction(Settings.ACTION_NFC_SETTINGS); + } else if (Build.VERSION.SDK_INT >= 14) { + // this API was added in 4.0 aka Ice Cream Sandwich + intent.setAction(Settings.ACTION_NFCSHARING_SETTINGS); + } else { + // no NFC support, so nothing to do here + finish(); + return; + } + startActivity(intent); + finish(); + } +} diff --git a/src/org/fdroid/fdroid/views/fragments/RepoDetailsFragment.java b/src/org/fdroid/fdroid/views/fragments/RepoDetailsFragment.java index d86807c0d..4e762d238 100644 --- a/src/org/fdroid/fdroid/views/fragments/RepoDetailsFragment.java +++ b/src/org/fdroid/fdroid/views/fragments/RepoDetailsFragment.java @@ -1,9 +1,13 @@ package org.fdroid.fdroid.views.fragments; +import android.annotation.TargetApi; import android.app.Activity; import android.app.AlertDialog; import android.content.ContentValues; import android.content.DialogInterface; +import android.content.Intent; +import android.nfc.NfcAdapter; +import android.os.Build; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.view.MenuItemCompat; @@ -50,6 +54,9 @@ public class RepoDetailsFragment extends Fragment { private static final int DELETE = 0; private static final int UPDATE = 1; + private static final int ENABLE_NFC = 2; + + private MenuItem enableNfc = null; // TODO: Currently initialised in onCreateView. Not sure if that is the // best way to go about this... @@ -244,18 +251,51 @@ public class RepoDetailsFragment extends Fragment { MenuItemCompat.setShowAsAction(delete, MenuItemCompat.SHOW_AS_ACTION_IF_ROOM | MenuItemCompat.SHOW_AS_ACTION_WITH_TEXT); + } + @Override + public void onPrepareOptionsMenu(Menu menu) { + if (Build.VERSION.SDK_INT >= 14) + prepareNfcMenuItems(menu); + } + + @TargetApi(16) + private void prepareNfcMenuItems(Menu menu) { + boolean needsEnableNfcMenuItem = false; + NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(getActivity()); + if (Build.VERSION.SDK_INT < 16) + needsEnableNfcMenuItem = !nfcAdapter.isEnabled(); + else + needsEnableNfcMenuItem = !nfcAdapter.isNdefPushEnabled(); + if (needsEnableNfcMenuItem) { + if (enableNfc != null) + return; // already created + enableNfc = menu.add(Menu.NONE, ENABLE_NFC, 0, R.string.enable_nfc_send); + enableNfc.setIcon(android.R.drawable.ic_menu_preferences); + MenuItemCompat.setShowAsAction(enableNfc, + MenuItemCompat.SHOW_AS_ACTION_IF_ROOM | + MenuItemCompat.SHOW_AS_ACTION_WITH_TEXT); + } else if (enableNfc != null) { + // remove the existing MenuItem since NFC is now enabled + menu.removeItem(enableNfc.getItemId()); + enableNfc = null; + } } @Override public boolean onOptionsItemSelected(MenuItem item) { - if (item.getItemId() == DELETE) { - promptForDelete(); - return true; - } else if (item.getItemId() == UPDATE) { - performUpdate(); - return true; + switch (item.getItemId()) { + case DELETE: + promptForDelete(); + return true; + case UPDATE: + performUpdate(); + return true; + case ENABLE_NFC: + Intent intent = new Intent(getActivity(), NfcNotEnabledActivity.class); + startActivity(intent); + return true; } return false;