SettingsController.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * Nextcloud Talk application
  3. *
  4. * @author Mario Danic
  5. * Copyright (C) 2017 Mario Danic (mario@lovelyhq.com)
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. package com.nextcloud.talk.controllers;
  21. import android.animation.Animator;
  22. import android.animation.AnimatorListenerAdapter;
  23. import android.content.Intent;
  24. import android.net.Uri;
  25. import android.support.annotation.NonNull;
  26. import android.text.TextUtils;
  27. import android.view.LayoutInflater;
  28. import android.view.View;
  29. import android.view.ViewGroup;
  30. import android.widget.TextView;
  31. import com.bluelinelabs.conductor.RouterTransaction;
  32. import com.bluelinelabs.conductor.changehandler.VerticalChangeHandler;
  33. import com.bumptech.glide.load.model.GlideUrl;
  34. import com.bumptech.glide.load.model.LazyHeaders;
  35. import com.nextcloud.talk.BuildConfig;
  36. import com.nextcloud.talk.R;
  37. import com.nextcloud.talk.api.helpers.api.ApiHelper;
  38. import com.nextcloud.talk.application.NextcloudTalkApplication;
  39. import com.nextcloud.talk.controllers.base.BaseController;
  40. import com.nextcloud.talk.persistence.entities.UserEntity;
  41. import com.nextcloud.talk.utils.ColorUtils;
  42. import com.nextcloud.talk.utils.SettingsMessageHolder;
  43. import com.nextcloud.talk.utils.database.user.UserUtils;
  44. import com.nextcloud.talk.utils.glide.GlideApp;
  45. import com.nextcloud.talk.utils.preferences.AppPreferences;
  46. import com.nextcloud.talk.utils.preferences.MagicUserInputModule;
  47. import com.yarolegovich.mp.MaterialChoicePreference;
  48. import com.yarolegovich.mp.MaterialEditTextPreference;
  49. import com.yarolegovich.mp.MaterialPreferenceCategory;
  50. import com.yarolegovich.mp.MaterialPreferenceScreen;
  51. import com.yarolegovich.mp.MaterialStandardPreference;
  52. import net.orange_box.storebox.listeners.OnPreferenceValueChangedListener;
  53. import org.greenrobot.eventbus.EventBus;
  54. import java.util.ArrayList;
  55. import java.util.Arrays;
  56. import java.util.List;
  57. import javax.inject.Inject;
  58. import autodagger.AutoInjector;
  59. import butterknife.BindView;
  60. import cn.carbs.android.avatarimageview.library.AvatarImageView;
  61. @AutoInjector(NextcloudTalkApplication.class)
  62. public class SettingsController extends BaseController {
  63. public static final String TAG = "SettingsController";
  64. @BindView(R.id.settings_screen)
  65. MaterialPreferenceScreen settingsScreen;
  66. @BindView(R.id.settings_proxy_choice)
  67. MaterialChoicePreference proxyChoice;
  68. @BindView(R.id.settings_proxy_port_edit)
  69. MaterialEditTextPreference proxyPortEditText;
  70. @BindView(R.id.settings_licence)
  71. MaterialStandardPreference licenceButton;
  72. @BindView(R.id.settings_privacy)
  73. MaterialStandardPreference privacyButton;
  74. @BindView(R.id.settings_source_code)
  75. MaterialStandardPreference sourceCodeButton;
  76. @BindView(R.id.settings_version)
  77. MaterialStandardPreference versionInfo;
  78. @BindView(R.id.avatar_image)
  79. AvatarImageView avatarImageView;
  80. @BindView(R.id.display_name_text)
  81. TextView displayName;
  82. @BindView(R.id.settings_remove_account)
  83. MaterialStandardPreference removeAccountButton;
  84. @BindView(R.id.settings_switch)
  85. MaterialStandardPreference switchAccountButton;
  86. @BindView(R.id.settings_reauthorize)
  87. MaterialStandardPreference reauthorizeButton;
  88. @BindView(R.id.settings_add_account)
  89. MaterialStandardPreference addAccountButton;
  90. @BindView(R.id.message_view)
  91. MaterialPreferenceCategory messageView;
  92. @BindView(R.id.message_text)
  93. TextView messageText;
  94. @Inject
  95. EventBus eventBus;
  96. @Inject
  97. AppPreferences appPreferences;
  98. @Inject
  99. UserUtils userUtils;
  100. private OnPreferenceValueChangedListener<String> proxyTypeChangeListener;
  101. private OnPreferenceValueChangedListener<Boolean> proxyCredentialsChangeListener;
  102. @Override
  103. protected View inflateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container) {
  104. return inflater.inflate(R.layout.controller_settings, container, false);
  105. }
  106. @Override
  107. protected void onViewBound(@NonNull View view) {
  108. super.onViewBound(view);
  109. NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this);
  110. }
  111. @Override
  112. protected void onAttach(@NonNull View view) {
  113. super.onAttach(view);
  114. if ("No proxy".equals(appPreferences.getProxyType()) || appPreferences.getProxyType() == null) {
  115. hideProxySettings();
  116. } else {
  117. showProxySettings();
  118. }
  119. if (appPreferences.getProxyCredentials()) {
  120. showProxyCredentials();
  121. } else {
  122. hideProxyCredentials();
  123. }
  124. appPreferences.registerProxyTypeListener(proxyTypeChangeListener = new ProxyTypeChangeListener());
  125. appPreferences.registerProxyCredentialsListener(proxyCredentialsChangeListener = new
  126. ProxyCredentialsChangeListener());
  127. List<String> listWithIntFields = new ArrayList<>();
  128. listWithIntFields.add("proxy_port");
  129. settingsScreen.setUserInputModule(new MagicUserInputModule(getActivity(), listWithIntFields));
  130. settingsScreen.setVisibilityController(R.id.settings_proxy_use_credentials,
  131. Arrays.asList(R.id.settings_proxy_username_edit, R.id.settings_proxy_password_edit),
  132. true);
  133. if (!TextUtils.isEmpty(getResources().getString(R.string.nc_gpl3_url))) {
  134. licenceButton.setOnClickListener(view1 -> {
  135. Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(getResources().
  136. getString(R.string.nc_gpl3_url)));
  137. startActivity(browserIntent);
  138. });
  139. } else {
  140. licenceButton.setVisibility(View.GONE);
  141. }
  142. if (!TextUtils.isEmpty(getResources().getString(R.string.nc_privacy_url))) {
  143. privacyButton.setOnClickListener(view12 -> {
  144. Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(getResources().
  145. getString(R.string.nc_privacy_url)));
  146. startActivity(browserIntent);
  147. });
  148. } else {
  149. privacyButton.setVisibility(View.GONE);
  150. }
  151. if (!TextUtils.isEmpty(getResources().getString(R.string.nc_source_code_url))) {
  152. sourceCodeButton.setOnClickListener(view13 -> {
  153. Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(getResources().
  154. getString(R.string.nc_source_code_url)));
  155. startActivity(browserIntent);
  156. });
  157. } else {
  158. sourceCodeButton.setVisibility(View.GONE);
  159. }
  160. versionInfo.setSummary("v" + BuildConfig.VERSION_NAME);
  161. UserEntity userEntity = userUtils.getCurrentUser();
  162. if (userEntity != null) {
  163. // Awful hack
  164. avatarImageView.setTextAndColorSeed(String.valueOf(userEntity.getDisplayName().
  165. toUpperCase().charAt(0)), ColorUtils.colorSeed);
  166. GlideUrl glideUrl = new GlideUrl(ApiHelper.getUrlForAvatarWithName(userEntity.getBaseUrl(),
  167. userEntity.getUsername()), new LazyHeaders.Builder()
  168. .setHeader("Accept", "image/*")
  169. .setHeader("User-Agent", ApiHelper.getUserAgent())
  170. .build());
  171. GlideApp.with(NextcloudTalkApplication.getSharedApplication().getApplicationContext())
  172. .load(glideUrl)
  173. .circleCrop()
  174. .centerInside()
  175. .into(avatarImageView);
  176. displayName.setText(userEntity.getDisplayName());
  177. }
  178. if (userUtils.getUsers().size() <= 1) {
  179. switchAccountButton.setVisibility(View.GONE);
  180. }
  181. reauthorizeButton.setOnClickListener(new View.OnClickListener() {
  182. @Override
  183. public void onClick(View view) {
  184. getParentController().getRouter().pushController(RouterTransaction.with(
  185. new WebViewLoginController(userEntity.getBaseUrl(), true))
  186. .pushChangeHandler(new VerticalChangeHandler())
  187. .popChangeHandler(new VerticalChangeHandler()));
  188. }
  189. });
  190. addAccountButton.setOnClickListener(new View.OnClickListener() {
  191. @Override
  192. public void onClick(View view) {
  193. getParentController().getRouter().pushController(RouterTransaction.with(new
  194. ServerSelectionController()).pushChangeHandler(new VerticalChangeHandler())
  195. .popChangeHandler(new VerticalChangeHandler()));
  196. }
  197. });
  198. if (SettingsMessageHolder.getInstance().getMessageType() != null) {
  199. switch (SettingsMessageHolder.getInstance().getMessageType()) {
  200. case ACCOUNT_UPDATED_NOT_ADDED:
  201. messageText.setText(getResources().getString(R.string.nc_settings_account_updated));
  202. messageView.setVisibility(View.VISIBLE);
  203. break;
  204. case WRONG_ACCOUNT:
  205. messageText.setText(getResources().getString(R.string.nc_settings_wrong_account));
  206. messageView.setVisibility(View.VISIBLE);
  207. break;
  208. default:
  209. messageView.setVisibility(View.GONE);
  210. break;
  211. }
  212. SettingsMessageHolder.getInstance().setMessageType(null);
  213. messageView.animate()
  214. .translationY(0)
  215. .alpha(0.0f)
  216. .setDuration(2000)
  217. .setStartDelay(5000)
  218. .setListener(new AnimatorListenerAdapter() {
  219. @Override
  220. public void onAnimationEnd(Animator animation) {
  221. super.onAnimationEnd(animation);
  222. messageView.setVisibility(View.GONE);
  223. }
  224. });
  225. } else {
  226. messageView.setVisibility(View.GONE);
  227. }
  228. }
  229. @Override
  230. public void onDestroy() {
  231. appPreferences.unregisterProxyTypeListener(proxyTypeChangeListener);
  232. appPreferences.unregisterProxyCredentialsListener(proxyCredentialsChangeListener);
  233. super.onDestroy();
  234. }
  235. private void hideProxySettings() {
  236. appPreferences.removeProxyHost();
  237. appPreferences.removeProxyPort();
  238. appPreferences.removeProxyCredentials();
  239. appPreferences.removeProxyUsername();
  240. appPreferences.removeProxyPassword();
  241. settingsScreen.findViewById(R.id.settings_proxy_host_edit).setVisibility(View.GONE);
  242. settingsScreen.findViewById(R.id.settings_proxy_port_edit).setVisibility(View.GONE);
  243. settingsScreen.findViewById(R.id.settings_proxy_use_credentials).setVisibility(View.GONE);
  244. settingsScreen.findViewById(R.id.settings_proxy_username_edit).setVisibility(View.GONE);
  245. settingsScreen.findViewById(R.id.settings_proxy_password_edit).setVisibility(View.GONE);
  246. }
  247. private void showProxySettings() {
  248. settingsScreen.findViewById(R.id.settings_proxy_host_edit).setVisibility(View.VISIBLE);
  249. settingsScreen.findViewById(R.id.settings_proxy_port_edit).setVisibility(View.VISIBLE);
  250. settingsScreen.findViewById(R.id.settings_proxy_use_credentials).setVisibility(View.VISIBLE);
  251. }
  252. private void showProxyCredentials() {
  253. settingsScreen.findViewById(R.id.settings_proxy_username_edit).setVisibility(View.VISIBLE);
  254. settingsScreen.findViewById(R.id.settings_proxy_password_edit).setVisibility(View.VISIBLE);
  255. }
  256. private void hideProxyCredentials() {
  257. appPreferences.removeProxyUsername();
  258. appPreferences.removeProxyPassword();
  259. settingsScreen.findViewById(R.id.settings_proxy_username_edit).setVisibility(View.GONE);
  260. settingsScreen.findViewById(R.id.settings_proxy_password_edit).setVisibility(View.GONE);
  261. }
  262. private class ProxyCredentialsChangeListener implements OnPreferenceValueChangedListener<Boolean> {
  263. @Override
  264. public void onChanged(Boolean newValue) {
  265. if (newValue) {
  266. showProxyCredentials();
  267. } else {
  268. hideProxyCredentials();
  269. }
  270. }
  271. }
  272. private class ProxyTypeChangeListener implements OnPreferenceValueChangedListener<String> {
  273. @Override
  274. public void onChanged(String newValue) {
  275. if ("No proxy".equals(newValue)) {
  276. hideProxySettings();
  277. } else {
  278. switch (newValue) {
  279. case "HTTP":
  280. proxyPortEditText.setValue("3128");
  281. break;
  282. case "DIRECT":
  283. proxyPortEditText.setValue("8080");
  284. break;
  285. case "SOCKS":
  286. proxyPortEditText.setValue("1080");
  287. break;
  288. default:
  289. break;
  290. }
  291. showProxySettings();
  292. }
  293. }
  294. }
  295. }