Skip to content

Commit 9a071fa

Browse files
committed
Update opoc
1 parent 9b856c5 commit 9a071fa

File tree

6 files changed

+130
-20
lines changed

6 files changed

+130
-20
lines changed

Diff for: CHANGELOG.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
![](https://test.gsantner.net/matomo/piwik.php?action_name=inapp_changelog&idsite=2&rec=1&urlref=https%3A%2F%2Fgithub.com%2Fgsantner%2Fmemetastic%2FCHANGELOG.md&_cvar=%7B%221%22%3A%5B%22source%22%2C%22changelog%22%5D%2C%222%22%3A%5B%22project%22%2C%22memetastic%22%5D%2C%223%22%3A%5B%22packageid%22%2C%22io.github.gsantner.memetastic%22%5D%2C%224%22%3A%5B%22referrer%22%2C%22https%3A%2F%2Fgithub.com%2Fgsantner%2Fmemetastic%2FCHANGELOG.md%22%5D%7D)
2-
1+
### v1.7 (In progress)
2+
- Add more languages for translation
3+
- Make favourite icon in popup smaller
34

45
### v1.6 [Blog post](https://gsantner.net/blog/2019/08/15/memetastic-v1.6-offline-meme-templates-funny-sites.html?source=inapp_changelog&project=memetastic)
56
- Simplify overall app usage

Diff for: app/src/main/java/net/gsantner/opoc/preference/SharedPreferencesPropertyBackend.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ public static boolean isCurrentHourOfDayBetween(int begin, int end) {
547547
*/
548548
public Date getDateOfDaysAgo(int days) {
549549
Calendar cal = new GregorianCalendar();
550-
cal.add(Calendar.DAY_OF_MONTH, -days);
550+
cal.add(Calendar.DATE, -days);
551551
return cal.getTime();
552552
}
553553

Diff for: app/src/main/java/net/gsantner/opoc/util/Callback.java

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212

1313
@SuppressWarnings("unused")
1414
public class Callback {
15+
16+
public interface a0 {
17+
void callback();
18+
}
19+
1520
public interface a1<A> {
1621
void callback(A arg1);
1722
}

Diff for: app/src/main/java/net/gsantner/opoc/util/ContextUtils.java

+41-7
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
import android.os.Build;
4242
import android.os.Environment;
4343
import android.os.SystemClock;
44+
import android.os.VibrationEffect;
45+
import android.os.Vibrator;
4446
import android.support.annotation.ColorInt;
4547
import android.support.annotation.ColorRes;
4648
import android.support.annotation.DrawableRes;
@@ -51,7 +53,9 @@
5153
import android.support.v4.app.ActivityManagerCompat;
5254
import android.support.v4.content.ContextCompat;
5355
import android.support.v4.graphics.drawable.DrawableCompat;
56+
import android.support.v4.text.TextUtilsCompat;
5457
import android.support.v4.util.Pair;
58+
import android.support.v4.view.ViewCompat;
5559
import android.text.Html;
5660
import android.text.InputFilter;
5761
import android.text.SpannableString;
@@ -82,6 +86,7 @@
8286
import java.util.List;
8387
import java.util.Locale;
8488

89+
import static android.content.Context.VIBRATOR_SERVICE;
8590
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
8691
import static android.graphics.Bitmap.CompressFormat;
8792

@@ -150,6 +155,10 @@ public Drawable rdrawable(@DrawableRes int resId) {
150155
* Get color by given color ressource id
151156
*/
152157
public int rcolor(@ColorRes int resId) {
158+
if (resId == 0) {
159+
Log.e(getClass().getName(), "ContextUtils::rcolor: resId is 0!");
160+
return Color.BLACK;
161+
}
153162
return ContextCompat.getColor(_context, resId);
154163
}
155164

@@ -175,12 +184,12 @@ public boolean areRessourcesAvailable(final ResType resType, final String... res
175184
* @param intColor The color coded in int
176185
* @param withAlpha Optional; Set first bool parameter to true to also include alpha value
177186
*/
178-
public String colorToHexString(int intColor, boolean... withAlpha) {
187+
public static String colorToHexString(int intColor, boolean... withAlpha) {
179188
boolean a = withAlpha != null && withAlpha.length >= 1 && withAlpha[0];
180189
return String.format(a ? "#%08X" : "#%06X", (a ? 0xFFFFFFFF : 0xFFFFFF) & intColor);
181190
}
182191

183-
public String getAndroidVersion() {
192+
public static String getAndroidVersion() {
184193
return Build.VERSION.RELEASE + " (" + Build.VERSION.SDK_INT + ")";
185194
}
186195

@@ -286,7 +295,7 @@ public Object getBuildConfigValue(String fieldName) {
286295
*/
287296
public Boolean bcbool(String fieldName, Boolean defaultValue) {
288297
Object field = getBuildConfigValue(fieldName);
289-
if (field != null && field instanceof Boolean) {
298+
if (field instanceof Boolean) {
290299
return (Boolean) field;
291300
}
292301
return defaultValue;
@@ -297,7 +306,7 @@ public Boolean bcbool(String fieldName, Boolean defaultValue) {
297306
*/
298307
public String bcstr(String fieldName, String defaultValue) {
299308
Object field = getBuildConfigValue(fieldName);
300-
if (field != null && field instanceof String) {
309+
if (field instanceof String) {
301310
return (String) field;
302311
}
303312
return defaultValue;
@@ -308,7 +317,7 @@ public String bcstr(String fieldName, String defaultValue) {
308317
*/
309318
public Integer bcint(String fieldName, int defaultValue) {
310319
Object field = getBuildConfigValue(fieldName);
311-
if (field != null && field instanceof Integer) {
320+
if (field instanceof Integer) {
312321
return (Integer) field;
313322
}
314323
return defaultValue;
@@ -490,10 +499,16 @@ public Locale getLocaleByAndroidCode(String androidLC) {
490499
*/
491500
public void setAppLanguage(String androidLC) {
492501
Locale locale = getLocaleByAndroidCode(androidLC);
502+
locale = (locale != null && !androidLC.isEmpty()) ? locale : Resources.getSystem().getConfiguration().locale;
503+
setLocale(locale);
504+
}
505+
506+
public ContextUtils setLocale(Locale locale) {
493507
Configuration config = _context.getResources().getConfiguration();
494-
config.locale = (locale != null && !androidLC.isEmpty())
495-
? locale : Resources.getSystem().getConfiguration().locale;
508+
config.locale = (locale != null ? locale : Resources.getSystem().getConfiguration().locale);
496509
_context.getResources().updateConfiguration(config, null);
510+
Locale.setDefault(locale);
511+
return this;
497512
}
498513

499514
/**
@@ -827,6 +842,9 @@ public Drawable tintDrawable(@Nullable Drawable drawable, @ColorInt int color) {
827842
* This may not work on some devices and it maybe won't work on future android updates
828843
*/
829844
public void setSubMenuIconsVisiblity(Menu menu, boolean visible) {
845+
if (TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()) == ViewCompat.LAYOUT_DIRECTION_RTL) {
846+
return;
847+
}
830848
if (menu.getClass().getSimpleName().equals("MenuBuilder")) {
831849
try {
832850
@SuppressLint("PrivateApi") Method m = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
@@ -957,6 +975,22 @@ public boolean isDeviceGoodHardware() {
957975
return true;
958976
}
959977
}
978+
979+
// Vibrate device one time by given amount of time, defaulting to 50ms
980+
// Requires <uses-permission android:name="android.permission.VIBRATE" /> in AndroidManifest to work
981+
@SuppressWarnings("UnnecessaryReturnStatement")
982+
@SuppressLint("MissingPermission")
983+
public void vibrate(int... ms) {
984+
int ms_v = ms != null && ms.length > 0 ? ms[0] : 50;
985+
Vibrator vibrator = ((Vibrator) _context.getSystemService(VIBRATOR_SERVICE));
986+
if (vibrator == null) {
987+
return;
988+
} else if (Build.VERSION.SDK_INT >= 26) {
989+
vibrator.vibrate(VibrationEffect.createOneShot(ms_v, VibrationEffect.DEFAULT_AMPLITUDE));
990+
} else {
991+
vibrator.vibrate(ms_v);
992+
}
993+
}
960994
}
961995

962996

Diff for: app/src/main/java/net/gsantner/opoc/util/FileUtils.java

+35-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.io.OutputStream;
3030
import java.net.URLConnection;
3131
import java.text.DecimalFormat;
32+
import java.text.DecimalFormatSymbols;
3233
import java.util.ArrayList;
3334
import java.util.List;
3435
import java.util.Locale;
@@ -240,6 +241,30 @@ public static boolean copyFile(final File src, final File dst) {
240241
}
241242
}
242243

244+
public static boolean copyFile(final File src, final FileOutputStream os) {
245+
InputStream is = null;
246+
try {
247+
try {
248+
is = new FileInputStream(src);
249+
byte[] buf = new byte[BUFFER_SIZE];
250+
int len;
251+
while ((len = is.read(buf)) > 0) {
252+
os.write(buf, 0, len);
253+
}
254+
return true;
255+
} finally {
256+
if (is != null) {
257+
is.close();
258+
}
259+
if (os != null) {
260+
os.close();
261+
}
262+
}
263+
} catch (IOException ex) {
264+
return false;
265+
}
266+
}
267+
243268
// Returns -1 if the file did not contain any of the needles, otherwise,
244269
// the index of which needle was found in the contents of the file.
245270
//
@@ -452,7 +477,15 @@ public static String getReadableFileSize(long size, boolean abbreviation) {
452477
}
453478
String[] units = abbreviation ? new String[]{"B", "kB", "MB", "GB", "TB"} : new String[]{"Bytes", "Kilobytes", "Megabytes", "Gigabytes", "Terabytes"};
454479
int unit = (int) (Math.log10(size) / Math.log10(1024));
455-
return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, unit))
456-
+ " " + units[unit];
480+
return new DecimalFormat("#,##0.#", DecimalFormatSymbols.getInstance(Locale.ENGLISH)).format(size / Math.pow(1024, unit)) + " " + units[unit];
481+
}
482+
483+
public static int[] getTimeDiffHMS(long now, long past) {
484+
int[] ret = new int[3];
485+
long diff = Math.abs(now - past);
486+
ret[0] = (int) (diff / (1000 * 60 * 60)); // hours
487+
ret[1] = (int) (diff / (1000 * 60)) % 60; // min
488+
ret[2] = (int) (diff / 1000) % 60; // sec
489+
return ret;
457490
}
458491
}

Diff for: app/src/main/java/net/gsantner/opoc/util/ShareUtil.java

+45-8
Original file line numberDiff line numberDiff line change
@@ -360,18 +360,23 @@ public boolean shareImage(Bitmap bitmap, Bitmap.CompressFormat format, int quali
360360
*/
361361
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
362362
@SuppressWarnings("deprecation")
363-
public PrintJob print(WebView webview, String jobName) {
363+
public PrintJob print(final WebView webview, final String jobName, final boolean... landscape) {
364364
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
365-
PrintDocumentAdapter printAdapter;
366-
PrintManager printManager = (PrintManager) _context.getSystemService(Context.PRINT_SERVICE);
365+
final PrintDocumentAdapter printAdapter;
366+
final PrintManager printManager = (PrintManager) _context.getSystemService(Context.PRINT_SERVICE);
367367
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
368368
printAdapter = webview.createPrintDocumentAdapter(jobName);
369369
} else {
370370
printAdapter = webview.createPrintDocumentAdapter();
371371
}
372+
final PrintAttributes.Builder attrib = new PrintAttributes.Builder();
373+
if (landscape != null && landscape.length > 0 && landscape[0]) {
374+
attrib.setMediaSize(new PrintAttributes.MediaSize("ISO_A4", "android", 11690, 8270));
375+
attrib.setMinMargins(new PrintAttributes.Margins(0, 0, 0, 0));
376+
}
372377
if (printManager != null) {
373378
try {
374-
return printManager.print(jobName, printAdapter, new PrintAttributes.Builder().build());
379+
return printManager.print(jobName, printAdapter, attrib.build());
375380
} catch (Exception ignored) {
376381
}
377382
}
@@ -572,6 +577,14 @@ public File extractFileFromIntent(Intent receivingIntent) {
572577
}
573578
}
574579

580+
// media/ prefix for External storage
581+
if (fileStr.startsWith((tmps = "media/"))) {
582+
File f = new File(Uri.decode(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + fileStr.substring(tmps.length())));
583+
if (f.exists()) {
584+
return f;
585+
}
586+
}
587+
575588
// Next/OwnCloud Fileprovider
576589
for (String fp : new String[]{"org.nextcloud.files", "org.nextcloud.beta.files", "org.owncloud.files"}) {
577590
if (fileProvider.equals(fp) && fileStr.startsWith(tmps = "external_files/")) {
@@ -587,6 +600,16 @@ public File extractFileFromIntent(Intent receivingIntent) {
587600
return new File(Uri.decode(Environment.getExternalStorageDirectory().getAbsolutePath() + fileStr.substring(tmps.length())));
588601
}
589602

603+
if (fileStr.startsWith(tmps = "external_files/")) {
604+
for (String prefix : new String[]{Environment.getExternalStorageDirectory().getAbsolutePath(), "/storage", ""}) {
605+
File f = new File(Uri.decode(prefix + "/" + fileStr.substring(tmps.length())));
606+
if (f.exists()) {
607+
return f;
608+
}
609+
}
610+
611+
}
612+
590613
// URI Encoded paths with full path after content://package/
591614
if (fileStr.startsWith("/") || fileStr.startsWith("%2F")) {
592615
tmpf = new File(Uri.decode(fileStr));
@@ -624,6 +647,11 @@ public void requestGalleryPicture() {
624647
}
625648
}
626649

650+
public String extractFileFromIntentStr(Intent receivingIntent) {
651+
File f = extractFileFromIntent(receivingIntent);
652+
return f != null ? f.getAbsolutePath() : null;
653+
}
654+
627655
/**
628656
* Request a picture from camera-like apps
629657
* Result ({@link String}) will be available from {@link Activity#onActivityResult(int, int, Intent)}.
@@ -647,7 +675,7 @@ public String requestCameraPicture(File target) {
647675
if (target != null && !target.isDirectory()) {
648676
photoFile = target;
649677
} else {
650-
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH-mm-ss", Locale.getDefault());
678+
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH-mm-ss", Locale.ENGLISH);
651679
File storageDir = target != null ? target : new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "Camera");
652680
String imageFileName = ((new ContextUtils(_context).rstr("app_name")).replaceAll("[^a-zA-Z0-9\\.\\-]", "_") + "_").replace("__", "_") + sdf.format(new Date());
653681
photoFile = new File(storageDir, imageFileName + ".jpg");
@@ -717,6 +745,10 @@ public Object extractResultFromActivityResult(int requestCode, int resultCode, I
717745
cursor.close();
718746
}
719747

748+
// Try to grab via file extraction method
749+
data.setAction(Intent.ACTION_VIEW);
750+
picturePath = picturePath != null ? picturePath : extractFileFromIntentStr(data);
751+
720752
// Retrieve image from file descriptor / Cloud, e.g.: Google Drive, Picasa
721753
if (picturePath == null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
722754
try {
@@ -999,7 +1031,8 @@ private Activity greedyGetActivity(Activity... activity) {
9991031
public boolean canWriteFile(File file, boolean isDir) {
10001032
if (file == null) {
10011033
return false;
1002-
} else if (file.getAbsolutePath().startsWith(Environment.getExternalStorageDirectory().getAbsolutePath())) {
1034+
} else if (file.getAbsolutePath().startsWith(Environment.getExternalStorageDirectory().getAbsolutePath())
1035+
|| file.getAbsolutePath().startsWith(_context.getFilesDir().getAbsolutePath())) {
10031036
boolean s1 = isDir && file.getParentFile().canWrite();
10041037
return !isDir && file.getParentFile() != null ? file.getParentFile().canWrite() : file.canWrite();
10051038
} else {
@@ -1087,11 +1120,12 @@ public void showMountSdDialog(@StringRes int title, @StringRes int description,
10871120
dialogi.show();
10881121
}
10891122

1123+
@SuppressWarnings({"ResultOfMethodCallIgnored", "StatementWithEmptyBody"})
10901124
public void writeFile(File file, boolean isDirectory, Callback.a2<Boolean, FileOutputStream> writeFileCallback) {
10911125
try {
10921126
FileOutputStream fileOutputStream = null;
10931127
ParcelFileDescriptor pfd = null;
1094-
if (file.canWrite()) {
1128+
if (file.canWrite() || (!file.exists() && file.getParentFile().canWrite())) {
10951129
if (isDirectory) {
10961130
file.mkdirs();
10971131
} else {
@@ -1112,7 +1146,10 @@ public void writeFile(File file, boolean isDirectory, Callback.a2<Boolean, FileO
11121146
writeFileCallback.callback(fileOutputStream != null || (isDirectory && file.exists()), fileOutputStream);
11131147
}
11141148
if (fileOutputStream != null) {
1115-
fileOutputStream.close();
1149+
try {
1150+
fileOutputStream.close();
1151+
} catch (Exception ignored) {
1152+
}
11161153
}
11171154
if (pfd != null) {
11181155
pfd.close();

0 commit comments

Comments
 (0)