UserInfoActivity.java 21 KB

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