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 handleSetListening(boolean listening) {
55        mListening = listening;
56        if (mListening) {
57            mContext.registerReceiver(mNfcReceiver,
58                    new IntentFilter(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED));
59        } else {
60            mContext.unregisterReceiver(mNfcReceiver);
61        }
62    }
63
64    @Override
65    public boolean isAvailable() {
66        return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC);
67    }
68
69    @Override
70    protected void handleUserSwitch(int newUserId) {
71    }
72
73    @Override
74    public Intent getLongClickIntent() {
75        return new Intent(Settings.ACTION_NFC_SETTINGS);
76    }
77
78    @Override
79    protected void handleClick() {
80        if (!getAdapter().isEnabled()) {
81            getAdapter().enable();
82        } else {
83            getAdapter().disable();
84        }
85    }
86
87    @Override
88    protected void handleSecondaryClick() {
89        handleClick();
90    }
91
92    @Override
93    public CharSequence getTileLabel() {
94        return mContext.getString(R.string.quick_settings_nfc_label);
95    }
96
97    @Override
98    protected void handleUpdateState(BooleanState state, Object arg) {
99        final Drawable mEnable = mContext.getDrawable(R.drawable.ic_qs_nfc_enabled);
100        final Drawable mDisable = mContext.getDrawable(R.drawable.ic_qs_nfc_disabled);
101
102        if (getAdapter() == null) return;
103        state.value = getAdapter().isEnabled();
104        state.label = mContext.getString(R.string.quick_settings_nfc_label);
105        state.icon = new DrawableIcon(state.value ? mEnable : mDisable);
106        state.expandedAccessibilityClassName = Switch.class.getName();
107        state.contentDescription = state.label;
108    }
109
110    @Override
111    public int getMetricsCategory() {
112        return MetricsEvent.QS_NFC;
113    }
114
115    @Override
116    protected String composeChangeAnnouncement() {
117        if (mState.value) {
118            return mContext.getString(R.string.quick_settings_nfc_on);
119        } else {
120            return mContext.getString(R.string.quick_settings_nfc_off);
121        }
122    }
123
124    private NfcAdapter getAdapter() {
125        if (mAdapter == null) {
126            try {
127                mAdapter = NfcAdapter.getNfcAdapter(mContext);
128            } catch (UnsupportedOperationException e) {
129                mAdapter = null;
130            }
131        }
132        return mAdapter;
133    }
134
135    private BroadcastReceiver mNfcReceiver = new BroadcastReceiver() {
136        @Override
137        public void onReceive(Context context, Intent intent) {
138            refreshState();
139        }
140    };
141}
142