summaryrefslogtreecommitdiff
path: root/org.fox.ttcomics/src/main/java/org/fox/ttcomics2/SyncClient.java
blob: 7d4c64a908b5d60b61044dcdebc1866faf1e7b59 (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
package org.fox.ttcomics2;

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

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

public class SyncClient {
	public interface PositionReceivedListener {
		void onPositionReceived(int position);
	}
	
	private class HttpTask extends AsyncTask<String, Integer, Boolean> {
		protected String m_response = null;
		protected int m_responseCode = -1;
		
		@Override
		protected Boolean doInBackground(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 false;
			
			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();

			    m_responseCode = conn.getResponseCode();				    

			    if (m_responseCode == HttpURLConnection.HTTP_OK) {
			    	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);

					m_response = response.toString();

					if (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();
						}
					}
				}
			};
			
			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() {
		if (m_owner != null) {
			Log.d(TAG, "Clearing sync data...");
			
			HttpTask task = new HttpTask();
			
			task.execute("clear");
		}
	}

	public void clearData(String hash) {
		if (m_owner != null) {
			Log.d(TAG, "Clearing sync data: " + hash);
			
			HttpTask task = new HttpTask();
			
			task.execute("clear", hash);
		}
	}

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