UserInfoActivity.java 21 KB

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