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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
//
// 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 java.io.IOException;
import fil.libre.repwifiapp.Commons;
import fil.libre.repwifiapp.R;
import fil.libre.repwifiapp.Commons.RequestCode;
import fil.libre.repwifiapp.helpers.AccessPointInfo;
import fil.libre.repwifiapp.helpers.ConnectionStatus;
import fil.libre.repwifiapp.helpers.NetworkManager;
import fil.libre.repwifiapp.helpers.RootCommand;
import fil.libre.repwifiapp.helpers.Utils;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setImage();
setupSharedResources();
RootCommand su = new RootCommand(null);
try {
su.execute();
} catch (Exception e) {
Utils.logError("Error while trying to get first Super User access. Aborting.",e);
finish();
}
try {
Commons.initObjects();
} catch (Exception e) {
Utils.logError("Error on creating engine. Aborting.",e);
finish();
}
checkConnectionStatus();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menu_credits:
launchCreditsActivity();
return true;
default:
return true;
}
}
@Override
public void onRestart(){
super.onRestart();
ConnectionStatus status = Commons.connectionEngine.getConnectionStatus();
if (status != null && status.isConnected()){
launchStatusActivity(status);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent){
Utils.logDebug("Main onActivityResult(): ",1);
if (intent == null){
return;
}
if (resultCode != RESULT_OK){
return;
}
AccessPointInfo i = null;
if (intent.hasExtra(Commons.EXTRA_APINFO)){
i = (AccessPointInfo)intent.getExtras().getSerializable(Commons.EXTRA_APINFO);
}
switch (requestCode) {
case RequestCode.PASS_INPUT:
handleResultSetPass(i);
break;
case RequestCode.SELECT_CONN:
boolean rescan = (boolean)intent.getExtras().getBoolean(Commons.EXTRA_RESCAN);
handleResultSelect(i, rescan);
break;
case RequestCode.CONNECT:
boolean conres = intent.getExtras().getBoolean(Commons.EXTRA_BOOLEAN);
handleFinishedConnecting(conres, i);
break;
case RequestCode.STATUS_GET:
ConnectionStatus status = (ConnectionStatus)intent.getExtras().getSerializable(Commons.EXTRA_CONSTATUS);
handleResultGetStatus(status);
break;
case RequestCode.NETWORKS_GET:
AccessPointInfo[] nets = (AccessPointInfo[])intent.getExtras().getSerializable(Commons.EXTRA_APINFO_ARR);
launchSelectActivity(nets, true);
case RequestCode.STATUS_SHOW:
//do nothing
break;
case RequestCode.SELECT_DETAILS:
launchDetailsActivity(i);
break;
case RequestCode.DETAILS_SHOW:
boolean del = intent.getExtras().getBoolean(Commons.EXTRA_DELETE);
if (del){ deleteNetwork(i); }
break;
default:
break;
}
}
private void setImage(){
ImageView img = (ImageView)findViewById(R.id.img_logo);
try {
Drawable d = Drawable.createFromStream(getAssets().open("repwifi-logo-0.png"),null);
img.setImageDrawable(d);
} catch (IOException e) {
Utils.logError("Error while loading logo image",e);
}
}
private void setupSharedResources(){
Commons.colorThemeDark = getResources().getColor(R.color.ThemeDark);
Commons.colorThemeLight = getResources().getColor(R.color.ThemeLight);
Commons.colorBlack = getResources().getColor(R.color.black);
Commons.setAppDataFolder(getExternalFilesDir(null).getAbsolutePath());
}
private void handleResultSelect(AccessPointInfo i, boolean rescan){
if (rescan){
doScan();
}else if (i != null){
if (i.needsPassword()){
//try to fetch network's password from storage
AccessPointInfo fromStorage = Commons.storage.getSavedNetwork(i);
if (fromStorage == null){
launchPasswordActivity(i);
return;
}else{
//use fetched network
i = fromStorage;
}
}
connectToNetwork(i);
}
}
private void handleResultSetPass(AccessPointInfo i){
connectToNetwork(i);
}
private void handleResultGetStatus(ConnectionStatus status){
if (status != null && status.isConnected()){
launchStatusActivity(status);
}
}
private void handleFinishedConnecting(boolean connectionResult, AccessPointInfo info){
if(connectionResult && info.needsPassword()){
//Save network
if (Commons.storage.save(info)){
Toast toast2 = Toast.makeText(getApplicationContext(), "Network Saved!",Toast.LENGTH_LONG);
toast2.show();
}else {
Toast toast2 = Toast.makeText(getApplicationContext(), "FAILED to save network!",Toast.LENGTH_LONG);
toast2.show();
}
checkConnectionStatus();
}else{
//alert that connection failed
Toast toast = Toast.makeText(getApplicationContext(), "FAILED to connect!", Toast.LENGTH_LONG);
toast.show();
}
}
private void launchPasswordActivity(AccessPointInfo info){
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.setClass(getApplicationContext(), InputPasswordActivity.class);
intent.putExtra(Commons.EXTRA_APINFO, info);
startActivityForResult(intent, RequestCode.PASS_INPUT);
}
private void launchStatusActivity(ConnectionStatus status){
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.putExtra(Commons.EXTRA_CONSTATUS, status);
intent.setClass(getApplicationContext(), ShowStatusActivity.class);
startActivityForResult(intent, RequestCode.STATUS_SHOW);
}
private void launchSelectActivity(AccessPointInfo[] nets,boolean forConnection){
Intent intent = new Intent(this, SelectNetworkActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.putExtra(Commons.EXTRA_APINFO_ARR, nets);
if (forConnection){
intent.putExtra(Commons.EXTRA_REQCODE, RequestCode.SELECT_CONN);
startActivityForResult(intent, RequestCode.SELECT_CONN);
}
else{
intent.putExtra(Commons.EXTRA_REQCODE, RequestCode.SELECT_DETAILS);
startActivityForResult(intent, RequestCode.SELECT_DETAILS);
}
}
private void launchDetailsActivity(AccessPointInfo info){
Intent intent = new Intent(this, NetworkDetailsActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.putExtra(Commons.EXTRA_APINFO, info);
startActivityForResult(intent, RequestCode.DETAILS_SHOW);
}
private void launchCreditsActivity(){
Intent intent = new Intent(this, CreditsActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivityForResult(intent, RequestCode.NONE);
}
private void deleteNetwork(AccessPointInfo info){
NetworkManager manager = new NetworkManager(Commons.getNetworkStorageFile());
String msg = "";
if (manager.remove(info)){
msg = "Network info deleted!";
}else{
msg = "FAILED to delete network info!";
}
Toast toast = Toast.makeText(this, msg, Toast.LENGTH_LONG);
toast.show();
}
private void connectToNetwork(AccessPointInfo info){
Intent intent = new Intent(this, LongTaskActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.putExtra(Commons.EXTRA_REQCODE, RequestCode.CONNECT);
intent.putExtra(Commons.EXTRA_APINFO, info);
startActivityForResult(intent, RequestCode.CONNECT);
}
private void checkConnectionStatus(){
Intent intent = new Intent(this, LongTaskActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.putExtra(Commons.EXTRA_REQCODE, RequestCode.STATUS_GET);
startActivityForResult(intent, RequestCode.STATUS_GET);
}
private void doScan(){
Intent intent = new Intent(this, LongTaskActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.putExtra(Commons.EXTRA_REQCODE, RequestCode.NETWORKS_GET);
startActivityForResult(intent, RequestCode.NETWORKS_GET);
}
public void btnScanClick(View v){
doScan();
}
public void btnManageClick(View v){
NetworkManager manager = new NetworkManager(Commons.getNetworkStorageFile());
AccessPointInfo[] infos = manager.getKnownNetworks();
if (infos == null || infos.length == 0){
Toast toast = Toast.makeText(this, "No saved network", Toast.LENGTH_LONG);
toast.show();
}
else{
launchSelectActivity(infos, false);
}
}
}
|