Skip to content

Commit e6c902c

Browse files
romanbbLorDClockaN
authored andcommitted
SysUI: Add NFC Tile
AICPfy: - Make it work with N - Hide on devices that don't support NFC Change-Id: I884b3492b1d81c39235df1660e4e0e06653abce2
1 parent 99819cb commit e6c902c

File tree

7 files changed

+229
-1
lines changed

7 files changed

+229
-1
lines changed

packages/SystemUI/AndroidManifest_cm.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
<uses-permission android:name="cyanogenmod.permission.READ_WEATHER" />
3131
<protected-broadcast android:name="com.cyanogenmod.lockclock.action.FORCE_WEATHER_UPDATE" />
3232

33+
<!-- NFC Tile -->
34+
<uses-permission android:name="android.permission.NFC" />
35+
3336
<application>
3437

3538
<activity-alias
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
/*
4+
* Copyright (C) 2015 The CyanogenMod Project
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
-->
19+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
20+
android:width="64dp"
21+
android:height="64dp"
22+
android:viewportWidth="48"
23+
android:viewportHeight="48">
24+
25+
<path
26+
android:fillColor="#4DFFFFFF"
27+
android:pathData="M16.4,8l-4-4H40c2.2,0,4,1.8,4,4v27.6l-4-4V8H16.4Z
28+
M26,12c-1.5,0-2.8,0.9-3.5,2.1l3.5,3.5 V16h6v7.6l4,4V12H26z
29+
M43.8,41.1c-0.5,1.7-2,2.9-3.8,2.9H8c-2.2,0-4-1.8-4-4V8c0-1.8,1.3-3.4,2.9-3.8L32,29.2l4,4L43.8,41.1z
30+
M25,27.9c-0.3,0.1-0.7,0.1-1,0.1c-2.2,0-4-1.8-4-4c0-0.4,0-0.7,0.1-1L16,18.8V32h13.2L25,27.9z
31+
M37.2,40l-4-4H12V14.8l-4-4V40H37.2 z" />
32+
</vector>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
/*
4+
* Copyright (C) 2015 The CyanogenMod Project
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
-->
19+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
20+
android:width="64dp"
21+
android:height="64dp"
22+
android:viewportWidth="48"
23+
android:viewportHeight="48">
24+
25+
<path
26+
android:fillColor="#FFFFFF"
27+
android:pathData="M40,4H8C5.8,4,4,5.8,4,8v32c0,2.2,1.8,4,4,4h32c2.2,0,4-1.8,4-4V8C44,5.8,42.2,4,40,4Z
28+
M40,40H8V8h32V40z
29+
M36,12H26c-2.2,0-4,1.8-4,4v4.6c-1.2,0.7-2,2-2,3.4c0,2.2,1.8,4,4,4s4-1.8,4-4c0-1.5-0.8-2.8-2-3.4V16h6v16H16
30+
V16h4v-4h-8v24h24V12z" />
31+
</vector>

packages/SystemUI/res/values/cm_strings.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,9 @@
111111

112112
<!-- Path data for circle battery -->
113113
<string name="battery_circle_path" translatable="false">M 12 3.5 C 16.6944203736 3.5 20.5 7.30557962644 20.5 12 C 20.5 16.6944203736 16.6944203736 20.5 12 20.5 C 7.30557962644 20.5 3.5 16.6944203736 3.5 12 C 3.5 7.30557962644 7.30557962644 3.5 12 3.5 Z</string>
114+
115+
<!-- QS NFC tile -->
116+
<string name="quick_settings_nfc">NFC</string>
117+
<string name="quick_settings_nfc_label">NFC</string>
118+
<string name="quick_settings_nfc_off">NFC off</string>
114119
</resources>

packages/SystemUI/res/values/config.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105

106106
<!-- The default tiles to display in QuickSettings -->
107107
<string name="quick_settings_tiles_stock" translatable="false">
108-
wifi,cell,battery,dnd,flashlight,rotation,bt,airplane,location,hotspot,inversion,saver,work,cast,night,screenshot,adb_network,sync,expanded_desktop,reboot,usb_tether,music,ime,caffeine,navigation_bar,screenrecord,heads_up,pulse,locale,aicp_extras,pie,sound,ambient_display,app_picker
108+
wifi,cell,battery,dnd,flashlight,rotation,bt,airplane,location,hotspot,inversion,saver,work,cast,night,screenshot,adb_network,sync,expanded_desktop,reboot,usb_tether,music,ime,caffeine,navigation_bar,screenrecord,heads_up,pulse,locale,aicp_extras,pie,sound,ambient_display,app_picker,nfc
109109
</string>
110110

111111
<!-- The tiles to display in QuickSettings -->
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/*
2+
* Copyright (C) 2015 The CyanogenMod Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.android.systemui.qs.tiles;
18+
19+
import android.content.BroadcastReceiver;
20+
import android.content.Context;
21+
import android.content.Intent;
22+
import android.content.IntentFilter;
23+
import android.nfc.NfcAdapter;
24+
25+
import android.util.Log;
26+
import com.android.systemui.R;
27+
import com.android.systemui.qs.QSTile;
28+
import com.android.internal.logging.MetricsProto.MetricsEvent;
29+
import org.cyanogenmod.internal.util.QSUtils;
30+
31+
public class NfcTile extends QSTile<QSTile.BooleanState> {
32+
33+
private boolean mListening;
34+
35+
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
36+
@Override
37+
public void onReceive(Context context, Intent intent) {
38+
refreshState();
39+
}
40+
};
41+
42+
public NfcTile(Host host) {
43+
super(host);
44+
isAvailable();
45+
}
46+
47+
@Override
48+
public BooleanState newTileState() {
49+
return new BooleanState();
50+
}
51+
52+
@Override
53+
protected void handleClick() {
54+
boolean newState = !getState().value;
55+
setState(newState);
56+
refreshState();
57+
}
58+
59+
@Override
60+
protected void handleLongClick() {
61+
mHost.startActivityDismissingKeyguard(new Intent("android.settings.NFC_SETTINGS"));
62+
}
63+
64+
private void setState(boolean on) {
65+
try {
66+
NfcAdapter nfcAdapter = NfcAdapter.getNfcAdapter(mContext);
67+
if (nfcAdapter == null) {
68+
Log.e(TAG, "tried to set NFC state, but no NFC adapter was found");
69+
return;
70+
}
71+
if (on) {
72+
nfcAdapter.enable();
73+
} else {
74+
nfcAdapter.disable();
75+
}
76+
} catch (UnsupportedOperationException e) {
77+
// ignore
78+
}
79+
}
80+
81+
private int getNfcAdapterState() {
82+
try {
83+
NfcAdapter nfcAdapter = NfcAdapter.getNfcAdapter(mContext);
84+
if (nfcAdapter == null) {
85+
Log.e(TAG, "tried to get NFC state, but no NFC adapter was found");
86+
return NfcAdapter.STATE_OFF;
87+
}
88+
return nfcAdapter.getAdapterState();
89+
} catch (UnsupportedOperationException e) {
90+
// ignore
91+
return NfcAdapter.STATE_OFF;
92+
}
93+
}
94+
95+
/**
96+
* Helper method to encapsulate intermediate states (turning off/on) to help determine whether
97+
* the adapter will be on or off.
98+
* @param nfcState The current NFC adapter state.
99+
* @return boolean representing what state the adapter is/will be in
100+
*/
101+
private static boolean isEnabled(int nfcState) {
102+
switch (nfcState) {
103+
case NfcAdapter.STATE_TURNING_ON:
104+
case NfcAdapter.STATE_ON:
105+
return true;
106+
case NfcAdapter.STATE_TURNING_OFF:
107+
case NfcAdapter.STATE_OFF:
108+
default:
109+
return false;
110+
}
111+
}
112+
113+
@Override
114+
public Intent getLongClickIntent() {
115+
return null;
116+
}
117+
118+
@Override
119+
public CharSequence getTileLabel() {
120+
return mContext.getString(R.string.quick_settings_nfc_label);
121+
}
122+
123+
@Override
124+
protected void handleUpdateState(BooleanState state, Object arg) {
125+
final int nfcState = getNfcAdapterState();
126+
state.value = isAvailable() && isEnabled(nfcState);
127+
128+
state.icon = ResourceIcon.get(state.value ?
129+
R.drawable.ic_qs_nfc_on : R.drawable.ic_qs_nfc_off);
130+
state.label = mContext.getString(R.string.quick_settings_nfc_label);
131+
}
132+
133+
@Override
134+
public int getMetricsCategory() {
135+
return MetricsEvent.QUICK_SETTINGS;
136+
}
137+
138+
@Override
139+
public void setListening(boolean listening) {
140+
if (mListening == listening) return;
141+
mListening = listening;
142+
if (listening) {
143+
mContext.registerReceiver(mReceiver,
144+
new IntentFilter(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED));
145+
refreshState();
146+
} else {
147+
mContext.unregisterReceiver(mReceiver);
148+
}
149+
}
150+
151+
@Override
152+
public boolean isAvailable() {
153+
return QSUtils.deviceSupportsNfc(mContext);
154+
}
155+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
import com.android.systemui.qs.tiles.LocationTile;
6464
import com.android.systemui.qs.tiles.MusicTile;
6565
import com.android.systemui.qs.tiles.NavigationBarTile;
66+
import com.android.systemui.qs.tiles.NfcTile;
6667
import com.android.systemui.qs.tiles.NightDisplayTile;
6768
import com.android.systemui.qs.tiles.PieTile;
6869
import com.android.systemui.qs.tiles.PulseTile;
@@ -472,6 +473,7 @@ public QSTile<?> createTile(String tileSpec) {
472473
else if (tileSpec.equals("sound")) return new SoundTile(this);
473474
else if (tileSpec.equals("ambient_display")) return new AmbientDisplayTile(this);
474475
else if (tileSpec.equals("app_picker")) return new AppPickerTile(this);
476+
else if (tileSpec.equals("nfc")) return new NfcTile(this);
475477
// Intent tiles.
476478
else if (tileSpec.startsWith(IntentTile.PREFIX)) return IntentTile.create(this,tileSpec);
477479
else if (tileSpec.startsWith(CustomTile.PREFIX)) return CustomTile.create(this,tileSpec);

0 commit comments

Comments
 (0)