Permalink
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...
romanbb authored and LorDClockaN committed Sep 29, 2016
1 parent 99819cb commit e6c902c63f575cfb24e6fc651ee0020391976101
@@ -30,6 +30,9 @@
<uses-permission android:name="cyanogenmod.permission.READ_WEATHER" />
<protected-broadcast android:name="com.cyanogenmod.lockclock.action.FORCE_WEATHER_UPDATE" />

<!-- NFC Tile -->
<uses-permission android:name="android.permission.NFC" />

<application>

<activity-alias
@@ -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>
@@ -111,4 +111,9 @@

<!-- Path data for circle battery -->
<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>

<!-- QS NFC tile -->
<string name="quick_settings_nfc">NFC</string>
<string name="quick_settings_nfc_label">NFC</string>
<string name="quick_settings_nfc_off">NFC off</string>
</resources>
@@ -105,7 +105,7 @@

<!-- The default tiles to display in QuickSettings -->
<string name="quick_settings_tiles_stock" translatable="false">
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
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
</string>

<!-- The tiles to display in QuickSettings -->
@@ -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);
}
}
@@ -63,6 +63,7 @@
import com.android.systemui.qs.tiles.LocationTile;
import com.android.systemui.qs.tiles.MusicTile;
import com.android.systemui.qs.tiles.NavigationBarTile;
import com.android.systemui.qs.tiles.NfcTile;
import com.android.systemui.qs.tiles.NightDisplayTile;
import com.android.systemui.qs.tiles.PieTile;
import com.android.systemui.qs.tiles.PulseTile;
@@ -472,6 +473,7 @@ public void changeTiles(List<String> previousTiles, List<String> newTiles) {
else if (tileSpec.equals("sound")) return new SoundTile(this);
else if (tileSpec.equals("ambient_display")) return new AmbientDisplayTile(this);
else if (tileSpec.equals("app_picker")) return new AppPickerTile(this);
else if (tileSpec.equals("nfc")) return new NfcTile(this);
// Intent tiles.
else if (tileSpec.startsWith(IntentTile.PREFIX)) return IntentTile.create(this,tileSpec);
else if (tileSpec.startsWith(CustomTile.PREFIX)) return CustomTile.create(this,tileSpec);

0 comments on commit e6c902c

Please sign in to comment.