UserInfoActivity.java 20 KB

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