summaryrefslogtreecommitdiff
path: root/Defogger/src/no/mork/android/defogger/ConfigureNetworkActivity.java
blob: 14d710d88f7c94f3b28416f6605eb64d878de7aa (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
/*
 *  SPDX-License-Identifier: GPL-3.0-only
 *  Copyright (c) 2019  Bjørn Mork <bjorn@mork.no>
 */

package no.mork.android.defogger;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.ParcelUuid;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ConfigureNetworkActivity extends Activity {
    private static String msg = "Defogger Network Config: ";
    private ArrayAdapter<String> networklist;
    private String selected = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	Log.d(msg, "onCreate()");
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_configurenetwork);

	// Get the Intent that started this activity and extract parameters
	Intent intent = getIntent();
	String[] networks = intent.getStringArrayExtra("networks");
	if (networks == null)
	    networks = new String[0];

	networklist = new ArrayAdapter<String> (this, R.layout.item_net, networks) {
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
		    // Check if an existing view is being reused, otherwise inflate the view
		    if (convertView == null) {
			convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_net, parent, false);
		    }
	    
		    //L=I=aaaa7,M=0,C=4,S=4,E=2,P=100
		    // Get the data item for this position
		    String txt = getItem(position);
		    TextView ssid = (TextView) convertView.findViewById(R.id.ssid);
		    ssid.setText(txt.substring(4).split(",", 1)[0]);
		    
		    // react when selecting item
		    convertView.setOnClickListener(new OnClickListener() {
			    private String ret = getItem(position).substring(2);

			    @Override
			    public void onClick(View v) {
				Log.d(msg, "onClick() will return " + ret);
				selected = ret;
			    }
			});

		    return convertView;
		}
	    };
 
	ListView listView = (ListView) findViewById(R.id.networks);
	listView.setAdapter(networklist);
	setResult(RESULT_CANCELED); // default
    }

    @Override
    protected void onResume() {
	Log.d(msg, "onResume()");
        super.onResume();
    }

    @Override
    protected void onPause() {
	Log.d(msg, "onPause()");
        super.onPause();
    }

    @Override
    protected void onStop() {
	Log.d(msg, "onStop()");
        super.onStop();
    }

    @Override
    protected void onDestroy() {
	Log.d(msg, "onDestroy()");
        super.onDestroy();
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
	Log.d(msg, "onSaveInstanceState()");

	// Save the user's current game state

	// Always call the superclass so it can save the view hierarchy state
	super.onSaveInstanceState(savedInstanceState);
    }

    
    public void onRestoreInstanceState(Bundle savedInstanceState) {
	Log.d(msg, "onRestoreInstanceState()");
	// Always call the superclass so it can restore the view hierarchy
	super.onRestoreInstanceState(savedInstanceState);

	// Restore state members from saved instance
    }

    public void onBackPressed() {
    	Log.d(msg, "onBackPressed()");
	//	returnConfigResult(null);
	//	setResult(RESULT_CANCELED); // default
        super.onBackPressed();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	Log.d(msg, "onOptionsItemSelected()");
	switch (item.getItemId()){
	case android.R.id.home:
	    onBackPressed();
	    return true;
	}
	return super.onOptionsItemSelected(item);
    }
    
    public void returnConfigResult(String config) {
	Log.d(msg, "returnConfigResult(): " + config);
 	Intent intent = new Intent();
	if (config != null) {
	    intent.putExtra("netconf", config);
	    setResult(RESULT_OK, intent);
	}
	finish();
    }

    public void doCancel(View v) {
	returnConfigResult(null);
    }

    public void doFinish(View v) {
	//                cfg = "M=" + net["M"] + ";I=" + essid + ";S=" + net["S"] + ";E=" + net["E"] + ";K=" + passwd
	// L=I=Telenor_Guest,M=0,C=11,S=0,E=0,P=74
	// L=I=Telenor_employee,M=0,C=11,S=4,E=2,P=74
  	if (selected == null)
	    returnConfigResult(null);

	Log.d(msg, "selected is " + selected);

	Map<String,String> kv = Util.splitKV(selected, ",");
    	EditText edit = (EditText) findViewById(R.id.password);
	String password = edit.getText().toString();

    	edit = (EditText) findViewById(R.id.ssid);
	String ssid = kv.containsKey("I") ? kv.get("I") : edit.getText().toString();

	Log.d(msg, "kv is " + kv);
	
	/* returning empty ssid is not allowed */
	if (ssid == null || ssid.length() == 0)
	    returnConfigResult(null);

	if (password == null)
	    password = "";

	/* set defaults, assuming open network if password is empty and ssid was not found */
	int S = kv.containsKey("S") ? Integer.parseInt(kv.get("S")) : password.length() > 0 ? 4 : 0;
	int E = kv.containsKey("E") ? Integer.parseInt(kv.get("E")) : password.length() > 0 ? 2 : 0;

	/* password is required unless open network */
	if (password.length() == 0 && (S != 0 || E !=0))
	    returnConfigResult(null);
	else
	    returnConfigResult("M=0;I=" + ssid + ";S=" + S + ";E=" + E + ";K=" + password);
    }
    
}