summaryrefslogtreecommitdiff
path: root/org.fox.ttcomics/src/main/java/org/fox/ttcomics2/sync/SyncClient.java
blob: 0d27817a52a26fb6780ed31245bd1c73553ce7eb (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
194
195
196
197
198
199
200
201
202
203
204
package org.fox.ttcomics2.sync;

import android.os.AsyncTask;
import android.util.Log;

import org.fox.ttcomics2.CommonActivity;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class SyncClient {
	public interface PositionReceivedListener {
		void onPositionReceived(int position);
	}

	public interface DataClearedListener {
		void onDataCleared(boolean result);
	}

	private class HttpTask extends AsyncTask<String, Integer, Boolean> {
		protected String m_response = null;
		protected int m_responseCode = -1;
		
		@Override
		protected Boolean doInBackground(String... params) {

			try {
				HttpURLConnection conn = doSyncHttpRequest(params);

				if (conn != null) {
					m_responseCode = conn.getResponseCode();

					if (m_responseCode == HttpURLConnection.HTTP_OK) {
						m_response = readHttpResponse(conn);

						//Log.d(TAG, "<<< " + m_response);

						if (m_response != null && m_response.indexOf("ERROR") == -1) {
							return true;
						} else {
							return false;
						}
					} else {
						Log.d(TAG, "HTTP error, code: " + m_responseCode);
					}

					conn.disconnect();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}

			return false;
		}
		
	}
	
	private final String TAG = this.getClass().getSimpleName();
	private static final String SYNC_ENDPOINT = "http://tt-rss.org/tcrsync/";
	private String m_owner = null;
	
	public void setOwner(String owner) {
		m_owner = CommonActivity.sha1(owner);
	}
	
	public int getPosition(String hash, final PositionReceivedListener listener) {
		if (m_owner != null) {
			Log.d(TAG, "Requesting sync data...");

			HttpTask task = new HttpTask() {
				@Override
				protected void onPostExecute(Boolean result) {
					if (result) {
						try {
							listener.onPositionReceived(Integer.valueOf(m_response));
						} catch (NumberFormatException e) {
							e.printStackTrace();
						}
					} else {
						listener.onPositionReceived(-1);
					}
				}
			};
			
			task.execute("get", hash);

		}
		return -1;
	}
	
	public void setPosition(String hash, int position) {
		if (m_owner != null) {
			Log.d(TAG, "Uploading sync data...");
			
			HttpTask task = new HttpTask();
			
			task.execute("set", hash, String.valueOf(position));
		}
	}
	
	public void clearData(final DataClearedListener listener) {
		if (m_owner != null) {
			Log.d(TAG, "Clearing sync data...");
			
			HttpTask task = new HttpTask() {
				@Override
				protected void onPostExecute(Boolean result) {
					if (listener != null) listener.onDataCleared(result);
				}
			};
			
			task.execute("clear");
		}
	}

	public void clearData(String hash, final DataClearedListener listener) {
		if (m_owner != null) {
			Log.d(TAG, "Clearing sync data: " + hash);

			HttpTask task = new HttpTask() {
				@Override
				protected void onPostExecute(Boolean result) {
					if (listener != null) listener.onDataCleared(result);
				}
			};


			task.execute("clear", hash);
		}
	}

	public HttpURLConnection doSyncHttpRequest(String ... params) {
		String requestStr = null;
		String op = params[0];

		if (op.equals("set")) {
			requestStr = String.format("op=set&owner=%1$s&hash=%2$s&position=%3$s", m_owner, params[1], params[2]);
		} else if (op.equals("get")) {
			requestStr = String.format("op=get&owner=%1$s&hash=%2$s", m_owner, params[1]);
		} else if (op.equals("clear")) {
			if (params.length > 1) {
				requestStr = String.format("op=clear&owner=%1$s&hash=%2$s", m_owner, params[1]);
			} else {
				requestStr = String.format("op=clear&owner=%1$s", m_owner);
			}
		}

		requestStr += "&version=2";

		Log.d(TAG, requestStr);

		if (requestStr == null) return null;

		try {
			byte[] postData = requestStr.getBytes("UTF-8");

			URL url = new URL(SYNC_ENDPOINT);

			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setDoInput(true);
			conn.setDoOutput(true);
			conn.setUseCaches(false);
			conn.setRequestMethod("POST");

			OutputStream out = conn.getOutputStream();
			out.write(postData);
			out.close();

			return conn;

		} catch (Exception e) {
		}

		return null;
	}

	public String readHttpResponse(HttpURLConnection conn) {
		try {
			StringBuffer response = new StringBuffer();
			InputStreamReader in = new InputStreamReader(conn.getInputStream(), "UTF-8");

			char[] buf = new char[1024];
			int read = 0;

			while ((read = in.read(buf)) >= 0) {
				response.append(buf, 0, read);
			}

			//Log.d(TAG, "<<< " + response);

			return response.toString();
		} catch (IOException e) {
			return null;
		}
	}

	public boolean hasOwner() {
		return m_owner != null;
	}
	
}