From c09a7a3c2b977af40b0c5ac1bfd2e4a2d779cd26 Mon Sep 17 00:00:00 2001 From: Thomas Date: Mon, 23 Jan 2023 18:06:25 +0100 Subject: [PATCH] improvements --- .../app/fedilab/android/BaseMainActivity.java | 376 +++++++++--------- .../mastodon/client/entities/app/Account.java | 3 +- .../peertube/activities/LoginActivity.java | 3 +- .../peertube/activities/PeertubeActivity.java | 62 +-- .../peertube/activities/WebviewActivity.java | 4 +- .../peertube/client/RetrofitPeertubeAPI.java | 16 +- .../android/peertube/helper/Helper.java | 10 +- .../peertube/webview/CustomWebview.java | 53 --- .../webview/MastalabWebChromeClient.java | 4 +- .../android/peertube/webview/ProxyHelper.java | 9 +- .../peertube/layout/activity_peertube.xml | 6 +- .../layout/activity_webview_peertube.xml | 2 +- 12 files changed, 256 insertions(+), 292 deletions(-) delete mode 100644 app/src/main/java/app/fedilab/android/peertube/webview/CustomWebview.java diff --git a/app/src/main/java/app/fedilab/android/BaseMainActivity.java b/app/src/main/java/app/fedilab/android/BaseMainActivity.java index 9230ae23d..c2fdc3e70 100644 --- a/app/src/main/java/app/fedilab/android/BaseMainActivity.java +++ b/app/src/main/java/app/fedilab/android/BaseMainActivity.java @@ -18,6 +18,7 @@ import static app.fedilab.android.BaseMainActivity.status.DISCONNECTED; import static app.fedilab.android.BaseMainActivity.status.UNKNOWN; import static app.fedilab.android.mastodon.helper.CacheHelper.deleteDir; import static app.fedilab.android.mastodon.helper.Helper.PREF_USER_TOKEN; +import static app.fedilab.android.mastodon.helper.Helper.TAG; import static app.fedilab.android.mastodon.helper.Helper.displayReleaseNotesIfNeeded; import static app.fedilab.android.mastodon.ui.drawer.StatusAdapter.sendAction; @@ -41,6 +42,7 @@ import android.provider.BaseColumns; import android.text.Editable; import android.text.Html; import android.text.TextWatcher; +import android.util.Log; import android.util.Patterns; import android.util.TypedValue; import android.view.Gravity; @@ -335,8 +337,195 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt }); permissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS); } + NavHeaderMainBinding headerMainBinding = NavHeaderMainBinding.inflate(getLayoutInflater()); + currentAccount = null; + //Update account details + new Thread(() -> { + try { + if (currentToken == null) { + currentToken = sharedpreferences.getString(Helper.PREF_USER_TOKEN, null); + } + currentAccount = new Account(BaseMainActivity.this).getConnectedAccount(); + Log.v(TAG, "currentToken! " + currentToken); + Log.v(TAG, "currentAccount! " + currentAccount); + if (currentAccount != null && currentAccount.api == Account.API.PEERTUBE) { + startActivity(new Intent(this, PeertubeBaseMainActivity.class)); + finish(); + } + } catch (DBException e) { + e.printStackTrace(); + } + //If the attached account is null, the app will fetch remote instance to get up-to-date values + if (currentAccount != null && currentAccount.mastodon_account == null) { + OkHttpClient okHttpClient = new OkHttpClient.Builder() + .readTimeout(60, TimeUnit.SECONDS) + .connectTimeout(60, TimeUnit.SECONDS) + .callTimeout(60, TimeUnit.SECONDS) + .proxy(Helper.getProxy(getApplication().getApplicationContext())) + .build(); + Retrofit retrofit = new Retrofit.Builder() + .baseUrl("https://" + MainActivity.currentInstance + "/api/v1/") + .addConverterFactory(GsonConverterFactory.create(Helper.getDateBuilder())) + .client(okHttpClient) + .build(); + MastodonAccountsService mastodonAccountsService = retrofit.create(MastodonAccountsService.class); + retrofit2.Call accountCall = mastodonAccountsService.verify_credentials(MainActivity.currentToken); + if (accountCall != null) { + try { + retrofit2.Response accountResponse = accountCall.execute(); + if (accountResponse.isSuccessful()) { + currentAccount.mastodon_account = accountResponse.body(); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + } + Handler mainHandler = new Handler(Looper.getMainLooper()); + Runnable myRunnable = () -> { + if (currentAccount == null || currentAccount.mastodon_account == null) { + //It is not, the user is redirected to the login page + Intent myIntent = new Intent(BaseMainActivity.this, LoginActivity.class); + startActivity(myIntent); + finish(); + return; + } + bottomMenu = new BottomMenu(BaseMainActivity.this).hydrate(currentAccount, binding.bottomNavView); + if (currentAccount.mastodon_account.locked) { + binding.navView.getMenu().findItem(R.id.nav_follow_requests).setVisible(true); + } + if (currentAccount.admin) { + binding.navView.getMenu().findItem(R.id.nav_administration).setVisible(true); + } + if (bottomMenu != null) { + //ManageClick on bottom menu items + if (binding.bottomNavView.findViewById(R.id.nav_home) != null) { + binding.bottomNavView.findViewById(R.id.nav_home).setOnLongClickListener(view -> { + int position = BottomMenu.getPosition(bottomMenu, R.id.nav_home); + if (position >= 0) { + manageFilters(position); + } + return false; + }); + } + if (binding.bottomNavView.findViewById(R.id.nav_local) != null) { + binding.bottomNavView.findViewById(R.id.nav_local).setOnLongClickListener(view -> { + int position = BottomMenu.getPosition(bottomMenu, R.id.nav_local); + if (position >= 0) { + manageFilters(position); + } + return false; + }); + } + if (binding.bottomNavView.findViewById(R.id.nav_public) != null) { + binding.bottomNavView.findViewById(R.id.nav_public).setOnLongClickListener(view -> { + int position = BottomMenu.getPosition(bottomMenu, R.id.nav_public); + if (position >= 0) { + manageFilters(position); + } + return false; + }); + } + binding.bottomNavView.setOnItemSelectedListener(item -> { + int itemId = item.getItemId(); + int position = BottomMenu.getPosition(bottomMenu, itemId); + if (position >= 0) { + if (binding.viewPager.getCurrentItem() == position) { + scrollToTop(); + binding.bottomNavView.removeBadge(itemId); + } else { + binding.viewPager.setCurrentItem(position, false); + } + } + return true; + }); + } + currentInstance = currentAccount.instance; + currentUserID = currentAccount.user_id; + show_boosts = sharedpreferences.getBoolean(getString(R.string.SET_SHOW_BOOSTS) + currentUserID + currentInstance, true); + show_replies = sharedpreferences.getBoolean(getString(R.string.SET_SHOW_REPLIES) + currentUserID + currentInstance, true); + show_dms = sharedpreferences.getBoolean(getString(R.string.SET_SHOW_DMS) + currentUserID + currentInstance, true); + regex_home = sharedpreferences.getString(getString(R.string.SET_FILTER_REGEX_HOME) + currentUserID + currentInstance, null); + regex_local = sharedpreferences.getString(getString(R.string.SET_FILTER_REGEX_LOCAL) + currentUserID + currentInstance, null); + regex_public = sharedpreferences.getString(getString(R.string.SET_FILTER_REGEX_PUBLIC) + currentUserID + currentInstance, null); + show_art_nsfw = sharedpreferences.getBoolean(getString(R.string.SET_ART_WITH_NSFW) + currentUserID + currentInstance, false); + + binding.profilePicture.setOnClickListener(v -> binding.drawerLayout.openDrawer(GravityCompat.START)); + Helper.loadPP(BaseMainActivity.this, binding.profilePicture, currentAccount); + headerMainBinding.accountAcc.setText(String.format("%s@%s", currentAccount.mastodon_account.username, currentAccount.instance)); + if (currentAccount.mastodon_account.display_name == null || currentAccount.mastodon_account.display_name.isEmpty()) { + currentAccount.mastodon_account.display_name = currentAccount.mastodon_account.acct; + } + if (!isFinishing()) { + headerMainBinding.accountName.setText( + currentAccount.mastodon_account.getSpanDisplayName(BaseMainActivity.this, + new WeakReference<>(headerMainBinding.accountName)), + TextView.BufferType.SPANNABLE); + } + float scale = sharedpreferences.getFloat(getString(R.string.SET_FONT_SCALE), 1.1f); + headerMainBinding.accountName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18 * 1.1f / scale); + headerMainBinding.accountAcc.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18 * 1.1f / scale); + Helper.loadPP(BaseMainActivity.this, headerMainBinding.accountProfilePicture, currentAccount, false); + MastodonHelper.loadProfileMediaMastodon(BaseMainActivity.this, headerMainBinding.backgroundImage, currentAccount.mastodon_account, MastodonHelper.MediaAccountType.HEADER); + headerMainBinding.backgroundImage.setAlpha(0.5f); + /* + * Some general data are loaded when the app starts such; + * - Pinned timelines (in app feature) + * - Instance info (for limits) + * - Emoji for picker + * - Filters for timelines + + */ + + //Update pinned timelines + new ViewModelProvider(BaseMainActivity.this).get(TopBarVM.class).getDBPinned() + .observe(this, pinned -> { + this.pinned = pinned; + //Initialize the slug of the first fragment + //First it's taken from db (last stored values) + PinnedTimelineHelper.redrawTopBarPinned(BaseMainActivity.this, binding, pinned, bottomMenu, null); + //Fetch remote lists for the authenticated account and update them + new ViewModelProvider(BaseMainActivity.this).get(TimelinesVM.class).getLists(currentInstance, currentToken) + .observe(this, mastodonLists -> + PinnedTimelineHelper.redrawTopBarPinned(BaseMainActivity.this, binding, pinned, bottomMenu, mastodonLists) + ); + }); + + //Update emoji in db for the current instance + new ViewModelProvider(BaseMainActivity.this).get(InstancesVM.class).getEmoji(currentInstance); + //Retrieve instance info + new ViewModelProvider(BaseMainActivity.this).get(InstancesVM.class).getInstance(currentInstance) + .observe(BaseMainActivity.this, instance -> { + instanceInfo = instance.info; + SharedPreferences.Editor editor = sharedpreferences.edit(); + editor.putString(getString(R.string.INSTANCE_INFO) + MainActivity.currentInstance, Instance.serialize(instanceInfo)); + editor.apply(); + }); + //Retrieve filters + new ViewModelProvider(BaseMainActivity.this).get(FiltersVM.class).getFilters(currentInstance, currentToken) + .observe(BaseMainActivity.this, filters -> mainFilters = filters); + new ViewModelProvider(BaseMainActivity.this).get(AccountsVM.class).getConnectedAccount(currentInstance, currentToken) + .observe(BaseMainActivity.this, mastodonAccount -> { + //Initialize static var + if (mastodonAccount != null && currentAccount != null) { + currentAccount.mastodon_account = mastodonAccount; + displayReleaseNotesIfNeeded(BaseMainActivity.this, false); + new Thread(() -> { + try { + //Update account in db + new Account(BaseMainActivity.this).insertOrUpdate(currentAccount); + } catch (DBException e) { + e.printStackTrace(); + } + }).start(); + } + }); + + }; + mainHandler.post(myRunnable); + }).start(); filteredAccounts = new ArrayList<>(); mamageNewIntent(getIntent()); filterFetched = false; @@ -363,7 +552,7 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt .setOpenableLayout(binding.drawerLayout) .build(); - NavHeaderMainBinding headerMainBinding = NavHeaderMainBinding.inflate(getLayoutInflater()); + binding.navView.addHeaderView(headerMainBinding.getRoot()); binding.navView.setNavigationItemSelectedListener(menuItem -> { int id = menuItem.getItemId(); @@ -593,192 +782,7 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt }); popup.show(); }); - currentAccount = null; - //Update account details - new Thread(() -> { - try { - if (currentToken == null) { - currentToken = sharedpreferences.getString(Helper.PREF_USER_TOKEN, null); - } - currentAccount = new Account(BaseMainActivity.this).getConnectedAccount(); - if (currentAccount.api == Account.API.PEERTUBE) { - startActivity(new Intent(this, PeertubeBaseMainActivity.class)); - finish(); - } - } catch (DBException e) { - e.printStackTrace(); - } - //If the attached account is null, the app will fetch remote instance to get up-to-date values - if (currentAccount != null && currentAccount.mastodon_account == null) { - OkHttpClient okHttpClient = new OkHttpClient.Builder() - .readTimeout(60, TimeUnit.SECONDS) - .connectTimeout(60, TimeUnit.SECONDS) - .callTimeout(60, TimeUnit.SECONDS) - .proxy(Helper.getProxy(getApplication().getApplicationContext())) - .build(); - Retrofit retrofit = new Retrofit.Builder() - .baseUrl("https://" + MainActivity.currentInstance + "/api/v1/") - .addConverterFactory(GsonConverterFactory.create(Helper.getDateBuilder())) - .client(okHttpClient) - .build(); - MastodonAccountsService mastodonAccountsService = retrofit.create(MastodonAccountsService.class); - retrofit2.Call accountCall = mastodonAccountsService.verify_credentials(MainActivity.currentToken); - if (accountCall != null) { - try { - retrofit2.Response accountResponse = accountCall.execute(); - if (accountResponse.isSuccessful()) { - currentAccount.mastodon_account = accountResponse.body(); - } - } catch (Exception e) { - e.printStackTrace(); - } - } - } - Handler mainHandler = new Handler(Looper.getMainLooper()); - Runnable myRunnable = () -> { - if (currentAccount == null || currentAccount.mastodon_account == null) { - //It is not, the user is redirected to the login page - Intent myIntent = new Intent(BaseMainActivity.this, LoginActivity.class); - startActivity(myIntent); - finish(); - return; - } - bottomMenu = new BottomMenu(BaseMainActivity.this).hydrate(currentAccount, binding.bottomNavView); - if (currentAccount.mastodon_account.locked) { - binding.navView.getMenu().findItem(R.id.nav_follow_requests).setVisible(true); - } - if (currentAccount.admin) { - binding.navView.getMenu().findItem(R.id.nav_administration).setVisible(true); - } - if (bottomMenu != null) { - //ManageClick on bottom menu items - if (binding.bottomNavView.findViewById(R.id.nav_home) != null) { - binding.bottomNavView.findViewById(R.id.nav_home).setOnLongClickListener(view -> { - int position = BottomMenu.getPosition(bottomMenu, R.id.nav_home); - if (position >= 0) { - manageFilters(position); - } - return false; - }); - } - if (binding.bottomNavView.findViewById(R.id.nav_local) != null) { - binding.bottomNavView.findViewById(R.id.nav_local).setOnLongClickListener(view -> { - int position = BottomMenu.getPosition(bottomMenu, R.id.nav_local); - if (position >= 0) { - manageFilters(position); - } - return false; - }); - } - if (binding.bottomNavView.findViewById(R.id.nav_public) != null) { - binding.bottomNavView.findViewById(R.id.nav_public).setOnLongClickListener(view -> { - int position = BottomMenu.getPosition(bottomMenu, R.id.nav_public); - if (position >= 0) { - manageFilters(position); - } - return false; - }); - } - binding.bottomNavView.setOnItemSelectedListener(item -> { - int itemId = item.getItemId(); - int position = BottomMenu.getPosition(bottomMenu, itemId); - if (position >= 0) { - if (binding.viewPager.getCurrentItem() == position) { - scrollToTop(); - binding.bottomNavView.removeBadge(itemId); - } else { - binding.viewPager.setCurrentItem(position, false); - } - } - return true; - }); - } - currentInstance = currentAccount.instance; - currentUserID = currentAccount.user_id; - - show_boosts = sharedpreferences.getBoolean(getString(R.string.SET_SHOW_BOOSTS) + currentUserID + currentInstance, true); - show_replies = sharedpreferences.getBoolean(getString(R.string.SET_SHOW_REPLIES) + currentUserID + currentInstance, true); - show_dms = sharedpreferences.getBoolean(getString(R.string.SET_SHOW_DMS) + currentUserID + currentInstance, true); - regex_home = sharedpreferences.getString(getString(R.string.SET_FILTER_REGEX_HOME) + currentUserID + currentInstance, null); - regex_local = sharedpreferences.getString(getString(R.string.SET_FILTER_REGEX_LOCAL) + currentUserID + currentInstance, null); - regex_public = sharedpreferences.getString(getString(R.string.SET_FILTER_REGEX_PUBLIC) + currentUserID + currentInstance, null); - show_art_nsfw = sharedpreferences.getBoolean(getString(R.string.SET_ART_WITH_NSFW) + currentUserID + currentInstance, false); - - binding.profilePicture.setOnClickListener(v -> binding.drawerLayout.openDrawer(GravityCompat.START)); - Helper.loadPP(BaseMainActivity.this, binding.profilePicture, currentAccount); - headerMainBinding.accountAcc.setText(String.format("%s@%s", currentAccount.mastodon_account.username, currentAccount.instance)); - if (currentAccount.mastodon_account.display_name == null || currentAccount.mastodon_account.display_name.isEmpty()) { - currentAccount.mastodon_account.display_name = currentAccount.mastodon_account.acct; - } - if (!isFinishing()) { - headerMainBinding.accountName.setText( - currentAccount.mastodon_account.getSpanDisplayName(BaseMainActivity.this, - new WeakReference<>(headerMainBinding.accountName)), - TextView.BufferType.SPANNABLE); - } - float scale = sharedpreferences.getFloat(getString(R.string.SET_FONT_SCALE), 1.1f); - headerMainBinding.accountName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18 * 1.1f / scale); - headerMainBinding.accountAcc.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18 * 1.1f / scale); - Helper.loadPP(BaseMainActivity.this, headerMainBinding.accountProfilePicture, currentAccount, false); - MastodonHelper.loadProfileMediaMastodon(BaseMainActivity.this, headerMainBinding.backgroundImage, currentAccount.mastodon_account, MastodonHelper.MediaAccountType.HEADER); - headerMainBinding.backgroundImage.setAlpha(0.5f); - /* - * Some general data are loaded when the app starts such; - * - Pinned timelines (in app feature) - * - Instance info (for limits) - * - Emoji for picker - * - Filters for timelines - - */ - - //Update pinned timelines - new ViewModelProvider(BaseMainActivity.this).get(TopBarVM.class).getDBPinned() - .observe(this, pinned -> { - this.pinned = pinned; - //Initialize the slug of the first fragment - //First it's taken from db (last stored values) - PinnedTimelineHelper.redrawTopBarPinned(BaseMainActivity.this, binding, pinned, bottomMenu, null); - //Fetch remote lists for the authenticated account and update them - new ViewModelProvider(BaseMainActivity.this).get(TimelinesVM.class).getLists(currentInstance, currentToken) - .observe(this, mastodonLists -> - PinnedTimelineHelper.redrawTopBarPinned(BaseMainActivity.this, binding, pinned, bottomMenu, mastodonLists) - ); - }); - - //Update emoji in db for the current instance - new ViewModelProvider(BaseMainActivity.this).get(InstancesVM.class).getEmoji(currentInstance); - //Retrieve instance info - new ViewModelProvider(BaseMainActivity.this).get(InstancesVM.class).getInstance(currentInstance) - .observe(BaseMainActivity.this, instance -> { - instanceInfo = instance.info; - SharedPreferences.Editor editor = sharedpreferences.edit(); - editor.putString(getString(R.string.INSTANCE_INFO) + MainActivity.currentInstance, Instance.serialize(instanceInfo)); - editor.apply(); - }); - //Retrieve filters - new ViewModelProvider(BaseMainActivity.this).get(FiltersVM.class).getFilters(currentInstance, currentToken) - .observe(BaseMainActivity.this, filters -> mainFilters = filters); - new ViewModelProvider(BaseMainActivity.this).get(AccountsVM.class).getConnectedAccount(currentInstance, currentToken) - .observe(BaseMainActivity.this, mastodonAccount -> { - //Initialize static var - if (mastodonAccount != null && currentAccount != null) { - currentAccount.mastodon_account = mastodonAccount; - displayReleaseNotesIfNeeded(BaseMainActivity.this, false); - new Thread(() -> { - try { - //Update account in db - new Account(BaseMainActivity.this).insertOrUpdate(currentAccount); - } catch (DBException e) { - e.printStackTrace(); - } - }).start(); - } - }); - - }; - mainHandler.post(myRunnable); - }).start(); //Toolbar search binding.toolbarSearch.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override diff --git a/app/src/main/java/app/fedilab/android/mastodon/client/entities/app/Account.java b/app/src/main/java/app/fedilab/android/mastodon/client/entities/app/Account.java index f6387b263..085125edf 100644 --- a/app/src/main/java/app/fedilab/android/mastodon/client/entities/app/Account.java +++ b/app/src/main/java/app/fedilab/android/mastodon/client/entities/app/Account.java @@ -263,8 +263,9 @@ public class Account extends BaseAccount implements Serializable { if (token.getRefresh_token() != null) { values.put(Sqlite.COL_REFRESH_TOKEN, token.getRefresh_token()); } - if (token.getAccess_token() != null) + if (token.getAccess_token() != null) { values.put(Sqlite.COL_TOKEN, token.getAccess_token()); + } SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(context); String userId = sharedpreferences.getString(Helper.PREF_USER_ID, null); String instance = HelperInstance.getLiveInstance(context); diff --git a/app/src/main/java/app/fedilab/android/peertube/activities/LoginActivity.java b/app/src/main/java/app/fedilab/android/peertube/activities/LoginActivity.java index 62506e1ed..9ccf5608e 100644 --- a/app/src/main/java/app/fedilab/android/peertube/activities/LoginActivity.java +++ b/app/src/main/java/app/fedilab/android/peertube/activities/LoginActivity.java @@ -182,8 +182,9 @@ public class LoginActivity extends BaseBarActivity { oauthParams.setPassword(binding.loginPasswd.getText().toString()); } try { + Log.v(TAG, "token: GET"); Token token = new RetrofitPeertubeAPI(LoginActivity.this, finalInstance, null).manageToken(oauthParams); - Log.v(TAG, "token: " + token); + Log.v(TAG, ">token: " + token); proceedLogin(token, finalInstance); } catch (final Exception e) { oauthParams.setUsername(binding.loginUid.getText().toString().toLowerCase().trim()); diff --git a/app/src/main/java/app/fedilab/android/peertube/activities/PeertubeActivity.java b/app/src/main/java/app/fedilab/android/peertube/activities/PeertubeActivity.java index 05cbbf326..f236ef904 100644 --- a/app/src/main/java/app/fedilab/android/peertube/activities/PeertubeActivity.java +++ b/app/src/main/java/app/fedilab/android/peertube/activities/PeertubeActivity.java @@ -63,6 +63,7 @@ import android.view.WindowManager; import android.view.animation.Animation; import android.view.animation.TranslateAnimation; import android.view.inputmethod.InputMethodManager; +import android.webkit.WebView; import android.widget.EditText; import android.widget.ImageButton; import android.widget.ImageView; @@ -155,7 +156,6 @@ import app.fedilab.android.peertube.viewmodel.PlaylistsVM; import app.fedilab.android.peertube.viewmodel.PostActionsVM; import app.fedilab.android.peertube.viewmodel.SearchVM; import app.fedilab.android.peertube.viewmodel.TimelineVM; -import app.fedilab.android.peertube.webview.CustomWebview; import app.fedilab.android.peertube.webview.MastalabWebChromeClient; import app.fedilab.android.peertube.webview.MastalabWebViewClient; import es.dmoral.toasty.Toasty; @@ -209,6 +209,8 @@ public class PeertubeActivity extends BasePeertubeActivity implements CommentLis @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + binding = ActivityPeertubeBinding.inflate(getLayoutInflater()); + setContentView(binding.getRoot()); videoOrientationType = videoOrientation.LANDSCAPE; max_id = "0"; SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE); @@ -306,7 +308,7 @@ public class PeertubeActivity extends BasePeertubeActivity implements CommentLis binding.webviewVideo.setVisibility(View.VISIBLE); binding.mediaVideo.setVisibility(View.GONE); binding.doubleTapPlayerView.setVisibility(View.GONE); - CustomWebview webview_video = Helper.initializeWebview(PeertubeActivity.this, R.id.webview_video, null); + WebView webview_video = Helper.initializeWebview(PeertubeActivity.this, R.id.webview_video, null); MastalabWebChromeClient mastalabWebChromeClient = new MastalabWebChromeClient(PeertubeActivity.this, webview_video, binding.mainMediaFrame, binding.videoLayout); mastalabWebChromeClient.setOnToggledFullscreen(fullscreen -> { @@ -1940,35 +1942,45 @@ public class PeertubeActivity extends BasePeertubeActivity implements CommentLis private void initControllerButtons() { PlayerControlView controlView = binding.doubleTapPlayerView.findViewById(R.id.exo_controller); + if (controlView == null) { + return; + } fullScreenIcon = controlView.findViewById(R.id.exo_fullscreen_icon); View fullScreenButton = controlView.findViewById(R.id.exo_fullscreen_button); - fullScreenButton.setOnClickListener(v -> { - if (!fullScreenMode) { - openFullscreenDialog(); - } else { - closeFullscreenDialog(); - setRequestedOrientationCustom(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); - } - }); - + if (fullScreenButton != null) { + fullScreenButton.setOnClickListener(v -> { + if (!fullScreenMode) { + openFullscreenDialog(); + } else { + closeFullscreenDialog(); + setRequestedOrientationCustom(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); + } + }); + } ImageButton playButton = controlView.findViewById(R.id.exo_play); - playButton.setOnClickListener(v -> { - if (autoFullscreen && !fullScreenMode) { - openFullscreenDialog(); - } - player.setPlayWhenReady(true); - }); + if (playButton != null) { + playButton.setOnClickListener(v -> { + if (autoFullscreen && !fullScreenMode) { + openFullscreenDialog(); + } + player.setPlayWhenReady(true); + }); + } View exo_next = controlView.findViewById(R.id.exo_next); - exo_next.setOnClickListener(v -> playNextVideo()); + if (exo_next != null) { + exo_next.setOnClickListener(v -> playNextVideo()); + } View exoSettings = controlView.findViewById(R.id.exo_settings); - exoSettings.setOnClickListener(v -> { - if (binding.videoParams.getVisibility() == View.VISIBLE) { - closeMainMenuOptions(); - } else { - openMainMenuOptions(); - } - }); + if (exoSettings != null) { + exoSettings.setOnClickListener(v -> { + if (binding.videoParams.getVisibility() == View.VISIBLE) { + closeMainMenuOptions(); + } else { + openMainMenuOptions(); + } + }); + } } diff --git a/app/src/main/java/app/fedilab/android/peertube/activities/WebviewActivity.java b/app/src/main/java/app/fedilab/android/peertube/activities/WebviewActivity.java index 2c002801c..c008e33e2 100644 --- a/app/src/main/java/app/fedilab/android/peertube/activities/WebviewActivity.java +++ b/app/src/main/java/app/fedilab/android/peertube/activities/WebviewActivity.java @@ -26,6 +26,7 @@ import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; +import android.webkit.WebView; import android.widget.FrameLayout; import android.widget.Toast; @@ -37,7 +38,6 @@ import org.jetbrains.annotations.NotNull; import app.fedilab.android.R; import app.fedilab.android.mastodon.activities.BaseBarActivity; import app.fedilab.android.peertube.helper.Helper; -import app.fedilab.android.peertube.webview.CustomWebview; import app.fedilab.android.peertube.webview.MastalabWebChromeClient; import app.fedilab.android.peertube.webview.MastalabWebViewClient; import es.dmoral.toasty.Toasty; @@ -47,7 +47,7 @@ public class WebviewActivity extends BaseBarActivity { private String url; private boolean peertubeLink; - private CustomWebview webView; + private WebView webView; @SuppressLint("SetJavaScriptEnabled") 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 82658f4c0..18a6edf6b 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 @@ -177,19 +177,19 @@ public class RetrofitPeertubeAPI { account.refresh_token = refresh_token; account.instance = instance; account.api = Account.API.PEERTUBE; + account.software = Account.API.PEERTUBE.name(); account.peertube_account = peertubeAccount; - SQLiteDatabase db = Sqlite.getInstance(activity.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); - boolean userExists = false; + account.user_id = peertubeAccount.getId(); + SharedPreferences.Editor editor = sharedpreferences.edit(); + editor.putString(PREF_USER_ID, account.user_id); + editor.putString(PREF_INSTANCE, host); + editor.putString(PREF_USER_TOKEN, token); + editor.apply(); try { - SharedPreferences.Editor editor = sharedpreferences.edit(); - editor.putString(PREF_USER_ID, account.user_id); - editor.putString(PREF_INSTANCE, host); - editor.apply(); new Account(activity).insertOrUpdate(account); } catch (DBException e) { e.printStackTrace(); } - Handler mainHandler = new Handler(Looper.getMainLooper()); Runnable myRunnable = () -> { Intent mainActivity = new Intent(activity, PeertubeMainActivity.class); @@ -257,8 +257,6 @@ public class RetrofitPeertubeAPI { SharedPreferences sharedpreferences = _context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedpreferences.edit(); editor.putString(PREF_USER_TOKEN, tokenReply.getAccess_token()); - editor.putString(Helper.PREF_SOFTWARE, null); - editor.putString(Helper.PREF_REMOTE_INSTANCE, null); editor.apply(); SQLiteDatabase db = Sqlite.getInstance(_context.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); new Account(_context).updatePeertubeToken(tokenReply); diff --git a/app/src/main/java/app/fedilab/android/peertube/helper/Helper.java b/app/src/main/java/app/fedilab/android/peertube/helper/Helper.java index 365017f1f..9c607af2d 100644 --- a/app/src/main/java/app/fedilab/android/peertube/helper/Helper.java +++ b/app/src/main/java/app/fedilab/android/peertube/helper/Helper.java @@ -46,6 +46,7 @@ import android.webkit.CookieManager; import android.webkit.URLUtil; import android.webkit.WebChromeClient; import android.webkit.WebSettings; +import android.webkit.WebView; import android.widget.ImageView; import android.widget.Toast; @@ -83,7 +84,6 @@ import app.fedilab.android.peertube.client.data.ChannelData; import app.fedilab.android.peertube.client.data.VideoData; import app.fedilab.android.peertube.client.entities.File; import app.fedilab.android.peertube.client.entities.PeertubeInformation; -import app.fedilab.android.peertube.webview.CustomWebview; import app.fedilab.android.peertube.webview.ProxyHelper; import es.dmoral.toasty.Toasty; import jp.wasabeef.glide.transformations.BlurTransformation; @@ -447,9 +447,9 @@ public class Helper { * @return CustomWebview */ @SuppressLint("SetJavaScriptEnabled") - public static CustomWebview initializeWebview(Activity activity, int webviewId, View rootView) { + public static WebView initializeWebview(Activity activity, int webviewId, View rootView) { - CustomWebview webView; + WebView webView; if (rootView == null) { webView = activity.findViewById(webviewId); } else { @@ -677,9 +677,9 @@ public class Helper { } AccountData.PeertubeAccount account = video.getAccount(); ChannelData.Channel channel = video.getChannel(); - if (account != null) { + if (account != null && account.getUserId() != null && account.getHost() != null) { return account.getUserId().compareTo(userId) == 0 && account.getHost().compareTo(instance) == 0; - } else if (channel != null) { + } else if (channel != null && channel.getOwnerAccount() != null && channel.getOwnerAccount().getUserId() != null && channel.getOwnerAccount().getHost() != null) { return channel.getOwnerAccount().getUserId().compareTo(userId) == 0 && channel.getHost().compareTo(instance) == 0; } else { return false; diff --git a/app/src/main/java/app/fedilab/android/peertube/webview/CustomWebview.java b/app/src/main/java/app/fedilab/android/peertube/webview/CustomWebview.java deleted file mode 100644 index 47df4bb01..000000000 --- a/app/src/main/java/app/fedilab/android/peertube/webview/CustomWebview.java +++ /dev/null @@ -1,53 +0,0 @@ -package app.fedilab.android.peertube.webview; -/* 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 . */ - -import android.annotation.TargetApi; -import android.content.Context; -import android.content.res.Configuration; -import android.os.Build; -import android.util.AttributeSet; -import android.webkit.WebView; - - -public class CustomWebview extends WebView { - - - public CustomWebview(Context context) { - super(getFixedContext(context)); - } - - public CustomWebview(Context context, AttributeSet attrs) { - super(getFixedContext(context), attrs); - } - - public CustomWebview(Context context, AttributeSet attrs, int defStyleAttr) { - super(getFixedContext(context), attrs, defStyleAttr); - } - - @TargetApi(Build.VERSION_CODES.LOLLIPOP) - public CustomWebview(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { - super(getFixedContext(context), attrs, defStyleAttr, defStyleRes); - } - - @SuppressWarnings("deprecation") - public CustomWebview(Context context, AttributeSet attrs, int defStyleAttr, boolean privateBrowsing) { - super(getFixedContext(context), attrs, defStyleAttr, privateBrowsing); - } - - public static Context getFixedContext(Context context) { - return context.createConfigurationContext(new Configuration()); - } -} diff --git a/app/src/main/java/app/fedilab/android/peertube/webview/MastalabWebChromeClient.java b/app/src/main/java/app/fedilab/android/peertube/webview/MastalabWebChromeClient.java index 52dbfb155..8dfee4844 100644 --- a/app/src/main/java/app/fedilab/android/peertube/webview/MastalabWebChromeClient.java +++ b/app/src/main/java/app/fedilab/android/peertube/webview/MastalabWebChromeClient.java @@ -37,7 +37,7 @@ import app.fedilab.android.R; public class MastalabWebChromeClient extends WebChromeClient implements MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener { - private final CustomWebview webView; + private final WebView webView; private final View activityNonVideoView; private final ViewGroup activityVideoView; private final ProgressBar pbar; @@ -48,7 +48,7 @@ public class MastalabWebChromeClient extends WebChromeClient implements MediaPla private boolean isVideoFullscreen; - public MastalabWebChromeClient(Activity activity, CustomWebview webView, FrameLayout activityNonVideoView, ViewGroup activityVideoView) { + public MastalabWebChromeClient(Activity activity, WebView webView, FrameLayout activityNonVideoView, ViewGroup activityVideoView) { this.activity = activity; this.isVideoFullscreen = false; this.webView = webView; diff --git a/app/src/main/java/app/fedilab/android/peertube/webview/ProxyHelper.java b/app/src/main/java/app/fedilab/android/peertube/webview/ProxyHelper.java index d065696a2..4cd4a2629 100644 --- a/app/src/main/java/app/fedilab/android/peertube/webview/ProxyHelper.java +++ b/app/src/main/java/app/fedilab/android/peertube/webview/ProxyHelper.java @@ -19,6 +19,7 @@ import android.content.Context; import android.content.Intent; import android.net.Proxy; import android.util.ArrayMap; +import android.webkit.WebView; import java.lang.reflect.Constructor; import java.lang.reflect.Field; @@ -28,14 +29,14 @@ import java.lang.reflect.Method; public class ProxyHelper { - public static void setProxy(Context context, CustomWebview webview, String host, int port, String applicationClassName) { + public static void setProxy(Context context, WebView webview, String host, int port, String applicationClassName) { setProxyKKPlus(context, webview, host, port, applicationClassName); } @SuppressWarnings("all") - private static boolean setProxyICS(CustomWebview webview, String host, int port) { + private static boolean setProxyICS(WebView webview, String host, int port) { try { Class jwcjb = Class.forName("android.webkit.JWebCoreJavaBridge"); Class params[] = new Class[1]; @@ -72,7 +73,7 @@ public class ProxyHelper { * Set Proxy for Android 4.1 - 4.3. */ @SuppressWarnings("all") - private static boolean setProxyJB(CustomWebview webview, String host, int port) { + private static boolean setProxyJB(WebView webview, String host, int port) { try { Class wvcClass = Class.forName("android.webkit.WebViewClassic"); @@ -115,7 +116,7 @@ public class ProxyHelper { // from https://stackoverflow.com/questions/19979578/android-webview-set-proxy-programatically-kitkat @SuppressLint("NewApi") @SuppressWarnings("all") - private static void setProxyKKPlus(Context appContext, CustomWebview webView, String host, int port, String applicationClassName) { + private static void setProxyKKPlus(Context appContext, WebView webView, String host, int port, String applicationClassName) { System.setProperty("http.proxyHost", host); System.setProperty("http.proxyPort", port + ""); diff --git a/app/src/main/res/layouts/peertube/layout/activity_peertube.xml b/app/src/main/res/layouts/peertube/layout/activity_peertube.xml index c771dd77e..b4437aae0 100644 --- a/app/src/main/res/layouts/peertube/layout/activity_peertube.xml +++ b/app/src/main/res/layouts/peertube/layout/activity_peertube.xml @@ -80,7 +80,7 @@ android:id="@+id/min_controller" layout="@layout/min_controller" /> - -