diff --git a/app/src/main/java/app/fedilab/android/activities/BaseMainActivity.java b/app/src/main/java/app/fedilab/android/activities/BaseMainActivity.java index f3b131421..5906a4214 100644 --- a/app/src/main/java/app/fedilab/android/activities/BaseMainActivity.java +++ b/app/src/main/java/app/fedilab/android/activities/BaseMainActivity.java @@ -260,18 +260,7 @@ public abstract class BaseMainActivity extends BaseActivity //Update the static variable which manages account type - if (account.getSocial() == null || account.getSocial().equals("MASTODON")) - social = UpdateAccountInfoAsyncTask.SOCIAL.MASTODON; - else if (account.getSocial().equals("PEERTUBE")) - social = UpdateAccountInfoAsyncTask.SOCIAL.PEERTUBE; - else if (account.getSocial().equals("PIXELFED")) - social = UpdateAccountInfoAsyncTask.SOCIAL.PIXELFED; - else if (account.getSocial().equals("PLEROMA")) - social = UpdateAccountInfoAsyncTask.SOCIAL.PLEROMA; - else if (account.getSocial().equals("GNU")) - social = UpdateAccountInfoAsyncTask.SOCIAL.GNU; - else if (account.getSocial().equals("FRIENDICA")) - social = UpdateAccountInfoAsyncTask.SOCIAL.FRIENDICA; + Helper.setSoftware(account.getSocial(), true); countNewStatus = 0; countNewNotifications = 0; diff --git a/app/src/main/java/app/fedilab/android/activities/LoginActivity.java b/app/src/main/java/app/fedilab/android/activities/LoginActivity.java index 82f2797d3..e8d552f3a 100644 --- a/app/src/main/java/app/fedilab/android/activities/LoginActivity.java +++ b/app/src/main/java/app/fedilab/android/activities/LoginActivity.java @@ -128,19 +128,7 @@ public class LoginActivity extends BaseActivity { if (b != null) { autofilledInstance = b.getString("instance", null); social = b.getString("social", null); - if (social != null) { - switch (social) { - case "MASTODON": - socialNetwork = UpdateAccountInfoAsyncTask.SOCIAL.MASTODON; - break; - case "PEERTUBE": - socialNetwork = UpdateAccountInfoAsyncTask.SOCIAL.PEERTUBE; - break; - case "GNU": - socialNetwork = UpdateAccountInfoAsyncTask.SOCIAL.GNU; - break; - } - } + socialNetwork = Helper.setSoftware(instanceNodeInfo.getName(), false); admin = b.getBoolean("admin", false); } @@ -281,27 +269,15 @@ public class LoginActivity extends BaseActivity { new Thread(new Runnable() { @Override public void run() { - instanceNodeInfo = new API(LoginActivity.this).getNodeInfo(instance); + instanceNodeInfo = new API(LoginActivity.this).displayNodeInfo(instance); runOnUiThread(new Runnable() { public void run() { connect_button.setEnabled(true); if (instanceNodeInfo != null && instanceNodeInfo.getName() != null) { - switch (instanceNodeInfo.getName()) { - case "MASTODON": - socialNetwork = UpdateAccountInfoAsyncTask.SOCIAL.MASTODON; - break; - case "PIXELFED": - socialNetwork = UpdateAccountInfoAsyncTask.SOCIAL.PIXELFED; - break; - case "PEERTUBE": - socialNetwork = UpdateAccountInfoAsyncTask.SOCIAL.PEERTUBE; - break; - case "GNU": - socialNetwork = UpdateAccountInfoAsyncTask.SOCIAL.GNU; - break; - } - if (instanceNodeInfo.getName().equals("MASTODON") || instanceNodeInfo.getName().equals("PIXELFED")) { + socialNetwork = Helper.setSoftware(instanceNodeInfo.getName(), false); + + if (instanceNodeInfo.getName().equals("PLEROMA") ||instanceNodeInfo.getName().equals("MASTODON") || instanceNodeInfo.getName().equals("PIXELFED")) { client_id_for_webview = true; retrievesClientId(); } else { @@ -310,7 +286,7 @@ public class LoginActivity extends BaseActivity { step_instance.setVisibility(View.GONE); instance_chosen.setText(instance); retrievesClientId(); - } else if (instanceNodeInfo.getName().equals("GNU")) { + } else if (instanceNodeInfo.getName().equals("GNU") || instanceNodeInfo.getName().equals("FRIENDICA")) { step_login_credential.setVisibility(View.VISIBLE); step_instance.setVisibility(View.GONE); instance_chosen.setText(instance); @@ -397,8 +373,6 @@ public class LoginActivity extends BaseActivity { } }); - } catch (HttpsConnection.HttpsConnectionException e) { - isLoadingInstance = false; } catch (Exception e) { isLoadingInstance = false; } @@ -788,14 +762,12 @@ public class LoginActivity extends BaseActivity { editor.apply(); return false; } else if (id == R.id.action_import_data) { - if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { - if (ContextCompat.checkSelfPermission(LoginActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != - PackageManager.PERMISSION_GRANTED) { - ActivityCompat.requestPermissions(LoginActivity.this, - new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, - TootActivity.MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE); - return true; - } + if (ContextCompat.checkSelfPermission(LoginActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != + PackageManager.PERMISSION_GRANTED) { + ActivityCompat.requestPermissions(LoginActivity.this, + new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, + TootActivity.MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE); + return true; } Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_OPENABLE); diff --git a/app/src/main/java/app/fedilab/android/activities/TootActivity.java b/app/src/main/java/app/fedilab/android/activities/TootActivity.java index 3df8dd2b5..776af1253 100644 --- a/app/src/main/java/app/fedilab/android/activities/TootActivity.java +++ b/app/src/main/java/app/fedilab/android/activities/TootActivity.java @@ -620,18 +620,7 @@ public class TootActivity extends BaseActivity implements UploadStatusDelegate, if (social == null || accountReply != null) { //Update the static variable which manages account type - if (account.getSocial() == null || account.getSocial().equals("MASTODON")) - social = UpdateAccountInfoAsyncTask.SOCIAL.MASTODON; - else if (account.getSocial().equals("PEERTUBE")) - social = UpdateAccountInfoAsyncTask.SOCIAL.PEERTUBE; - else if (account.getSocial().equals("PIXELFED")) - social = UpdateAccountInfoAsyncTask.SOCIAL.PIXELFED; - else if (account.getSocial().equals("PLEROMA")) - social = UpdateAccountInfoAsyncTask.SOCIAL.PLEROMA; - else if (account.getSocial().equals("GNU")) - social = UpdateAccountInfoAsyncTask.SOCIAL.GNU; - else if (account.getSocial().equals("FRIENDICA")) - social = UpdateAccountInfoAsyncTask.SOCIAL.FRIENDICA; + social = Helper.setSoftware(account.getSocial(), false); } switch (social) { case GNU: diff --git a/app/src/main/java/app/fedilab/android/asynctasks/RetrieveSearchAsyncTask.java b/app/src/main/java/app/fedilab/android/asynctasks/RetrieveSearchAsyncTask.java index 424d9adc9..fc83f9bd7 100644 --- a/app/src/main/java/app/fedilab/android/asynctasks/RetrieveSearchAsyncTask.java +++ b/app/src/main/java/app/fedilab/android/asynctasks/RetrieveSearchAsyncTask.java @@ -88,11 +88,12 @@ public class RetrieveSearchAsyncTask extends AsyncTask { domain = split[2]; } if (domain != null && username != null) { - InstanceNodeInfo node = api.getNodeInfo(domain); + InstanceNodeInfo node = api.displayNodeInfo(domain); String url = null; if (node != null && node.getName() != null) { switch (node.getName().trim()) { case "MASTODON": + case "PLEROMA": url = "https://" + domain + "/@" + username; break; case "PEERTUBE": @@ -102,6 +103,7 @@ public class RetrieveSearchAsyncTask extends AsyncTask { url = "https://" + domain + "/" + username; break; case "GNU": + case "FRIENDICA": url = "https://" + domain + "/profile/" + username; break; } diff --git a/app/src/main/java/app/fedilab/android/client/API.java b/app/src/main/java/app/fedilab/android/client/API.java index fbd45382a..51c809703 100644 --- a/app/src/main/java/app/fedilab/android/client/API.java +++ b/app/src/main/java/app/fedilab/android/client/API.java @@ -578,121 +578,6 @@ public class API { } - public InstanceNodeInfo getNodeInfo(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 | NoSuchAlgorithmException | 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 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 (!resobj.getJSONObject("metadata").isNull("features") && 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) { - if ( !jsonObject.isNull("name") ) { - name = jsonObject.getString("name").toUpperCase(); - switch (name) { - case "PLEROMA": - name = "MASTODON"; - break; - case "GNUSOCIAL": - case "HUBZILLA": - case "REDMATRIX": - case "FRIENDICA": - name = "GNU"; - break; - } - } - } - instanceNodeInfo.setName(name); - instanceNodeInfo.setVersion(jsonObject.getString("version")); - if( resobj.has("openRegistrations")) { - instanceNodeInfo.setOpenRegistrations(resobj.getBoolean("openRegistrations")); - } - } - } catch (JSONException e) { - setDefaultError(e); - } - } catch (IOException e) { - instanceNodeInfo.setConnectionError(true); - e.printStackTrace(); - } catch (NoSuchAlgorithmException | KeyManagementException | JSONException 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 | KeyManagementException | JSONException e1) { - e1.printStackTrace(); - } catch (HttpsConnection.HttpsConnectionException e1) { - if( e1.getStatusCode() == 404) { - instanceNodeInfo.setName("GNU"); - instanceNodeInfo.setVersion("unknown"); - instanceNodeInfo.setOpenRegistrations(true); - e1.printStackTrace(); - }else{ - instanceNodeInfo.setName("MASTODON"); - instanceNodeInfo.setVersion("3.0"); - instanceNodeInfo.setOpenRegistrations(false); - } - } - e.printStackTrace(); - } - return instanceNodeInfo; - } - - public InstanceNodeInfo displayNodeInfo(String domain) { String response; @@ -888,6 +773,11 @@ public class API { */ public Account verifyCredentials() { account = new Account(); + InstanceNodeInfo nodeinfo = displayNodeInfo(instance); + String social = null; + if( nodeinfo != null){ + social = nodeinfo.getName(); + } try { if (context == null) { setError(500, new Throwable("An error occured!")); @@ -895,9 +785,8 @@ public class API { } String response = new HttpsConnection(context, this.instance).get(getAbsoluteUrl("/accounts/verify_credentials"), 10, null, prefKeyOauthTokenT); account = parseAccountResponse(context, new JSONObject(response)); - InstanceNodeInfo nodeinfo = displayNodeInfo(instance); - if( nodeinfo != null && nodeinfo.getName() != null) { - account.setSocial(nodeinfo.getName().toUpperCase()); + if( social != null ) { + account.setSocial(social.toUpperCase()); } if (account != null && account.getSocial() != null && account.getSocial().equals("PLEROMA")) { isPleromaAdmin(account.getAcct()); @@ -928,6 +817,9 @@ public class API { try { response = new HttpsConnection(context, this.instance).get(getAbsoluteUrl("/accounts/verify_credentials"), 10, null, targetedAccount.getToken()); account = parseAccountResponse(context, new JSONObject(response)); + if( social != null ) { + account.setSocial(social.toUpperCase()); + } if (account.getSocial().equals("PLEROMA")) { isPleromaAdmin(account.getAcct()); } diff --git a/app/src/main/java/app/fedilab/android/client/GNUAPI.java b/app/src/main/java/app/fedilab/android/client/GNUAPI.java index f062a77df..01caea693 100644 --- a/app/src/main/java/app/fedilab/android/client/GNUAPI.java +++ b/app/src/main/java/app/fedilab/android/client/GNUAPI.java @@ -45,6 +45,7 @@ import app.fedilab.android.client.Entities.Application; import app.fedilab.android.client.Entities.Attachment; import app.fedilab.android.client.Entities.Conversation; import app.fedilab.android.client.Entities.Error; +import app.fedilab.android.client.Entities.InstanceNodeInfo; import app.fedilab.android.client.Entities.Mention; import app.fedilab.android.client.Entities.Notification; import app.fedilab.android.client.Entities.Relationship; @@ -181,19 +182,21 @@ public class GNUAPI { */ public Account verifyCredentials() { account = new Account(); + InstanceNodeInfo nodeinfo = new API(context).displayNodeInfo(instance); + String social = null; + if( nodeinfo != null){ + social = nodeinfo.getName(); + } try { String response = new HttpsConnection(context, this.instance).get(getAbsoluteUrl("/account/verify_credentials.json"), 60, null, prefKeyOauthTokenT); account = parseAccountResponse(context, new JSONObject(response)); + if( social != null ) { + account.setSocial(social.toUpperCase()); + } } catch (HttpsConnection.HttpsConnectionException e) { setError(e.getStatusCode(), e); e.printStackTrace(); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } catch (KeyManagementException e) { - e.printStackTrace(); - } catch (JSONException e) { + } catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) { e.printStackTrace(); } return account; diff --git a/app/src/main/java/app/fedilab/android/client/PeertubeAPI.java b/app/src/main/java/app/fedilab/android/client/PeertubeAPI.java index 93a35545c..9b1195f69 100644 --- a/app/src/main/java/app/fedilab/android/client/PeertubeAPI.java +++ b/app/src/main/java/app/fedilab/android/client/PeertubeAPI.java @@ -46,6 +46,7 @@ import app.fedilab.android.client.Entities.Error; import app.fedilab.android.client.Entities.Filters; import app.fedilab.android.client.Entities.HowToVideo; import app.fedilab.android.client.Entities.Instance; +import app.fedilab.android.client.Entities.InstanceNodeInfo; import app.fedilab.android.client.Entities.Peertube; import app.fedilab.android.client.Entities.PeertubeAccountNotification; import app.fedilab.android.client.Entities.PeertubeActorFollow; @@ -326,17 +327,19 @@ public class PeertubeAPI { */ public Account verifyCredentials() { account = new Account(); + InstanceNodeInfo nodeinfo = new API(context).displayNodeInfo(instance); + String social = null; + if( nodeinfo != null){ + social = nodeinfo.getName(); + } try { String response = new HttpsConnection(context, this.instance).get(getAbsoluteUrl("/users/me"), 60, null, prefKeyOauthTokenT); JSONObject accountObject = new JSONObject(response).getJSONObject("account"); account = parseAccountResponsePeertube(context, accountObject); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } catch (KeyManagementException e) { - e.printStackTrace(); - } catch (JSONException e) { + if( social != null ) { + account.setSocial(social.toUpperCase()); + } + } catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) { e.printStackTrace(); } catch (HttpsConnection.HttpsConnectionException e) { if (e.getStatusCode() == 401 || e.getStatusCode() == 403) { @@ -363,13 +366,10 @@ public class PeertubeAPI { response = new HttpsConnection(context, this.instance).get(getAbsoluteUrl("/users/me"), 60, null, targetedAccount.getToken()); JSONObject accountObject = new JSONObject(response).getJSONObject("account"); account = parseAccountResponsePeertube(context, accountObject); - } catch (IOException e1) { - e1.printStackTrace(); - } catch (NoSuchAlgorithmException e1) { - e1.printStackTrace(); - } catch (KeyManagementException e1) { - e1.printStackTrace(); - } catch (JSONException e1) { + if( social != null ) { + account.setSocial(social.toUpperCase()); + } + } catch (IOException | NoSuchAlgorithmException | KeyManagementException | JSONException e1) { e1.printStackTrace(); } catch (HttpsConnection.HttpsConnectionException e1) { e1.printStackTrace(); diff --git a/app/src/main/java/app/fedilab/android/helper/Helper.java b/app/src/main/java/app/fedilab/android/helper/Helper.java index a65077a5d..2de7d2a88 100644 --- a/app/src/main/java/app/fedilab/android/helper/Helper.java +++ b/app/src/main/java/app/fedilab/android/helper/Helper.java @@ -5000,4 +5000,36 @@ public class Helper { return (lastRefresh == null || lastRefresh.before(dateAllowed)); } + public static UpdateAccountInfoAsyncTask.SOCIAL setSoftware(String social, boolean updateSocial){ + //Update the static variable which manages account type + if( updateSocial) { + if (social == null || social.equals("MASTODON")) + MainActivity.social = UpdateAccountInfoAsyncTask.SOCIAL.MASTODON; + else if (social.equals("PEERTUBE")) + MainActivity.social = UpdateAccountInfoAsyncTask.SOCIAL.PEERTUBE; + else if (social.equals("PIXELFED")) + MainActivity.social = UpdateAccountInfoAsyncTask.SOCIAL.PIXELFED; + else if (social.equals("PLEROMA")) + MainActivity.social = UpdateAccountInfoAsyncTask.SOCIAL.PLEROMA; + else if (social.equals("GNU")) + MainActivity.social = UpdateAccountInfoAsyncTask.SOCIAL.GNU; + else if (social.equals("FRIENDICA")) + MainActivity.social = UpdateAccountInfoAsyncTask.SOCIAL.FRIENDICA; + } + UpdateAccountInfoAsyncTask.SOCIAL socialNetwork = null; + if (social == null || social.equals("MASTODON")) + socialNetwork = UpdateAccountInfoAsyncTask.SOCIAL.MASTODON; + else if (social.equals("PEERTUBE")) + socialNetwork = UpdateAccountInfoAsyncTask.SOCIAL.PEERTUBE; + else if (social.equals("PIXELFED")) + socialNetwork = UpdateAccountInfoAsyncTask.SOCIAL.PIXELFED; + else if (social.equals("PLEROMA")) + socialNetwork = UpdateAccountInfoAsyncTask.SOCIAL.PLEROMA; + else if (social.equals("GNU")) + socialNetwork = UpdateAccountInfoAsyncTask.SOCIAL.GNU; + else if (social.equals("FRIENDICA")) + socialNetwork = UpdateAccountInfoAsyncTask.SOCIAL.FRIENDICA; + return socialNetwork; + } + }