SettingsController.java 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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.app.KeyguardManager;
  24. import android.content.Context;
  25. import android.content.Intent;
  26. import android.net.Uri;
  27. import android.os.Build;
  28. import android.os.Bundle;
  29. import android.security.KeyChain;
  30. import android.text.TextUtils;
  31. import android.util.Log;
  32. import android.view.LayoutInflater;
  33. import android.view.View;
  34. import android.view.ViewGroup;
  35. import android.view.WindowManager;
  36. import android.widget.Checkable;
  37. import android.widget.ImageView;
  38. import android.widget.TextView;
  39. import androidx.annotation.NonNull;
  40. import androidx.annotation.Nullable;
  41. import androidx.core.view.ViewCompat;
  42. import androidx.work.OneTimeWorkRequest;
  43. import androidx.work.WorkManager;
  44. import autodagger.AutoInjector;
  45. import butterknife.BindView;
  46. import com.bluelinelabs.conductor.RouterTransaction;
  47. import com.bluelinelabs.conductor.changehandler.HorizontalChangeHandler;
  48. import com.bluelinelabs.conductor.changehandler.VerticalChangeHandler;
  49. import com.bluelinelabs.logansquare.LoganSquare;
  50. import com.bumptech.glide.load.model.GlideUrl;
  51. import com.bumptech.glide.load.model.LazyHeaders;
  52. import com.bumptech.glide.load.resource.bitmap.CircleCrop;
  53. import com.bumptech.glide.request.RequestOptions;
  54. import com.nextcloud.talk.BuildConfig;
  55. import com.nextcloud.talk.R;
  56. import com.nextcloud.talk.api.NcApi;
  57. import com.nextcloud.talk.application.NextcloudTalkApplication;
  58. import com.nextcloud.talk.controllers.base.BaseController;
  59. import com.nextcloud.talk.jobs.AccountRemovalWorker;
  60. import com.nextcloud.talk.models.RingtoneSettings;
  61. import com.nextcloud.talk.models.database.UserEntity;
  62. import com.nextcloud.talk.utils.ApiUtils;
  63. import com.nextcloud.talk.utils.DisplayUtils;
  64. import com.nextcloud.talk.utils.DoNotDisturbUtils;
  65. import com.nextcloud.talk.utils.SecurityUtils;
  66. import com.nextcloud.talk.utils.bundle.BundleKeys;
  67. import com.nextcloud.talk.utils.database.user.UserUtils;
  68. import com.nextcloud.talk.utils.glide.GlideApp;
  69. import com.nextcloud.talk.utils.preferences.AppPreferences;
  70. import com.nextcloud.talk.utils.preferences.MagicUserInputModule;
  71. import com.nextcloud.talk.utils.singletons.ApplicationWideMessageHolder;
  72. import com.yarolegovich.lovelydialog.LovelySaveStateHandler;
  73. import com.yarolegovich.lovelydialog.LovelyStandardDialog;
  74. import com.yarolegovich.mp.*;
  75. import io.reactivex.android.schedulers.AndroidSchedulers;
  76. import io.reactivex.disposables.Disposable;
  77. import io.reactivex.schedulers.Schedulers;
  78. import net.orange_box.storebox.listeners.OnPreferenceValueChangedListener;
  79. import org.greenrobot.eventbus.EventBus;
  80. import javax.inject.Inject;
  81. import java.io.IOException;
  82. import java.net.URI;
  83. import java.net.URISyntaxException;
  84. import java.util.*;
  85. @AutoInjector(NextcloudTalkApplication.class)
  86. public class SettingsController extends BaseController {
  87. public static final String TAG = "SettingsController";
  88. @BindView(R.id.settings_screen)
  89. MaterialPreferenceScreen settingsScreen;
  90. @BindView(R.id.settings_proxy_choice)
  91. MaterialChoicePreference proxyChoice;
  92. @BindView(R.id.settings_proxy_port_edit)
  93. MaterialEditTextPreference proxyPortEditText;
  94. @BindView(R.id.settings_licence)
  95. MaterialStandardPreference licenceButton;
  96. @BindView(R.id.settings_privacy)
  97. MaterialStandardPreference privacyButton;
  98. @BindView(R.id.settings_source_code)
  99. MaterialStandardPreference sourceCodeButton;
  100. @BindView(R.id.settings_version)
  101. MaterialStandardPreference versionInfo;
  102. @BindView(R.id.avatar_image)
  103. ImageView avatarImageView;
  104. @BindView(R.id.display_name_text)
  105. TextView displayNameTextView;
  106. @BindView(R.id.base_url_text)
  107. TextView baseUrlTextView;
  108. @BindView(R.id.settings_call_sound)
  109. MaterialStandardPreference settingsCallSound;
  110. @BindView(R.id.settings_message_sound)
  111. MaterialStandardPreference settingsMessageSound;
  112. @BindView(R.id.settings_remove_account)
  113. MaterialStandardPreference removeAccountButton;
  114. @BindView(R.id.settings_switch)
  115. MaterialStandardPreference switchAccountButton;
  116. @BindView(R.id.settings_reauthorize)
  117. MaterialStandardPreference reauthorizeButton;
  118. @BindView(R.id.settings_add_account)
  119. MaterialStandardPreference addAccountButton;
  120. @BindView(R.id.message_view)
  121. MaterialPreferenceCategory messageView;
  122. @BindView(R.id.settings_client_cert)
  123. MaterialStandardPreference certificateSetup;
  124. @BindView(R.id.settings_always_vibrate)
  125. MaterialSwitchPreference shouldVibrateSwitchPreference;
  126. @BindView(R.id.settings_incognito_keyboard)
  127. MaterialSwitchPreference incognitoKeyboardSwitchPreference;
  128. @BindView(R.id.settings_screen_security)
  129. MaterialSwitchPreference screenSecuritySwitchPreference;
  130. @BindView(R.id.settings_link_previews)
  131. MaterialSwitchPreference linkPreviewsSwitchPreference;
  132. @BindView(R.id.settings_screen_lock)
  133. MaterialSwitchPreference screenLockSwitchPreference;
  134. @BindView(R.id.settings_screen_lock_timeout)
  135. MaterialChoicePreference screenLockTimeoutChoicePreference;
  136. @BindView(R.id.message_text)
  137. TextView messageText;
  138. @Inject
  139. EventBus eventBus;
  140. @Inject
  141. AppPreferences appPreferences;
  142. @Inject
  143. NcApi ncApi;
  144. @Inject
  145. UserUtils userUtils;
  146. @Inject
  147. Context context;
  148. private LovelySaveStateHandler saveStateHandler;
  149. private UserEntity currentUser;
  150. private String credentials;
  151. private OnPreferenceValueChangedListener<String> proxyTypeChangeListener;
  152. private OnPreferenceValueChangedListener<Boolean> proxyCredentialsChangeListener;
  153. private OnPreferenceValueChangedListener<Boolean> screenSecurityChangeListener;
  154. private OnPreferenceValueChangedListener<Boolean> screenLockChangeListener;
  155. private OnPreferenceValueChangedListener<String> screenLockTimeoutChangeListener;
  156. private Disposable profileQueryDisposable;
  157. private Disposable dbQueryDisposable;
  158. private static final int ID_REMOVE_ACCOUNT_WARNING_DIALOG = 0;
  159. @Override
  160. protected View inflateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container) {
  161. return inflater.inflate(R.layout.controller_settings, container, false);
  162. }
  163. private void getCurrentUser() {
  164. currentUser = userUtils.getCurrentUser();
  165. credentials = ApiUtils.getCredentials(currentUser.getUsername(), currentUser.getToken());
  166. }
  167. @Override
  168. protected void onViewBound(@NonNull View view) {
  169. super.onViewBound(view);
  170. setHasOptionsMenu(true);
  171. ViewCompat.setTransitionName(avatarImageView, "userAvatar.transitionTag");
  172. NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this);
  173. getCurrentUser();
  174. if (saveStateHandler == null) {
  175. saveStateHandler = new LovelySaveStateHandler();
  176. }
  177. appPreferences.registerProxyTypeListener(proxyTypeChangeListener = new ProxyTypeChangeListener());
  178. appPreferences.registerProxyCredentialsListener(proxyCredentialsChangeListener = new ProxyCredentialsChangeListener());
  179. appPreferences.registerScreenSecurityListener(screenSecurityChangeListener = new ScreenSecurityChangeListener());
  180. appPreferences.registerScreenLockListener(screenLockChangeListener = new ScreenLockListener());
  181. appPreferences.registerScreenLockTimeoutListener(screenLockTimeoutChangeListener = new ScreenLockTimeoutListener());
  182. List<String> listWithIntFields = new ArrayList<>();
  183. listWithIntFields.add("proxy_port");
  184. settingsScreen.setUserInputModule(new MagicUserInputModule(getActivity(), listWithIntFields));
  185. settingsScreen.setVisibilityController(R.id.settings_proxy_use_credentials,
  186. Arrays.asList(R.id.settings_proxy_username_edit, R.id.settings_proxy_password_edit),
  187. true);
  188. if (!TextUtils.isEmpty(getResources().getString(R.string.nc_gpl3_url))) {
  189. licenceButton.addPreferenceClickListener(view1 -> {
  190. Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(getResources().
  191. getString(R.string.nc_gpl3_url)));
  192. startActivity(browserIntent);
  193. });
  194. } else {
  195. licenceButton.setVisibility(View.GONE);
  196. }
  197. if (!DoNotDisturbUtils.hasVibrator()) {
  198. shouldVibrateSwitchPreference.setVisibility(View.GONE);
  199. }
  200. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
  201. incognitoKeyboardSwitchPreference.setVisibility(View.GONE);
  202. }
  203. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
  204. screenLockSwitchPreference.setVisibility(View.GONE);
  205. screenLockTimeoutChoicePreference.setVisibility(View.GONE);
  206. } else {
  207. screenLockSwitchPreference.setSummary(String.format(Locale.getDefault(),
  208. getResources().getString(R.string.nc_settings_screen_lock_desc),
  209. getResources().getString(R.string.nc_app_name)));
  210. }
  211. if (!TextUtils.isEmpty(getResources().getString(R.string.nc_privacy_url))) {
  212. privacyButton.addPreferenceClickListener(view12 -> {
  213. Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(getResources().
  214. getString(R.string.nc_privacy_url)));
  215. startActivity(browserIntent);
  216. });
  217. } else {
  218. privacyButton.setVisibility(View.GONE);
  219. }
  220. if (!TextUtils.isEmpty(getResources().getString(R.string.nc_source_code_url))) {
  221. sourceCodeButton.addPreferenceClickListener(view13 -> {
  222. Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(getResources().
  223. getString(R.string.nc_source_code_url)));
  224. startActivity(browserIntent);
  225. });
  226. } else {
  227. sourceCodeButton.setVisibility(View.GONE);
  228. }
  229. versionInfo.setSummary("v" + BuildConfig.VERSION_NAME);
  230. settingsCallSound.setOnClickListener(v -> {
  231. Bundle bundle = new Bundle();
  232. bundle.putBoolean(BundleKeys.KEY_ARE_CALL_SOUNDS, true);
  233. getRouter().pushController(RouterTransaction.with(new RingtoneSelectionController(bundle))
  234. .pushChangeHandler(new HorizontalChangeHandler())
  235. .popChangeHandler(new HorizontalChangeHandler()));
  236. });
  237. settingsMessageSound.setOnClickListener(v -> {
  238. Bundle bundle = new Bundle();
  239. bundle.putBoolean(BundleKeys.KEY_ARE_CALL_SOUNDS, false);
  240. getRouter().pushController(RouterTransaction.with(new RingtoneSelectionController(bundle))
  241. .pushChangeHandler(new HorizontalChangeHandler())
  242. .popChangeHandler(new HorizontalChangeHandler()));
  243. });
  244. addAccountButton.addPreferenceClickListener(view15 -> {
  245. getRouter().pushController(RouterTransaction.with(new
  246. ServerSelectionController()).pushChangeHandler(new VerticalChangeHandler())
  247. .popChangeHandler(new VerticalChangeHandler()));
  248. });
  249. switchAccountButton.addPreferenceClickListener(view16 -> {
  250. getRouter().pushController(RouterTransaction.with(new
  251. SwitchAccountController()).pushChangeHandler(new VerticalChangeHandler())
  252. .popChangeHandler(new VerticalChangeHandler()));
  253. });
  254. String host = null;
  255. int port = -1;
  256. URI uri;
  257. try {
  258. uri = new URI(currentUser.getBaseUrl());
  259. host = uri.getHost();
  260. port = uri.getPort();
  261. } catch (URISyntaxException e) {
  262. Log.e(TAG, "Failed to create uri");
  263. }
  264. String finalHost = host;
  265. int finalPort = port;
  266. certificateSetup.addPreferenceClickListener(v -> KeyChain.choosePrivateKeyAlias(Objects.requireNonNull(getActivity()), alias -> {
  267. String finalAlias = alias;
  268. getActivity().runOnUiThread(() -> {
  269. if (finalAlias != null) {
  270. certificateSetup.setTitle(R.string.nc_client_cert_change);
  271. } else {
  272. certificateSetup.setTitle(R.string.nc_client_cert_setup);
  273. }
  274. });
  275. if (alias == null) {
  276. alias = "";
  277. }
  278. userUtils.createOrUpdateUser(null, null, null, null, null, null, null, currentUser.getId(),
  279. null, alias, null);
  280. }, new String[]{"RSA", "EC"}, null, finalHost, finalPort, currentUser.getClientCertificate
  281. ()));
  282. }
  283. private void showLovelyDialog(int dialogId, Bundle savedInstanceState) {
  284. switch (dialogId) {
  285. case ID_REMOVE_ACCOUNT_WARNING_DIALOG:
  286. showRemoveAccountWarning(savedInstanceState);
  287. break;
  288. default:
  289. break;
  290. }
  291. }
  292. @Override
  293. protected void onSaveViewState(@NonNull View view, @NonNull Bundle outState) {
  294. saveStateHandler.saveInstanceState(outState);
  295. super.onSaveViewState(view, outState);
  296. }
  297. @Override
  298. protected void onRestoreViewState(@NonNull View view, @NonNull Bundle savedViewState) {
  299. super.onRestoreViewState(view, savedViewState);
  300. if (LovelySaveStateHandler.wasDialogOnScreen(savedViewState)) {
  301. //Dialog won't be restarted automatically, so we need to call this method.
  302. //Each dialog knows how to restore its state
  303. showLovelyDialog(LovelySaveStateHandler.getSavedDialogId(savedViewState), savedViewState);
  304. }
  305. }
  306. private void showRemoveAccountWarning(Bundle savedInstanceState) {
  307. if (getActivity() != null) {
  308. new LovelyStandardDialog(getActivity(), LovelyStandardDialog.ButtonLayout.HORIZONTAL)
  309. .setTopColorRes(R.color.nc_darkRed)
  310. .setIcon(DisplayUtils.getTintedDrawable(getResources(),
  311. R.drawable.ic_delete_black_24dp, R.color.white))
  312. .setPositiveButtonColor(context.getResources().getColor(R.color.nc_darkRed))
  313. .setTitle(R.string.nc_settings_remove_account)
  314. .setMessage(R.string.nc_settings_remove_confirmation)
  315. .setPositiveButton(R.string.nc_settings_remove, new View.OnClickListener() {
  316. @Override
  317. public void onClick(View v) {
  318. removeCurrentAccount();
  319. }
  320. })
  321. .setNegativeButton(R.string.nc_cancel, null)
  322. .setInstanceStateHandler(ID_REMOVE_ACCOUNT_WARNING_DIALOG, saveStateHandler)
  323. .setSavedInstanceState(savedInstanceState)
  324. .show();
  325. }
  326. }
  327. private void removeCurrentAccount() {
  328. boolean otherUserExists = userUtils.scheduleUserForDeletionWithId(currentUser.getId());
  329. OneTimeWorkRequest accountRemovalWork = new OneTimeWorkRequest.Builder(AccountRemovalWorker.class).build();
  330. WorkManager.getInstance().enqueue(accountRemovalWork);
  331. if (otherUserExists && getView() != null) {
  332. onViewBound(getView());
  333. onAttach(getView());
  334. } else if (!otherUserExists) {
  335. getRouter().setRoot(RouterTransaction.with(
  336. new ServerSelectionController())
  337. .pushChangeHandler(new VerticalChangeHandler())
  338. .popChangeHandler(new VerticalChangeHandler()));
  339. }
  340. }
  341. @Override
  342. protected void onAttach(@NonNull View view) {
  343. super.onAttach(view);
  344. if (getActionBar() != null) {
  345. getActionBar().show();
  346. }
  347. dispose(null);
  348. getCurrentUser();
  349. if (!TextUtils.isEmpty(currentUser.getClientCertificate())) {
  350. certificateSetup.setTitle(R.string.nc_client_cert_change);
  351. } else {
  352. certificateSetup.setTitle(R.string.nc_client_cert_setup);
  353. }
  354. if (shouldVibrateSwitchPreference.getVisibility() == View.VISIBLE) {
  355. ((Checkable) shouldVibrateSwitchPreference.findViewById(R.id.mp_checkable)).setChecked(appPreferences.getShouldVibrateSetting());
  356. }
  357. ((Checkable) screenSecuritySwitchPreference.findViewById(R.id.mp_checkable)).setChecked(appPreferences.getIsScreenSecured());
  358. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  359. ((Checkable) incognitoKeyboardSwitchPreference.findViewById(R.id.mp_checkable)).setChecked(appPreferences.getIsKeyboardIncognito());
  360. }
  361. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  362. ((Checkable) incognitoKeyboardSwitchPreference.findViewById(R.id.mp_checkable)).setChecked(appPreferences.getIsKeyboardIncognito());
  363. }
  364. ((Checkable) linkPreviewsSwitchPreference.findViewById(R.id.mp_checkable)).setChecked(appPreferences.getAreLinkPreviewsAllowed());
  365. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  366. KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
  367. if (keyguardManager.isKeyguardSecure()) {
  368. screenLockSwitchPreference.setEnabled(true);
  369. screenLockTimeoutChoicePreference.setEnabled(true);
  370. ((Checkable) screenLockSwitchPreference.findViewById(R.id.mp_checkable)).setChecked(appPreferences.getIsScreenLocked());
  371. screenLockTimeoutChoicePreference.setEnabled(appPreferences.getIsScreenLocked());
  372. if (appPreferences.getIsScreenLocked()) {
  373. screenLockTimeoutChoicePreference.setAlpha(1.0f);
  374. } else {
  375. screenLockTimeoutChoicePreference.setAlpha(0.38f);
  376. }
  377. screenLockSwitchPreference.setAlpha(1.0f);
  378. } else {
  379. screenLockSwitchPreference.setEnabled(false);
  380. screenLockTimeoutChoicePreference.setEnabled(false);
  381. appPreferences.removeScreenLock();
  382. appPreferences.removeScreenLockTimeout();
  383. ((Checkable) screenLockSwitchPreference.findViewById(R.id.mp_checkable)).setChecked(false);
  384. screenLockSwitchPreference.setAlpha(0.38f);
  385. screenLockTimeoutChoicePreference.setAlpha(0.38f);
  386. }
  387. }
  388. String ringtoneName = "";
  389. RingtoneSettings ringtoneSettings;
  390. if (!TextUtils.isEmpty(appPreferences.getCallRingtoneUri())) {
  391. try {
  392. ringtoneSettings = LoganSquare.parse(appPreferences.getCallRingtoneUri(), RingtoneSettings.class);
  393. ringtoneName = ringtoneSettings.getRingtoneName();
  394. } catch (IOException e) {
  395. Log.e(TAG, "Failed to parse ringtone name");
  396. }
  397. settingsCallSound.setSummary(ringtoneName);
  398. } else {
  399. settingsCallSound.setSummary(R.string.nc_settings_default_ringtone);
  400. }
  401. ringtoneName = "";
  402. if (!TextUtils.isEmpty(appPreferences.getMessageRingtoneUri())) {
  403. try {
  404. ringtoneSettings = LoganSquare.parse(appPreferences.getMessageRingtoneUri(), RingtoneSettings.class);
  405. ringtoneName = ringtoneSettings.getRingtoneName();
  406. } catch (IOException e) {
  407. Log.e(TAG, "Failed to parse ringtone name");
  408. }
  409. settingsMessageSound.setSummary(ringtoneName);
  410. } else {
  411. settingsMessageSound.setSummary(R.string.nc_settings_default_ringtone);
  412. }
  413. if ("No proxy".equals(appPreferences.getProxyType()) || appPreferences.getProxyType() == null) {
  414. hideProxySettings();
  415. } else {
  416. showProxySettings();
  417. }
  418. if (appPreferences.getProxyCredentials()) {
  419. showProxyCredentials();
  420. } else {
  421. hideProxyCredentials();
  422. }
  423. if (currentUser != null) {
  424. baseUrlTextView.setText(Uri.parse(currentUser.getBaseUrl()).getHost());
  425. reauthorizeButton.addPreferenceClickListener(view14 -> {
  426. getRouter().pushController(RouterTransaction.with(
  427. new WebViewLoginController(currentUser.getBaseUrl(), true))
  428. .pushChangeHandler(new VerticalChangeHandler())
  429. .popChangeHandler(new VerticalChangeHandler()));
  430. });
  431. if (currentUser.getDisplayName() != null) {
  432. displayNameTextView.setText(currentUser.getDisplayName());
  433. }
  434. loadAvatarImage();
  435. profileQueryDisposable = ncApi.getUserProfile(credentials,
  436. ApiUtils.getUrlForUserProfile(currentUser.getBaseUrl()))
  437. .subscribeOn(Schedulers.newThread())
  438. .observeOn(AndroidSchedulers.mainThread())
  439. .subscribe(userProfileOverall -> {
  440. String displayName = null;
  441. if (!TextUtils.isEmpty(userProfileOverall.getOcs().getData()
  442. .getDisplayName())) {
  443. displayName = userProfileOverall.getOcs().getData()
  444. .getDisplayName();
  445. } else if (!TextUtils.isEmpty(userProfileOverall.getOcs().getData()
  446. .getDisplayNameAlt())) {
  447. displayName = userProfileOverall.getOcs().getData()
  448. .getDisplayNameAlt();
  449. }
  450. if ((!TextUtils.isEmpty(displayName) && !displayName.equals(currentUser.getDisplayName()))) {
  451. dbQueryDisposable = userUtils.createOrUpdateUser(null,
  452. null,
  453. null, displayName, null, null,
  454. null, currentUser.getId(), null, null, null)
  455. .subscribeOn(Schedulers.newThread())
  456. .observeOn(AndroidSchedulers.mainThread())
  457. .subscribe(userEntityResult -> {
  458. displayNameTextView.setText(userEntityResult.getDisplayName());
  459. },
  460. throwable -> {
  461. dispose(dbQueryDisposable);
  462. }, () -> dispose(dbQueryDisposable));
  463. }
  464. }, throwable -> {
  465. dispose(profileQueryDisposable);
  466. }, () -> dispose(profileQueryDisposable));
  467. removeAccountButton.addPreferenceClickListener(view1 -> {
  468. showLovelyDialog(ID_REMOVE_ACCOUNT_WARNING_DIALOG, null);
  469. });
  470. }
  471. if (userUtils.getUsers().size() <= 1) {
  472. switchAccountButton.setVisibility(View.GONE);
  473. }
  474. if (ApplicationWideMessageHolder.getInstance().getMessageType() != null) {
  475. switch (ApplicationWideMessageHolder.getInstance().getMessageType()) {
  476. case ACCOUNT_UPDATED_NOT_ADDED:
  477. messageText.setTextColor(getResources().getColor(R.color.colorPrimary));
  478. messageText.setText(getResources().getString(R.string.nc_settings_account_updated));
  479. messageView.setVisibility(View.VISIBLE);
  480. break;
  481. case SERVER_WITHOUT_TALK:
  482. messageText.setTextColor(getResources().getColor(R.color.nc_darkRed));
  483. messageText.setText(getResources().getString(R.string.nc_settings_wrong_account));
  484. messageView.setVisibility(View.VISIBLE);
  485. case ACCOUNT_WAS_IMPORTED:
  486. messageText.setTextColor(getResources().getColor(R.color.colorPrimary));
  487. messageText.setText(getResources().getString(R.string.nc_Server_account_imported));
  488. messageView.setVisibility(View.VISIBLE);
  489. break;
  490. case FAILED_TO_IMPORT_ACCOUNT:
  491. messageText.setTextColor(getResources().getColor(R.color.nc_darkRed));
  492. messageText.setText(getResources().getString(R.string.nc_server_failed_to_import_account));
  493. messageView.setVisibility(View.VISIBLE);
  494. break;
  495. default:
  496. messageView.setVisibility(View.GONE);
  497. break;
  498. }
  499. ApplicationWideMessageHolder.getInstance().setMessageType(null);
  500. messageView.animate()
  501. .translationY(0)
  502. .alpha(0.0f)
  503. .setDuration(2500)
  504. .setStartDelay(5000)
  505. .setListener(new AnimatorListenerAdapter() {
  506. @Override
  507. public void onAnimationEnd(Animator animation) {
  508. super.onAnimationEnd(animation);
  509. if (messageView != null) {
  510. messageView.setVisibility(View.GONE);
  511. }
  512. }
  513. });
  514. } else {
  515. if (messageView != null) {
  516. messageView.setVisibility(View.GONE);
  517. }
  518. }
  519. }
  520. private void loadAvatarImage() {
  521. String avatarId;
  522. if (!TextUtils.isEmpty(currentUser.getUserId())) {
  523. avatarId = currentUser.getUserId();
  524. } else {
  525. avatarId = currentUser.getUsername();
  526. }
  527. GlideUrl glideUrl = new GlideUrl(ApiUtils.getUrlForAvatarWithName(currentUser.getBaseUrl(),
  528. avatarId, R.dimen.avatar_size_big), new LazyHeaders.Builder()
  529. .setHeader("Accept", "image/*")
  530. .setHeader("User-Agent", ApiUtils.getUserAgent())
  531. .build());
  532. GlideApp.with(NextcloudTalkApplication.getSharedApplication().getApplicationContext())
  533. .load(glideUrl)
  534. .centerInside()
  535. .apply(RequestOptions.bitmapTransform(new CircleCrop()))
  536. .into(avatarImageView);
  537. }
  538. @Override
  539. public void onDestroy() {
  540. if (appPreferences != null) {
  541. appPreferences.unregisterProxyTypeListener(proxyTypeChangeListener);
  542. appPreferences.unregisterProxyCredentialsListener(proxyCredentialsChangeListener);
  543. appPreferences.unregisterScreenSecurityListener(screenSecurityChangeListener);
  544. appPreferences.unregisterScreenLockListener(screenLockChangeListener);
  545. appPreferences.unregisterScreenLockTimeoutListener(screenLockTimeoutChangeListener);
  546. }
  547. super.onDestroy();
  548. }
  549. private void hideProxySettings() {
  550. appPreferences.removeProxyHost();
  551. appPreferences.removeProxyPort();
  552. appPreferences.removeProxyCredentials();
  553. appPreferences.removeProxyUsername();
  554. appPreferences.removeProxyPassword();
  555. settingsScreen.findViewById(R.id.settings_proxy_host_edit).setVisibility(View.GONE);
  556. settingsScreen.findViewById(R.id.settings_proxy_port_edit).setVisibility(View.GONE);
  557. settingsScreen.findViewById(R.id.settings_proxy_use_credentials).setVisibility(View.GONE);
  558. settingsScreen.findViewById(R.id.settings_proxy_username_edit).setVisibility(View.GONE);
  559. settingsScreen.findViewById(R.id.settings_proxy_password_edit).setVisibility(View.GONE);
  560. }
  561. private void showProxySettings() {
  562. settingsScreen.findViewById(R.id.settings_proxy_host_edit).setVisibility(View.VISIBLE);
  563. settingsScreen.findViewById(R.id.settings_proxy_port_edit).setVisibility(View.VISIBLE);
  564. settingsScreen.findViewById(R.id.settings_proxy_use_credentials).setVisibility(View.VISIBLE);
  565. }
  566. private void showProxyCredentials() {
  567. settingsScreen.findViewById(R.id.settings_proxy_username_edit).setVisibility(View.VISIBLE);
  568. settingsScreen.findViewById(R.id.settings_proxy_password_edit).setVisibility(View.VISIBLE);
  569. }
  570. private void hideProxyCredentials() {
  571. appPreferences.removeProxyUsername();
  572. appPreferences.removeProxyPassword();
  573. settingsScreen.findViewById(R.id.settings_proxy_username_edit).setVisibility(View.GONE);
  574. settingsScreen.findViewById(R.id.settings_proxy_password_edit).setVisibility(View.GONE);
  575. }
  576. private void dispose(@Nullable Disposable disposable) {
  577. if (disposable != null && !disposable.isDisposed()) {
  578. disposable.dispose();
  579. } else if (disposable == null) {
  580. if (profileQueryDisposable != null && !profileQueryDisposable.isDisposed()) {
  581. profileQueryDisposable.dispose();
  582. profileQueryDisposable = null;
  583. } else if (profileQueryDisposable != null) {
  584. profileQueryDisposable = null;
  585. }
  586. if (dbQueryDisposable != null && !dbQueryDisposable.isDisposed()) {
  587. dbQueryDisposable.dispose();
  588. dbQueryDisposable = null;
  589. } else if (dbQueryDisposable != null) {
  590. dbQueryDisposable = null;
  591. }
  592. }
  593. }
  594. @Override
  595. protected String getTitle() {
  596. return getResources().getString(R.string.nc_settings);
  597. }
  598. private class ScreenLockTimeoutListener implements OnPreferenceValueChangedListener<String> {
  599. @Override
  600. public void onChanged(String newValue) {
  601. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  602. SecurityUtils.createKey(appPreferences.getScreenLockTimeout());
  603. }
  604. }
  605. }
  606. private class ScreenLockListener implements OnPreferenceValueChangedListener<Boolean> {
  607. @Override
  608. public void onChanged(Boolean newValue) {
  609. screenLockTimeoutChoicePreference.setEnabled(newValue);
  610. if (newValue) {
  611. screenLockTimeoutChoicePreference.setAlpha(1.0f);
  612. } else {
  613. screenLockTimeoutChoicePreference.setAlpha(0.38f);
  614. }
  615. }
  616. }
  617. private class ScreenSecurityChangeListener implements OnPreferenceValueChangedListener<Boolean> {
  618. @Override
  619. public void onChanged(Boolean newValue) {
  620. if (newValue) {
  621. if (getActivity() != null) {
  622. getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
  623. }
  624. } else {
  625. if (getActivity() != null) {
  626. getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
  627. }
  628. }
  629. }
  630. }
  631. private class ProxyCredentialsChangeListener implements OnPreferenceValueChangedListener<Boolean> {
  632. @Override
  633. public void onChanged(Boolean newValue) {
  634. if (newValue) {
  635. showProxyCredentials();
  636. } else {
  637. hideProxyCredentials();
  638. }
  639. }
  640. }
  641. private class ProxyTypeChangeListener implements OnPreferenceValueChangedListener<String> {
  642. @Override
  643. public void onChanged(String newValue) {
  644. if ("No proxy".equals(newValue)) {
  645. hideProxySettings();
  646. } else {
  647. switch (newValue) {
  648. case "HTTP":
  649. if (proxyPortEditText != null) {
  650. proxyPortEditText.setValue("3128");
  651. }
  652. break;
  653. case "DIRECT":
  654. if (proxyPortEditText != null) {
  655. proxyPortEditText.setValue("8080");
  656. }
  657. break;
  658. case "SOCKS":
  659. if (proxyPortEditText != null) {
  660. proxyPortEditText.setValue("1080");
  661. }
  662. break;
  663. default:
  664. break;
  665. }
  666. showProxySettings();
  667. }
  668. }
  669. }
  670. }