From abdfe8d9d1d46f0c5216a941c9061eeee8dd874a Mon Sep 17 00:00:00 2001 From: anonTree1417 Date: Thu, 2 Apr 2020 02:59:00 +0200 Subject: [PATCH] Adding option to delete certificates. --- .../mian/gitnex/activities/MainActivity.java | 13 +++--- .../gitnex/fragments/SettingsFragment.java | 29 +++++++++++++ .../helpers/ssl/MemorizingTrustManager.java | 4 +- app/src/main/res/layout/fragment_settings.xml | 16 ++++++- .../main/res/layout/layout_settings_certs.xml | 42 +++++++++++++++++++ app/src/main/res/values/strings.xml | 2 + 6 files changed, 97 insertions(+), 9 deletions(-) create mode 100644 app/src/main/res/layout/layout_settings_certs.xml diff --git a/app/src/main/java/org/mian/gitnex/activities/MainActivity.java b/app/src/main/java/org/mian/gitnex/activities/MainActivity.java index 3347c715..7156d378 100644 --- a/app/src/main/java/org/mian/gitnex/activities/MainActivity.java +++ b/app/src/main/java/org/mian/gitnex/activities/MainActivity.java @@ -1,5 +1,6 @@ package org.mian.gitnex.activities; +import android.app.Activity; import android.content.ActivityNotFoundException; import android.content.Context; import android.content.Intent; @@ -101,7 +102,7 @@ public class MainActivity extends BaseActivity implements NavigationView.OnNavig boolean connToInternet = AppUtil.haveNetworkConnection(getApplicationContext()); if(!tinyDb.getBoolean("loggedInMode")) { - logout(); + logout(this, ctx); return; } @@ -354,7 +355,7 @@ public class MainActivity extends BaseActivity implements NavigationView.OnNavig new SettingsFragment()).commit(); break; case R.id.nav_logout: - logout(); + logout(this, ctx); overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out); break; case R.id.nav_about: @@ -392,15 +393,15 @@ public class MainActivity extends BaseActivity implements NavigationView.OnNavig } } - public void logout() { + public static void logout(Activity activity, Context ctx) { - TinyDB tinyDb = new TinyDB(getApplicationContext()); + TinyDB tinyDb = new TinyDB(ctx.getApplicationContext()); tinyDb.putBoolean("loggedInMode", false); tinyDb.remove("basicAuthPassword"); tinyDb.putBoolean("basicAuthFlag", false); //tinyDb.clear(); - finish(); - startActivity(new Intent(MainActivity.this, LoginActivity.class)); + activity.finish(); + ctx.startActivity(new Intent(ctx, LoginActivity.class)); } diff --git a/app/src/main/java/org/mian/gitnex/fragments/SettingsFragment.java b/app/src/main/java/org/mian/gitnex/fragments/SettingsFragment.java index 492af7c4..98fc3441 100644 --- a/app/src/main/java/org/mian/gitnex/fragments/SettingsFragment.java +++ b/app/src/main/java/org/mian/gitnex/fragments/SettingsFragment.java @@ -13,7 +13,9 @@ import android.widget.LinearLayout; import android.widget.Switch; import android.widget.TextView; import org.mian.gitnex.R; +import org.mian.gitnex.activities.MainActivity; import org.mian.gitnex.helpers.Toasty; +import org.mian.gitnex.helpers.ssl.MemorizingTrustManager; import org.mian.gitnex.util.TinyDB; import java.util.Objects; import androidx.annotation.NonNull; @@ -67,6 +69,7 @@ public class SettingsFragment extends Fragment { LinearLayout homeScreenFrame = v.findViewById(R.id.homeScreenFrame); LinearLayout customFontFrame = v.findViewById(R.id.customFontFrame); LinearLayout themeFrame = v.findViewById(R.id.themeSelectionFrame); + LinearLayout certsFrame = v.findViewById(R.id.certsFrame); Switch issuesSwitch = v.findViewById(R.id.switchIssuesBadge); Switch pdfModeSwitch = v.findViewById(R.id.switchPdfMode); @@ -144,6 +147,32 @@ public class SettingsFragment extends Fragment { pdfModeSwitch.setChecked(false); } + // certs deletion + certsFrame.setOnClickListener(new View.OnClickListener() { + + @Override + public void onClick(View v) { + AlertDialog.Builder builder = new AlertDialog.Builder(ctx); + builder.setTitle("Delete trusted certificates?"); + builder.setMessage("Are you really sure to delete any manually trusted certificate?\n\nNotice: Your will be logged out too."); + builder.setPositiveButton("YES", new DialogInterface.OnClickListener() { + + @Override + public void onClick(DialogInterface dialog, int which) { + ctx.getSharedPreferences(MemorizingTrustManager.KEYSTORE_NAME, Context.MODE_PRIVATE) + .edit() + .remove(MemorizingTrustManager.KEYSTORE_KEY) + .apply(); + + MainActivity.logout(Objects.requireNonNull(getActivity()), ctx); + } + }); + + builder.setNeutralButton("NO", (dialog, which) -> dialog.dismiss()); + builder.create().show(); + } + }); + // issues badge switcher issuesSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { diff --git a/app/src/main/java/org/mian/gitnex/helpers/ssl/MemorizingTrustManager.java b/app/src/main/java/org/mian/gitnex/helpers/ssl/MemorizingTrustManager.java index 49ea6f6e..d79f1d6c 100644 --- a/app/src/main/java/org/mian/gitnex/helpers/ssl/MemorizingTrustManager.java +++ b/app/src/main/java/org/mian/gitnex/helpers/ssl/MemorizingTrustManager.java @@ -45,8 +45,8 @@ import javax.net.ssl.X509TrustManager; public class MemorizingTrustManager implements X509TrustManager { private final static int NOTIFICATION_ID = 100509; - private final static String KEYSTORE_NAME = "keystore"; - private final static String KEYSTORE_KEY = "keystore"; + public final static String KEYSTORE_NAME = "keystore"; + public final static String KEYSTORE_KEY = "keystore"; private Context context; private NotificationManager notificationManager; diff --git a/app/src/main/res/layout/fragment_settings.xml b/app/src/main/res/layout/fragment_settings.xml index 5f6eb145..09aec96b 100644 --- a/app/src/main/res/layout/fragment_settings.xml +++ b/app/src/main/res/layout/fragment_settings.xml @@ -33,7 +33,7 @@ layout="@layout/layout_settings_fileview"/> + + + + diff --git a/app/src/main/res/layout/layout_settings_certs.xml b/app/src/main/res/layout/layout_settings_certs.xml new file mode 100644 index 00000000..52b4a67b --- /dev/null +++ b/app/src/main/res/layout/layout_settings_certs.xml @@ -0,0 +1,42 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c70314e0..711cf468 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -246,6 +246,8 @@ Translation + Certs + Delete certificates Date & Time Settings saved Language