aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/fil/libre/repwifiapp/activities/SelectNetworkActivity.java
blob: 341fb6832f652968ce081e6fa13013bbeb563906 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//
// Copyright 2017 Filippo "Fil" Bergamo <fil.bergamo@riseup.net>
// 
// This file is part of RepWifiApp.
//
// RepWifiApp is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// RepWifiApp is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with RepWifiApp.  If not, see <http://www.gnu.org/licenses/>.
// 
// ********************************************************************

package fil.libre.repwifiapp.activities;

import fil.libre.repwifiapp.ActivityLauncher;
import fil.libre.repwifiapp.Commons;
import fil.libre.repwifiapp.R;
import fil.libre.repwifiapp.helpers.AccessPointInfo;
import fil.libre.repwifiapp.helpers.NetworkButton;
import fil.libre.repwifiapp.helpers.Utils;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class SelectNetworkActivity extends Activity implements OnClickListener {

    private AccessPointInfo[] aps;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_network);

        setTitle("Select network");

        getNetworks();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Suppress menu button
        return true;
    }

    private void writeOut(String msg) {

        TextView v = (TextView) findViewById(R.id.txt_selnets);
        v.setText(msg);

    }

    private void getNetworks() {

        Intent intent = getIntent();
        if (!intent.hasExtra(ActivityLauncher.EXTRA_APINFO_ARR)) {
            this.setResult(RESULT_CANCELED);
            finish();
            return;
        }
        AccessPointInfo[] nets = (AccessPointInfo[]) intent.getExtras().getSerializable(
                        ActivityLauncher.EXTRA_APINFO_ARR);
        if (nets == null) {
            this.setResult(RESULT_CANCELED);
            finish();
            return;
        }

        int reqCode = intent.getExtras().getInt(ActivityLauncher.EXTRA_REQCODE);

        this.aps = nets;

        if (reqCode == ActivityLauncher.RequestCode.SELECT_CONN) {
            showNetworksForConnection(nets);
        } else {
            showNetworksForManagement(nets);
        }

    }

    public void btnScanClick(View v) {
        returnResults(null, true);
    }

    @Override
    public void onClick(View v) {

        if (v instanceof NetworkButton) {
            networkNameClick((NetworkButton) v);
        }

    }

    public void networkNameClick(NetworkButton b) {

        for (AccessPointInfo i : this.aps) {

            if (i.getBssid().equals(b.getNetworkBSSID())) {

                returnResults(i, false);

            }

        }
    }

    private void returnResults(AccessPointInfo i, boolean rescan) {

        Intent intent = new Intent();
        intent.putExtra(ActivityLauncher.EXTRA_APINFO, i);
        intent.putExtra(ActivityLauncher.EXTRA_RESCAN, rescan);
        setResult(RESULT_OK, intent);
        finish();

    }

    private void showNetworksForConnection(AccessPointInfo[] info) {

        if (info == null) {
            Utils.logError("Unable to retrieve network list!");
            writeOut("Unable to retrieve network list!");
            return;
        }

        if (info.length == 0) {
            writeOut("No network found.");
            toggleBtnRescan(true);
            return;
        }

        writeOut("Select the network you want to connect to:");
        toggleBtnRescan(false);

        for (AccessPointInfo i : info) {

            addButtonForNetwork(i);

        }

    }

    private void showNetworksForManagement(AccessPointInfo[] info) {

        if (info == null || info.length == 0) {
            return;
        }

        writeOut("Select network info to manage:");
        toggleBtnRescan(false);

        for (AccessPointInfo i : info) {

            addButtonForNetwork(i);

        }

    }

    private void toggleBtnRescan(boolean enable) {

        Button b = (Button) findViewById(R.id.btn_rescan);
        if (enable) {
            b.setVisibility(View.VISIBLE);
        } else {
            b.setVisibility(View.INVISIBLE);
        }
    }

    private void addButtonForNetwork(AccessPointInfo info) {

        TableLayout s = (TableLayout) findViewById(R.id.table_networks);
        TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(
                        LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        TableRow row = new TableRow(this);
        TableRow.LayoutParams rowParams = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT,
                        LayoutParams.WRAP_CONTENT);
        rowParams.gravity = Gravity.FILL_HORIZONTAL;
        row.setPadding(10, 10, 10, 10);
        row.setLayoutParams(rowParams);
        row.setGravity(Gravity.FILL_HORIZONTAL);
        row.setLayoutParams(rowParams);

        NetworkButton button = new NetworkButton(this, info.getBssid());

        TableRow.LayoutParams params = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT,
                        LayoutParams.WRAP_CONTENT);
        button.setLayoutParams(params);
        button.setBackground(getResources().getDrawable(R.drawable.repwifi_button));
        button.setTextColor(Commons.colorThemeLight);
        button.setTextSize(20);
        button.setPadding(25, 10, 25, 10);
        button.setGravity(Gravity.CENTER_HORIZONTAL);
        button.setText(info.getSsid(20));
        button.setOnClickListener(this);

        row.addView(button, params);
        row.setGravity(Gravity.CENTER_HORIZONTAL);
        s.addView(row, tableParams);
        s.setGravity(Gravity.FILL_HORIZONTAL);

    }

}