UserInfoActivity.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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.Build;
  37. import android.os.Bundle;
  38. import android.support.annotation.ColorInt;
  39. import android.support.annotation.DrawableRes;
  40. import android.support.annotation.NonNull;
  41. import android.support.annotation.Nullable;
  42. import android.support.design.widget.AppBarLayout;
  43. import android.support.v4.graphics.drawable.DrawableCompat;
  44. import android.support.v7.widget.DividerItemDecoration;
  45. import android.support.v7.widget.RecyclerView;
  46. import android.support.v7.widget.helper.ItemTouchHelper;
  47. import android.text.TextUtils;
  48. import android.view.LayoutInflater;
  49. import android.view.Menu;
  50. import android.view.MenuInflater;
  51. import android.view.MenuItem;
  52. import android.view.View;
  53. import android.view.ViewGroup;
  54. import android.webkit.URLUtil;
  55. import android.widget.ImageView;
  56. import android.widget.LinearLayout;
  57. import android.widget.ProgressBar;
  58. import android.widget.TextView;
  59. import butterknife.BindString;
  60. import butterknife.BindView;
  61. import butterknife.ButterKnife;
  62. import butterknife.Unbinder;
  63. import com.bumptech.glide.Glide;
  64. import com.bumptech.glide.request.animation.GlideAnimation;
  65. import com.bumptech.glide.request.target.SimpleTarget;
  66. import com.google.gson.Gson;
  67. import com.owncloud.android.R;
  68. import com.owncloud.android.authentication.AccountUtils;
  69. import com.owncloud.android.authentication.AuthenticatorActivity;
  70. import com.owncloud.android.datamodel.ArbitraryDataProvider;
  71. import com.owncloud.android.datamodel.PushConfigurationState;
  72. import com.owncloud.android.lib.common.UserInfo;
  73. import com.owncloud.android.lib.common.operations.RemoteOperation;
  74. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  75. import com.owncloud.android.lib.common.utils.Log_OC;
  76. import com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation;
  77. import com.owncloud.android.ui.events.TokenPushEvent;
  78. import com.owncloud.android.utils.DisplayUtils;
  79. import com.owncloud.android.utils.PushUtils;
  80. import com.owncloud.android.utils.ThemeUtils;
  81. import org.greenrobot.eventbus.EventBus;
  82. import org.greenrobot.eventbus.Subscribe;
  83. import org.greenrobot.eventbus.ThreadMode;
  84. import org.parceler.Parcels;
  85. import java.util.LinkedList;
  86. import java.util.List;
  87. /**
  88. * This Activity presents the user information.
  89. */
  90. public class UserInfoActivity extends FileActivity {
  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) LinearLayout emptyContentContainer;
  97. @BindView(R.id.empty_list_view_text) TextView emptyContentMessage;
  98. @BindView(R.id.empty_list_view_headline) TextView emptyContentHeadline;
  99. @BindView(R.id.empty_list_icon) ImageView emptyContentIcon;
  100. @BindView(R.id.user_info_view) LinearLayout userInfoView;
  101. @BindView(R.id.user_icon) ImageView avatar;
  102. @BindView(R.id.userinfo_username) TextView userName;
  103. @BindView(R.id.userinfo_username_full) TextView fullName;
  104. @BindView(R.id.user_info_list) RecyclerView mUserInfoList;
  105. @BindView(R.id.empty_list_progress) ProgressBar multiListProgressBar;
  106. @BindString(R.string.user_information_retrieval_error) String sorryMessage;
  107. private float mCurrentAccountAvatarRadiusDimension;
  108. private Unbinder unbinder;
  109. private UserInfo userInfo;
  110. private Account account;
  111. @Override
  112. public void onCreate(Bundle savedInstanceState) {
  113. Log_OC.v(TAG, "onCreate() start");
  114. super.onCreate(savedInstanceState);
  115. Bundle bundle = getIntent().getExtras();
  116. account = Parcels.unwrap(bundle.getParcelable(KEY_ACCOUNT));
  117. if (savedInstanceState != null && savedInstanceState.containsKey(KEY_USER_DATA)) {
  118. userInfo = Parcels.unwrap(savedInstanceState.getParcelable(KEY_USER_DATA));
  119. }
  120. mCurrentAccountAvatarRadiusDimension = getResources().getDimension(R.dimen.nav_drawer_header_avatar_radius);
  121. setContentView(R.layout.user_info_layout);
  122. unbinder = ButterKnife.bind(this);
  123. setAccount(AccountUtils.getCurrentOwnCloudAccount(this));
  124. onAccountSet(false);
  125. boolean useBackgroundImage = URLUtil.isValidUrl(
  126. getStorageManager().getCapability(account.name).getServerBackground());
  127. setupToolbar(useBackgroundImage);
  128. updateActionBarTitleAndHomeButtonByString("");
  129. mUserInfoList.setAdapter(new UserInfoAdapter(null, ThemeUtils.primaryColor(getAccount())));
  130. mUserInfoList.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
  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.change_password:
  153. changeAccountPassword(account);
  154. break;
  155. case R.id.delete_account:
  156. openAccountRemovalConfirmationDialog(account, getFragmentManager(), false);
  157. break;
  158. default:
  159. retval = super.onOptionsItemSelected(item);
  160. break;
  161. }
  162. return retval;
  163. }
  164. public void onDestroy() {
  165. super.onDestroy();
  166. unbinder.unbind();
  167. }
  168. private void setMultiListLoadingMessage() {
  169. if (emptyContentContainer != null) {
  170. emptyContentHeadline.setText(R.string.file_list_loading);
  171. emptyContentMessage.setText("");
  172. emptyContentIcon.setVisibility(View.GONE);
  173. emptyContentMessage.setVisibility(View.GONE);
  174. multiListProgressBar.getIndeterminateDrawable().setColorFilter(ThemeUtils.primaryColor(),
  175. PorterDuff.Mode.SRC_IN);
  176. multiListProgressBar.setVisibility(View.VISIBLE);
  177. }
  178. }
  179. private void setErrorMessageForMultiList(String headline, String message, @DrawableRes int errorResource) {
  180. if (emptyContentContainer != null && emptyContentMessage != null) {
  181. emptyContentHeadline.setText(headline);
  182. emptyContentMessage.setText(message);
  183. emptyContentIcon.setImageResource(errorResource);
  184. multiListProgressBar.setVisibility(View.GONE);
  185. emptyContentIcon.setVisibility(View.VISIBLE);
  186. emptyContentMessage.setVisibility(View.VISIBLE);
  187. }
  188. }
  189. private void setHeaderImage() {
  190. if (getStorageManager().getCapability(account.name).getServerBackground() != null) {
  191. ViewGroup appBar = findViewById(R.id.appbar);
  192. if (appBar != null) {
  193. ImageView backgroundImageView = appBar.findViewById(R.id.drawer_header_background);
  194. String background = getStorageManager().getCapability(account.name).getServerBackground();
  195. int primaryColor = ThemeUtils.primaryColor(getAccount());
  196. if (URLUtil.isValidUrl(background)) {
  197. // background image
  198. SimpleTarget target = new SimpleTarget<Drawable>() {
  199. @Override
  200. public void onResourceReady(Drawable resource, GlideAnimation glideAnimation) {
  201. Drawable[] drawables = {new ColorDrawable(primaryColor), resource};
  202. LayerDrawable layerDrawable = new LayerDrawable(drawables);
  203. backgroundImageView.setImageDrawable(layerDrawable);
  204. }
  205. @Override
  206. public void onLoadFailed(Exception e, Drawable errorDrawable) {
  207. Drawable[] drawables = {new ColorDrawable(primaryColor),
  208. getResources().getDrawable(R.drawable.background)};
  209. LayerDrawable layerDrawable = new LayerDrawable(drawables);
  210. backgroundImageView.setImageDrawable(layerDrawable);
  211. }
  212. };
  213. Glide.with(this)
  214. .load(background)
  215. .centerCrop()
  216. .placeholder(R.drawable.background)
  217. .error(R.drawable.background)
  218. .crossFade()
  219. .into(target);
  220. } else {
  221. // plain color
  222. backgroundImageView.setImageDrawable(new ColorDrawable(primaryColor));
  223. }
  224. }
  225. }
  226. }
  227. private void populateUserInfoUi(UserInfo userInfo) {
  228. userName.setText(account.name);
  229. DisplayUtils.setAvatar(account, UserInfoActivity.this,
  230. mCurrentAccountAvatarRadiusDimension, getResources(), getStorageManager(), avatar);
  231. int tint = ThemeUtils.primaryColor(account);
  232. if (!TextUtils.isEmpty(userInfo.getDisplayName())) {
  233. fullName.setText(userInfo.getDisplayName());
  234. }
  235. if (userInfo.getPhone() == null && userInfo.getEmail() == null && userInfo.getAddress() == null
  236. && userInfo.getTwitter() == null & userInfo.getWebpage() == null) {
  237. setErrorMessageForMultiList(getString(R.string.userinfo_no_info_headline),
  238. getString(R.string.userinfo_no_info_text), R.drawable.ic_user);
  239. } else {
  240. emptyContentContainer.setVisibility(View.GONE);
  241. userInfoView.setVisibility(View.VISIBLE);
  242. if (mUserInfoList.getAdapter() instanceof UserInfoAdapter) {
  243. mUserInfoList.setAdapter(new UserInfoAdapter(createUserInfoDetails(userInfo), tint));
  244. }
  245. }
  246. }
  247. private List<UserInfoDetailsItem> createUserInfoDetails(UserInfo userInfo) {
  248. List<UserInfoDetailsItem> result = new LinkedList<>();
  249. addToListIfNeeded(result, R.drawable.ic_phone, userInfo.getPhone());
  250. addToListIfNeeded(result, R.drawable.ic_email, userInfo.getEmail());
  251. addToListIfNeeded(result, R.drawable.ic_map_marker, userInfo.getAddress());
  252. addToListIfNeeded(result, R.drawable.ic_web, DisplayUtils.beautifyURL(userInfo.getWebpage()));
  253. addToListIfNeeded(result, R.drawable.ic_twitter, DisplayUtils.beautifyTwitterHandle(userInfo.getTwitter()));
  254. return result;
  255. }
  256. private void addToListIfNeeded(List<UserInfoDetailsItem> info, @DrawableRes int icon, String text) {
  257. if (!TextUtils.isEmpty(text))
  258. info.add(new UserInfoDetailsItem(icon, text));
  259. }
  260. private void changeAccountPassword(Account account) {
  261. // let the user update credentials with one click
  262. Intent updateAccountCredentials = new Intent(this, AuthenticatorActivity.class);
  263. updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACCOUNT, account);
  264. updateAccountCredentials.putExtra(AuthenticatorActivity.EXTRA_ACTION,
  265. AuthenticatorActivity.ACTION_UPDATE_TOKEN);
  266. startActivity(updateAccountCredentials);
  267. }
  268. public static void openAccountRemovalConfirmationDialog(Account account, FragmentManager fragmentManager,
  269. boolean removeDirectly) {
  270. UserInfoActivity.AccountRemovalConfirmationDialog dialog =
  271. UserInfoActivity.AccountRemovalConfirmationDialog.newInstance(account, removeDirectly);
  272. dialog.show(fragmentManager, "dialog");
  273. }
  274. public static class AccountRemovalConfirmationDialog extends DialogFragment {
  275. private Account account;
  276. public static UserInfoActivity.AccountRemovalConfirmationDialog newInstance(Account account,
  277. boolean removeDirectly) {
  278. Bundle bundle = new Bundle();
  279. bundle.putParcelable(KEY_ACCOUNT, account);
  280. bundle.putBoolean(KEY_DIRECT_REMOVE, removeDirectly);
  281. UserInfoActivity.AccountRemovalConfirmationDialog dialog = new
  282. UserInfoActivity.AccountRemovalConfirmationDialog();
  283. dialog.setArguments(bundle);
  284. return dialog;
  285. }
  286. @Override
  287. public void onCreate(@Nullable Bundle savedInstanceState) {
  288. super.onCreate(savedInstanceState);
  289. account = getArguments().getParcelable(KEY_ACCOUNT);
  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. class UserInfoDetailsItem {
  370. @DrawableRes int icon;
  371. String text;
  372. UserInfoDetailsItem(@DrawableRes int icon, String text) {
  373. this.icon = icon;
  374. this.text = text;
  375. }
  376. }
  377. class UserInfoAdapter extends RecyclerView.Adapter<UserInfoAdapter.ViewHolder> {
  378. class ViewHolder extends RecyclerView.ViewHolder {
  379. @BindView(R.id.icon) ImageView icon;
  380. @BindView(R.id.text) TextView text;
  381. public ViewHolder(View itemView) {
  382. super(itemView);
  383. ButterKnife.bind(this, itemView);
  384. }
  385. }
  386. List<UserInfoDetailsItem> mDisplayList;
  387. @ColorInt int mTintColor;
  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. @Override
  397. public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  398. LayoutInflater inflater = LayoutInflater.from(parent.getContext());
  399. View view = inflater.inflate(R.layout.user_info_details_table_item, parent, false);
  400. ViewHolder holder = new ViewHolder(view);
  401. return holder;
  402. }
  403. @Override
  404. public void onBindViewHolder(ViewHolder holder, int position) {
  405. UserInfoDetailsItem item = mDisplayList.get(position);
  406. holder.icon.setImageResource(item.icon);
  407. holder.text.setText(item.text);
  408. DrawableCompat.setTint(holder.icon.getDrawable(), mTintColor);
  409. }
  410. @Override
  411. public int getItemCount() {
  412. return mDisplayList.size();
  413. }
  414. }
  415. }