UserInfoActivity.java 21 KB

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