Some fixes

This commit is contained in:
Thomas 2020-11-28 10:33:13 +01:00
parent e7f55aace9
commit b0e29c0ba4
4 changed files with 98 additions and 69 deletions

View File

@ -18,6 +18,9 @@
<string name="set_video_in_list">Vidéos dans une liste</string>
<string name="set_video_in_list_description">Change la mise en page pour afficher les vidéos dans une liste</string>
<string name="_retry">Réessayer</string>
<string name="refresh_token_failed">Échec de rafraîchissement du jeton d\'accès</string>
<string name="refresh_token_failed_message">Vous pouvez réessayer de le rafraîchir ou simplement déconnecter le compte</string>
<string name="export_list">Exporter</string>
<string name="import_list">Importer</string>
<string name="originally_published">Date de publication</string>

View File

@ -105,6 +105,9 @@
<item>12 hours</item>
</string-array>
<string name="_retry">Retry</string>
<string name="refresh_token_failed">Token refresh failed!</string>
<string name="refresh_token_failed_message">You can retry this action or simply disconnect the current account</string>
<string name="yes">Yes</string>
<string name="no">No</string>
<string name="cancel">Cancel</string>

View File

@ -24,6 +24,7 @@ import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
@ -194,6 +195,7 @@ public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v(Helper.TAG, "onCreate: " + savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
View view = binding.getRoot();
setContentView(view);
@ -238,7 +240,7 @@ public class MainActivity extends AppCompatActivity {
mostLikedFragment.setArguments(bundle);
overviewFragment = new DisplayOverviewFragment();
Log.v(Helper.TAG, "Helper.isLoggedIn(MainActivity.this): " + Helper.isLoggedIn(MainActivity.this));
if (!Helper.isLoggedIn(MainActivity.this)) {
PagerAdapter mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
binding.viewpager.setAdapter(mPagerAdapter);
@ -278,73 +280,7 @@ public class MainActivity extends AppCompatActivity {
if (Helper.isLoggedIn(MainActivity.this)) {
navView.inflateMenu(R.menu.bottom_nav_menu_connected);
new Thread(() -> {
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String tokenStr = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null);
String instance = Helper.getLiveInstance(MainActivity.this);
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
String instanceShar = sharedpreferences.getString(Helper.PREF_INSTANCE, null);
String userIdShar = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
Account account = new AccountDAO(MainActivity.this, db).getAccountByToken(tokenStr);
if (account == null) {
account = new AccountDAO(MainActivity.this, db).getAccountByIdInstance(userIdShar, instanceShar);
}
if (account != null) {
Account finalAccount = account;
OauthParams oauthParams = new OauthParams();
oauthParams.setGrant_type("refresh_token");
oauthParams.setClient_id(account.getClient_id());
oauthParams.setClient_secret(account.getClient_secret());
oauthParams.setRefresh_token(account.getRefresh_token());
oauthParams.setAccess_token(account.getToken());
try {
Token token = new RetrofitPeertubeAPI(MainActivity.this).manageToken(oauthParams);
if (token == null && Helper.instanceOnline(instance)) {
runOnUiThread(() -> Helper.logoutCurrentUser(MainActivity.this, finalAccount));
return;
} else if (token == null) {
return;
}
runOnUiThread(() -> {
//To avoid a token issue with subscriptions, adding fragment is done when the token is refreshed.
new Handler().post(() -> {
if (Helper.isLoggedIn(MainActivity.this)) {
PagerAdapter mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
binding.viewpager.setAdapter(mPagerAdapter);
}
});
});
userMe = new RetrofitPeertubeAPI(MainActivity.this, instance, token.getAccess_token()).verifyCredentials();
if (userMe != null && userMe.getAccount() != null) {
new AccountDAO(MainActivity.this, db).updateAccount(userMe.getAccount());
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.PREF_KEY_ID, account.getId());
editor.putString(Helper.PREF_KEY_NAME, account.getUsername());
editor.putBoolean(getString(R.string.set_autoplay_choice), userMe.isAutoPlayVideo());
editor.putBoolean(getString(R.string.set_store_in_history), userMe.isVideosHistoryEnabled());
editor.putBoolean(getString(R.string.set_autoplay_next_video_choice), userMe.isAutoPlayNextVideo());
editor.putString(getString(R.string.set_video_sensitive_choice), userMe.getNsfwPolicy());
//Sync languages from server
List<String> videoLanguageServer = userMe.getVideoLanguages();
if (videoLanguageServer != null) {
Set<String> videoLanguageServerSet = new TreeSet<>(videoLanguageServer);
videoLanguageServerSet.addAll(videoLanguageServer);
Set<String> videoLanguageLocal = sharedpreferences.getStringSet(getString(R.string.set_video_language_choice), null);
if (videoLanguageServerSet.size() > 0 && videoLanguageLocal != null) {
videoLanguageServer.addAll(videoLanguageLocal);
}
editor.putStringSet(getString(R.string.set_video_language_choice), videoLanguageServerSet);
editor.apply();
}
}
instanceConfig = new RetrofitPeertubeAPI(MainActivity.this).getConfigInstance();
} catch (Error error) {
runOnUiThread(() -> Helper.logoutCurrentUser(MainActivity.this, finalAccount));
error.printStackTrace();
}
}
}).start();
refreshToken();
} else {
navView.inflateMenu(R.menu.bottom_nav_menu);
@ -380,6 +316,92 @@ public class MainActivity extends AppCompatActivity {
}
}
private void refreshToken() {
new Thread(() -> {
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String tokenStr = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null);
String instance = Helper.getLiveInstance(MainActivity.this);
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
String instanceShar = sharedpreferences.getString(Helper.PREF_INSTANCE, null);
String userIdShar = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
Account account = new AccountDAO(MainActivity.this, db).getAccountByToken(tokenStr);
if (account == null) {
account = new AccountDAO(MainActivity.this, db).getAccountByIdInstance(userIdShar, instanceShar);
}
if (account != null) {
Account finalAccount = account;
OauthParams oauthParams = new OauthParams();
oauthParams.setGrant_type("refresh_token");
oauthParams.setClient_id(account.getClient_id());
oauthParams.setClient_secret(account.getClient_secret());
oauthParams.setRefresh_token(account.getRefresh_token());
oauthParams.setAccess_token(account.getToken());
try {
Token token = new RetrofitPeertubeAPI(MainActivity.this).manageToken(oauthParams);
if (token == null && Helper.instanceOnline(instance)) {
runOnUiThread(() -> {
AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
alt_bld.setTitle(R.string.refresh_token_failed);
alt_bld.setMessage(R.string.refresh_token_failed_message);
alt_bld.setNegativeButton(R.string.action_logout, (dialog, id) -> {
dialog.dismiss();
Helper.logoutCurrentUser(MainActivity.this, finalAccount);
});
alt_bld.setPositiveButton(R.string._retry, (dialog, id) -> {
dialog.dismiss();
refreshToken();
});
AlertDialog alert = alt_bld.create();
alert.show();
});
return;
} else if (token == null) {
return;
}
runOnUiThread(() -> {
//To avoid a token issue with subscriptions, adding fragment is done when the token is refreshed.
new Handler().post(() -> {
if (Helper.isLoggedIn(MainActivity.this)) {
PagerAdapter mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
binding.viewpager.setAdapter(mPagerAdapter);
}
});
});
userMe = new RetrofitPeertubeAPI(MainActivity.this, instance, token.getAccess_token()).verifyCredentials();
if (userMe != null && userMe.getAccount() != null) {
new AccountDAO(MainActivity.this, db).updateAccount(userMe.getAccount());
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.PREF_KEY_ID, account.getId());
editor.putString(Helper.PREF_KEY_NAME, account.getUsername());
editor.putBoolean(getString(R.string.set_autoplay_choice), userMe.isAutoPlayVideo());
editor.putBoolean(getString(R.string.set_store_in_history), userMe.isVideosHistoryEnabled());
editor.putBoolean(getString(R.string.set_autoplay_next_video_choice), userMe.isAutoPlayNextVideo());
editor.putString(getString(R.string.set_video_sensitive_choice), userMe.getNsfwPolicy());
//Sync languages from server
List<String> videoLanguageServer = userMe.getVideoLanguages();
if (videoLanguageServer != null) {
Set<String> videoLanguageServerSet = new TreeSet<>(videoLanguageServer);
videoLanguageServerSet.addAll(videoLanguageServer);
Set<String> videoLanguageLocal = sharedpreferences.getStringSet(getString(R.string.set_video_language_choice), null);
if (videoLanguageServerSet.size() > 0 && videoLanguageLocal != null) {
videoLanguageServer.addAll(videoLanguageLocal);
}
editor.putStringSet(getString(R.string.set_video_language_choice), videoLanguageServerSet);
editor.apply();
}
}
instanceConfig = new RetrofitPeertubeAPI(MainActivity.this).getConfigInstance();
} catch (Error error) {
runOnUiThread(() -> Helper.logoutCurrentUser(MainActivity.this, finalAccount));
error.printStackTrace();
}
}
}).start();
}
@Override
public boolean onCreateOptionsMenu(@NotNull Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
@ -610,6 +632,7 @@ public class MainActivity extends AppCompatActivity {
@Override
protected void onNewIntent(Intent intent) {
Log.v(Helper.TAG, "onNewIntent: " + intent);
super.onNewIntent(intent);
if (intent == null)
return;

View File

@ -1519,7 +1519,7 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd
}
break;
case AUTONEXT:
binding.subMenuTitle.setText(R.string.set_autoplay);
binding.subMenuTitle.setText(R.string.set_autoplay_next_video_settings);
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
boolean autoplayNextVideo = sharedpreferences.getBoolean(getString(R.string.set_autoplay_next_video_choice), true);
items.add(new MenuItemView(0, getString(R.string.no), !autoplayNextVideo));