• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.android.pmc;
2 
3 import android.app.AlarmManager;
4 import android.app.PendingIntent;
5 import android.content.BroadcastReceiver;
6 import android.content.Context;
7 import android.content.Intent;
8 import android.content.IntentFilter;
9 import android.net.wifi.WifiManager;
10 import android.os.AsyncTask;
11 import android.os.PowerManager;
12 import android.os.SystemClock;
13 import android.util.Log;
14 
15 /**
16  * Call wifi scan whenever an alarm is received.
17  */
18 public class WifiConnScanReceiver extends BroadcastReceiver {
19     int mScanCount = 0;
20     ConnectvityScanTask mConnScanTask;
21     PMCMainActivity mPMCMainActivity;
22     private WifiManager mWifiManager;
23     private Context mContext;
24     private PowerManager.WakeLock mWakeLock;
25     private int mAlarmInterval;
26     private AlarmManager mAlarmManager;
27     private PendingIntent mAlarmIntent;
28 
WifiConnScanReceiver(PMCMainActivity activity, int interval, AlarmManager alarmManager, PendingIntent alarmIntent)29     public WifiConnScanReceiver(PMCMainActivity activity, int interval, AlarmManager alarmManager,
30                                 PendingIntent alarmIntent) {
31         mPMCMainActivity = activity;
32         mScanCount = 0;
33         mAlarmInterval = interval;
34         mAlarmManager = alarmManager;
35         mAlarmIntent = alarmIntent;
36     }
37 
38     @Override
onReceive(Context context, Intent intent)39     public void onReceive(Context context, Intent intent) {
40         if (mConnScanTask != null && mConnScanTask.getStatus() != AsyncTask.Status.FINISHED) {
41             Log.e(PMCMainActivity.TAG, "Previous connection scan still running.");
42             try {
43                 mConnScanTask.get();
44             } catch (Exception e) {
45                 Log.e(PMCMainActivity.TAG, "Connection scan cancelled.");
46             }
47         } else {
48             mContext = context;
49             PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
50             mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "WIFITEST");
51             // Acquire the lock
52             mWakeLock.acquire();
53             mWifiManager = (WifiManager) context.getApplicationContext()
54                     .getSystemService(Context.WIFI_SERVICE);
55             Log.i(PMCMainActivity.TAG, "Starting Connectivity Scan Task");
56             mConnScanTask = new ConnectvityScanTask();
57             mConnScanTask.execute();
58         }
59         scheduleConnScan();
60     }
61 
62     /**
63      * Schedule the next connectivity scan.
64      */
scheduleConnScan()65     public void scheduleConnScan() {
66         Log.i(PMCMainActivity.TAG, "Scheduling the next conn scan after " + mAlarmInterval);
67         mAlarmManager.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP,
68                 SystemClock.elapsedRealtime() + mAlarmInterval, mAlarmIntent);
69     }
70 
71     /**
72      * Cancel the connectivity scans.
73      */
cancelConnScan()74     public void cancelConnScan() {
75         mAlarmManager.cancel(mAlarmIntent);
76         if (mConnScanTask != null) mConnScanTask.cancel(true);
77     }
78 
79     class ConnectvityScanTask extends AsyncTask<Integer, Integer, String> {
80         WifiScanReceiver mWifiScanReceiver;
81         Boolean mScanCompleted = false;
82 
ConnectvityScanTask()83         ConnectvityScanTask() {
84             mWifiScanReceiver = new WifiScanReceiver();
85             mContext.getApplicationContext().registerReceiver(mWifiScanReceiver,
86                     new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
87         }
88 
89         @Override
doInBackground(Integer... stime)90         protected String doInBackground(Integer... stime) {
91             //android.os.Debug.waitForDebugger();
92             int waitCount = 0;
93             try {
94                 mScanCompleted = false;
95                 mWifiManager.startScan();
96                 while (!mScanCompleted) {
97                     if (waitCount >= 100) {
98                         return "Timeout, scan results avaiable action didn't triggered";
99                     } else {
100                         Thread.sleep(100);
101                         waitCount += 1;
102                     }
103                 }
104                 waitCount = 0;
105                 mScanCount += 1;
106                 Log.d(PMCMainActivity.TAG, "Number of scan completed " + mScanCount);
107                 publishProgress(mScanCount);
108             } catch (Exception e) {
109                 Log.e(PMCMainActivity.TAG, e.toString());
110                 return e.toString();
111             }
112             return null;
113         }
114 
115         @Override
onCancelled(String result)116         protected void onCancelled(String result) {
117             mContext.getApplicationContext().unregisterReceiver(mWifiScanReceiver);
118             mWakeLock.release();
119         }
120 
121         @Override
onProgressUpdate(Integer... values)122         protected void onProgressUpdate(Integer... values) {
123             Log.d(PMCMainActivity.TAG, "ConnectvityScanTask onProgressUpdate updating the UI");
124             mPMCMainActivity.updateProgressStatus("Total Connectivity scan completed :: "
125                     + Integer.toString(values[0].intValue()));
126         }
127 
128         @Override
onPostExecute(String error)129         protected void onPostExecute(String error) {
130             if (error != null) {
131                 Log.e(PMCMainActivity.TAG, error);
132                 mPMCMainActivity.updateProgressStatus(error);
133             }
134             mContext.getApplicationContext().unregisterReceiver(mWifiScanReceiver);
135             mWakeLock.release();
136         }
137 
138         class WifiScanReceiver extends BroadcastReceiver {
139             @Override
onReceive(Context c, Intent intent)140             public void onReceive(Context c, Intent intent) {
141                 String action = intent.getAction();
142                 if (action.equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
143                     Log.d(PMCMainActivity.TAG, "Wifi connection scan finished, results available.");
144                     mScanCompleted = true;
145                 }
146             }
147         }
148     }
149 }
150