ChooseRichDocumentsTemplateDialogFragment.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. *
  3. * Nextcloud Android client application
  4. *
  5. * @author Tobias Kaminsky
  6. * Copyright (C) 2019 Tobias Kaminsky
  7. * Copyright (C) 2019 Nextcloud GmbH
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. */
  22. package com.owncloud.android.ui.dialog;
  23. import android.app.Activity;
  24. import android.app.Dialog;
  25. import android.content.Intent;
  26. import android.os.AsyncTask;
  27. import android.os.Bundle;
  28. import android.view.LayoutInflater;
  29. import android.view.View;
  30. import android.view.Window;
  31. import android.view.WindowManager.LayoutParams;
  32. import android.widget.Button;
  33. import com.nextcloud.client.account.CurrentAccountProvider;
  34. import com.nextcloud.client.account.User;
  35. import com.nextcloud.client.di.Injectable;
  36. import com.nextcloud.client.network.ClientFactory;
  37. import com.owncloud.android.MainApp;
  38. import com.owncloud.android.R;
  39. import com.owncloud.android.databinding.ChooseTemplateBinding;
  40. import com.owncloud.android.datamodel.FileDataStorageManager;
  41. import com.owncloud.android.datamodel.OCFile;
  42. import com.owncloud.android.datamodel.Template;
  43. import com.owncloud.android.files.CreateFileFromTemplateOperation;
  44. import com.owncloud.android.files.FetchTemplateOperation;
  45. import com.owncloud.android.lib.common.OwnCloudClient;
  46. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  47. import com.owncloud.android.lib.common.utils.Log_OC;
  48. import com.owncloud.android.lib.resources.files.ReadFileRemoteOperation;
  49. import com.owncloud.android.lib.resources.files.model.RemoteFile;
  50. import com.owncloud.android.ui.activity.ExternalSiteWebView;
  51. import com.owncloud.android.ui.activity.RichDocumentsEditorWebView;
  52. import com.owncloud.android.ui.adapter.RichDocumentsTemplateAdapter;
  53. import com.owncloud.android.utils.DisplayUtils;
  54. import com.owncloud.android.utils.FileStorageUtils;
  55. import com.owncloud.android.utils.NextcloudServer;
  56. import com.owncloud.android.utils.ThemeUtils;
  57. import org.parceler.Parcels;
  58. import java.lang.ref.WeakReference;
  59. import java.util.ArrayList;
  60. import java.util.List;
  61. import javax.inject.Inject;
  62. import androidx.annotation.NonNull;
  63. import androidx.appcompat.app.AlertDialog;
  64. import androidx.fragment.app.DialogFragment;
  65. import androidx.recyclerview.widget.GridLayoutManager;
  66. /**
  67. * Dialog to show templates for new documents/spreadsheets/presentations.
  68. */
  69. public class ChooseRichDocumentsTemplateDialogFragment extends DialogFragment implements View.OnClickListener,
  70. RichDocumentsTemplateAdapter.ClickListener, Injectable {
  71. private static final String ARG_PARENT_FOLDER = "PARENT_FOLDER";
  72. private static final String ARG_TYPE = "TYPE";
  73. private static final String TAG = ChooseRichDocumentsTemplateDialogFragment.class.getSimpleName();
  74. private static final String DOT = ".";
  75. private RichDocumentsTemplateAdapter adapter;
  76. private OCFile parentFolder;
  77. private OwnCloudClient client;
  78. @Inject CurrentAccountProvider currentAccount;
  79. @Inject ClientFactory clientFactory;
  80. private Button positiveButton;
  81. public enum Type {
  82. DOCUMENT,
  83. SPREADSHEET,
  84. PRESENTATION
  85. }
  86. ChooseTemplateBinding binding;
  87. @NextcloudServer(max = 18) // will be removed in favor of generic direct editing
  88. public static ChooseRichDocumentsTemplateDialogFragment newInstance(OCFile parentFolder, Type type) {
  89. ChooseRichDocumentsTemplateDialogFragment frag = new ChooseRichDocumentsTemplateDialogFragment();
  90. Bundle args = new Bundle();
  91. args.putParcelable(ARG_PARENT_FOLDER, parentFolder);
  92. args.putString(ARG_TYPE, type.name());
  93. frag.setArguments(args);
  94. return frag;
  95. }
  96. @Override
  97. public void onStart() {
  98. super.onStart();
  99. int color = ThemeUtils.primaryAccentColor(getContext());
  100. AlertDialog alertDialog = (AlertDialog) getDialog();
  101. positiveButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
  102. ThemeUtils.themeBorderlessButton(positiveButton, color);
  103. positiveButton.setOnClickListener(this);
  104. positiveButton.setEnabled(false);
  105. ThemeUtils.themeBorderlessButton(alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL), color);
  106. checkEnablingCreateButton();
  107. }
  108. @NonNull
  109. @Override
  110. public Dialog onCreateDialog(Bundle savedInstanceState) {
  111. Bundle arguments = getArguments();
  112. if (arguments == null) {
  113. throw new IllegalArgumentException("Arguments may not be null");
  114. }
  115. Activity activity = getActivity();
  116. if (activity == null) {
  117. throw new IllegalArgumentException("Activity may not be null");
  118. }
  119. parentFolder = arguments.getParcelable(ARG_PARENT_FOLDER);
  120. // Inflate the layout for the dialog
  121. LayoutInflater inflater = requireActivity().getLayoutInflater();
  122. binding = ChooseTemplateBinding.inflate(inflater, null, false);
  123. View view = binding.getRoot();
  124. binding.filename.requestFocus();
  125. ThemeUtils.colorTextInputLayout(binding.filenameContainer, ThemeUtils.primaryColor(getContext()));
  126. try {
  127. client = clientFactory.create(currentAccount.getUser());
  128. } catch (ClientFactory.CreationException e) {
  129. throw new RuntimeException(e); // we'll NPE without the client
  130. }
  131. Type type = Type.valueOf(arguments.getString(ARG_TYPE));
  132. new FetchTemplateTask(this, client).execute(type);
  133. binding.list.setHasFixedSize(true);
  134. binding.list.setLayoutManager(new GridLayoutManager(activity, 2));
  135. adapter = new RichDocumentsTemplateAdapter(type, this, getContext(), currentAccount, clientFactory);
  136. binding.list.setAdapter(adapter);
  137. // Build the dialog
  138. AlertDialog.Builder builder = new AlertDialog.Builder(activity);
  139. builder.setView(view)
  140. .setPositiveButton(R.string.create, null)
  141. .setNeutralButton(R.string.common_cancel, null)
  142. .setTitle(getTitle(type));
  143. Dialog dialog = builder.create();
  144. Window window = dialog.getWindow();
  145. if (window != null) {
  146. window.setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
  147. }
  148. return dialog;
  149. }
  150. private int getTitle(Type type) {
  151. if (type == Type.DOCUMENT) {
  152. return R.string.create_new_document;
  153. } else if (type == Type.SPREADSHEET) {
  154. return R.string.create_new_spreadsheet;
  155. } else if (type == Type.PRESENTATION) {
  156. return R.string.create_new_presentation;
  157. }
  158. return R.string.select_template;
  159. }
  160. @Override
  161. public void onDestroyView() {
  162. super.onDestroyView();
  163. binding = null;
  164. }
  165. private void createFromTemplate(Template template, String path) {
  166. new CreateFileFromTemplateTask(this, client, template, path, currentAccount.getUser()).execute();
  167. }
  168. public void setTemplateList(List<Template> templateList) {
  169. adapter.setTemplateList(templateList);
  170. adapter.notifyDataSetChanged();
  171. }
  172. @Override
  173. public void onClick(View v) {
  174. String name = binding.filename.getText().toString();
  175. String path = parentFolder.getRemotePath() + name;
  176. Template selectedTemplate = adapter.getSelectedTemplate();
  177. if (selectedTemplate == null) {
  178. DisplayUtils.showSnackMessage(binding.list, R.string.select_one_template);
  179. } else if (name.isEmpty() || name.equalsIgnoreCase(DOT + selectedTemplate.getExtension())) {
  180. DisplayUtils.showSnackMessage(binding.list, R.string.enter_filename);
  181. } else if (!name.endsWith(selectedTemplate.getExtension())) {
  182. createFromTemplate(selectedTemplate, path + DOT + selectedTemplate.getExtension());
  183. } else {
  184. createFromTemplate(selectedTemplate, path);
  185. }
  186. }
  187. @Override
  188. public void onClick(Template template) {
  189. onTemplateChosen(template);
  190. }
  191. private void onTemplateChosen(Template template) {
  192. adapter.setTemplateAsActive(template);
  193. prefillFilenameIfEmpty(template);
  194. checkEnablingCreateButton();
  195. }
  196. private void prefillFilenameIfEmpty(Template template) {
  197. String name = binding.filename.getText().toString();
  198. if (name.isEmpty() || name.equalsIgnoreCase(DOT + template.getExtension())) {
  199. binding.filename.setText(String.format("%s.%s", template.name, template.extension));
  200. }
  201. binding.filename.setSelection(binding.filename.getText().toString().lastIndexOf('.'));
  202. }
  203. private void checkEnablingCreateButton() {
  204. Template selectedTemplate = adapter.getSelectedTemplate();
  205. String name = binding.filename.getText().toString();
  206. positiveButton.setEnabled(selectedTemplate != null && !name.isEmpty() &&
  207. !name.equalsIgnoreCase(DOT + selectedTemplate.getExtension()));
  208. }
  209. private static class CreateFileFromTemplateTask extends AsyncTask<Void, Void, String> {
  210. private OwnCloudClient client;
  211. private WeakReference<ChooseRichDocumentsTemplateDialogFragment> chooseTemplateDialogFragmentWeakReference;
  212. private Template template;
  213. private String path;
  214. private User user;
  215. private OCFile file;
  216. CreateFileFromTemplateTask(ChooseRichDocumentsTemplateDialogFragment chooseTemplateDialogFragment,
  217. OwnCloudClient client,
  218. Template template,
  219. String path,
  220. User user
  221. ) {
  222. this.client = client;
  223. this.chooseTemplateDialogFragmentWeakReference = new WeakReference<>(chooseTemplateDialogFragment);
  224. this.template = template;
  225. this.path = path;
  226. this.user = user;
  227. }
  228. @Override
  229. protected String doInBackground(Void... voids) {
  230. RemoteOperationResult result = new CreateFileFromTemplateOperation(path, template.getId()).execute(client);
  231. if (result.isSuccess()) {
  232. // get file
  233. RemoteOperationResult newFileResult = new ReadFileRemoteOperation(path).execute(client);
  234. if (newFileResult.isSuccess()) {
  235. OCFile temp = FileStorageUtils.fillOCFile((RemoteFile) newFileResult.getData().get(0));
  236. if (chooseTemplateDialogFragmentWeakReference.get() != null) {
  237. FileDataStorageManager storageManager = new FileDataStorageManager(
  238. user.toPlatformAccount(),
  239. chooseTemplateDialogFragmentWeakReference.get().requireContext().getContentResolver());
  240. storageManager.saveFile(temp);
  241. file = storageManager.getFileByPath(path);
  242. return result.getData().get(0).toString();
  243. } else {
  244. return "";
  245. }
  246. } else {
  247. return "";
  248. }
  249. } else {
  250. return "";
  251. }
  252. }
  253. @Override
  254. protected void onPostExecute(String url) {
  255. ChooseRichDocumentsTemplateDialogFragment fragment = chooseTemplateDialogFragmentWeakReference.get();
  256. if (fragment != null && fragment.isAdded()) {
  257. if (url.isEmpty()) {
  258. DisplayUtils.showSnackMessage(fragment.binding.list, "Error creating file from template");
  259. } else {
  260. Intent collaboraWebViewIntent = new Intent(MainApp.getAppContext(), RichDocumentsEditorWebView.class);
  261. collaboraWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_TITLE, "Collabora");
  262. collaboraWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_URL, url);
  263. collaboraWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_FILE, file);
  264. collaboraWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_SHOW_SIDEBAR, false);
  265. collaboraWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_TEMPLATE, Parcels.wrap(template));
  266. fragment.startActivity(collaboraWebViewIntent);
  267. fragment.dismiss();
  268. }
  269. } else {
  270. Log_OC.e(TAG, "Error creating file from template!");
  271. }
  272. }
  273. }
  274. private static class FetchTemplateTask extends AsyncTask<Type, Void, List<Template>> {
  275. private OwnCloudClient client;
  276. private WeakReference<ChooseRichDocumentsTemplateDialogFragment> chooseTemplateDialogFragmentWeakReference;
  277. FetchTemplateTask(ChooseRichDocumentsTemplateDialogFragment chooseTemplateDialogFragment, OwnCloudClient client) {
  278. this.client = client;
  279. this.chooseTemplateDialogFragmentWeakReference = new WeakReference<>(chooseTemplateDialogFragment);
  280. }
  281. @Override
  282. protected List<Template> doInBackground(Type... type) {
  283. FetchTemplateOperation fetchTemplateOperation = new FetchTemplateOperation(type[0]);
  284. RemoteOperationResult result = fetchTemplateOperation.execute(client);
  285. if (!result.isSuccess()) {
  286. return new ArrayList<>();
  287. }
  288. List<Template> templateList = new ArrayList<>();
  289. for (Object object : result.getData()) {
  290. templateList.add((Template) object);
  291. }
  292. return templateList;
  293. }
  294. @Override
  295. protected void onPostExecute(List<Template> templateList) {
  296. ChooseRichDocumentsTemplateDialogFragment fragment = chooseTemplateDialogFragmentWeakReference.get();
  297. if (fragment != null) {
  298. if (templateList.isEmpty()) {
  299. DisplayUtils.showSnackMessage(fragment.binding.list, R.string.error_retrieving_templates);
  300. } else {
  301. if (templateList.size() == 1) {
  302. fragment.onTemplateChosen(templateList.get(0));
  303. fragment.binding.list.setVisibility(View.GONE);
  304. } else {
  305. String name = DOT + templateList.get(0).getExtension();
  306. fragment.binding.filename.setText(name);
  307. fragment.binding.helperText.setVisibility(View.VISIBLE);
  308. }
  309. fragment.setTemplateList(templateList);
  310. }
  311. } else {
  312. Log_OC.e(TAG, "Error streaming file: no previewMediaFragment!");
  313. }
  314. }
  315. }
  316. }