|
@@ -20,24 +20,295 @@
|
|
|
|
|
|
package com.nextcloud.talk.controllers;
|
|
|
|
|
|
+import android.content.Intent;
|
|
|
+import android.net.Uri;
|
|
|
import android.support.annotation.NonNull;
|
|
|
+import android.text.TextUtils;
|
|
|
import android.view.LayoutInflater;
|
|
|
import android.view.View;
|
|
|
import android.view.ViewGroup;
|
|
|
+import android.widget.TextView;
|
|
|
|
|
|
+import com.bluelinelabs.conductor.RouterTransaction;
|
|
|
+import com.bluelinelabs.conductor.changehandler.VerticalChangeHandler;
|
|
|
+import com.bumptech.glide.load.model.GlideUrl;
|
|
|
+import com.bumptech.glide.load.model.LazyHeaders;
|
|
|
+import com.nextcloud.talk.BuildConfig;
|
|
|
import com.nextcloud.talk.R;
|
|
|
+import com.nextcloud.talk.api.helpers.api.ApiHelper;
|
|
|
import com.nextcloud.talk.application.NextcloudTalkApplication;
|
|
|
import com.nextcloud.talk.controllers.base.BaseController;
|
|
|
+import com.nextcloud.talk.persistence.entities.UserEntity;
|
|
|
+import com.nextcloud.talk.utils.ColorUtils;
|
|
|
+import com.nextcloud.talk.utils.database.user.UserUtils;
|
|
|
+import com.nextcloud.talk.utils.glide.GlideApp;
|
|
|
+import com.nextcloud.talk.utils.preferences.AppPreferences;
|
|
|
+import com.nextcloud.talk.utils.preferences.MagicUserInputModule;
|
|
|
+import com.yarolegovich.mp.MaterialChoicePreference;
|
|
|
+import com.yarolegovich.mp.MaterialEditTextPreference;
|
|
|
+import com.yarolegovich.mp.MaterialPreferenceScreen;
|
|
|
+import com.yarolegovich.mp.MaterialStandardPreference;
|
|
|
+
|
|
|
+import net.orange_box.storebox.listeners.OnPreferenceValueChangedListener;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+import javax.inject.Inject;
|
|
|
|
|
|
import autodagger.AutoInjector;
|
|
|
+import butterknife.BindView;
|
|
|
+import cn.carbs.android.avatarimageview.library.AvatarImageView;
|
|
|
|
|
|
@AutoInjector(NextcloudTalkApplication.class)
|
|
|
public class SettingsController extends BaseController {
|
|
|
|
|
|
public static final String TAG = "SettingsController";
|
|
|
|
|
|
+ @BindView(R.id.settings_screen)
|
|
|
+ MaterialPreferenceScreen settingsScreen;
|
|
|
+
|
|
|
+ @BindView(R.id.settings_proxy_choice)
|
|
|
+ MaterialChoicePreference proxyChoice;
|
|
|
+
|
|
|
+ @BindView(R.id.settings_proxy_port_edit)
|
|
|
+ MaterialEditTextPreference proxyPortEditText;
|
|
|
+
|
|
|
+ @BindView(R.id.settings_licence)
|
|
|
+ MaterialStandardPreference licenceButton;
|
|
|
+
|
|
|
+ @BindView(R.id.settings_privacy)
|
|
|
+ MaterialStandardPreference privacyButton;
|
|
|
+
|
|
|
+ @BindView(R.id.settings_source_code)
|
|
|
+ MaterialStandardPreference sourceCodeButton;
|
|
|
+
|
|
|
+ @BindView(R.id.settings_version)
|
|
|
+ MaterialStandardPreference versionInfo;
|
|
|
+
|
|
|
+ @BindView(R.id.avatar_image)
|
|
|
+ AvatarImageView avatarImageView;
|
|
|
+
|
|
|
+ @BindView(R.id.avatar_image_invisible)
|
|
|
+ AvatarImageView avatarImageViewInvisible;
|
|
|
+
|
|
|
+ @BindView(R.id.display_name_text)
|
|
|
+ TextView displayName;
|
|
|
+
|
|
|
+ @BindView(R.id.settings_remove_account)
|
|
|
+ MaterialStandardPreference removeAccountButton;
|
|
|
+
|
|
|
+ @BindView(R.id.settings_switch)
|
|
|
+ MaterialStandardPreference switchAccountButton;
|
|
|
+
|
|
|
+ @BindView(R.id.settings_reauthorize)
|
|
|
+ MaterialStandardPreference reauthorizeButton;
|
|
|
+
|
|
|
+ @BindView(R.id.settings_add_account)
|
|
|
+ MaterialStandardPreference addAccountButton;
|
|
|
+
|
|
|
+ @Inject
|
|
|
+ AppPreferences appPreferences;
|
|
|
+
|
|
|
+ @Inject
|
|
|
+ UserUtils userUtils;
|
|
|
+
|
|
|
+ private OnPreferenceValueChangedListener<String> proxyTypeChangeListener;
|
|
|
+ private OnPreferenceValueChangedListener<Boolean> proxyCredentialsChangeListener;
|
|
|
+
|
|
|
@Override
|
|
|
protected View inflateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container) {
|
|
|
return inflater.inflate(R.layout.controller_settings, container, false);
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onViewBound(@NonNull View view) {
|
|
|
+ super.onViewBound(view);
|
|
|
+ NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onAttach(@NonNull View view) {
|
|
|
+ super.onAttach(view);
|
|
|
+
|
|
|
+ if ("No proxy".equals(appPreferences.getProxyType()) || appPreferences.getProxyType() == null) {
|
|
|
+ hideProxySettings();
|
|
|
+ } else {
|
|
|
+ showProxySettings();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (appPreferences.getProxyCredentials()) {
|
|
|
+ showProxyCredentials();
|
|
|
+ } else {
|
|
|
+ hideProxyCredentials();
|
|
|
+ }
|
|
|
+
|
|
|
+ appPreferences.registerProxyTypeListener(proxyTypeChangeListener = new ProxyTypeChangeListener());
|
|
|
+ appPreferences.registerProxyCredentialsListener(proxyCredentialsChangeListener = new
|
|
|
+ ProxyCredentialsChangeListener());
|
|
|
+
|
|
|
+ List<String> listWithIntFields = new ArrayList<>();
|
|
|
+ listWithIntFields.add("proxy_port");
|
|
|
+
|
|
|
+ settingsScreen.setUserInputModule(new MagicUserInputModule(getActivity(), listWithIntFields));
|
|
|
+ settingsScreen.setVisibilityController(R.id.settings_proxy_use_credentials,
|
|
|
+ Arrays.asList(R.id.settings_proxy_username_edit, R.id.settings_proxy_password_edit),
|
|
|
+ true);
|
|
|
+
|
|
|
+ if (!TextUtils.isEmpty(getResources().getString(R.string.nc_gpl3_url))) {
|
|
|
+ licenceButton.setOnClickListener(view1 -> {
|
|
|
+ Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(getResources().
|
|
|
+ getString(R.string.nc_gpl3_url)));
|
|
|
+ startActivity(browserIntent);
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ licenceButton.setVisibility(View.GONE);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!TextUtils.isEmpty(getResources().getString(R.string.nc_privacy_url))) {
|
|
|
+ privacyButton.setOnClickListener(view12 -> {
|
|
|
+ Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(getResources().
|
|
|
+ getString(R.string.nc_privacy_url)));
|
|
|
+ startActivity(browserIntent);
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ privacyButton.setVisibility(View.GONE);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!TextUtils.isEmpty(getResources().getString(R.string.nc_source_code_url))) {
|
|
|
+ sourceCodeButton.setOnClickListener(view13 -> {
|
|
|
+ Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(getResources().
|
|
|
+ getString(R.string.nc_source_code_url)));
|
|
|
+ startActivity(browserIntent);
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ sourceCodeButton.setVisibility(View.GONE);
|
|
|
+ }
|
|
|
+
|
|
|
+ versionInfo.setSummary("v" + BuildConfig.VERSION_NAME);
|
|
|
+
|
|
|
+ UserEntity userEntity = userUtils.getCurrentUser();
|
|
|
+ if (userEntity != null) {
|
|
|
+ // Awful hack
|
|
|
+ avatarImageViewInvisible.setTextAndColorSeed(String.valueOf(userEntity.getDisplayName().
|
|
|
+ toUpperCase().charAt(0)), ColorUtils.colorSeed);
|
|
|
+
|
|
|
+ GlideUrl glideUrl = new GlideUrl(ApiHelper.getUrlForAvatarWithName(userEntity.getBaseUrl(),
|
|
|
+ userEntity.getUsername()), new LazyHeaders.Builder()
|
|
|
+ .setHeader("Accept", "image/*")
|
|
|
+ .setHeader("User-Agent", ApiHelper.getUserAgent())
|
|
|
+ .build());
|
|
|
+
|
|
|
+ GlideApp.with(NextcloudTalkApplication.getSharedApplication().getApplicationContext())
|
|
|
+ .load(glideUrl)
|
|
|
+ .circleCrop()
|
|
|
+ .centerInside()
|
|
|
+ .into(avatarImageView)
|
|
|
+ .onLoadFailed(avatarImageViewInvisible.getDrawable());
|
|
|
+
|
|
|
+ displayName.setText(userEntity.getDisplayName());
|
|
|
+ }
|
|
|
+
|
|
|
+ if (userUtils.getUsers().size() <= 1) {
|
|
|
+ switchAccountButton.setVisibility(View.GONE);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ reauthorizeButton.setOnClickListener(new View.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(View view) {
|
|
|
+ getParentController().getRouter().pushController(RouterTransaction.with(
|
|
|
+ new WebViewLoginController(userEntity.getBaseUrl(),
|
|
|
+ true)).pushChangeHandler(new VerticalChangeHandler())
|
|
|
+ .popChangeHandler(new VerticalChangeHandler()));
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ addAccountButton.setOnClickListener(new View.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(View view) {
|
|
|
+ getParentController().getRouter().pushController(RouterTransaction.with(new
|
|
|
+ ServerSelectionController()).pushChangeHandler(new VerticalChangeHandler())
|
|
|
+ .popChangeHandler(new VerticalChangeHandler()));
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onDestroy() {
|
|
|
+ appPreferences.unregisterProxyTypeListener(proxyTypeChangeListener);
|
|
|
+ appPreferences.unregisterProxyCredentialsListener(proxyCredentialsChangeListener);
|
|
|
+ super.onDestroy();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private void hideProxySettings() {
|
|
|
+ appPreferences.removeProxyHost();
|
|
|
+ appPreferences.removeProxyPort();
|
|
|
+ appPreferences.removeProxyCredentials();
|
|
|
+ appPreferences.removeProxyUsername();
|
|
|
+ appPreferences.removeProxyPassword();
|
|
|
+ settingsScreen.findViewById(R.id.settings_proxy_host_edit).setVisibility(View.GONE);
|
|
|
+ settingsScreen.findViewById(R.id.settings_proxy_port_edit).setVisibility(View.GONE);
|
|
|
+ settingsScreen.findViewById(R.id.settings_proxy_use_credentials).setVisibility(View.GONE);
|
|
|
+ settingsScreen.findViewById(R.id.settings_proxy_username_edit).setVisibility(View.GONE);
|
|
|
+ settingsScreen.findViewById(R.id.settings_proxy_password_edit).setVisibility(View.GONE);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void showProxySettings() {
|
|
|
+ settingsScreen.findViewById(R.id.settings_proxy_host_edit).setVisibility(View.VISIBLE);
|
|
|
+ settingsScreen.findViewById(R.id.settings_proxy_port_edit).setVisibility(View.VISIBLE);
|
|
|
+ settingsScreen.findViewById(R.id.settings_proxy_use_credentials).setVisibility(View.VISIBLE);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void showProxyCredentials() {
|
|
|
+ settingsScreen.findViewById(R.id.settings_proxy_username_edit).setVisibility(View.VISIBLE);
|
|
|
+ settingsScreen.findViewById(R.id.settings_proxy_password_edit).setVisibility(View.VISIBLE);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void hideProxyCredentials() {
|
|
|
+ appPreferences.removeProxyUsername();
|
|
|
+ appPreferences.removeProxyPassword();
|
|
|
+ settingsScreen.findViewById(R.id.settings_proxy_username_edit).setVisibility(View.GONE);
|
|
|
+ settingsScreen.findViewById(R.id.settings_proxy_password_edit).setVisibility(View.GONE);
|
|
|
+ }
|
|
|
+
|
|
|
+ private class ProxyCredentialsChangeListener implements OnPreferenceValueChangedListener<Boolean> {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onChanged(Boolean newValue) {
|
|
|
+ if (newValue) {
|
|
|
+ showProxyCredentials();
|
|
|
+ } else {
|
|
|
+ hideProxyCredentials();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private class ProxyTypeChangeListener implements OnPreferenceValueChangedListener<String> {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onChanged(String newValue) {
|
|
|
+ if ("No proxy".equals(newValue)) {
|
|
|
+ hideProxySettings();
|
|
|
+ } else {
|
|
|
+ switch (newValue) {
|
|
|
+ case "HTTP":
|
|
|
+ proxyPortEditText.setValue("3128");
|
|
|
+ break;
|
|
|
+ case "DIRECT":
|
|
|
+ proxyPortEditText.setValue("8080");
|
|
|
+ break;
|
|
|
+ case "SOCKS":
|
|
|
+ proxyPortEditText.setValue("1080");
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ showProxySettings();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|