1 /* 2 * Copyright 2020 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 17 package com.google.android.iwlan; 18 19 import android.content.BroadcastReceiver; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.IntentFilter; 23 import android.net.wifi.WifiManager; 24 import android.util.Log; 25 26 public class IwlanBroadcastReceiver extends BroadcastReceiver { 27 private static final String TAG = "IwlanBroadcastReceiver"; 28 29 private static boolean mIsReceiverRegistered = false; 30 private static IwlanBroadcastReceiver mInstance; 31 startListening(Context context)32 public static void startListening(Context context) { 33 if (mIsReceiverRegistered) { 34 Log.d(TAG, "startListening: Receiver already registered"); 35 return; 36 } 37 IntentFilter intentFilter = new IntentFilter(); 38 intentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); 39 intentFilter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED); 40 intentFilter.addAction(Intent.ACTION_SCREEN_ON); 41 context.registerReceiver(getInstance(), intentFilter); 42 mIsReceiverRegistered = true; 43 } 44 stopListening(Context context)45 public static void stopListening(Context context) { 46 if (!mIsReceiverRegistered) { 47 Log.d(TAG, "stopListening: Receiver not registered!"); 48 return; 49 } 50 context.unregisterReceiver(getInstance()); 51 mIsReceiverRegistered = false; 52 } 53 getInstance()54 private static IwlanBroadcastReceiver getInstance() { 55 if (mInstance == null) { 56 mInstance = new IwlanBroadcastReceiver(); 57 } 58 return mInstance; 59 } 60 61 @Override onReceive(Context context, Intent intent)62 public void onReceive(Context context, Intent intent) { 63 String action = intent.getAction(); 64 Log.d(TAG, "onReceive: " + action); 65 switch (action) { 66 case Intent.ACTION_AIRPLANE_MODE_CHANGED: 67 case WifiManager.WIFI_STATE_CHANGED_ACTION: 68 case Intent.ACTION_SCREEN_ON: 69 IwlanEventListener.onBroadcastReceived(intent); 70 break; 71 } 72 } 73 } 74