dandelion/app/src/main/java/com/github/dfa/diaspora_android/task/GetPodsService.java

139 lines
5.2 KiB
Java
Raw Normal View History

2016-03-03 17:46:31 +01:00
/*
This file is part of the Diaspora for Android.
2016-03-03 17:46:31 +01:00
Diaspora for Android is free software: you can redistribute it and/or modify
2016-03-03 17:46:31 +01:00
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Diaspora for Android is distributed in the hope that it will be useful,
2016-03-03 17:46:31 +01:00
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with the Diaspora for Android.
2016-03-03 17:46:31 +01:00
If not, see <http://www.gnu.org/licenses/>.
*/
2016-03-29 19:38:50 +02:00
package com.github.dfa.diaspora_android.task;
2016-03-03 17:46:31 +01:00
import android.app.Service;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.IBinder;
import android.util.Log;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
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 java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class GetPodsService extends Service {
2016-03-29 19:38:50 +02:00
public static final String MESSAGE = "com.github.dfa.diaspora.podsreceived";
2016-03-03 17:46:31 +01:00
private static final String TAG = "Diaspora Pod Service";
public GetPodsService() {
}
2016-03-03 17:46:31 +01:00
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
getPods();
return super.onStartCommand(intent, flags, startId);
}
private void getPods() {
/*
* Most of the code in this AsyncTask is from the file getPodlistTask.java
* from the app "Diaspora Webclient".
* A few modifications and adaptations were made by me.
* Source:
* https://github.com/voidcode/Diaspora-Webclient/blob/master/src/com/voidcode/diasporawebclient/getPodlistTask.java
* Thanks to Terkel Sørensen
*/
AsyncTask<Void, Void, String[]> getPodsAsync = new AsyncTask<Void, Void, String[]>() {
@Override
protected String[] doInBackground(Void... params) {
// TODO: Update deprecated code
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(TAG, "Failed to download list of pods");
}
} catch (IOException e) {
//TODO handle json buggy feed
e.printStackTrace();
}
//Parse the JSON Data
try {
JSONObject jsonObjectAll = new JSONObject(builder.toString());
JSONArray jsonArrayAll = jsonObjectAll.getJSONArray("pods");
Log.d(TAG, "Number of entries " + jsonArrayAll.length());
list = new ArrayList<>();
for (int i = 0; i < jsonArrayAll.length(); i++) {
JSONObject jo = jsonArrayAll.getJSONObject(i);
if (jo.getString("secure").equals("true"))
2016-03-03 17:46:31 +01:00
list.add(jo.getString("domain"));
}
} catch (Exception e) {
//TODO Handle Parsing errors here
e.printStackTrace();
}
if (list != null)
return list.toArray(new String[list.size()]);
else
return null;
}
@Override
protected void onPostExecute(String[] strings) {
Intent broadcastIntent = new Intent(MESSAGE);
if (strings != null)
broadcastIntent.putExtra("pods", strings);
sendBroadcast(broadcastIntent);
stopSelf();
}
};
getPodsAsync.execute();
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
}