Skip to content

Commit

Permalink
Framework: Add NFC Tile to Quick Settings (2 of 2)
Browse files Browse the repository at this point in the history
Need to figure out how to instantiate a new NFC adapter after the
Tile is created.

Change-Id: I5ad99f1ac8f4730653c9697c2c0d0e86b7d4facd
  • Loading branch information
DvTonder authored and Danesh committed Dec 22, 2012
1 parent 50ed9b2 commit 7b1b76a
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 0 deletions.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions packages/SystemUI/res/values/strings.xml
Expand Up @@ -542,6 +542,7 @@
<string name="quick_settings_report_bug">Report bug</string>
<string name="quick_settings_sync">Sync</string>
<string name="quick_settings_torch">Torch</string>
<string name="quick_settings_nfc">NFC</string>

<!-- Text to display next to the minimal graphical battery meter. [CHAR LIMIT=3] -->
<string name="status_bar_settings_battery_meter_min_format" translatable="false">
Expand Down
103 changes: 103 additions & 0 deletions packages/SystemUI/src/com/android/systemui/quicksettings/NfcTile.java
@@ -0,0 +1,103 @@
package com.android.systemui.quicksettings;

import android.content.Context;
import android.content.Intent;
import android.nfc.NfcAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.android.systemui.R;
import com.android.systemui.statusbar.phone.QuickSettingsContainerView;
import com.android.systemui.statusbar.phone.QuickSettingsController;


public class NfcTile extends QuickSettingsTile {

private static String TAG = "NfcTile";
private static NfcAdapter mNfcAdapter;
private static final int NFC_ADAPTER_UNKNOWN = -100;

public NfcTile(Context context, LayoutInflater inflater,
QuickSettingsContainerView container,
QuickSettingsController qsc) {
super(context, inflater, container, qsc);

setState(NFC_ADAPTER_UNKNOWN);

mOnClick = new View.OnClickListener() {
@Override
public void onClick(View v) {
toggleState();
applyNfcChanges();
}
};

mOnLongClick = new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Intent intent = new Intent("android.settings.NFC_SETTINGS");
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startSettingsActivity(intent);
return true;
}
};

qsc.registerAction(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED, this);
}

@Override
public void onReceive(Context context, Intent intent) {
applyNfcChanges();
}

private void applyNfcChanges() {
updateTileState();
updateQuickSettings();
}

protected void toggleState() {
int state = getNfcState();
switch (state) {
case NfcAdapter.STATE_TURNING_ON:
case NfcAdapter.STATE_ON:
mNfcAdapter.disable();
break;
case NfcAdapter.STATE_TURNING_OFF:
case NfcAdapter.STATE_OFF:
mNfcAdapter.enable();
break;
}
}

private void updateTileState() {
// Get the initial label
mLabel = mContext.getString(R.string.quick_settings_nfc);
setState(getNfcState());
}

private void setState(int state) {
switch (state) {
case NfcAdapter.STATE_TURNING_ON:
case NfcAdapter.STATE_ON:
mDrawable = R.drawable.ic_qs_nfc_on;
break;
case NfcAdapter.STATE_TURNING_OFF:
case NfcAdapter.STATE_OFF:
default:
mDrawable = R.drawable.ic_qs_nfc_off;
mLabel += " " + mContext.getString(R.string.quick_settings_label_disabled);
break;
}
}

private int getNfcState() {
if (mNfcAdapter != null || (mNfcAdapter = NfcAdapter.getDefaultAdapter(mContext)) != null) {
return mNfcAdapter.getAdapterState();
} else {
Log.d(TAG, "No NFC adapter available");
return NFC_ADAPTER_UNKNOWN;
}
}
}
Expand Up @@ -28,6 +28,7 @@
import android.content.pm.PackageManager;
import android.database.ContentObserver;
import android.net.Uri;
import android.nfc.NfcAdapter;
import android.os.Handler;
import android.provider.Settings;
import android.util.Log;
Expand All @@ -41,6 +42,7 @@
import com.android.systemui.quicksettings.BluetoothTile;
import com.android.systemui.quicksettings.BrightnessTile;
import com.android.systemui.quicksettings.BugReportTile;
import com.android.systemui.quicksettings.NfcTile;
import com.android.systemui.quicksettings.TorchTile;
import com.android.systemui.quicksettings.GPSTile;
import com.android.systemui.quicksettings.InputMethodTile;
Expand Down Expand Up @@ -98,6 +100,7 @@ public class QuickSettingsController {
public static final String TILE_LTE = "toggleLte";
public static final String TILE_WIMAX = "toggleWimax";
public static final String TILE_PROFILE = "toggleProfile";
public static final String TILE_NFC = "toggleNfc";

private static final String TILE_DELIMITER = "|";
private static final String TILES_DEFAULT = TILE_USER
Expand Down Expand Up @@ -145,6 +148,7 @@ public class QuickSettingsController {
public static final int WIFIAP_TILE = 20;
public static final int PROFILE_TILE = 21;
public static final int SYNC_TILE = 22;
public static final int NFC_TILE = 23;
public static final int USER_TILE = 99;
private InputMethodTile IMETile;

Expand Down Expand Up @@ -220,6 +224,10 @@ void loadTiles() {
if (systemProfilesEnabled(resolver)) {
mQuickSettings.add(PROFILE_TILE);
}
} else if (tile.equals(TILE_NFC)) {
// User cannot add the NFC tile if the device does not support it
// No need to check again here
mQuickSettings.add(NFC_TILE);
} else if (tile.equals(TILE_WIMAX)) {
// Not available yet
} else if (tile.equals(TILE_LTE)) {
Expand Down Expand Up @@ -447,6 +455,10 @@ void addQuickSettings(LayoutInflater inflater){
qs = new SyncTile(mContext, inflater,
(QuickSettingsContainerView) mContainerView, this);
break;
case NFC_TILE:
qs = new NfcTile(mContext, inflater,
(QuickSettingsContainerView) mContainerView, this);
break;
}
if (qs != null) {
qs.setupQuickSettingsTile();
Expand Down

0 comments on commit 7b1b76a

Please sign in to comment.