2017-01-03 18:32:45 +01:00
|
|
|
/*
|
|
|
|
|
Copyright 2017 Olga Miller <olga.rgb@gmail.com>
|
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
package om.sstvencoder;
|
|
|
|
|
|
2017-01-07 14:54:32 +01:00
|
|
|
import android.content.Context;
|
2017-01-03 18:32:45 +01:00
|
|
|
import android.content.Intent;
|
|
|
|
|
import android.graphics.Rect;
|
2020-06-18 22:35:44 +02:00
|
|
|
import androidx.exifinterface.media.ExifInterface;
|
2017-01-07 14:54:32 +01:00
|
|
|
import android.net.Uri;
|
|
|
|
|
import android.os.Build;
|
2017-01-03 18:32:45 +01:00
|
|
|
import android.os.Environment;
|
2020-06-22 23:33:26 +02:00
|
|
|
|
2020-06-18 22:35:44 +02:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
import androidx.core.content.FileProvider;
|
2017-01-03 18:32:45 +01:00
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.Locale;
|
|
|
|
|
|
2017-02-11 11:44:37 +01:00
|
|
|
final class Utility {
|
2017-01-03 18:32:45 +01:00
|
|
|
@NonNull
|
2017-02-11 11:44:37 +01:00
|
|
|
static Rect getEmbeddedRect(int w, int h, int iw, int ih) {
|
2017-01-03 18:32:45 +01:00
|
|
|
Rect rect;
|
|
|
|
|
|
|
|
|
|
int ow = (9 * w) / 10;
|
|
|
|
|
int oh = (9 * h) / 10;
|
|
|
|
|
|
|
|
|
|
if (iw * oh < ow * ih) {
|
2017-02-11 11:44:37 +01:00
|
|
|
int right = (iw * oh) / ih;
|
|
|
|
|
rect = new Rect(0, 0, right, oh);
|
|
|
|
|
rect.offset((w - right) / 2, (h - oh) / 2);
|
2017-01-03 18:32:45 +01:00
|
|
|
} else {
|
2017-02-11 11:44:37 +01:00
|
|
|
int bottom = (ih * ow) / iw;
|
|
|
|
|
rect = new Rect(0, 0, ow, bottom);
|
|
|
|
|
rect.offset((w - ow) / 2, (h - bottom) / 2);
|
2017-01-03 18:32:45 +01:00
|
|
|
}
|
|
|
|
|
return rect;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-11 10:39:48 +01:00
|
|
|
static float getTextSizeFactor(int w, int h) {
|
|
|
|
|
return 0.1f * (Utility.getEmbeddedRect(w, h, 320, 240).height());
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-03 18:32:45 +01:00
|
|
|
static String createMessage(Exception ex) {
|
2018-03-10 13:02:24 +01:00
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
sb.append(ex.getMessage());
|
|
|
|
|
sb.append("\n");
|
|
|
|
|
for (StackTraceElement el : ex.getStackTrace()) {
|
|
|
|
|
sb.append("\n");
|
|
|
|
|
sb.append(el.toString());
|
|
|
|
|
}
|
|
|
|
|
return sb.toString();
|
2017-01-03 18:32:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
|
static Intent createEmailIntent(final String subject, final String text) {
|
|
|
|
|
Intent intent = new Intent(Intent.ACTION_SEND);
|
|
|
|
|
intent.setType("text/email");
|
|
|
|
|
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"olga.rgb@gmail.com"});
|
|
|
|
|
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
|
|
|
|
|
intent.putExtra(Intent.EXTRA_TEXT, text);
|
|
|
|
|
return intent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int convertToDegrees(int exifOrientation) {
|
|
|
|
|
switch (exifOrientation) {
|
|
|
|
|
case ExifInterface.ORIENTATION_ROTATE_90:
|
|
|
|
|
return 90;
|
|
|
|
|
case ExifInterface.ORIENTATION_ROTATE_180:
|
|
|
|
|
return 180;
|
|
|
|
|
case ExifInterface.ORIENTATION_ROTATE_270:
|
|
|
|
|
return 270;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-07 14:54:32 +01:00
|
|
|
static Uri createImageUri(Context context) {
|
2017-01-03 18:32:45 +01:00
|
|
|
if (!isExternalStorageWritable())
|
|
|
|
|
return null;
|
2017-01-07 14:54:32 +01:00
|
|
|
File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
|
|
|
|
|
File file = new File(dir, createFileName() + ".jpg");
|
|
|
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N)
|
|
|
|
|
// API level 24 and higher: FileUriExposedException
|
|
|
|
|
return Uri.fromFile(file); // file:// URI
|
|
|
|
|
// API level 15: Camera crash
|
|
|
|
|
return FileProvider.getUriForFile(context, "om.sstvencoder", file); // content:// URI
|
2017-01-03 18:32:45 +01:00
|
|
|
}
|
|
|
|
|
|
2020-06-22 23:33:26 +02:00
|
|
|
static String createWaveFileName() {
|
|
|
|
|
return createFileName() + ".wav";
|
2017-01-03 18:32:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static String createFileName() {
|
|
|
|
|
return new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-22 23:33:26 +02:00
|
|
|
static boolean isExternalStorageWritable() {
|
2017-01-03 18:32:45 +01:00
|
|
|
String state = Environment.getExternalStorageState();
|
|
|
|
|
return Environment.MEDIA_MOUNTED.equals(state);
|
|
|
|
|
}
|
|
|
|
|
}
|