/*
 * Copyright (C) 2010-2015 The MPDroid Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.namelessdev.mpdroid;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import java.util.ArrayList;
import java.util.Collection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WifiConnectionSettings extends PreferenceActivity {

    private static final String KEY_WIFI_BASED_CATEGORY = "wifibasedCategory";

    private static final String KEY_WIFI_BASED_SCREEN = "wifibasedScreen";

    private static final int MAIN = 0;

    private static final Pattern QUOTATION_DELIMITER = Pattern.compile("\"");

    private static final String TAG = "WifiConnectionSettings";

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.wificonnectionsettings);

        final SharedPreferences settings = PreferenceManager
                .getDefaultSharedPreferences(this);
        /** If the warning has never been shown before, show it. */
        if (!settings.getBoolean("newWarningShown", false)) {
            startActivity(new Intent(this, WarningActivity.class));
        }
    }

    @Override
    public boolean onCreateOptionsMenu(final Menu menu) {
        final boolean result = super.onCreateOptionsMenu(menu);
        menu.add(0, MAIN, 0, R.string.mainMenu).setIcon(android.R.drawable.ic_menu_revert);

        return result;
    }

    @Override
    public boolean onOptionsItemSelected(final MenuItem item) {
        boolean result = super.onOptionsItemSelected(item);

        if (item.getItemId() == MAIN) {
            final Intent intent = new Intent(this, MainMenuActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            result = true;
        }

        return result;
    }

    /** Method is called on any click of a preference... */
    @Override
    public boolean onPreferenceTreeClick(final PreferenceScreen preferenceScreen,
            final Preference preference) {
        Intent intent;
        final PreferenceCategory preferenceCategory =
                (PreferenceCategory) preferenceScreen.findPreference(KEY_WIFI_BASED_CATEGORY);
        final Collection<WifiConfiguration> wifiList = new ArrayList<>();

        if (preferenceCategory == null) {
            Log.e(TAG, "Failed to find PreferenceCategory: " + KEY_WIFI_BASED_CATEGORY);
        } else {
            if (preference.getKey().equals(KEY_WIFI_BASED_SCREEN)) {
                /** Clear the wifi list. */
                preferenceCategory.removeAll();

                final WifiManager wifiManager = (WifiManager) getSystemService(
                        Context.WIFI_SERVICE);
                wifiList.addAll(wifiManager.getConfiguredNetworks());
            }

            for (final WifiConfiguration wifi : wifiList) {
                if (wifi != null && wifi.SSID != null) {
                    // Friendly SSID-Name
                    final Matcher matcher = QUOTATION_DELIMITER.matcher(wifi.SSID);
                    final String ssid = matcher.replaceAll("");

                    // Add PreferenceScreen for each network
                    final PreferenceScreen ssidItem =
                            getPreferenceManager().createPreferenceScreen(this);

                    intent = new Intent(this, ConnectionSettings.class);
                    intent.putExtra("SSID", ssid);

                    ssidItem.setPersistent(false);
                    ssidItem.setKey("wifiNetwork" + ssid);
                    ssidItem.setTitle(ssid);
                    ssidItem.setIntent(intent);

                    if (WifiConfiguration.Status.CURRENT == wifi.status) {
                        ssidItem.setSummary(R.string.connected);
                    } else {
                        ssidItem.setSummary(R.string.notInRange);
                    }

                    preferenceCategory.addPreference(ssidItem);
                }
            }
        }

        return false;
    }
}

@@ -3679,16 +3679,40 @@
 anager =
+%0A                       
  (WifiMa
@@ -3739,125 +3739,564 @@
 ice(
-%0A                        Context.WIFI_SERVICE);%0A                wifiList.addAll(wifiManager.getConfiguredNetworks());
+Context.WIFI_SERVICE);%0A%0A                if (wifiManager == null) %7B%0A                    Log.e(TAG, %22Failed to retrieve the WifiManager service.%22);%0A                %7D else %7B%0A                    final Collection%3CWifiConfiguration%3E networks =%0A                            wifiManager.getConfiguredNetworks();%0A%0A                    if (networks == null) %7B%0A                        Log.e(TAG, %22Failed to retrieve a list of configured networks.%22);%0A                    %7D else %7B%0A                        wifiList.addAll(networks);%0A                    %7D%0A                %7D
 %0A   

Check that the WifiManager/Configured initialized.