package com.lukekorth.pebblelocker;

import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.PreferenceCategory;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.provider.Settings;

public class WiFiNetworks extends PremiumFeatures {
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	
	public void onResume() {
		super.onResume();
		
		if(!PreferenceManager.getDefaultSharedPreferences(this).getBoolean("donated", false)) {
			requirePremiumPurchase();
		} else {
			// Root
			PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);

			// Inline preferences
			PreferenceCategory inlinePrefCat = new PreferenceCategory(this);
			inlinePrefCat.setTitle("WiFi Networks");
			root.addPreference(inlinePrefCat);

			// List stored networks
			WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
		    List<WifiConfiguration> configs = wifiManager.getConfiguredNetworks();
		    
		    if(configs == null) {
		    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
		        builder.setMessage("Wifi must be enabled to get a list of saved networks, please enable it before using this feature");
		        builder.setCancelable(false);
		        builder.setPositiveButton("Open Wifi Settings", new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
					}
		        });
		        builder.setNegativeButton("Cancel", new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						WiFiNetworks.this.finish();
					}
		        });
		        builder.show();
		    } else {
			    for (WifiConfiguration config : configs) {
			    	String ssid = WiFiNetworks.stripQuotes(config.SSID);
			    	
			    	// Checkbox preference
					CheckBoxPreference checkboxPref = new CheckBoxPreference(this);
					checkboxPref.setKey(ssid);
					checkboxPref.setTitle(ssid);
					inlinePrefCat.addPreference(checkboxPref);
			    }
	
				setPreferenceScreen(root);
		    }
		}
	}
	
	public void onDestroy() {
		super.onDestroy();
	}

	public static String stripQuotes(String input) {
		if(input.startsWith("\"") && input.endsWith("\""))
			return input.substring(1, input.length() - 1);
		else
			return input;
	}
}

@@ -2229,16 +2229,58 @@
 %09%09    %09%0A
+%09%09%09    %09if(ssid != null && ssid != %22%22) %7B%0A%09
 %09%09%09    %09
@@ -2307,16 +2307,17 @@
 ce%0A%09%09%09%09%09
+%09
 CheckBox
@@ -2369,32 +2369,33 @@
 nce(this);%0A%09%09%09%09%09
+%09
 checkboxPref.set
@@ -2401,24 +2401,25 @@
 tKey(ssid);%0A
+%09
 %09%09%09%09%09checkbo
@@ -2436,24 +2436,25 @@
 itle(ssid);%0A
+%09
 %09%09%09%09%09inlineP
@@ -2489,16 +2489,26 @@
 xPref);%0A
+%09%09%09    %09%7D%0A
 %09%09%09    %7D
@@ -2660,16 +2660,16 @@
 nput) %7B%0A
-
 %09%09if(inp
@@ -2670,16 +2670,33 @@
 if(input
+ != null && input
 .startsW

Fix NPE