SelectFromListWizardFragment.java revision 48fce31d5176b2f4100da514dd46a45f24e41c84
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 android.app.Activity;
2065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.app.Fragment;
2165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.content.Context;
2265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.net.wifi.ScanResult;
2365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.net.wifi.WifiManager;
2465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.os.Bundle;
2565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.os.Handler;
2665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.os.Parcel;
2765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.os.Parcelable;
2865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.view.LayoutInflater;
2965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.view.View;
3065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.view.ViewGroup;
3165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.view.inputmethod.InputMethodManager;
3265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.widget.AdapterView;
3365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.widget.FrameLayout;
3465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.widget.ImageView;
3565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.widget.TextView;
3665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
376316b89e65828d537da309851529d1295efd5730Tony Mantlerimport com.android.tv.settings.R;
386316b89e65828d537da309851529d1295efd5730Tony Mantlerimport com.android.tv.settings.connectivity.WifiSecurity;
396316b89e65828d537da309851529d1295efd5730Tony Mantlerimport com.android.tv.settings.util.AccessibilityHelper;
406316b89e65828d537da309851529d1295efd5730Tony Mantlerimport com.android.tv.settings.widget.ScrollAdapterView;
416316b89e65828d537da309851529d1295efd5730Tony Mantlerimport com.android.tv.settings.widget.ScrollArrayAdapter;
426316b89e65828d537da309851529d1295efd5730Tony Mantler
4365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport java.util.ArrayList;
4465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport java.util.Comparator;
4565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
4665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane/**
4765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Displays a UI for selecting a wifi network from a list in the "wizard" style.
4865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane */
4965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lanepublic class SelectFromListWizardFragment extends Fragment {
5065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
5165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public static class ListItemComparator implements Comparator<ListItem> {
5265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        @Override
5365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        public int compare(ListItem o1, ListItem o2) {
5465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            ScanResult o1ScanResult = o1.getScanResult();
5565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            ScanResult o2ScanResult = o2.getScanResult();
5665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (o1ScanResult == null) {
5765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                if (o2ScanResult == null) {
5865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    return 0;
5965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                } else {
6065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    return 1;
6165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
6265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            } else {
6365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                if (o2ScanResult == null) {
6465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    return -1;
6565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                } else {
6665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    int levelDiff = o2ScanResult.level - o1ScanResult.level;
6765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    if (levelDiff != 0) {
6865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        return levelDiff;
6965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    }
7065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    return o1ScanResult.SSID.compareTo(o2ScanResult.SSID);
7165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
7265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
7365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
7465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
7565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
7665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public static class ListItem implements Parcelable {
7765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
7865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        private final String mName;
7965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        private final int mIconResource;
8065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        private final int mIconLevel;
8165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        private final boolean mHasIconLevel;
8265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        private final ScanResult mScanResult;
8365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
8465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        public ListItem(String name, int iconResource) {
8565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mName = name;
8665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mIconResource = iconResource;
8765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mIconLevel = 0;
8865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mHasIconLevel = false;
8965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mScanResult = null;
9065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
9165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
9265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        public ListItem(ScanResult scanResult) {
9365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mName = scanResult.SSID;
947e552863cea67e57b375f3db15e0baf70e22c39eTony Mantler            mIconResource = WifiSecurity.NONE == WifiSecurity.getSecurity(scanResult)
957e552863cea67e57b375f3db15e0baf70e22c39eTony Mantler                    ? R.drawable.setup_wifi_signal_open
9665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    : R.drawable.setup_wifi_signal_lock;
9765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mIconLevel = WifiManager.calculateSignalLevel(scanResult.level, 4);
9865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mHasIconLevel = true;
9965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            mScanResult = scanResult;
10065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
10165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
10265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        public String getName() {
10365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return mName;
10465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
10565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
10665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        int getIconResource() {
10765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return mIconResource;
10865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
10965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
11065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        int getIconLevel() {
11165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return mIconLevel;
11265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
11365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
11465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        boolean hasIconLevel() {
11565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return mHasIconLevel;
11665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
11765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
11865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        ScanResult getScanResult() {
11965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return mScanResult;
12065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
12165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
12265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        @Override
12365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        public String toString() {
12465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return mName;
12565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
12665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
12765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        public static Parcelable.Creator<ListItem> CREATOR = new Parcelable.Creator<ListItem>() {
12865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
12965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            @Override
13065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            public ListItem createFromParcel(Parcel source) {
1317e552863cea67e57b375f3db15e0baf70e22c39eTony Mantler                ScanResult scanResult = source.readParcelable(ScanResult.class.getClassLoader());
13265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                if (scanResult == null) {
13365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    return new ListItem(source.readString(), source.readInt());
13465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                } else {
13565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    return new ListItem(scanResult);
13665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
13765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
13865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
13965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            @Override
14065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            public ListItem[] newArray(int size) {
14165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                return new ListItem[size];
14265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
14365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        };
14465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
14565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        @Override
14665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        public int describeContents() {
14765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return 0;
14865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
14965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
15065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        @Override
15165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        public void writeToParcel(Parcel dest, int flags) {
15265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            dest.writeParcelable(mScanResult, flags);
15365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (mScanResult == null) {
15465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                dest.writeString(mName);
15565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                dest.writeInt(mIconResource);
15665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
15765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
15865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
15965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        @Override
16065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        public boolean equals(Object o) {
16165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (o instanceof ListItem) {
16265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                ListItem li = (ListItem) o;
16365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                if (mScanResult == null && li.mScanResult == null) {
16465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    return mName.equals(li.mName);
16565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
16665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                return (mScanResult != null && li.mScanResult != null && mName.equals(li.mName) &&
16765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        WifiSecurity.getSecurity(mScanResult)
16865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        == WifiSecurity.getSecurity(li.mScanResult));
16965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
17065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return false;
17165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
17265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
17365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
17465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public interface Listener {
17565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        void onListSelectionComplete(ListItem listItem);
17665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        void onListFocusChanged(ListItem listItem);
17765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
17865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
17965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String EXTRA_TITLE = "title";
18065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String EXTRA_DESCRIPTION = "description";
18165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String EXTRA_LIST_ELEMENTS = "list_elements";
18265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static final String EXTRA_LAST_SELECTION = "last_selection";
18365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
18465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public static SelectFromListWizardFragment newInstance(String title, String description,
18565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            ArrayList<ListItem> listElements, ListItem lastSelection) {
18665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        SelectFromListWizardFragment fragment = new SelectFromListWizardFragment();
18765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        Bundle args = new Bundle();
18865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        args.putString(EXTRA_TITLE, title);
18965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        args.putString(EXTRA_DESCRIPTION, description);
19065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        args.putParcelableArrayList(EXTRA_LIST_ELEMENTS, listElements);
19165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        args.putParcelable(EXTRA_LAST_SELECTION, lastSelection);
19265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        fragment.setArguments(args);
19365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return fragment;
19465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
19565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
19665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private Handler mHandler;
19765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private View mMainView;
19865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private ScrollArrayAdapter<ListItem> mAdapter;
19965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private ScrollAdapterView mScrollAdapterView;
20065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private ArrayList<ListItem> mListItems;
20165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private ListItem mLastSelected;
20265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
20365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public void update(ArrayList<ListItem> listElements) {
20465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        ListItem lastSelection = mLastSelected;
20565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mListItems = listElements;
20665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mAdapter.clear();
20765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mAdapter.addAll(listElements);
20865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (lastSelection != null) {
20965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            for (int i = 0, size = listElements.size(); i < size; i++) {
21065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                if (lastSelection.equals(listElements.get(i))) {
21165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    mScrollAdapterView.setSelection(i);
21265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    break;
21365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
21465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
21565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
21665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
21765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
21865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    @Override
21965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle icicle) {
22065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mHandler = new Handler();
22165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mMainView = inflater.inflate(R.layout.account_content_area, container, false);
2226316b89e65828d537da309851529d1295efd5730Tony Mantler
2236316b89e65828d537da309851529d1295efd5730Tony Mantler        final ViewGroup descriptionArea = (ViewGroup) mMainView.findViewById(R.id.description);
2246316b89e65828d537da309851529d1295efd5730Tony Mantler        final View content = inflater.inflate(R.layout.wifi_content, descriptionArea, false);
2256316b89e65828d537da309851529d1295efd5730Tony Mantler        descriptionArea.addView(content);
2266316b89e65828d537da309851529d1295efd5730Tony Mantler
2276316b89e65828d537da309851529d1295efd5730Tony Mantler        final ViewGroup actionArea = (ViewGroup) mMainView.findViewById(R.id.action);
22865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mScrollAdapterView = (ScrollAdapterView) inflater.inflate(
2296316b89e65828d537da309851529d1295efd5730Tony Mantler                R.layout.setup_scroll_adapter_view, actionArea, false);
2306316b89e65828d537da309851529d1295efd5730Tony Mantler        actionArea.addView(mScrollAdapterView);
23165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
23265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        TextView titleText = (TextView) content.findViewById(R.id.title_text);
23365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        TextView descriptionText = (TextView) content.findViewById(R.id.description_text);
23465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
23565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        Bundle args = getArguments();
23665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        String title = args.getString(EXTRA_TITLE);
23765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        String description = args.getString(EXTRA_DESCRIPTION);
23865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mListItems = args.getParcelableArrayList(EXTRA_LIST_ELEMENTS);
23965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        ListItem lastSelection = args.getParcelable(EXTRA_LAST_SELECTION);
24065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
24165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mAdapter = new ScrollArrayAdapter<ListItem>(getActivity(), R.layout.setup_list_item,
24265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                R.id.list_item_text, mListItems) {
24365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            @Override
24465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            public View getView(int position, View convertView, ViewGroup parent) {
24565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                View v = super.getView(position, convertView, parent);
24665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                ListItem item = getItem(position);
24765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                FrameLayout iconHolder = (FrameLayout) v.findViewById(R.id.list_item_icon);
24865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                iconHolder.removeAllViews();
24965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                Activity a = getActivity();
25065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                if (a != null) {
25165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    ImageView icon = new ImageView(a);
25265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    icon.setImageResource(item.getIconResource());
25365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    if (item.hasIconLevel()) {
25465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        icon.setImageLevel(item.getIconLevel());
25565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    }
25665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    iconHolder.addView(icon);
25765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
25865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                return v;
25965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
26065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        };
26165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mScrollAdapterView.setAdapter(mAdapter);
26265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
26365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        boolean forceFocusable = AccessibilityHelper.forceFocusableViews(getActivity());
26465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (title != null) {
26565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            titleText.setText(title);
26665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            titleText.setVisibility(View.VISIBLE);
26765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (forceFocusable) {
26865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                titleText.setFocusable(true);
26965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                titleText.setFocusableInTouchMode(true);
27065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
27165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } else {
27265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            titleText.setVisibility(View.GONE);
27365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
27465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
27565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (description != null) {
27665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            descriptionText.setText(description);
27765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            descriptionText.setVisibility(View.VISIBLE);
27865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (forceFocusable) {
27965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                descriptionText.setFocusable(true);
28065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                descriptionText.setFocusableInTouchMode(true);
28165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
28265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        } else {
28365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            descriptionText.setVisibility(View.GONE);
28465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
28565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
28665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (lastSelection != null) {
28765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            for (int i = 0, size = mListItems.size(); i < size; i++) {
28865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                if (lastSelection.equals(mListItems.get(i))) {
28965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    mScrollAdapterView.setSelection(i);
29065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    break;
29165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
29265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
29365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
29465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
29565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mScrollAdapterView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
29665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            @Override
29765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            public void onItemClick(AdapterView<?> pare, View view, int position, long id) {
29865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                Activity a = getActivity();
29948fce31d5176b2f4100da514dd46a45f24e41c84Tony Mantler                if (a instanceof Listener && isResumed()) {
30065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    ((Listener) a).onListSelectionComplete(mListItems.get(position));
30165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
30265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
30365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        });
30465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
30565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mScrollAdapterView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
30665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            @Override
30765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
30865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                mLastSelected = mListItems.get(position);
30965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                Activity a = getActivity();
31065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                if (a instanceof Listener) {
31165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    ((Listener) a).onListFocusChanged(mLastSelected);
31265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                }
31365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
31465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
31565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            @Override
31665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            public void onNothingSelected(AdapterView<?> parent) {
31765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
31865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
31965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        });
32065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
32165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return mMainView;
32265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
32365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
32465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    @Override
32565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public void onResume() {
32665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        super.onResume();
32765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        mHandler.post(new Runnable() {
32865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            @Override
32965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            public void run() {
33065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                InputMethodManager inputMethodManager = (InputMethodManager) getActivity()
33165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        .getSystemService(Context.INPUT_METHOD_SERVICE);
33265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                inputMethodManager.hideSoftInputFromWindow(
33365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                        mMainView.getApplicationWindowToken(), 0);
33465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
33565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        });
33665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
33765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane}
338