UserInfoActivity.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /**
  2. * Nextcloud Android client application
  3. *
  4. * @author Mario Danic
  5. * @author Andy Scherzinger
  6. * Copyright (C) 2017 Mario Danic
  7. * Copyright (C) 2017 Andy Scherzinger
  8. * Copyright (C) 2017 Nextcloud GmbH.
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. package com.owncloud.android.ui.activity;
  24. import android.accounts.Account;
  25. import android.accounts.AccountManager;
  26. import android.app.AlertDialog;
  27. import android.app.Dialog;
  28. import android.app.DialogFragment;
  29. import android.app.FragmentManager;
  30. import android.content.ContentResolver;
  31. import android.content.DialogInterface;
  32. import android.content.Intent;
  33. import android.graphics.Color;
  34. import android.graphics.PorterDuff;
  35. import android.graphics.drawable.Drawable;
  36. import android.os.Build;
  37. import android.os.Bundle;
  38. import android.support.annotation.ColorInt;
  39. import android.support.annotation.NonNull;
  40. import android.support.annotation.Nullable;
  41. import android.support.design.widget.AppBarLayout;
  42. import android.support.v4.graphics.drawable.DrawableCompat;
  43. import android.text.TextUtils;
  44. import android.view.Menu;
  45. import android.view.MenuInflater;
  46. import android.view.MenuItem;
  47. import android.view.View;
  48. import android.webkit.URLUtil;
  49. import android.widget.ImageView;
  50. import android.widget.LinearLayout;
  51. import android.widget.ProgressBar;
  52. import android.widget.TextView;
  53. import com.bumptech.glide.Glide;
  54. import com.bumptech.glide.request.animation.GlideAnimation;
  55. import com.bumptech.glide.request.target.SimpleTarget;
  56. import com.google.gson.Gson;
  57. import com.owncloud.android.R;
  58. import com.owncloud.android.authentication.AccountUtils;
  59. import com.owncloud.android.authentication.AuthenticatorActivity;
  60. import com.owncloud.android.datamodel.ArbitraryDataProvider;
  61. import com.owncloud.android.datamodel.PushConfigurationState;
  62. import com.owncloud.android.datamodel.SyncedFolderProvider;
  63. import com.owncloud.android.lib.common.UserInfo;
  64. import com.owncloud.android.lib.common.operations.RemoteOperation;
  65. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  66. import com.owncloud.android.lib.common.utils.Log_OC;
  67. import com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation;
  68. import com.owncloud.android.ui.events.TokenPushEvent;
  69. import com.owncloud.android.utils.DisplayUtils;
  70. import com.owncloud.android.utils.PushUtils;
  71. import com.owncloud.android.utils.ThemeUtils;
  72. import org.greenrobot.eventbus.EventBus;
  73. import org.greenrobot.eventbus.Subscribe;
  74. import org.greenrobot.eventbus.ThreadMode;
  75. import org.parceler.Parcels;
  76. import butterknife.BindString;
  77. import butterknife.BindView;
  78. import butterknife.ButterKnife;
  79. import butterknife.Unbinder;
  80. /**
  81. * This Activity presents the user information.
  82. */
  83. public class UserInfoActivity extends FileActivity {
  84. private static final String TAG = UserInfoActivity.class.getSimpleName();
  85. private static final String KEY_USER_DATA = "USER_DATA";
  86. private static final String KEY_ACCOUNT = "ACCOUNT";
  87. private static final String KEY_DIRECT_REMOVE = "DIRECT_REMOVE";
  88. private static final int KEY_DELETE_CODE = 101;
  89. @BindView(R.id.empty_list_view)
  90. public LinearLayout emptyContentContainer;
  91. @BindView(R.id.empty_list_view_text)
  92. public TextView emptyContentMessage;
  93. @BindView(R.id.empty_list_view_headline)
  94. public TextView emptyContentHeadline;
  95. @BindView(R.id.empty_list_icon)
  96. public ImageView emptyContentIcon;
  97. @BindView(R.id.user_info_view)
  98. public LinearLayout userInfoView;
  99. @BindView(R.id.user_icon)
  100. public ImageView avatar;
  101. @BindView(R.id.drawer_username)
  102. public TextView userName;
  103. @BindView(R.id.drawer_username_full)
  104. public TextView fullName;
  105. @BindView(R.id.phone_container)
  106. public View mPhoneNumberContainer;
  107. @BindView(R.id.phone_number)
  108. public TextView mPhoneNumberTextView;
  109. @BindView(R.id.phone_icon)
  110. public ImageView mPhoneNumberIcon;
  111. @BindView(R.id.email_container)
  112. public View mEmailContainer;
  113. @BindView(R.id.email_address)
  114. public TextView mEmailAddressTextView;
  115. @BindView(R.id.email_icon)
  116. public ImageView mEmailIcon;
  117. @BindView(R.id.address_container)
  118. public View mAddressContainer;
  119. @BindView(R.id.address)
  120. public TextView mAddressTextView;
  121. @BindView(R.id.address_icon)
  122. public ImageView mAddressIcon;
  123. @BindView(R.id.website_container)
  124. public View mWebsiteContainer;
  125. @BindView(R.id.website_address)
  126. public TextView mWebsiteTextView;
  127. @BindView(R.id.website_icon)
  128. public ImageView mWebsiteIcon;
  129. @BindView(R.id.twitter_container)
  130. public View mTwitterContainer;
  131. @BindView(R.id.twitter_handle)
  132. public TextView mTwitterHandleTextView;
  133. @BindView(R.id.twitter_icon)
  134. public ImageView mTwitterIcon;
  135. @BindView(R.id.empty_list_progress)
  136. public ProgressBar multiListProgressBar;
  137. @BindString(R.string.preview_sorry)
  138. public String sorryMessage;
  139. private float mCurrentAccountAvatarRadiusDimension;
  140. private Unbinder unbinder;
  141. private UserInfo userInfo;
  142. private Account account;
  143. @Override
  144. public void onCreate(Bundle savedInstanceState) {
  145. Log_OC.v(TAG, "onCreate() start");
  146. super.onCreate(savedInstanceState);
  147. Bundle bundle = getIntent().getExtras();
  148. account = Parcels.unwrap(bundle.getParcelable(KEY_ACCOUNT));
  149. if (savedInstanceState != null && savedInstanceState.containsKey(KEY_USER_DATA)) {
  150. userInfo = Parcels.unwrap(savedInstanceState.getParcelable(KEY_USER_DATA));
  151. }
  152. mCurrentAccountAvatarRadiusDimension = getResources().getDimension(R.dimen.nav_drawer_header_avatar_radius);
  153. setContentView(R.layout.user_info_layout);
  154. unbinder = ButterKnife.bind(this);
  155. setAccount(AccountUtils.getCurrentOwnCloudAccount(this));
  156. onAccountSet(false);
  157. boolean useBackgroundImage = URLUtil.isValidUrl(
  158. getStorageManager().getCapability(account.name).getServerBackground());
  159. setupToolbar(useBackgroundImage);
  160. updateActionBarTitleAndHomeButtonByString("");
  161. if (userInfo != null) {
  162. populateUserInfoUi(userInfo);
  163. emptyContentContainer.setVisibility(View.GONE);
  164. userInfoView.setVisibility(View.VISIBLE);
  165. } else {
  166. setMultiListLoadingMessage();
  167. fetchAndSetData();
  168. }
  169. setHeaderImage();
  170. }
  171. @Override
  172. public boolean onCreateOptionsMenu(Menu menu) {
  173. MenuInflater inflater = getMenuInflater();
  174. inflater.inflate(R.menu.user_info_menu, menu);
  175. return true;
  176. }
  177. @Override
  178. public boolean onOptionsItemSelected(MenuItem item) {
  179. boolean retval = true;
  180. switch (item.getItemId()) {
  181. case android.R.id.home:
  182. onBackPressed();
  183. break;
  184. case R.id.change_password:
  185. changeAccountPassword(account);
  186. break;
  187. case R.id.delete_account:
  188. openAccountRemovalConfirmationDialog(account, getFragmentManager(), false);
  189. break;
  190. default:
  191. retval = super.onOptionsItemSelected(item);
  192. break;
  193. }
  194. return retval;
  195. }
  196. public void onDestroy() {
  197. super.onDestroy();
  198. unbinder.unbind();
  199. }
  200. private void setMultiListLoadingMessage() {
  201. if (emptyContentContainer != null) {
  202. emptyContentHeadline.setText(R.string.file_list_loading);
  203. emptyContentMessage.setText("");
  204. emptyContentIcon.setVisibility(View.GONE);
  205. multiListProgressBar.getIndeterminateDrawable().setColorFilter(ThemeUtils.primaryColor(),
  206. PorterDuff.Mode.SRC_IN);
  207. multiListProgressBar.setVisibility(View.VISIBLE);
  208. }
  209. }
  210. private void setMessageForMultiList(String headline, String message) {
  211. if (emptyContentContainer != null && emptyContentMessage != null) {
  212. emptyContentHeadline.setText(headline);
  213. emptyContentMessage.setText(message);
  214. multiListProgressBar.setVisibility(View.GONE);
  215. }
  216. }
  217. private void setHeaderImage() {
  218. if (getStorageManager().getCapability(account.name).getServerBackground() != null) {
  219. final AppBarLayout appBar = (AppBarLayout) findViewById(R.id.appbar);
  220. if (appBar != null) {
  221. String background = getStorageManager().getCapability(account.name).getServerBackground();
  222. if (URLUtil.isValidUrl(background)) {
  223. // background image
  224. SimpleTarget target = new SimpleTarget<Drawable>() {
  225. @Override
  226. public void onResourceReady(Drawable resource, GlideAnimation glideAnimation) {
  227. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
  228. appBar.setBackgroundDrawable(resource);
  229. } else {
  230. appBar.setBackground(resource);
  231. }
  232. }
  233. };
  234. Glide.with(this)
  235. .load(background)
  236. .centerCrop()
  237. .placeholder(R.drawable.background)
  238. .error(R.drawable.background)
  239. .crossFade()
  240. .into(target);
  241. } else if (!background.isEmpty()) {
  242. // plain color
  243. int color = Color.parseColor(background);
  244. appBar.setBackgroundColor(color);
  245. }
  246. }
  247. }
  248. }
  249. private void populateUserInfoUi(UserInfo userInfo) {
  250. userName.setText(account.name);
  251. DisplayUtils.setAvatar(account, UserInfoActivity.this,
  252. mCurrentAccountAvatarRadiusDimension, getResources(), getStorageManager(), avatar);
  253. int tint = ThemeUtils.primaryColor(account);
  254. if (userInfo != null) {
  255. if (!TextUtils.isEmpty(userInfo.getDisplayName())) {
  256. fullName.setText(userInfo.getDisplayName());
  257. }
  258. populateUserInfoElement(mPhoneNumberContainer, mPhoneNumberTextView, userInfo.getPhone(),
  259. mPhoneNumberIcon, tint);
  260. populateUserInfoElement(mEmailContainer, mEmailAddressTextView, userInfo.getEmail(), mEmailIcon, tint);
  261. populateUserInfoElement(mAddressContainer, mAddressTextView, userInfo.getAddress(), mAddressIcon, tint);
  262. populateUserInfoElement(
  263. mWebsiteContainer,
  264. mWebsiteTextView,
  265. DisplayUtils.beautifyURL(userInfo.getWebpage()),
  266. mWebsiteIcon,
  267. tint);
  268. populateUserInfoElement(
  269. mTwitterContainer,
  270. mTwitterHandleTextView,
  271. DisplayUtils.beautifyTwitterHandle(userInfo.getTwitter()),
  272. mTwitterIcon,
  273. tint);
  274. }
  275. }
  276. private void populateUserInfoElement(View container, TextView textView, String text, ImageView icon, @ColorInt int
  277. tint) {
  278. if (!TextUtils.isEmpty(text)) {
  279. textView.setText(text);
  280. DrawableCompat.setTint(icon.getDrawable(), tint);
  281. } else {
  282. container.setVisibility(View.GONE);
  283. }
  284. }
  285. private void changeAccountPassword(Account account) {
  286. // let the user update credentials with one click
  287. Intent updateAccountCredentials = new Intent(this, AuthenticatorActivity.class);
  288. updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, account);
  289. updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACTION,
  290. AuthenticatorActivity.ACTION_UPDATE_TOKEN);
  291. startActivity(updateAccountCredentials);
  292. }
  293. public static void openAccountRemovalConfirmationDialog(Account account, FragmentManager fragmentManager,
  294. boolean removeDirectly) {
  295. UserInfoActivity.AccountRemovalConfirmationDialog dialog =
  296. UserInfoActivity.AccountRemovalConfirmationDialog.newInstance(account, removeDirectly);
  297. dialog.show(fragmentManager, "dialog");
  298. }
  299. public static class AccountRemovalConfirmationDialog extends DialogFragment {
  300. private Account account;
  301. public static UserInfoActivity.AccountRemovalConfirmationDialog newInstance(Account account,
  302. boolean removeDirectly) {
  303. Bundle bundle = new Bundle();
  304. bundle.putParcelable(KEY_ACCOUNT, account);
  305. bundle.putBoolean(KEY_DIRECT_REMOVE, removeDirectly);
  306. UserInfoActivity.AccountRemovalConfirmationDialog dialog = new
  307. UserInfoActivity.AccountRemovalConfirmationDialog();
  308. dialog.setArguments(bundle);
  309. return dialog;
  310. }
  311. @Override
  312. public void onCreate(@Nullable Bundle savedInstanceState) {
  313. super.onCreate(savedInstanceState);
  314. account = getArguments().getParcelable(KEY_ACCOUNT);
  315. }
  316. @NonNull
  317. @Override
  318. public Dialog onCreateDialog(Bundle savedInstanceState) {
  319. final boolean removeDirectly = getArguments().getBoolean(KEY_DIRECT_REMOVE);
  320. return new AlertDialog.Builder(getActivity(), R.style.Theme_ownCloud_Dialog)
  321. .setTitle(R.string.delete_account)
  322. .setMessage(getResources().getString(R.string.delete_account_warning, account.name))
  323. .setIcon(R.drawable.ic_warning)
  324. .setPositiveButton(R.string.common_ok,
  325. new DialogInterface.OnClickListener() {
  326. @Override
  327. public void onClick(DialogInterface dialogInterface, int i) {
  328. // remove contact backup job
  329. ContactsPreferenceActivity.cancelContactBackupJobForAccount(getActivity(), account);
  330. ContentResolver contentResolver = getActivity().getContentResolver();
  331. // delete all synced folder for an account
  332. SyncedFolderProvider syncedFolderProvider = new SyncedFolderProvider(
  333. contentResolver);
  334. syncedFolderProvider.deleteSyncFoldersForAccount(account);
  335. // disable daily backup
  336. ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(
  337. contentResolver);
  338. arbitraryDataProvider.storeOrUpdateKeyValue(account.name,
  339. ContactsPreferenceActivity.PREFERENCE_CONTACTS_AUTOMATIC_BACKUP,
  340. "false");
  341. String arbitraryDataPushString;
  342. if (!TextUtils.isEmpty(arbitraryDataPushString = arbitraryDataProvider.getValue(
  343. account, PushUtils.KEY_PUSH)) &&
  344. !TextUtils.isEmpty(getResources().getString(R.string.push_server_url))) {
  345. Gson gson = new Gson();
  346. PushConfigurationState pushArbitraryData = gson.fromJson(arbitraryDataPushString,
  347. PushConfigurationState.class);
  348. pushArbitraryData.setShouldBeDeleted(true);
  349. arbitraryDataProvider.storeOrUpdateKeyValue(account.name, PushUtils.KEY_PUSH,
  350. gson.toJson(pushArbitraryData));
  351. EventBus.getDefault().post(new TokenPushEvent());
  352. }
  353. if (getActivity() != null && !removeDirectly) {
  354. Bundle bundle = new Bundle();
  355. bundle.putParcelable(KEY_ACCOUNT, Parcels.wrap(account));
  356. Intent intent = new Intent();
  357. intent.putExtras(bundle);
  358. getActivity().setResult(KEY_DELETE_CODE, intent);
  359. getActivity().finish();
  360. } else {
  361. AccountManager am = (AccountManager) getActivity()
  362. .getSystemService(ACCOUNT_SERVICE);
  363. am.removeAccount(account, null, null);
  364. Intent start = new Intent(getActivity(), FileDisplayActivity.class);
  365. start.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  366. startActivity(start);
  367. }
  368. }
  369. })
  370. .setNegativeButton(R.string.common_cancel, null)
  371. .create();
  372. }
  373. }
  374. private void fetchAndSetData() {
  375. Thread t = new Thread(new Runnable() {
  376. public void run() {
  377. RemoteOperation getRemoteUserInfoOperation = new GetRemoteUserInfoOperation();
  378. RemoteOperationResult result = getRemoteUserInfoOperation.execute(account, UserInfoActivity.this);
  379. if (result.isSuccess() && result.getData() != null) {
  380. userInfo = (UserInfo) result.getData().get(0);
  381. runOnUiThread(new Runnable() {
  382. @Override
  383. public void run() {
  384. populateUserInfoUi(userInfo);
  385. emptyContentContainer.setVisibility(View.GONE);
  386. userInfoView.setVisibility(View.VISIBLE);
  387. }
  388. });
  389. } else {
  390. // show error
  391. setMessageForMultiList(result.getLogMessage(), sorryMessage);
  392. Log_OC.d(TAG, result.getLogMessage());
  393. }
  394. }
  395. });
  396. t.start();
  397. }
  398. @Override
  399. protected void onSaveInstanceState(Bundle outState) {
  400. super.onSaveInstanceState(outState);
  401. if (userInfo != null) {
  402. outState.putParcelable(KEY_USER_DATA, Parcels.wrap(userInfo));
  403. }
  404. }
  405. @Subscribe(threadMode = ThreadMode.BACKGROUND)
  406. public void onMessageEvent(TokenPushEvent event) {
  407. PushUtils.pushRegistrationToServer();
  408. }
  409. }