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.QSHost;
34import com.android.systemui.plugins.qs.QSTile.BooleanState;
35import com.android.systemui.qs.tileimpl.QSTileImpl;
36
37/** Quick settings tile: Enable/Disable NFC **/
38public class NfcTile extends QSTileImpl<BooleanState> {
39
40    private NfcAdapter mAdapter;
41
42    private boolean mListening;
43
44    public NfcTile(QSHost host) {
45        super(host);
46    }
47
48    @Override
49    public BooleanState newTileState() {
50        return new BooleanState();
51    }
52
53    @Override
54    public void setListening(boolean listening) {
55        mListening = listening;
56        if (mListening) {
57            mContext.registerReceiver(mNfcReceiver,
58                    new IntentFilter(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED));
59            if (mAdapter == null) {
60                try {
61                    mAdapter = NfcAdapter.getNfcAdapter(mContext);
62                } catch (UnsupportedOperationException e) {
63                    mAdapter = null;
64                }
65            }
66        } else {
67            mContext.unregisterReceiver(mNfcReceiver);
68        }
69    }
70
71    @Override
72    public boolean isAvailable() {
73        return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC);
74    }
75
76    @Override
77    protected void handleUserSwitch(int newUserId) {
78    }
79
80    @Override
81    public Intent getLongClickIntent() {
82        return new Intent(Settings.ACTION_NFC_SETTINGS);
83    }
84
85    @Override
86    protected void handleClick() {
87        if (mAdapter == null) return;
88        if (!mAdapter.isEnabled()) {
89            mAdapter.enable();
90        } else {
91            mAdapter.disable();
92        }
93    }
94
95    @Override
96    protected void handleSecondaryClick() {
97        handleClick();
98    }
99
100    @Override
101    public CharSequence getTileLabel() {
102        return mContext.getString(R.string.quick_settings_nfc_label);
103    }
104
105    @Override
106    protected void handleUpdateState(BooleanState state, Object arg) {
107        final Drawable mEnable = mContext.getDrawable(R.drawable.ic_qs_nfc_enabled);
108        final Drawable mDisable = mContext.getDrawable(R.drawable.ic_qs_nfc_disabled);
109        state.value = mAdapter == null ? false : mAdapter.isEnabled();
110        state.label = mContext.getString(R.string.quick_settings_nfc_label);
111        state.icon = new DrawableIcon(state.value ? mEnable : mDisable);
112        state.expandedAccessibilityClassName = 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