NfcTile.java revision 97705ce11664e9bbe6ab1c2eca8aa7cf01bcc84c
1/* 2 * Copyright (c) 2016, The Android Open Source Project 3 * Contributed by the Paranoid Android Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package com.android.systemui.qs.tiles; 19 20import android.content.BroadcastReceiver; 21import android.content.Context; 22import android.content.Intent; 23import android.content.IntentFilter; 24import android.content.pm.PackageManager; 25import android.graphics.drawable.Drawable; 26import android.nfc.NfcAdapter; 27import android.provider.Settings; 28import android.widget.Switch; 29 30import com.android.internal.logging.MetricsLogger; 31import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 32import com.android.systemui.R; 33import com.android.systemui.qs.QSTile; 34 35/** Quick settings tile: Enable/Disable NFC **/ 36public class NfcTile extends QSTile<QSTile.BooleanState> { 37 38 private NfcAdapter mAdapter; 39 40 private boolean mListening; 41 42 public NfcTile(Host host) { 43 super(host); 44 } 45 46 @Override 47 public BooleanState newTileState() { 48 return new BooleanState(); 49 } 50 51 @Override 52 public void setListening(boolean listening) { 53 mListening = listening; 54 if (mListening) { 55 mContext.registerReceiver(mNfcReceiver, 56 new IntentFilter(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED)); 57 if (mAdapter == null) { 58 try { 59 mAdapter = NfcAdapter.getNfcAdapter(mContext); 60 } catch (UnsupportedOperationException e) { 61 mAdapter = null; 62 } 63 } 64 } else { 65 mContext.unregisterReceiver(mNfcReceiver); 66 } 67 } 68 69 @Override 70 public boolean isAvailable() { 71 return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC); 72 } 73 74 @Override 75 protected void handleUserSwitch(int newUserId) { 76 } 77 78 @Override 79 public Intent getLongClickIntent() { 80 return new Intent(Settings.ACTION_NFC_SETTINGS); 81 } 82 83 @Override 84 protected void handleClick() { 85 if (mAdapter == null) return; 86 MetricsLogger.action(mContext, getMetricsCategory(), !mState.value); 87 if (!mAdapter.isEnabled()) { 88 mAdapter.enable(); 89 } else { 90 mAdapter.disable(); 91 } 92 } 93 94 @Override 95 protected void handleSecondaryClick() { 96 handleClick(); 97 } 98 99 @Override 100 public CharSequence getTileLabel() { 101 return mContext.getString(R.string.quick_settings_nfc_label); 102 } 103 104 @Override 105 protected void handleUpdateState(BooleanState state, Object arg) { 106 final Drawable mEnable = mContext.getDrawable(R.drawable.ic_qs_nfc_enabled); 107 final Drawable mDisable = mContext.getDrawable(R.drawable.ic_qs_nfc_disabled); 108 state.value = mAdapter == null ? false : mAdapter.isEnabled(); 109 state.label = mContext.getString(R.string.quick_settings_nfc_label); 110 state.icon = new DrawableIcon(state.value ? mEnable : mDisable); 111 state.minimalAccessibilityClassName = state.expandedAccessibilityClassName 112 = Switch.class.getName(); 113 state.contentDescription = state.label; 114 } 115 116 @Override 117 public int getMetricsCategory() { 118 return MetricsEvent.QS_NFC; 119 } 120 121 @Override 122 protected String composeChangeAnnouncement() { 123 if (mState.value) { 124 return mContext.getString(R.string.quick_settings_nfc_on); 125 } else { 126 return mContext.getString(R.string.quick_settings_nfc_off); 127 } 128 } 129 130 private BroadcastReceiver mNfcReceiver = new BroadcastReceiver() { 131 @Override 132 public void onReceive(Context context, Intent intent) { 133 refreshState(); 134 } 135 }; 136} 137