SSTVEncoder2/app/src/main/java/om/sstvencoder/Utility.java

114 lines
3.8 KiB
Java
Raw Normal View History

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;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Rect;
import android.media.ExifInterface;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public final class Utility {
@NonNull
public static Rect getEmbeddedRect(int w, int h, int iw, int ih) {
Rect rect;
int ow = (9 * w) / 10;
int oh = (9 * h) / 10;
if (iw * oh < ow * ih) {
rect = new android.graphics.Rect(0, 0, (iw * oh) / ih, oh);
rect.offset((w - (iw * oh) / ih) / 2, (h - oh) / 2);
} else {
rect = new android.graphics.Rect(0, 0, ow, (ih * ow) / iw);
rect.offset((w - ow) / 2, (h - (ih * ow) / iw) / 2);
}
return rect;
}
static String createMessage(Exception ex) {
String message = ex.getMessage() + "\n";
for (StackTraceElement el : ex.getStackTrace())
message += "\n" + el.toString();
return message;
}
@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;
}
@NonNull
static ContentValues getWavContentValues(File file) {
ContentValues values = new ContentValues();
values.put(MediaStore.Audio.Media.ALBUM, "SSTV Encoder");
values.put(MediaStore.Audio.Media.ARTIST, "");
values.put(MediaStore.Audio.Media.DATA, file.toString());
values.put(MediaStore.Audio.Media.IS_MUSIC, true);
values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/wav");
values.put(MediaStore.Audio.Media.TITLE, file.getName());
return values;
}
static File createImageFilePath() {
File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
if (!isExternalStorageWritable())
return null;
return new File(dir, createFileName() + ".jpg");
}
static File createWaveFilePath() {
// sdcard/Music
File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
if (!isExternalStorageWritable())
return null;
return new File(dir, createFileName() + ".wav");
}
private static String createFileName() {
return new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
}
private static boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
return Environment.MEDIA_MOUNTED.equals(state);
}
}