|
| 1 | +package com.example.tobiastrumm.freifunkautoconnect; |
| 2 | + |
| 3 | +import android.app.NotificationManager; |
| 4 | +import android.app.PendingIntent; |
| 5 | +import android.app.Service; |
| 6 | +import android.content.BroadcastReceiver; |
| 7 | +import android.content.Context; |
| 8 | +import android.content.Intent; |
| 9 | +import android.content.IntentFilter; |
| 10 | +import android.net.wifi.ScanResult; |
| 11 | +import android.net.wifi.WifiManager; |
| 12 | +import android.os.Environment; |
| 13 | +import android.os.IBinder; |
| 14 | +import android.provider.Settings; |
| 15 | +import android.support.v4.app.NotificationCompat; |
| 16 | +import android.util.Log; |
| 17 | + |
| 18 | +import java.io.BufferedReader; |
| 19 | +import java.io.File; |
| 20 | +import java.io.FileInputStream; |
| 21 | +import java.io.IOException; |
| 22 | +import java.io.InputStreamReader; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.Collections; |
| 25 | +import java.util.List; |
| 26 | + |
| 27 | +/** |
| 28 | + * Created by Tobias on 29.07.2015. |
| 29 | + */ |
| 30 | +public class NotificationService extends Service { |
| 31 | + |
| 32 | + private final static String TAG = NotificationService.class.getSimpleName(); |
| 33 | + private final static int NOTIFICATION_ID = 1; |
| 34 | + |
| 35 | + private List<Network> networks; |
| 36 | + private List<Network> foundNetworks = new ArrayList<>(); |
| 37 | + |
| 38 | + private WifiReceiver wifiReceiver; |
| 39 | + |
| 40 | + private NotificationManager mNotificationManager; |
| 41 | + |
| 42 | + private class WifiReceiver extends BroadcastReceiver{ |
| 43 | + private WifiReceiver(){} |
| 44 | + |
| 45 | + @Override |
| 46 | + public void onReceive(Context context, Intent intent) { |
| 47 | + Log.d(TAG, "onReceive was called"); |
| 48 | + boolean alreadyConnected = false; |
| 49 | + |
| 50 | + WifiManager wm = (WifiManager) getSystemService(Context.WIFI_SERVICE); |
| 51 | + List<ScanResult> results = wm.getScanResults(); |
| 52 | + List<Network> oldFoundNetworks = new ArrayList<>(foundNetworks); |
| 53 | + foundNetworks = new ArrayList<>(); |
| 54 | + |
| 55 | + if(results != null){ |
| 56 | + // Look if known Freifunk networks are in reach. |
| 57 | + for(ScanResult r: results){ |
| 58 | + Network compareWith = new Network('"' + r.SSID + '"'); |
| 59 | + if(networks.contains(compareWith)){ |
| 60 | + foundNetworks.add(compareWith); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + // Look if device is already connected to a known Freifunk network |
| 65 | + String currentSSID = wm.getConnectionInfo().getSSID(); |
| 66 | + if(currentSSID != null){ |
| 67 | + Log.d(TAG, "Check if " + currentSSID + " is a Freifunk network"); |
| 68 | + Network compareWith = new Network(currentSSID); |
| 69 | + if(foundNetworks.contains(compareWith)){ |
| 70 | + alreadyConnected = true; |
| 71 | + Log.d(TAG, "Already connected with " + compareWith.ssid); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // Look if this scan found different Freifunk networks than the previous scan. |
| 76 | + boolean sameResults = oldFoundNetworks.containsAll(foundNetworks); |
| 77 | + |
| 78 | + String foundNetworksStr = ""; |
| 79 | + for(Network n: foundNetworks){ |
| 80 | + foundNetworksStr += n.ssid + ", "; |
| 81 | + } |
| 82 | + Log.d(TAG, "Found networks: " + foundNetworksStr); |
| 83 | + |
| 84 | + String oldfoundNetworksStr = ""; |
| 85 | + for(Network n: oldFoundNetworks){ |
| 86 | + oldfoundNetworksStr += n.ssid + ", "; |
| 87 | + } |
| 88 | + Log.d(TAG, "Old Found networks: " + oldfoundNetworksStr); |
| 89 | + |
| 90 | + Log.d(TAG, "!isEmpty: " + !foundNetworks.isEmpty() + " !sameResults: " + !sameResults + " !alreadyConnected: " + !alreadyConnected); |
| 91 | + // Cancel the notification if no Freifunk network is in reach anymore. |
| 92 | + if(foundNetworks.isEmpty()){ |
| 93 | + mNotificationManager.cancel(NOTIFICATION_ID); |
| 94 | + } |
| 95 | + if(!foundNetworks.isEmpty() && !sameResults && !alreadyConnected){ |
| 96 | + sendNotification(foundNetworks); |
| 97 | + Log.d(TAG, "sendNotification was called"); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private void sendNotification(List<Network> networks){ |
| 103 | + String text = getString(R.string.notification_text); |
| 104 | + for(Network n: networks){ |
| 105 | + text += n.ssid + ", "; |
| 106 | + } |
| 107 | + text = text.substring(0, text.length()-2); |
| 108 | + |
| 109 | + NotificationCompat.Builder mBuilder = |
| 110 | + new NotificationCompat.Builder(NotificationService.this) |
| 111 | + .setSmallIcon(R.mipmap.ic_wifi_white_48dp) |
| 112 | + .setContentTitle(getString(R.string.notification_title)) |
| 113 | + .setContentText(text) |
| 114 | + .setContentIntent(PendingIntent.getActivity(NotificationService.this,0, new Intent(Settings.ACTION_WIFI_SETTINGS),0)); |
| 115 | + |
| 116 | + mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); |
| 117 | + Log.d(TAG, "Notification was sent."); |
| 118 | + } |
| 119 | + |
| 120 | + |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public IBinder onBind(Intent intent) { |
| 125 | + return null; |
| 126 | + } |
| 127 | + |
| 128 | + |
| 129 | + @Override |
| 130 | + public int onStartCommand(Intent intent, int flags, int startId) { |
| 131 | + Log.d(TAG, "NotificationService was started"); |
| 132 | + networks = new ArrayList<>(); |
| 133 | + try{ |
| 134 | + getSSIDs(); |
| 135 | + } |
| 136 | + catch(IOException e){ |
| 137 | + Log.w(TAG, "Could not read SSIDs from csv file."); |
| 138 | + stopSelf(); |
| 139 | + } |
| 140 | + |
| 141 | + // Register broadcast receiver |
| 142 | + wifiReceiver = new WifiReceiver(); |
| 143 | + registerReceiver(wifiReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); |
| 144 | + return START_STICKY; |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public void onCreate() { |
| 149 | + super.onCreate(); |
| 150 | + |
| 151 | + mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public void onDestroy() { |
| 156 | + Log.d(TAG, "On destroyed was called"); |
| 157 | + unregisterReceiver(wifiReceiver); |
| 158 | + mNotificationManager.cancel(NOTIFICATION_ID); |
| 159 | + super.onDestroy(); |
| 160 | + } |
| 161 | + |
| 162 | + private void getSSIDs() throws IOException { |
| 163 | + InputStreamReader is = new InputStreamReader(getAssets().open("ssids.csv")); |
| 164 | + BufferedReader reader = new BufferedReader(is); |
| 165 | + String line; |
| 166 | + while ((line = reader.readLine()) != null) { |
| 167 | + networks.add(new Network(line)); |
| 168 | + } |
| 169 | + |
| 170 | + // Read user defined ssids |
| 171 | + // Check if external storage is available |
| 172 | + String state = Environment.getExternalStorageState(); |
| 173 | + if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { |
| 174 | + File user_ssids = new File(Environment.getExternalStorageDirectory() + File.separator + MainActivity.DIRECTORY + File.separator + MainActivity.USER_SSIDS_FILE ); |
| 175 | + // Check if file exists |
| 176 | + // If the file was found/created: |
| 177 | + if(user_ssids.exists()){ |
| 178 | + is = new InputStreamReader(new FileInputStream(user_ssids)); |
| 179 | + reader = new BufferedReader(is); |
| 180 | + while ((line = reader.readLine()) != null) { |
| 181 | + networks.add(new Network(line)); |
| 182 | + } |
| 183 | + } |
| 184 | + else{ |
| 185 | + Log.w(TAG, "Could not find."); |
| 186 | + } |
| 187 | + } |
| 188 | + Collections.sort(networks); |
| 189 | + } |
| 190 | +} |
0 commit comments