Android Open Source - WifiProxySetting Placeholder Fragment






From Project

Back to project page WifiProxySetting.

License

The source code is released under:

MIT License

If you think the Android project WifiProxySetting listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.kyokomi.wifiproxysetting.ui.fragment;
//  www.  jav a 2s .c o m
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.gson.Gson;
import com.kyokomi.wifiproxysetting.DemoApplication;
import com.kyokomi.wifiproxysetting.R;

import java.util.List;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import butterknife.OnItemClick;

/**
 * A placeholder fragment containing a simple view.
 */
public class PlaceholderFragment extends DemoFragment {

    @Inject
    SharedPreferences sharedPreferences;
    @Inject
    WifiManager wifiManager;
    @Inject
    Gson gson;

    @InjectView(R.id.wifiListView)
    ListView mListView;
    @InjectView(R.id.button1)
    Button mButton1;
    @InjectView(R.id.button2)
    Button mButton2;

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);

        ((DemoApplication) getActivity().getApplication()).component().inject(this);

        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        ButterKnife.inject(this, rootView);

        showConfiguredNetworks();

        return rootView;
    }

    @OnItemClick(R.id.wifiListView)
    public void showWifiConfigDetail(int position) {
        final String ssid = (String) mListView.getAdapter().getItem(position);

        // WIFI ??ON
        if (!wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(true);
        }

        // ??????????SSID??????????
        List<WifiConfiguration> wifiConfigurations = wifiManager.getConfiguredNetworks();
        Optional<WifiConfiguration> wifiConfigurationOptional = Iterables.tryFind(wifiConfigurations, new Predicate<WifiConfiguration>() {
            @Override
            public boolean apply(WifiConfiguration input) {
                return (input.SSID.equals(ssid));
            }
        });

        if (!wifiConfigurationOptional.isPresent()) {
            // ????????????????????????
            return;
        }

        // ???????JSON??????????
        final String wifiConfig = gson.toJson(wifiConfigurationOptional.get());
        Log.d(ssid, wifiConfig);

        // ????????????????
        AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this.getActivity());
        // TODO: ????
        alertBuilder.setTitle("Wifi?????");
        alertBuilder.setMessage(wifiConfig);
        alertBuilder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (sharedPreferences.contains(ssid)) {
                    String settingJson = sharedPreferences.getString(ssid, null);
                    Log.d("Preferences : " + ssid, settingJson);
                } else {
                    sharedPreferences.edit().putString(ssid, wifiConfig).apply();
                }
            }
        });
        // Save???????????load????????
        if (sharedPreferences.contains(ssid)) {
            alertBuilder.setNeutralButton("Load", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String settingJson = sharedPreferences.getString(ssid, null);
                    Log.d("Preferences : " + ssid, settingJson);
                    // ??
                    WifiConfiguration wifiConfiguration = gson.fromJson(settingJson,
                            WifiConfiguration.class);
                    wifiManager.updateNetwork(wifiConfiguration);
                }
            });
        }
        alertBuilder.setNegativeButton("Close", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // ??????????????
            }
        });
        // ????????????????????????
        alertBuilder.setCancelable(true);
        AlertDialog alertDialog = alertBuilder.create();
        // ????????????
        alertDialog.show();
    }

    @OnClick(R.id.button1)
    public void showConfiguredNetworks() {
        // WIFI on
        if (!wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(true);
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1);

        List<WifiConfiguration> wifiConfigurations = wifiManager.getConfiguredNetworks();
        for (WifiConfiguration wifiConfiguration : wifiConfigurations) {
            adapter.add(wifiConfiguration.SSID);
        }
        mListView.setAdapter(adapter);
    }

    @OnClick(R.id.button2)
    public void showScanResults() {
        // WIFI on
        if (!wifiManager.isWifiEnabled()) {
            wifiManager.setWifiEnabled(true);
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1);

        List<ScanResult> scanResultList = wifiManager.getScanResults();
        for (ScanResult scanResult : scanResultList) {
            adapter.add(scanResult.SSID);
        }
        mListView.setAdapter(adapter);
    }
}




Java Source Code List

com.kyokomi.wifiproxysetting.AndroidModule.java
com.kyokomi.wifiproxysetting.ApplicationTest.java
com.kyokomi.wifiproxysetting.DemoApplication.java
com.kyokomi.wifiproxysetting.ForApplication.java
com.kyokomi.wifiproxysetting.ui.activity.DemoActivity.java
com.kyokomi.wifiproxysetting.ui.activity.MainActivity.java
com.kyokomi.wifiproxysetting.ui.fragment.DemoFragment.java
com.kyokomi.wifiproxysetting.ui.fragment.PlaceholderFragment.java