Unverified Commit 7361d4bc authored by gsantner's avatar gsantner 🦀
Browse files

Update opoc

Update shared helper utilities of my projects to latest state
parent 1948c28c
......@@ -16,6 +16,7 @@ import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
......@@ -143,4 +144,17 @@ public abstract class GsFragmentBase extends Fragment {
public Menu getFragmentMenu() {
return _fragmentMenu;
}
/**
* Get the toolbar from activity
* Requires id to be set to @+id/toolbar
*/
@SuppressWarnings("ConstantConditions")
protected Toolbar getToolbar() {
try {
return (Toolbar) getActivity().findViewById(new ContextUtils(getActivity()).getResId(ContextUtils.ResType.ID, "toolbar"));
} catch (Exception e) {
return null;
}
}
}
......@@ -547,7 +547,7 @@ public class SharedPreferencesPropertyBackend implements PropertyBackend<String,
*/
public Date getDateOfDaysAgo(int days) {
Calendar cal = new GregorianCalendar();
cal.add(Calendar.DAY_OF_MONTH, -days);
cal.add(Calendar.DATE, -days);
return cal.getTime();
}
......
......@@ -11,8 +11,11 @@
package net.gsantner.opoc.ui;
import android.app.Activity;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Build;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
......@@ -23,6 +26,7 @@ import android.support.v7.widget.AppCompatEditText;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
......@@ -56,9 +60,14 @@ public class SearchOrCustomTextDialog {
public Callback.a1<String> callback;
public List<? extends CharSequence> data = new ArrayList<>();
public List<? extends CharSequence> highlightData = new ArrayList<>();
public List<Integer> iconsForData = new ArrayList<>();
public String messageText = "";
public boolean isSearchEnabled = true;
public boolean isDarkDialog = false;
public int dialogWidthDp = WindowManager.LayoutParams.MATCH_PARENT;
public int dialogHeightDp = WindowManager.LayoutParams.WRAP_CONTENT;
public int gravity = Gravity.NO_GRAVITY;
public int searchInputType = 0;
@ColorInt
public int textColor = 0xFF000000;
......@@ -89,6 +98,17 @@ public class SearchOrCustomTextDialog {
TextView textView = (TextView) super.getView(pos, convertView, parent);
String text = textView.getText().toString();
int posInOriginalList = dopt.data.indexOf(text);
if (posInOriginalList >= 0 && dopt.iconsForData != null && posInOriginalList < dopt.iconsForData.size() && dopt.iconsForData.get(posInOriginalList) != 0) {
textView.setCompoundDrawablesWithIntrinsicBounds(dopt.iconsForData.get(posInOriginalList), 0, 0, 0);
textView.setCompoundDrawablePadding(32);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
textView.setCompoundDrawableTintList(ColorStateList.valueOf(dopt.isDarkDialog ? Color.WHITE : Color.BLACK));
}
} else {
textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
}
boolean hl = dopt.highlightData.contains(text);
textView.setTextColor(hl ? dopt.highlightColor : dopt.textColor);
textView.setTypeface(null, hl ? Typeface.BOLD : Typeface.NORMAL);
......@@ -132,6 +152,7 @@ public class SearchOrCustomTextDialog {
searchEditText.setTextColor(dopt.textColor);
searchEditText.setHintTextColor((dopt.textColor & 0x00FFFFFF) | 0x99000000);
searchEditText.setHint(dopt.searchHintText);
searchEditText.setInputType(dopt.searchInputType == 0 ? searchEditText.getInputType() : dopt.searchInputType);
searchEditText.addTextChangedListener(new TextWatcher() {
@Override
......@@ -166,9 +187,11 @@ public class SearchOrCustomTextDialog {
dialogBuilder.setMessage(dopt.messageText);
}
dialogBuilder.setView(linearLayout)
.setTitle(dopt.titleText)
.setOnCancelListener(null)
.setNegativeButton(dopt.cancelButtonText, (dialogInterface, i) -> dialogInterface.dismiss());
if (dopt.titleText != 0) {
dialogBuilder.setTitle(dopt.titleText);
}
if (dopt.isSearchEnabled) {
dialogBuilder.setPositiveButton(dopt.okButtonText, (dialogInterface, i) -> {
dialogInterface.dismiss();
......@@ -204,7 +227,15 @@ public class SearchOrCustomTextDialog {
}
dialog.show();
if ((w = dialog.getWindow()) != null) {
w.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
int ds_w = dopt.dialogWidthDp < 100 ? dopt.dialogWidthDp : ((int) (dopt.dialogWidthDp * activity.getResources().getDisplayMetrics().density));
int ds_h = dopt.dialogHeightDp < 100 ? dopt.dialogHeightDp : ((int) (dopt.dialogHeightDp * activity.getResources().getDisplayMetrics().density));
w.setLayout(ds_w, ds_h);
}
if ((w = dialog.getWindow()) != null && dopt.gravity != Gravity.NO_GRAVITY) {
WindowManager.LayoutParams wlp = w.getAttributes();
wlp.gravity = dopt.gravity;
w.setAttributes(wlp);
}
if (dopt.isSearchEnabled) {
......
......@@ -12,6 +12,11 @@ package net.gsantner.opoc.util;
@SuppressWarnings("unused")
public class Callback {
public interface a0 {
void callback();
}
public interface a1<A> {
void callback(A arg1);
}
......
......@@ -29,6 +29,7 @@ import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URLConnection;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
......@@ -240,6 +241,30 @@ public class FileUtils {
}
}
public static boolean copyFile(final File src, final FileOutputStream os) {
InputStream is = null;
try {
try {
is = new FileInputStream(src);
byte[] buf = new byte[BUFFER_SIZE];
int len;
while ((len = is.read(buf)) > 0) {
os.write(buf, 0, len);
}
return true;
} finally {
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
}
} catch (IOException ex) {
return false;
}
}
// Returns -1 if the file did not contain any of the needles, otherwise,
// the index of which needle was found in the contents of the file.
//
......@@ -452,7 +477,15 @@ public class FileUtils {
}
String[] units = abbreviation ? new String[]{"B", "kB", "MB", "GB", "TB"} : new String[]{"Bytes", "Kilobytes", "Megabytes", "Gigabytes", "Terabytes"};
int unit = (int) (Math.log10(size) / Math.log10(1024));
return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, unit))
+ " " + units[unit];
return new DecimalFormat("#,##0.#", DecimalFormatSymbols.getInstance(Locale.ENGLISH)).format(size / Math.pow(1024, unit)) + " " + units[unit];
}
public static int[] getTimeDiffHMS(long now, long past) {
int[] ret = new int[3];
long diff = Math.abs(now - past);
ret[0] = (int) (diff / (1000 * 60 * 60)); // hours
ret[1] = (int) (diff / (1000 * 60)) % 60; // min
ret[2] = (int) (diff / 1000) % 60; // sec
return ret;
}
}
......@@ -77,7 +77,7 @@ public class NetworkUtils {
int written = 0;
final float invLength = 1f / connection.getContentLength();
byte data[] = new byte[BUFFER_SIZE];
byte[] data = new byte[BUFFER_SIZE];
while ((count = input.read(data)) != -1) {
output.write(data, 0, count);
if (invLength != -1f && progressCallback != null) {
......
......@@ -13,8 +13,8 @@ import java.text.SimpleDateFormat
buildscript {
ext {
version_gradle_tools = "3.5.1"
version_plugin_kotlin = "1.3.50"
version_gradle_tools = "3.5.2"
version_plugin_kotlin = "1.3.60"
enable_plugin_kotlin = false
version_compileSdk = 28
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment