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
|
package org.fox.ttrss;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;
import org.json.JSONTokener;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class LoginActivity extends Activity {
private final String TAG = this.getClass().getSimpleName();
private SharedPreferences m_prefs;
private String m_themeName = "";
private String m_sessionId = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
m_prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
if (m_prefs.getString("theme", "THEME_DARK").equals("THEME_DARK")) {
setTheme(R.style.DarkTheme);
} else {
setTheme(R.style.LightTheme);
}
m_themeName = m_prefs.getString("theme", "THEME_DARK");
setContentView(R.layout.login);
}
protected void updateLoginStatus(int id) {
TextView tv = (TextView) findViewById(R.id.login_status_text);
if (tv != null) {
tv.setText(id);
}
}
protected void showLoginProgress(boolean show) {
View v = findViewById(R.id.login_progress);
v.setVisibility((show) ? View.VISIBLE : View.GONE);
}
@Override
public void onResume() {
super.onResume();
if (!m_prefs.getString("theme", "THEME_DARK").equals(m_themeName)) {
Intent refresh = new Intent(this, LoginActivity.class);
startActivity(refresh);
finish();
}
showLoginProgress(false);
if (isConfigured()) {
updateLoginStatus(R.string.login_ready);
} else {
updateLoginStatus(R.string.login_need_configure);
}
}
public boolean isConfigured() {
String login = m_prefs.getString("login", "");
String password = m_prefs.getString("password", "");
String ttrssUrl = m_prefs.getString("ttrss_url", "");
return !(login.equals("") || password.equals("") || ttrssUrl.equals(""));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.login_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.preferences:
Intent intent = new Intent(this, PreferencesActivity.class);
startActivityForResult(intent, 0);
return true;
case R.id.login:
performLogin();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@SuppressWarnings({ "serial", "unchecked" })
private void performLogin() {
ApiRequest task = new ApiRequest(null, m_prefs.getString("ttrss_url", null)) {
@Override
protected void onPostExecute(JsonElement result) {
if (result != null) {
try {
JsonObject rv = result.getAsJsonObject();
int status = rv.get("status").getAsInt();
if (status == 0) {
JsonObject content = rv.get("content").getAsJsonObject();
if (content != null) {
m_sessionId = content.get("session_id").getAsString();
showLoginProgress(false);
updateLoginStatus(R.string.login_success);
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("sessionId", m_sessionId);
startActivityForResult(intent, 0);
finish();
return;
}
} else {
JsonObject content = rv.get("content").getAsJsonObject();
if (content != null) {
String error = content.get("error").getAsString();
if (error.equals("LOGIN_ERROR")) {
updateLoginStatus(R.string.login_wrong_password);
} else if (error.equals("API_DISABLED")) {
updateLoginStatus(R.string.login_api_disabled);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
showLoginProgress(false);
updateLoginStatus(R.string.login_failed);
}
};
updateLoginStatus(R.string.login_in_progress);
showLoginProgress(true);
task.execute(new HashMap<String,String>() {
{
put("op", "login");
put("user", m_prefs.getString("login", null));
put("password", m_prefs.getString("password", null));
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
|