ChooseTemplateDialogFragment.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. public static final int SINGLE_TEMPLATE = 1;
  80. private TemplateAdapter adapter;
  81. private OCFile parentFolder;
  82. private String title;
  83. @Inject ClientFactory clientFactory;
  84. private Creator creator;
  85. @Inject CurrentAccountProvider currentAccount;
  86. private Button positiveButton;
  87. public enum Type {
  88. DOCUMENT,
  89. SPREADSHEET,
  90. PRESENTATION
  91. }
  92. ChooseTemplateBinding binding;
  93. public static ChooseTemplateDialogFragment newInstance(OCFile parentFolder, Creator creator, String headline) {
  94. ChooseTemplateDialogFragment frag = new ChooseTemplateDialogFragment();
  95. Bundle args = new Bundle();
  96. args.putParcelable(ARG_PARENT_FOLDER, parentFolder);
  97. args.putParcelable(ARG_CREATOR, creator);
  98. args.putString(ARG_HEADLINE, headline);
  99. frag.setArguments(args);
  100. return frag;
  101. }
  102. @Override
  103. public void onStart() {
  104. super.onStart();
  105. int color = ThemeUtils.primaryAccentColor(getContext());
  106. AlertDialog alertDialog = (AlertDialog) getDialog();
  107. positiveButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
  108. ThemeUtils.themeBorderlessButton(positiveButton, color);
  109. positiveButton.setOnClickListener(this);
  110. positiveButton.setEnabled(false);
  111. ThemeUtils.themeBorderlessButton(alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL), color);
  112. checkEnablingCreateButton();
  113. }
  114. @NonNull
  115. @Override
  116. public Dialog onCreateDialog(Bundle savedInstanceState) {
  117. Bundle arguments = getArguments();
  118. if (arguments == null) {
  119. throw new IllegalArgumentException("Arguments may not be null");
  120. }
  121. Activity activity = getActivity();
  122. if (activity == null) {
  123. throw new IllegalArgumentException("Activity may not be null");
  124. }
  125. parentFolder = arguments.getParcelable(ARG_PARENT_FOLDER);
  126. creator = arguments.getParcelable(ARG_CREATOR);
  127. title = arguments.getString(ARG_HEADLINE, getString(R.string.select_template));
  128. if (savedInstanceState == null) {
  129. title = arguments.getString(ARG_HEADLINE);
  130. } else {
  131. title = savedInstanceState.getString(ARG_HEADLINE);
  132. }
  133. // Inflate the layout for the dialog
  134. LayoutInflater inflater = requireActivity().getLayoutInflater();
  135. binding = ChooseTemplateBinding.inflate(inflater, null, false);
  136. View view = binding.getRoot();
  137. binding.filename.requestFocus();
  138. ThemeUtils.colorTextInput(binding.filenameContainer, binding.filename, ThemeUtils.primaryColor(getContext()));
  139. binding.filename.setOnKeyListener((v, keyCode, event) -> {
  140. checkEnablingCreateButton();
  141. return false;
  142. });
  143. binding.filename.addTextChangedListener(new TextWatcher() {
  144. @Override
  145. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  146. // generated method stub
  147. }
  148. @Override
  149. public void onTextChanged(CharSequence s, int start, int before, int count) {
  150. // generated method stub
  151. }
  152. @Override
  153. public void afterTextChanged(Editable s) {
  154. checkEnablingCreateButton();
  155. }
  156. });
  157. try {
  158. User user = currentAccount.getUser();
  159. new FetchTemplateTask(this, clientFactory, user, creator).execute();
  160. } catch (Exception e) {
  161. Log_OC.e(TAG, "Loading stream url not possible: " + e);
  162. }
  163. binding.list.setHasFixedSize(true);
  164. binding.list.setLayoutManager(new GridLayoutManager(activity, 2));
  165. adapter = new TemplateAdapter(creator.getMimetype(), this, getContext(), currentAccount, clientFactory);
  166. binding.list.setAdapter(adapter);
  167. // Build the dialog
  168. AlertDialog.Builder builder = new AlertDialog.Builder(activity);
  169. builder.setView(view)
  170. .setPositiveButton(R.string.create, null)
  171. .setNeutralButton(R.string.common_cancel, null)
  172. .setTitle(title);
  173. Dialog dialog = builder.create();
  174. Window window = dialog.getWindow();
  175. if (window != null) {
  176. window.setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
  177. }
  178. return dialog;
  179. }
  180. @Override
  181. public void onDestroyView() {
  182. super.onDestroyView();
  183. binding = null;
  184. }
  185. @Override
  186. public void onSaveInstanceState(@NonNull Bundle savedInstanceState) {
  187. super.onSaveInstanceState(savedInstanceState);
  188. savedInstanceState.putString(ARG_HEADLINE, title);
  189. }
  190. private void createFromTemplate(Template template, String path) {
  191. new CreateFileFromTemplateTask(this, clientFactory, currentAccount.getUser(), template, path, creator).execute();
  192. }
  193. public void setTemplateList(TemplateList templateList) {
  194. adapter.setTemplateList(templateList);
  195. adapter.notifyDataSetChanged();
  196. }
  197. @Override
  198. public void onClick(Template template) {
  199. onTemplateChosen(template);
  200. }
  201. private void onTemplateChosen(Template template) {
  202. adapter.setTemplateAsActive(template);
  203. prefillFilenameIfEmpty(template);
  204. checkEnablingCreateButton();
  205. }
  206. private void prefillFilenameIfEmpty(Template template) {
  207. String name = binding.filename.getText().toString();
  208. if (name.isEmpty() || name.equalsIgnoreCase(DOT + template.getExtension())) {
  209. binding.filename.setText(String.format("%s.%s", template.title, template.extension));
  210. }
  211. binding.filename.setSelection(binding.filename.getText().toString().lastIndexOf('.'));
  212. }
  213. @Override
  214. public void onClick(View v) {
  215. String name = binding.filename.getText().toString();
  216. String path = parentFolder.getRemotePath() + name;
  217. Template selectedTemplate = adapter.getSelectedTemplate();
  218. if (selectedTemplate == null) {
  219. DisplayUtils.showSnackMessage(binding.list, R.string.select_one_template);
  220. } else if (name.isEmpty() || name.equalsIgnoreCase(DOT + selectedTemplate.getExtension())) {
  221. DisplayUtils.showSnackMessage(binding.list, R.string.enter_filename);
  222. } else if (!name.endsWith(selectedTemplate.getExtension())) {
  223. createFromTemplate(selectedTemplate, path + DOT + selectedTemplate.getExtension());
  224. } else {
  225. createFromTemplate(selectedTemplate, path);
  226. }
  227. }
  228. private void checkEnablingCreateButton() {
  229. Template selectedTemplate = adapter.getSelectedTemplate();
  230. String name = binding.filename.getText().toString();
  231. positiveButton.setEnabled(selectedTemplate != null && !name.isEmpty() &&
  232. !name.equalsIgnoreCase(DOT + selectedTemplate.getExtension()));
  233. }
  234. private static class CreateFileFromTemplateTask extends AsyncTask<Void, Void, String> {
  235. private ClientFactory clientFactory;
  236. private WeakReference<ChooseTemplateDialogFragment> chooseTemplateDialogFragmentWeakReference;
  237. private Template template;
  238. private String path;
  239. private Creator creator;
  240. private User user;
  241. private OCFile file;
  242. CreateFileFromTemplateTask(ChooseTemplateDialogFragment chooseTemplateDialogFragment,
  243. ClientFactory clientFactory,
  244. User user,
  245. Template template,
  246. String path,
  247. Creator creator
  248. ) {
  249. this.clientFactory = clientFactory;
  250. this.chooseTemplateDialogFragmentWeakReference = new WeakReference<>(chooseTemplateDialogFragment);
  251. this.template = template;
  252. this.path = path;
  253. this.creator = creator;
  254. this.user = user;
  255. }
  256. @Override
  257. protected String doInBackground(Void... voids) {
  258. try {
  259. OwnCloudClient client = clientFactory.create(user);
  260. RemoteOperationResult result =
  261. new DirectEditingCreateFileRemoteOperation(path,
  262. creator.getEditor(),
  263. creator.getId(),
  264. template.getTitle()).execute(client);
  265. if (!result.isSuccess()) {
  266. return "";
  267. }
  268. RemoteOperationResult newFileResult = new ReadFileRemoteOperation(path).execute(client);
  269. if (!newFileResult.isSuccess()) {
  270. return "";
  271. }
  272. final ChooseTemplateDialogFragment fragment = chooseTemplateDialogFragmentWeakReference.get();
  273. if (fragment == null) {
  274. return "";
  275. }
  276. final Context context = fragment.getContext();
  277. if (context == null) {
  278. // fragment has been detached
  279. return "";
  280. }
  281. FileDataStorageManager storageManager = new FileDataStorageManager(user.toPlatformAccount(),
  282. context.getContentResolver());
  283. OCFile temp = FileStorageUtils.fillOCFile((RemoteFile) newFileResult.getData().get(0));
  284. storageManager.saveFile(temp);
  285. file = storageManager.getFileByPath(path);
  286. return result.getData().get(0).toString();
  287. } catch (ClientFactory.CreationException e) {
  288. Log_OC.e(TAG, "Error creating file from template!", e);
  289. return "";
  290. }
  291. }
  292. @Override
  293. protected void onPostExecute(String url) {
  294. final ChooseTemplateDialogFragment fragment = chooseTemplateDialogFragmentWeakReference.get();
  295. if (fragment != null && fragment.isAdded()) {
  296. if (url.isEmpty()) {
  297. DisplayUtils.showSnackMessage(fragment.binding.list, "Error creating file from template");
  298. } else {
  299. Intent editorWebView = new Intent(MainApp.getAppContext(), TextEditorWebView.class);
  300. editorWebView.putExtra(ExternalSiteWebView.EXTRA_TITLE, "Text");
  301. editorWebView.putExtra(ExternalSiteWebView.EXTRA_URL, url);
  302. editorWebView.putExtra(ExternalSiteWebView.EXTRA_FILE, file);
  303. editorWebView.putExtra(ExternalSiteWebView.EXTRA_SHOW_SIDEBAR, false);
  304. fragment.startActivity(editorWebView);
  305. fragment.dismiss();
  306. }
  307. } else {
  308. Log_OC.e(TAG, "Error creating file from template!");
  309. }
  310. }
  311. }
  312. private static class FetchTemplateTask extends AsyncTask<Void, Void, TemplateList> {
  313. private User user;
  314. private ClientFactory clientFactory;
  315. private WeakReference<ChooseTemplateDialogFragment> chooseTemplateDialogFragmentWeakReference;
  316. private Creator creator;
  317. FetchTemplateTask(ChooseTemplateDialogFragment chooseTemplateDialogFragment,
  318. ClientFactory clientFactory,
  319. User user,
  320. Creator creator) {
  321. this.user = user;
  322. this.clientFactory = clientFactory;
  323. this.chooseTemplateDialogFragmentWeakReference = new WeakReference<>(chooseTemplateDialogFragment);
  324. this.creator = creator;
  325. }
  326. @Override
  327. protected TemplateList doInBackground(Void... voids) {
  328. try {
  329. OwnCloudClient client = clientFactory.create(user);
  330. RemoteOperationResult result = new DirectEditingObtainListOfTemplatesRemoteOperation(creator.getEditor(),
  331. creator.getId())
  332. .execute(client);
  333. if (!result.isSuccess()) {
  334. return new TemplateList();
  335. }
  336. return (TemplateList) result.getSingleData();
  337. } catch (ClientFactory.CreationException e) {
  338. Log_OC.e(TAG, "Could not fetch template", e);
  339. return new TemplateList();
  340. }
  341. }
  342. @Override
  343. protected void onPostExecute(TemplateList templateList) {
  344. ChooseTemplateDialogFragment fragment = chooseTemplateDialogFragmentWeakReference.get();
  345. if (fragment != null && fragment.isAdded()) {
  346. if (templateList.templates.isEmpty()) {
  347. DisplayUtils.showSnackMessage(fragment.binding.list, R.string.error_retrieving_templates);
  348. } else {
  349. if (templateList.templates.size() == SINGLE_TEMPLATE) {
  350. fragment.onTemplateChosen(templateList.templates.values().iterator().next());
  351. fragment.binding.list.setVisibility(View.GONE);
  352. } else {
  353. String name = DOT + templateList.templates.values().iterator().next().getExtension();
  354. fragment.binding.filename.setText(name);
  355. fragment.binding.helperText.setVisibility(View.VISIBLE);
  356. }
  357. fragment.setTemplateList(templateList);
  358. }
  359. } else {
  360. Log_OC.e(TAG, "Error streaming file: no previewMediaFragment!");
  361. }
  362. }
  363. }
  364. }