/*
*
* Nextcloud Android client application
*
* @author Tobias Kaminsky
* Copyright (C) 2019 Tobias Kaminsky
* Copyright (C) 2019 Nextcloud GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
package com.owncloud.android.ui.preview;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;
import com.nextcloud.client.account.User;
import com.nextcloud.client.account.UserAccountManager;
import com.owncloud.android.R;
import com.owncloud.android.datamodel.OCFile;
import com.owncloud.android.files.FileMenuFilter;
import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.ui.dialog.ConfirmationDialogFragment;
import com.owncloud.android.ui.dialog.RemoveFilesDialogFragment;
import com.owncloud.android.utils.DisplayUtils;
import com.owncloud.android.utils.MimeTypeUtil;
import org.mozilla.universalchardet.ReaderFactory;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.io.StringWriter;
import java.lang.ref.WeakReference;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import javax.inject.Inject;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.SearchView;
import androidx.core.view.MenuItemCompat;
public class PreviewTextFileFragment extends PreviewTextFragment {
private static final String EXTRA_FILE = "FILE";
private static final String EXTRA_USER = "USER";
private static final String EXTRA_OPEN_SEARCH = "SEARCH";
private static final String EXTRA_SEARCH_QUERY = "SEARCH_QUERY";
private static final String TAG = PreviewTextFileFragment.class.getSimpleName();
private TextLoadAsyncTask textLoadAsyncTask;
private User user;
@Inject UserAccountManager accountManager;
public static PreviewTextFileFragment create(User user, OCFile file, boolean openSearch, String searchQuery) {
Bundle args = new Bundle();
args.putParcelable(EXTRA_FILE, file);
args.putParcelable(EXTRA_USER, user);
args.putBoolean(EXTRA_OPEN_SEARCH, openSearch);
args.putString(EXTRA_SEARCH_QUERY, searchQuery);
PreviewTextFileFragment fragment = new PreviewTextFileFragment();
fragment.setArguments(args);
return fragment;
}
/**
* Creates an empty fragment for previews.
*
* MUST BE KEPT: the system uses it when tries to re-instantiate a fragment automatically (for instance, when the
* device is turned a aside).
*
* DO NOT CALL IT: an {@link OCFile} and {@link User} must be provided for a successful construction
*/
public PreviewTextFileFragment() {
super();
user = null;
}
/**
* {@inheritDoc}
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
OCFile file = getFile();
Bundle args = getArguments();
if (file == null) {
file = args.getParcelable(EXTRA_FILE);
}
if (user == null) {
user = args.getParcelable(EXTRA_USER);
}
if (args.containsKey(EXTRA_SEARCH_QUERY)) {
searchQuery = args.getString(EXTRA_SEARCH_QUERY);
}
searchOpen = args.getBoolean(EXTRA_OPEN_SEARCH, false);
if (savedInstanceState == null) {
if (file == null) {
throw new IllegalStateException("Instanced with a NULL OCFile");
}
if (user == null) {
throw new IllegalStateException("Instanced with a NULL ownCloud Account");
}
} else {
file = savedInstanceState.getParcelable(EXTRA_FILE);
user = savedInstanceState.getParcelable(EXTRA_USER);
}
handler = new Handler();
setFile(file);
}
/**
* {@inheritDoc}
*/
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
outState.putParcelable(PreviewTextFileFragment.EXTRA_FILE, getFile());
outState.putParcelable(PreviewTextFileFragment.EXTRA_USER, user);
super.onSaveInstanceState(outState);
}
@Override
void loadAndShowTextPreview() {
textLoadAsyncTask = new TextLoadAsyncTask(new WeakReference<>(binding.textPreview),
new WeakReference<>(binding.emptyListProgress));
textLoadAsyncTask.execute(getFile().getStoragePath());
}
/**
* Reads the file to preview and shows its contents. Too critical to be anonymous.
*/
private class TextLoadAsyncTask extends AsyncTask