Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
SysUI: Add NFC Tile
AICPfy: - Make it work with N - Hide on devices that don't support NFC Change-Id: I884b3492b1d81c39235df1660e4e0e06653abce2
- Loading branch information
Showing
with
229 additions
and 1 deletion.
- +3 −0 packages/SystemUI/AndroidManifest_cm.xml
- +32 −0 packages/SystemUI/res/drawable/ic_qs_nfc_off.xml
- +31 −0 packages/SystemUI/res/drawable/ic_qs_nfc_on.xml
- +5 −0 packages/SystemUI/res/values/cm_strings.xml
- +1 −1 packages/SystemUI/res/values/config.xml
- +155 −0 packages/SystemUI/src/com/android/systemui/qs/tiles/NfcTile.java
- +2 −0 packages/SystemUI/src/com/android/systemui/statusbar/phone/QSTileHost.java
@@ -0,0 +1,32 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!-- | ||
/* | ||
* Copyright (C) 2015 The CyanogenMod Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
--> | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="64dp" | ||
android:height="64dp" | ||
android:viewportWidth="48" | ||
android:viewportHeight="48"> | ||
|
||
<path | ||
android:fillColor="#4DFFFFFF" | ||
android:pathData="M16.4,8l-4-4H40c2.2,0,4,1.8,4,4v27.6l-4-4V8H16.4Z | ||
M26,12c-1.5,0-2.8,0.9-3.5,2.1l3.5,3.5 V16h6v7.6l4,4V12H26z | ||
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 | ||
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 | ||
M37.2,40l-4-4H12V14.8l-4-4V40H37.2 z" /> | ||
</vector> |
@@ -0,0 +1,31 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!-- | ||
/* | ||
* Copyright (C) 2015 The CyanogenMod Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
--> | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="64dp" | ||
android:height="64dp" | ||
android:viewportWidth="48" | ||
android:viewportHeight="48"> | ||
|
||
<path | ||
android:fillColor="#FFFFFF" | ||
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 | ||
M40,40H8V8h32V40z | ||
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 | ||
V16h4v-4h-8v24h24V12z" /> | ||
</vector> |
@@ -0,0 +1,155 @@ | ||
/* | ||
* Copyright (C) 2015 The CyanogenMod Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.android.systemui.qs.tiles; | ||
|
||
import android.content.BroadcastReceiver; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.content.IntentFilter; | ||
import android.nfc.NfcAdapter; | ||
|
||
import android.util.Log; | ||
import com.android.systemui.R; | ||
import com.android.systemui.qs.QSTile; | ||
import com.android.internal.logging.MetricsProto.MetricsEvent; | ||
import org.cyanogenmod.internal.util.QSUtils; | ||
|
||
public class NfcTile extends QSTile<QSTile.BooleanState> { | ||
|
||
private boolean mListening; | ||
|
||
private BroadcastReceiver mReceiver = new BroadcastReceiver() { | ||
@Override | ||
public void onReceive(Context context, Intent intent) { | ||
refreshState(); | ||
} | ||
}; | ||
|
||
public NfcTile(Host host) { | ||
super(host); | ||
isAvailable(); | ||
} | ||
|
||
@Override | ||
public BooleanState newTileState() { | ||
return new BooleanState(); | ||
} | ||
|
||
@Override | ||
protected void handleClick() { | ||
boolean newState = !getState().value; | ||
setState(newState); | ||
refreshState(); | ||
} | ||
|
||
@Override | ||
protected void handleLongClick() { | ||
mHost.startActivityDismissingKeyguard(new Intent("android.settings.NFC_SETTINGS")); | ||
} | ||
|
||
private void setState(boolean on) { | ||
try { | ||
NfcAdapter nfcAdapter = NfcAdapter.getNfcAdapter(mContext); | ||
if (nfcAdapter == null) { | ||
Log.e(TAG, "tried to set NFC state, but no NFC adapter was found"); | ||
return; | ||
} | ||
if (on) { | ||
nfcAdapter.enable(); | ||
} else { | ||
nfcAdapter.disable(); | ||
} | ||
} catch (UnsupportedOperationException e) { | ||
// ignore | ||
} | ||
} | ||
|
||
private int getNfcAdapterState() { | ||
try { | ||
NfcAdapter nfcAdapter = NfcAdapter.getNfcAdapter(mContext); | ||
if (nfcAdapter == null) { | ||
Log.e(TAG, "tried to get NFC state, but no NFC adapter was found"); | ||
return NfcAdapter.STATE_OFF; | ||
} | ||
return nfcAdapter.getAdapterState(); | ||
} catch (UnsupportedOperationException e) { | ||
// ignore | ||
return NfcAdapter.STATE_OFF; | ||
} | ||
} | ||
|
||
/** | ||
* Helper method to encapsulate intermediate states (turning off/on) to help determine whether | ||
* the adapter will be on or off. | ||
* @param nfcState The current NFC adapter state. | ||
* @return boolean representing what state the adapter is/will be in | ||
*/ | ||
private static boolean isEnabled(int nfcState) { | ||
switch (nfcState) { | ||
case NfcAdapter.STATE_TURNING_ON: | ||
case NfcAdapter.STATE_ON: | ||
return true; | ||
case NfcAdapter.STATE_TURNING_OFF: | ||
case NfcAdapter.STATE_OFF: | ||
default: | ||
return false; | ||
} | ||
} | ||
|
||
@Override | ||
public Intent getLongClickIntent() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public CharSequence getTileLabel() { | ||
return mContext.getString(R.string.quick_settings_nfc_label); | ||
} | ||
|
||
@Override | ||
protected void handleUpdateState(BooleanState state, Object arg) { | ||
final int nfcState = getNfcAdapterState(); | ||
state.value = isAvailable() && isEnabled(nfcState); | ||
|
||
state.icon = ResourceIcon.get(state.value ? | ||
R.drawable.ic_qs_nfc_on : R.drawable.ic_qs_nfc_off); | ||
state.label = mContext.getString(R.string.quick_settings_nfc_label); | ||
} | ||
|
||
@Override | ||
public int getMetricsCategory() { | ||
return MetricsEvent.QUICK_SETTINGS; | ||
} | ||
|
||
@Override | ||
public void setListening(boolean listening) { | ||
if (mListening == listening) return; | ||
mListening = listening; | ||
if (listening) { | ||
mContext.registerReceiver(mReceiver, | ||
new IntentFilter(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED)); | ||
refreshState(); | ||
} else { | ||
mContext.unregisterReceiver(mReceiver); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isAvailable() { | ||
return QSUtils.deviceSupportsNfc(mContext); | ||
} | ||
} |