added TLS 1.2 support for old android versions, fixed crash on android < 21, layout fix

This commit is contained in:
nuclearfog 2021-01-24 19:32:33 +01:00
parent 3252dc4410
commit 1c4c0fe4ad
No known key found for this signature in database
GPG Key ID: D5490E4A81F97B14
12 changed files with 134 additions and 46 deletions

View File

@ -25,11 +25,6 @@ import org.nuclearfog.twidda.backend.utils.AppStyles;
import org.nuclearfog.twidda.backend.utils.ErrorHandler;
import org.nuclearfog.twidda.database.GlobalSettings;
import java.security.NoSuchAlgorithmException;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLParameters;
import static android.content.Intent.ACTION_VIEW;
import static android.os.AsyncTask.Status.FINISHED;
import static android.os.AsyncTask.Status.RUNNING;
@ -65,7 +60,6 @@ public class LoginActivity extends AppCompatActivity implements OnClickListener
linkButton.setOnClickListener(this);
loginButton.setOnClickListener(this);
checkTLSSupport();
}
@ -167,26 +161,4 @@ public class LoginActivity extends AppCompatActivity implements OnClickListener
public void onError(EngineException error) {
ErrorHandler.handleFailure(this, error);
}
/**
* Check if phone supports TLS 1.2 which is required for twitter api
*/
private void checkTLSSupport() {
boolean tls12Found = false;
try {
SSLParameters param = SSLContext.getDefault().getDefaultSSLParameters();
String[] protocols = param.getProtocols();
for (String protocol : protocols) {
if (protocol.equals("TLSv1.2") || protocol.equals("TLSv1.3")) {
tls12Found = true;
break;
}
}
} catch (NoSuchAlgorithmException er) {
// ignore
}
if (!tls12Found) {
Toast.makeText(this, R.string.info_phone_tls_support, LENGTH_LONG).show();
}
}
}

View File

@ -16,6 +16,7 @@ import android.widget.TextView;
import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView.Adapter;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
@ -77,8 +78,10 @@ public class ListAdapter extends Adapter<ViewHolder> {
TypedArray drawables = context.getResources().obtainTypedArray(R.array.list_item_icons);
icons = new Drawable[drawables.length()];
for (int index = 0; index < drawables.length(); index++)
icons[index] = drawables.getDrawable(index);
for (int index = 0; index < drawables.length(); index++) {
int resId = drawables.getResourceId(index, 0);
icons[index] = AppCompatResources.getDrawable(context, resId);
}
drawables.recycle();
colorIcons();
}

View File

@ -15,6 +15,7 @@ import android.widget.TextView;
import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView.Adapter;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
@ -57,8 +58,10 @@ public class MessageAdapter extends Adapter<ViewHolder> {
TypedArray drawables = context.getResources().obtainTypedArray(R.array.dm_item_icons);
icons = new Drawable[drawables.length()];
for (int index = 0; index < drawables.length(); index++)
icons[index] = drawables.getDrawable(index);
for (int index = 0; index < drawables.length(); index++) {
int resId = drawables.getResourceId(index, 0);
icons[index] = AppCompatResources.getDrawable(context, resId);
}
drawables.recycle();
setIconColor();
}

View File

@ -18,6 +18,7 @@ import android.widget.TextView;
import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView.Adapter;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
@ -85,8 +86,10 @@ public class TweetAdapter extends Adapter<ViewHolder> {
TypedArray tArray = context.getResources().obtainTypedArray(R.array.tweet_item_icons);
icons = new Drawable[tArray.length()];
for (int index = 0; index < icons.length; index++)
icons[index] = tArray.getDrawable(index);
for (int index = 0; index < icons.length; index++) {
int resId = tArray.getResourceId(index, 0);
icons[index] = AppCompatResources.getDrawable(context, resId);
}
tArray.recycle();
setIconColors();
}

View File

