Skip to content

Commit 655cb83

Browse files
DvTonderGodFox
authored andcommitted
Framework: Add NFC Tile to Quick Settings (2 of 2)
Need to figure out how to instantiate a new NFC adapter after the Tile is created. Change-Id: I5ad99f1ac8f4730653c9697c2c0d0e86b7d4facd
1 parent 7d46f04 commit 655cb83

9 files changed

Lines changed: 116 additions & 0 deletions

File tree

4.02 KB
Loading
4.03 KB
Loading
3.51 KB
Loading
3.54 KB
Loading
4.6 KB
Loading
4.55 KB
Loading

packages/SystemUI/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,7 @@
540540
<string name="quick_settings_report_bug">Report bug</string>
541541
<string name="quick_settings_sync">Sync</string>
542542
<string name="quick_settings_torch">Torch</string>
543+
<string name="quick_settings_nfc">NFC</string>
543544

544545
<!-- Text to display next to the minimal graphical battery meter. [CHAR LIMIT=3] -->
545546
<string name="status_bar_settings_battery_meter_min_format" translatable="false">
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package com.android.systemui.quicksettings;
2+
3+
import android.content.Context;
4+
import android.content.Intent;
5+
import android.nfc.NfcAdapter;
6+
import android.util.Log;
7+
import android.view.LayoutInflater;
8+
import android.view.View;
9+
10+
import com.android.systemui.R;
11+
import com.android.systemui.statusbar.phone.QuickSettingsContainerView;
12+
import com.android.systemui.statusbar.phone.QuickSettingsController;
13+
14+
15+
public class NfcTile extends QuickSettingsTile {
16+
17+
private static String TAG = "NfcTile";
18+
private static NfcAdapter mNfcAdapter;
19+
private static final int NFC_ADAPTER_UNKNOWN = -100;
20+
21+
public NfcTile(Context context, LayoutInflater inflater,
22+
QuickSettingsContainerView container,
23+
QuickSettingsController qsc) {
24+
super(context, inflater, container, qsc);
25+
26+
setState(NFC_ADAPTER_UNKNOWN);
27+
28+
mOnClick = new View.OnClickListener() {
29+
@Override
30+
public void onClick(View v) {
31+
toggleState();
32+
applyNfcChanges();
33+
}
34+
};
35+
36+
mOnLongClick = new View.OnLongClickListener() {
37+
@Override
38+
public boolean onLongClick(View v) {
39+
Intent intent = new Intent("android.settings.NFC_SETTINGS");
40+
intent.addCategory(Intent.CATEGORY_DEFAULT);
41+
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
42+
startSettingsActivity(intent);
43+
return true;
44+
}
45+
};
46+
47+
qsc.registerAction(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED, this);
48+
}
49+
50+
@Override
51+
public void onReceive(Context context, Intent intent) {
52+
applyNfcChanges();
53+
}
54+
55+
private void applyNfcChanges() {
56+
updateTileState();
57+
updateQuickSettings();
58+
}
59+
60+
protected void toggleState() {
61+
int state = getNfcState();
62+
switch (state) {
63+
case NfcAdapter.STATE_TURNING_ON:
64+
case NfcAdapter.STATE_ON:
65+
mNfcAdapter.disable();
66+
break;
67+
case NfcAdapter.STATE_TURNING_OFF:
68+
case NfcAdapter.STATE_OFF:
69+
mNfcAdapter.enable();
70+
break;
71+
}
72+
}
73+
74+
private void updateTileState() {
75+
// Get the initial label
76+
mLabel = mContext.getString(R.string.quick_settings_nfc);
77+
setState(getNfcState());
78+
}
79+
80+
private void setState(int state) {
81+
switch (state) {
82+
case NfcAdapter.STATE_TURNING_ON:
83+
case NfcAdapter.STATE_ON:
84+
mDrawable = R.drawable.ic_qs_nfc_on;
85+
break;
86+
case NfcAdapter.STATE_TURNING_OFF:
87+
case NfcAdapter.STATE_OFF:
88+
default:
89+
mDrawable = R.drawable.ic_qs_nfc_off;
90+
mLabel += " " + mContext.getString(R.string.quick_settings_label_disabled);
91+
break;
92+
}
93+
}
94+
95+
private int getNfcState() {
96+
if (mNfcAdapter != null || (mNfcAdapter = NfcAdapter.getDefaultAdapter(mContext)) != null) {
97+
return mNfcAdapter.getAdapterState();
98+
} else {
99+
Log.d(TAG, "No NFC adapter available");
100+
return NFC_ADAPTER_UNKNOWN;
101+
}
102+
}
103+
}

packages/SystemUI/src/com/android/systemui/statusbar/phone/QuickSettingsController.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import android.content.pm.PackageManager;
2929
import android.database.ContentObserver;
3030
import android.net.Uri;
31+
import android.nfc.NfcAdapter;
3132
import android.os.Handler;
3233
import android.provider.Settings;
3334
import android.util.Log;
@@ -41,6 +42,7 @@
4142
import com.android.systemui.quicksettings.BluetoothTile;
4243
import com.android.systemui.quicksettings.BrightnessTile;
4344
import com.android.systemui.quicksettings.BugReportTile;
45+
import com.android.systemui.quicksettings.NfcTile;
4446
import com.android.systemui.quicksettings.TorchTile;
4547
import com.android.systemui.quicksettings.GPSTile;
4648
import com.android.systemui.quicksettings.InputMethodTile;
@@ -98,6 +100,7 @@ public class QuickSettingsController {
98100
public static final String TILE_LTE = "toggleLte";
99101
public static final String TILE_WIMAX = "toggleWimax";
100102
public static final String TILE_PROFILE = "toggleProfile";
103+
public static final String TILE_NFC = "toggleNfc";
101104

102105
private static final String TILE_DELIMITER = "|";
103106
private static final String TILES_DEFAULT = TILE_USER
@@ -145,6 +148,7 @@ public class QuickSettingsController {
145148
public static final int WIFIAP_TILE = 20;
146149
public static final int PROFILE_TILE = 21;
147150
public static final int SYNC_TILE = 22;
151+
public static final int NFC_TILE = 23;
148152
public static final int USER_TILE = 99;
149153
private InputMethodTile IMETile;
150154

@@ -220,6 +224,10 @@ void loadTiles() {
220224
if (systemProfilesEnabled(resolver)) {
221225
mQuickSettings.add(PROFILE_TILE);
222226
}
227+
} else if (tile.equals(TILE_NFC)) {
228+
// User cannot add the NFC tile if the device does not support it
229+
// No need to check again here
230+
mQuickSettings.add(NFC_TILE);
223231
} else if (tile.equals(TILE_WIMAX)) {
224232
// Not available yet
225233
} else if (tile.equals(TILE_LTE)) {
@@ -447,6 +455,10 @@ void addQuickSettings(LayoutInflater inflater){
447455
qs = new SyncTile(mContext, inflater,
448456
(QuickSettingsContainerView) mContainerView, this);
449457
break;
458+
case NFC_TILE:
459+
qs = new NfcTile(mContext, inflater,
460+
(QuickSettingsContainerView) mContainerView, this);
461+
break;
450462
}
451463
if (qs != null) {
452464
qs.setupQuickSettingsTile();

0 commit comments

Comments
 (0)