SettingsController.java 33 KB

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