ChooseRichDocumentsTemplateDialogFragment.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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.theme.ThemeButtonUtils;
  57. import com.owncloud.android.utils.theme.ThemeColorUtils;
  58. import com.owncloud.android.utils.theme.ThemeTextInputUtils;
  59. import org.parceler.Parcels;
  60. import java.lang.ref.WeakReference;
  61. import java.util.ArrayList;
  62. import java.util.List;
  63. import javax.inject.Inject;
  64. import androidx.annotation.NonNull;
  65. import androidx.appcompat.app.AlertDialog;
  66. import androidx.fragment.app.DialogFragment;
  67. import androidx.recyclerview.widget.GridLayoutManager;
  68. /**
  69. * Dialog to show templates for new documents/spreadsheets/presentations.
  70. */
  71. public class ChooseRichDocumentsTemplateDialogFragment extends DialogFragment implements View.OnClickListener,
  72. RichDocumentsTemplateAdapter.ClickListener, Injectable {
  73. private static final String ARG_PARENT_FOLDER = "PARENT_FOLDER";
  74. private static final String ARG_TYPE = "TYPE";
  75. private static final String TAG = ChooseRichDocumentsTemplateDialogFragment.class.getSimpleName();
  76. private static final String DOT = ".";
  77. public static final int SINGLE_TEMPLATE = 1;
  78. private RichDocumentsTemplateAdapter adapter;
  79. private OCFile parentFolder;
  80. private OwnCloudClient client;
  81. @Inject CurrentAccountProvider currentAccount;
  82. @Inject ClientFactory clientFactory;
  83. private Button positiveButton;
  84. public enum Type {
  85. DOCUMENT,
  86. SPREADSHEET,
  87. PRESENTATION
  88. }
  89. ChooseTemplateBinding binding;
  90. @NextcloudServer(max = 18) // will be removed in favor of generic direct editing
  91. public static ChooseRichDocumentsTemplateDialogFragment newInstance(OCFile parentFolder, Type type) {
  92. ChooseRichDocumentsTemplateDialogFragment frag = new ChooseRichDocumentsTemplateDialogFragment();
  93. Bundle args = new Bundle();
  94. args.putParcelable(ARG_PARENT_FOLDER, parentFolder);
  95. args.putString(ARG_TYPE, type.name());
  96. frag.setArguments(args);
  97. return frag;
  98. }
  99. @Override
  100. public void onStart() {
  101. super.onStart();
  102. AlertDialog alertDialog = (AlertDialog) getDialog();
  103. positiveButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
  104. ThemeButtonUtils.themeBorderlessButton(positiveButton,
  105. alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL));
  106. positiveButton.setOnClickListener(this);
  107. positiveButton.setEnabled(false);
  108. checkEnablingCreateButton();
  109. }
  110. @NonNull
  111. @Override
  112. public Dialog onCreateDialog(Bundle savedInstanceState) {
  113. Bundle arguments = getArguments();
  114. if (arguments == null) {
  115. throw new IllegalArgumentException("Arguments may not be null");
  116. }
  117. Activity activity = getActivity();
  118. if (activity == null) {
  119. throw new IllegalArgumentException("Activity may not be null");
  120. }
  121. try {
  122. client = clientFactory.create(currentAccount.getUser());
  123. } catch (ClientFactory.CreationException e) {
  124. throw new RuntimeException(e); // we'll NPE without the client
  125. }
  126. parentFolder = arguments.getParcelable(ARG_PARENT_FOLDER);
  127. // Inflate the layout for the dialog
  128. LayoutInflater inflater = requireActivity().getLayoutInflater();
  129. binding = ChooseTemplateBinding.inflate(inflater, null, false);
  130. View view = binding.getRoot();
  131. binding.filename.requestFocus();
  132. ThemeTextInputUtils.colorTextInput(binding.filenameContainer,
  133. binding.filename,
  134. ThemeColorUtils.primaryColor(getContext()));
  135. Type type = Type.valueOf(arguments.getString(ARG_TYPE));
  136. new FetchTemplateTask(this, client).execute(type);
  137. binding.list.setHasFixedSize(true);
  138. binding.list.setLayoutManager(new GridLayoutManager(activity, 2));
  139. adapter = new RichDocumentsTemplateAdapter(type, this, getContext(), currentAccount, clientFactory);
  140. binding.list.setAdapter(adapter);
  141. // Build the dialog
  142. AlertDialog.Builder builder = new AlertDialog.Builder(activity);
  143. builder.setView(view)
  144. .setPositiveButton(R.string.create, null)
  145. .setNeutralButton(R.string.common_cancel, null)
  146. .setTitle(getTitle(type));
  147. Dialog dialog = builder.create();
  148. Window window = dialog.getWindow();
  149. if (window != null) {
  150. window.setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
  151. }
  152. return dialog;
  153. }
  154. private int getTitle(Type type) {
  155. if (type == Type.DOCUMENT) {
  156. return R.string.create_new_document;
  157. } else if (type == Type.SPREADSHEET) {
  158. return R.string.create_new_spreadsheet;
  159. } else if (type == Type.PRESENTATION) {
  160. return R.string.create_new_presentation;
  161. }
  162. return R.string.select_template;
  163. }
  164. @Override
  165. public void onDestroyView() {
  166. super.onDestroyView();
  167. binding = null;
  168. }
  169. private void createFromTemplate(Template template, String path) {
  170. new CreateFileFromTemplateTask(this, client, template, path, currentAccount.getUser()).execute();
  171. }
  172. public void setTemplateList(List<Template> templateList) {
  173. adapter.setTemplateList(templateList);
  174. adapter.notifyDataSetChanged();
  175. }
  176. @Override
  177. public void onClick(View v) {
  178. String name = binding.filename.getText().toString();
  179. String path = parentFolder.getRemotePath() + name;
  180. Template selectedTemplate = adapter.getSelectedTemplate();
  181. if (selectedTemplate == null) {
  182. DisplayUtils.showSnackMessage(binding.list, R.string.select_one_template);
  183. } else if (name.isEmpty() || name.equalsIgnoreCase(DOT + selectedTemplate.getExtension())) {
  184. DisplayUtils.showSnackMessage(binding.list, R.string.enter_filename);
  185. } else if (!name.endsWith(selectedTemplate.getExtension())) {
  186. createFromTemplate(selectedTemplate, path + DOT + selectedTemplate.getExtension());
  187. } else {
  188. createFromTemplate(selectedTemplate, path);
  189. }
  190. }
  191. @Override
  192. public void onClick(Template template) {
  193. onTemplateChosen(template);
  194. }
  195. private void onTemplateChosen(Template template) {
  196. adapter.setTemplateAsActive(template);
  197. prefillFilenameIfEmpty(template);
  198. checkEnablingCreateButton();
  199. }
  200. private void prefillFilenameIfEmpty(Template template) {
  201. String name = binding.filename.getText().toString();
  202. if (name.isEmpty() || name.equalsIgnoreCase(DOT + template.getExtension())) {
  203. binding.filename.setText(String.format("%s.%s", template.name, template.extension));
  204. }
  205. binding.filename.setSelection(binding.filename.getText().toString().lastIndexOf('.'));
  206. }
  207. private void checkEnablingCreateButton() {
  208. Template selectedTemplate = adapter.getSelectedTemplate();
  209. String name = binding.filename.getText().toString();
  210. positiveButton.setEnabled(selectedTemplate != null && !name.isEmpty() &&
  211. !name.equalsIgnoreCase(DOT + selectedTemplate.getExtension()));
  212. }
  213. private static class CreateFileFromTemplateTask extends AsyncTask<Void, Void, String> {
  214. private OwnCloudClient client;
  215. private WeakReference<ChooseRichDocumentsTemplateDialogFragment> chooseTemplateDialogFragmentWeakReference;
  216. private Template template;
  217. private String path;
  218. private User user;
  219. private OCFile file;
  220. CreateFileFromTemplateTask(ChooseRichDocumentsTemplateDialogFragment chooseTemplateDialogFragment,
  221. OwnCloudClient client,
  222. Template template,
  223. String path,
  224. User user
  225. ) {
  226. this.client = client;
  227. this.chooseTemplateDialogFragmentWeakReference = new WeakReference<>(chooseTemplateDialogFragment);
  228. this.template = template;
  229. this.path = path;
  230. this.user = user;
  231. }
  232. @Override
  233. protected String doInBackground(Void... voids) {
  234. RemoteOperationResult result = new CreateFileFromTemplateOperation(path, template.getId()).execute(client);
  235. if (result.isSuccess()) {
  236. // get file
  237. RemoteOperationResult newFileResult = new ReadFileRemoteOperation(path).execute(client);
  238. if (newFileResult.isSuccess()) {
  239. OCFile temp = FileStorageUtils.fillOCFile((RemoteFile) newFileResult.getData().get(0));
  240. if (chooseTemplateDialogFragmentWeakReference.get() != null) {
  241. FileDataStorageManager storageManager = new FileDataStorageManager(
  242. user.toPlatformAccount(),
  243. chooseTemplateDialogFragmentWeakReference.get().requireContext().getContentResolver());
  244. storageManager.saveFile(temp);
  245. file = storageManager.getFileByPath(path);
  246. return result.getData().get(0).toString();
  247. } else {
  248. return "";
  249. }
  250. } else {
  251. return "";
  252. }
  253. } else {
  254. return "";
  255. }
  256. }
  257. @Override
  258. protected void onPostExecute(String url) {
  259. ChooseRichDocumentsTemplateDialogFragment fragment = chooseTemplateDialogFragmentWeakReference.get();
  260. if (fragment != null && fragment.isAdded()) {
  261. if (url.isEmpty()) {
  262. DisplayUtils.showSnackMessage(fragment.binding.list, "Error creating file from template");
  263. } else {
  264. Intent collaboraWebViewIntent = new Intent(MainApp.getAppContext(), RichDocumentsEditorWebView.class);
  265. collaboraWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_TITLE, "Collabora");
  266. collaboraWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_URL, url);
  267. collaboraWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_FILE, file);
  268. collaboraWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_SHOW_SIDEBAR, false);
  269. collaboraWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_TEMPLATE, Parcels.wrap(template));
  270. fragment.startActivity(collaboraWebViewIntent);
  271. fragment.dismiss();
  272. }
  273. } else {
  274. Log_OC.e(TAG, "Error creating file from template!");
  275. }
  276. }
  277. }
  278. private static class FetchTemplateTask extends AsyncTask<Type, Void, List<Template>> {
  279. private OwnCloudClient client;
  280. private WeakReference<ChooseRichDocumentsTemplateDialogFragment> chooseTemplateDialogFragmentWeakReference;
  281. FetchTemplateTask(ChooseRichDocumentsTemplateDialogFragment chooseTemplateDialogFragment, OwnCloudClient client) {
  282. this.client = client;
  283. this.chooseTemplateDialogFragmentWeakReference = new WeakReference<>(chooseTemplateDialogFragment);
  284. }
  285. @Override
  286. protected List<Template> doInBackground(Type... type) {
  287. FetchTemplateOperation fetchTemplateOperation = new FetchTemplateOperation(type[0]);
  288. RemoteOperationResult result = fetchTemplateOperation.execute(client);
  289. if (!result.isSuccess()) {
  290. return new ArrayList<>();
  291. }
  292. List<Template> templateList = new ArrayList<>();
  293. for (Object object : result.getData()) {
  294. templateList.add((Template) object);
  295. }
  296. return templateList;
  297. }
  298. @Override
  299. protected void onPostExecute(List<Template> templateList) {
  300. ChooseRichDocumentsTemplateDialogFragment fragment = chooseTemplateDialogFragmentWeakReference.get();
  301. if (fragment != null) {
  302. if (templateList.isEmpty()) {
  303. DisplayUtils.showSnackMessage(fragment.binding.list, R.string.error_retrieving_templates);
  304. } else {
  305. if (templateList.size() == SINGLE_TEMPLATE) {
  306. fragment.onTemplateChosen(templateList.get(0));
  307. fragment.binding.list.setVisibility(View.GONE);
  308. } else {
  309. String name = DOT + templateList.get(0).getExtension();
  310. fragment.binding.filename.setText(name);
  311. fragment.binding.helperText.setVisibility(View.VISIBLE);
  312. }
  313. fragment.setTemplateList(templateList);
  314. }
  315. } else {
  316. Log_OC.e(TAG, "Error streaming file: no previewMediaFragment!");
  317. }
  318. }
  319. }
  320. }