show space remaining

This commit is contained in:
Alexey Kuznetsov 2016-03-28 09:23:19 +03:00
commit a6e8c680dd
5 changed files with 137 additions and 5 deletions

View file

@ -517,6 +517,7 @@ public class MainActivity extends AppCompatActivity implements AbsListView.OnScr
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume");
if (permitted(PERMISSIONS))
load();
@ -525,6 +526,8 @@ public class MainActivity extends AppCompatActivity implements AbsListView.OnScr
checkPending();
updateHeader();
final int selected = getLastRecording();
list.setSelection(selected);
if (selected != -1) {
@ -659,4 +662,34 @@ public class MainActivity extends AppCompatActivity implements AbsListView.OnScr
AppIndex.AppIndexApi.end(client, viewAction);
client.disconnect();
}
void updateHeader() {
File f = storage.getTempRecording();
long free = storage.getFree(f);
long sec = storage.average(free);
String str = "";
long diff = sec;
int diffSeconds = (int) (diff / 1000 % 60);
int diffMinutes = (int) (diff / (60 * 1000) % 60);
int diffHours = (int) (diff / (60 * 60 * 1000) % 24);
int diffDays = (int) (diff / (24 * 60 * 60 * 1000));
if (diffDays > 0) {
str = getResources().getQuantityString(R.plurals.days, diffDays, diffDays);
} else if (diffHours > 0) {
str = getResources().getQuantityString(R.plurals.hours, diffHours, diffHours);
} else if (diffMinutes > 0) {
str = getResources().getQuantityString(R.plurals.minutes, diffMinutes, diffMinutes);
} else if (diffSeconds > 0) {
str = getResources().getQuantityString(R.plurals.seconds, diffSeconds, diffSeconds);
}
String ss = String.format("free %s ~ %s left", MainApplication.formatSize(free), str);
TextView text = (TextView) findViewById(R.id.space_left);
text.setText(ss);
}
}

View file

@ -26,7 +26,10 @@ public class MainApplication extends Application {
}
public static String formatSize(long s) {
if (s > 0.1 * 1024 * 1024) {
if (s > 0.1 * 1024 * 1024 * 1024) {
float f = s / 1024f / 1024f / 1024f;
return String.format("%.1f GB", f);
} else if (s > 0.1 * 1024 * 1024) {
float f = s / 1024f / 1024f;
return String.format("%.1f MB", f);
} else {

View file

@ -5,11 +5,18 @@ import android.content.ContentResolver;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.media.AudioFormat;
import android.os.Build;
import android.os.StatFs;
import android.preference.PreferenceManager;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.widget.Toast;
import com.github.axet.audiorecorder.R;
import com.github.axet.audiorecorder.activities.RecordingActivity;
import com.github.axet.audiorecorder.encoders.FormatM4A;
import com.github.axet.audiorecorder.encoders.FormatWAV;
import java.io.File;
import java.io.FileInputStream;
@ -185,8 +192,68 @@ public class Storage {
return list;
}
// get average recording miliseconds based on compression format
public long average(long free) {
final SharedPreferences shared = PreferenceManager.getDefaultSharedPreferences(context);
int rate = Integer.parseInt(shared.getString(MainApplication.PREFERENCE_RATE, ""));
String ext = shared.getString(MainApplication.PREFERENCE_ENCODING, "");
if (ext.equals("m4a")) {
long y1 = 365723; // one minute sample 16000Hz
long x1 = 16000; // at 16000
long y2 = 493743; // one minute sample
long x2 = 44000; // at 44000
long x = rate;
long y = (x - x1) * (y2 - y1) / (x2 - x1) + y1;
int m = RawSamples.CHANNEL_CONFIG == AudioFormat.CHANNEL_IN_MONO ? 1 : 2;
long perSec = (y / 60) * m;
return free / perSec * 1000;
}
// default raw
int m = RawSamples.CHANNEL_CONFIG == AudioFormat.CHANNEL_IN_MONO ? 1 : 2;
int c = RawSamples.AUDIO_FORMAT == AudioFormat.ENCODING_PCM_16BIT ? 2 : 1;
long perSec = (c * m * rate);
return free / perSec * 1000;
}
public long getFree(File f) {
while (!f.exists())
f = f.getParentFile();
StatFs fsi = new StatFs(f.getPath());
if (Build.VERSION.SDK_INT < 18)
return fsi.getBlockSize() * fsi.getAvailableBlocks();
else
return fsi.getBlockSizeLong() * fsi.getAvailableBlocksLong();
}
public File getTempRecording() {
return new File(context.getApplicationInfo().dataDir, TMP_REC);
File internal = new File(context.getApplicationInfo().dataDir, TMP_REC);
// Starting in KITKAT, no permissions are required to read or write to the returned path;
// it's always accessible to the calling app
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
if (!permitted(PERMISSIONS))
return internal;
}
File external = new File(context.getExternalCacheDir(), TMP_REC);
if (internal.exists())
return internal;
if (external.exists())
return external;
long freeI = getFree(internal);
long freeE = getFree(external);
if (freeI > freeE)
return internal;
else
return external;
}
public FileOutputStream open(File f) {

View file

@ -1,17 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".activities.MainActivity"
tools:showIn="@layout/activity_main">
<TextView
android:id="@+id/space_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="5dp"
android:text="free 32G ~ 3 hours left" />
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
@ -27,4 +35,4 @@
android:textAlignment="center"
android:visibility="gone"></TextView>
</FrameLayout>
</LinearLayout>

View file

@ -33,4 +33,25 @@
<string name="title_activity_main">Audio Recorder</string>
<string name="action_settings">Settings</string>
<plurals name="days">
<item quantity="one">%d day</item>
<item quantity="other">%d days</item>
</plurals>
<plurals name="hours">
<item quantity="one">%d hour</item>
<item quantity="other">%d hours</item>
</plurals>
<plurals name="minutes">
<item quantity="one">%d minute</item>
<item quantity="other">%d minutes</item>
</plurals>
<plurals name="seconds">
<item quantity="one">%d second</item>
<item quantity="other">%d seconds</item>
</plurals>
</resources>