ChooseTemplateDialogFragment.java 16 KB

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