ChooseRichDocumentsTemplateDialogFragment.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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.accounts.Account;
  24. import android.annotation.SuppressLint;
  25. import android.app.Activity;
  26. import android.app.Dialog;
  27. import android.content.DialogInterface;
  28. import android.content.Intent;
  29. import android.graphics.PorterDuff;
  30. import android.os.AsyncTask;
  31. import android.os.Bundle;
  32. import android.view.LayoutInflater;
  33. import android.view.View;
  34. import android.view.Window;
  35. import android.view.WindowManager.LayoutParams;
  36. import android.widget.EditText;
  37. import com.nextcloud.client.account.CurrentAccountProvider;
  38. import com.nextcloud.client.account.User;
  39. import com.nextcloud.client.di.Injectable;
  40. import com.nextcloud.client.network.ClientFactory;
  41. import com.owncloud.android.MainApp;
  42. import com.owncloud.android.R;
  43. import com.owncloud.android.datamodel.FileDataStorageManager;
  44. import com.owncloud.android.datamodel.OCFile;
  45. import com.owncloud.android.datamodel.Template;
  46. import com.owncloud.android.files.CreateFileFromTemplateOperation;
  47. import com.owncloud.android.files.FetchTemplateOperation;
  48. import com.owncloud.android.lib.common.OwnCloudAccount;
  49. import com.owncloud.android.lib.common.OwnCloudClient;
  50. import com.owncloud.android.lib.common.OwnCloudClientManagerFactory;
  51. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  52. import com.owncloud.android.lib.common.utils.Log_OC;
  53. import com.owncloud.android.lib.resources.files.ReadFileRemoteOperation;
  54. import com.owncloud.android.lib.resources.files.model.RemoteFile;
  55. import com.owncloud.android.ui.activity.ExternalSiteWebView;
  56. import com.owncloud.android.ui.activity.RichDocumentsEditorWebView;
  57. import com.owncloud.android.ui.adapter.RichDocumentsTemplateAdapter;
  58. import com.owncloud.android.utils.DisplayUtils;
  59. import com.owncloud.android.utils.FileStorageUtils;
  60. import com.owncloud.android.utils.NextcloudServer;
  61. import com.owncloud.android.utils.ThemeUtils;
  62. import org.parceler.Parcels;
  63. import java.lang.ref.WeakReference;
  64. import java.util.ArrayList;
  65. import java.util.List;
  66. import javax.inject.Inject;
  67. import androidx.annotation.NonNull;
  68. import androidx.appcompat.app.AlertDialog;
  69. import androidx.fragment.app.DialogFragment;
  70. import androidx.recyclerview.widget.GridLayoutManager;
  71. import androidx.recyclerview.widget.RecyclerView;
  72. import butterknife.BindView;
  73. import butterknife.ButterKnife;
  74. /**
  75. * Dialog to show templates for new documents/spreadsheets/presentations.
  76. */
  77. public class ChooseRichDocumentsTemplateDialogFragment extends DialogFragment implements DialogInterface.OnClickListener,
  78. RichDocumentsTemplateAdapter.ClickListener, Injectable {
  79. private static final String ARG_PARENT_FOLDER = "PARENT_FOLDER";
  80. private static final String ARG_TYPE = "TYPE";
  81. private static final String TAG = ChooseRichDocumentsTemplateDialogFragment.class.getSimpleName();
  82. private static final String DOT = ".";
  83. private RichDocumentsTemplateAdapter adapter;
  84. private OCFile parentFolder;
  85. private OwnCloudClient client;
  86. @Inject CurrentAccountProvider currentAccount;
  87. @Inject ClientFactory clientFactory;
  88. public enum Type {
  89. DOCUMENT,
  90. SPREADSHEET,
  91. PRESENTATION
  92. }
  93. @BindView(R.id.list)
  94. RecyclerView listView;
  95. @BindView(R.id.filename)
  96. EditText fileName;
  97. @NextcloudServer(max = 18) // will be removed in favor of generic direct editing
  98. public static ChooseRichDocumentsTemplateDialogFragment newInstance(OCFile parentFolder, Type type) {
  99. ChooseRichDocumentsTemplateDialogFragment frag = new ChooseRichDocumentsTemplateDialogFragment();
  100. Bundle args = new Bundle();
  101. args.putParcelable(ARG_PARENT_FOLDER, parentFolder);
  102. args.putString(ARG_TYPE, type.name());
  103. frag.setArguments(args);
  104. return frag;
  105. }
  106. @Override
  107. public void onStart() {
  108. super.onStart();
  109. int color = ThemeUtils.primaryAccentColor(getContext());
  110. AlertDialog alertDialog = (AlertDialog) getDialog();
  111. alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(color);
  112. alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(color);
  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. int accentColor = ThemeUtils.primaryAccentColor(getContext());
  126. parentFolder = arguments.getParcelable(ARG_PARENT_FOLDER);
  127. Type type = Type.valueOf(arguments.getString(ARG_TYPE));
  128. // Inflate the layout for the dialog
  129. LayoutInflater inflater = activity.getLayoutInflater();
  130. @SuppressLint("InflateParams") View view = inflater.inflate(R.layout.choose_template, null);
  131. ButterKnife.bind(this, view);
  132. fileName.requestFocus();
  133. fileName.getBackground().setColorFilter(accentColor, PorterDuff.Mode.SRC_ATOP);
  134. try {
  135. client = clientFactory.create(currentAccount.getUser());
  136. } catch (ClientFactory.CreationException e) {
  137. throw new RuntimeException(e); // we'll NPE without the client
  138. }
  139. new FetchTemplateTask(this, client).execute(type);
  140. listView.setHasFixedSize(true);
  141. listView.setLayoutManager(new GridLayoutManager(activity, 2));
  142. adapter = new RichDocumentsTemplateAdapter(type, this, getContext(), currentAccount, clientFactory);
  143. listView.setAdapter(adapter);
  144. // Build the dialog
  145. AlertDialog.Builder builder = new AlertDialog.Builder(activity);
  146. builder.setView(view)
  147. .setNegativeButton(R.string.common_cancel, this)
  148. .setTitle(ThemeUtils.getColoredTitle(getResources().getString(R.string.select_template), accentColor));
  149. Dialog dialog = builder.create();
  150. Window window = dialog.getWindow();
  151. if (window != null) {
  152. window.setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
  153. }
  154. return dialog;
  155. }
  156. private void createFromTemplate(Template template, String path) {
  157. new CreateFileFromTemplateTask(this, client, template, path, currentAccount.getUser()).execute();
  158. }
  159. public void setTemplateList(List<Template> templateList) {
  160. adapter.setTemplateList(templateList);
  161. adapter.notifyDataSetChanged();
  162. }
  163. @Override
  164. public void onClick(Template template) {
  165. String name = fileName.getText().toString();
  166. String path = parentFolder.getRemotePath() + name;
  167. if (name.isEmpty() || name.equalsIgnoreCase(DOT + template.getExtension())) {
  168. DisplayUtils.showSnackMessage(listView, R.string.enter_filename);
  169. } else if (!name.endsWith(template.getExtension())) {
  170. createFromTemplate(template, path + DOT + template.getExtension());
  171. } else {
  172. createFromTemplate(template, path);
  173. }
  174. }
  175. @Override
  176. public void onClick(DialogInterface dialog, int which) {
  177. // cancel is handled by dialog itself, no other button available
  178. }
  179. private static class CreateFileFromTemplateTask extends AsyncTask<Void, Void, String> {
  180. private OwnCloudClient client;
  181. private WeakReference<ChooseRichDocumentsTemplateDialogFragment> chooseTemplateDialogFragmentWeakReference;
  182. private Template template;
  183. private String path;
  184. private User user;
  185. private OCFile file;
  186. CreateFileFromTemplateTask(ChooseRichDocumentsTemplateDialogFragment chooseTemplateDialogFragment,
  187. OwnCloudClient client,
  188. Template template,
  189. String path,
  190. User user
  191. ) {
  192. this.client = client;
  193. this.chooseTemplateDialogFragmentWeakReference = new WeakReference<>(chooseTemplateDialogFragment);
  194. this.template = template;
  195. this.path = path;
  196. this.user = user;
  197. }
  198. @Override
  199. protected String doInBackground(Void... voids) {
  200. RemoteOperationResult result = new CreateFileFromTemplateOperation(path, template.getId()).execute(client);
  201. if (result.isSuccess()) {
  202. // get file
  203. RemoteOperationResult newFileResult = new ReadFileRemoteOperation(path).execute(client);
  204. if (newFileResult.isSuccess()) {
  205. OCFile temp = FileStorageUtils.fillOCFile((RemoteFile) newFileResult.getData().get(0));
  206. if (chooseTemplateDialogFragmentWeakReference.get() != null) {
  207. FileDataStorageManager storageManager = new FileDataStorageManager(
  208. user.toPlatformAccount(),
  209. chooseTemplateDialogFragmentWeakReference.get().requireContext().getContentResolver());
  210. storageManager.saveFile(temp);
  211. file = storageManager.getFileByPath(path);
  212. return result.getData().get(0).toString();
  213. } else {
  214. return "";
  215. }
  216. } else {
  217. return "";
  218. }
  219. } else {
  220. return "";
  221. }
  222. }
  223. @Override
  224. protected void onPostExecute(String url) {
  225. ChooseRichDocumentsTemplateDialogFragment fragment = chooseTemplateDialogFragmentWeakReference.get();
  226. if (fragment != null && fragment.isAdded()) {
  227. if (url.isEmpty()) {
  228. DisplayUtils.showSnackMessage(fragment.listView, "Error creating file from template");
  229. } else {
  230. Intent collaboraWebViewIntent = new Intent(MainApp.getAppContext(), RichDocumentsEditorWebView.class);
  231. collaboraWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_TITLE, "Collabora");
  232. collaboraWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_URL, url);
  233. collaboraWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_FILE, file);
  234. collaboraWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_SHOW_SIDEBAR, false);
  235. collaboraWebViewIntent.putExtra(ExternalSiteWebView.EXTRA_TEMPLATE, Parcels.wrap(template));
  236. fragment.startActivity(collaboraWebViewIntent);
  237. fragment.dismiss();
  238. }
  239. } else {
  240. Log_OC.e(TAG, "Error creating file from template!");
  241. }
  242. }
  243. }
  244. private static class FetchTemplateTask extends AsyncTask<Type, Void, List<Template>> {
  245. private OwnCloudClient client;
  246. private WeakReference<ChooseRichDocumentsTemplateDialogFragment> chooseTemplateDialogFragmentWeakReference;
  247. FetchTemplateTask(ChooseRichDocumentsTemplateDialogFragment chooseTemplateDialogFragment, OwnCloudClient client) {
  248. this.client = client;
  249. this.chooseTemplateDialogFragmentWeakReference = new WeakReference<>(chooseTemplateDialogFragment);
  250. }
  251. @Override
  252. protected List<Template> doInBackground(Type... type) {
  253. FetchTemplateOperation fetchTemplateOperation = new FetchTemplateOperation(type[0]);
  254. RemoteOperationResult result = fetchTemplateOperation.execute(client);
  255. if (!result.isSuccess()) {
  256. return new ArrayList<>();
  257. }
  258. List<Template> templateList = new ArrayList<>();
  259. for (Object object : result.getData()) {
  260. templateList.add((Template) object);
  261. }
  262. return templateList;
  263. }
  264. @Override
  265. protected void onPostExecute(List<Template> templateList) {
  266. ChooseRichDocumentsTemplateDialogFragment fragment = chooseTemplateDialogFragmentWeakReference.get();
  267. if (fragment != null) {
  268. if (templateList.isEmpty()) {
  269. DisplayUtils.showSnackMessage(fragment.listView, R.string.error_retrieving_templates);
  270. } else {
  271. fragment.setTemplateList(templateList);
  272. String name = DOT + templateList.get(0).getExtension();
  273. fragment.fileName.setText(name);
  274. }
  275. } else {
  276. Log_OC.e(TAG, "Error streaming file: no previewMediaFragment!");
  277. }
  278. }
  279. }
  280. }