UserInfoActivity.java 21 KB

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