SelectFromListWizardFragment.java revision 65a5a7d84ad9b5324ae53eda526e39e513473af7
165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane/*
265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Copyright (C) 2014 The Android Open Source Project
365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Licensed under the Apache License, Version 2.0 (the "License");
565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * you may not use this file except in compliance with the License.
665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * You may obtain a copy of the License at
765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *      http://www.apache.org/licenses/LICENSE-2.0
965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
1065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Unless required by applicable law or agreed to in writing, software
1165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * distributed under the License is distributed on an "AS IS" BASIS,
1265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * See the License for the specific language governing permissions and
1465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * limitations under the License.
1565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane */
1665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
1765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lanepackage com.android.tv.settings.connectivity.setup;
1865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
1965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport com.android.tv.settings.R;
2065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport com.android.tv.settings.connectivity.WifiSecurity;
2165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport com.android.tv.settings.util.AccessibilityHelper;
2265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport com.android.tv.settings.widget.ScrollAdapterView;
2365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport com.android.tv.settings.widget.ScrollArrayAdapter;
2465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
2565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.app.Activity;
2665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.app.Fragment;
2765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.content.Context;
2865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.net.wifi.ScanResult;
2965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.net.wifi.WifiManager;
3065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.os.Bundle;
3165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.os.Handler;
3265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.os.Parcel;
3365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.os.Parcelable;
3465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.text.TextUtils;
3565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.util.Log;
3665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.view.KeyEvent;
3765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.view.LayoutInflater;
3865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.view.View;
3965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.view.ViewGroup;
4065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.view.inputmethod.InputMethodManager;
4165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.widget.AdapterView;
4265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.widget.EditText;
4365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.widget.FrameLayout;
4465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.widget.ImageView;
4565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.widget.TextView;
4665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
4765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport java.util.ArrayList;
4865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport java.util.Comparator;
4965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
5065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane/**
5165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Displays a UI for selecting a wifi network from a list in the "wizard" style.
5265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane */
5365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lanepublic class SelectFromListWizardFragment extends Fragment {
5465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
5565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String TAG = "SelectFromListWizardFragment";
5665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final boolean DEBUG = false;
5765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
5865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public static class ListItemComparator implements Comparator<ListItem> {
5965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        @Override
6065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        public int compare(ListItem o1, ListItem o2) {
6165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            ScanResult o1ScanResult = o1.getScanResult();
6265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            ScanResult o2ScanResult = o2.getScanResult();
6365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (o1ScanResult == null) {
6465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                if (o2ScanResult == null) {
6565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    return 0;
6665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                } else {
6765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    return 1;
6865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
6965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            } else {
7065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                if (o2ScanResult == null) {
7165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    return -1;
7265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                } else {
7365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    int levelDiff = o2ScanResult.level - o1ScanResult.level;
7465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    if (levelDiff != 0) {
7565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        return levelDiff;
7665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    }
7765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    return o1ScanResult.SSID.compareTo(o2ScanResult.SSID);
7865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
7965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
8065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
8165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
8265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
8365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public static class ListItem implements Parcelable {
8465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
8565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        private final String mName;
8665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        private final int mIconResource;
8765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        private final int mIconLevel;
8865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        private final boolean mHasIconLevel;
8965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        private final ScanResult mScanResult;
9065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
9165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        public ListItem(String name, int iconResource) {
9265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mName = name;
9365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mIconResource = iconResource;
9465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mIconLevel = 0;
9565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mHasIconLevel = false;
9665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mScanResult = null;
9765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
9865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
9965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        public ListItem(ScanResult scanResult) {
10065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mName = scanResult.SSID;
10165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mIconResource = WifiSecurity.isOpen(scanResult) ? R.drawable.setup_wifi_signal_open
10265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    : R.drawable.setup_wifi_signal_lock;
10365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mIconLevel = WifiManager.calculateSignalLevel(scanResult.level, 4);
10465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mHasIconLevel = true;
10565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mScanResult = scanResult;
10665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
10765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
10865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        public String getName() {
10965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return mName;
11065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
11165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
11265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        int getIconResource() {
11365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return mIconResource;
11465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
11565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
11665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        int getIconLevel() {
11765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return mIconLevel;
11865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
11965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
12065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        boolean hasIconLevel() {
12165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return mHasIconLevel;
12265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
12365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
12465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        ScanResult getScanResult() {
12565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return mScanResult;
12665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
12765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
12865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        @Override
12965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        public String toString() {
13065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return mName;
13165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
13265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
13365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        public static Parcelable.Creator<ListItem> CREATOR = new Parcelable.Creator<ListItem>() {
13465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
13565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            @Override
13665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            public ListItem createFromParcel(Parcel source) {
13765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                ScanResult scanResult = (ScanResult) source.readParcelable(
13865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        ScanResult.class.getClassLoader());
13965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                if (scanResult == null) {
14065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    return new ListItem(source.readString(), source.readInt());
14165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                } else {
14265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    return new ListItem(scanResult);
14365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
14465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
14565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
14665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            @Override
14765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            public ListItem[] newArray(int size) {
14865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                return new ListItem[size];
14965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
15065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        };
15165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
15265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        @Override
15365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        public int describeContents() {
15465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return 0;
15565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
15665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
15765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        @Override
15865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        public void writeToParcel(Parcel dest, int flags) {
15965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            dest.writeParcelable(mScanResult, flags);
16065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (mScanResult == null) {
16165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                dest.writeString(mName);
16265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                dest.writeInt(mIconResource);
16365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
16465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
16565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
16665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        @Override
16765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        public boolean equals(Object o) {
16865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (o instanceof ListItem) {
16965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                ListItem li = (ListItem) o;
17065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                if (mScanResult == null && li.mScanResult == null) {
17165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    return mName.equals(li.mName);
17265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
17365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                return (mScanResult != null && li.mScanResult != null && mName.equals(li.mName) &&
17465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        WifiSecurity.getSecurity(mScanResult)
17565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        == WifiSecurity.getSecurity(li.mScanResult));
17665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
17765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return false;
17865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
17965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
18065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
18165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public interface Listener {
18265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        void onListSelectionComplete(ListItem listItem);
18365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        void onListFocusChanged(ListItem listItem);
18465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
18565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
18665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String EXTRA_TITLE = "title";
18765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String EXTRA_DESCRIPTION = "description";
18865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String EXTRA_LIST_ELEMENTS = "list_elements";
18965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String EXTRA_LAST_SELECTION = "last_selection";
19065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
19165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public static SelectFromListWizardFragment newInstance(String title, String description,
19265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            ArrayList<ListItem> listElements, ListItem lastSelection) {
19365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        SelectFromListWizardFragment fragment = new SelectFromListWizardFragment();
19465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        Bundle args = new Bundle();
19565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        args.putString(EXTRA_TITLE, title);
19665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        args.putString(EXTRA_DESCRIPTION, description);
19765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        args.putParcelableArrayList(EXTRA_LIST_ELEMENTS, listElements);
19865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        args.putParcelable(EXTRA_LAST_SELECTION, lastSelection);
19965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        fragment.setArguments(args);
20065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return fragment;
20165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
20265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
20365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private Handler mHandler;
20465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private View mMainView;
20565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private ScrollArrayAdapter<ListItem> mAdapter;
20665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private ScrollAdapterView mScrollAdapterView;
20765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private ArrayList<ListItem> mListItems;
20865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private ListItem mLastSelected;
20965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
21065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public void update(ArrayList<ListItem> listElements) {
21165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        ListItem lastSelection = mLastSelected;
21265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mListItems = listElements;
21365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mAdapter.clear();
21465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mAdapter.addAll(listElements);
21565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (lastSelection != null) {
21665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            for (int i = 0, size = listElements.size(); i < size; i++) {
21765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                if (lastSelection.equals(listElements.get(i))) {
21865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    if (DEBUG) {
21965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        Log.d(TAG, "Found " + lastSelection.getName() + " with ssid "
22065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                                + (mLastSelected.getScanResult() == null ? null
22165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                                        : mLastSelected.getScanResult().SSID)
22265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                                + " and bssid " + (mLastSelected.getScanResult() == null ? null
22365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                                        : mLastSelected.getScanResult().BSSID));
22465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    }
22565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    mScrollAdapterView.setSelection(i);
22665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    break;
22765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
22865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
22965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
23065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
23165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
23265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    @Override
23365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle icicle) {
23465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mHandler = new Handler();
23565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mMainView = inflater.inflate(R.layout.account_content_area, container, false);
23665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        View content = inflater.inflate(R.layout.wifi_content, null);
23765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mScrollAdapterView = (ScrollAdapterView) inflater.inflate(
23865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                R.layout.setup_scroll_adapter_view, null);
23965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        ((ViewGroup) mMainView.findViewById(R.id.description)).addView(content);
24065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        ((ViewGroup) mMainView.findViewById(R.id.action)).addView(mScrollAdapterView);
24165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
24265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        TextView titleText = (TextView) content.findViewById(R.id.title_text);
24365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        TextView descriptionText = (TextView) content.findViewById(R.id.description_text);
24465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
24565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        Bundle args = getArguments();
24665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        String title = args.getString(EXTRA_TITLE);
24765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        String description = args.getString(EXTRA_DESCRIPTION);
24865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mListItems = args.getParcelableArrayList(EXTRA_LIST_ELEMENTS);
24965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        ListItem lastSelection = args.getParcelable(EXTRA_LAST_SELECTION);
25065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
25165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mAdapter = new ScrollArrayAdapter<ListItem>(getActivity(), R.layout.setup_list_item,
25265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                R.id.list_item_text, mListItems) {
25365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            @Override
25465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            public View getView(int position, View convertView, ViewGroup parent) {
25565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                View v = super.getView(position, convertView, parent);
25665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                ListItem item = getItem(position);
25765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                FrameLayout iconHolder = (FrameLayout) v.findViewById(R.id.list_item_icon);
25865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                iconHolder.removeAllViews();
25965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                Activity a = getActivity();
26065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                if (a != null) {
26165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    ImageView icon = new ImageView(a);
26265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    icon.setImageResource(item.getIconResource());
26365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    if (item.hasIconLevel()) {
26465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        icon.setImageLevel(item.getIconLevel());
26565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    }
26665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    iconHolder.addView(icon);
26765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
26865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                return v;
26965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
27065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        };
27165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mScrollAdapterView.setAdapter(mAdapter);
27265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
27365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        boolean forceFocusable = AccessibilityHelper.forceFocusableViews(getActivity());
27465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (title != null) {
27565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            titleText.setText(title);
27665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            titleText.setVisibility(View.VISIBLE);
27765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (forceFocusable) {
27865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                titleText.setFocusable(true);
27965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                titleText.setFocusableInTouchMode(true);
28065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
28165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } else {
28265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            titleText.setVisibility(View.GONE);
28365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
28465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
28565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (description != null) {
28665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            descriptionText.setText(description);
28765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            descriptionText.setVisibility(View.VISIBLE);
28865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (forceFocusable) {
28965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                descriptionText.setFocusable(true);
29065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                descriptionText.setFocusableInTouchMode(true);
29165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
29265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } else {
29365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            descriptionText.setVisibility(View.GONE);
29465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
29565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
29665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (lastSelection != null) {
29765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            for (int i = 0, size = mListItems.size(); i < size; i++) {
29865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                if (lastSelection.equals(mListItems.get(i))) {
29965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    mScrollAdapterView.setSelection(i);
30065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    break;
30165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
30265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
30365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
30465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
30565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mScrollAdapterView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
30665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            @Override
30765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            public void onItemClick(AdapterView<?> pare, View view, int position, long id) {
30865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                Activity a = getActivity();
30965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                if (a instanceof Listener) {
31065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    ((Listener) a).onListSelectionComplete(mListItems.get(position));
31165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
31265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
31365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        });
31465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
31565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mScrollAdapterView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
31665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            @Override
31765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
31865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                mLastSelected = mListItems.get(position);
31965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                if (DEBUG) {
32065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    Log.d(TAG, "Scrolled to " + mLastSelected.getName() + " with ssid "
32165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                            + (mLastSelected.getScanResult() == null ? null
32265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                                    : mLastSelected.getScanResult().SSID) + " and bssid "
32365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                            + (mLastSelected.getScanResult() == null ? null
32465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                                    : mLastSelected.getScanResult().BSSID));
32565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
32665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                Activity a = getActivity();
32765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                if (a instanceof Listener) {
32865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    ((Listener) a).onListFocusChanged(mLastSelected);
32965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
33065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
33165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
33265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            @Override
33365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            public void onNothingSelected(AdapterView<?> parent) {
33465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
33565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
33665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        });
33765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
33865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return mMainView;
33965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
34065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
34165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    @Override
34265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public void onResume() {
34365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        super.onResume();
34465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mHandler.post(new Runnable() {
34565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            @Override
34665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            public void run() {
34765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                InputMethodManager inputMethodManager = (InputMethodManager) getActivity()
34865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        .getSystemService(Context.INPUT_METHOD_SERVICE);
34965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                inputMethodManager.hideSoftInputFromWindow(
35065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        mMainView.getApplicationWindowToken(), 0);
35165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
35265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        });
35365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
35465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane}
355