|
|
@@ -0,0 +1,159 @@
|
|
|
+package com.example.david.libretasker.InputSelector;
|
|
|
+
|
|
|
+import android.content.Context;
|
|
|
+import android.net.Uri;
|
|
|
+import android.net.wifi.WifiConfiguration;
|
|
|
+import android.net.wifi.WifiManager;
|
|
|
+import android.os.Bundle;
|
|
|
+import android.support.v4.app.Fragment;
|
|
|
+import android.support.v4.app.FragmentTransaction;
|
|
|
+import android.view.LayoutInflater;
|
|
|
+import android.view.View;
|
|
|
+import android.view.ViewGroup;
|
|
|
+import android.widget.AdapterView;
|
|
|
+import android.widget.ArrayAdapter;
|
|
|
+import android.widget.Spinner;
|
|
|
+
|
|
|
+import com.example.david.libretasker.R;
|
|
|
+
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * A simple {@link Fragment} subclass.
|
|
|
+ * Activities that contain this fragment must implement the
|
|
|
+ * {@link Network.OnFragmentInteractionListener} interface
|
|
|
+ * to handle interaction events.
|
|
|
+ * Use the {@link Network#newInstance} factory method to
|
|
|
+ * create an instance of this fragment.
|
|
|
+ */
|
|
|
+public class Network extends Fragment {
|
|
|
+ // TODO: Rename parameter arguments, choose names that match
|
|
|
+ // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
|
|
|
+ private static final String ARG_PARAM1 = "param1";
|
|
|
+ private static final String ARG_PARAM2 = "param2";
|
|
|
+
|
|
|
+ // TODO: Rename and change types of parameters
|
|
|
+ private String mParam1;
|
|
|
+ private String mParam2;
|
|
|
+
|
|
|
+ private OnFragmentInteractionListener mListener;
|
|
|
+
|
|
|
+ public Network() {
|
|
|
+ // Required empty public constructor
|
|
|
+ }
|
|
|
+
|
|
|
+ // TODO: Rename and change types and number of parameters
|
|
|
+ public static Network newInstance() {
|
|
|
+ return new Network();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onCreate(Bundle savedInstanceState) {
|
|
|
+ super.onCreate(savedInstanceState);
|
|
|
+ if (getArguments() != null) {
|
|
|
+ mParam1 = getArguments().getString(ARG_PARAM1);
|
|
|
+ mParam2 = getArguments().getString(ARG_PARAM2);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
|
|
+ Bundle savedInstanceState) {
|
|
|
+ // Inflate the layout for this fragment
|
|
|
+ View v = inflater.inflate(R.layout.fragment_wifi, container, false);
|
|
|
+ Spinner state = (Spinner) v.findViewById(R.id.NetworkStateSpinner);
|
|
|
+ List<String> l = Arrays.asList("Connected", "Connecting", "Disconnected", "Disconnecting");
|
|
|
+ final ArrayAdapter<CharSequence> adapter = new ArrayAdapter(getContext(), android.R.layout.simple_spinner_item, l);
|
|
|
+ adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
|
|
+ // Apply the adapter to the spinner
|
|
|
+ state.setAdapter(adapter);
|
|
|
+
|
|
|
+ Spinner type = (Spinner) v.findViewById(R.id.NetworkTypeSpinner);
|
|
|
+ List<String> ltype = Arrays.asList("Wifi", "Mobile", "VPN");
|
|
|
+ final ArrayAdapter<CharSequence> typeadapter = new ArrayAdapter(getContext(), android.R.layout.simple_spinner_item, ltype);
|
|
|
+ typeadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
|
|
|
+ // Apply the adapter to the spinner
|
|
|
+ type.setAdapter(typeadapter);
|
|
|
+
|
|
|
+ type.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
|
|
|
+ Fragment f = null;
|
|
|
+ @Override
|
|
|
+ public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long id) {
|
|
|
+ if ( typeadapter.getItem(pos) == null )
|
|
|
+ return;
|
|
|
+ FragmentTransaction ft = getFragmentManager().beginTransaction();
|
|
|
+
|
|
|
+ if ( typeadapter.getItem(pos).equals("Wifi")) {
|
|
|
+ String[] a = new String[]{"On", "Off", "Any"};
|
|
|
+ f = MultiList.newInstance("SSIDs", connectionInfo());
|
|
|
+ ft.replace(R.id.networkFrame, f);
|
|
|
+ ft.commit();
|
|
|
+ } else {
|
|
|
+ if ( f != null ) {
|
|
|
+ ft.remove(f);
|
|
|
+ ft.commit();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onNothingSelected(AdapterView<?> adapterView) {
|
|
|
+
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ return v;
|
|
|
+ }
|
|
|
+
|
|
|
+ // TODO: Rename method, update argument and hook method into UI event
|
|
|
+ public void onButtonPressed(Uri uri) {
|
|
|
+ if (mListener != null) {
|
|
|
+ mListener.onFragmentInteraction(uri);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onAttach(Context context) {
|
|
|
+ super.onAttach(context);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onDetach() {
|
|
|
+ super.onDetach();
|
|
|
+ mListener = null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * This interface must be implemented by activities that contain this
|
|
|
+ * fragment to allow an interaction in this fragment to be communicated
|
|
|
+ * to the activity and potentially other fragments contained in that
|
|
|
+ * activity.
|
|
|
+ * <p>
|
|
|
+ * See the Android Training lesson <a href=
|
|
|
+ * "http://developer.android.com/training/basics/fragments/communicating.html"
|
|
|
+ * >Communicating with Other Fragments</a> for more information.
|
|
|
+ */
|
|
|
+ public interface OnFragmentInteractionListener {
|
|
|
+ // TODO: Update argument type and name
|
|
|
+ void onFragmentInteraction(Uri uri);
|
|
|
+ }
|
|
|
+
|
|
|
+ private String[] connectionInfo() {
|
|
|
+ WifiManager wm = (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE);
|
|
|
+ if ( !wm.isWifiEnabled() ) {
|
|
|
+ wm.setWifiEnabled(true);
|
|
|
+ }
|
|
|
+ List<WifiConfiguration> configs = wm.getConfiguredNetworks();
|
|
|
+ if (configs == null)
|
|
|
+ return null;
|
|
|
+
|
|
|
+ String[] SSIDs = new String[configs.size()];
|
|
|
+ int i = 0;
|
|
|
+ for (WifiConfiguration c : configs)
|
|
|
+ SSIDs[i++] = c.SSID.substring(1, c.SSID.length()-1);
|
|
|
+ Arrays.sort(SSIDs);
|
|
|
+ return SSIDs;
|
|
|
+ }
|
|
|
+}
|