/
SettingsActivity.java
207 lines (200 loc) · 7.08 KB
/
SettingsActivity.java
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
package com.voidcode.diasporawebclient;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;
public class SettingsActivity extends Activity {
public static final String SETTINGS_FILENAME="settings";
public ListView lvPods;
public String lvPods_arr[] = getPods();
private EditText editTextCurrentpod;
JSONArray jsonArray;
@Override
public void onCreate(Bundle savedInstanceState) {
if(isNetworkAvailable())
{
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
editTextCurrentpod = (EditText) findViewById(R.id.editText_currentpod);
lvPods = (ListView) findViewById(R.id.listView_poduptime);
//show the currentpod to user
SharedPreferences preferences = getSharedPreferences(SETTINGS_FILENAME, MODE_PRIVATE);
editTextCurrentpod.setText(preferences.getString("currentpod", "You need to choose a pod"));
editTextCurrentpod.selectAll();
//fill listview with pods form http://podupti.me
fillListview(this.lvPods_arr);
//podsearch, A fast find search on editTextCurrentpod, So user don´t have to scholl the podlist to finde a pod
editTextCurrentpod.addTextChangedListener(new TextWatcher()
{
List<String> filter_podurl_list = null;
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
public void onTextChanged(CharSequence s, int start, int before, int count)
{
filter_podurl_list=new ArrayList<String>();
for(String podurl:lvPods_arr)
{
if(podurl.startsWith(s.toString()))
{
filter_podurl_list.add(podurl);
}
}
}
public void afterTextChanged(Editable s)
{
///add reslut to listview
if(!filter_podurl_list.equals(null))
fillListview(filter_podurl_list.toArray(new String[filter_podurl_list.size()]));
}
});
}
else
{
this.finish();
startActivity(new Intent(this, SetupInternetActivity.class));
}
}
public void fillListview(String _lvPods_arr[])
{
lvPods.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, _lvPods_arr));
lvPods.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
//onclick put select pod to editTextCurrentpod
editTextCurrentpod.setText(lvPods.getItemAtPosition(position).toString());
}
});
}
//Screen orientation crashes app fix
//http://jamesgiang.wordpress.com/2010/06/05/screen-orientation-crashes-my-app/
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
}
// Handle the Back button
@Override
public boolean onKeyDown(int keyCode, KeyEvent msg){
if((keyCode == KeyEvent.KEYCODE_BACK))
{
this.finish();
startActivity(new Intent(this, MainActivity.class));
return false;
}
else
return true;
}
public void Onclick_SaveSettings(View v) throws IOException
{
//get userinput
String new_currentpod = editTextCurrentpod.getText().toString();
//if user has added a pod
if(!new_currentpod.equals(""))
{
// Save the new currentpod
SharedPreferences preferences = getSharedPreferences(SETTINGS_FILENAME, MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("currentpod", new_currentpod);
editor.commit();
//goto MainActivity
this.finish();
startActivity(new Intent(this, MainActivity.class));
}
else
Toast.makeText(getApplicationContext(), "You need to choose a pod", Toast.LENGTH_SHORT).show();
}
//simple json parsing, to retrieve list of pods from podupti.me
//https://github.com/voidcode/Diaspora-Webclient/pull/4
//by: https://github.com/vipulnsward
public String [] getPods()
{
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
List<String> list = null;
try {
HttpGet httpGet = new HttpGet("http://podupti.me/api.php?key=4r45tg&format=json");
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} else {
//TODO Notify User about failure
//Log.e("Diaspora-WebClient", "Failed to download file");
}
} catch (ClientProtocolException e) {
//TODO handle network unreachable exception here
e.printStackTrace();
} catch (IOException e) {
//TODO handle json buggy feed
e.printStackTrace();
}
//Parse the JSON Data
try {
JSONObject j=new JSONObject(builder.toString());
JSONArray jr=j.getJSONArray("pods");
//Log.i("Diaspora-WebClient","Number of entries " + jr.length());
list=new ArrayList<String>();
for (int i = 0; i < jr.length(); i++) {
JSONObject jo = jr.getJSONObject(i);
//Log.i("Diaspora-WebClient", jo.getString("domain"));
String secure=jo.getString("secure");
if(secure.equals("true"))
list.add(jo.getString("domain"));
}
}catch (Exception e) {
//TODO Handle Parsing errors here
e.printStackTrace();
}
return list.toArray(new String[list.size()]);
}
private boolean isNetworkAvailable()
{
ConnectivityManager connec = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mobileInfo = connec.getNetworkInfo(0);
NetworkInfo wifiInfo = connec.getNetworkInfo(1);
NetworkInfo wimaxInfo = connec.getNetworkInfo(6);
if (wimaxInfo!=null) {
return mobileInfo.isConnected() || wifiInfo.isConnected()|| wimaxInfo.isConnected();
}
else {
return mobileInfo.isConnected() || wifiInfo.isConnected();
}
}
}