From 8d32a01bd24b8a0d1a98f0581b3394a78fab8242 Mon Sep 17 00:00:00 2001 From: Nick Pelly Date: Tue, 9 Aug 2011 07:03:49 -0700 Subject: [PATCH] Add NFC to airplane mode NFC now observes airplanes mode, and is by default toggleable in airplane mode. Change-Id: I5f19d35422c9eb1cffb0b5e0631b3f3c5eeaedcf --- api/current.txt | 1 + core/java/android/nfc/INfcAdapter.aidl | 4 +- core/java/android/nfc/NfcAdapter.java | 100 ++++++++++++++---- core/java/android/provider/Settings.java | 5 + .../SettingsProvider/res/values/defaults.xml | 4 +- .../providers/settings/DatabaseHelper.java | 21 +++- 6 files changed, 108 insertions(+), 27 deletions(-) diff --git a/api/current.txt b/api/current.txt index 72ba55148a311..6b224d2d08719 100644 --- a/api/current.txt +++ b/api/current.txt @@ -17032,6 +17032,7 @@ package android.provider { field public static final deprecated java.lang.String PARENTAL_CONTROL_REDIRECT_URL = "parental_control_redirect_url"; field public static final java.lang.String RADIO_BLUETOOTH = "bluetooth"; field public static final java.lang.String RADIO_CELL = "cell"; + field public static final java.lang.String RADIO_NFC = "nfc"; field public static final java.lang.String RADIO_WIFI = "wifi"; field public static final java.lang.String RINGTONE = "ringtone"; field public static final java.lang.String SCREEN_BRIGHTNESS = "screen_brightness"; diff --git a/core/java/android/nfc/INfcAdapter.aidl b/core/java/android/nfc/INfcAdapter.aidl index 83a055c7b4cb5..4fc248f6c3c02 100644 --- a/core/java/android/nfc/INfcAdapter.aidl +++ b/core/java/android/nfc/INfcAdapter.aidl @@ -35,7 +35,7 @@ interface INfcAdapter INfcAdapterExtras getNfcAdapterExtrasInterface(); // NfcAdapter-class related methods - boolean isEnabled(); + int getState(); void enableForegroundDispatch(in ComponentName activity, in PendingIntent intent, in IntentFilter[] filters, in TechListParcel techLists); void disableForegroundDispatch(in ComponentName activity); @@ -48,5 +48,5 @@ interface INfcAdapter boolean enable(); boolean enableZeroClick(); boolean disableZeroClick(); - boolean zeroClickEnabled(); + boolean isZeroClickEnabled(); } diff --git a/core/java/android/nfc/NfcAdapter.java b/core/java/android/nfc/NfcAdapter.java index 4d04027ec57c6..6a904aeb0d4b5 100644 --- a/core/java/android/nfc/NfcAdapter.java +++ b/core/java/android/nfc/NfcAdapter.java @@ -157,23 +157,35 @@ public final class NfcAdapter { public static final String EXTRA_ID = "android.nfc.extra.ID"; /** - * Broadcast Action: an adapter's state changed between enabled and disabled. - * - * The new value is stored in the extra EXTRA_NEW_BOOLEAN_STATE and just contains - * whether it's enabled or disabled, not including any information about whether it's - * actively enabling or disabling. - * + * Broadcast Action: The state of the local NFC adapter has been + * changed. + *

For example, NFC has been turned on or off. + *

Always contains the extra field {@link #EXTRA_STATE} * @hide */ - public static final String ACTION_ADAPTER_STATE_CHANGE = - "android.nfc.action.ADAPTER_STATE_CHANGE"; + @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) + public static final String ACTION_ADAPTER_STATE_CHANGED = + "android.nfc.action.ADAPTER_STATE_CHANGED"; /** - * The Intent extra for ACTION_ADAPTER_STATE_CHANGE, saying what the new state is. - * + * Used as an int extra field in {@link #ACTION_STATE_CHANGED} + * intents to request the current power state. Possible values are: + * {@link #STATE_OFF}, + * {@link #STATE_TURNING_ON}, + * {@link #STATE_ON}, + * {@link #STATE_TURNING_OFF}, * @hide */ - public static final String EXTRA_NEW_BOOLEAN_STATE = "android.nfc.isEnabled"; + public static final String EXTRA_ADAPTER_STATE = "android.nfc.extra.ADAPTER_STATE"; + + /** @hide */ + public static final int STATE_OFF = 1; + /** @hide */ + public static final int STATE_TURNING_ON = 2; + /** @hide */ + public static final int STATE_ON = 3; + /** @hide */ + public static final int STATE_TURNING_OFF = 4; /** * LLCP link status: The LLCP link is activated. @@ -430,18 +442,48 @@ public final class NfcAdapter { */ public boolean isEnabled() { try { - return sService.isEnabled(); + return sService.getState() == STATE_ON; } catch (RemoteException e) { attemptDeadServiceRecovery(e); return false; } } + /** + * Return the state of this NFC Adapter. + * + *

Returns one of {@link #STATE_ON}, {@link #STATE_TURNING_ON}, + * {@link #STATE_OFF}, {@link #STATE_TURNING_OFF}. + * + *

{@link #isEnabled()} is equivalent to + * {@link #getAdapterState()} == {@link #STATE_ON} + * + * @return the current state of this NFC adapter + * + * @hide + */ + public int getAdapterState() { + try { + return sService.getState(); + } catch (RemoteException e) { + attemptDeadServiceRecovery(e); + return NfcAdapter.STATE_OFF; + } + } + /** * Enable NFC hardware. - *

- * NOTE: may block for ~second or more. Poor API. Avoid - * calling from the UI thread. + * + *

This call is asynchronous. Listen for + * {@link #ACTION_ADAPTER_STATE_CHANGED} broadcasts to find out when the + * operation is complete. + * + *

If this returns true, then either NFC is already on, or + * a {@link #ACTION_ADAPTER_STATE_CHANGED} broadcast will be sent + * to indicate a state transition. If this returns false, then + * there is some problem that prevents an attempt to turn + * NFC on (for example we are in airplane mode and NFC is not + * toggleable in airplane mode on this platform). * * @hide */ @@ -456,11 +498,19 @@ public final class NfcAdapter { /** * Disable NFC hardware. - * No NFC features will work after this call, and the hardware + * + *

No NFC features will work after this call, and the hardware * will not perform or respond to any NFC communication. - *

- * NOTE: may block for ~second or more. Poor API. Avoid - * calling from the UI thread. + * + *

This call is asynchronous. Listen for + * {@link #ACTION_ADAPTER_STATE_CHANGED} broadcasts to find out when the + * operation is complete. + * + *

If this returns true, then either NFC is already off, or + * a {@link #ACTION_ADAPTER_STATE_CHANGED} broadcast will be sent + * to indicate a state transition. If this returns false, then + * there is some problem that prevents an attempt to turn + * NFC off. * * @hide */ @@ -712,14 +762,20 @@ public final class NfcAdapter { } /** - * Return true if zero-click sharing is enabled. + * Return true if zero-click sharing feature is enabled. + *

This function can return true even if NFC is currently turned-off. + * This indicates that zero-click is not currently active, but it has + * been requested by the user and will be active as soon as NFC is turned + * on. + *

If you want to check if zero-click sharing is currently active, use + * {@link #isEnabled()} && {@link #isZeroClickEnabled()} * * @return true if zero-click sharing is enabled * @hide */ - public boolean zeroClickEnabled() { + public boolean isZeroClickEnabled() { try { - return sService.zeroClickEnabled(); + return sService.isZeroClickEnabled(); } catch (RemoteException e) { attemptDeadServiceRecovery(e); return false; diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java index 1ecdfceaf015d..f8702b9d91178 100644 --- a/core/java/android/provider/Settings.java +++ b/core/java/android/provider/Settings.java @@ -1168,6 +1168,11 @@ public final class Settings { */ public static final String RADIO_CELL = "cell"; + /** + * Constant for use in AIRPLANE_MODE_RADIOS to specify NFC radio. + */ + public static final String RADIO_NFC = "nfc"; + /** * A comma separated list of radios that need to be disabled when airplane mode * is on. This overrides WIFI_ON and BLUETOOTH_ON, if Wi-Fi and bluetooth are diff --git a/packages/SettingsProvider/res/values/defaults.xml b/packages/SettingsProvider/res/values/defaults.xml index 12dbdf99736f7..3920257a7c433 100644 --- a/packages/SettingsProvider/res/values/defaults.xml +++ b/packages/SettingsProvider/res/values/defaults.xml @@ -21,8 +21,8 @@ 60000 false - cell,bluetooth,wifi - bluetooth,wifi + cell,bluetooth,wifi,nfc + bluetooth,wifi,nfc true true true diff --git a/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java b/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java index f4890e00c8ba1..5495d0840477f 100644 --- a/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java +++ b/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java @@ -63,7 +63,7 @@ public class DatabaseHelper extends SQLiteOpenHelper { // database gets upgraded properly. At a minimum, please confirm that 'upgradeVersion' // is properly propagated through your change. Not doing so will result in a loss of user // settings. - private static final int DATABASE_VERSION = 69; + private static final int DATABASE_VERSION = 70; private Context mContext; @@ -917,6 +917,25 @@ public class DatabaseHelper extends SQLiteOpenHelper { upgradeVersion = 69; } + if (upgradeVersion == 69) { + // Add RADIO_NFC to AIRPLANE_MODE_RADIO and AIRPLANE_MODE_TOGGLEABLE_RADIOS + String airplaneRadios = mContext.getResources().getString( + R.string.def_airplane_mode_radios); + String toggleableRadios = mContext.getResources().getString( + R.string.airplane_mode_toggleable_radios); + db.beginTransaction(); + try { + db.execSQL("UPDATE system SET value='" + airplaneRadios + "' " + + "WHERE name='" + Settings.System.AIRPLANE_MODE_RADIOS + "'"); + db.execSQL("UPDATE system SET value='" + toggleableRadios + "' " + + "WHERE name='" + Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS + "'"); + db.setTransactionSuccessful(); + } finally { + db.endTransaction(); + } + upgradeVersion = 70; + } + // *** Remember to update DATABASE_VERSION above! if (upgradeVersion != currentVersion) {