1
0
mirror of https://framagit.org/tom79/fedilab-tube synced 2025-06-05 21:09:11 +02:00

apply flavors

This commit is contained in:
Thomas
2020-09-13 19:20:04 +02:00
parent 7827c97bf9
commit f0eaceb8a4
41 changed files with 631 additions and 744 deletions

View File

@ -22,6 +22,7 @@ import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.UnderlineSpan;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
@ -31,6 +32,7 @@ import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;
import org.json.JSONException;
@ -66,7 +68,7 @@ public class LoginActivity extends AppCompatActivity {
private EditText login_passwd;
private Button connectionButton;
private String actionToken;
private TextInputEditText login_instance;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -93,6 +95,12 @@ public class LoginActivity extends AppCompatActivity {
});
TextInputLayout login_instance_container = findViewById(R.id.login_instance_container);
login_instance = findViewById(R.id.login_instance);
if (BuildConfig.full_instances) {
login_instance_container.setVisibility(View.VISIBLE);
}
login_uid = findViewById(R.id.login_uid);
login_passwd = findViewById(R.id.login_passwd);
@ -129,29 +137,33 @@ public class LoginActivity extends AppCompatActivity {
Toasty.error(LoginActivity.this, getString(R.string.email_error)).show();
return;
}
String[] emailArray = login_uid.getText().toString().split("@");
if (emailArray.length > 1 && !Arrays.asList(Helper.valideEmails).contains(emailArray[1])) {
Toasty.error(LoginActivity.this, getString(R.string.email_error_domain, emailArray[1])).show();
return;
}
String host = emailArray[1];
String instance = Helper.getPeertubeUrl(host);
final HashMap<String, String> parameters = new HashMap<>();
connectionButton.setEnabled(false);
try {
instance = URLEncoder.encode(instance, "utf-8");
} catch (UnsupportedEncodingException e) {
Toasty.error(LoginActivity.this, getString(R.string.client_error), Toast.LENGTH_LONG).show();
final HashMap<String, String> parameters = new HashMap<>();
String instance, host;
if (!BuildConfig.full_instances) {
String[] emailArray = login_uid.getText().toString().split("@");
if (emailArray.length > 1 && !Arrays.asList(Helper.valideEmails).contains(emailArray[1])) {
Toasty.error(LoginActivity.this, getString(R.string.email_error_domain, emailArray[1])).show();
return;
}
host = emailArray[1];
instance = Helper.getPeertubeUrl(host);
} else {
if (login_instance == null || login_instance.getText() == null || login_instance.getText().toString().trim().length() == 0) {
Toasty.error(LoginActivity.this, getString(R.string.instance_fails)).show();
return;
}
instance = host = login_instance.getText().toString();
}
if (Arrays.asList(Helper.openid).contains(host)) {
String finalInstance = instance;
if (Arrays.asList(Helper.openid).contains(host) || !BuildConfig.full_instances) {
new Thread(() -> {
try {
actionToken = "/api/v1/oauth-clients/local";
String response = new HttpsConnection(LoginActivity.this).get("https://" + finalInstance + actionToken, 30, null, null);
String response = new HttpsConnection(LoginActivity.this).get("https://" + instance + actionToken, 30, null, null);
if (response == null) {
runOnUiThread(() -> {
connectionButton.setEnabled(true);
@ -192,11 +204,10 @@ public class LoginActivity extends AppCompatActivity {
parameters.put(Helper.REDIRECT_URIS, Helper.REDIRECT_CONTENT);
parameters.put(Helper.SCOPES, Helper.OAUTH_SCOPES_PEERTUBE);
parameters.put(Helper.WEBSITE, Helper.WEBSITE_VALUE);
String finalInstance = instance;
new Thread(() -> {
try {
actionToken = "/api/v1/oauth-clients/local";
String response = new HttpsConnection(LoginActivity.this).get("https://" + finalInstance + actionToken, 30, parameters, null);
String response = new HttpsConnection(LoginActivity.this).get("https://" + instance + actionToken, 30, parameters, null);
if (response == null) {
runOnUiThread(() -> {
connectionButton.setEnabled(true);
@ -231,7 +242,7 @@ public class LoginActivity extends AppCompatActivity {
parameters.put("scope", "user");
String oauthUrl = "/api/v1/users/token";
try {
String responseLogin = new HttpsConnection(LoginActivity.this).post("https://" + finalInstance + oauthUrl, 30, parameters, null);
String responseLogin = new HttpsConnection(LoginActivity.this).post("https://" + instance + oauthUrl, 30, parameters, null);
proceedLogin(responseLogin, host);
} catch (final Exception e) {
parameters.clear();
@ -250,7 +261,7 @@ public class LoginActivity extends AppCompatActivity {
}
parameters.put("scope", "user");
try {
String responseLogin = new HttpsConnection(LoginActivity.this).post("https://" + finalInstance + oauthUrl, 30, parameters, null);
String responseLogin = new HttpsConnection(LoginActivity.this).post("https://" + instance + oauthUrl, 30, parameters, null);
proceedLogin(responseLogin, host);
} catch (final Exception e2) {
e2.printStackTrace();

View File

@ -22,6 +22,9 @@ import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
@ -43,11 +46,13 @@ import java.util.LinkedHashMap;
import app.fedilab.fedilabtube.client.HttpsConnection;
import app.fedilab.fedilabtube.client.PeertubeAPI;
import app.fedilab.fedilabtube.client.entities.Account;
import app.fedilab.fedilabtube.client.entities.InstanceNodeInfo;
import app.fedilab.fedilabtube.client.entities.PeertubeInformation;
import app.fedilab.fedilabtube.helper.Helper;
import app.fedilab.fedilabtube.sqlite.AccountDAO;
import app.fedilab.fedilabtube.sqlite.Sqlite;
import app.fedilab.fedilabtube.viewmodel.FeedsVM;
import es.dmoral.toasty.Toasty;
import static app.fedilab.fedilabtube.helper.Helper.academies;
@ -173,7 +178,11 @@ public class MainActivity extends AppCompatActivity {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_change_instance) {
showRadioButtonDialog();
if (BuildConfig.full_instances) {
showRadioButtonDialogFullInstances();
} else {
showRadioButtonDialog();
}
return true;
} else if (item.getItemId() == R.id.action_account) {
Intent intent;
@ -249,4 +258,43 @@ public class MainActivity extends AppCompatActivity {
AlertDialog alert = alt_bld.create();
alert.show();
}
@SuppressLint("ApplySharedPref")
private void showRadioButtonDialogFullInstances() {
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
alt_bld.setTitle(R.string.instance_choice);
String instance = Helper.getLiveInstance(MainActivity.this);
final EditText input = new EditText(MainActivity.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
input.setLayoutParams(lp);
alt_bld.setView(input);
input.setText(instance);
alt_bld.setPositiveButton(R.string.validate,
(dialog, which) -> new Thread(() -> {
try {
String newInstance = input.getText().toString().trim();
InstanceNodeInfo instanceNodeInfo = new PeertubeAPI(MainActivity.this).displayNodeInfo(newInstance);
if (instanceNodeInfo.getName() != null && instanceNodeInfo.getName().trim().toLowerCase().compareTo("peertube") == 0) {
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.PREF_INSTANCE, newInstance);
editor.commit();
runOnUiThread(() -> {
dialog.dismiss();
recreate();
});
} else {
runOnUiThread(() -> Toasty.error(MainActivity.this, getString(R.string.not_valide_instance), Toast.LENGTH_LONG).show());
}
} catch (Exception e) {
e.printStackTrace();
}
}).start());
alt_bld.setNegativeButton(R.string.cancel, (dialog, which) -> dialog.dismiss());
AlertDialog alert = alt_bld.create();
alert.show();
}
}

View File

@ -30,6 +30,7 @@ import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;
import java.util.Arrays;
import java.util.regex.Matcher;
@ -58,13 +59,19 @@ public class PeertubeRegisterActivity extends AppCompatActivity {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
signup = findViewById(R.id.signup);
TextInputLayout login_instance_container = findViewById(R.id.login_instance_container);
TextInputEditText username = findViewById(R.id.username);
TextInputEditText email = findViewById(R.id.email);
TextInputEditText password = findViewById(R.id.password);
TextInputEditText password_confirm = findViewById(R.id.password_confirm);
TextInputEditText login_instance = findViewById(R.id.login_instance);
CheckBox agreement = findViewById(R.id.agreement);
error_message = findViewById(R.id.error_message);
if (BuildConfig.full_instances) {
login_instance_container.setVisibility(View.VISIBLE);
}
username.setOnFocusChangeListener((view, focused) -> {
if (!focused && username.getText() != null) {
@ -134,8 +141,13 @@ public class PeertubeRegisterActivity extends AppCompatActivity {
}
signup.setEnabled(false);
String host = emailArray[1];
instance = Helper.getPeertubeUrl(host);
if (BuildConfig.full_instances) {
instance = login_instance.getText().toString();
} else {
String host = emailArray[1];
instance = Helper.getPeertubeUrl(host);
}
AccountCreation accountCreation = new AccountCreation();
accountCreation.setEmail(email.getText().toString().trim());
accountCreation.setPassword(password.getText().toString().trim());
@ -193,10 +205,17 @@ public class PeertubeRegisterActivity extends AppCompatActivity {
TextView agreement_text = findViewById(R.id.agreement_text);
String tos = getString(R.string.tos);
String serverrules = getString(R.string.server_rules);
String content_agreement = getString(R.string.agreement_check,
"<a href='https://apps.education.fr/cgu#peertube' >" + serverrules + "</a>",
"<a href='https://apps.education.fr/bonnes-pratiques/' >" + tos + "</a>"
);
String content_agreement;
if (BuildConfig.full_instances) {
content_agreement = getString(R.string.agreement_check_peertube,
"<a href='https://" + instance + "/about/instance#terms-section' >" + tos + "</a>"
);
} else {
content_agreement = getString(R.string.agreement_check,
"<a href='https://apps.education.fr/cgu#peertube' >" + serverrules + "</a>",
"<a href='https://apps.education.fr/bonnes-pratiques/' >" + tos + "</a>"
);
}
agreement_text.setMovementMethod(LinkMovementMethod.getInstance());
agreement_text.setText(Html.fromHtml(content_agreement));
setTitle(R.string.create_an_account);

View File

@ -48,6 +48,8 @@ import app.fedilab.fedilabtube.client.entities.AccountCreation;
import app.fedilab.fedilabtube.client.entities.ChannelCreation;
import app.fedilab.fedilabtube.client.entities.Error;
import app.fedilab.fedilabtube.client.entities.Instance;
import app.fedilab.fedilabtube.client.entities.InstanceNodeInfo;
import app.fedilab.fedilabtube.client.entities.NodeInfo;
import app.fedilab.fedilabtube.client.entities.Peertube;
import app.fedilab.fedilabtube.client.entities.PeertubeAccountNotification;
import app.fedilab.fedilabtube.client.entities.PeertubeActorFollow;
@ -2091,6 +2093,55 @@ public class PeertubeAPI {
return accounts;
}
public InstanceNodeInfo displayNodeInfo(String domain) {
if (domain == null) {
return null;
}
String response;
InstanceNodeInfo instanceNodeInfo = new InstanceNodeInfo();
if (domain.startsWith("http://")) {
domain = domain.replace("http://", "");
}
if (domain.startsWith("https://")) {
domain = domain.replace("https://", "");
}
try {
response = new HttpsConnection(context).get("https://" + 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).get(nodeInfo.getHref(), 30, null, null);
JSONObject resobj = new JSONObject(response);
JSONObject jsonObject = resobj.getJSONObject("software");
String name = jsonObject.getString("name").toUpperCase();
if (name.compareTo("CORGIDON") == 0) {
name = "MASTODON";
}
instanceNodeInfo.setName(name);
instanceNodeInfo.setVersion(jsonObject.getString("version"));
instanceNodeInfo.setOpenRegistrations(resobj.getBoolean("openRegistrations"));
}
} catch (JSONException e) {
e.printStackTrace();
}
} catch (IOException | JSONException | NoSuchAlgorithmException | KeyManagementException | HttpsConnection.HttpsConnectionException e) {
e.printStackTrace();
}
return instanceNodeInfo;
}
/**
* Set the error message
*

View File

@ -0,0 +1,137 @@
package app.fedilab.fedilabtube.client.entities;
/* Copyright 2020 Thomas Schneider
*
* This file is a part of TubeLab
*
* This program is free software; you can redistribute it and/or modify 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.
*
* TubeLab is distributed in the hope that it will be useful, 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 TubeLab; if not,
* see <http://www.gnu.org/licenses>. */
@SuppressWarnings("unused")
public class InstanceNodeInfo {
private String name;
private String title;
private String version;
private boolean openRegistrations;
private boolean connectionError;
private int numberOfUsers = 0;
private int numberOfPosts = 0;
private int numberOfInstance = 0;
private String staffAccountStr;
private Account staffAccount;
private String nodeName;
private String nodeDescription;
private String thumbnail;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public boolean isOpenRegistrations() {
return openRegistrations;
}
public void setOpenRegistrations(boolean openRegistrations) {
this.openRegistrations = openRegistrations;
}
public boolean isConnectionError() {
return connectionError;
}
public void setConnectionError(boolean connectionError) {
this.connectionError = connectionError;
}
public int getNumberOfUsers() {
return numberOfUsers;
}
public void setNumberOfUsers(int numberOfUsers) {
this.numberOfUsers = numberOfUsers;
}
public int getNumberOfPosts() {
return numberOfPosts;
}
public void setNumberOfPosts(int numberOfPosts) {
this.numberOfPosts = numberOfPosts;
}
public String getStaffAccountStr() {
return staffAccountStr;
}
public void setStaffAccountStr(String staffAccountStr) {
this.staffAccountStr = staffAccountStr;
}
public Account getStaffAccount() {
return staffAccount;
}
public void setStaffAccount(Account staffAccount) {
this.staffAccount = staffAccount;
}
public String getNodeName() {
return nodeName;
}
public void setNodeName(String nodeName) {
this.nodeName = nodeName;
}
public String getNodeDescription() {
return nodeDescription;
}
public void setNodeDescription(String nodeDescription) {
this.nodeDescription = nodeDescription;
}
public String getThumbnail() {
return thumbnail;
}
public void setThumbnail(String thumbnail) {
this.thumbnail = thumbnail;
}
public int getNumberOfInstance() {
return numberOfInstance;
}
public void setNumberOfInstance(int numberOfInstance) {
this.numberOfInstance = numberOfInstance;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}

View File

@ -0,0 +1,37 @@
package app.fedilab.fedilabtube.client.entities;
/* Copyright 2020 Thomas Schneider
*
* This file is a part of TubeLab
*
* This program is free software; you can redistribute it and/or modify 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.
*
* TubeLab is distributed in the hope that it will be useful, 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 TubeLab; if not,
* see <http://www.gnu.org/licenses>. */
@SuppressWarnings("unused")
public class NodeInfo {
private String rel;
private String href;
public String getRel() {
return rel;
}
public void setRel(String rel) {
this.rel = rel;
}
public String getHref() {
return href;
}
public void setHref(String href) {
this.href = href;
}
}

View File

@ -38,7 +38,6 @@ import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.ViewModelProvider;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

View File

@ -56,6 +56,7 @@ import java.util.Locale;
import java.util.TimeZone;
import java.util.regex.Pattern;
import app.fedilab.fedilabtube.BuildConfig;
import app.fedilab.fedilabtube.MainActivity;
import app.fedilab.fedilabtube.R;
import app.fedilab.fedilabtube.WebviewActivity;
@ -209,6 +210,9 @@ public class Helper {
*/
public static String getPeertubeUrl(String acad) {
if (BuildConfig.full_instances) {
return acad;
}
if (acad.compareTo("education.gouv.fr") == 0 || acad.compareTo("igesr.gouv.fr") == 0) {
acad = "education.fr";
} else if (acad.compareTo("ac-nancy-metz.fr") == 0) {
@ -241,14 +245,19 @@ public class Helper {
*/
public static String getLiveInstance(Context context) {
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String acad = sharedpreferences.getString(Helper.PREF_INSTANCE, "tube.ac-lyon.fr");
if (acad == null) {
acad = "tube.ac-lyon.fr";
}
if (acad.startsWith("tube-")) {
return acad;
String acad;
if (BuildConfig.full_instances) {
return sharedpreferences.getString(Helper.PREF_INSTANCE, "peertube.social");
} else {
return getPeertubeUrl(acad);
acad = sharedpreferences.getString(Helper.PREF_INSTANCE, "tube.ac-lyon.fr");
if (acad == null) {
acad = "tube.ac-lyon.fr";
}
if (acad.startsWith("tube-")) {
return acad;
} else {
return getPeertubeUrl(acad);
}
}
}