From 2f3f9b9f06e2d6cb35d5185e19dded092d593065 Mon Sep 17 00:00:00 2001 From: petrnalevka Date: Fri, 7 Aug 2020 17:28:18 +0200 Subject: [PATCH] Initial commit --- app/build.gradle | 46 +++++ app/proguard-rules.pro | 21 +++ .../ExampleInstrumentedTest.kt | 22 +++ app/src/main/AndroidManifest.xml | 35 ++++ .../recordforeground/MainActivity.kt | 61 +++++++ .../recordforeground/RecordingService.kt | 107 +++++++++++ .../drawable-v24/ic_launcher_foreground.xml | 30 ++++ .../res/drawable/ic_launcher_background.xml | 170 ++++++++++++++++++ app/src/main/res/layout/activity_main.xml | 42 +++++ app/src/main/res/menu/menu_main.xml | 10 ++ .../res/mipmap-anydpi-v26/ic_launcher.xml | 5 + .../mipmap-anydpi-v26/ic_launcher_round.xml | 5 + app/src/main/res/mipmap-hdpi/ic_launcher.png | Bin 0 -> 3593 bytes .../res/mipmap-hdpi/ic_launcher_round.png | Bin 0 -> 5339 bytes app/src/main/res/mipmap-mdpi/ic_launcher.png | Bin 0 -> 2636 bytes .../res/mipmap-mdpi/ic_launcher_round.png | Bin 0 -> 3388 bytes app/src/main/res/mipmap-xhdpi/ic_launcher.png | Bin 0 -> 4926 bytes .../res/mipmap-xhdpi/ic_launcher_round.png | Bin 0 -> 7472 bytes .../main/res/mipmap-xxhdpi/ic_launcher.png | Bin 0 -> 7909 bytes .../res/mipmap-xxhdpi/ic_launcher_round.png | Bin 0 -> 11873 bytes .../main/res/mipmap-xxxhdpi/ic_launcher.png | Bin 0 -> 10652 bytes .../res/mipmap-xxxhdpi/ic_launcher_round.png | Bin 0 -> 16570 bytes app/src/main/res/navigation/nav_graph.xml | 28 +++ app/src/main/res/values/colors.xml | 6 + app/src/main/res/values/dimens.xml | 3 + app/src/main/res/values/strings.xml | 12 ++ app/src/main/res/values/styles.xml | 19 ++ .../recordforeground/ExampleUnitTest.kt | 16 ++ build.gradle | 26 +++ gradle.properties | 21 +++ gradle/wrapper/gradle-wrapper.properties | 6 + settings.gradle | 2 + 32 files changed, 693 insertions(+) create mode 100644 app/build.gradle create mode 100644 app/proguard-rules.pro create mode 100644 app/src/androidTest/java/com/urbandroid/recordforeground/ExampleInstrumentedTest.kt create mode 100644 app/src/main/AndroidManifest.xml create mode 100644 app/src/main/java/com/urbandroid/recordforeground/MainActivity.kt create mode 100644 app/src/main/java/com/urbandroid/recordforeground/RecordingService.kt create mode 100644 app/src/main/res/drawable-v24/ic_launcher_foreground.xml create mode 100644 app/src/main/res/drawable/ic_launcher_background.xml create mode 100644 app/src/main/res/layout/activity_main.xml create mode 100644 app/src/main/res/menu/menu_main.xml create mode 100644 app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml create mode 100644 app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml create mode 100644 app/src/main/res/mipmap-hdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-hdpi/ic_launcher_round.png create mode 100644 app/src/main/res/mipmap-mdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-mdpi/ic_launcher_round.png create mode 100644 app/src/main/res/mipmap-xhdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xhdpi/ic_launcher_round.png create mode 100644 app/src/main/res/mipmap-xxhdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png create mode 100644 app/src/main/res/mipmap-xxxhdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png create mode 100644 app/src/main/res/navigation/nav_graph.xml create mode 100644 app/src/main/res/values/colors.xml create mode 100644 app/src/main/res/values/dimens.xml create mode 100644 app/src/main/res/values/strings.xml create mode 100644 app/src/main/res/values/styles.xml create mode 100644 app/src/test/java/com/urbandroid/recordforeground/ExampleUnitTest.kt create mode 100644 build.gradle create mode 100644 gradle.properties create mode 100644 gradle/wrapper/gradle-wrapper.properties create mode 100644 settings.gradle diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 0000000..3b6725f --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,46 @@ +apply plugin: 'com.android.application' +apply plugin: 'kotlin-android' +apply plugin: 'kotlin-android-extensions' + +android { + compileSdkVersion 30 + + defaultConfig { + applicationId "com.urbandroid.recordforeground" + minSdkVersion 26 + targetSdkVersion 30 + versionCode 1 + versionName "1.0" + + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } + kotlinOptions { + jvmTarget = '1.8' + } +} + +dependencies { + implementation fileTree(dir: "libs", include: ["*.jar"]) + implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" + implementation 'androidx.core:core-ktx:1.3.1' + implementation 'androidx.appcompat:appcompat:1.2.0' + implementation 'com.google.android.material:material:1.1.0' + implementation 'androidx.constraintlayout:constraintlayout:1.1.3' + implementation 'androidx.navigation:navigation-fragment-ktx:2.3.0' + implementation 'androidx.navigation:navigation-ui-ktx:2.3.0' + testImplementation 'junit:junit:4.12' + androidTestImplementation 'androidx.test.ext:junit:1.1.1' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' + +} \ No newline at end of file diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..481bb43 --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile \ No newline at end of file diff --git a/app/src/androidTest/java/com/urbandroid/recordforeground/ExampleInstrumentedTest.kt b/app/src/androidTest/java/com/urbandroid/recordforeground/ExampleInstrumentedTest.kt new file mode 100644 index 0000000..201397e --- /dev/null +++ b/app/src/androidTest/java/com/urbandroid/recordforeground/ExampleInstrumentedTest.kt @@ -0,0 +1,22 @@ +package com.urbandroid.recordforeground + +import androidx.test.ext.junit.runners.AndroidJUnit4 +import androidx.test.platform.app.InstrumentationRegistry +import org.junit.Assert.assertEquals +import org.junit.Test +import org.junit.runner.RunWith + +/** + * Instrumented test, which will execute on an Android device. + * + * See [testing documentation](http://d.android.com/tools/testing). + */ +@RunWith(AndroidJUnit4::class) +class ExampleInstrumentedTest { + @Test + fun useAppContext() { + // Context of the app under test. + val appContext = InstrumentationRegistry.getInstrumentation().targetContext + assertEquals("com.urbandroid.recordforeground", appContext.packageName) + } +} \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..539f9e4 --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/urbandroid/recordforeground/MainActivity.kt b/app/src/main/java/com/urbandroid/recordforeground/MainActivity.kt new file mode 100644 index 0000000..63a8922 --- /dev/null +++ b/app/src/main/java/com/urbandroid/recordforeground/MainActivity.kt @@ -0,0 +1,61 @@ +package com.urbandroid.recordforeground + +import android.Manifest +import android.app.AlarmManager +import android.app.PendingIntent +import android.content.Context +import android.content.Intent +import android.os.Bundle +import android.util.Log +import android.view.Menu +import android.view.MenuItem +import android.widget.Toast +import androidx.appcompat.app.AppCompatActivity +import kotlinx.android.synthetic.main.activity_main.* +import java.util.* +import java.util.concurrent.TimeUnit + +const val TAG : String = "RecordForeground" + +const val NOTIFICATION_CHANNEL_FOREGROUND = "foreground" + +class MainActivity : AppCompatActivity() { + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_main) + setSupportActionBar(findViewById(R.id.toolbar)) + + startLater.setOnClickListener { + val time = System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(10) + Log.i(TAG, "Scheduling start ${Date(time)}") + val am = getSystemService(Context.ALARM_SERVICE) as AlarmManager + am.setExactAndAllowWhileIdle(AlarmManager.RTC, time, PendingIntent.getForegroundService(this, 0, Intent(this, RecordingService::class.java), PendingIntent.FLAG_UPDATE_CURRENT)) + + Toast.makeText(this, "Foreground service will start in a minute", Toast.LENGTH_LONG).show() + finish() + } + + startNow.setOnClickListener { + startForegroundService(Intent(this, RecordingService::class.java)) + } + + requestPermissions(arrayOf(Manifest.permission.RECORD_AUDIO), 0) + } + + override fun onCreateOptionsMenu(menu: Menu): Boolean { + // Inflate the menu; this adds items to the action bar if it is present. + menuInflater.inflate(R.menu.menu_main, menu) + return true + } + + override fun onOptionsItemSelected(item: MenuItem): Boolean { + // Handle action bar item clicks here. The action bar will + // automatically handle clicks on the Home/Up button, so long + // as you specify a parent activity in AndroidManifest.xml. + return when (item.itemId) { + R.id.action_settings -> true + else -> super.onOptionsItemSelected(item) + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/urbandroid/recordforeground/RecordingService.kt b/app/src/main/java/com/urbandroid/recordforeground/RecordingService.kt new file mode 100644 index 0000000..6f8e42d --- /dev/null +++ b/app/src/main/java/com/urbandroid/recordforeground/RecordingService.kt @@ -0,0 +1,107 @@ +package com.urbandroid.recordforeground + +import android.app.NotificationChannel +import android.app.NotificationManager +import android.app.PendingIntent +import android.app.Service +import android.content.Context +import android.content.Intent +import android.media.AudioFormat +import android.media.AudioRecord +import android.media.MediaRecorder +import android.net.Uri +import android.os.Build +import android.os.IBinder +import android.util.Log +import androidx.core.app.NotificationCompat +import androidx.core.content.ContextCompat + +class RecordingService : Service() { + + override fun onBind(p0: Intent?): IBinder? { + return null + } + + override fun onCreate() { + super.onCreate() + + Log.i(TAG, "Foreground service created") + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + + val channelName = "Foreground" + val importance = NotificationManager.IMPORTANCE_LOW + val notificationChannel = NotificationChannel(NOTIFICATION_CHANNEL_FOREGROUND, channelName, importance) + notificationChannel.setShowBadge(false) + notificationManager.createNotificationChannel(notificationChannel); + } + + val i = Intent(this, MainActivity::class.java) + i.flags = Intent.FLAG_ACTIVITY_NEW_TASK + val pi = PendingIntent.getActivity(this, 4242, i, PendingIntent.FLAG_UPDATE_CURRENT) + + val context = this + + var intent = Intent() + + if (Build.VERSION.SDK_INT >= 26) { + intent = Intent(android.provider.Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS) + intent.putExtra(android.provider.Settings.EXTRA_CHANNEL_ID, NOTIFICATION_CHANNEL_FOREGROUND) + intent.putExtra(android.provider.Settings.EXTRA_APP_PACKAGE, context.packageName) + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + } else if (Build.VERSION.SDK_INT >= 23) { + intent.setClassName("com.android.settings", "com.android.settings.Settings\$AppNotificationSettingsActivity") + intent.putExtra("app_package", context.packageName) + intent.putExtra("app_uid", context.applicationInfo.uid) + + if (Build.VERSION.SDK_INT >= 26) { + intent.putExtra(android.provider.Settings.EXTRA_CHANNEL_ID, NOTIFICATION_CHANNEL_FOREGROUND) + } + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + + intent.putExtra("android.provider.extra.APP_PACKAGE", context.packageName) + } else if (Build.VERSION.SDK_INT > 16) { + try { + intent = Intent() + intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + intent.action = android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS + val uri = Uri.fromParts("package", context.packageName, null) + intent.data = uri + } catch (e: Exception) { + Log.e(TAG, "Error", e) + } + + } + + val notificationBuilder = NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_FOREGROUND) + .setChannelId(NOTIFICATION_CHANNEL_FOREGROUND) + .setColor(ContextCompat.getColor(applicationContext, R.color.colorAccent)) + .setContentIntent(pi) + .setShowWhen(false) + .setContentText("Running") + + startForeground(2342, notificationBuilder.build()) + + Log.i(TAG, "Foreground service started") + + val minBufferSize = AudioRecord.getMinBufferSize(44100, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT) + + val recorder = AudioRecord( + MediaRecorder.AudioSource.MIC, + 44100, + AudioFormat.CHANNEL_IN_MONO, + AudioFormat.ENCODING_PCM_16BIT, + minBufferSize) + + if (recorder.state == AudioRecord.STATE_UNINITIALIZED) { + // this condition is true even this is called from a process with an active foreground service + Log.i(TAG, "Recorder uninitialized FAILED") + } else { + Log.i(TAG, "Recorder initialized SUCCESS") + } + + recorder.release() + stopSelf() + } +} \ No newline at end of file diff --git a/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 0000000..2b068d1 --- /dev/null +++ b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_launcher_background.xml b/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..07d5da9 --- /dev/null +++ b/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..ced2208 --- /dev/null +++ b/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,42 @@ + + + + + + + + + +