Skip to content

Commit

Permalink
Merge pull request #1647 from TranceLove/bugfix/issue1456
Browse files Browse the repository at this point in the history
Use DateUtils to format date
  • Loading branch information
EmmanuelMess committed Aug 26, 2019
1 parent 3b37b94 commit 06d34d9
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public void onBindViewHolder(final CompressedItemViewHolder holder, int position

final StringBuilder stringBuilder = new StringBuilder(rowItem.name);
if (compressedExplorerFragment.showLastModified)
holder.date.setText(Utils.getDate(rowItem.date, compressedExplorerFragment.year));
holder.date.setText(Utils.getDate(context, rowItem.date, compressedExplorerFragment.year));
if (rowItem.directory) {
holder.genericIcon.setImageDrawable(folder);
gradientDrawable.setColor(compressedExplorerFragment.iconskin);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@

package com.amaze.filemanager.adapters.data;

import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.amaze.filemanager.filesystem.HybridFileParcelable;
import com.amaze.filemanager.ui.icons.Icons;
Expand Down Expand Up @@ -52,25 +55,25 @@ public class LayoutElementParcelable implements Parcelable {
//same as hfile.modes but different than openmode in Main.java
private OpenMode mode = OpenMode.FILE;

public LayoutElementParcelable(boolean isBack, String goback, boolean showThumbs) {
this(true, new File("..").getName(), "..", "", "", goback, 0,
public LayoutElementParcelable(@NonNull Context c, boolean isBack, String goback, boolean showThumbs) {
this(c,true, new File("..").getName(), "..", "", "", goback, 0,
false, "", true, showThumbs, OpenMode.UNKNOWN);
}

public LayoutElementParcelable(String path, String permissions, String symlink,
public LayoutElementParcelable(@NonNull Context c, String path, String permissions, String symlink,
String size, long longSize,boolean header, String date,
boolean isDirectory, boolean useThumbs, OpenMode openMode) {
this(new File(path).getName(), path, permissions, symlink, size, longSize, header,
this(c, new File(path).getName(), path, permissions, symlink, size, longSize, header,
date, isDirectory, useThumbs, openMode);
}

public LayoutElementParcelable(String title, String path, String permissions,
public LayoutElementParcelable(@NonNull Context c, String title, String path, String permissions,
String symlink, String size, long longSize, boolean header,
String date, boolean isDirectory, boolean useThumbs, OpenMode openMode) {
this(false, title, path, permissions, symlink, size, longSize, header, date, isDirectory, useThumbs, openMode);
this(c,false, title, path, permissions, symlink, size, longSize, header, date, isDirectory, useThumbs, openMode);
}

public LayoutElementParcelable(boolean isBack, String title, String path, String permissions,
public LayoutElementParcelable(@NonNull Context c, boolean isBack, String title, String path, String permissions,
String symlink, String size, long longSize, boolean header,
String date, boolean isDirectory, boolean useThumbs, OpenMode openMode) {
filetype = Icons.getTypeOfFile(path, isDirectory);
Expand Down Expand Up @@ -111,7 +114,7 @@ public LayoutElementParcelable(boolean isBack, String title, String path, String
this.isDirectory = isDirectory;
if (!date.trim().equals("")) {
this.date = Long.parseLong(date);
this.date1 = Utils.getDate(this.date, CURRENT_YEAR);
this.date1 = Utils.getDate(c, this.date, CURRENT_YEAR);
} else {
this.date = 0;
this.date1 = "";
Expand Down Expand Up @@ -143,6 +146,7 @@ public String toString() {
return title + "\n" + desc;
}

//Hopefully it should be safe - nobody else is using this
public LayoutElementParcelable(Parcel im) {
filetype = im.readInt();
iconData = im.readParcelable(IconDataParcelable.class.getClassLoader());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ private LayoutElementParcelable createListParcelables(HybridFileParcelable baseF
ma.file_count++;
}

LayoutElementParcelable layoutElement = new LayoutElementParcelable(baseFile.getName(),
LayoutElementParcelable layoutElement = new LayoutElementParcelable(c, baseFile.getName(),
baseFile.getPath(), baseFile.getPermission(), baseFile.getLink(), size,
longSize, false, baseFile.getDate() + "", baseFile.isDirectory(),
showThumbs, baseFile.getMode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void handleMessage(Message msg) {
case CustomFileObserver.NEW_ITEM:
HybridFile fileCreated = new HybridFile(main.openMode,
main.getCurrentPath() + "/" + path);
main.getElementsList().add(fileCreated.generateLayoutElement(useThumbs));
main.getElementsList().add(fileCreated.generateLayoutElement(main.getContext(), useThumbs));
break;
case CustomFileObserver.DELETED_ITEM:
for (int i = 0; i < main.getElementsList().size(); i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1255,19 +1255,19 @@ public String getNameString(Context context) {
* Generates a {@link LayoutElementParcelable} adapted compatible element.
* Currently supports only local filesystem
*/
public LayoutElementParcelable generateLayoutElement(boolean showThumbs) {
public LayoutElementParcelable generateLayoutElement(@NonNull Context c, boolean showThumbs) {
switch (mode) {
case FILE:
case ROOT:
File file = new File(path);
LayoutElementParcelable layoutElement;
if (isDirectory()) {

layoutElement = new LayoutElementParcelable(path, RootHelper.parseFilePermission(file),
layoutElement = new LayoutElementParcelable(c, path, RootHelper.parseFilePermission(file),
"", folderSize() + "", 0, true, file.lastModified() + "",
false, showThumbs, mode);
} else {
layoutElement = new LayoutElementParcelable(
layoutElement = new LayoutElementParcelable(c,
file.getPath(), RootHelper.parseFilePermission(file),
file.getPath(), file.length() + "", file.length(), false, file.lastModified() + "",
false, showThumbs, mode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1164,7 +1164,7 @@ public void reloadListElements(boolean back, boolean results, boolean grid) {

private LayoutElementParcelable getBackElement() {
if (back == null) {
back = new LayoutElementParcelable(true, getString(R.string.goback), getBoolean(PREFERENCE_SHOW_THUMB));
back = new LayoutElementParcelable(getContext(), true, getString(R.string.goback), getBoolean(PREFERENCE_SHOW_THUMB));
}

return back;
Expand Down Expand Up @@ -1469,7 +1469,7 @@ public ArrayList<LayoutElementParcelable> addToSmb(SmbFile[] mFile, String path,
if (aMFile.isDirectory()) {
folder_count++;

LayoutElementParcelable layoutElement = new LayoutElementParcelable(name, aMFile.getPath(),
LayoutElementParcelable layoutElement = new LayoutElementParcelable(getContext(), name, aMFile.getPath(),
"", "", "", 0, false,
aMFile.lastModified() + "", true,
getBoolean(PREFERENCE_SHOW_THUMB), OpenMode.SMB);
Expand All @@ -1479,7 +1479,7 @@ public ArrayList<LayoutElementParcelable> addToSmb(SmbFile[] mFile, String path,

} else {
file_count++;
LayoutElementParcelable layoutElement = new LayoutElementParcelable(name,
LayoutElementParcelable layoutElement = new LayoutElementParcelable(getContext(), name,
aMFile.getPath(), "", "", Formatter.formatFileSize(getContext(),
aMFile.length()), aMFile.length(), false, aMFile.lastModified() + "",
false, getBoolean(PREFERENCE_SHOW_THUMB), OpenMode.SMB);
Expand All @@ -1498,7 +1498,7 @@ private LayoutElementParcelable addTo(HybridFileParcelable mFile) {
if (!dataUtils.isFileHidden(mFile.getPath())) {
if (mFile.isDirectory()) {
size = "";
LayoutElementParcelable layoutElement = new LayoutElementParcelable(f.getPath(), mFile.getPermission(),
LayoutElementParcelable layoutElement = new LayoutElementParcelable(getContext(), f.getPath(), mFile.getPermission(),
mFile.getLink(), size, 0, true,
mFile.getDate() + "", false,
getBoolean(PREFERENCE_SHOW_THUMB), mFile.getMode());
Expand All @@ -1520,7 +1520,7 @@ private LayoutElementParcelable addTo(HybridFileParcelable mFile) {
//e.printStackTrace();
}
try {
LayoutElementParcelable layoutElement = new LayoutElementParcelable(f.getPath(),
LayoutElementParcelable layoutElement = new LayoutElementParcelable(getContext(), f.getPath(),
mFile.getPermission(), mFile.getLink(), size, longSize, false,
mFile.getDate() + "", false,
getBoolean(PREFERENCE_SHOW_THUMB), mFile.getMode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ public void setupDialog(Dialog dialog, int style) {
mFileLocationTextView = rootView.findViewById(R.id.text_view_file_location);
mFileLocationTextView.setText(mFile.getPath());
mFileAccessedTextView = rootView.findViewById(R.id.text_view_file_accessed);
mFileAccessedTextView.setText(Utils.getDate(mFile.getDate()));
mFileAccessedTextView.setText(Utils.getDate(getContext(), mFile.getDate()));
mFileModifiedTextView = rootView.findViewById(R.id.text_view_file_modified);
mFileModifiedTextView.setText(Utils.getDate(mFile.getDate()));
mFileModifiedTextView.setText(Utils.getDate(getContext(), mFile.getDate()));

CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) ((View) rootView.getParent()).getLayoutParams();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ private static void showPropertiesDialog(final HybridFileParcelable baseFile, fi
final Context c = base.getApplicationContext();
int accentColor = base.getAccent();
long last = baseFile.getDate();
final String date = Utils.getDate(last),
final String date = Utils.getDate(base, last),
items = c.getString(R.string.calculating),
name = baseFile.getName(),
parent = baseFile.getReadablePath(baseFile.getParent(c));
Expand Down
23 changes: 13 additions & 10 deletions app/src/main/java/com/amaze/filemanager/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
import android.net.Uri;
import android.os.Build;
import android.support.annotation.ColorRes;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.text.format.DateUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
Expand All @@ -22,7 +24,6 @@
import com.amaze.filemanager.filesystem.HybridFileParcelable;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.concurrent.TimeUnit;

/**
Expand All @@ -35,12 +36,11 @@
public class Utils {

private static final int INDEX_NOT_FOUND = -1;
private static final SimpleDateFormat DATE_NO_MINUTES = new SimpleDateFormat("MMM dd, yyyy");
private static final SimpleDateFormat DATE_WITH_MINUTES = new SimpleDateFormat("MMM dd yyyy | KK:mm a");
private static final String INPUT_INTENT_BLACKLIST_COLON = ";";
private static final String INPUT_INTENT_BLACKLIST_PIPE = "\\|";
private static final String INPUT_INTENT_BLACKLIST_AMP = "&&";
private static final String INPUT_INTENT_BLACKLIST_DOTS = "\\.\\.\\.";
private static final String DATE_TIME_FORMAT = "%s | %s %s";

//methods for fastscroller
public static float clamp(float min, float max, float value) {
Expand Down Expand Up @@ -72,15 +72,17 @@ public static void setTint(Context context, CheckBox box, int color) {
}
}

public static String getDate(long f) {
return DATE_WITH_MINUTES.format(f);
public static String getDate(@NonNull Context c, long f) {
return String.format(DATE_TIME_FORMAT,
DateUtils.formatDateTime(c, f, DateUtils.FORMAT_SHOW_DATE),
DateUtils.formatDateTime(c, f, DateUtils.FORMAT_SHOW_TIME));
}

public static String getDate(long f, String year) {
String date = DATE_NO_MINUTES.format(f);
if (date.substring(date.length() - 4, date.length()).equals(year))
date = date.substring(0, date.length() - 6);
return date;
public static String getDate(@NonNull Context c, long f, @NonNull String year) {
return String.format(DATE_TIME_FORMAT,
DateUtils.formatDateTime(c, f, DateUtils.FORMAT_SHOW_DATE),
DateUtils.formatDateTime(c, f, DateUtils.FORMAT_SHOW_TIME),
year);
}

/**
Expand All @@ -89,6 +91,7 @@ public static String getDate(long f, String year) {
* @param color the resource id for the color
* @return the color
*/
@SuppressWarnings("deprecation")
public static int getColor(Context c, @ColorRes int color) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return c.getColor(color);
Expand Down

0 comments on commit 06d34d9

Please sign in to comment.