Home | History | Annotate | Download | only in nfc
      1 /*
      2  * Copyright (C) 2018 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.android.settings.nfc;
     17 
     18 import android.content.Context;
     19 import android.database.ContentObserver;
     20 import android.net.Uri;
     21 import android.nfc.NfcAdapter;
     22 import android.os.Handler;
     23 import android.os.Looper;
     24 import android.provider.Settings;
     25 import android.support.annotation.VisibleForTesting;
     26 import android.support.v7.preference.Preference;
     27 
     28 /**
     29  * NfcAirplaneModeObserver is a helper to manage the Nfc on/off when airplane mode status
     30  * is changed.
     31  */
     32 public class NfcAirplaneModeObserver extends ContentObserver {
     33 
     34     private final Context mContext;
     35     private final NfcAdapter mNfcAdapter;
     36     private final Preference mPreference;
     37     private int mAirplaneMode;
     38 
     39     @VisibleForTesting
     40     final static Uri AIRPLANE_MODE_URI =
     41             Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON);
     42 
     43     public NfcAirplaneModeObserver(Context context, NfcAdapter nfcAdapter, Preference preference) {
     44         super(new Handler(Looper.getMainLooper()));
     45         mContext = context;
     46         mNfcAdapter = nfcAdapter;
     47         mPreference = preference;
     48         updateNfcPreference();
     49     }
     50 
     51     public void register() {
     52         mContext.getContentResolver().registerContentObserver(AIRPLANE_MODE_URI, false, this);
     53     }
     54 
     55     public void unregister() {
     56         mContext.getContentResolver().unregisterContentObserver(this);
     57     }
     58 
     59     @Override
     60     public void onChange(boolean selfChange, Uri uri) {
     61         super.onChange(selfChange, uri);
     62         updateNfcPreference();
     63     }
     64 
     65     private void updateNfcPreference() {
     66         final int airplaneMode = Settings.Global.getInt(
     67                 mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, mAirplaneMode);
     68         if (airplaneMode == mAirplaneMode) {
     69             return;
     70         }
     71 
     72         mAirplaneMode = airplaneMode;
     73         boolean toggleable = mAirplaneMode != 1;
     74         if (toggleable) {
     75             mNfcAdapter.enable();
     76         } else {
     77             mNfcAdapter.disable();
     78         }
     79         mPreference.setEnabled(toggleable);
     80     }
     81 }
     82