dandelion/app/src/main/java/com/github/dfa/diaspora_android/service/FetchPodsService.java

114 lines
3.8 KiB
Java
Raw Normal View History

2016-03-03 17:46:31 +01:00
/*
This file is part of the dandelion*.
2016-03-03 17:46:31 +01:00
dandelion* 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.
dandelion* 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 dandelion*.
2016-03-03 17:46:31 +01:00
If not, see <http://www.gnu.org/licenses/>.
*/
2016-10-26 16:23:14 +02:00
package com.github.dfa.diaspora_android.service;
2016-03-03 17:46:31 +01:00
import android.app.Service;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.IBinder;
2016-06-05 13:57:34 +02:00
import android.support.v4.content.LocalBroadcastManager;
2016-03-03 17:46:31 +01:00
import com.github.dfa.diaspora_android.data.DiasporaPodList;
import com.github.dfa.diaspora_android.util.AppLog;
2016-06-05 13:57:34 +02:00
import org.json.JSONException;
2016-03-03 17:46:31 +01:00
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.net.ssl.HttpsURLConnection;
import info.guardianproject.netcipher.NetCipher;
2016-10-26 16:23:14 +02:00
public class FetchPodsService extends Service {
2016-06-05 13:57:34 +02:00
public static final String MESSAGE_PODS_RECEIVED = "com.github.dfa.diaspora.podsreceived";
2017-05-26 12:13:28 +02:00
public static final String EXTRA_PODLIST = "pods";
2017-06-10 13:01:57 +02:00
2016-10-26 16:23:14 +02:00
public FetchPodsService() {
}
2016-03-03 17:46:31 +01:00
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
2017-05-26 12:13:28 +02:00
new GetPodsTask(this).execute();
2016-03-03 17:46:31 +01:00
return super.onStartCommand(intent, flags, startId);
}
2017-05-26 12:13:28 +02:00
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
}
class GetPodsTask extends AsyncTask<Void, Void, DiasporaPodList> {
2018-05-13 12:08:27 +02:00
private static final String PODDY_PODLIST_URL = "https://raw.githubusercontent.com/gsantner/dandelion/master/app/src/main/res/raw/podlist.json";
2017-05-26 12:13:28 +02:00
private final Service service;
GetPodsTask(Service service) {
this.service = service;
}
@Override
protected DiasporaPodList doInBackground(Void... params) {
StringBuilder sb = new StringBuilder();
BufferedReader br = null;
try {
HttpsURLConnection con = NetCipher.getHttpsURLConnection(PODDY_PODLIST_URL);
if (con.getResponseCode() == HttpsURLConnection.HTTP_OK) {
br = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
2016-03-03 17:46:31 +01:00
}
2017-05-26 12:13:28 +02:00
// Parse JSON & return pod list
JSONObject json = new JSONObject(sb.toString());
return new DiasporaPodList().fromJson(json);
} else {
AppLog.e(this, "Failed to download list of pods");
2016-03-03 17:46:31 +01:00
}
2017-05-26 12:13:28 +02:00
} catch (IOException | JSONException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException ignored) {
}
2016-03-03 17:46:31 +01:00
}
2017-05-26 12:13:28 +02:00
}
// Could not fetch list of pods :(
return new DiasporaPodList();
2016-03-03 17:46:31 +01:00
}
@Override
2017-05-26 12:13:28 +02:00
protected void onPostExecute(DiasporaPodList pods) {
if (pods == null) {
pods = new DiasporaPodList();
}
Intent broadcastIntent = new Intent(FetchPodsService.MESSAGE_PODS_RECEIVED);
broadcastIntent.putExtra(FetchPodsService.EXTRA_PODLIST, pods);
LocalBroadcastManager.getInstance(service.getApplicationContext()).sendBroadcast(broadcastIntent);
service.stopSelf();
2016-03-03 17:46:31 +01:00
}
2017-05-26 12:13:28 +02:00
}