@ -17,6 +17,7 @@ import android.widget.TextView;
import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView.Adapter;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
@ -79,8 +80,10 @@ public class UserAdapter extends Adapter<ViewHolder> {
TypedArray drawables = context.getResources().obtainTypedArray(R.array.user_item_icons);
icons = new Drawable[drawables.length()];
for (int index = 0; index < drawables.length(); index++)
icons[index] = drawables.getDrawable(index);
for (int index = 0; index < drawables.length(); index++) {
int resId = drawables.getResourceId(index, 0);
icons[index] = AppCompatResources.getDrawable(context, resId);
}
drawables.recycle();
setIconColor();
}

View File

@ -0,0 +1,82 @@
package org.nuclearfog.twidda.backend.engine;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
/**
* @author fkrauthan
* @see <a href="https://gist.githubusercontent.com/fkrauthan/ac8624466a4dee4fd02f/raw/309efc30e31c96a932ab9d19bf4d73b286b00573/TLSSocketFactory.java"/>
*/
public class TLSSocketFactory extends SSLSocketFactory {
private SSLSocketFactory internalSSLSocketFactory;
public TLSSocketFactory() throws KeyManagementException, NoSuchAlgorithmException {
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, null, null);
internalSSLSocketFactory = context.getSocketFactory();
}
@Override
public String[] getDefaultCipherSuites() {
return internalSSLSocketFactory.getDefaultCipherSuites();
}
@Override
public String[] getSupportedCipherSuites() {
return internalSSLSocketFactory.getSupportedCipherSuites();
}
@Override
public Socket createSocket() throws IOException {
return enableTLSOnSocket(internalSSLSocketFactory.createSocket());
}
@Override
public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(s, host, port, autoClose));
}
@Override
public Socket createSocket(String host, int port) throws IOException {
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port));
}
@Override
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException {
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port, localHost, localPort));
}
@Override
public Socket createSocket(InetAddress host, int port) throws IOException {
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port));
}
@Override
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(address, port, localAddress, localPort));
}
private Socket enableTLSOnSocket(Socket socket) {
if (socket instanceof SSLSocket) {
((SSLSocket) socket).setEnabledProtocols(new String[]{"TLSv1.1", "TLSv1.2"});
}
return socket;
}
}

View File

