From ce1e1bc98986dcac405033651e819b750763c737 Mon Sep 17 00:00:00 2001 From: Alexey Kuznetsov Date: Sun, 18 Jun 2017 17:10:09 +0300 Subject: [PATCH 01/13] add lockscreen controls --- app/src/main/AndroidManifest.xml | 2 + .../activities/MainActivity.java | 27 ++- .../activities/RecordingActivity.java | 4 +- .../activities/SettingsActivity.java | 13 ++ .../audiorecorder/app/MainApplication.java | 2 + .../services/RecordingService.java | 188 +++++++++++------- app/src/main/res/values-de/strings.xml | 11 +- app/src/main/res/values-ja/strings.xml | 3 +- app/src/main/res/values-pt-rBR/strings.xml | 3 +- app/src/main/res/values-ru/strings.xml | 5 +- app/src/main/res/values/strings.xml | 3 +- app/src/main/res/xml-de/pref_general.xml | 6 + app/src/main/res/xml-ja/pref_general.xml | 6 + app/src/main/res/xml-pt-rBR/pref_general.xml | 6 + app/src/main/res/xml-ru/pref_general.xml | 6 + app/src/main/res/xml/pref_general.xml | 6 + 16 files changed, 207 insertions(+), 84 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 79186e9..cab2c37 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -23,6 +23,7 @@ @@ -35,6 +36,7 @@ diff --git a/app/src/main/java/com/github/axet/audiorecorder/activities/MainActivity.java b/app/src/main/java/com/github/axet/audiorecorder/activities/MainActivity.java index 3981009..ba6c30d 100644 --- a/app/src/main/java/com/github/axet/audiorecorder/activities/MainActivity.java +++ b/app/src/main/java/com/github/axet/audiorecorder/activities/MainActivity.java @@ -2,9 +2,11 @@ package com.github.axet.audiorecorder.activities; import android.Manifest; import android.app.AlertDialog; +import android.content.BroadcastReceiver; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; +import android.content.IntentFilter; import android.content.SharedPreferences; import android.content.res.Configuration; import android.net.Uri; @@ -18,6 +20,7 @@ import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; +import android.view.WindowManager; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; @@ -26,6 +29,7 @@ import com.github.axet.audiolibrary.app.Recordings; import com.github.axet.audiolibrary.app.Storage; import com.github.axet.audiorecorder.R; import com.github.axet.audiorecorder.app.MainApplication; +import com.github.axet.audiorecorder.services.RecordingService; import java.io.File; import java.util.Collections; @@ -44,6 +48,16 @@ public class MainActivity extends AppCompatActivity { int themeId; + BroadcastReceiver receiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + String a = intent.getAction(); + if (a.equals(Intent.ACTION_SCREEN_OFF)) { + moveTaskToBack(true); + } + } + }; + public static void startActivity(Context context) { Intent i = new Intent(context, MainActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); @@ -65,6 +79,9 @@ public class MainActivity extends AppCompatActivity { setAppTheme(getAppTheme(this)); super.onCreate(savedInstanceState); + getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | + WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); + setContentView(R.layout.activity_main); progressEmpty = findViewById(R.id.progress_empty); @@ -103,6 +120,13 @@ public class MainActivity extends AppCompatActivity { Error(e); } } + + RecordingService.startIfEnabled(this); + + IntentFilter ff = new IntentFilter(); + ff.addAction(Intent.ACTION_SCREEN_OFF); + ff.addAction(Intent.ACTION_SCREEN_ON); + registerReceiver(receiver, ff); } void checkPending() { @@ -257,6 +281,7 @@ public class MainActivity extends AppCompatActivity { protected void onDestroy() { super.onDestroy(); recordings.close(); + unregisterReceiver(receiver); } void updateHeader() { @@ -264,7 +289,7 @@ public class MainActivity extends AppCompatActivity { long free = storage.getFree(f); long sec = storage.average(free); TextView text = (TextView) findViewById(R.id.space_left); - text.setText(((MainApplication) getApplication()).formatFree(free, sec)); + text.setText(MainApplication.formatFree(this, free, sec)); } diff --git a/app/src/main/java/com/github/axet/audiorecorder/activities/RecordingActivity.java b/app/src/main/java/com/github/axet/audiorecorder/activities/RecordingActivity.java index a93f893..1e020b8 100644 --- a/app/src/main/java/com/github/axet/audiorecorder/activities/RecordingActivity.java +++ b/app/src/main/java/com/github/axet/audiorecorder/activities/RecordingActivity.java @@ -433,7 +433,7 @@ public class RecordingActivity extends AppCompatActivity { long perSec = (c * m * rate); long sec = free / perSec * 1000; - state.setText(s + "\n(" + ((MainApplication) getApplication()).formatFree(free, sec) + ")"); + state.setText(s + "\n(" + MainApplication.formatFree(this, free, sec) + ")"); } void editPlay(boolean show) { @@ -549,7 +549,7 @@ public class RecordingActivity extends AppCompatActivity { receiver = null; } - RecordingService.stopService(this); + RecordingService.stopRecording(this); if (pscl != null) { TelephonyManager tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE); diff --git a/app/src/main/java/com/github/axet/audiorecorder/activities/SettingsActivity.java b/app/src/main/java/com/github/axet/audiorecorder/activities/SettingsActivity.java index 97c524d..ecceba5 100644 --- a/app/src/main/java/com/github/axet/audiorecorder/activities/SettingsActivity.java +++ b/app/src/main/java/com/github/axet/audiorecorder/activities/SettingsActivity.java @@ -30,6 +30,7 @@ import com.github.axet.audiolibrary.app.Storage; import com.github.axet.audiorecorder.R; import com.github.axet.audiorecorder.app.MainApplication; import com.github.axet.audiolibrary.encoders.Factory; +import com.github.axet.audiorecorder.services.RecordingService; import java.lang.reflect.Array; import java.util.ArrayList; @@ -215,6 +216,13 @@ public class SettingsActivity extends AppCompatActivity implements SharedPrefere startActivity(new Intent(this, SettingsActivity.class)); overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out); } + if (key.equals(MainApplication.PREFERENCE_CONTROLS)) { + if (sharedPreferences.getBoolean(MainApplication.PREFERENCE_CONTROLS, false)) { + RecordingService.start(this); + } else { + RecordingService.stopService(this); + } + } } @Override @@ -255,6 +263,11 @@ public class SettingsActivity extends AppCompatActivity implements SharedPrefere } return super.onOptionsItemSelected(item); } + + @Override + public void onResume() { + super.onResume(); + } } } diff --git a/app/src/main/java/com/github/axet/audiorecorder/app/MainApplication.java b/app/src/main/java/com/github/axet/audiorecorder/app/MainApplication.java index 42e4cc6..2c6373d 100644 --- a/app/src/main/java/com/github/axet/audiorecorder/app/MainApplication.java +++ b/app/src/main/java/com/github/axet/audiorecorder/app/MainApplication.java @@ -6,6 +6,8 @@ import com.github.axet.audiorecorder.R; public class MainApplication extends com.github.axet.audiolibrary.app.MainApplication { + public static final String PREFERENCE_CONTROLS = "controls"; + @Override public void onCreate() { super.onCreate(); diff --git a/app/src/main/java/com/github/axet/audiorecorder/services/RecordingService.java b/app/src/main/java/com/github/axet/audiorecorder/services/RecordingService.java index c18f8ea..a5e8b3a 100644 --- a/app/src/main/java/com/github/axet/audiorecorder/services/RecordingService.java +++ b/app/src/main/java/com/github/axet/audiorecorder/services/RecordingService.java @@ -1,5 +1,6 @@ package com.github.axet.audiorecorder.services; +import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; @@ -7,18 +8,25 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; +import android.content.SharedPreferences; import android.os.Build; import android.os.IBinder; +import android.preference.PreferenceManager; import android.support.annotation.Nullable; import android.support.v4.app.NotificationCompat; import android.util.Log; import android.view.View; import android.widget.RemoteViews; +import com.github.axet.androidlibrary.widgets.OptimizationPreferenceCompat; +import com.github.axet.audiolibrary.app.Storage; import com.github.axet.audiorecorder.R; +import com.github.axet.audiorecorder.activities.MainActivity; import com.github.axet.audiorecorder.activities.RecordingActivity; import com.github.axet.audiorecorder.app.MainApplication; +import java.io.File; + /** * RecordingActivity more likly to be removed from memory when paused then service. Notification button * does not handle getActvity without unlocking screen. The only option is to have Service. @@ -34,23 +42,19 @@ public class RecordingService extends Service { public static String SHOW_ACTIVITY = RecordingService.class.getCanonicalName() + ".SHOW_ACTIVITY"; public static String PAUSE_BUTTON = RecordingService.class.getCanonicalName() + ".PAUSE_BUTTON"; + public static String RECORD_BUTTON = RecordingService.class.getCanonicalName() + ".RECORD_BUTTON"; - RecordingReceiver receiver; + Storage storage = new Storage(this); - String targetFile; - boolean recording; - boolean encoding; + public static void startIfEnabled(Context context) { + SharedPreferences shared = PreferenceManager.getDefaultSharedPreferences(context); + if (!shared.getBoolean(MainApplication.PREFERENCE_CONTROLS, false)) + return; + start(context); + } - public class RecordingReceiver extends BroadcastReceiver { - @Override - public void onReceive(Context context, Intent intent) { - if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { - // showRecordingActivity(); - } - if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { - // do nothing. do not annoy user. he will see alarm screen on next screen on event. - } - } + public static void start(Context context) { + context.startService(new Intent(context, RecordingService.class)); } public static void startService(Context context, String targetFile, boolean recording, boolean encoding) { @@ -61,6 +65,15 @@ public class RecordingService extends Service { ); } + public static void stopRecording(Context context) { + SharedPreferences shared = PreferenceManager.getDefaultSharedPreferences(context); + if (shared.getBoolean(MainApplication.PREFERENCE_CONTROLS, false)) { + start(context); + return; + } + stopService(context); + } + public static void stopService(Context context) { context.stopService(new Intent(context, RecordingService.class)); } @@ -72,11 +85,7 @@ public class RecordingService extends Service { public void onCreate() { super.onCreate(); Log.d(TAG, "onCreate"); - receiver = new RecordingReceiver(); - IntentFilter filter = new IntentFilter(); - filter.addAction(Intent.ACTION_SCREEN_ON); - filter.addAction(Intent.ACTION_SCREEN_OFF); - registerReceiver(receiver, filter); + startForeground(NOTIFICATION_RECORDING_ICON, build(new Intent())); } @Override @@ -85,17 +94,18 @@ public class RecordingService extends Service { if (intent != null) { String a = intent.getAction(); - if (a == null) { - targetFile = intent.getStringExtra("targetFile"); - recording = intent.getBooleanExtra("recording", false); - encoding = intent.getBooleanExtra("encoding", false); - showNotificationAlarm(true); + showNotificationAlarm(true, intent); } else if (a.equals(PAUSE_BUTTON)) { Intent i = new Intent(RecordingActivity.PAUSE_BUTTON); sendBroadcast(i); - } else if (a.equals(SHOW_ACTIVITY)) { + } else if (a.equals(RECORD_BUTTON)) { RecordingActivity.startActivity(this, false); + } else if (a.equals(SHOW_ACTIVITY)) { + if (!intent.getBooleanExtra("recording", false)) + MainActivity.startActivity(this); + else + RecordingActivity.startActivity(this, false); } } @@ -119,61 +129,97 @@ public class RecordingService extends Service { super.onDestroy(); Log.d(TAG, "onDestory"); - showNotificationAlarm(false); + stopForeground(false); - unregisterReceiver(receiver); + showNotificationAlarm(false, null); + } + + public Notification build(Intent intent) { + String targetFile = intent.getStringExtra("targetFile"); + boolean recording = intent.getBooleanExtra("recording", false); + boolean encoding = intent.getBooleanExtra("encoding", false); + + PendingIntent main = PendingIntent.getService(this, 0, + new Intent(this, RecordingService.class).setAction(SHOW_ACTIVITY).putExtra("recording", targetFile != null), + PendingIntent.FLAG_UPDATE_CURRENT); + + PendingIntent pe = PendingIntent.getService(this, 0, + new Intent(this, RecordingService.class).setAction(PAUSE_BUTTON), + PendingIntent.FLAG_UPDATE_CURRENT); + + PendingIntent re = PendingIntent.getService(this, 0, + new Intent(this, RecordingService.class).setAction(RECORD_BUTTON), + PendingIntent.FLAG_UPDATE_CURRENT); + + RemoteViews view = new RemoteViews(getPackageName(), MainApplication.getTheme(getBaseContext(), + R.layout.notifictaion_recording_light, + R.layout.notifictaion_recording_dark)); + + String title; + String text; + if (targetFile == null) { + title = getString(R.string.app_name); + File f = storage.getStoragePath(); + long free = storage.getFree(f); + long sec = storage.average(free); + text = MainApplication.formatFree(this, free, sec); + view.setViewVisibility(R.id.notification_record, View.VISIBLE); + view.setOnClickPendingIntent(R.id.notification_record, re); + view.setViewVisibility(R.id.notification_pause, View.GONE); + } else { + if (recording) + title = getString(R.string.recording_title); + else + title = getString(R.string.pause_title); + text = ".../" + targetFile; + view.setViewVisibility(R.id.notification_record, View.GONE); + view.setViewVisibility(R.id.notification_pause, View.VISIBLE); + } + + if (encoding) { + view.setViewVisibility(R.id.notification_pause, View.GONE); + title = getString(R.string.encoding_title); + } + + view.setOnClickPendingIntent(R.id.status_bar_latest_event_content, main); + view.setTextViewText(R.id.notification_title, title); + view.setTextViewText(R.id.notification_text, text); + view.setOnClickPendingIntent(R.id.notification_pause, pe); + view.setImageViewResource(R.id.notification_pause, !recording ? R.drawable.ic_play_arrow_black_24dp : R.drawable.ic_pause_black_24dp); + + NotificationCompat.Builder builder = new NotificationCompat.Builder(this) + .setOngoing(true) + .setContentTitle(title) + .setContentText(text) + .setTicker(title) + .setSmallIcon(R.drawable.ic_mic_24dp) + .setContent(view); + + if (Build.VERSION.SDK_INT < 11) { + builder.setContentIntent(main); + } + + if (Build.VERSION.SDK_INT >= 21) + builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC); + + return builder.build(); } // alarm dismiss button - public void showNotificationAlarm(boolean show) { + public void showNotificationAlarm(boolean show, Intent intent) { NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); - if (!show) { notificationManager.cancel(NOTIFICATION_RECORDING_ICON); } else { - PendingIntent main = PendingIntent.getService(this, 0, - new Intent(this, RecordingService.class).setAction(SHOW_ACTIVITY), - PendingIntent.FLAG_UPDATE_CURRENT); - - PendingIntent pe = PendingIntent.getService(this, 0, - new Intent(this, RecordingService.class).setAction(PAUSE_BUTTON), - PendingIntent.FLAG_UPDATE_CURRENT); - - RemoteViews view = new RemoteViews(getPackageName(), MainApplication.getTheme(getBaseContext(), - R.layout.notifictaion_recording_light, - R.layout.notifictaion_recording_dark)); - - String title = getString(R.string.recording_title); - String text = ".../" + targetFile; - - if (encoding) { - view.setViewVisibility(R.id.notification_pause, View.GONE); - title = getString(R.string.encoding_title); - } - - view.setOnClickPendingIntent(R.id.status_bar_latest_event_content, main); - view.setTextViewText(R.id.notification_title, title); - view.setTextViewText(R.id.notification_text, text); - view.setOnClickPendingIntent(R.id.notification_pause, pe); - view.setImageViewResource(R.id.notification_pause, !recording ? R.drawable.ic_play_arrow_black_24dp : R.drawable.ic_pause_black_24dp); - - NotificationCompat.Builder builder = new NotificationCompat.Builder(this) - .setOngoing(true) - .setContentTitle(title) - .setContentText(text) - .setTicker(title) - .setSmallIcon(R.drawable.ic_mic_24dp) - .setContent(view); - - if (Build.VERSION.SDK_INT < 11) { - builder.setContentIntent(main); - } - - if (Build.VERSION.SDK_INT >= 21) - builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC); - - notificationManager.notify(NOTIFICATION_RECORDING_ICON, builder.build()); + notificationManager.notify(NOTIFICATION_RECORDING_ICON, build(intent)); } } + + + @Override + public void onTaskRemoved(Intent rootIntent) { + super.onTaskRemoved(rootIntent); + Log.d(TAG, "onTaskRemoved"); + } } diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 7e38707..c1ee858 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -24,15 +24,16 @@ Einstellungen Nicht erlaubt Keine App zur Anzeige von Ordnern installiert - Pause (bei Anruf) - Encoding - Pause - Bearbeiten + pause (bei Anruf) + aufnehmen + encoding + pause + bearbeiten Bestätige Abbruch Willst du wirklich abbrechen? - Aufnehmen Encoding... Aufnahme + Pause... Öffne Aufnahme-Ordner Aufnahmeliste ist leer\n\nDrücke \'Aufnehmen\' um die Aufnahme zu starten diff --git a/app/src/main/res/values-ja/strings.xml b/app/src/main/res/values-ja/strings.xml index bfdcc33..4b30a54 100644 --- a/app/src/main/res/values-ja/strings.xml +++ b/app/src/main/res/values-ja/strings.xml @@ -25,14 +25,15 @@ 許可されていません フォルダーを表示するアプリケーションがインストールされていません 一時停止 (着信により保留) + 録音中 エンコーディング 一時停止 編集 キャンセルの確認 キャンセルしてもよろしいですか? - 録音中 エンコード中... 録音中 + 一時停止... 録音フォルダーを開く 録音リストは空です\n\n録音をクリックすると録音を開始します diff --git a/app/src/main/res/values-pt-rBR/strings.xml b/app/src/main/res/values-pt-rBR/strings.xml index f6bad0b..e5e1a7e 100644 --- a/app/src/main/res/values-pt-rBR/strings.xml +++ b/app/src/main/res/values-pt-rBR/strings.xml @@ -25,14 +25,15 @@ Não permitido Não foi encontrado um aplicativo para explorar arquivos pausado (chamada atendida) + gravando codificando pausado editando Confirme o cancelamento Tem certeza que deseja cancelar? - gravando Codificando... Gravando + Pausado... Abrir pasta das gravações A lista de gravações está vazia\n\nToque no ícone de microfone para começar a gravar diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index b4c6252..f6d6c62 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -25,14 +25,15 @@ Доступ запрещен Программа для просмотра папок не установлена пауза (звонок) + запись кодировка пауза редактор Отменить запись Вы уверены? - запись Кодирование... - Запись + Запись... + Пауза... Открыть папку с записями Список записей пуст.\n\nНажмите на \'Микрофон\' чтобы начать запись. diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1449735..d9b2371 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -57,13 +57,14 @@ Not permitted No folder view application installed pause (hold by call) + recording encoding pause edit Confirm cancel Are you sure you want to cancel? - recording Encoding... + Pause... Recording Open Recording Folder Recording List is Empty\n\nClick Record to Start Recording diff --git a/app/src/main/res/xml-de/pref_general.xml b/app/src/main/res/xml-de/pref_general.xml index b5e9106..b4e3b79 100644 --- a/app/src/main/res/xml-de/pref_general.xml +++ b/app/src/main/res/xml-de/pref_general.xml @@ -58,6 +58,12 @@ android:summary="Aktiviere Audio-Profil \'Nicht Stören\' während Aufnahmen" android:title="Ruhe-Modus" /> + + + + + + + + + + Date: Sun, 18 Jun 2017 17:28:25 +0300 Subject: [PATCH 02/13] fix restore samples crash --- app/build.gradle | 2 +- .../audiorecorder/activities/RecordingActivity.java | 12 +++++------- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index c7c69b5..6d41096 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -43,5 +43,5 @@ android { dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' - compile 'com.github.axet:android-audio-library:0.0.55' // compile project(':android-audio-library') + compile 'com.github.axet:android-audio-library:0.0.56' // compile project(':android-audio-library') } diff --git a/app/src/main/java/com/github/axet/audiorecorder/activities/RecordingActivity.java b/app/src/main/java/com/github/axet/audiorecorder/activities/RecordingActivity.java index 1e020b8..1995176 100644 --- a/app/src/main/java/com/github/axet/audiorecorder/activities/RecordingActivity.java +++ b/app/src/main/java/com/github/axet/audiorecorder/activities/RecordingActivity.java @@ -282,13 +282,11 @@ public class RecordingActivity extends AppCompatActivity { int len = rs.read(buf); rs.close(); - pitch.clear(cut / samplesUpdate); - for (int i = 0; i < len; i += samplesUpdate * MainApplication.getChannels(this)) { - double dB = 0; - for (int c = 0; c < MainApplication.getChannels(this); c++) { - dB += RawSamples.getDB(buf, i + samplesUpdate * c, samplesUpdate); - } - dB = dB / MainApplication.getChannels(this); + int samplesUpdateStereo = samplesUpdate * MainApplication.getChannels(this); + pitch.clear(cut / samplesUpdateStereo); + len = len / samplesUpdateStereo * samplesUpdateStereo; // cut right overs (leftovers from right) + for (int i = 0; i < len; i += samplesUpdateStereo) { + double dB = RawSamples.getDB(buf, i, samplesUpdateStereo); pitch.add(dB); } updateSamples(samplesTime); From 135818b7930effe55c4aa01ec6532281caeb5149 Mon Sep 17 00:00:00 2001 From: Alexey Kuznetsov Date: Sun, 18 Jun 2017 17:38:10 +0300 Subject: [PATCH 03/13] cleanups --- .../audiorecorder/activities/RecordingActivity.java | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/github/axet/audiorecorder/activities/RecordingActivity.java b/app/src/main/java/com/github/axet/audiorecorder/activities/RecordingActivity.java index 1995176..a6a7a6e 100644 --- a/app/src/main/java/com/github/axet/audiorecorder/activities/RecordingActivity.java +++ b/app/src/main/java/com/github/axet/audiorecorder/activities/RecordingActivity.java @@ -181,8 +181,11 @@ public class RecordingActivity extends AppCompatActivity { sampleRate = Integer.parseInt(shared.getString(MainApplication.PREFERENCE_RATE, "")); sampleRate = Sound.getValidRecordRate(MainApplication.getInMode(this), sampleRate); - if (sampleRate == -1) - throw new RuntimeException("Unable to initailze audio"); + if (sampleRate == -1) { + Toast.makeText(this, "Unable to initailze audio", Toast.LENGTH_SHORT).show(); + finish(); + return; + } samplesUpdate = (int) (pitch.getPitchTime() * sampleRate / 1000.0); updateBufferSize(false); @@ -292,10 +295,6 @@ public class RecordingActivity extends AppCompatActivity { updateSamples(samplesTime); } - boolean isEmulator() { - return "goldfish".equals(Build.HARDWARE); - } - void pauseButton() { if (thread != null) { stopRecording(getString(R.string.pause)); @@ -594,7 +593,6 @@ public class RecordingActivity extends AppCompatActivity { rs.open(samplesTime); - int c = MainApplication.getInMode(RecordingActivity.this); int min = AudioRecord.getMinBufferSize(sampleRate, c, Sound.DEFAULT_AUDIOFORMAT); if (min <= 0) From 8b8689c9f542192b8f4bbaecc97e764c451b85d1 Mon Sep 17 00:00:00 2001 From: vv01f Date: Sun, 18 Jun 2017 15:52:40 +0000 Subject: [PATCH 04/13] Update pref_general.xml --- app/src/main/res/xml-de/pref_general.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/res/xml-de/pref_general.xml b/app/src/main/res/xml-de/pref_general.xml index b4e3b79..4a04b39 100644 --- a/app/src/main/res/xml-de/pref_general.xml +++ b/app/src/main/res/xml-de/pref_general.xml @@ -61,8 +61,8 @@ + android:summary="Zeige Steuerelemente im Sperrbildschirm an" + android:title="Steuerelemente im Sperrbildschirm" /> Date: Sun, 18 Jun 2017 19:39:58 +0300 Subject: [PATCH 05/13] add recording db buffer --- .../activities/RecordingActivity.java | 35 ++++++++++++++----- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/github/axet/audiorecorder/activities/RecordingActivity.java b/app/src/main/java/com/github/axet/audiorecorder/activities/RecordingActivity.java index a6a7a6e..f844f44 100644 --- a/app/src/main/java/com/github/axet/audiorecorder/activities/RecordingActivity.java +++ b/app/src/main/java/com/github/axet/audiorecorder/activities/RecordingActivity.java @@ -48,6 +48,7 @@ import com.github.axet.audiorecorder.app.Storage; import com.github.axet.audiorecorder.services.RecordingService; import java.io.File; +import java.nio.ShortBuffer; public class RecordingActivity extends AppCompatActivity { public static final String TAG = RecordingActivity.class.getSimpleName(); @@ -615,13 +616,17 @@ public class RecordingActivity extends AppCompatActivity { boolean stableRefresh = false; + int samplesUpdateStereo = samplesUpdate * MainApplication.getChannels(RecordingActivity.this); + + ShortBuffer dbBuffer = null; + while (!Thread.currentThread().isInterrupted()) { synchronized (bufferSizeLock) { if (buffer == null || buffer.length != bufferSize) buffer = new short[bufferSize]; } - final int readSize = recorder.read(buffer, 0, buffer.length); + int readSize = recorder.read(buffer, 0, buffer.length); if (readSize <= 0) { break; } @@ -631,16 +636,25 @@ public class RecordingActivity extends AppCompatActivity { start = end; - int s = readSize / MainApplication.getChannels(RecordingActivity.this); + int samples = readSize / MainApplication.getChannels(RecordingActivity.this); - if (stableRefresh || diff >= s) { + if (stableRefresh || diff >= samples) { stableRefresh = true; rs.write(buffer, readSize); - int ps = samplesUpdate * MainApplication.getChannels(RecordingActivity.this); - for (int i = 0; i < readSize; i += ps) { - final double dB = RawSamples.getDB(buffer, i, ps); + short[] dbBuf; + int readSizeUpdate; + if (dbBuffer != null) { + dbBuffer.put(buffer, 0, readSize); + readSizeUpdate = dbBuffer.position() / samplesUpdateStereo * samplesUpdateStereo; + dbBuf = dbBuffer.array(); + } else { + dbBuf = buffer; + readSizeUpdate = readSize / samplesUpdateStereo * samplesUpdateStereo; + } + for (int i = 0; i < readSizeUpdate; i += samplesUpdateStereo) { + final double dB = RawSamples.getDB(dbBuf, i, samplesUpdateStereo); handle.post(new Runnable() { @Override public void run() { @@ -648,9 +662,14 @@ public class RecordingActivity extends AppCompatActivity { } }); } + int readSizeLen = readSize - readSizeUpdate; + if (readSizeLen > 0) { + dbBuffer = ShortBuffer.allocate(samplesUpdateStereo); + dbBuffer.put(dbBuf, readSizeUpdate, readSizeLen); + } - samplesTime += s; - samplesTimeCount += s; + samplesTime += samples; + samplesTimeCount += samples; if (samplesTimeCount > samplesTimeUpdate) { final long m = samplesTime; handle.post(new Runnable() { From a8675577db6b793fa36ac5a44db9acc3ba37d612 Mon Sep 17 00:00:00 2001 From: Alexey Kuznetsov Date: Sun, 18 Jun 2017 19:46:40 +0300 Subject: [PATCH 06/13] add recording db buffer --- .../activities/RecordingActivity.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/github/axet/audiorecorder/activities/RecordingActivity.java b/app/src/main/java/com/github/axet/audiorecorder/activities/RecordingActivity.java index f844f44..c555ec2 100644 --- a/app/src/main/java/com/github/axet/audiorecorder/activities/RecordingActivity.java +++ b/app/src/main/java/com/github/axet/audiorecorder/activities/RecordingActivity.java @@ -93,6 +93,8 @@ public class RecordingActivity extends AppCompatActivity { RecordingReceiver receiver; Handler handler = new Handler(); + ShortBuffer dbBuffer = null; + public static void startActivity(Context context, boolean pause) { Intent i = new Intent(context, RecordingActivity.class); if (pause) { @@ -288,12 +290,18 @@ public class RecordingActivity extends AppCompatActivity { int samplesUpdateStereo = samplesUpdate * MainApplication.getChannels(this); pitch.clear(cut / samplesUpdateStereo); - len = len / samplesUpdateStereo * samplesUpdateStereo; // cut right overs (leftovers from right) - for (int i = 0; i < len; i += samplesUpdateStereo) { + int lenUpdate = len / samplesUpdateStereo * samplesUpdateStereo; // cut right overs (leftovers from right) + for (int i = 0; i < lenUpdate; i += samplesUpdateStereo) { double dB = RawSamples.getDB(buf, i, samplesUpdateStereo); pitch.add(dB); } updateSamples(samplesTime); + + int diff = len - lenUpdate; + if (diff > 0) { + dbBuffer = ShortBuffer.allocate(samplesUpdateStereo); + dbBuffer.put(buf, lenUpdate, diff); + } } void pauseButton() { @@ -618,8 +626,6 @@ public class RecordingActivity extends AppCompatActivity { int samplesUpdateStereo = samplesUpdate * MainApplication.getChannels(RecordingActivity.this); - ShortBuffer dbBuffer = null; - while (!Thread.currentThread().isInterrupted()) { synchronized (bufferSizeLock) { if (buffer == null || buffer.length != bufferSize) From cd28d0109045d281ba52c01dc1787d05ba73f51d Mon Sep 17 00:00:00 2001 From: Alexey Kuznetsov Date: Sun, 18 Jun 2017 23:12:09 +0300 Subject: [PATCH 07/13] update br --- app/src/main/res/xml-pt-rBR/pref_general.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/res/xml-pt-rBR/pref_general.xml b/app/src/main/res/xml-pt-rBR/pref_general.xml index 911a72a..56ed82e 100644 --- a/app/src/main/res/xml-pt-rBR/pref_general.xml +++ b/app/src/main/res/xml-pt-rBR/pref_general.xml @@ -61,8 +61,8 @@ + android:summary="Mostrar controles quando a tela estiver bloqueada" + android:title="Controle da tela bloqueada" /> Date: Mon, 19 Jun 2017 22:38:19 +0300 Subject: [PATCH 08/13] cleanups --- .../github/axet/audiorecorder/activities/MainActivity.java | 4 ++-- .../axet/audiorecorder/activities/RecordingActivity.java | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/github/axet/audiorecorder/activities/MainActivity.java b/app/src/main/java/com/github/axet/audiorecorder/activities/MainActivity.java index ba6c30d..78f77ea 100644 --- a/app/src/main/java/com/github/axet/audiorecorder/activities/MainActivity.java +++ b/app/src/main/java/com/github/axet/audiorecorder/activities/MainActivity.java @@ -286,8 +286,8 @@ public class MainActivity extends AppCompatActivity { void updateHeader() { File f = storage.getStoragePath(); - long free = storage.getFree(f); - long sec = storage.average(free); + long free = Storage.getFree(f); + long sec = Storage.average(this, free); TextView text = (TextView) findViewById(R.id.space_left); text.setText(MainApplication.formatFree(this, free, sec)); } diff --git a/app/src/main/java/com/github/axet/audiorecorder/activities/RecordingActivity.java b/app/src/main/java/com/github/axet/audiorecorder/activities/RecordingActivity.java index c555ec2..b2eceff 100644 --- a/app/src/main/java/com/github/axet/audiorecorder/activities/RecordingActivity.java +++ b/app/src/main/java/com/github/axet/audiorecorder/activities/RecordingActivity.java @@ -428,7 +428,7 @@ public class RecordingActivity extends AppCompatActivity { } void setState(String s) { - long free = storage.getFree(storage.getTempRecording()); + long free = Storage.getFree(storage.getTempRecording()); final SharedPreferences shared = PreferenceManager.getDefaultSharedPreferences(this); @@ -647,7 +647,7 @@ public class RecordingActivity extends AppCompatActivity { if (stableRefresh || diff >= samples) { stableRefresh = true; - rs.write(buffer, readSize); + rs.write(buffer, 0, readSize); short[] dbBuf; int readSizeUpdate; @@ -672,6 +672,8 @@ public class RecordingActivity extends AppCompatActivity { if (readSizeLen > 0) { dbBuffer = ShortBuffer.allocate(samplesUpdateStereo); dbBuffer.put(dbBuf, readSizeUpdate, readSizeLen); + } else { + dbBuffer = null; } samplesTime += samples; From 4d0f90ca038d1bdaa8d8fbce844c3a96024aaca5 Mon Sep 17 00:00:00 2001 From: Alexey Kuznetsov Date: Mon, 19 Jun 2017 22:38:29 +0300 Subject: [PATCH 09/13] cleanups --- .../axet/audiorecorder/services/RecordingService.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/github/axet/audiorecorder/services/RecordingService.java b/app/src/main/java/com/github/axet/audiorecorder/services/RecordingService.java index a5e8b3a..baebb14 100644 --- a/app/src/main/java/com/github/axet/audiorecorder/services/RecordingService.java +++ b/app/src/main/java/com/github/axet/audiorecorder/services/RecordingService.java @@ -44,7 +44,7 @@ public class RecordingService extends Service { public static String PAUSE_BUTTON = RecordingService.class.getCanonicalName() + ".PAUSE_BUTTON"; public static String RECORD_BUTTON = RecordingService.class.getCanonicalName() + ".RECORD_BUTTON"; - Storage storage = new Storage(this); + Storage storage = new Storage(this); // for storage path public static void startIfEnabled(Context context) { SharedPreferences shared = PreferenceManager.getDefaultSharedPreferences(context); @@ -160,8 +160,8 @@ public class RecordingService extends Service { if (targetFile == null) { title = getString(R.string.app_name); File f = storage.getStoragePath(); - long free = storage.getFree(f); - long sec = storage.average(free); + long free = Storage.getFree(f); + long sec = Storage.average(this, free); text = MainApplication.formatFree(this, free, sec); view.setViewVisibility(R.id.notification_record, View.VISIBLE); view.setOnClickPendingIntent(R.id.notification_record, re); From b252490b83f1f7714944976717aff0bb2ffb366c Mon Sep 17 00:00:00 2001 From: Alexey Kuznetsov Date: Tue, 20 Jun 2017 12:21:44 +0300 Subject: [PATCH 10/13] fix updateing pitch view --- app/build.gradle | 2 +- .../activities/RecordingActivity.java | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 6d41096..0c7c8c6 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -43,5 +43,5 @@ android { dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' - compile 'com.github.axet:android-audio-library:0.0.56' // compile project(':android-audio-library') + compile 'com.github.axet:android-audio-library:0.0.58' // compile project(':android-audio-library') } diff --git a/app/src/main/java/com/github/axet/audiorecorder/activities/RecordingActivity.java b/app/src/main/java/com/github/axet/audiorecorder/activities/RecordingActivity.java index b2eceff..2e4a15b 100644 --- a/app/src/main/java/com/github/axet/audiorecorder/activities/RecordingActivity.java +++ b/app/src/main/java/com/github/axet/audiorecorder/activities/RecordingActivity.java @@ -48,6 +48,8 @@ import com.github.axet.audiorecorder.app.Storage; import com.github.axet.audiorecorder.services.RecordingService; import java.io.File; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; import java.nio.ShortBuffer; public class RecordingActivity extends AppCompatActivity { @@ -652,13 +654,18 @@ public class RecordingActivity extends AppCompatActivity { short[] dbBuf; int readSizeUpdate; if (dbBuffer != null) { - dbBuffer.put(buffer, 0, readSize); - readSizeUpdate = dbBuffer.position() / samplesUpdateStereo * samplesUpdateStereo; - dbBuf = dbBuffer.array(); + ShortBuffer bb = ShortBuffer.allocate(dbBuffer.position() + readSize); + dbBuffer.flip(); + bb.put(dbBuffer); + bb.put(buffer, 0, readSize); + dbBuf = new short[bb.position()]; + readSize = dbBuf.length; + bb.flip(); + bb.get(dbBuf, 0, dbBuf.length); } else { dbBuf = buffer; - readSizeUpdate = readSize / samplesUpdateStereo * samplesUpdateStereo; } + readSizeUpdate = readSize / samplesUpdateStereo * samplesUpdateStereo; for (int i = 0; i < readSizeUpdate; i += samplesUpdateStereo) { final double dB = RawSamples.getDB(dbBuf, i, samplesUpdateStereo); handle.post(new Runnable() { @@ -670,7 +677,7 @@ public class RecordingActivity extends AppCompatActivity { } int readSizeLen = readSize - readSizeUpdate; if (readSizeLen > 0) { - dbBuffer = ShortBuffer.allocate(samplesUpdateStereo); + dbBuffer = ShortBuffer.allocate(readSizeLen); dbBuffer.put(dbBuf, readSizeUpdate, readSizeLen); } else { dbBuffer = null; From 9edf8e69288ebae6e54b08b3abbac9b8c92f05b8 Mon Sep 17 00:00:00 2001 From: Alexey Kuznetsov Date: Tue, 20 Jun 2017 12:31:54 +0300 Subject: [PATCH 11/13] check penging --- app/src/main/AndroidManifest.xml | 24 +++++++++++++ .../services/OnBootReceiver.java | 16 +++++++++ .../services/OnExternalReceiver.java | 34 +++++++++++++++++++ .../services/OnUpgradeReceiver.java | 19 +++++++++++ .../services/RecordingService.java | 9 +++++ 5 files changed, 102 insertions(+) create mode 100644 app/src/main/java/com/github/axet/audiorecorder/services/OnBootReceiver.java create mode 100644 app/src/main/java/com/github/axet/audiorecorder/services/OnExternalReceiver.java create mode 100644 app/src/main/java/com/github/axet/audiorecorder/services/OnUpgradeReceiver.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index cab2c37..d309902 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -7,6 +7,7 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/java/com/github/axet/audiorecorder/services/OnBootReceiver.java b/app/src/main/java/com/github/axet/audiorecorder/services/OnBootReceiver.java new file mode 100644 index 0000000..e07c680 --- /dev/null +++ b/app/src/main/java/com/github/axet/audiorecorder/services/OnBootReceiver.java @@ -0,0 +1,16 @@ +package com.github.axet.audiorecorder.services; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.util.Log; + +public class OnBootReceiver extends BroadcastReceiver { + String TAG = OnBootReceiver.class.getSimpleName(); + + @Override + public void onReceive(Context context, Intent i) { + Log.d(TAG, "onReceive"); + RecordingService.startIfEnabledPending(context); + } +} diff --git a/app/src/main/java/com/github/axet/audiorecorder/services/OnExternalReceiver.java b/app/src/main/java/com/github/axet/audiorecorder/services/OnExternalReceiver.java new file mode 100644 index 0000000..47a77de --- /dev/null +++ b/app/src/main/java/com/github/axet/audiorecorder/services/OnExternalReceiver.java @@ -0,0 +1,34 @@ +package com.github.axet.audiorecorder.services; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageInfo; +import android.content.pm.PackageManager; +import android.util.Log; + +public class OnExternalReceiver extends BroadcastReceiver { + String TAG = OnExternalReceiver.class.getSimpleName(); + + boolean isExternal(Context context) { + PackageManager pm = context.getPackageManager(); + try { + PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0); + ApplicationInfo ai = pi.applicationInfo; + return (ai.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) == ApplicationInfo.FLAG_EXTERNAL_STORAGE; + } catch (PackageManager.NameNotFoundException ignore) { + } + return false; + } + + @Override + public void onReceive(Context context, Intent intent) { + Log.d(TAG, "onReceive"); + + if (!isExternal(context)) + return; + + RecordingService.startIfEnabledPending(context); + } +} diff --git a/app/src/main/java/com/github/axet/audiorecorder/services/OnUpgradeReceiver.java b/app/src/main/java/com/github/axet/audiorecorder/services/OnUpgradeReceiver.java new file mode 100644 index 0000000..a92f189 --- /dev/null +++ b/app/src/main/java/com/github/axet/audiorecorder/services/OnUpgradeReceiver.java @@ -0,0 +1,19 @@ +package com.github.axet.audiorecorder.services; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.util.Log; + +/** + * http://stackoverflow.com/questions/2133986 + */ +public class OnUpgradeReceiver extends BroadcastReceiver { + String TAG = OnUpgradeReceiver.class.getSimpleName(); + + @Override + public void onReceive(Context context, Intent intent) { + Log.d(TAG, "onReceive"); + RecordingService.startIfEnabledPending(context); + } +} diff --git a/app/src/main/java/com/github/axet/audiorecorder/services/RecordingService.java b/app/src/main/java/com/github/axet/audiorecorder/services/RecordingService.java index baebb14..1f353b5 100644 --- a/app/src/main/java/com/github/axet/audiorecorder/services/RecordingService.java +++ b/app/src/main/java/com/github/axet/audiorecorder/services/RecordingService.java @@ -53,6 +53,15 @@ public class RecordingService extends Service { start(context); } + public static void startIfEnabledPending(Context context) { + Storage s = new Storage(context); + if (s.recordingPending()) { + RecordingActivity.startActivity(context, true); + return; + } + startIfEnabled(context); + } + public static void start(Context context) { context.startService(new Intent(context, RecordingService.class)); } From 7e050716a05b0ca47d23c48d7ca00e2ce38ea642 Mon Sep 17 00:00:00 2001 From: Alexey Kuznetsov Date: Tue, 20 Jun 2017 12:54:59 +0300 Subject: [PATCH 12/13] check pending on lock --- app/src/main/AndroidManifest.xml | 2 +- .../activities/RecordingActivity.java | 20 ++++++++++++++++--- .../audiorecorder/app/MainApplication.java | 1 + .../services/RecordingService.java | 11 ++++++---- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index d309902..04560ae 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -7,7 +7,7 @@ - + Date: Tue, 20 Jun 2017 12:55:11 +0300 Subject: [PATCH 13/13] Bump version audiorecorder-2.0.0 --- app/build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 0c7c8c6..c77731f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -8,8 +8,8 @@ android { applicationId "com.github.axet.audiorecorder" minSdkVersion 9 targetSdkVersion 23 - versionCode 154 - versionName "1.7.4" + versionCode 155 + versionName "2.0.0" } signingConfigs { release {