-
Notifications
You must be signed in to change notification settings - Fork 5
/
QuotaFragment.java
199 lines (133 loc) · 6.01 KB
/
QuotaFragment.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
package com.yeokm1.nussocprintandroid.fragments;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.yeokm1.nussocprintandroid.R;
import com.yeokm1.nussocprintandroid.core.Storage;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
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 java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class QuotaFragment extends Fragment {
private TextView outputTextView;
private Button refreshButton;
private final String TAG = "Quota";
private final String QUOTA_REGEX = "<tr><td bgcolor=.*?>(.*?)</td><td bgcolor=.*?>(.*?)</td></tr>";
private Pattern patternQuotaPage;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_quota, container, false);
outputTextView = (TextView) view.findViewById(R.id.fragment_quota_output);
outputTextView.setMovementMethod(new ScrollingMovementMethod());
refreshButton = (Button) view.findViewById(R.id.fragment_quota_refresh_button);
patternQuotaPage = Pattern.compile(QUOTA_REGEX);
refreshButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
refreshPrintQuota();
}
});
refreshPrintQuota();
return view;
}
public void refreshPrintQuota(){
Storage storage = Storage.getInstance();
String username = storage.getUsername();
String password = storage.getPassword();
if(username.length() == 0 || password.length() == 0){
Toast.makeText(getActivity(), R.string.misc_missing_credential, Toast.LENGTH_SHORT).show();
return;
}
String loginURL = getString(R.string.quota_login_url);
String quotaPageRelURL = getString(R.string.quota_page_relative_url);
outputTextView.setText(R.string.quota_refresh_text);
new AsyncTask<String, String, String>() {
@Override
protected String doInBackground(String... params) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(params[0]);
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("destination", params[1]));
nameValuePairs.add(new BasicNameValuePair("credential_0", params[2]));
nameValuePairs.add(new BasicNameValuePair("credential_1", params[3]));
nameValuePairs.add(new BasicNameValuePair("AuthType", "AuthDBICookieHandler"));
nameValuePairs.add(new BasicNameValuePair("AuthName", "mysoc"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
String responseOutput = inputStreamToString(response.getEntity().getContent());
return responseOutput;
} catch (Exception e) {
return "";
}
}
@Override
public void onPostExecute(String output){
if(isAdded() && outputTextView != null){
if(output.length() == 0){
outputTextView.setText(R.string.misc_no_network);
} else {
String processed = processHTMLOutputToQuota(output);
outputTextView.setText(processed);
}
}
}
}.execute(new String[]{loginURL, quotaPageRelURL, username, password});
}
private String processHTMLOutputToQuota(String input){
Matcher match = patternQuotaPage.matcher(input);
StringBuffer finalString = new StringBuffer();
//Find quota strings
while (match.find()) {
String matched = match.group();
String processed = android.text.Html.fromHtml(matched).toString();
int indexToInsertTo = processed.length();
while(Character.isDigit(processed.charAt(indexToInsertTo - 1))){
indexToInsertTo--;
}
StringBuffer temp = new StringBuffer(processed);
temp.insert(indexToInsertTo, ": ");
String quotaString = temp.toString();
finalString.append(quotaString + System.getProperty("line.separator") + System.getProperty("line.separator"));
}
String finalText = finalString.toString();
if(finalText.length() == 0){
finalText = getString(R.string.misc_wrong_credentials);
}
Log.i(TAG, finalText);
return finalText;
}
private String inputStreamToString(InputStream is) throws IOException {
String line = "";
StringBuilder total = new StringBuilder();
// Wrap a BufferedReader around the InputStream
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
// Read response until the end
while ((line = rd.readLine()) != null) {
total.append(line);
}
String result = total.toString();
// Return full string
return result;
}
}