aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/fil/libre/repwifiapp/activities/LongTaskActivity.java
blob: f9e7685cbd89834511fb681559789235a620db2b (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
package fil.libre.repwifiapp.activities;


import fil.libre.repwifiapp.Commons;
import fil.libre.repwifiapp.R;
import fil.libre.repwifiapp.helpers.AccessPointInfo;
import fil.libre.repwifiapp.helpers.ConnectionStatus;
import fil.libre.repwifiapp.helpers.Utils;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
import android.app.Activity;
import android.content.Intent;

public class LongTaskActivity extends Activity {


	private class Task extends AsyncTask<Object, Object, Object>{

		private int REQ_CODE;

		public Task(int reqCode, Object input){
			this.REQ_CODE = reqCode;
		}

		@Override
		protected Object doInBackground(Object... params) {

			Object ret = null;

			switch (this.REQ_CODE){

			case Commons.RequestCode.CONNECT:

				ret = Commons.connectionEngine.connect((AccessPointInfo)params[0]);
				break;

			case Commons.RequestCode.NETWORKS_GET:

				ret = Commons.connectionEngine.getAvailableNetworks();
				break;

			case Commons.RequestCode.STATUS_GET:

				ret = Commons.connectionEngine.getConnectionStatus();
				break;

			default:

				break;

			}

			return ret;

		}

		@Override
		protected void onPostExecute(Object result) {
			taskCompleted(result, this.REQ_CODE);
		}

	}

	private AccessPointInfo currentNetwork = null;
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_long_task);
			
		startTask();

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

	private void startTask() {

		//retrieve the request code:
		Intent intent = getIntent();
		if (! intent.hasExtra(Commons.EXTRA_REQCODE)){
			this.setResult(RESULT_CANCELED);
			finish();
		}

		Object input = null;
		int reqCode = intent.getExtras().getInt(Commons.EXTRA_REQCODE);
			
		switch (reqCode) {
		
		case Commons.RequestCode.CONNECT:
			setTitle("Connecting...");
			setMessage("Connecting...");			
			//Extract AccessPointInfo
			input = intent.getExtras().getSerializable(Commons.EXTRA_APINFO);
			currentNetwork = (AccessPointInfo)input;			
			break;
			
		case Commons.RequestCode.NETWORKS_GET:
			setTitle("Scanning...");
			setMessage("Scanning for Networks...");
			
		case Commons.RequestCode.STATUS_GET:
			setTitle("Checking status...");
			setMessage("Checking status...");

		default:
			setTitle("Please wait...");
			setMessage("Please wait...");
			break;
		}
		
		Task task = new Task(reqCode, input);
		task.execute(input);

	}

	private void taskCompleted(Object result, int reqCode){

		Utils.logDebug("Finished long task reqCode: "+ reqCode,1);
		
		//Return to caller:
		Intent intent = this.getIntent();


		switch (reqCode){

		case Commons.RequestCode.CONNECT:

			intent.putExtra(Commons.EXTRA_BOOLEAN, (Boolean)result);
			intent.putExtra(Commons.EXTRA_APINFO, this.currentNetwork);
			break;

		case Commons.RequestCode.NETWORKS_GET:

			intent.putExtra(Commons.EXTRA_APINFO_ARR, (AccessPointInfo[])result);
			break;

		case Commons.RequestCode.STATUS_GET:

			intent.putExtra(Commons.EXTRA_CONSTATUS, (ConnectionStatus)result);
			break;

		default:

			Utils.logDebug("Task terminating in null: ",1);
			break;

		}

		this.setResult(RESULT_OK, intent);
		finish();

	}

	
	private void setMessage(String msg) {
		TextView txt = (TextView)findViewById(R.id.txt_msg);
		txt.setText(msg);
	}

	@Override
	public void onBackPressed() {
		//suppress
	}



}