2022-02-25 22:26:22 +01:00
|
|
|
package app.grapheneos.pdfviewer.loader;
|
2019-06-28 05:22:08 +02:00
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.Typeface;
|
|
|
|
import android.text.SpannableStringBuilder;
|
|
|
|
import android.text.Spanned;
|
|
|
|
import android.text.style.StyleSpan;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import androidx.loader.content.AsyncTaskLoader;
|
|
|
|
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
import java.text.ParseException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2022-02-25 22:26:22 +01:00
|
|
|
import app.grapheneos.pdfviewer.R;
|
|
|
|
import app.grapheneos.pdfviewer.Utils;
|
2019-06-28 05:22:08 +02:00
|
|
|
|
|
|
|
public class DocumentPropertiesLoader extends AsyncTaskLoader<List<CharSequence>> {
|
|
|
|
public static final String TAG = "DocumentPropertiesLoader";
|
|
|
|
|
|
|
|
public static final int ID = 1;
|
|
|
|
|
|
|
|
private final String mProperties;
|
|
|
|
private final int mNumPages;
|
|
|
|
|
2022-02-18 15:03:53 +01:00
|
|
|
String fileName;
|
|
|
|
Long fileSize;
|
2019-06-28 05:22:08 +02:00
|
|
|
|
2022-02-18 15:03:53 +01:00
|
|
|
public DocumentPropertiesLoader(Context context, String properties, int numPages, String fileName, Long fileSize) {
|
2019-06-28 05:22:08 +02:00
|
|
|
super(context);
|
|
|
|
|
|
|
|
mProperties = properties;
|
|
|
|
mNumPages = numPages;
|
2022-02-18 15:03:53 +01:00
|
|
|
this.fileName = fileName;
|
|
|
|
this.fileSize = fileSize;
|
2019-06-28 05:22:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<CharSequence> loadInBackground() {
|
|
|
|
final Context context = getContext();
|
|
|
|
|
|
|
|
final String[] names = context.getResources().getStringArray(R.array.property_names);
|
|
|
|
final List<CharSequence> properties = new ArrayList<>(names.length);
|
|
|
|
|
2022-02-18 15:03:53 +01:00
|
|
|
properties.add(getProperty(null, names[0], fileName));
|
|
|
|
properties.add(getProperty(null, names[1], Utils.parseFileSize(fileSize)));
|
2019-06-28 05:22:08 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
final JSONObject json = new JSONObject(mProperties);
|
|
|
|
|
|
|
|
properties.add(getProperty(json, names[2], "Title"));
|
|
|
|
properties.add(getProperty(json, names[3], "Author"));
|
|
|
|
properties.add(getProperty(json, names[4], "Subject"));
|
|
|
|
properties.add(getProperty(json, names[5], "Keywords"));
|
|
|
|
properties.add(getProperty(json, names[6], "CreationDate"));
|
|
|
|
properties.add(getProperty(json, names[7], "ModDate"));
|
|
|
|
properties.add(getProperty(json, names[8], "Producer"));
|
|
|
|
properties.add(getProperty(json, names[9], "Creator"));
|
|
|
|
properties.add(getProperty(json, names[10], "PDFFormatVersion"));
|
|
|
|
properties.add(getProperty(null, names[11], String.valueOf(mNumPages)));
|
|
|
|
|
|
|
|
return properties;
|
|
|
|
} catch (JSONException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void deliverResult(List<CharSequence> properties) {
|
2022-02-18 15:03:53 +01:00
|
|
|
if (!isReset() && isStarted()) {
|
2019-06-28 05:22:08 +02:00
|
|
|
super.deliverResult(properties);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onStartLoading() {
|
|
|
|
forceLoad();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onStopLoading() {
|
|
|
|
cancelLoad();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onReset() {
|
|
|
|
super.onReset();
|
|
|
|
|
|
|
|
onStopLoading();
|
|
|
|
}
|
|
|
|
|
|
|
|
private CharSequence getProperty(final JSONObject json, String name, String specName) {
|
|
|
|
final SpannableStringBuilder property = new SpannableStringBuilder(name).append(":\n");
|
|
|
|
final String value = json != null ? json.optString(specName, "-") : specName;
|
|
|
|
|
2022-10-02 16:10:55 +02:00
|
|
|
if (specName != null && specName.endsWith("Date")) {
|
2019-06-28 05:22:08 +02:00
|
|
|
final Context context = getContext();
|
|
|
|
try {
|
|
|
|
property.append(value.equals("-") ? value : Utils.parseDate(value));
|
|
|
|
} catch (ParseException e) {
|
|
|
|
Log.w(TAG, e.getMessage() + " for " + value + " at offset: " + e.getErrorOffset());
|
|
|
|
property.append(context.getString(R.string.document_properties_invalid_date));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
property.append(value);
|
|
|
|
}
|
|
|
|
property.setSpan(new StyleSpan(Typeface.BOLD), 0, name.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
|
|
|
|
return property;
|
|
|
|
}
|
|
|
|
}
|