SettingsController.java 32 KB

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