UserInfoActivity.java 21 KB

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