From 42bdcaf6b60b086959e269ff6155623713f3613e Mon Sep 17 00:00:00 2001 From: Thomas Date: Thu, 26 Jan 2023 12:08:31 +0100 Subject: [PATCH] Support proxy for Peertube --- .../app/fedilab/android/BaseMainActivity.java | 93 ++++++++++--------- .../activities/PeertubeMainActivity.java | 5 +- .../peertube/client/RetrofitPeertubeAPI.java | 15 ++- 3 files changed, 63 insertions(+), 50 deletions(-) diff --git a/app/src/main/java/app/fedilab/android/BaseMainActivity.java b/app/src/main/java/app/fedilab/android/BaseMainActivity.java index b2f68571..19bdccfc 100644 --- a/app/src/main/java/app/fedilab/android/BaseMainActivity.java +++ b/app/src/main/java/app/fedilab/android/BaseMainActivity.java @@ -72,6 +72,7 @@ import androidx.core.app.ActivityOptionsCompat; import androidx.core.view.GravityCompat; import androidx.cursoradapter.widget.CursorAdapter; import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentTransaction; import androidx.lifecycle.ViewModelProvider; import androidx.localbroadcastmanager.content.LocalBroadcastManager; @@ -562,6 +563,52 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt } } + public static void headerOptionInfoClick(Activity activity, NavHeaderMainBinding headerMainBinding, FragmentManager fragmentManager) { + PopupMenu popup = new PopupMenu(activity, headerMainBinding.headerOptionInfo); + popup.getMenuInflater() + .inflate(R.menu.main, popup.getMenu()); + + popup.setOnMenuItemClickListener(item -> { + int itemId = item.getItemId(); + if (itemId == R.id.action_logout_account) { + AlertDialog.Builder alt_bld = new MaterialAlertDialogBuilder(activity, Helper.dialogStyle()); + alt_bld.setTitle(R.string.action_logout); + if (currentAccount.mastodon_account != null && currentAccount.instance != null) { + alt_bld.setMessage(activity.getString(R.string.logout_account_confirmation, currentAccount.mastodon_account.username, currentAccount.instance)); + } else if (currentAccount.peertube_account != null && currentAccount.instance != null) { + alt_bld.setMessage(activity.getString(R.string.logout_account_confirmation, currentAccount.peertube_account.getUsername(), currentAccount.instance)); + } else { + alt_bld.setMessage(activity.getString(R.string.logout_account_confirmation, "", "")); + } + alt_bld.setPositiveButton(R.string.action_logout, (dialog, id) -> { + dialog.dismiss(); + try { + Helper.removeAccount(activity); + } catch (DBException e) { + e.printStackTrace(); + } + }); + alt_bld.setNegativeButton(R.string.cancel, (dialog, id) -> dialog.dismiss()); + AlertDialog alert = alt_bld.create(); + alert.show(); + return true; + } else if (itemId == R.id.action_proxy) { + (new ProxyActivity()).show(fragmentManager, null); + return true; + } + return true; + }); + popup.show(); + } + + protected abstract void rateThisApp(); + + @Override + protected void onNewIntent(Intent intent) { + super.onNewIntent(intent); + mamageNewIntent(intent); + } + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -891,43 +938,7 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt manageDrawerMenu(BaseMainActivity.this, binding.navView, headerMainBinding); }); - headerMainBinding.headerOptionInfo.setOnClickListener(v -> { - PopupMenu popup = new PopupMenu(BaseMainActivity.this, headerMainBinding.headerOptionInfo); - popup.getMenuInflater() - .inflate(R.menu.main, popup.getMenu()); - - popup.setOnMenuItemClickListener(item -> { - int itemId = item.getItemId(); - if (itemId == R.id.action_logout_account) { - AlertDialog.Builder alt_bld = new MaterialAlertDialogBuilder(BaseMainActivity.this, Helper.dialogStyle()); - alt_bld.setTitle(R.string.action_logout); - if (currentAccount.mastodon_account != null && currentAccount.mastodon_account.username != null && currentAccount.instance != null) { - alt_bld.setMessage(getString(R.string.logout_account_confirmation, currentAccount.mastodon_account.username, currentAccount.instance)); - } else if (currentAccount.mastodon_account != null && currentAccount.mastodon_account.acct != null) { - alt_bld.setMessage(getString(R.string.logout_account_confirmation, currentAccount.mastodon_account.acct, "")); - } else { - alt_bld.setMessage(getString(R.string.logout_account_confirmation, "", "")); - } - alt_bld.setPositiveButton(R.string.action_logout, (dialog, id) -> { - dialog.dismiss(); - try { - Helper.removeAccount(BaseMainActivity.this); - } catch (DBException e) { - e.printStackTrace(); - } - }); - alt_bld.setNegativeButton(R.string.cancel, (dialog, id) -> dialog.dismiss()); - AlertDialog alert = alt_bld.create(); - alert.show(); - return true; - } else if (itemId == R.id.action_proxy) { - (new ProxyActivity()).show(getSupportFragmentManager(), null); - return true; - } - return true; - }); - popup.show(); - }); + headerMainBinding.headerOptionInfo.setOnClickListener(v -> headerOptionInfoClick(BaseMainActivity.this, headerMainBinding, getSupportFragmentManager())); //Toolbar search binding.toolbarSearch.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @@ -1044,14 +1055,6 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt fetchRecentAccounts(BaseMainActivity.this, headerMainBinding); } - protected abstract void rateThisApp(); - - @Override - protected void onNewIntent(Intent intent) { - super.onNewIntent(intent); - mamageNewIntent(intent); - } - /** * Open notifications tab when coming from a notification device * diff --git a/app/src/main/java/app/fedilab/android/peertube/activities/PeertubeMainActivity.java b/app/src/main/java/app/fedilab/android/peertube/activities/PeertubeMainActivity.java index f1c6bc2e..0d55ea7f 100644 --- a/app/src/main/java/app/fedilab/android/peertube/activities/PeertubeMainActivity.java +++ b/app/src/main/java/app/fedilab/android/peertube/activities/PeertubeMainActivity.java @@ -19,6 +19,7 @@ import static app.fedilab.android.BaseMainActivity.currentInstance; import static app.fedilab.android.BaseMainActivity.currentToken; import static app.fedilab.android.BaseMainActivity.fetchRecentAccounts; import static app.fedilab.android.BaseMainActivity.headerMenuOpen; +import static app.fedilab.android.BaseMainActivity.headerOptionInfoClick; import static app.fedilab.android.BaseMainActivity.manageDrawerMenu; import static app.fedilab.android.mastodon.helper.Helper.PREF_USER_ID; import static app.fedilab.android.mastodon.helper.Helper.PREF_USER_INSTANCE; @@ -306,7 +307,9 @@ public class PeertubeMainActivity extends PeertubeBaseMainActivity { mainHandler.post(myRunnable); }).start(); headerMainBinding.instanceInfo.setVisibility(View.GONE); - headerMainBinding.headerOptionInfo.setVisibility(View.GONE); + + headerMainBinding.headerOptionInfo.setOnClickListener(v -> headerOptionInfoClick(PeertubeMainActivity.this, headerMainBinding, getSupportFragmentManager())); + binding.drawerNavView.addHeaderView(headerMainBinding.getRoot()); binding.drawerNavView.setNavigationItemSelectedListener(item -> { diff --git a/app/src/main/java/app/fedilab/android/peertube/client/RetrofitPeertubeAPI.java b/app/src/main/java/app/fedilab/android/peertube/client/RetrofitPeertubeAPI.java index d89cd88d..14fa77b3 100644 --- a/app/src/main/java/app/fedilab/android/peertube/client/RetrofitPeertubeAPI.java +++ b/app/src/main/java/app/fedilab/android/peertube/client/RetrofitPeertubeAPI.java @@ -105,10 +105,7 @@ import retrofit2.converter.gson.GsonConverterFactory; @SuppressWarnings({"unused", "RedundantSuppression", "ConstantConditions"}) public class RetrofitPeertubeAPI { - final OkHttpClient okHttpClient = new OkHttpClient.Builder() - .readTimeout(60, TimeUnit.SECONDS) - .connectTimeout(60, TimeUnit.SECONDS) - .build(); + private final String finalUrl; private final Context _context; private final String instance; @@ -213,6 +210,11 @@ public class RetrofitPeertubeAPI { } private PeertubeService init() { + final OkHttpClient okHttpClient = new OkHttpClient.Builder() + .readTimeout(60, TimeUnit.SECONDS) + .connectTimeout(60, TimeUnit.SECONDS) + .proxy(app.fedilab.android.mastodon.helper.Helper.getProxy(_context.getApplicationContext())) + .build(); Retrofit retrofit = new Retrofit.Builder() .baseUrl(finalUrl) .addConverterFactory(GsonConverterFactory.create()) @@ -230,6 +232,11 @@ public class RetrofitPeertubeAPI { if (!URLUtil.isValidUrl("https://" + instance)) { return null; } + final OkHttpClient okHttpClient = new OkHttpClient.Builder() + .readTimeout(60, TimeUnit.SECONDS) + .connectTimeout(60, TimeUnit.SECONDS) + .proxy(app.fedilab.android.mastodon.helper.Helper.getProxy(_context.getApplicationContext())) + .build(); Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://" + instance) .addConverterFactory(GsonConverterFactory.create())