@ -30,6 +30,10 @@ import java.net.URL;
import java.util.LinkedList;
import java.util.List;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLParameters;
import io.michaelrocks.paranoid.Obfuscate;
import twitter4j.DirectMessage;
import twitter4j.GeoLocation;
@ -76,6 +80,23 @@ public class TwitterEngine {
* Initialize Twitter4J instance
*/
private void initTwitter() {
// check for TLS 1.2 support and activate it
try {
boolean tlsEnabled = false;
SSLParameters param = SSLContext.getDefault().getDefaultSSLParameters();
String[] protocols = param.getProtocols();
for (String protocol : protocols) {
if (protocol.equals("TLSv1.2") || protocol.equals("TLSv1.3")) {
tlsEnabled = true;
break;
}
}
if (!tlsEnabled) {
HttpsURLConnection.setDefaultSSLSocketFactory(new TLSSocketFactory());
}
} catch (Exception err) {
err.printStackTrace();
}
ConfigurationBuilder builder = new ConfigurationBuilder();
if (settings.isCustomApiSet()) {
builder.setOAuthConsumerKey(settings.getConsumerKey());

View File

@ -22,6 +22,7 @@ import android.widget.TextView;
import androidx.annotation.ArrayRes;
import androidx.annotation.Nullable;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.cardview.widget.CardView;
import androidx.viewpager.widget.ViewPager;
@ -231,7 +232,10 @@ public final class AppStyles {
}
/**
* set Tab icons for TabLayout
* set tab icons
*
* @param tabLayout tablayout to set tab icons
* @param settings settings to set color
*/
public static void setTabIcons(TabLayout tabLayout, GlobalSettings settings, @ArrayRes int array) {
Context context = tabLayout.getContext();
@ -239,7 +243,8 @@ public final class AppStyles {
for (int index = 0; index < tArray.length(); index++) {
TabLayout.Tab mTab = tabLayout.getTabAt(index);
if (mTab != null) {
Drawable icon = tArray.getDrawable(index);
int resId = tArray.getResourceId(index, 0);
Drawable icon = AppCompatResources.getDrawable(context, resId);
setDrawableColor(icon, settings.getIconColor());
mTab.setIcon(icon);
}
@ -262,7 +267,8 @@ public final class AppStyles {
for (int index = 0; index < tArray.length(); index++) {
TabLayout.Tab mTab = tabLayout.getTabAt(index);
if (mTab != null) {
Drawable icon = tArray.getDrawable(index);
int resId = tArray.getResourceId(index, 0);
Drawable icon = AppCompatResources.getDrawable(context, resId);
setDrawableColor(icon, settings.getIconColor());
View v = View.inflate(context, R.layout.icon_profile_tab, null);
ImageView imageIcon = v.findViewById(R.id.tab_icon);

View File

@ -4,6 +4,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@android:color/white"
android:orientation="vertical">
<TextView

View File

@ -95,10 +95,8 @@
<string name="proxy_password">Passwort</string>
<string name="error_dm_send">Direktnachricht konnte nicht an diesen Nutzer gesendet werden!</string>
<string name="confirm_mute">Nutzer stummschalten?</string>
<string name="error_location">Standort konnte nicht ermittelt werden!</string>
<string name="error_cant_copy_clipboard">Link konnte nicht kopiert werden!</string>
<string name="info_gps_attached">GPS Position hinzugefügt</string>
<string name="info_get_location">starte GPS lokalisierung…</string>
<string name="error_gps">GPS lokalisierung fehlgeschlagen!</string>
<string name="info_get_link">PIN muss erst von Twitter abgefragt werden! Bitte den ersten Button drücken!</string>
<string name="error_token_not_set">Bitte zuerst link anklicken!</string>
@ -134,7 +132,6 @@
<string name="error_wrong_connection_settings">Falsche Verbindung angegeben!</string>
<string name="error_open_link">Link konnte nicht geöffnet werden!</string>
<string name="tweet_sensitive_media">Sensible Inhalte</string>
<string name="info_phone_tls_support">TLS 1.2 wird nicht unterstützt. Die App wird möglicherweise nicht funktionieren!</string>
<string name="login_info">3 Schritte zum Login</string>
<string name="info_fetching_link">öffne Twitter login Seite</string>
<string name="info_login_to_twitter">Melde in Twitter an</string>

View File

@ -112,12 +112,10 @@
<string name="info_list_created">Userlist created</string>
<string name="info_fetching_link">redirecting to Twitter login</string>
<string name="info_login_to_twitter">login to Twitter</string>
<string name="info_phone_tls_support">Phone does not support TLS 1.2. App will probably not work!</string>
<string name="error_wrong_connection_settings">Wrong connection settings!</string>
<string name="info_cant_add_video">can\'t add video</string>
<string name="info_get_link">get Twitter PIN from browser first. Please press the first button!</string>
<string name="info_gps_attached">GPS position added</string>
<string name="info_get_location">starting location…</string>
<string name="info_tweet_retweeted">retweeted</string>
<string name="info_tweet_unretweeted">Retweet removed</string>
<string name="info_tweet_favored">Tweet added to favorites</string>
@ -153,7 +151,6 @@
<string name="error_no_media_app">No gallery app found!</string>
<string name="error_connection_failed">Connection failed!</string>
<string name="error_invalid_link">Invalid link!</string>
<string name="error_location">could not fetch location!</string>
<string name="error_cant_copy_clipboard">can\'t copy link to clipboard!</string>
<string name="error_gps">could not fetch GPS data!</string>
<string name="error_token_not_set">Please get link first!</string>

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.AppCompat">
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="colorAccent">@android:color/white</item>
<item name="android:colorBackground">@android:color/black</item>
<item name="android:windowAnimationStyle">@style/TransactionPending</item>