Improve detection of the network

This commit is contained in:
tom79 2019-09-24 19:13:30 +02:00
parent 72538519cd
commit 0a6999d28b
3 changed files with 121 additions and 8 deletions

View File

@ -61,6 +61,7 @@ import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.SwitchCompat;
import androidx.appcompat.widget.Toolbar;
import android.util.Log;
import android.util.Patterns;
import android.view.LayoutInflater;
import android.view.Menu;
@ -216,6 +217,7 @@ public abstract class BaseMainActivity extends BaseActivity
return;
}
//Update the static variable which manages account type
if (account.getSocial() == null || account.getSocial().equals("MASTODON"))
social = UpdateAccountInfoAsyncTask.SOCIAL.MASTODON;

View File

@ -27,6 +27,7 @@ import java.net.URLDecoder;
import app.fedilab.android.client.API;
import app.fedilab.android.client.Entities.Account;
import app.fedilab.android.client.Entities.InstanceNodeInfo;
import app.fedilab.android.client.GNUAPI;
import app.fedilab.android.client.PeertubeAPI;
import app.fedilab.android.helper.Helper;
@ -70,8 +71,9 @@ public class UpdateAccountInfoAsyncTask extends AsyncTask<Void, Void, Void> {
Account account;
if (social == SOCIAL.MASTODON || social == SOCIAL.PIXELFED) {
account = new API(this.contextReference.get(), instance, null).verifyCredentials();
if (account != null)
account.setSocial(account.getSocial());
InstanceNodeInfo info = new API(this.contextReference.get(), instance, null).getRealNodeInfo(this.instance);
if (info != null)
account.setSocial(info.getName().toUpperCase());
} else if (social == SOCIAL.PEERTUBE) {
account = new PeertubeAPI(this.contextReference.get(), instance, null).verifyCredentials();
if (account != null)

View File

@ -19,6 +19,7 @@ import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
@ -190,6 +191,9 @@ public class API {
}
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
tootPerPage = sharedpreferences.getInt(Helper.SET_TOOT_PER_PAGE, Helper.TOOTS_PER_PAGE);
if( MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.PIXELFED && tootPerPage > 30 ){
tootPerPage = 30;
}
accountPerPage = Helper.ACCOUNTS_PER_PAGE;
notificationPerPage = Helper.NOTIFICATIONS_PER_PAGE;
this.prefKeyOauthTokenT = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null);
@ -574,6 +578,113 @@ public class API {
return apiResponse;
}
public InstanceNodeInfo getRealNodeInfo(String domain) {
//Try to guess URL scheme for the onion instance
String scheme = "https";
if (domain.endsWith(".onion")) {
try {
new HttpsConnection(context, domain).get("http://" + domain, 30, null, null);
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.SET_ONION_SCHEME + domain, "http");
scheme = "http";
editor.apply();
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (HttpsConnection.HttpsConnectionException e) {
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.SET_ONION_SCHEME + domain, "https");
editor.apply();
}
}
String response;
InstanceNodeInfo instanceNodeInfo = new InstanceNodeInfo();
try {
response = new HttpsConnection(context, domain).get(scheme + "://" + domain + "/.well-known/nodeinfo", 30, null, null);
JSONArray jsonArray = new JSONObject(response).getJSONArray("links");
ArrayList<NodeInfo> nodeInfos = new ArrayList<>();
try {
int i = 0;
while (i < jsonArray.length()) {
JSONObject resobj = jsonArray.getJSONObject(i);
NodeInfo nodeInfo = new NodeInfo();
nodeInfo.setHref(resobj.getString("href"));
nodeInfo.setRel(resobj.getString("rel"));
i++;
nodeInfos.add(nodeInfo);
}
if (nodeInfos.size() > 0) {
NodeInfo nodeInfo = nodeInfos.get(nodeInfos.size() - 1);
response = new HttpsConnection(context, this.instance).get(nodeInfo.getHref(), 30, null, null);
JSONObject resobj = new JSONObject(response);
JSONObject jsonObject = resobj.getJSONObject("software");
String name = null;
if (resobj.has("metadata") && resobj.getJSONObject("metadata").has("features")) {
JSONArray features = resobj.getJSONObject("metadata").getJSONArray("features");
if (features.length() > 0) {
for (int counter = 0; counter < features.length(); counter++) {
if (features.getString(counter).toUpperCase().equals("MASTODON_API")) {
name = "MASTODON";
break;
}
}
}
}
if (name == null) {
name = jsonObject.getString("name").toUpperCase();
}
instanceNodeInfo.setName(name);
instanceNodeInfo.setVersion(jsonObject.getString("version"));
instanceNodeInfo.setOpenRegistrations(resobj.getBoolean("openRegistrations"));
}
} catch (JSONException e) {
setDefaultError(e);
}
} catch (IOException e) {
instanceNodeInfo.setConnectionError(true);
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (HttpsConnection.HttpsConnectionException e) {
try {
response = new HttpsConnection(context, this.instance).get(scheme + "://" + domain + "/api/v1/instance", 30, null, null);
JSONObject jsonObject = new JSONObject(response);
instanceNodeInfo.setName("MASTODON");
instanceNodeInfo.setVersion(jsonObject.getString("version"));
instanceNodeInfo.setOpenRegistrations(true);
} catch (IOException e1) {
instanceNodeInfo.setConnectionError(true);
e1.printStackTrace();
} catch (NoSuchAlgorithmException e1) {
e1.printStackTrace();
} catch (KeyManagementException e1) {
e1.printStackTrace();
} catch (HttpsConnection.HttpsConnectionException e1) {
instanceNodeInfo.setName("GNU");
instanceNodeInfo.setVersion("MASTODON");
instanceNodeInfo.setOpenRegistrations(true);
e1.printStackTrace();
} catch (JSONException e1) {
e1.printStackTrace();
}
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return instanceNodeInfo;
}
public InstanceNodeInfo getNodeInfo(String domain) {
//Try to guess URL scheme for the onion instance
@ -770,6 +881,9 @@ public class API {
}
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
tootPerPage = sharedpreferences.getInt(Helper.SET_TOOT_PER_PAGE, Helper.TOOTS_PER_PAGE);
if( MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.PIXELFED && tootPerPage > 30 ){
tootPerPage = 30;
}
accountPerPage = Helper.ACCOUNTS_PER_PAGE;
notificationPerPage = Helper.NOTIFICATIONS_PER_PAGE;
if (instance != null)
@ -896,6 +1010,7 @@ public class API {
}
String response = new HttpsConnection(context, this.instance).get(getAbsoluteUrl("/accounts/verify_credentials"), 10, null, prefKeyOauthTokenT);
account = parseAccountResponse(context, new JSONObject(response));
account.setSocial(getNodeInfo(instance).getName().toUpperCase());
if (account != null && account.getSocial() != null && account.getSocial().equals("PLEROMA")) {
isPleromaAdmin(account.getAcct());
}
@ -5778,12 +5893,6 @@ public class API {
account.setAvatar_static(resobj.get("avatar_static").toString());
account.setHeader(resobj.get("header").toString());
account.setHeader_static(resobj.get("header_static").toString());
try {
account.setSocial(resobj.get("software").toString().toUpperCase());
} catch (Exception ignored) {
account.setSocial("MASTODON");
}
try {
if (resobj.has("pleroma")) {
account.setSocial("PLEROMA");