/* * Nextcloud Talk application * * @author Mario Danic * Copyright (C) 2017 Mario Danic (mario@lovelyhq.com) * * 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. * * This program 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 this program. If not, see . */ package com.nextcloud.talk.controllers; import android.animation.Animator; import android.animation.AnimatorListenerAdapter; 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.SettingsMessageHolder; 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.MaterialPreferenceCategory; import com.yarolegovich.mp.MaterialPreferenceScreen; import com.yarolegovich.mp.MaterialStandardPreference; import net.orange_box.storebox.listeners.OnPreferenceValueChangedListener; import org.greenrobot.eventbus.EventBus; 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.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; @BindView(R.id.message_view) MaterialPreferenceCategory messageView; @BindView(R.id.message_text) TextView messageText; @Inject EventBus eventBus; @Inject AppPreferences appPreferences; @Inject UserUtils userUtils; private OnPreferenceValueChangedListener proxyTypeChangeListener; private OnPreferenceValueChangedListener 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 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 avatarImageView.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); 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())); } }); if (SettingsMessageHolder.getInstance().getMessageType() != null) { switch (SettingsMessageHolder.getInstance().getMessageType()) { case ACCOUNT_UPDATED_NOT_ADDED: messageText.setText(getResources().getString(R.string.nc_settings_account_updated)); messageView.setVisibility(View.VISIBLE); break; case WRONG_ACCOUNT: messageText.setText(getResources().getString(R.string.nc_settings_wrong_account)); messageView.setVisibility(View.VISIBLE); break; default: messageView.setVisibility(View.GONE); break; } SettingsMessageHolder.getInstance().setMessageType(null); messageView.animate() .translationY(0) .alpha(0.0f) .setDuration(2000) .setStartDelay(5000) .setListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { super.onAnimationEnd(animation); messageView.setVisibility(View.GONE); } }); } else { messageView.setVisibility(View.GONE); } } @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 { @Override public void onChanged(Boolean newValue) { if (newValue) { showProxyCredentials(); } else { hideProxyCredentials(); } } } private class ProxyTypeChangeListener implements OnPreferenceValueChangedListener { @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(); } } } }