1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings.wifi;
18
19import android.content.Intent;
20import android.content.res.TypedArray;
21import android.database.DataSetObserver;
22import android.net.wifi.WifiConfiguration;
23import android.os.Bundle;
24import android.view.Gravity;
25import android.view.LayoutInflater;
26import android.view.Menu;
27import android.view.MenuItem;
28import android.view.View;
29import android.view.View.OnClickListener;
30import android.view.ViewGroup;
31import android.widget.AbsListView.LayoutParams;
32import android.widget.ListAdapter;
33import android.widget.ListView;
34import android.widget.TextView;
35
36import com.android.settings.R;
37
38/**
39 * This customized version of WifiSettings is shown to the user only during Setup Wizard. Menu
40 * selections are limited, clicking on an access point will auto-advance to the next screen (once
41 * connected), and, if the user opts to skip ahead without a wifi connection, a warning message
42 * alerts of possible carrier data charges or missing software updates.
43 */
44public class WifiSettingsForSetupWizard extends WifiSettings {
45
46    private static final String TAG = "WifiSettingsForSetupWizard";
47
48    // show a text regarding data charges when wifi connection is required during setup wizard
49    protected static final String EXTRA_SHOW_WIFI_REQUIRED_INFO = "wifi_show_wifi_required_info";
50
51    private View mAddOtherNetworkItem;
52    private ListAdapter mAdapter;
53    private TextView mEmptyFooter;
54    private boolean mListLastEmpty = false;
55
56    @Override
57    public View onCreateView(final LayoutInflater inflater, ViewGroup container,
58            Bundle savedInstanceState) {
59
60        final View view = inflater.inflate(R.layout.setup_preference, container, false);
61
62        final ListView list = (ListView) view.findViewById(android.R.id.list);
63        final View title = view.findViewById(R.id.title);
64        if (title == null) {
65            final View header = inflater.inflate(R.layout.setup_wizard_header, list, false);
66            list.addHeaderView(header, null, false);
67        }
68
69        mAddOtherNetworkItem = inflater.inflate(R.layout.setup_wifi_add_network, list, false);
70        list.addFooterView(mAddOtherNetworkItem, null, true);
71        mAddOtherNetworkItem.setOnClickListener(new OnClickListener() {
72            @Override
73            public void onClick(View v) {
74                if (mWifiManager.isWifiEnabled()) {
75                    onAddNetworkPressed();
76                }
77            }
78        });
79
80        final Intent intent = getActivity().getIntent();
81        if (intent.getBooleanExtra(EXTRA_SHOW_WIFI_REQUIRED_INFO, false)) {
82            view.findViewById(R.id.wifi_required_info).setVisibility(View.VISIBLE);
83        }
84
85        return view;
86    }
87
88    @Override
89    public void onActivityCreated(Bundle savedInstanceState) {
90        super.onActivityCreated(savedInstanceState);
91
92        getView().setSystemUiVisibility(
93                View.STATUS_BAR_DISABLE_HOME |
94                View.STATUS_BAR_DISABLE_RECENT |
95                View.STATUS_BAR_DISABLE_NOTIFICATION_ALERTS |
96                View.STATUS_BAR_DISABLE_CLOCK);
97
98        if (hasNextButton()) {
99            getNextButton().setVisibility(View.GONE);
100        }
101
102        mAdapter = getPreferenceScreen().getRootAdapter();
103        mAdapter.registerDataSetObserver(new DataSetObserver() {
104            @Override
105            public void onChanged() {
106                super.onChanged();
107                updateFooter();
108            }
109        });
110    }
111
112    @Override
113    public void registerForContextMenu(View view) {
114        // Suppressed during setup wizard
115    }
116
117    @Override
118    /* package */ WifiEnabler createWifiEnabler() {
119        // Not shown during setup wizard
120        return null;
121    }
122
123    @Override
124    /* package */ void addOptionsMenuItems(Menu menu) {
125        final boolean wifiIsEnabled = mWifiManager.isWifiEnabled();
126        final TypedArray ta = getActivity().getTheme()
127                .obtainStyledAttributes(new int[] {R.attr.ic_wps});
128        menu.add(Menu.NONE, MENU_ID_WPS_PBC, 0, R.string.wifi_menu_wps_pbc)
129                .setIcon(ta.getDrawable(0))
130                .setEnabled(wifiIsEnabled)
131                .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
132        menu.add(Menu.NONE, MENU_ID_ADD_NETWORK, 0, R.string.wifi_add_network)
133                .setEnabled(wifiIsEnabled)
134                .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
135        ta.recycle();
136    }
137
138    @Override
139    protected void connect(final WifiConfiguration config) {
140        WifiSetupActivity activity = (WifiSetupActivity) getActivity();
141        activity.networkSelected();
142        super.connect(config);
143    }
144
145    @Override
146    protected void connect(final int networkId) {
147        WifiSetupActivity activity = (WifiSetupActivity) getActivity();
148        activity.networkSelected();
149        super.connect(networkId);
150    }
151
152    @Override
153    protected TextView initEmptyView() {
154        mEmptyFooter = new TextView(getActivity());
155        mEmptyFooter.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
156                LayoutParams.MATCH_PARENT));
157        mEmptyFooter.setGravity(Gravity.CENTER);
158        mEmptyFooter.setCompoundDrawablesWithIntrinsicBounds(0,
159                R.drawable.ic_wifi_emptystate, 0,0);
160        return mEmptyFooter;
161    }
162
163    protected void updateFooter() {
164        final boolean isEmpty = mAdapter.isEmpty();
165        if (isEmpty != mListLastEmpty) {
166            final ListView list = getListView();
167            if (isEmpty) {
168                list.removeFooterView(mAddOtherNetworkItem);
169                list.addFooterView(mEmptyFooter, null, false);
170            } else {
171                list.removeFooterView(mEmptyFooter);
172                list.addFooterView(mAddOtherNetworkItem, null, true);
173            }
174            mListLastEmpty = isEmpty;
175        }
176    }
177}
178